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" |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 40 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 42 | #include <cctype> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 43 | #include <deque> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 44 | #include <set> |
| 45 | #include <string> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 46 | #include <vector> |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 49 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 51 | namespace { |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 52 | /// \brief Helper types for tracking macro definitions. |
| 53 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 54 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 55 | |
| 56 | struct MCAsmMacroParameter { |
| 57 | StringRef Name; |
| 58 | MCAsmMacroArgument Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 59 | bool Required; |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 60 | bool Vararg; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 61 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 62 | MCAsmMacroParameter() : Required(false), Vararg(false) {} |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 65 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 66 | |
| 67 | struct MCAsmMacro { |
| 68 | StringRef Name; |
| 69 | StringRef Body; |
| 70 | MCAsmMacroParameters Parameters; |
| 71 | |
| 72 | public: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 73 | MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P) |
| 74 | : Name(N), Body(B), Parameters(std::move(P)) {} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 77 | /// \brief Helper class for storing information about an active macro |
| 78 | /// instantiation. |
| 79 | struct MacroInstantiation { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 80 | /// The location of the instantiation. |
| 81 | SMLoc InstantiationLoc; |
| 82 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 83 | /// The buffer where parsing should resume upon instantiation completion. |
| 84 | int ExitBuffer; |
| 85 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 86 | /// The location where parsing should resume upon instantiation completion. |
| 87 | SMLoc ExitLoc; |
| 88 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 89 | /// The depth of TheCondStack at the start of the instantiation. |
| 90 | size_t CondStackDepth; |
| 91 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 92 | public: |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 93 | MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 96 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 97 | /// \brief The parsed operands from the last parsed statement. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 98 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 99 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 100 | /// \brief The opcode from the last parsed instruction. |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 101 | unsigned Opcode; |
| 102 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 103 | /// \brief Was there an error parsing the inline assembly? |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 104 | bool ParseError; |
| 105 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 106 | SmallVectorImpl<AsmRewrite> *AsmRewrites; |
| 107 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 108 | ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 109 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 110 | : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 113 | /// \brief The concrete assembly parser instance. |
| 114 | class AsmParser : public MCAsmParser { |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 115 | AsmParser(const AsmParser &) = delete; |
| 116 | void operator=(const AsmParser &) = delete; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 117 | private: |
| 118 | AsmLexer Lexer; |
| 119 | MCContext &Ctx; |
| 120 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 121 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 122 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 123 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 124 | void *SavedDiagContext; |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 125 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 127 | /// This is the current buffer index we're lexing from as managed by the |
| 128 | /// SourceMgr object. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 129 | unsigned CurBuffer; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 130 | |
| 131 | AsmCond TheCondState; |
| 132 | std::vector<AsmCond> TheCondStack; |
| 133 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 134 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 135 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 136 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 137 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 138 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 139 | /// \brief Map of currently defined macros. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 140 | StringMap<MCAsmMacro> MacroMap; |
Daniel Dunbar | c1f58ec | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 141 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 142 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 143 | std::vector<MacroInstantiation*> ActiveMacros; |
| 144 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 145 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 146 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 147 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 148 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 149 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 150 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 151 | /// \brief Keeps track of how many .macro's have been instantiated. |
| 152 | unsigned NumOfMacroInstantiations; |
| 153 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 154 | /// Flag tracking whether any errors have been encountered. |
| 155 | unsigned HadError : 1; |
| 156 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 157 | /// The values from the last parsed cpp hash file line comment if any. |
| 158 | StringRef CppHashFilename; |
| 159 | int64_t CppHashLineNumber; |
| 160 | SMLoc CppHashLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 161 | unsigned CppHashBuf; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 162 | /// When generating dwarf for assembly source files we need to calculate the |
| 163 | /// 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] | 164 | /// 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] | 165 | /// cache we save the last info we queried with SrcMgr.FindLineNumber(). |
| 166 | SMLoc LastQueryIDLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 167 | unsigned LastQueryBuffer; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 168 | unsigned LastQueryLine; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 169 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 170 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| 171 | unsigned AssemblerDialect; |
| 172 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 173 | /// \brief is Darwin compatibility enabled? |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 174 | bool IsDarwin; |
| 175 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 176 | /// \brief Are we parsing ms-style inline assembly? |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 177 | bool ParsingInlineAsm; |
| 178 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 179 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 180 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 181 | const MCAsmInfo &MAI); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 182 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 183 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 184 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 185 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 186 | void addDirectiveHandler(StringRef Directive, |
| 187 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 188 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 191 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 192 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 193 | } |
| 194 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 195 | public: |
| 196 | /// @name MCAsmParser Interface |
| 197 | /// { |
| 198 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 199 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 200 | MCAsmLexer &getLexer() override { return Lexer; } |
| 201 | MCContext &getContext() override { return Ctx; } |
| 202 | MCStreamer &getStreamer() override { return Out; } |
| 203 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 204 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 205 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 206 | else |
| 207 | return AssemblerDialect; |
| 208 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 209 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 210 | AssemblerDialect = i; |
| 211 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 212 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 213 | void Note(SMLoc L, const Twine &Msg, |
| 214 | ArrayRef<SMRange> Ranges = None) override; |
| 215 | bool Warning(SMLoc L, const Twine &Msg, |
| 216 | ArrayRef<SMRange> Ranges = None) override; |
| 217 | bool Error(SMLoc L, const Twine &Msg, |
| 218 | ArrayRef<SMRange> Ranges = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 219 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 220 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 221 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 222 | void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; } |
| 223 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 224 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 225 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 226 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 227 | SmallVectorImpl<std::pair<void *,bool> > &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 228 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 229 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 230 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 231 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 232 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 233 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 234 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 235 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 236 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 237 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 238 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 239 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 240 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 241 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 242 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 243 | bool parseIdentifier(StringRef &Res) override; |
| 244 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 245 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 246 | void checkForValidSection() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 247 | /// } |
| 248 | |
| 249 | private: |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 250 | |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 251 | bool parseStatement(ParseStatementInfo &Info, |
| 252 | MCAsmParserSemaCallback *SI); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 253 | void eatToEndOfLine(); |
| 254 | bool parseCppHashLineFilenameComment(const SMLoc &L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 255 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 256 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 257 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 258 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 259 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 260 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 261 | const SMLoc &L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 262 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 263 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 264 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 265 | |
| 266 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 267 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 268 | |
| 269 | /// \brief Lookup a previously defined macro. |
| 270 | /// \param Name Macro name. |
| 271 | /// \returns Pointer to macro. NULL if no such macro was defined. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 272 | const MCAsmMacro* lookupMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 273 | |
| 274 | /// \brief Define a new macro with the given name and information. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 275 | void defineMacro(StringRef Name, MCAsmMacro Macro); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 276 | |
| 277 | /// \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] | 278 | void undefineMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 279 | |
| 280 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 281 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 282 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 283 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 284 | /// |
| 285 | /// \param M The macro. |
| 286 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 287 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 288 | |
| 289 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 290 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 291 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 292 | /// \brief Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 293 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 294 | |
| 295 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 296 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 297 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 298 | void printMacroInstantiations(); |
| 299 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 300 | ArrayRef<SMRange> Ranges = None) const { |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 301 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 302 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 303 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 304 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 305 | /// \brief Enter the specified file. This returns true on failure. |
| 306 | bool enterIncludeFile(const std::string &Filename); |
| 307 | |
| 308 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 309 | /// This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 310 | bool processIncbinFile(const std::string &Filename); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 311 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 312 | /// \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] | 313 | /// current token is not set; clients should ensure Lex() is called |
| 314 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 315 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 316 | /// \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] | 317 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 318 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 319 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 320 | /// \brief Parse up to the end of statement and a return the contents from the |
| 321 | /// current token until the end of the statement; the current token on exit |
| 322 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 323 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 324 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 325 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 326 | /// 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] | 327 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 328 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 329 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 330 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 331 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 332 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 333 | MCBinaryExpr::Opcode &Kind); |
| 334 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 335 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 336 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 337 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 338 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 339 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 340 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 341 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 342 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 343 | DK_NO_DIRECTIVE, // Placeholder |
| 344 | DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 345 | DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA, |
| 346 | 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] | 347 | 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] | 348 | DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 349 | DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 350 | DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, |
| 351 | DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, |
| 352 | DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, |
| 353 | 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] | 354 | 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] | 355 | DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF, |
| 356 | DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 357 | DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, |
| 358 | DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, |
| 359 | DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, |
| 360 | DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, |
| 361 | DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, |
| 362 | 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] | 363 | DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE, |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 364 | DK_MACROS_ON, DK_MACROS_OFF, |
| 365 | DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 366 | DK_SLEB128, DK_ULEB128, |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 367 | DK_ERR, DK_ERROR, DK_WARNING, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 368 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 371 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 372 | /// directives parsed by this class. |
| 373 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 374 | |
| 375 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 376 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
| 377 | bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 378 | bool parseDirectiveOctaValue(); // ".octa" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 379 | bool parseDirectiveRealValue(const fltSemantics &); // ".single", ... |
| 380 | bool parseDirectiveFill(); // ".fill" |
| 381 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 382 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 383 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 384 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 385 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 386 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 387 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 388 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 389 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 390 | bool parseDirectiveLine(); |
| 391 | bool parseDirectiveLoc(); |
| 392 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 393 | |
| 394 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 395 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 396 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 397 | bool parseDirectiveCFISections(); |
| 398 | bool parseDirectiveCFIStartProc(); |
| 399 | bool parseDirectiveCFIEndProc(); |
| 400 | bool parseDirectiveCFIDefCfaOffset(); |
| 401 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 402 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 403 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 404 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 405 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 406 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 407 | bool parseDirectiveCFIRememberState(); |
| 408 | bool parseDirectiveCFIRestoreState(); |
| 409 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 410 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 411 | bool parseDirectiveCFIEscape(); |
| 412 | bool parseDirectiveCFISignalFrame(); |
| 413 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 414 | |
| 415 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 416 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 417 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 418 | bool parseDirectiveEndMacro(StringRef Directive); |
| 419 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 420 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 421 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 422 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 423 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 424 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 425 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 426 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 427 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 428 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 429 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 430 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 431 | |
| 432 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 433 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 434 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 435 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 436 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 437 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 438 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 439 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 440 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 441 | bool parseDirectiveAbort(); // ".abort" |
| 442 | bool parseDirectiveInclude(); // ".include" |
| 443 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 444 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 445 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 446 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 447 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 448 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 449 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 450 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 451 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 452 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 453 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 454 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 455 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 456 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 457 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 458 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 459 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 460 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 461 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 462 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 463 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 464 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 465 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 466 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 467 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 468 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 469 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 470 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 471 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 472 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 473 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 474 | size_t Len); |
| 475 | |
| 476 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 477 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 478 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 479 | // "end" |
| 480 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 481 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 482 | // ".err" or ".error" |
| 483 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 484 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 485 | // ".warning" |
| 486 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 487 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 488 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 489 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 490 | } |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 491 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 492 | namespace llvm { |
| 493 | |
| 494 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 495 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 496 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 497 | |
| 498 | } |
| 499 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 500 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 501 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 502 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
| 503 | const MCAsmInfo &MAI) |
| 504 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
| 505 | PlatformParser(nullptr), CurBuffer(SM.getMainFileID()), |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 506 | MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0), |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 507 | AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 508 | // Save the old handler. |
| 509 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 510 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 511 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 512 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 513 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 514 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 515 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 516 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 517 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 518 | PlatformParser.reset(createCOFFAsmParser()); |
| 519 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 520 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 521 | PlatformParser.reset(createDarwinAsmParser()); |
| 522 | IsDarwin = true; |
| 523 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 524 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 525 | PlatformParser.reset(createELFAsmParser()); |
| 526 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 527 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 528 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 529 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 530 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 531 | |
| 532 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 535 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 536 | assert((HadError || ActiveMacros.empty()) && |
| 537 | "Unexpected active macro instantiation!"); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 540 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 541 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 542 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 543 | it = ActiveMacros.rbegin(), |
| 544 | ie = ActiveMacros.rend(); |
| 545 | it != ie; ++it) |
| 546 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 547 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 550 | void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
| 551 | printMessage(L, SourceMgr::DK_Note, Msg, Ranges); |
| 552 | printMacroInstantiations(); |
| 553 | } |
| 554 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 555 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 556 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 557 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 558 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 559 | return Error(L, Msg, Ranges); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 560 | printMessage(L, SourceMgr::DK_Warning, Msg, Ranges); |
| 561 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 562 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 565 | bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 566 | HadError = true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 567 | printMessage(L, SourceMgr::DK_Error, Msg, Ranges); |
| 568 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 572 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 573 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 574 | unsigned NewBuf = |
| 575 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 576 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 577 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 578 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 579 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 580 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 583 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 584 | /// 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] | 585 | /// 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] | 586 | /// returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 587 | bool AsmParser::processIncbinFile(const std::string &Filename) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 588 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 589 | unsigned NewBuf = |
| 590 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 591 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 592 | return true; |
| 593 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 594 | // Pick up the bytes from the file and emit them. |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 595 | getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer()); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 599 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 600 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 601 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 602 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 605 | const AsmToken &AsmParser::Lex() { |
| 606 | const AsmToken *tok = &Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 607 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 608 | if (tok->is(AsmToken::Eof)) { |
| 609 | // If this is the end of an included file, pop the parent file off the |
| 610 | // include stack. |
| 611 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 612 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 613 | jumpToLoc(ParentIncludeLoc); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 614 | tok = &Lexer.Lex(); |
| 615 | } |
| 616 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 617 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 618 | if (tok->is(AsmToken::Error)) |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 619 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
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 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 624 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 625 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 626 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 627 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 629 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 630 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 631 | |
| 632 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 633 | AsmCond StartingCondState = TheCondState; |
| 634 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 635 | // If we are generating dwarf for assembly source files save the initial text |
| 636 | // section and generate a .file directive. |
| 637 | if (getContext().getGenDwarfForAssembly()) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 638 | MCSection *Sec = getStreamer().getCurrentSection().first; |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 639 | if (!Sec->getBeginSymbol()) { |
| 640 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 641 | getStreamer().EmitLabel(SectionStartSym); |
| 642 | Sec->setBeginSymbol(SectionStartSym); |
| 643 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 644 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 645 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 646 | (void)InsertResult; |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 647 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 648 | 0, StringRef(), getContext().getMainFileName())); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 651 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 652 | while (Lexer.isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 653 | ParseStatementInfo Info; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 654 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 655 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 656 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 657 | // We had an error, validate that one was emitted and recover by skipping to |
| 658 | // the next line. |
| 659 | assert(HadError && "Parse statement returned an error, but none emitted!"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 660 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 661 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 662 | |
| 663 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 664 | TheCondState.Ignore != StartingCondState.Ignore) |
| 665 | return TokError("unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 666 | |
| 667 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 668 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 669 | if (!LineTables.empty()) { |
| 670 | unsigned Index = 0; |
| 671 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 672 | if (File.Name.empty() && Index != 0) |
| 673 | TokError("unassigned file number: " + Twine(Index) + |
| 674 | " for .file directives"); |
| 675 | ++Index; |
| 676 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 677 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 678 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 679 | // Check to see that all assembler local symbols were actually defined. |
| 680 | // Targets that don't do subsections via symbols may not want this, though, |
| 681 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 682 | // as otherwise we won't necessarilly have seen everything yet. |
| 683 | if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { |
| 684 | const MCContext::SymbolTable &Symbols = getContext().getSymbols(); |
| 685 | for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 686 | e = Symbols.end(); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 687 | i != e; ++i) { |
| 688 | MCSymbol *Sym = i->getValue(); |
| 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. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame^] | 696 | printMessage(getLexer().getLoc(), SourceMgr::DK_Error, |
| 697 | "assembler local symbol '" + Sym->getName() + |
| 698 | "' not defined"); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 702 | // Finalize the output stream if there are no errors and if the client wants |
| 703 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 704 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 705 | Out.Finish(); |
| 706 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 707 | return HadError; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 710 | void AsmParser::checkForValidSection() { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 711 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 712 | TokError("expected section directive before assembly directive"); |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 713 | Out.InitSections(false); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 717 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 718 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 719 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 720 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 721 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 722 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 723 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 724 | Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 727 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 728 | const char *Start = getTok().getLoc().getPointer(); |
| 729 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 730 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 731 | Lex(); |
| 732 | |
| 733 | const char *End = getTok().getLoc().getPointer(); |
| 734 | return StringRef(Start, End - Start); |
| 735 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 736 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 737 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 738 | const char *Start = getTok().getLoc().getPointer(); |
| 739 | |
| 740 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 741 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 742 | Lex(); |
| 743 | |
| 744 | const char *End = getTok().getLoc().getPointer(); |
| 745 | return StringRef(Start, End - Start); |
| 746 | } |
| 747 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 748 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 749 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 750 | /// |
| 751 | /// parenexpr ::= expr) |
| 752 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 753 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 754 | if (parseExpression(Res)) |
| 755 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 756 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 757 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 758 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 759 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 760 | return false; |
| 761 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 762 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 763 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 764 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 765 | /// |
| 766 | /// bracketexpr ::= expr] |
| 767 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 768 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 769 | if (parseExpression(Res)) |
| 770 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 771 | if (Lexer.isNot(AsmToken::RBrac)) |
| 772 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 773 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 774 | Lex(); |
| 775 | return false; |
| 776 | } |
| 777 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 778 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 779 | /// primaryexpr ::= (parenexpr |
| 780 | /// primaryexpr ::= symbol |
| 781 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 782 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 783 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 784 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 785 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 786 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 787 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 788 | default: |
| 789 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 790 | // If we have an error assume that we've already handled it. |
| 791 | case AsmToken::Error: |
| 792 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 793 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 794 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 795 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 796 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 797 | Res = MCUnaryExpr::createLNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 798 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 799 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 800 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 801 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 802 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 803 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 804 | if (parseIdentifier(Identifier)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 805 | if (FirstTokenKind == AsmToken::Dollar) { |
| 806 | if (Lexer.getMAI().getDollarIsPC()) { |
| 807 | // This is a '$' reference, which references the current PC. Emit a |
| 808 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 809 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 810 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 811 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 812 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 813 | EndLoc = FirstTokenLoc; |
| 814 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 815 | } |
| 816 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 817 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 818 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 819 | // Parse symbol variant |
| 820 | std::pair<StringRef, StringRef> Split; |
| 821 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 822 | if (FirstTokenKind == AsmToken::String) { |
| 823 | if (Lexer.is(AsmToken::At)) { |
| 824 | Lexer.Lex(); // eat @ |
| 825 | SMLoc AtLoc = getLexer().getLoc(); |
| 826 | StringRef VName; |
| 827 | if (parseIdentifier(VName)) |
| 828 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 829 | |
| 830 | Split = std::make_pair(Identifier, VName); |
| 831 | } |
| 832 | } else { |
| 833 | Split = Identifier.split('@'); |
| 834 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 835 | } else if (Lexer.is(AsmToken::LParen)) { |
| 836 | Lexer.Lex(); // eat ( |
| 837 | StringRef VName; |
| 838 | parseIdentifier(VName); |
| 839 | if (Lexer.isNot(AsmToken::RParen)) { |
| 840 | return Error(Lexer.getTok().getLoc(), |
| 841 | "unexpected token in variant, expected ')'"); |
| 842 | } |
| 843 | Lexer.Lex(); // eat ) |
| 844 | Split = std::make_pair(Identifier, VName); |
| 845 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 846 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 847 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 848 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 849 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 850 | StringRef SymbolName = Identifier; |
| 851 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 852 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 853 | // Lookup the symbol variant if used. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 854 | if (Split.second.size()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 855 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 856 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 857 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 858 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 859 | Variant = MCSymbolRefExpr::VK_None; |
| 860 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 861 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 862 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 865 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 866 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 867 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 868 | // If this is an absolute variable reference, substitute it now to preserve |
| 869 | // semantics in the face of reassignment. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame^] | 870 | if (Sym->isVariable() && |
| 871 | isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 872 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 873 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 874 | |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame^] | 875 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 876 | return false; |
| 877 | } |
| 878 | |
| 879 | // Otherwise create a symbol ref. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 880 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 881 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 882 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 883 | case AsmToken::BigNum: |
| 884 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 885 | case AsmToken::Integer: { |
| 886 | SMLoc Loc = getTok().getLoc(); |
| 887 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 888 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 889 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 890 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 891 | // Look for 'b' or 'f' following an Integer as a directional label |
| 892 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 893 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 894 | // Lookup the symbol variant if used. |
| 895 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 896 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 897 | if (Split.first.size() != IDVal.size()) { |
| 898 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 899 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 900 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 901 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 902 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 903 | if (IDVal == "f" || IDVal == "b") { |
| 904 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 905 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 906 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 907 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 908 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 909 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 910 | Lex(); // Eat identifier. |
| 911 | } |
| 912 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 913 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 914 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 915 | case AsmToken::Real: { |
| 916 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 917 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 918 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 919 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 920 | Lex(); // Eat token. |
| 921 | return false; |
| 922 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 923 | case AsmToken::Dot: { |
| 924 | // This is a '.' reference, which references the current PC. Emit a |
| 925 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 926 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 927 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 928 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 929 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 930 | Lex(); // Eat identifier. |
| 931 | return false; |
| 932 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 933 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 934 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 935 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 936 | case AsmToken::LBrac: |
| 937 | if (!PlatformParser->HasBracketExpressions()) |
| 938 | return TokError("brackets expression not supported on this target"); |
| 939 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 940 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 941 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 942 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 943 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 944 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 945 | Res = MCUnaryExpr::createMinus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 946 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 947 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 948 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 949 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 950 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 951 | Res = MCUnaryExpr::createPlus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 952 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 953 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 954 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 955 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 956 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 957 | Res = MCUnaryExpr::createNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 958 | return false; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 961 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 962 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 963 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 964 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 967 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 968 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 969 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 970 | // Ask the target implementation about this expression first. |
| 971 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 972 | if (NewE) |
| 973 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 974 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 975 | // if there is exactly one symbol. |
| 976 | switch (E->getKind()) { |
| 977 | case MCExpr::Target: |
| 978 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 979 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 980 | |
| 981 | case MCExpr::SymbolRef: { |
| 982 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 983 | |
| 984 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 985 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 986 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 987 | return E; |
| 988 | } |
| 989 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 990 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | case MCExpr::Unary: { |
| 994 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 995 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 996 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 997 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 998 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | case MCExpr::Binary: { |
| 1002 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1003 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1004 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1007 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1008 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1009 | if (!LHS) |
| 1010 | LHS = BE->getLHS(); |
| 1011 | if (!RHS) |
| 1012 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1013 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1014 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1017 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1018 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1021 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1022 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1023 | /// expr ::= expr &&,|| expr -> lowest. |
| 1024 | /// expr ::= expr |,^,&,! expr |
| 1025 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1026 | /// expr ::= expr <<,>> expr |
| 1027 | /// expr ::= expr +,- expr |
| 1028 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1029 | /// expr ::= primaryexpr |
| 1030 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1031 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1032 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1033 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1034 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1035 | return true; |
| 1036 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1037 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1038 | // expression to include the modifier. This is inefficient, but in general we |
| 1039 | // expect users to use 'a@modifier op b'. |
| 1040 | if (Lexer.getKind() == AsmToken::At) { |
| 1041 | Lex(); |
| 1042 | |
| 1043 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1044 | return TokError("unexpected symbol modifier following '@'"); |
| 1045 | |
| 1046 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1047 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1048 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1049 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1050 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1051 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1052 | if (!ModifiedRes) { |
| 1053 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1054 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1055 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1056 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1057 | Res = ModifiedRes; |
| 1058 | Lex(); |
| 1059 | } |
| 1060 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1061 | // Try to constant fold it up front, if possible. |
| 1062 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1063 | if (Res->evaluateAsAbsolute(Value)) |
| 1064 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1065 | |
| 1066 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1067 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1068 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1069 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1070 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1071 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1074 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1075 | SMLoc &EndLoc) { |
| 1076 | if (parseParenExpr(Res, EndLoc)) |
| 1077 | return true; |
| 1078 | |
| 1079 | for (; ParenDepth > 0; --ParenDepth) { |
| 1080 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1081 | return true; |
| 1082 | |
| 1083 | // We don't Lex() the last RParen. |
| 1084 | // This is the same behavior as parseParenExpression(). |
| 1085 | if (ParenDepth - 1 > 0) { |
| 1086 | if (Lexer.isNot(AsmToken::RParen)) |
| 1087 | return TokError("expected ')' in parentheses expression"); |
| 1088 | EndLoc = Lexer.getTok().getEndLoc(); |
| 1089 | Lex(); |
| 1090 | } |
| 1091 | } |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1095 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1096 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1097 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1098 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1099 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1100 | return true; |
| 1101 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1102 | if (!Expr->evaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1103 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 1108 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1109 | MCBinaryExpr::Opcode &Kind) { |
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: |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 1161 | Kind = MAI.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 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1185 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1186 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1187 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1188 | SMLoc &EndLoc) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1189 | while (1) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1190 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1191 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1193 | // If the next token is lower precedence than we are allowed to eat, return |
| 1194 | // successfully with what we ate already. |
| 1195 | if (TokPrec < Precedence) |
| 1196 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1197 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1198 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1200 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1201 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1202 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1203 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1205 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1206 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1207 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1208 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1209 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1210 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1211 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1212 | // Merge LHS and RHS according to operator. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1213 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext()); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1217 | /// ParseStatement: |
| 1218 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1219 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1220 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1221 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1222 | MCAsmParserSemaCallback *SI) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1223 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1224 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1225 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1226 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1227 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1228 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1229 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1230 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1231 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1232 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1233 | int64_t LocalLabelVal = -1; |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1234 | // A full line comment is a '#' as the first token. |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1235 | if (Lexer.is(AsmToken::Hash)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1236 | return parseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 3f56104 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1237 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1238 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1239 | if (Lexer.is(AsmToken::Integer)) { |
| 1240 | LocalLabelVal = getTok().getIntVal(); |
| 1241 | if (LocalLabelVal < 0) { |
| 1242 | if (!TheCondState.Ignore) |
| 1243 | return TokError("unexpected token at start of statement"); |
| 1244 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1245 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1246 | IDVal = getTok().getString(); |
| 1247 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1248 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1249 | if (!TheCondState.Ignore) |
| 1250 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1253 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1254 | // Treat '.' as a valid identifier in this context. |
| 1255 | Lex(); |
| 1256 | IDVal = "."; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1257 | } else if (parseIdentifier(IDVal)) { |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1258 | if (!TheCondState.Ignore) |
| 1259 | return TokError("unexpected token at start of statement"); |
| 1260 | IDVal = ""; |
| 1261 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1263 | // Handle conditional assembly here before checking for skipping. We |
| 1264 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1265 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1266 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1267 | DirectiveKindMap.find(IDVal); |
| 1268 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1269 | ? DK_NO_DIRECTIVE |
| 1270 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1271 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1272 | default: |
| 1273 | break; |
| 1274 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1275 | case DK_IFEQ: |
| 1276 | case DK_IFGE: |
| 1277 | case DK_IFGT: |
| 1278 | case DK_IFLE: |
| 1279 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1280 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1281 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1282 | case DK_IFB: |
| 1283 | return parseDirectiveIfb(IDLoc, true); |
| 1284 | case DK_IFNB: |
| 1285 | return parseDirectiveIfb(IDLoc, false); |
| 1286 | case DK_IFC: |
| 1287 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1288 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1289 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1290 | case DK_IFNC: |
| 1291 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1292 | case DK_IFNES: |
| 1293 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1294 | case DK_IFDEF: |
| 1295 | return parseDirectiveIfdef(IDLoc, true); |
| 1296 | case DK_IFNDEF: |
| 1297 | case DK_IFNOTDEF: |
| 1298 | return parseDirectiveIfdef(IDLoc, false); |
| 1299 | case DK_ELSEIF: |
| 1300 | return parseDirectiveElseIf(IDLoc); |
| 1301 | case DK_ELSE: |
| 1302 | return parseDirectiveElse(IDLoc); |
| 1303 | case DK_ENDIF: |
| 1304 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1305 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1306 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1307 | // Ignore the statement if in the middle of inactive conditional |
| 1308 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1309 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1310 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1313 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1314 | // FIXME: Recurse on local labels? |
| 1315 | |
| 1316 | // See what kind of statement we have. |
| 1317 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1318 | case AsmToken::Colon: { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1319 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1321 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1322 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1323 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1324 | // Diagnose attempt to use '.' as a label. |
| 1325 | if (IDVal == ".") |
| 1326 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1327 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1328 | // Diagnose attempt to use a variable as a label. |
| 1329 | // |
| 1330 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1331 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1332 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1333 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1334 | if (LocalLabelVal == -1) { |
| 1335 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1336 | StringRef RewrittenLabel = |
| 1337 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
| 1338 | assert(RewrittenLabel.size() && |
| 1339 | "We should have an internal name here."); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1340 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc, |
| 1341 | IDVal.size(), RewrittenLabel)); |
| 1342 | IDVal = RewrittenLabel; |
| 1343 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1344 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1345 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1346 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 1347 | |
| 1348 | Sym->redefineIfPossible(); |
| 1349 | |
Daniel Dunbar | deb7ba9 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1350 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1351 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1352 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1353 | // Emit the label. |
Chad Rosier | f3feab3 | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1354 | if (!ParsingInlineAsm) |
| 1355 | Out.EmitLabel(Sym); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1356 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1357 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1358 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1359 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1360 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1361 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1362 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1363 | getTargetParser().onLabelParsed(Sym); |
| 1364 | |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1365 | // Consume any end of statement token, if present, to avoid spurious |
| 1366 | // AddBlankLine calls(). |
| 1367 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1368 | Lex(); |
| 1369 | if (Lexer.is(AsmToken::Eof)) |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1373 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1374 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1375 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1376 | case AsmToken::Equal: |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1377 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1378 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1379 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1380 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1381 | |
| 1382 | default: // Normal instruction or directive. |
| 1383 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1384 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1385 | |
| 1386 | // 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] | 1387 | if (areMacrosEnabled()) |
| 1388 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1389 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1390 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1391 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1392 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1393 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1394 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1395 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1396 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1397 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1398 | // 1. The target-specific assembly parser. Some directives are target |
| 1399 | // specific or may potentially behave differently on certain targets. |
| 1400 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1401 | // (like the ELF parser) register themselves as extensions. |
| 1402 | // 3. The generic directive parser implemented by this class. These are |
| 1403 | // all the directives that behave in a target and platform independent |
| 1404 | // manner, or at least have a default behavior that's shared between |
| 1405 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1406 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1407 | // First query the target-specific parser. It will return 'true' if it |
| 1408 | // isn't interested in this directive. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1409 | if (!getTargetParser().ParseDirective(ID)) |
| 1410 | return false; |
| 1411 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1412 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1413 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1414 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1415 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1416 | if (Handler.first) |
| 1417 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1418 | |
| 1419 | // Finally, if no one else is interested in this directive, it must be |
| 1420 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1421 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1422 | default: |
| 1423 | break; |
| 1424 | case DK_SET: |
| 1425 | case DK_EQU: |
| 1426 | return parseDirectiveSet(IDVal, true); |
| 1427 | case DK_EQUIV: |
| 1428 | return parseDirectiveSet(IDVal, false); |
| 1429 | case DK_ASCII: |
| 1430 | return parseDirectiveAscii(IDVal, false); |
| 1431 | case DK_ASCIZ: |
| 1432 | case DK_STRING: |
| 1433 | return parseDirectiveAscii(IDVal, true); |
| 1434 | case DK_BYTE: |
| 1435 | return parseDirectiveValue(1); |
| 1436 | case DK_SHORT: |
| 1437 | case DK_VALUE: |
| 1438 | case DK_2BYTE: |
| 1439 | return parseDirectiveValue(2); |
| 1440 | case DK_LONG: |
| 1441 | case DK_INT: |
| 1442 | case DK_4BYTE: |
| 1443 | return parseDirectiveValue(4); |
| 1444 | case DK_QUAD: |
| 1445 | case DK_8BYTE: |
| 1446 | return parseDirectiveValue(8); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1447 | case DK_OCTA: |
| 1448 | return parseDirectiveOctaValue(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1449 | case DK_SINGLE: |
| 1450 | case DK_FLOAT: |
| 1451 | return parseDirectiveRealValue(APFloat::IEEEsingle); |
| 1452 | case DK_DOUBLE: |
| 1453 | return parseDirectiveRealValue(APFloat::IEEEdouble); |
| 1454 | case DK_ALIGN: { |
| 1455 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1456 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1457 | } |
| 1458 | case DK_ALIGN32: { |
| 1459 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1460 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1461 | } |
| 1462 | case DK_BALIGN: |
| 1463 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1464 | case DK_BALIGNW: |
| 1465 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1466 | case DK_BALIGNL: |
| 1467 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1468 | case DK_P2ALIGN: |
| 1469 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1470 | case DK_P2ALIGNW: |
| 1471 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1472 | case DK_P2ALIGNL: |
| 1473 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1474 | case DK_ORG: |
| 1475 | return parseDirectiveOrg(); |
| 1476 | case DK_FILL: |
| 1477 | return parseDirectiveFill(); |
| 1478 | case DK_ZERO: |
| 1479 | return parseDirectiveZero(); |
| 1480 | case DK_EXTERN: |
| 1481 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1482 | return false; |
| 1483 | case DK_GLOBL: |
| 1484 | case DK_GLOBAL: |
| 1485 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1486 | case DK_LAZY_REFERENCE: |
| 1487 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1488 | case DK_NO_DEAD_STRIP: |
| 1489 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1490 | case DK_SYMBOL_RESOLVER: |
| 1491 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1492 | case DK_PRIVATE_EXTERN: |
| 1493 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1494 | case DK_REFERENCE: |
| 1495 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1496 | case DK_WEAK_DEFINITION: |
| 1497 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1498 | case DK_WEAK_REFERENCE: |
| 1499 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1500 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1501 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1502 | case DK_COMM: |
| 1503 | case DK_COMMON: |
| 1504 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1505 | case DK_LCOMM: |
| 1506 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1507 | case DK_ABORT: |
| 1508 | return parseDirectiveAbort(); |
| 1509 | case DK_INCLUDE: |
| 1510 | return parseDirectiveInclude(); |
| 1511 | case DK_INCBIN: |
| 1512 | return parseDirectiveIncbin(); |
| 1513 | case DK_CODE16: |
| 1514 | case DK_CODE16GCC: |
| 1515 | return TokError(Twine(IDVal) + " not supported yet"); |
| 1516 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1517 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1518 | case DK_IRP: |
| 1519 | return parseDirectiveIrp(IDLoc); |
| 1520 | case DK_IRPC: |
| 1521 | return parseDirectiveIrpc(IDLoc); |
| 1522 | case DK_ENDR: |
| 1523 | return parseDirectiveEndr(IDLoc); |
| 1524 | case DK_BUNDLE_ALIGN_MODE: |
| 1525 | return parseDirectiveBundleAlignMode(); |
| 1526 | case DK_BUNDLE_LOCK: |
| 1527 | return parseDirectiveBundleLock(); |
| 1528 | case DK_BUNDLE_UNLOCK: |
| 1529 | return parseDirectiveBundleUnlock(); |
| 1530 | case DK_SLEB128: |
| 1531 | return parseDirectiveLEB128(true); |
| 1532 | case DK_ULEB128: |
| 1533 | return parseDirectiveLEB128(false); |
| 1534 | case DK_SPACE: |
| 1535 | case DK_SKIP: |
| 1536 | return parseDirectiveSpace(IDVal); |
| 1537 | case DK_FILE: |
| 1538 | return parseDirectiveFile(IDLoc); |
| 1539 | case DK_LINE: |
| 1540 | return parseDirectiveLine(); |
| 1541 | case DK_LOC: |
| 1542 | return parseDirectiveLoc(); |
| 1543 | case DK_STABS: |
| 1544 | return parseDirectiveStabs(); |
| 1545 | case DK_CFI_SECTIONS: |
| 1546 | return parseDirectiveCFISections(); |
| 1547 | case DK_CFI_STARTPROC: |
| 1548 | return parseDirectiveCFIStartProc(); |
| 1549 | case DK_CFI_ENDPROC: |
| 1550 | return parseDirectiveCFIEndProc(); |
| 1551 | case DK_CFI_DEF_CFA: |
| 1552 | return parseDirectiveCFIDefCfa(IDLoc); |
| 1553 | case DK_CFI_DEF_CFA_OFFSET: |
| 1554 | return parseDirectiveCFIDefCfaOffset(); |
| 1555 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1556 | return parseDirectiveCFIAdjustCfaOffset(); |
| 1557 | case DK_CFI_DEF_CFA_REGISTER: |
| 1558 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 1559 | case DK_CFI_OFFSET: |
| 1560 | return parseDirectiveCFIOffset(IDLoc); |
| 1561 | case DK_CFI_REL_OFFSET: |
| 1562 | return parseDirectiveCFIRelOffset(IDLoc); |
| 1563 | case DK_CFI_PERSONALITY: |
| 1564 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 1565 | case DK_CFI_LSDA: |
| 1566 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 1567 | case DK_CFI_REMEMBER_STATE: |
| 1568 | return parseDirectiveCFIRememberState(); |
| 1569 | case DK_CFI_RESTORE_STATE: |
| 1570 | return parseDirectiveCFIRestoreState(); |
| 1571 | case DK_CFI_SAME_VALUE: |
| 1572 | return parseDirectiveCFISameValue(IDLoc); |
| 1573 | case DK_CFI_RESTORE: |
| 1574 | return parseDirectiveCFIRestore(IDLoc); |
| 1575 | case DK_CFI_ESCAPE: |
| 1576 | return parseDirectiveCFIEscape(); |
| 1577 | case DK_CFI_SIGNAL_FRAME: |
| 1578 | return parseDirectiveCFISignalFrame(); |
| 1579 | case DK_CFI_UNDEFINED: |
| 1580 | return parseDirectiveCFIUndefined(IDLoc); |
| 1581 | case DK_CFI_REGISTER: |
| 1582 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 1583 | case DK_CFI_WINDOW_SAVE: |
| 1584 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1585 | case DK_MACROS_ON: |
| 1586 | case DK_MACROS_OFF: |
| 1587 | return parseDirectiveMacrosOnOff(IDVal); |
| 1588 | case DK_MACRO: |
| 1589 | return parseDirectiveMacro(IDLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1590 | case DK_EXITM: |
| 1591 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1592 | case DK_ENDM: |
| 1593 | case DK_ENDMACRO: |
| 1594 | return parseDirectiveEndMacro(IDVal); |
| 1595 | case DK_PURGEM: |
| 1596 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 1597 | case DK_END: |
| 1598 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 1599 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 1600 | return parseDirectiveError(IDLoc, false); |
| 1601 | case DK_ERROR: |
| 1602 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 1603 | case DK_WARNING: |
| 1604 | return parseDirectiveWarning(IDLoc); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1605 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1606 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1607 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1608 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1609 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1610 | // __asm _emit or __asm __emit |
| 1611 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 1612 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1613 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1614 | |
| 1615 | // __asm align |
| 1616 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1617 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1618 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1619 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1620 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1621 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1622 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1623 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1624 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1625 | Info.ParsedOperands); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1626 | Info.ParseError = HadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1628 | // Dump the parsed representation, if requested. |
| 1629 | if (getShowParsedOperands()) { |
| 1630 | SmallString<256> Str; |
| 1631 | raw_svector_ostream OS(Str); |
| 1632 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1633 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1634 | if (i != 0) |
| 1635 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1636 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1637 | } |
| 1638 | OS << "]"; |
| 1639 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1640 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1643 | // If we are generating dwarf for the current section then generate a .loc |
| 1644 | // directive for the instruction. |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1645 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1646 | getContext().getGenDwarfSectionSyms().count( |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 1647 | getStreamer().getCurrentSection().first)) { |
| 1648 | unsigned Line; |
| 1649 | if (ActiveMacros.empty()) |
| 1650 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 1651 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 1652 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 1653 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1654 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1655 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1656 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1657 | // 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] | 1658 | if (CppHashFilename.size()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 1659 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
| 1660 | 0, StringRef(), CppHashFilename); |
| 1661 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1662 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1663 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 1664 | // cache with the different Loc from the call above we save the last |
| 1665 | // info we queried here with SrcMgr.FindLineNumber(). |
| 1666 | unsigned CppHashLocLineNo; |
| 1667 | if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf) |
| 1668 | CppHashLocLineNo = LastQueryLine; |
| 1669 | else { |
| 1670 | CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf); |
| 1671 | LastQueryLine = CppHashLocLineNo; |
| 1672 | LastQueryIDLoc = CppHashLoc; |
| 1673 | LastQueryBuffer = CppHashBuf; |
| 1674 | } |
| 1675 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1676 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1677 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1678 | getStreamer().EmitDwarfLocDirective( |
| 1679 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 1680 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 1681 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1684 | // If parsing succeeded, match the instruction. |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1685 | if (!HadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1686 | uint64_t ErrorInfo; |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1687 | getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1688 | Info.ParsedOperands, Out, |
Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1689 | ErrorInfo, ParsingInlineAsm); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1690 | } |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1692 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1693 | // that. |
| 1694 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1695 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1696 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1697 | /// 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] | 1698 | /// 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] | 1699 | void AsmParser::eatToEndOfLine() { |
Rafael Espindola | e0d0908 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1700 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1701 | Lexer.LexUntilEndOfLine(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1702 | // Eat EOL. |
| 1703 | Lex(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1706 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1707 | /// ::= # number "filename" |
| 1708 | /// or just as a full line comment if it doesn't have a number and a string. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1709 | bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1710 | Lex(); // Eat the hash token. |
| 1711 | |
| 1712 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1713 | // Consume the line since in cases it is not a well-formed line directive, |
| 1714 | // as if were simply a full line comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1715 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1720 | Lex(); |
| 1721 | |
| 1722 | if (getLexer().isNot(AsmToken::String)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1723 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1724 | return false; |
| 1725 | } |
| 1726 | |
| 1727 | StringRef Filename = getTok().getString(); |
| 1728 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1729 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1730 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1731 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1732 | CppHashLoc = L; |
| 1733 | CppHashFilename = Filename; |
| 1734 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1735 | CppHashBuf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Ignore any trailing characters, they're just comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1738 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1739 | return false; |
| 1740 | } |
| 1741 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1742 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1743 | /// for the Filename and LineNo if any in the diagnostic. |
| 1744 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1745 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1746 | raw_ostream &OS = errs(); |
| 1747 | |
| 1748 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| 1749 | const SMLoc &DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1750 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1751 | unsigned CppHashBuf = |
| 1752 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1753 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1754 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1755 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1756 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1757 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 1758 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1759 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1760 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1763 | // 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] | 1764 | // manager changed or buffer changed (like in a nested include) then just |
| 1765 | // print the normal diagnostic using its Filename and LineNo. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1766 | if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1767 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1768 | if (Parser->SavedDiagHandler) |
| 1769 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1770 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1771 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1772 | return; |
| 1773 | } |
| 1774 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1775 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1776 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1777 | // the diagnostic. |
Jakub Staszak | ec2ffa9 | 2013-09-16 22:03:38 +0000 | [diff] [blame] | 1778 | const std::string &Filename = Parser->CppHashFilename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1779 | |
| 1780 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1781 | int CppHashLocLineNo = |
| 1782 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1783 | int LineNo = |
| 1784 | Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1785 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1786 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 1787 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1788 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1789 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1790 | if (Parser->SavedDiagHandler) |
| 1791 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1792 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1793 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1796 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1797 | // difference being that that function accepts '@' as part of identifiers and |
| 1798 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1799 | // '@' as a special case when needed. |
| 1800 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1801 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 1802 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1805 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 1806 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1807 | ArrayRef<MCAsmMacroArgument> A, |
| 1808 | bool EnableAtPseudoVariable, const SMLoc &L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1809 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1810 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1811 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1812 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1813 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1814 | // A macro without parameters is handled differently on Darwin: |
| 1815 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1816 | while (!Body.empty()) { |
| 1817 | // Scan for the next substitution. |
| 1818 | std::size_t End = Body.size(), Pos = 0; |
| 1819 | for (; Pos != End; ++Pos) { |
| 1820 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1821 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1822 | // This macro has no parameters, look for $0, $1, etc. |
| 1823 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1824 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1825 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1826 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1827 | if (Next == '$' || Next == 'n' || |
| 1828 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1829 | break; |
| 1830 | } else { |
| 1831 | // This macro has parameters, look for \foo, \bar, etc. |
| 1832 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1833 | break; |
| 1834 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | // Add the prefix. |
| 1838 | OS << Body.slice(0, Pos); |
| 1839 | |
| 1840 | // Check if we reached the end. |
| 1841 | if (Pos == End) |
| 1842 | break; |
| 1843 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1844 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1845 | switch (Body[Pos + 1]) { |
| 1846 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1847 | case '$': |
| 1848 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1849 | break; |
| 1850 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1851 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1852 | case 'n': |
| 1853 | OS << A.size(); |
| 1854 | break; |
| 1855 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1856 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1857 | default: { |
| 1858 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1859 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1860 | if (Index >= A.size()) |
| 1861 | break; |
| 1862 | |
| 1863 | // Otherwise substitute with the token values, with spaces eliminated. |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1864 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1865 | ie = A[Index].end(); |
| 1866 | it != ie; ++it) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1867 | OS << it->getString(); |
| 1868 | break; |
| 1869 | } |
| 1870 | } |
| 1871 | Pos += 2; |
| 1872 | } else { |
| 1873 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Check for the \@ pseudo-variable. |
| 1876 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1877 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1878 | else |
| 1879 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 1880 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1881 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1882 | const char *Begin = Body.data() + Pos + 1; |
| 1883 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1884 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1885 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1886 | if (Argument == "@") { |
| 1887 | OS << NumOfMacroInstantiations; |
| 1888 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1889 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1890 | for (; Index < NParameters; ++Index) |
| 1891 | if (Parameters[Index].Name == Argument) |
| 1892 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1893 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1894 | if (Index == NParameters) { |
| 1895 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 1896 | Pos += 3; |
| 1897 | else { |
| 1898 | OS << '\\' << Argument; |
| 1899 | Pos = I; |
| 1900 | } |
| 1901 | } else { |
| 1902 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
| 1903 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
| 1904 | ie = A[Index].end(); |
| 1905 | it != ie; ++it) |
| 1906 | // We expect no quotes around the string's contents when |
| 1907 | // parsing for varargs. |
| 1908 | if (it->getKind() != AsmToken::String || VarargParameter) |
| 1909 | OS << it->getString(); |
| 1910 | else |
| 1911 | OS << it->getStringContents(); |
| 1912 | |
| 1913 | Pos += 1 + Argument.size(); |
| 1914 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1915 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1916 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1917 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1918 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1919 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1920 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1921 | return false; |
| 1922 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1923 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 1924 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 1925 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 1926 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1927 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1928 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1929 | static bool isOperator(AsmToken::TokenKind kind) { |
| 1930 | switch (kind) { |
| 1931 | default: |
| 1932 | return false; |
| 1933 | case AsmToken::Plus: |
| 1934 | case AsmToken::Minus: |
| 1935 | case AsmToken::Tilde: |
| 1936 | case AsmToken::Slash: |
| 1937 | case AsmToken::Star: |
| 1938 | case AsmToken::Dot: |
| 1939 | case AsmToken::Equal: |
| 1940 | case AsmToken::EqualEqual: |
| 1941 | case AsmToken::Pipe: |
| 1942 | case AsmToken::PipePipe: |
| 1943 | case AsmToken::Caret: |
| 1944 | case AsmToken::Amp: |
| 1945 | case AsmToken::AmpAmp: |
| 1946 | case AsmToken::Exclaim: |
| 1947 | case AsmToken::ExclaimEqual: |
| 1948 | case AsmToken::Percent: |
| 1949 | case AsmToken::Less: |
| 1950 | case AsmToken::LessEqual: |
| 1951 | case AsmToken::LessLess: |
| 1952 | case AsmToken::LessGreater: |
| 1953 | case AsmToken::Greater: |
| 1954 | case AsmToken::GreaterEqual: |
| 1955 | case AsmToken::GreaterGreater: |
| 1956 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1960 | namespace { |
| 1961 | class AsmLexerSkipSpaceRAII { |
| 1962 | public: |
| 1963 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 1964 | Lexer.setSkipSpace(SkipSpace); |
| 1965 | } |
| 1966 | |
| 1967 | ~AsmLexerSkipSpaceRAII() { |
| 1968 | Lexer.setSkipSpace(true); |
| 1969 | } |
| 1970 | |
| 1971 | private: |
| 1972 | AsmLexer &Lexer; |
| 1973 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1974 | } |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1975 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1976 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 1977 | |
| 1978 | if (Vararg) { |
| 1979 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1980 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 1981 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1982 | } |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1986 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1987 | unsigned AddTokens = 0; |
| 1988 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1989 | // Darwin doesn't use spaces to delmit arguments. |
| 1990 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1991 | |
| 1992 | for (;;) { |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1993 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1994 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1995 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1996 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1997 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1998 | |
| 1999 | if (Lexer.is(AsmToken::Space)) { |
| 2000 | Lex(); // Eat spaces |
| 2001 | |
| 2002 | // Spaces can delimit parameters, but could also be part an expression. |
| 2003 | // If the token after a space is an operator, add the token and the next |
| 2004 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2005 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2006 | if (isOperator(Lexer.getKind())) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2007 | // Check to see whether the token is used as an operator, |
| 2008 | // or part of an identifier |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 2009 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2010 | if (*NextChar == ' ') |
| 2011 | AddTokens = 2; |
| 2012 | } |
| 2013 | |
| 2014 | if (!AddTokens && ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | } |
| 2017 | } |
| 2018 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2019 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2020 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2021 | // to be able to fill in the remaining default parameter values |
| 2022 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2023 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Adjust the current parentheses level. |
| 2026 | if (Lexer.is(AsmToken::LParen)) |
| 2027 | ++ParenLevel; |
| 2028 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2029 | --ParenLevel; |
| 2030 | |
| 2031 | // Append the token to the current argument list. |
| 2032 | MA.push_back(getTok()); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2033 | if (AddTokens) |
| 2034 | AddTokens--; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2035 | Lex(); |
| 2036 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2037 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2038 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2039 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2040 | return false; |
| 2041 | } |
| 2042 | |
| 2043 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2044 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2045 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2046 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2047 | bool NamedParametersFound = false; |
| 2048 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2049 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2050 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2051 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2052 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2053 | // Parse two kinds of macro invocations: |
| 2054 | // - macros defined without any parameters accept an arbitrary number of them |
| 2055 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2056 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2057 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2058 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2059 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2060 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2061 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2062 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2063 | if (parseIdentifier(FA.Name)) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2064 | Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2065 | eatToEndOfStatement(); |
| 2066 | return true; |
| 2067 | } |
| 2068 | |
| 2069 | if (!Lexer.is(AsmToken::Equal)) { |
| 2070 | TokError("expected '=' after formal parameter identifier"); |
| 2071 | eatToEndOfStatement(); |
| 2072 | return true; |
| 2073 | } |
| 2074 | Lex(); |
| 2075 | |
| 2076 | NamedParametersFound = true; |
| 2077 | } |
| 2078 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2079 | if (NamedParametersFound && FA.Name.empty()) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2080 | Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2081 | eatToEndOfStatement(); |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2085 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
| 2086 | if (parseMacroArgument(FA.Value, Vararg)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2087 | return true; |
| 2088 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2089 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2090 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2091 | unsigned FAI = 0; |
| 2092 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2093 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2094 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2095 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2096 | if (FAI >= NParameters) { |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2097 | assert(M && "expected macro to be defined"); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2098 | Error(IDLoc, |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2099 | "parameter named '" + FA.Name + "' does not exist for macro '" + |
Saleem Abdulrasool | 3f44cd7 | 2014-03-17 17:13:57 +0000 | [diff] [blame] | 2100 | M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2101 | return true; |
| 2102 | } |
| 2103 | PI = FAI; |
| 2104 | } |
| 2105 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2106 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2107 | if (A.size() <= PI) |
| 2108 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2109 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2110 | |
| 2111 | if (FALocs.size() <= PI) |
| 2112 | FALocs.resize(PI + 1); |
| 2113 | |
| 2114 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2115 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2116 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2117 | // At the end of the statement, fill in remaining arguments that have |
| 2118 | // default values. If there aren't any, then the next argument is |
| 2119 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2120 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2121 | bool Failure = false; |
| 2122 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2123 | if (A[FAI].empty()) { |
| 2124 | if (M->Parameters[FAI].Required) { |
| 2125 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2126 | "missing value for required parameter " |
| 2127 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2128 | Failure = true; |
| 2129 | } |
| 2130 | |
| 2131 | if (!M->Parameters[FAI].Value.empty()) |
| 2132 | A[FAI] = M->Parameters[FAI].Value; |
| 2133 | } |
| 2134 | } |
| 2135 | return Failure; |
| 2136 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2137 | |
| 2138 | if (Lexer.is(AsmToken::Comma)) |
| 2139 | Lex(); |
| 2140 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2141 | |
| 2142 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2145 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2146 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 2147 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2150 | void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 2151 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2154 | void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2155 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2156 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2157 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 2158 | // this, although we should protect against infinite loops. |
| 2159 | if (ActiveMacros.size() == 20) |
| 2160 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 2161 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2162 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2163 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2164 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2165 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2166 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2167 | // to hold the macro body with substitutions. |
| 2168 | SmallString<256> Buf; |
| 2169 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2170 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2171 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2172 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2173 | return true; |
| 2174 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2175 | // 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] | 2176 | // instantiation. |
| 2177 | OS << ".endmacro\n"; |
| 2178 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2179 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2180 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2181 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2182 | // Create the macro instantiation object and add to the current macro |
| 2183 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2184 | MacroInstantiation *MI = new MacroInstantiation( |
| 2185 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2186 | ActiveMacros.push_back(MI); |
| 2187 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2188 | ++NumOfMacroInstantiations; |
| 2189 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2190 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2191 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2192 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2193 | Lex(); |
| 2194 | |
| 2195 | return false; |
| 2196 | } |
| 2197 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2198 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2199 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2200 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2201 | Lex(); |
| 2202 | |
| 2203 | // Pop the instantiation entry. |
| 2204 | delete ActiveMacros.back(); |
| 2205 | ActiveMacros.pop_back(); |
| 2206 | } |
| 2207 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2208 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2209 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2210 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2211 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2212 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| 2213 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2214 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2215 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2216 | if (!Sym) { |
| 2217 | // In the case where we parse an expression starting with a '.', we will |
| 2218 | // not generate an error, nor will we create a symbol. In this case we |
| 2219 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2220 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2221 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2222 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2223 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2224 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2225 | if (NoDeadStrip) |
| 2226 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2227 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2228 | return false; |
| 2229 | } |
| 2230 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2231 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2232 | /// ::= identifier |
| 2233 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2234 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2235 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2236 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2237 | // 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] | 2238 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2239 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2240 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2241 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2242 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2243 | // Consume the prefix character, and check for a following identifier. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2244 | Lex(); |
| 2245 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2246 | return true; |
| 2247 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2248 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| 2249 | if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2250 | return true; |
| 2251 | |
| 2252 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2253 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2254 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2255 | Lex(); |
| 2256 | return false; |
| 2257 | } |
| 2258 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2259 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2260 | return true; |
| 2261 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2262 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2263 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2264 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2265 | |
| 2266 | return false; |
| 2267 | } |
| 2268 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2269 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2270 | /// ::= .equ identifier ',' expression |
| 2271 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2272 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2273 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2274 | StringRef Name; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2275 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2276 | if (parseIdentifier(Name)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2277 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2278 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2279 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2280 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2281 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2282 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2283 | return parseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2286 | bool AsmParser::parseEscapedString(std::string &Data) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2287 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2288 | |
| 2289 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2290 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2291 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2292 | if (Str[i] != '\\') { |
| 2293 | Data += Str[i]; |
| 2294 | continue; |
| 2295 | } |
| 2296 | |
| 2297 | // Recognize escaped characters. Note that this escape semantics currently |
| 2298 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2299 | ++i; |
| 2300 | if (i == e) |
| 2301 | return TokError("unexpected backslash at end of string"); |
| 2302 | |
| 2303 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2304 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2305 | // Consume up to three octal characters. |
| 2306 | unsigned Value = Str[i] - '0'; |
| 2307 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2308 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2309 | ++i; |
| 2310 | Value = Value * 8 + (Str[i] - '0'); |
| 2311 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2312 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2313 | ++i; |
| 2314 | Value = Value * 8 + (Str[i] - '0'); |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | if (Value > 255) |
| 2319 | return TokError("invalid octal escape sequence (out of range)"); |
| 2320 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2321 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2322 | continue; |
| 2323 | } |
| 2324 | |
| 2325 | // Otherwise recognize individual escapes. |
| 2326 | switch (Str[i]) { |
| 2327 | default: |
| 2328 | // Just reject invalid escape sequences for now. |
| 2329 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2330 | |
| 2331 | case 'b': Data += '\b'; break; |
| 2332 | case 'f': Data += '\f'; break; |
| 2333 | case 'n': Data += '\n'; break; |
| 2334 | case 'r': Data += '\r'; break; |
| 2335 | case 't': Data += '\t'; break; |
| 2336 | case '"': Data += '"'; break; |
| 2337 | case '\\': Data += '\\'; break; |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | return false; |
| 2342 | } |
| 2343 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2344 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2345 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2346 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2347 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2348 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2349 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2350 | for (;;) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2351 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2352 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2353 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2354 | std::string Data; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2355 | if (parseEscapedString(Data)) |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2356 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2357 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2358 | getStreamer().EmitBytes(Data); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2359 | if (ZeroTerminated) |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2360 | getStreamer().EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2361 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2362 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2363 | |
| 2364 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2365 | break; |
| 2366 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2367 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2368 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2369 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2373 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2374 | return false; |
| 2375 | } |
| 2376 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2377 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2378 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2379 | bool AsmParser::parseDirectiveValue(unsigned Size) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2380 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2381 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2382 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2383 | for (;;) { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2384 | const MCExpr *Value; |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2385 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2386 | if (parseExpression(Value)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2387 | return true; |
| 2388 | |
Daniel Dunbar | 6738a2e | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2389 | // Special case constant expressions to match code generator. |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2390 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2391 | assert(Size <= 8 && "Invalid size"); |
| 2392 | uint64_t IntValue = MCE->getValue(); |
| 2393 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2394 | return Error(ExprLoc, "literal value out of range for directive"); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2395 | getStreamer().EmitIntValue(IntValue, Size); |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2396 | } else |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 2397 | getStreamer().EmitValue(Value, Size, ExprLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2398 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2399 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2400 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2401 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2402 | // FIXME: Improve diagnostic. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2403 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2404 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2405 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2409 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2410 | return false; |
| 2411 | } |
| 2412 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2413 | /// ParseDirectiveOctaValue |
| 2414 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
| 2415 | bool AsmParser::parseDirectiveOctaValue() { |
| 2416 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2417 | checkForValidSection(); |
| 2418 | |
| 2419 | for (;;) { |
| 2420 | if (Lexer.getKind() == AsmToken::Error) |
| 2421 | return true; |
| 2422 | if (Lexer.getKind() != AsmToken::Integer && |
| 2423 | Lexer.getKind() != AsmToken::BigNum) |
| 2424 | return TokError("unknown token in expression"); |
| 2425 | |
| 2426 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2427 | APInt IntValue = getTok().getAPIntVal(); |
| 2428 | Lex(); |
| 2429 | |
| 2430 | uint64_t hi, lo; |
| 2431 | if (IntValue.isIntN(64)) { |
| 2432 | hi = 0; |
| 2433 | lo = IntValue.getZExtValue(); |
| 2434 | } else if (IntValue.isIntN(128)) { |
David Woodhouse | 6c9a6f9 | 2014-02-01 16:52:33 +0000 | [diff] [blame] | 2435 | // It might actually have more than 128 bits, but the top ones are zero. |
| 2436 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2437 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2438 | } else |
| 2439 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2440 | |
| 2441 | if (MAI.isLittleEndian()) { |
| 2442 | getStreamer().EmitIntValue(lo, 8); |
| 2443 | getStreamer().EmitIntValue(hi, 8); |
| 2444 | } else { |
| 2445 | getStreamer().EmitIntValue(hi, 8); |
| 2446 | getStreamer().EmitIntValue(lo, 8); |
| 2447 | } |
| 2448 | |
| 2449 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2450 | break; |
| 2451 | |
| 2452 | // FIXME: Improve diagnostic. |
| 2453 | if (getLexer().isNot(AsmToken::Comma)) |
| 2454 | return TokError("unexpected token in directive"); |
| 2455 | Lex(); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | Lex(); |
| 2460 | return false; |
| 2461 | } |
| 2462 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2463 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2464 | /// ::= (.single | .double) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2465 | bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) { |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2466 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2467 | checkForValidSection(); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2468 | |
| 2469 | for (;;) { |
| 2470 | // We don't truly support arithmetic on floating point expressions, so we |
| 2471 | // have to manually parse unary prefixes. |
| 2472 | bool IsNeg = false; |
| 2473 | if (getLexer().is(AsmToken::Minus)) { |
| 2474 | Lex(); |
| 2475 | IsNeg = true; |
| 2476 | } else if (getLexer().is(AsmToken::Plus)) |
| 2477 | Lex(); |
| 2478 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2479 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2480 | getLexer().isNot(AsmToken::Real) && |
| 2481 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2482 | return TokError("unexpected token in directive"); |
| 2483 | |
| 2484 | // Convert to an APFloat. |
| 2485 | APFloat Value(Semantics); |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2486 | StringRef IDVal = getTok().getString(); |
| 2487 | if (getLexer().is(AsmToken::Identifier)) { |
| 2488 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2489 | Value = APFloat::getInf(Semantics); |
| 2490 | else if (!IDVal.compare_lower("nan")) |
| 2491 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2492 | else |
| 2493 | return TokError("invalid floating point literal"); |
| 2494 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2495 | APFloat::opInvalidOp) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2496 | return TokError("invalid floating point literal"); |
| 2497 | if (IsNeg) |
| 2498 | Value.changeSign(); |
| 2499 | |
| 2500 | // Consume the numeric token. |
| 2501 | Lex(); |
| 2502 | |
| 2503 | // Emit the value as an integer. |
| 2504 | APInt AsInt = Value.bitcastToAPInt(); |
| 2505 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2506 | AsInt.getBitWidth() / 8); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2507 | |
| 2508 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2509 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2510 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2511 | if (getLexer().isNot(AsmToken::Comma)) |
| 2512 | return TokError("unexpected token in directive"); |
| 2513 | Lex(); |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | Lex(); |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2521 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2522 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2523 | bool AsmParser::parseDirectiveZero() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2524 | checkForValidSection(); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2525 | |
| 2526 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2527 | if (parseAbsoluteExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2528 | return true; |
| 2529 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2530 | int64_t Val = 0; |
| 2531 | if (getLexer().is(AsmToken::Comma)) { |
| 2532 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2533 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2534 | return true; |
| 2535 | } |
| 2536 | |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2537 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2538 | return TokError("unexpected token in '.zero' directive"); |
| 2539 | |
| 2540 | Lex(); |
| 2541 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2542 | getStreamer().EmitFill(NumBytes, Val); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2543 | |
| 2544 | return false; |
| 2545 | } |
| 2546 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2547 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2548 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2549 | bool AsmParser::parseDirectiveFill() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2550 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2551 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2552 | SMLoc RepeatLoc = getLexer().getLoc(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2553 | int64_t NumValues; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2554 | if (parseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2555 | return true; |
| 2556 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2557 | if (NumValues < 0) { |
| 2558 | Warning(RepeatLoc, |
| 2559 | "'.fill' directive with negative repeat count has no effect"); |
| 2560 | NumValues = 0; |
| 2561 | } |
| 2562 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2563 | int64_t FillSize = 1; |
| 2564 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2565 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2566 | SMLoc SizeLoc, ExprLoc; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2567 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2568 | if (getLexer().isNot(AsmToken::Comma)) |
| 2569 | return TokError("unexpected token in '.fill' directive"); |
| 2570 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2571 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2572 | SizeLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2573 | if (parseAbsoluteExpression(FillSize)) |
| 2574 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2575 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2576 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2577 | if (getLexer().isNot(AsmToken::Comma)) |
| 2578 | return TokError("unexpected token in '.fill' directive"); |
| 2579 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2580 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2581 | ExprLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2582 | if (parseAbsoluteExpression(FillExpr)) |
| 2583 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2584 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2585 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2586 | return TokError("unexpected token in '.fill' directive"); |
| 2587 | |
| 2588 | Lex(); |
| 2589 | } |
| 2590 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2591 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2592 | if (FillSize < 0) { |
| 2593 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
| 2594 | NumValues = 0; |
| 2595 | } |
| 2596 | if (FillSize > 8) { |
| 2597 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 2598 | FillSize = 8; |
| 2599 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2600 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2601 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 2602 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 2603 | |
Alexey Samsonov | 1b0713c | 2014-09-02 17:25:29 +0000 | [diff] [blame] | 2604 | if (NumValues > 0) { |
| 2605 | int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize; |
| 2606 | FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8); |
| 2607 | for (uint64_t i = 0, e = NumValues; i != e; ++i) { |
| 2608 | getStreamer().EmitIntValue(FillExpr, NonZeroFillSize); |
| 2609 | if (NonZeroFillSize < FillSize) |
| 2610 | getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize); |
| 2611 | } |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2612 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2613 | |
| 2614 | return false; |
| 2615 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2616 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2617 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2618 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2619 | bool AsmParser::parseDirectiveOrg() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2620 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2621 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2622 | const MCExpr *Offset; |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2623 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2624 | if (parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2625 | return true; |
| 2626 | |
| 2627 | // Parse optional fill expression. |
| 2628 | int64_t FillExpr = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2629 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2630 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2631 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2632 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2633 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2634 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2635 | return true; |
| 2636 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2637 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2638 | return TokError("unexpected token in '.org' directive"); |
| 2639 | } |
| 2640 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2641 | Lex(); |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 2642 | |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2643 | // Only limited forms of relocatable expressions are accepted here, it |
| 2644 | // has to be relative to the current section. The streamer will return |
| 2645 | // 'true' if the expression wasn't evaluatable. |
| 2646 | if (getStreamer().EmitValueToOffset(Offset, FillExpr)) |
| 2647 | return Error(Loc, "expected assembly-time absolute expression"); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2648 | |
| 2649 | return false; |
| 2650 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2651 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2652 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2653 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2654 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2655 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2656 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2657 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2658 | int64_t Alignment; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2659 | if (parseAbsoluteExpression(Alignment)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2660 | return true; |
| 2661 | |
| 2662 | SMLoc MaxBytesLoc; |
| 2663 | bool HasFillExpr = false; |
| 2664 | int64_t FillExpr = 0; |
| 2665 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2666 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2667 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2668 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2669 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2670 | |
| 2671 | // The fill expression can be omitted while specifying a maximum number of |
| 2672 | // alignment bytes, e.g: |
| 2673 | // .align 3,,4 |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2674 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2675 | HasFillExpr = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2676 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2677 | return true; |
| 2678 | } |
| 2679 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2680 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2681 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2682 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2683 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2684 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2685 | MaxBytesLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2686 | if (parseAbsoluteExpression(MaxBytesToFill)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2687 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2688 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2689 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2690 | return TokError("unexpected token in directive"); |
| 2691 | } |
| 2692 | } |
| 2693 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2694 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2695 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2696 | if (!HasFillExpr) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2697 | FillExpr = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2698 | |
| 2699 | // Compute alignment in bytes. |
| 2700 | if (IsPow2) { |
| 2701 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2702 | if (Alignment >= 32) { |
| 2703 | Error(AlignmentLoc, "invalid alignment value"); |
| 2704 | Alignment = 31; |
| 2705 | } |
| 2706 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2707 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2708 | } else { |
| 2709 | // Reject alignments that aren't a power of two, for gas compatibility. |
| 2710 | if (!isPowerOf2_64(Alignment)) |
| 2711 | Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2714 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2715 | if (MaxBytesLoc.isValid()) { |
| 2716 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2717 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2718 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2719 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2723 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2724 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2725 | MaxBytesToFill = 0; |
| 2726 | } |
| 2727 | } |
| 2728 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2729 | // Check whether we should use optimal code alignment for this .align |
| 2730 | // directive. |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 2731 | const MCSection *Section = getStreamer().getCurrentSection().first; |
| 2732 | assert(Section && "must have section to emit alignment"); |
| 2733 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2734 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2735 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2736 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2737 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2738 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2739 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2740 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2741 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2742 | |
| 2743 | return false; |
| 2744 | } |
| 2745 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2746 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2747 | /// ::= .file [number] filename |
| 2748 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2749 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2750 | // FIXME: I'm not sure what this is. |
| 2751 | int64_t FileNumber = -1; |
| 2752 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2753 | if (getLexer().is(AsmToken::Integer)) { |
| 2754 | FileNumber = getTok().getIntVal(); |
| 2755 | Lex(); |
| 2756 | |
| 2757 | if (FileNumber < 1) |
| 2758 | return TokError("file number less than one"); |
| 2759 | } |
| 2760 | |
| 2761 | if (getLexer().isNot(AsmToken::String)) |
| 2762 | return TokError("unexpected token in '.file' directive"); |
| 2763 | |
| 2764 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2765 | // Allow the strings to have escaped octal character sequence. |
| 2766 | std::string Path = getTok().getString(); |
| 2767 | if (parseEscapedString(Path)) |
| 2768 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2769 | Lex(); |
| 2770 | |
| 2771 | StringRef Directory; |
| 2772 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2773 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2774 | if (getLexer().is(AsmToken::String)) { |
| 2775 | if (FileNumber == -1) |
| 2776 | return TokError("explicit path specified, but no file number"); |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2777 | if (parseEscapedString(FilenameData)) |
| 2778 | return true; |
| 2779 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2780 | Directory = Path; |
| 2781 | Lex(); |
| 2782 | } else { |
| 2783 | Filename = Path; |
| 2784 | } |
| 2785 | |
| 2786 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2787 | return TokError("unexpected token in '.file' directive"); |
| 2788 | |
| 2789 | if (FileNumber == -1) |
| 2790 | getStreamer().EmitFileDirective(Filename); |
| 2791 | else { |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 2792 | if (getContext().getGenDwarfForAssembly()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2793 | Error(DirectiveLoc, |
| 2794 | "input can't have .file dwarf directives when -g is " |
| 2795 | "used to generate dwarf debug info for assembly code"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2796 | |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2797 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) == |
| 2798 | 0) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2799 | Error(FileNumberLoc, "file number already allocated"); |
| 2800 | } |
| 2801 | |
| 2802 | return false; |
| 2803 | } |
| 2804 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2805 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2806 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2807 | bool AsmParser::parseDirectiveLine() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2808 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2809 | if (getLexer().isNot(AsmToken::Integer)) |
| 2810 | return TokError("unexpected token in '.line' directive"); |
| 2811 | |
| 2812 | int64_t LineNumber = getTok().getIntVal(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2813 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2814 | Lex(); |
| 2815 | |
| 2816 | // FIXME: Do something with the .line. |
| 2817 | } |
| 2818 | |
| 2819 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2820 | return TokError("unexpected token in '.line' directive"); |
| 2821 | |
| 2822 | return false; |
| 2823 | } |
| 2824 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2825 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2826 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2827 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2828 | /// The first number is a file number, must have been previously assigned with |
| 2829 | /// a .file directive, the second number is the line number and optionally the |
| 2830 | /// third number is a column position (zero if not specified). The remaining |
| 2831 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2832 | bool AsmParser::parseDirectiveLoc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2833 | if (getLexer().isNot(AsmToken::Integer)) |
| 2834 | return TokError("unexpected token in '.loc' directive"); |
| 2835 | int64_t FileNumber = getTok().getIntVal(); |
| 2836 | if (FileNumber < 1) |
| 2837 | return TokError("file number less than one in '.loc' directive"); |
| 2838 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2839 | return TokError("unassigned file number in '.loc' directive"); |
| 2840 | Lex(); |
| 2841 | |
| 2842 | int64_t LineNumber = 0; |
| 2843 | if (getLexer().is(AsmToken::Integer)) { |
| 2844 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 2845 | if (LineNumber < 0) |
| 2846 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2847 | Lex(); |
| 2848 | } |
| 2849 | |
| 2850 | int64_t ColumnPos = 0; |
| 2851 | if (getLexer().is(AsmToken::Integer)) { |
| 2852 | ColumnPos = getTok().getIntVal(); |
| 2853 | if (ColumnPos < 0) |
| 2854 | return TokError("column position less than zero in '.loc' directive"); |
| 2855 | Lex(); |
| 2856 | } |
| 2857 | |
| 2858 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2859 | unsigned Isa = 0; |
| 2860 | int64_t Discriminator = 0; |
| 2861 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2862 | for (;;) { |
| 2863 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2864 | break; |
| 2865 | |
| 2866 | StringRef Name; |
| 2867 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2868 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2869 | return TokError("unexpected token in '.loc' directive"); |
| 2870 | |
| 2871 | if (Name == "basic_block") |
| 2872 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2873 | else if (Name == "prologue_end") |
| 2874 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2875 | else if (Name == "epilogue_begin") |
| 2876 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2877 | else if (Name == "is_stmt") { |
| 2878 | Loc = getTok().getLoc(); |
| 2879 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2880 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2881 | return true; |
| 2882 | // The expression must be the constant 0 or 1. |
| 2883 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2884 | int Value = MCE->getValue(); |
| 2885 | if (Value == 0) |
| 2886 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2887 | else if (Value == 1) |
| 2888 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2889 | else |
| 2890 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2891 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2892 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2893 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2894 | } else if (Name == "isa") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2895 | Loc = getTok().getLoc(); |
| 2896 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2897 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2898 | return true; |
| 2899 | // The expression must be a constant greater or equal to 0. |
| 2900 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2901 | int Value = MCE->getValue(); |
| 2902 | if (Value < 0) |
| 2903 | return Error(Loc, "isa number less than zero"); |
| 2904 | Isa = Value; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2905 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2906 | return Error(Loc, "isa number not a constant value"); |
| 2907 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2908 | } else if (Name == "discriminator") { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2909 | if (parseAbsoluteExpression(Discriminator)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2910 | return true; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2911 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2912 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2913 | } |
| 2914 | |
| 2915 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2916 | break; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 2921 | Isa, Discriminator, StringRef()); |
| 2922 | |
| 2923 | return false; |
| 2924 | } |
| 2925 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2926 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2927 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2928 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2929 | return TokError("unsupported directive '.stabs'"); |
| 2930 | } |
| 2931 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2932 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2933 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2934 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2935 | StringRef Name; |
| 2936 | bool EH = false; |
| 2937 | bool Debug = false; |
| 2938 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2939 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2940 | return TokError("Expected an identifier"); |
| 2941 | |
| 2942 | if (Name == ".eh_frame") |
| 2943 | EH = true; |
| 2944 | else if (Name == ".debug_frame") |
| 2945 | Debug = true; |
| 2946 | |
| 2947 | if (getLexer().is(AsmToken::Comma)) { |
| 2948 | Lex(); |
| 2949 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2950 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2951 | return TokError("Expected an identifier"); |
| 2952 | |
| 2953 | if (Name == ".eh_frame") |
| 2954 | EH = true; |
| 2955 | else if (Name == ".debug_frame") |
| 2956 | Debug = true; |
| 2957 | } |
| 2958 | |
| 2959 | getStreamer().EmitCFISections(EH, Debug); |
| 2960 | return false; |
| 2961 | } |
| 2962 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2963 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2964 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2965 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2966 | StringRef Simple; |
| 2967 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2968 | if (parseIdentifier(Simple) || Simple != "simple") |
| 2969 | return TokError("unexpected token in .cfi_startproc directive"); |
| 2970 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 2971 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2972 | return false; |
| 2973 | } |
| 2974 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2975 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2976 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2977 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2978 | getStreamer().EmitCFIEndProc(); |
| 2979 | return false; |
| 2980 | } |
| 2981 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2982 | /// \brief parse register name or number. |
| 2983 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2984 | SMLoc DirectiveLoc) { |
| 2985 | unsigned RegNo; |
| 2986 | |
| 2987 | if (getLexer().isNot(AsmToken::Integer)) { |
| 2988 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 2989 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 2990 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2991 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2992 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2993 | |
| 2994 | return false; |
| 2995 | } |
| 2996 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2997 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2998 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2999 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3000 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3001 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3002 | return true; |
| 3003 | |
| 3004 | if (getLexer().isNot(AsmToken::Comma)) |
| 3005 | return TokError("unexpected token in directive"); |
| 3006 | Lex(); |
| 3007 | |
| 3008 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3009 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3010 | return true; |
| 3011 | |
| 3012 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3013 | return false; |
| 3014 | } |
| 3015 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3016 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3017 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3018 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3019 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3020 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3021 | return true; |
| 3022 | |
| 3023 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3024 | return false; |
| 3025 | } |
| 3026 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3027 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3028 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3029 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3030 | int64_t Register1 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3031 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3032 | return true; |
| 3033 | |
| 3034 | if (getLexer().isNot(AsmToken::Comma)) |
| 3035 | return TokError("unexpected token in directive"); |
| 3036 | Lex(); |
| 3037 | |
| 3038 | int64_t Register2 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3039 | if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3040 | return true; |
| 3041 | |
| 3042 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3043 | return false; |
| 3044 | } |
| 3045 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3046 | /// parseDirectiveCFIWindowSave |
| 3047 | /// ::= .cfi_window_save |
| 3048 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3049 | getStreamer().EmitCFIWindowSave(); |
| 3050 | return false; |
| 3051 | } |
| 3052 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3053 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3054 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3055 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3056 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3057 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3058 | return true; |
| 3059 | |
| 3060 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3061 | return false; |
| 3062 | } |
| 3063 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3064 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3065 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3066 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3067 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3068 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3069 | return true; |
| 3070 | |
| 3071 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3072 | return false; |
| 3073 | } |
| 3074 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3075 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3076 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3077 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3078 | int64_t Register = 0; |
| 3079 | int64_t Offset = 0; |
| 3080 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3081 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3082 | return true; |
| 3083 | |
| 3084 | if (getLexer().isNot(AsmToken::Comma)) |
| 3085 | return TokError("unexpected token in directive"); |
| 3086 | Lex(); |
| 3087 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3088 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3089 | return true; |
| 3090 | |
| 3091 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3095 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3096 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3097 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3098 | int64_t Register = 0; |
| 3099 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3100 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3101 | return true; |
| 3102 | |
| 3103 | if (getLexer().isNot(AsmToken::Comma)) |
| 3104 | return TokError("unexpected token in directive"); |
| 3105 | Lex(); |
| 3106 | |
| 3107 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3108 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3109 | return true; |
| 3110 | |
| 3111 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3112 | return false; |
| 3113 | } |
| 3114 | |
| 3115 | static bool isValidEncoding(int64_t Encoding) { |
| 3116 | if (Encoding & ~0xff) |
| 3117 | return false; |
| 3118 | |
| 3119 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3120 | return true; |
| 3121 | |
| 3122 | const unsigned Format = Encoding & 0xf; |
| 3123 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3124 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3125 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3126 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3127 | return false; |
| 3128 | |
| 3129 | const unsigned Application = Encoding & 0x70; |
| 3130 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3131 | Application != dwarf::DW_EH_PE_pcrel) |
| 3132 | return false; |
| 3133 | |
| 3134 | return true; |
| 3135 | } |
| 3136 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3137 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3138 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3139 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3140 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3141 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3142 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3143 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3144 | return true; |
| 3145 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3146 | return false; |
| 3147 | |
| 3148 | if (!isValidEncoding(Encoding)) |
| 3149 | return TokError("unsupported encoding."); |
| 3150 | |
| 3151 | if (getLexer().isNot(AsmToken::Comma)) |
| 3152 | return TokError("unexpected token in directive"); |
| 3153 | Lex(); |
| 3154 | |
| 3155 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3156 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3157 | return TokError("expected identifier in directive"); |
| 3158 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3159 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3160 | |
| 3161 | if (IsPersonality) |
| 3162 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3163 | else |
| 3164 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3165 | return false; |
| 3166 | } |
| 3167 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3168 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3169 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3170 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3171 | getStreamer().EmitCFIRememberState(); |
| 3172 | return false; |
| 3173 | } |
| 3174 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3175 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3176 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3177 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3178 | getStreamer().EmitCFIRestoreState(); |
| 3179 | return false; |
| 3180 | } |
| 3181 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3182 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3183 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3184 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3185 | int64_t Register = 0; |
| 3186 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3187 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3188 | return true; |
| 3189 | |
| 3190 | getStreamer().EmitCFISameValue(Register); |
| 3191 | return false; |
| 3192 | } |
| 3193 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3194 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3195 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3196 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3197 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3198 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3199 | return true; |
| 3200 | |
| 3201 | getStreamer().EmitCFIRestore(Register); |
| 3202 | return false; |
| 3203 | } |
| 3204 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3205 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3206 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3207 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3208 | std::string Values; |
| 3209 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3210 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3211 | return true; |
| 3212 | |
| 3213 | Values.push_back((uint8_t)CurrValue); |
| 3214 | |
| 3215 | while (getLexer().is(AsmToken::Comma)) { |
| 3216 | Lex(); |
| 3217 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3218 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3219 | return true; |
| 3220 | |
| 3221 | Values.push_back((uint8_t)CurrValue); |
| 3222 | } |
| 3223 | |
| 3224 | getStreamer().EmitCFIEscape(Values); |
| 3225 | return false; |
| 3226 | } |
| 3227 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3228 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3229 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3230 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3231 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3232 | return Error(getLexer().getLoc(), |
| 3233 | "unexpected token in '.cfi_signal_frame'"); |
| 3234 | |
| 3235 | getStreamer().EmitCFISignalFrame(); |
| 3236 | return false; |
| 3237 | } |
| 3238 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3239 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3240 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3241 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3242 | int64_t Register = 0; |
| 3243 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3244 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3245 | return true; |
| 3246 | |
| 3247 | getStreamer().EmitCFIUndefined(Register); |
| 3248 | return false; |
| 3249 | } |
| 3250 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3251 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3252 | /// ::= .macros_on |
| 3253 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3254 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3255 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3256 | return Error(getLexer().getLoc(), |
| 3257 | "unexpected token in '" + Directive + "' directive"); |
| 3258 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3259 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3260 | return false; |
| 3261 | } |
| 3262 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3263 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3264 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3265 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3266 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3267 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3268 | return TokError("expected identifier in '.macro' directive"); |
| 3269 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3270 | if (getLexer().is(AsmToken::Comma)) |
| 3271 | Lex(); |
| 3272 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3273 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3274 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3275 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3276 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3277 | return Error(Lexer.getLoc(), |
| 3278 | "Vararg parameter '" + Parameters.back().Name + |
| 3279 | "' should be last one in the list of parameters."); |
| 3280 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3281 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3282 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3283 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3284 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3285 | if (Lexer.is(AsmToken::Colon)) { |
| 3286 | Lex(); // consume ':' |
| 3287 | |
| 3288 | SMLoc QualLoc; |
| 3289 | StringRef Qualifier; |
| 3290 | |
| 3291 | QualLoc = Lexer.getLoc(); |
| 3292 | if (parseIdentifier(Qualifier)) |
| 3293 | return Error(QualLoc, "missing parameter qualifier for " |
| 3294 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3295 | |
| 3296 | if (Qualifier == "req") |
| 3297 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 3298 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3299 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3300 | else |
| 3301 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 3302 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3303 | } |
| 3304 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3305 | if (getLexer().is(AsmToken::Equal)) { |
| 3306 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3307 | |
| 3308 | SMLoc ParamLoc; |
| 3309 | |
| 3310 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3311 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3312 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3313 | |
| 3314 | if (Parameter.Required) |
| 3315 | Warning(ParamLoc, "pointless default value for required parameter " |
| 3316 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3317 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3318 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3319 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (getLexer().is(AsmToken::Comma)) |
| 3322 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | // Eat the end of statement. |
| 3326 | Lex(); |
| 3327 | |
| 3328 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3329 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3330 | |
| 3331 | // Lex the macro definition. |
| 3332 | for (;;) { |
| 3333 | // Check whether we have reached the end of the file. |
| 3334 | if (getLexer().is(AsmToken::Eof)) |
| 3335 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3336 | |
| 3337 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3338 | if (getLexer().is(AsmToken::Identifier)) { |
| 3339 | if (getTok().getIdentifier() == ".endm" || |
| 3340 | getTok().getIdentifier() == ".endmacro") { |
| 3341 | if (MacroDepth == 0) { // Outermost macro. |
| 3342 | EndToken = getTok(); |
| 3343 | Lex(); |
| 3344 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3345 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3346 | "' directive"); |
| 3347 | break; |
| 3348 | } else { |
| 3349 | // Otherwise we just found the end of an inner macro. |
| 3350 | --MacroDepth; |
| 3351 | } |
| 3352 | } else if (getTok().getIdentifier() == ".macro") { |
| 3353 | // We allow nested macros. Those aren't instantiated until the outermost |
| 3354 | // macro is expanded so just ignore them for now. |
| 3355 | ++MacroDepth; |
| 3356 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3360 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3363 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3364 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3365 | } |
| 3366 | |
| 3367 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3368 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3369 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3370 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3371 | defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters))); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3372 | return false; |
| 3373 | } |
| 3374 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3375 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3376 | /// |
| 3377 | /// With the support added for named parameters there may be code out there that |
| 3378 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3379 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3380 | /// 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] | 3381 | /// 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] | 3382 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3383 | /// warning that the positional parameter found in body which have no effect. |
| 3384 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3385 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3386 | /// intended or change the macro to use the named parameters. It is possible |
| 3387 | /// this warning will trigger when the none of the named parameters are used |
| 3388 | /// 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] | 3389 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3390 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 3391 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3392 | // If this macro is not defined with named parameters the warning we are |
| 3393 | // checking for here doesn't apply. |
| 3394 | unsigned NParameters = Parameters.size(); |
| 3395 | if (NParameters == 0) |
| 3396 | return; |
| 3397 | |
| 3398 | bool NamedParametersFound = false; |
| 3399 | bool PositionalParametersFound = false; |
| 3400 | |
| 3401 | // Look at the body of the macro for use of both the named parameters and what |
| 3402 | // are likely to be positional parameters. This is what expandMacro() is |
| 3403 | // doing when it finds the parameters in the body. |
| 3404 | while (!Body.empty()) { |
| 3405 | // Scan for the next possible parameter. |
| 3406 | std::size_t End = Body.size(), Pos = 0; |
| 3407 | for (; Pos != End; ++Pos) { |
| 3408 | // Check for a substitution or escape. |
| 3409 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3410 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3411 | break; |
| 3412 | |
| 3413 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3414 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3415 | continue; |
| 3416 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 3417 | if (Next == '$' || Next == 'n' || |
| 3418 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | } |
| 3421 | |
| 3422 | // Check if we reached the end. |
| 3423 | if (Pos == End) |
| 3424 | break; |
| 3425 | |
| 3426 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3427 | switch (Body[Pos + 1]) { |
| 3428 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3429 | case '$': |
| 3430 | break; |
| 3431 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3432 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3433 | case 'n': |
| 3434 | PositionalParametersFound = true; |
| 3435 | break; |
| 3436 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3437 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3438 | default: { |
| 3439 | PositionalParametersFound = true; |
| 3440 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3441 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3442 | } |
| 3443 | Pos += 2; |
| 3444 | } else { |
| 3445 | unsigned I = Pos + 1; |
| 3446 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3447 | ++I; |
| 3448 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3449 | const char *Begin = Body.data() + Pos + 1; |
| 3450 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3451 | unsigned Index = 0; |
| 3452 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3453 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3454 | break; |
| 3455 | |
| 3456 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3457 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 3458 | Pos += 3; |
| 3459 | else { |
| 3460 | Pos = I; |
| 3461 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3462 | } else { |
| 3463 | NamedParametersFound = true; |
| 3464 | Pos += 1 + Argument.size(); |
| 3465 | } |
| 3466 | } |
| 3467 | // Update the scan point. |
| 3468 | Body = Body.substr(Pos); |
| 3469 | } |
| 3470 | |
| 3471 | if (!NamedParametersFound && PositionalParametersFound) |
| 3472 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3473 | "used in macro body, possible positional parameter " |
| 3474 | "found in body which will have no effect"); |
| 3475 | } |
| 3476 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 3477 | /// parseDirectiveExitMacro |
| 3478 | /// ::= .exitm |
| 3479 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
| 3480 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3481 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3482 | |
| 3483 | if (!isInsideMacroInstantiation()) |
| 3484 | return TokError("unexpected '" + Directive + "' in file, " |
| 3485 | "no current macro definition"); |
| 3486 | |
| 3487 | // Exit all conditionals that are active in the current macro. |
| 3488 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 3489 | TheCondState = TheCondStack.back(); |
| 3490 | TheCondStack.pop_back(); |
| 3491 | } |
| 3492 | |
| 3493 | handleMacroExit(); |
| 3494 | return false; |
| 3495 | } |
| 3496 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3497 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3498 | /// ::= .endm |
| 3499 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3500 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3501 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3502 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3503 | |
| 3504 | // If we are inside a macro instantiation, terminate the current |
| 3505 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3506 | if (isInsideMacroInstantiation()) { |
| 3507 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3508 | return false; |
| 3509 | } |
| 3510 | |
| 3511 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3512 | // .endmacro directives are handled during the macro definition parsing. |
| 3513 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3514 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3517 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3518 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3519 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3520 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3521 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3522 | return TokError("expected identifier in '.purgem' directive"); |
| 3523 | |
| 3524 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3525 | return TokError("unexpected token in '.purgem' directive"); |
| 3526 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3527 | if (!lookupMacro(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3528 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3529 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3530 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3531 | return false; |
| 3532 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3533 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3534 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3535 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3536 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3537 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3538 | |
| 3539 | // Expect a single argument: an expression that evaluates to a constant |
| 3540 | // in the inclusive range 0-30. |
| 3541 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3542 | int64_t AlignSizePow2; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3543 | if (parseAbsoluteExpression(AlignSizePow2)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3544 | return true; |
| 3545 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3546 | return TokError("unexpected token after expression in" |
| 3547 | " '.bundle_align_mode' directive"); |
| 3548 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3549 | return Error(ExprLoc, |
| 3550 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3551 | |
| 3552 | Lex(); |
| 3553 | |
| 3554 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3555 | // unsigned. |
| 3556 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3557 | return false; |
| 3558 | } |
| 3559 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3560 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3561 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3562 | bool AsmParser::parseDirectiveBundleLock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3563 | checkForValidSection(); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3564 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3565 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3566 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3567 | StringRef Option; |
| 3568 | SMLoc Loc = getTok().getLoc(); |
| 3569 | const char *kInvalidOptionError = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3570 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3571 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3572 | if (parseIdentifier(Option)) |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3573 | return Error(Loc, kInvalidOptionError); |
| 3574 | |
| 3575 | if (Option != "align_to_end") |
| 3576 | return Error(Loc, kInvalidOptionError); |
| 3577 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3578 | return Error(Loc, |
| 3579 | "unexpected token after '.bundle_lock' directive option"); |
| 3580 | AlignToEnd = true; |
| 3581 | } |
| 3582 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3583 | Lex(); |
| 3584 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3585 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3586 | return false; |
| 3587 | } |
| 3588 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3589 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3590 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3591 | bool AsmParser::parseDirectiveBundleUnlock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3592 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3593 | |
| 3594 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3595 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3596 | Lex(); |
| 3597 | |
| 3598 | getStreamer().EmitBundleUnlock(); |
| 3599 | return false; |
| 3600 | } |
| 3601 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3602 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3603 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3604 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3605 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3606 | |
| 3607 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3608 | if (parseAbsoluteExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3609 | return true; |
| 3610 | |
| 3611 | int64_t FillExpr = 0; |
| 3612 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3613 | if (getLexer().isNot(AsmToken::Comma)) |
| 3614 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3615 | Lex(); |
| 3616 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3617 | if (parseAbsoluteExpression(FillExpr)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3618 | return true; |
| 3619 | |
| 3620 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3621 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3622 | } |
| 3623 | |
| 3624 | Lex(); |
| 3625 | |
| 3626 | if (NumBytes <= 0) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3627 | return TokError("invalid number of bytes in '" + Twine(IDVal) + |
| 3628 | "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | // 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] | 3631 | getStreamer().EmitFill(NumBytes, FillExpr); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3632 | |
| 3633 | return false; |
| 3634 | } |
| 3635 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3636 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3637 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3638 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3639 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3640 | const MCExpr *Value; |
| 3641 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3642 | for (;;) { |
| 3643 | if (parseExpression(Value)) |
| 3644 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3645 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3646 | if (Signed) |
| 3647 | getStreamer().EmitSLEB128Value(Value); |
| 3648 | else |
| 3649 | getStreamer().EmitULEB128Value(Value); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3650 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3651 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3652 | break; |
| 3653 | |
| 3654 | if (getLexer().isNot(AsmToken::Comma)) |
| 3655 | return TokError("unexpected token in directive"); |
| 3656 | Lex(); |
| 3657 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3658 | |
| 3659 | return false; |
| 3660 | } |
| 3661 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3662 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3663 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3664 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3665 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3666 | for (;;) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3667 | StringRef Name; |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3668 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3669 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3670 | if (parseIdentifier(Name)) |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3671 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3672 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3673 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3674 | |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3675 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3676 | if (Sym->isTemporary()) |
| 3677 | return Error(Loc, "non-local symbol required in directive"); |
| 3678 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 3679 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 3680 | return Error(Loc, "unable to emit symbol attribute"); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3681 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3682 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3683 | break; |
| 3684 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3685 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3686 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3687 | Lex(); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3688 | } |
| 3689 | } |
| 3690 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3691 | Lex(); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3692 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3693 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3694 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3695 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3696 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3697 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3698 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3699 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3700 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3701 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3702 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3703 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3704 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3705 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3706 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3707 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3708 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3709 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3710 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3711 | |
| 3712 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3713 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3714 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3715 | return true; |
| 3716 | |
| 3717 | int64_t Pow2Alignment = 0; |
| 3718 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3719 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3720 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3721 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3722 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3723 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3724 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3725 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3726 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3727 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3728 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3729 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3730 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3731 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3732 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3733 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3734 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3735 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3736 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3737 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3738 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3739 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3740 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3741 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3742 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3743 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3744 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3745 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3746 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3747 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3748 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3749 | // 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] | 3750 | // may internally end up wanting an alignment in bytes. |
| 3751 | // FIXME: Diagnose overflow. |
| 3752 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3753 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3754 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3755 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3756 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3757 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3758 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3759 | // 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] | 3760 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3761 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3762 | return false; |
| 3763 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3764 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3765 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3766 | return false; |
| 3767 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3768 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3769 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3770 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3771 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3772 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3773 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3774 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3775 | StringRef Str = parseStringToEndOfStatement(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3776 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3777 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3778 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3779 | Lex(); |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3780 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3781 | if (Str.empty()) |
| 3782 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3783 | else |
| 3784 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3785 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3786 | |
| 3787 | return false; |
| 3788 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3789 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3790 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3791 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3792 | bool AsmParser::parseDirectiveInclude() { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3793 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3794 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3795 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3796 | // Allow the strings to have escaped octal character sequence. |
| 3797 | std::string Filename; |
| 3798 | if (parseEscapedString(Filename)) |
| 3799 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3800 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3801 | Lex(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3802 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3803 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3804 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3805 | |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3806 | // Attempt to switch the lexer to the included file before consuming the end |
| 3807 | // of statement to avoid losing it when we switch. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3808 | if (enterIncludeFile(Filename)) { |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3809 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3810 | return true; |
| 3811 | } |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3812 | |
| 3813 | return false; |
| 3814 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3815 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3816 | /// parseDirectiveIncbin |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3817 | /// ::= .incbin "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3818 | bool AsmParser::parseDirectiveIncbin() { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3819 | if (getLexer().isNot(AsmToken::String)) |
| 3820 | return TokError("expected string in '.incbin' directive"); |
| 3821 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3822 | // Allow the strings to have escaped octal character sequence. |
| 3823 | std::string Filename; |
| 3824 | if (parseEscapedString(Filename)) |
| 3825 | return true; |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3826 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3827 | Lex(); |
| 3828 | |
| 3829 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3830 | return TokError("unexpected token in '.incbin' directive"); |
| 3831 | |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3832 | // Attempt to process the included file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3833 | if (processIncbinFile(Filename)) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3834 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3835 | return true; |
| 3836 | } |
| 3837 | |
| 3838 | return false; |
| 3839 | } |
| 3840 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3841 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3842 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 3843 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3844 | TheCondStack.push_back(TheCondState); |
| 3845 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3846 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3847 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3848 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3849 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3850 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3851 | return true; |
| 3852 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3853 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3854 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3855 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3856 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3857 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3858 | switch (DirKind) { |
| 3859 | default: |
| 3860 | llvm_unreachable("unsupported directive"); |
| 3861 | case DK_IF: |
| 3862 | case DK_IFNE: |
| 3863 | break; |
| 3864 | case DK_IFEQ: |
| 3865 | ExprValue = ExprValue == 0; |
| 3866 | break; |
| 3867 | case DK_IFGE: |
| 3868 | ExprValue = ExprValue >= 0; |
| 3869 | break; |
| 3870 | case DK_IFGT: |
| 3871 | ExprValue = ExprValue > 0; |
| 3872 | break; |
| 3873 | case DK_IFLE: |
| 3874 | ExprValue = ExprValue <= 0; |
| 3875 | break; |
| 3876 | case DK_IFLT: |
| 3877 | ExprValue = ExprValue < 0; |
| 3878 | break; |
| 3879 | } |
| 3880 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3881 | TheCondState.CondMet = ExprValue; |
| 3882 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3883 | } |
| 3884 | |
| 3885 | return false; |
| 3886 | } |
| 3887 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3888 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3889 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3890 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3891 | TheCondStack.push_back(TheCondState); |
| 3892 | TheCondState.TheCond = AsmCond::IfCond; |
| 3893 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3894 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3895 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3896 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3897 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3898 | |
| 3899 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3900 | return TokError("unexpected token in '.ifb' directive"); |
| 3901 | |
| 3902 | Lex(); |
| 3903 | |
| 3904 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 3905 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3906 | } |
| 3907 | |
| 3908 | return false; |
| 3909 | } |
| 3910 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3911 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3912 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 3913 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3914 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3915 | TheCondStack.push_back(TheCondState); |
| 3916 | TheCondState.TheCond = AsmCond::IfCond; |
| 3917 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3918 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3919 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3920 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3921 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3922 | |
| 3923 | if (getLexer().isNot(AsmToken::Comma)) |
| 3924 | return TokError("unexpected token in '.ifc' directive"); |
| 3925 | |
| 3926 | Lex(); |
| 3927 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3928 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3929 | |
| 3930 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3931 | return TokError("unexpected token in '.ifc' directive"); |
| 3932 | |
| 3933 | Lex(); |
| 3934 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 3935 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3936 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3937 | } |
| 3938 | |
| 3939 | return false; |
| 3940 | } |
| 3941 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3942 | /// parseDirectiveIfeqs |
| 3943 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3944 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3945 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3946 | if (ExpectEqual) |
| 3947 | TokError("expected string parameter for '.ifeqs' directive"); |
| 3948 | else |
| 3949 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3950 | eatToEndOfStatement(); |
| 3951 | return true; |
| 3952 | } |
| 3953 | |
| 3954 | StringRef String1 = getTok().getStringContents(); |
| 3955 | Lex(); |
| 3956 | |
| 3957 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3958 | if (ExpectEqual) |
| 3959 | TokError("expected comma after first string for '.ifeqs' directive"); |
| 3960 | else |
| 3961 | TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3962 | eatToEndOfStatement(); |
| 3963 | return true; |
| 3964 | } |
| 3965 | |
| 3966 | Lex(); |
| 3967 | |
| 3968 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3969 | if (ExpectEqual) |
| 3970 | TokError("expected string parameter for '.ifeqs' directive"); |
| 3971 | else |
| 3972 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3973 | eatToEndOfStatement(); |
| 3974 | return true; |
| 3975 | } |
| 3976 | |
| 3977 | StringRef String2 = getTok().getStringContents(); |
| 3978 | Lex(); |
| 3979 | |
| 3980 | TheCondStack.push_back(TheCondState); |
| 3981 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3982 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3983 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3984 | |
| 3985 | return false; |
| 3986 | } |
| 3987 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3988 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3989 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3990 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3991 | StringRef Name; |
| 3992 | TheCondStack.push_back(TheCondState); |
| 3993 | TheCondState.TheCond = AsmCond::IfCond; |
| 3994 | |
| 3995 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3996 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3997 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3998 | if (parseIdentifier(Name)) |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3999 | return TokError("expected identifier after '.ifdef'"); |
| 4000 | |
| 4001 | Lex(); |
| 4002 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4003 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4004 | |
| 4005 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4006 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4007 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4008 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4009 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4010 | } |
| 4011 | |
| 4012 | return false; |
| 4013 | } |
| 4014 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4015 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4016 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4017 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4018 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4019 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4020 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 4021 | " an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4022 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4023 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4024 | bool LastIgnoreState = false; |
| 4025 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4026 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4027 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4028 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4029 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4030 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4031 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4032 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4033 | return true; |
| 4034 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4035 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4036 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4037 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4038 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4039 | TheCondState.CondMet = ExprValue; |
| 4040 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4041 | } |
| 4042 | |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4046 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4047 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4048 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4049 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4050 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4051 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4052 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4053 | |
| 4054 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4055 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4056 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 4057 | ".elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4058 | TheCondState.TheCond = AsmCond::ElseCond; |
| 4059 | bool LastIgnoreState = false; |
| 4060 | if (!TheCondStack.empty()) |
| 4061 | LastIgnoreState = TheCondStack.back().Ignore; |
| 4062 | if (LastIgnoreState || TheCondState.CondMet) |
| 4063 | TheCondState.Ignore = true; |
| 4064 | else |
| 4065 | TheCondState.Ignore = false; |
| 4066 | |
| 4067 | return false; |
| 4068 | } |
| 4069 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4070 | /// parseDirectiveEnd |
| 4071 | /// ::= .end |
| 4072 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
| 4073 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4074 | return TokError("unexpected token in '.end' directive"); |
| 4075 | |
| 4076 | Lex(); |
| 4077 | |
| 4078 | while (Lexer.isNot(AsmToken::Eof)) |
| 4079 | Lex(); |
| 4080 | |
| 4081 | return false; |
| 4082 | } |
| 4083 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4084 | /// parseDirectiveError |
| 4085 | /// ::= .err |
| 4086 | /// ::= .error [string] |
| 4087 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 4088 | if (!TheCondStack.empty()) { |
| 4089 | if (TheCondStack.back().Ignore) { |
| 4090 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4091 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | if (!WithMessage) |
| 4096 | return Error(L, ".err encountered"); |
| 4097 | |
| 4098 | StringRef Message = ".error directive invoked in source file"; |
| 4099 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4100 | if (Lexer.isNot(AsmToken::String)) { |
| 4101 | TokError(".error argument must be a string"); |
| 4102 | eatToEndOfStatement(); |
| 4103 | return true; |
| 4104 | } |
| 4105 | |
| 4106 | Message = getTok().getStringContents(); |
| 4107 | Lex(); |
| 4108 | } |
| 4109 | |
| 4110 | Error(L, Message); |
| 4111 | return true; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4114 | /// parseDirectiveWarning |
| 4115 | /// ::= .warning [string] |
| 4116 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 4117 | if (!TheCondStack.empty()) { |
| 4118 | if (TheCondStack.back().Ignore) { |
| 4119 | eatToEndOfStatement(); |
| 4120 | return false; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | StringRef Message = ".warning directive invoked in source file"; |
| 4125 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4126 | if (Lexer.isNot(AsmToken::String)) { |
| 4127 | TokError(".warning argument must be a string"); |
| 4128 | eatToEndOfStatement(); |
| 4129 | return true; |
| 4130 | } |
| 4131 | |
| 4132 | Message = getTok().getStringContents(); |
| 4133 | Lex(); |
| 4134 | } |
| 4135 | |
| 4136 | Warning(L, Message); |
| 4137 | return false; |
| 4138 | } |
| 4139 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4140 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4141 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4142 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4143 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4144 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4145 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4146 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4147 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4148 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4149 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 4150 | ".else"); |
| 4151 | if (!TheCondStack.empty()) { |
| 4152 | TheCondState = TheCondStack.back(); |
| 4153 | TheCondStack.pop_back(); |
| 4154 | } |
| 4155 | |
| 4156 | return false; |
| 4157 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 4158 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4159 | void AsmParser::initializeDirectiveKindMap() { |
| 4160 | DirectiveKindMap[".set"] = DK_SET; |
| 4161 | DirectiveKindMap[".equ"] = DK_EQU; |
| 4162 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 4163 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 4164 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 4165 | DirectiveKindMap[".string"] = DK_STRING; |
| 4166 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 4167 | DirectiveKindMap[".short"] = DK_SHORT; |
| 4168 | DirectiveKindMap[".value"] = DK_VALUE; |
| 4169 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 4170 | DirectiveKindMap[".long"] = DK_LONG; |
| 4171 | DirectiveKindMap[".int"] = DK_INT; |
| 4172 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 4173 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 4174 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 4175 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4176 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 4177 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 4178 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 4179 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 4180 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 4181 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 4182 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 4183 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 4184 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 4185 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 4186 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 4187 | DirectiveKindMap[".org"] = DK_ORG; |
| 4188 | DirectiveKindMap[".fill"] = DK_FILL; |
| 4189 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 4190 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 4191 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 4192 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4193 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 4194 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 4195 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 4196 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 4197 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 4198 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 4199 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 4200 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 4201 | DirectiveKindMap[".comm"] = DK_COMM; |
| 4202 | DirectiveKindMap[".common"] = DK_COMMON; |
| 4203 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 4204 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 4205 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 4206 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 4207 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 4208 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 4209 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4210 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4211 | DirectiveKindMap[".irp"] = DK_IRP; |
| 4212 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 4213 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 4214 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 4215 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 4216 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 4217 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4218 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 4219 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 4220 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 4221 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 4222 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 4223 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4224 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 4225 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 4226 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4227 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4228 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4229 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4230 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 4231 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 4232 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 4233 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 4234 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4235 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4236 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 4237 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 4238 | DirectiveKindMap[".space"] = DK_SPACE; |
| 4239 | DirectiveKindMap[".file"] = DK_FILE; |
| 4240 | DirectiveKindMap[".line"] = DK_LINE; |
| 4241 | DirectiveKindMap[".loc"] = DK_LOC; |
| 4242 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 4243 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 4244 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 4245 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 4246 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 4247 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 4248 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 4249 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 4250 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 4251 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 4252 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 4253 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 4254 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 4255 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 4256 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 4257 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 4258 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 4259 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 4260 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 4261 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 4262 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 4263 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 4264 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4265 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 4266 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 4267 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4268 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4269 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 4270 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 4271 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4272 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4273 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4274 | DirectiveKindMap[".warning"] = DK_WARNING; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4277 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4278 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4279 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4280 | unsigned NestLevel = 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4281 | for (;;) { |
| 4282 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4283 | if (getLexer().is(AsmToken::Eof)) { |
| 4284 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4285 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4288 | if (Lexer.is(AsmToken::Identifier) && |
| 4289 | (getTok().getIdentifier() == ".rept")) { |
| 4290 | ++NestLevel; |
| 4291 | } |
| 4292 | |
| 4293 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4294 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4295 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4296 | EndToken = getTok(); |
| 4297 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4298 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4299 | TokError("unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4300 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4301 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4302 | break; |
| 4303 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4304 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4307 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4308 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4312 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4313 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 4314 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4315 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4316 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 4317 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4320 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4321 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4322 | OS << ".endr\n"; |
| 4323 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 4324 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 4325 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4326 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4327 | // Create the macro instantiation object and add to the current macro |
| 4328 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 4329 | MacroInstantiation *MI = new MacroInstantiation( |
| 4330 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4331 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4332 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4333 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 4334 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 4335 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4336 | Lex(); |
| 4337 | } |
| 4338 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4339 | /// parseDirectiveRept |
| 4340 | /// ::= .rep | .rept count |
| 4341 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4342 | const MCExpr *CountExpr; |
| 4343 | SMLoc CountLoc = getTok().getLoc(); |
| 4344 | if (parseExpression(CountExpr)) |
| 4345 | return true; |
| 4346 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4347 | int64_t Count; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 4348 | if (!CountExpr->evaluateAsAbsolute(Count)) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4349 | eatToEndOfStatement(); |
| 4350 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 4351 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4352 | |
| 4353 | if (Count < 0) |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4354 | return Error(CountLoc, "Count is negative"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4355 | |
| 4356 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4357 | return TokError("unexpected token in '" + Dir + "' directive"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4358 | |
| 4359 | // Eat the end of statement. |
| 4360 | Lex(); |
| 4361 | |
| 4362 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4363 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4364 | if (!M) |
| 4365 | return true; |
| 4366 | |
| 4367 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4368 | // to hold the macro body with substitutions. |
| 4369 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4370 | raw_svector_ostream OS(Buf); |
| 4371 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4372 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 4373 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4374 | return true; |
| 4375 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4376 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4377 | |
| 4378 | return false; |
| 4379 | } |
| 4380 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4381 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4382 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4383 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4384 | MCAsmMacroParameter Parameter; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4385 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4386 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4387 | return TokError("expected identifier in '.irp' directive"); |
| 4388 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4389 | if (Lexer.isNot(AsmToken::Comma)) |
| 4390 | return TokError("expected comma in '.irp' directive"); |
| 4391 | |
| 4392 | Lex(); |
| 4393 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4394 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4395 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4396 | return true; |
| 4397 | |
| 4398 | // Eat the end of statement. |
| 4399 | Lex(); |
| 4400 | |
| 4401 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4402 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4403 | if (!M) |
| 4404 | return true; |
| 4405 | |
| 4406 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4407 | // to hold the macro body with substitutions. |
| 4408 | SmallString<256> Buf; |
| 4409 | raw_svector_ostream OS(Buf); |
| 4410 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4411 | for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4412 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 4413 | // This is undocumented, but GAS seems to support it. |
| 4414 | if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4415 | return true; |
| 4416 | } |
| 4417 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4418 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4419 | |
| 4420 | return false; |
| 4421 | } |
| 4422 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4423 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4424 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4425 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4426 | MCAsmMacroParameter Parameter; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4427 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4428 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4429 | return TokError("expected identifier in '.irpc' directive"); |
| 4430 | |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4431 | if (Lexer.isNot(AsmToken::Comma)) |
| 4432 | return TokError("expected comma in '.irpc' directive"); |
| 4433 | |
| 4434 | Lex(); |
| 4435 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4436 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4437 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4438 | return true; |
| 4439 | |
| 4440 | if (A.size() != 1 || A.front().size() != 1) |
| 4441 | return TokError("unexpected token in '.irpc' directive"); |
| 4442 | |
| 4443 | // Eat the end of statement. |
| 4444 | Lex(); |
| 4445 | |
| 4446 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4447 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4448 | if (!M) |
| 4449 | return true; |
| 4450 | |
| 4451 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4452 | // to hold the macro body with substitutions. |
| 4453 | SmallString<256> Buf; |
| 4454 | raw_svector_ostream OS(Buf); |
| 4455 | |
| 4456 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4457 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 4458 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4459 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4460 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4461 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 4462 | // This is undocumented, but GAS seems to support it. |
| 4463 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4464 | return true; |
| 4465 | } |
| 4466 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4467 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4468 | |
| 4469 | return false; |
| 4470 | } |
| 4471 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4472 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4473 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 4474 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4475 | |
| 4476 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4477 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4478 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 4479 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4480 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4481 | return false; |
| 4482 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 4483 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4484 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4485 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4486 | const MCExpr *Value; |
| 4487 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4488 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4489 | return true; |
| 4490 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4491 | if (!MCE) |
| 4492 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 4493 | uint64_t IntValue = MCE->getValue(); |
| 4494 | if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) |
| 4495 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4496 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4497 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len)); |
| 4498 | return false; |
| 4499 | } |
| 4500 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4501 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4502 | const MCExpr *Value; |
| 4503 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4504 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4505 | return true; |
| 4506 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4507 | if (!MCE) |
| 4508 | return Error(ExprLoc, "unexpected expression in align"); |
| 4509 | uint64_t IntValue = MCE->getValue(); |
| 4510 | if (!isPowerOf2_64(IntValue)) |
| 4511 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 4512 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4513 | Info.AsmRewrites->push_back( |
| 4514 | AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue))); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4515 | return false; |
| 4516 | } |
| 4517 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 4518 | // We are comparing pointers, but the pointers are relative to a single string. |
| 4519 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 4520 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 4521 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 4522 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 4523 | return -1; |
| 4524 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 4525 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4526 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4527 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 4528 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 4529 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 4530 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4531 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 4532 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4533 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4534 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4535 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 4536 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4537 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4538 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4541 | bool AsmParser::parseMSInlineAsm( |
| 4542 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
| 4543 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
| 4544 | SmallVectorImpl<std::string> &Constraints, |
| 4545 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 4546 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4547 | SmallVector<void *, 4> InputDecls; |
| 4548 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4549 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4550 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4551 | SmallVector<std::string, 4> InputConstraints; |
| 4552 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4553 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4554 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4555 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4556 | |
| 4557 | // Prime the lexer. |
| 4558 | Lex(); |
| 4559 | |
| 4560 | // While we have input, parse each statement. |
| 4561 | unsigned InputIdx = 0; |
| 4562 | unsigned OutputIdx = 0; |
| 4563 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4564 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4565 | if (parseStatement(Info, &SI)) |
Chad Rosier | f1f6a72 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4566 | return true; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4567 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4568 | if (Info.ParseError) |
| 4569 | return true; |
| 4570 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4571 | if (Info.Opcode == ~0U) |
| 4572 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4573 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4574 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4575 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4576 | // Build the list of clobbers, outputs and inputs. |
| 4577 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4578 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4579 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4580 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4581 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4582 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4583 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4584 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 4585 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 4586 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4587 | unsigned NumDefs = Desc.getNumDefs(); |
| 4588 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4589 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 4590 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4591 | continue; |
| 4592 | } |
| 4593 | |
| 4594 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4595 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4596 | if (SymName.empty()) |
| 4597 | continue; |
| 4598 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4599 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4600 | if (!OpDecl) |
| 4601 | continue; |
| 4602 | |
| 4603 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4604 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4605 | if (isOutput) { |
| 4606 | ++InputIdx; |
| 4607 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4608 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 4609 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4610 | AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size())); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4611 | } else { |
| 4612 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4613 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 4614 | InputConstraints.push_back(Operand.getConstraint().str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4615 | AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size())); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4616 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4617 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 4618 | |
| 4619 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4620 | ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(), |
| 4621 | Desc.getNumImplicitDefs()); |
| 4622 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4626 | NumOutputs = OutputDecls.size(); |
| 4627 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4628 | |
| 4629 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4630 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 4631 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 4632 | ClobberRegs.end()); |
| 4633 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 4634 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 4635 | raw_string_ostream OS(Clobbers[I]); |
| 4636 | IP->printRegName(OS, ClobberRegs[I]); |
| 4637 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4638 | |
| 4639 | // Merge the various outputs and inputs. Output are expected first. |
| 4640 | if (NumOutputs || NumInputs) { |
| 4641 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4642 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4643 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4644 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4645 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4646 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4647 | } |
| 4648 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4649 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4650 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4655 | std::string AsmStringIR; |
| 4656 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 4657 | StringRef ASMString = |
| 4658 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 4659 | const char *AsmStart = ASMString.begin(); |
| 4660 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4661 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4662 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 4663 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4664 | if (Kind == AOK_Delete) |
| 4665 | continue; |
| 4666 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4667 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4668 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4669 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 4670 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4671 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4672 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4673 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4674 | // Skip the original expression. |
| 4675 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4676 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4677 | continue; |
| 4678 | } |
| 4679 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4680 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4681 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4682 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4683 | default: |
| 4684 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4685 | case AOK_Imm: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4686 | OS << "$$" << AR.Val; |
Chad Rosier | 11c42f2 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4687 | break; |
| 4688 | case AOK_ImmPrefix: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4689 | OS << "$$"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4690 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4691 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 4692 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4693 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4694 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4695 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4696 | break; |
| 4697 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4698 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4699 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4700 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4701 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4702 | default: break; |
| 4703 | case 8: OS << "byte ptr "; break; |
| 4704 | case 16: OS << "word ptr "; break; |
| 4705 | case 32: OS << "dword ptr "; break; |
| 4706 | case 64: OS << "qword ptr "; break; |
| 4707 | case 80: OS << "xword ptr "; break; |
| 4708 | case 128: OS << "xmmword ptr "; break; |
| 4709 | case 256: OS << "ymmword ptr "; break; |
| 4710 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4711 | break; |
| 4712 | case AOK_Emit: |
| 4713 | OS << ".byte"; |
| 4714 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4715 | case AOK_Align: { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4716 | unsigned Val = AR.Val; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4717 | OS << ".align " << Val; |
| 4718 | |
| 4719 | // Skip the original immediate. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4720 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4721 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 4722 | break; |
| 4723 | } |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4724 | case AOK_DotOperator: |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4725 | // Insert the dot if the user omitted it. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4726 | OS.flush(); |
| 4727 | if (AsmStringIR.back() != '.') |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4728 | OS << '.'; |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4729 | OS << AR.Val; |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4730 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4731 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4732 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4733 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4734 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4738 | if (AsmStart != AsmEnd) |
| 4739 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4740 | |
| 4741 | AsmString = OS.str(); |
| 4742 | return false; |
| 4743 | } |
| 4744 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4745 | namespace llvm { |
| 4746 | namespace MCParserUtils { |
| 4747 | |
| 4748 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 4749 | /// or subexpressions. |
| 4750 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 4751 | switch (Value->getKind()) { |
| 4752 | case MCExpr::Binary: { |
| 4753 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 4754 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 4755 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 4756 | } |
| 4757 | case MCExpr::Target: |
| 4758 | case MCExpr::Constant: |
| 4759 | return false; |
| 4760 | case MCExpr::SymbolRef: { |
| 4761 | const MCSymbol &S = |
| 4762 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 4763 | if (S.isVariable()) |
| 4764 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 4765 | return &S == Sym; |
| 4766 | } |
| 4767 | case MCExpr::Unary: |
| 4768 | return isSymbolUsedInExpression( |
| 4769 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 4770 | } |
| 4771 | |
| 4772 | llvm_unreachable("Unknown expr kind!"); |
| 4773 | } |
| 4774 | |
| 4775 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 4776 | MCAsmParser &Parser, MCSymbol *&Sym, |
| 4777 | const MCExpr *&Value) { |
| 4778 | MCAsmLexer &Lexer = Parser.getLexer(); |
| 4779 | |
| 4780 | // FIXME: Use better location, we should use proper tokens. |
| 4781 | SMLoc EqualLoc = Lexer.getLoc(); |
| 4782 | |
| 4783 | if (Parser.parseExpression(Value)) { |
| 4784 | Parser.TokError("missing expression"); |
| 4785 | Parser.eatToEndOfStatement(); |
| 4786 | return true; |
| 4787 | } |
| 4788 | |
| 4789 | // Note: we don't count b as used in "a = b". This is to allow |
| 4790 | // a = b |
| 4791 | // b = c |
| 4792 | |
| 4793 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 4794 | return Parser.TokError("unexpected token in assignment"); |
| 4795 | |
| 4796 | // Eat the end of statement marker. |
| 4797 | Parser.Lex(); |
| 4798 | |
| 4799 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 4800 | // used as a symbol, or it is an absolute symbol). |
| 4801 | Sym = Parser.getContext().lookupSymbol(Name); |
| 4802 | if (Sym) { |
| 4803 | // Diagnose assignment to a label. |
| 4804 | // |
| 4805 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 4806 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 4807 | if (isSymbolUsedInExpression(Sym, Value)) |
| 4808 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame^] | 4809 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 4810 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4811 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 4812 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 4813 | ; // Allow redefinitions of variables that haven't yet been used. |
| 4814 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 4815 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 4816 | else if (!Sym->isVariable()) |
| 4817 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 4818 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 4819 | return Parser.Error(EqualLoc, |
| 4820 | "invalid reassignment of non-absolute variable '" + |
| 4821 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4822 | } else if (Name == ".") { |
| 4823 | if (Parser.getStreamer().EmitValueToOffset(Value, 0)) { |
| 4824 | Parser.Error(EqualLoc, "expected absolute expression"); |
| 4825 | Parser.eatToEndOfStatement(); |
| 4826 | return true; |
| 4827 | } |
| 4828 | return false; |
| 4829 | } else |
| 4830 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 4831 | |
| 4832 | Sym->setRedefinable(allow_redef); |
| 4833 | |
| 4834 | return false; |
| 4835 | } |
| 4836 | |
| 4837 | } // namespace MCParserUtils |
| 4838 | } // namespace llvm |
| 4839 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4840 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4841 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
| 4842 | MCStreamer &Out, const MCAsmInfo &MAI) { |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4843 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4844 | } |