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(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 254 | bool parseCppHashLineFilenameComment(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, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 261 | 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()) { |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 684 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 685 | MCSymbol *Sym = TableEntry.getValue(); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 686 | // Variable symbols may not be marked as defined, so check those |
| 687 | // explicitly. If we know it's a variable, we have a definition for |
| 688 | // the purposes of this check. |
| 689 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 690 | // FIXME: We would really like to refer back to where the symbol was |
| 691 | // first referenced for a source location. We need to add something |
| 692 | // to track that. Currently, we just point to the end of the file. |
Jim Grosbach | 0fdd572 | 2015-10-16 22:07:59 +0000 | [diff] [blame] | 693 | return Error(getLexer().getLoc(), "assembler local symbol '" + |
| 694 | Sym->getName() + "' not defined"); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 698 | // Finalize the output stream if there are no errors and if the client wants |
| 699 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 700 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 701 | Out.Finish(); |
| 702 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 703 | return HadError; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 706 | void AsmParser::checkForValidSection() { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 707 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 708 | TokError("expected section directive before assembly directive"); |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 709 | Out.InitSections(false); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 713 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 714 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 715 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 716 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 717 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 718 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 719 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 720 | Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 723 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 724 | const char *Start = getTok().getLoc().getPointer(); |
| 725 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 726 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 727 | Lex(); |
| 728 | |
| 729 | const char *End = getTok().getLoc().getPointer(); |
| 730 | return StringRef(Start, End - Start); |
| 731 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 732 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 733 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 734 | const char *Start = getTok().getLoc().getPointer(); |
| 735 | |
| 736 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 737 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 738 | Lex(); |
| 739 | |
| 740 | const char *End = getTok().getLoc().getPointer(); |
| 741 | return StringRef(Start, End - Start); |
| 742 | } |
| 743 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 744 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 745 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 746 | /// |
| 747 | /// parenexpr ::= expr) |
| 748 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 749 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 750 | if (parseExpression(Res)) |
| 751 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 752 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 753 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 754 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 755 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 756 | return false; |
| 757 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 758 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 759 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 760 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 761 | /// |
| 762 | /// bracketexpr ::= expr] |
| 763 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 764 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 765 | if (parseExpression(Res)) |
| 766 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 767 | if (Lexer.isNot(AsmToken::RBrac)) |
| 768 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 769 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 770 | Lex(); |
| 771 | return false; |
| 772 | } |
| 773 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 774 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 775 | /// primaryexpr ::= (parenexpr |
| 776 | /// primaryexpr ::= symbol |
| 777 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 778 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 779 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 780 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 781 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 782 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 783 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 784 | default: |
| 785 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 786 | // If we have an error assume that we've already handled it. |
| 787 | case AsmToken::Error: |
| 788 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 789 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 790 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 791 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 792 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 793 | Res = MCUnaryExpr::createLNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 794 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 795 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 796 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 797 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 798 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 799 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 800 | if (parseIdentifier(Identifier)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 801 | if (FirstTokenKind == AsmToken::Dollar) { |
| 802 | if (Lexer.getMAI().getDollarIsPC()) { |
| 803 | // This is a '$' reference, which references the current PC. Emit a |
| 804 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 805 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 806 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 807 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 808 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 809 | EndLoc = FirstTokenLoc; |
| 810 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 811 | } |
| 812 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 813 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 814 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 815 | // Parse symbol variant |
| 816 | std::pair<StringRef, StringRef> Split; |
| 817 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 818 | if (FirstTokenKind == AsmToken::String) { |
| 819 | if (Lexer.is(AsmToken::At)) { |
| 820 | Lexer.Lex(); // eat @ |
| 821 | SMLoc AtLoc = getLexer().getLoc(); |
| 822 | StringRef VName; |
| 823 | if (parseIdentifier(VName)) |
| 824 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 825 | |
| 826 | Split = std::make_pair(Identifier, VName); |
| 827 | } |
| 828 | } else { |
| 829 | Split = Identifier.split('@'); |
| 830 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 831 | } else if (Lexer.is(AsmToken::LParen)) { |
| 832 | Lexer.Lex(); // eat ( |
| 833 | StringRef VName; |
| 834 | parseIdentifier(VName); |
| 835 | if (Lexer.isNot(AsmToken::RParen)) { |
| 836 | return Error(Lexer.getTok().getLoc(), |
| 837 | "unexpected token in variant, expected ')'"); |
| 838 | } |
| 839 | Lexer.Lex(); // eat ) |
| 840 | Split = std::make_pair(Identifier, VName); |
| 841 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 842 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 843 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 844 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 845 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 846 | StringRef SymbolName = Identifier; |
| 847 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 848 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 849 | // Lookup the symbol variant if used. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 850 | if (Split.second.size()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 851 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 852 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 853 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 854 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 855 | Variant = MCSymbolRefExpr::VK_None; |
| 856 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 857 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 858 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 861 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 862 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 864 | // If this is an absolute variable reference, substitute it now to preserve |
| 865 | // semantics in the face of reassignment. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 866 | if (Sym->isVariable() && |
| 867 | isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 868 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 869 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 870 | |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 871 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | |
| 875 | // Otherwise create a symbol ref. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 876 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 877 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 878 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 879 | case AsmToken::BigNum: |
| 880 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 881 | case AsmToken::Integer: { |
| 882 | SMLoc Loc = getTok().getLoc(); |
| 883 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 884 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 885 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 886 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 887 | // Look for 'b' or 'f' following an Integer as a directional label |
| 888 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 889 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 890 | // Lookup the symbol variant if used. |
| 891 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 892 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 893 | if (Split.first.size() != IDVal.size()) { |
| 894 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 895 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 896 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 897 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 898 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 899 | if (IDVal == "f" || IDVal == "b") { |
| 900 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 901 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 902 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 903 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 904 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 905 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 906 | Lex(); // Eat identifier. |
| 907 | } |
| 908 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 909 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 910 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 911 | case AsmToken::Real: { |
| 912 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 913 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 914 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 915 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 916 | Lex(); // Eat token. |
| 917 | return false; |
| 918 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 919 | case AsmToken::Dot: { |
| 920 | // This is a '.' reference, which references the current PC. Emit a |
| 921 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 922 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 923 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 924 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 925 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 926 | Lex(); // Eat identifier. |
| 927 | return false; |
| 928 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 929 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 930 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 931 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 932 | case AsmToken::LBrac: |
| 933 | if (!PlatformParser->HasBracketExpressions()) |
| 934 | return TokError("brackets expression not supported on this target"); |
| 935 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 936 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 937 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 938 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 939 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 940 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 941 | Res = MCUnaryExpr::createMinus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 942 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 943 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 944 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 945 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 946 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 947 | Res = MCUnaryExpr::createPlus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 948 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 949 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 950 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 951 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 952 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 953 | Res = MCUnaryExpr::createNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 954 | return false; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 957 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 958 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 959 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 960 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 963 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 964 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 965 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 966 | // Ask the target implementation about this expression first. |
| 967 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 968 | if (NewE) |
| 969 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 970 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 971 | // if there is exactly one symbol. |
| 972 | switch (E->getKind()) { |
| 973 | case MCExpr::Target: |
| 974 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 975 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 976 | |
| 977 | case MCExpr::SymbolRef: { |
| 978 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 979 | |
| 980 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 981 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 982 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 983 | return E; |
| 984 | } |
| 985 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 986 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | case MCExpr::Unary: { |
| 990 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 991 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 992 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 993 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 994 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | case MCExpr::Binary: { |
| 998 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 999 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1000 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1003 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1004 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1005 | if (!LHS) |
| 1006 | LHS = BE->getLHS(); |
| 1007 | if (!RHS) |
| 1008 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1009 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1010 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1013 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1014 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1017 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1018 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1019 | /// expr ::= expr &&,|| expr -> lowest. |
| 1020 | /// expr ::= expr |,^,&,! expr |
| 1021 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1022 | /// expr ::= expr <<,>> expr |
| 1023 | /// expr ::= expr +,- expr |
| 1024 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1025 | /// expr ::= primaryexpr |
| 1026 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1027 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1028 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1029 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1030 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1031 | return true; |
| 1032 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1033 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1034 | // expression to include the modifier. This is inefficient, but in general we |
| 1035 | // expect users to use 'a@modifier op b'. |
| 1036 | if (Lexer.getKind() == AsmToken::At) { |
| 1037 | Lex(); |
| 1038 | |
| 1039 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1040 | return TokError("unexpected symbol modifier following '@'"); |
| 1041 | |
| 1042 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1043 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1044 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1045 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1046 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1047 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1048 | if (!ModifiedRes) { |
| 1049 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1050 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1051 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1052 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1053 | Res = ModifiedRes; |
| 1054 | Lex(); |
| 1055 | } |
| 1056 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1057 | // Try to constant fold it up front, if possible. |
| 1058 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1059 | if (Res->evaluateAsAbsolute(Value)) |
| 1060 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1061 | |
| 1062 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1063 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1064 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1065 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1066 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1067 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1070 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1071 | SMLoc &EndLoc) { |
| 1072 | if (parseParenExpr(Res, EndLoc)) |
| 1073 | return true; |
| 1074 | |
| 1075 | for (; ParenDepth > 0; --ParenDepth) { |
| 1076 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1077 | return true; |
| 1078 | |
| 1079 | // We don't Lex() the last RParen. |
| 1080 | // This is the same behavior as parseParenExpression(). |
| 1081 | if (ParenDepth - 1 > 0) { |
| 1082 | if (Lexer.isNot(AsmToken::RParen)) |
| 1083 | return TokError("expected ')' in parentheses expression"); |
| 1084 | EndLoc = Lexer.getTok().getEndLoc(); |
| 1085 | Lex(); |
| 1086 | } |
| 1087 | } |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1091 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1092 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1093 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1094 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1095 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1096 | return true; |
| 1097 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1098 | if (!Expr->evaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1099 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1100 | |
| 1101 | return false; |
| 1102 | } |
| 1103 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1104 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1105 | MCBinaryExpr::Opcode &Kind, |
| 1106 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1107 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1108 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1109 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1110 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1111 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1112 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1113 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1114 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1115 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1116 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1117 | return 1; |
| 1118 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1119 | // Low Precedence: |, &, ^ |
| 1120 | // |
| 1121 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1122 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1123 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1124 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1125 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1126 | Kind = MCBinaryExpr::Xor; |
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::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1129 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1130 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1131 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1132 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1133 | case AsmToken::EqualEqual: |
| 1134 | Kind = MCBinaryExpr::EQ; |
| 1135 | return 3; |
| 1136 | case AsmToken::ExclaimEqual: |
| 1137 | case AsmToken::LessGreater: |
| 1138 | Kind = MCBinaryExpr::NE; |
| 1139 | return 3; |
| 1140 | case AsmToken::Less: |
| 1141 | Kind = MCBinaryExpr::LT; |
| 1142 | return 3; |
| 1143 | case AsmToken::LessEqual: |
| 1144 | Kind = MCBinaryExpr::LTE; |
| 1145 | return 3; |
| 1146 | case AsmToken::Greater: |
| 1147 | Kind = MCBinaryExpr::GT; |
| 1148 | return 3; |
| 1149 | case AsmToken::GreaterEqual: |
| 1150 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1151 | return 3; |
| 1152 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1153 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1154 | case AsmToken::LessLess: |
| 1155 | Kind = MCBinaryExpr::Shl; |
| 1156 | return 4; |
| 1157 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1158 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1159 | return 4; |
| 1160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1161 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1162 | case AsmToken::Plus: |
| 1163 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1164 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1165 | case AsmToken::Minus: |
| 1166 | Kind = MCBinaryExpr::Sub; |
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 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1169 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1170 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1171 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1172 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1173 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1174 | Kind = MCBinaryExpr::Div; |
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::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1177 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1178 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1182 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1183 | MCBinaryExpr::Opcode &Kind, |
| 1184 | bool ShouldUseLogicalShr) { |
| 1185 | switch (K) { |
| 1186 | default: |
| 1187 | return 0; // not a binop. |
| 1188 | |
| 1189 | // Lowest Precedence: &&, || |
| 1190 | case AsmToken::AmpAmp: |
| 1191 | Kind = MCBinaryExpr::LAnd; |
| 1192 | return 2; |
| 1193 | case AsmToken::PipePipe: |
| 1194 | Kind = MCBinaryExpr::LOr; |
| 1195 | return 1; |
| 1196 | |
| 1197 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1198 | case AsmToken::EqualEqual: |
| 1199 | Kind = MCBinaryExpr::EQ; |
| 1200 | return 3; |
| 1201 | case AsmToken::ExclaimEqual: |
| 1202 | case AsmToken::LessGreater: |
| 1203 | Kind = MCBinaryExpr::NE; |
| 1204 | return 3; |
| 1205 | case AsmToken::Less: |
| 1206 | Kind = MCBinaryExpr::LT; |
| 1207 | return 3; |
| 1208 | case AsmToken::LessEqual: |
| 1209 | Kind = MCBinaryExpr::LTE; |
| 1210 | return 3; |
| 1211 | case AsmToken::Greater: |
| 1212 | Kind = MCBinaryExpr::GT; |
| 1213 | return 3; |
| 1214 | case AsmToken::GreaterEqual: |
| 1215 | Kind = MCBinaryExpr::GTE; |
| 1216 | return 3; |
| 1217 | |
| 1218 | // Low Intermediate Precedence: +, - |
| 1219 | case AsmToken::Plus: |
| 1220 | Kind = MCBinaryExpr::Add; |
| 1221 | return 4; |
| 1222 | case AsmToken::Minus: |
| 1223 | Kind = MCBinaryExpr::Sub; |
| 1224 | return 4; |
| 1225 | |
| 1226 | // High Intermediate Precedence: |, &, ^ |
| 1227 | // |
| 1228 | // FIXME: gas seems to support '!' as an infix operator? |
| 1229 | case AsmToken::Pipe: |
| 1230 | Kind = MCBinaryExpr::Or; |
| 1231 | return 5; |
| 1232 | case AsmToken::Caret: |
| 1233 | Kind = MCBinaryExpr::Xor; |
| 1234 | return 5; |
| 1235 | case AsmToken::Amp: |
| 1236 | Kind = MCBinaryExpr::And; |
| 1237 | return 5; |
| 1238 | |
| 1239 | // Highest Precedence: *, /, %, <<, >> |
| 1240 | case AsmToken::Star: |
| 1241 | Kind = MCBinaryExpr::Mul; |
| 1242 | return 6; |
| 1243 | case AsmToken::Slash: |
| 1244 | Kind = MCBinaryExpr::Div; |
| 1245 | return 6; |
| 1246 | case AsmToken::Percent: |
| 1247 | Kind = MCBinaryExpr::Mod; |
| 1248 | return 6; |
| 1249 | case AsmToken::LessLess: |
| 1250 | Kind = MCBinaryExpr::Shl; |
| 1251 | return 6; |
| 1252 | case AsmToken::GreaterGreater: |
| 1253 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1254 | return 6; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1259 | MCBinaryExpr::Opcode &Kind) { |
| 1260 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1261 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1262 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1263 | } |
| 1264 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1265 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1266 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1267 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1268 | SMLoc &EndLoc) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1269 | while (1) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1270 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1271 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1273 | // If the next token is lower precedence than we are allowed to eat, return |
| 1274 | // successfully with what we ate already. |
| 1275 | if (TokPrec < Precedence) |
| 1276 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1277 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1278 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1280 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1281 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1282 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1283 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1285 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1286 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1287 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1288 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1289 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1290 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1291 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1292 | // Merge LHS and RHS according to operator. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1293 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext()); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1297 | /// ParseStatement: |
| 1298 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1299 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1300 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1301 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1302 | MCAsmParserSemaCallback *SI) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1303 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1304 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1305 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1306 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1307 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1308 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1309 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1310 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1311 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1312 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1313 | int64_t LocalLabelVal = -1; |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1314 | // A full line comment is a '#' as the first token. |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1315 | if (Lexer.is(AsmToken::Hash)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1316 | return parseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 3f56104 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1317 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1318 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1319 | if (Lexer.is(AsmToken::Integer)) { |
| 1320 | LocalLabelVal = getTok().getIntVal(); |
| 1321 | if (LocalLabelVal < 0) { |
| 1322 | if (!TheCondState.Ignore) |
| 1323 | return TokError("unexpected token at start of statement"); |
| 1324 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1325 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1326 | IDVal = getTok().getString(); |
| 1327 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1328 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1329 | if (!TheCondState.Ignore) |
| 1330 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1333 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1334 | // Treat '.' as a valid identifier in this context. |
| 1335 | Lex(); |
| 1336 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame^] | 1337 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1338 | // Treat '{' as a valid identifier in this context. |
| 1339 | Lex(); |
| 1340 | IDVal = "{"; |
| 1341 | |
| 1342 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1343 | // Treat '}' as a valid identifier in this context. |
| 1344 | Lex(); |
| 1345 | IDVal = "}"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1346 | } else if (parseIdentifier(IDVal)) { |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1347 | if (!TheCondState.Ignore) |
| 1348 | return TokError("unexpected token at start of statement"); |
| 1349 | IDVal = ""; |
| 1350 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1352 | // Handle conditional assembly here before checking for skipping. We |
| 1353 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1354 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1355 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1356 | DirectiveKindMap.find(IDVal); |
| 1357 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1358 | ? DK_NO_DIRECTIVE |
| 1359 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1360 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1361 | default: |
| 1362 | break; |
| 1363 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1364 | case DK_IFEQ: |
| 1365 | case DK_IFGE: |
| 1366 | case DK_IFGT: |
| 1367 | case DK_IFLE: |
| 1368 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1369 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1370 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1371 | case DK_IFB: |
| 1372 | return parseDirectiveIfb(IDLoc, true); |
| 1373 | case DK_IFNB: |
| 1374 | return parseDirectiveIfb(IDLoc, false); |
| 1375 | case DK_IFC: |
| 1376 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1377 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1378 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1379 | case DK_IFNC: |
| 1380 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1381 | case DK_IFNES: |
| 1382 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1383 | case DK_IFDEF: |
| 1384 | return parseDirectiveIfdef(IDLoc, true); |
| 1385 | case DK_IFNDEF: |
| 1386 | case DK_IFNOTDEF: |
| 1387 | return parseDirectiveIfdef(IDLoc, false); |
| 1388 | case DK_ELSEIF: |
| 1389 | return parseDirectiveElseIf(IDLoc); |
| 1390 | case DK_ELSE: |
| 1391 | return parseDirectiveElse(IDLoc); |
| 1392 | case DK_ENDIF: |
| 1393 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1394 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1395 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1396 | // Ignore the statement if in the middle of inactive conditional |
| 1397 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1398 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1399 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1402 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1403 | // FIXME: Recurse on local labels? |
| 1404 | |
| 1405 | // See what kind of statement we have. |
| 1406 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1407 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1408 | if (!getTargetParser().isLabel(ID)) |
| 1409 | break; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1410 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1412 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1413 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1414 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1415 | // Diagnose attempt to use '.' as a label. |
| 1416 | if (IDVal == ".") |
| 1417 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1418 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1419 | // Diagnose attempt to use a variable as a label. |
| 1420 | // |
| 1421 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1422 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1423 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1424 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1425 | if (LocalLabelVal == -1) { |
| 1426 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1427 | StringRef RewrittenLabel = |
| 1428 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
| 1429 | assert(RewrittenLabel.size() && |
| 1430 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1431 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1432 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1433 | IDVal = RewrittenLabel; |
| 1434 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1435 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1436 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1437 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 1438 | |
| 1439 | Sym->redefineIfPossible(); |
| 1440 | |
Daniel Dunbar | deb7ba9 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1441 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1442 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1443 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1444 | // Emit the label. |
Chad Rosier | f3feab3 | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1445 | if (!ParsingInlineAsm) |
| 1446 | Out.EmitLabel(Sym); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1447 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1448 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1449 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1450 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1451 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1452 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1453 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1454 | getTargetParser().onLabelParsed(Sym); |
| 1455 | |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1456 | // Consume any end of statement token, if present, to avoid spurious |
| 1457 | // AddBlankLine calls(). |
| 1458 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1459 | Lex(); |
| 1460 | if (Lexer.is(AsmToken::Eof)) |
| 1461 | return false; |
| 1462 | } |
| 1463 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1464 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1465 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1466 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1467 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1468 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1469 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1470 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1471 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1472 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1473 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1474 | |
| 1475 | default: // Normal instruction or directive. |
| 1476 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1477 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1478 | |
| 1479 | // 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] | 1480 | if (areMacrosEnabled()) |
| 1481 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1482 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1483 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1484 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1485 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1486 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1487 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1488 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1489 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1490 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1491 | // 1. The target-specific assembly parser. Some directives are target |
| 1492 | // specific or may potentially behave differently on certain targets. |
| 1493 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1494 | // (like the ELF parser) register themselves as extensions. |
| 1495 | // 3. The generic directive parser implemented by this class. These are |
| 1496 | // all the directives that behave in a target and platform independent |
| 1497 | // manner, or at least have a default behavior that's shared between |
| 1498 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1499 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1500 | // First query the target-specific parser. It will return 'true' if it |
| 1501 | // isn't interested in this directive. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1502 | if (!getTargetParser().ParseDirective(ID)) |
| 1503 | return false; |
| 1504 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1505 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1506 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1507 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1508 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1509 | if (Handler.first) |
| 1510 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1511 | |
| 1512 | // Finally, if no one else is interested in this directive, it must be |
| 1513 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1514 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1515 | default: |
| 1516 | break; |
| 1517 | case DK_SET: |
| 1518 | case DK_EQU: |
| 1519 | return parseDirectiveSet(IDVal, true); |
| 1520 | case DK_EQUIV: |
| 1521 | return parseDirectiveSet(IDVal, false); |
| 1522 | case DK_ASCII: |
| 1523 | return parseDirectiveAscii(IDVal, false); |
| 1524 | case DK_ASCIZ: |
| 1525 | case DK_STRING: |
| 1526 | return parseDirectiveAscii(IDVal, true); |
| 1527 | case DK_BYTE: |
| 1528 | return parseDirectiveValue(1); |
| 1529 | case DK_SHORT: |
| 1530 | case DK_VALUE: |
| 1531 | case DK_2BYTE: |
| 1532 | return parseDirectiveValue(2); |
| 1533 | case DK_LONG: |
| 1534 | case DK_INT: |
| 1535 | case DK_4BYTE: |
| 1536 | return parseDirectiveValue(4); |
| 1537 | case DK_QUAD: |
| 1538 | case DK_8BYTE: |
| 1539 | return parseDirectiveValue(8); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1540 | case DK_OCTA: |
| 1541 | return parseDirectiveOctaValue(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1542 | case DK_SINGLE: |
| 1543 | case DK_FLOAT: |
| 1544 | return parseDirectiveRealValue(APFloat::IEEEsingle); |
| 1545 | case DK_DOUBLE: |
| 1546 | return parseDirectiveRealValue(APFloat::IEEEdouble); |
| 1547 | case DK_ALIGN: { |
| 1548 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1549 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1550 | } |
| 1551 | case DK_ALIGN32: { |
| 1552 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1553 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1554 | } |
| 1555 | case DK_BALIGN: |
| 1556 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1557 | case DK_BALIGNW: |
| 1558 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1559 | case DK_BALIGNL: |
| 1560 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1561 | case DK_P2ALIGN: |
| 1562 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1563 | case DK_P2ALIGNW: |
| 1564 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1565 | case DK_P2ALIGNL: |
| 1566 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1567 | case DK_ORG: |
| 1568 | return parseDirectiveOrg(); |
| 1569 | case DK_FILL: |
| 1570 | return parseDirectiveFill(); |
| 1571 | case DK_ZERO: |
| 1572 | return parseDirectiveZero(); |
| 1573 | case DK_EXTERN: |
| 1574 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1575 | return false; |
| 1576 | case DK_GLOBL: |
| 1577 | case DK_GLOBAL: |
| 1578 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1579 | case DK_LAZY_REFERENCE: |
| 1580 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1581 | case DK_NO_DEAD_STRIP: |
| 1582 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1583 | case DK_SYMBOL_RESOLVER: |
| 1584 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1585 | case DK_PRIVATE_EXTERN: |
| 1586 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1587 | case DK_REFERENCE: |
| 1588 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1589 | case DK_WEAK_DEFINITION: |
| 1590 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1591 | case DK_WEAK_REFERENCE: |
| 1592 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1593 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1594 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1595 | case DK_COMM: |
| 1596 | case DK_COMMON: |
| 1597 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1598 | case DK_LCOMM: |
| 1599 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1600 | case DK_ABORT: |
| 1601 | return parseDirectiveAbort(); |
| 1602 | case DK_INCLUDE: |
| 1603 | return parseDirectiveInclude(); |
| 1604 | case DK_INCBIN: |
| 1605 | return parseDirectiveIncbin(); |
| 1606 | case DK_CODE16: |
| 1607 | case DK_CODE16GCC: |
| 1608 | return TokError(Twine(IDVal) + " not supported yet"); |
| 1609 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1610 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1611 | case DK_IRP: |
| 1612 | return parseDirectiveIrp(IDLoc); |
| 1613 | case DK_IRPC: |
| 1614 | return parseDirectiveIrpc(IDLoc); |
| 1615 | case DK_ENDR: |
| 1616 | return parseDirectiveEndr(IDLoc); |
| 1617 | case DK_BUNDLE_ALIGN_MODE: |
| 1618 | return parseDirectiveBundleAlignMode(); |
| 1619 | case DK_BUNDLE_LOCK: |
| 1620 | return parseDirectiveBundleLock(); |
| 1621 | case DK_BUNDLE_UNLOCK: |
| 1622 | return parseDirectiveBundleUnlock(); |
| 1623 | case DK_SLEB128: |
| 1624 | return parseDirectiveLEB128(true); |
| 1625 | case DK_ULEB128: |
| 1626 | return parseDirectiveLEB128(false); |
| 1627 | case DK_SPACE: |
| 1628 | case DK_SKIP: |
| 1629 | return parseDirectiveSpace(IDVal); |
| 1630 | case DK_FILE: |
| 1631 | return parseDirectiveFile(IDLoc); |
| 1632 | case DK_LINE: |
| 1633 | return parseDirectiveLine(); |
| 1634 | case DK_LOC: |
| 1635 | return parseDirectiveLoc(); |
| 1636 | case DK_STABS: |
| 1637 | return parseDirectiveStabs(); |
| 1638 | case DK_CFI_SECTIONS: |
| 1639 | return parseDirectiveCFISections(); |
| 1640 | case DK_CFI_STARTPROC: |
| 1641 | return parseDirectiveCFIStartProc(); |
| 1642 | case DK_CFI_ENDPROC: |
| 1643 | return parseDirectiveCFIEndProc(); |
| 1644 | case DK_CFI_DEF_CFA: |
| 1645 | return parseDirectiveCFIDefCfa(IDLoc); |
| 1646 | case DK_CFI_DEF_CFA_OFFSET: |
| 1647 | return parseDirectiveCFIDefCfaOffset(); |
| 1648 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1649 | return parseDirectiveCFIAdjustCfaOffset(); |
| 1650 | case DK_CFI_DEF_CFA_REGISTER: |
| 1651 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 1652 | case DK_CFI_OFFSET: |
| 1653 | return parseDirectiveCFIOffset(IDLoc); |
| 1654 | case DK_CFI_REL_OFFSET: |
| 1655 | return parseDirectiveCFIRelOffset(IDLoc); |
| 1656 | case DK_CFI_PERSONALITY: |
| 1657 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 1658 | case DK_CFI_LSDA: |
| 1659 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 1660 | case DK_CFI_REMEMBER_STATE: |
| 1661 | return parseDirectiveCFIRememberState(); |
| 1662 | case DK_CFI_RESTORE_STATE: |
| 1663 | return parseDirectiveCFIRestoreState(); |
| 1664 | case DK_CFI_SAME_VALUE: |
| 1665 | return parseDirectiveCFISameValue(IDLoc); |
| 1666 | case DK_CFI_RESTORE: |
| 1667 | return parseDirectiveCFIRestore(IDLoc); |
| 1668 | case DK_CFI_ESCAPE: |
| 1669 | return parseDirectiveCFIEscape(); |
| 1670 | case DK_CFI_SIGNAL_FRAME: |
| 1671 | return parseDirectiveCFISignalFrame(); |
| 1672 | case DK_CFI_UNDEFINED: |
| 1673 | return parseDirectiveCFIUndefined(IDLoc); |
| 1674 | case DK_CFI_REGISTER: |
| 1675 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 1676 | case DK_CFI_WINDOW_SAVE: |
| 1677 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1678 | case DK_MACROS_ON: |
| 1679 | case DK_MACROS_OFF: |
| 1680 | return parseDirectiveMacrosOnOff(IDVal); |
| 1681 | case DK_MACRO: |
| 1682 | return parseDirectiveMacro(IDLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1683 | case DK_EXITM: |
| 1684 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1685 | case DK_ENDM: |
| 1686 | case DK_ENDMACRO: |
| 1687 | return parseDirectiveEndMacro(IDVal); |
| 1688 | case DK_PURGEM: |
| 1689 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 1690 | case DK_END: |
| 1691 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 1692 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 1693 | return parseDirectiveError(IDLoc, false); |
| 1694 | case DK_ERROR: |
| 1695 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 1696 | case DK_WARNING: |
| 1697 | return parseDirectiveWarning(IDLoc); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1698 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1699 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1700 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1701 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1702 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1703 | // __asm _emit or __asm __emit |
| 1704 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 1705 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1706 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1707 | |
| 1708 | // __asm align |
| 1709 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1710 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1711 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1712 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1714 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1715 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1716 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1717 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1718 | Info.ParsedOperands); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1719 | Info.ParseError = HadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1720 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1721 | // Dump the parsed representation, if requested. |
| 1722 | if (getShowParsedOperands()) { |
| 1723 | SmallString<256> Str; |
| 1724 | raw_svector_ostream OS(Str); |
| 1725 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1726 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1727 | if (i != 0) |
| 1728 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1729 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1730 | } |
| 1731 | OS << "]"; |
| 1732 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1733 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1736 | // If we are generating dwarf for the current section then generate a .loc |
| 1737 | // directive for the instruction. |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1738 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1739 | getContext().getGenDwarfSectionSyms().count( |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 1740 | getStreamer().getCurrentSection().first)) { |
| 1741 | unsigned Line; |
| 1742 | if (ActiveMacros.empty()) |
| 1743 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 1744 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 1745 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 1746 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1747 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1748 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1749 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1750 | // 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] | 1751 | if (CppHashFilename.size()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 1752 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
| 1753 | 0, StringRef(), CppHashFilename); |
| 1754 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1755 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1756 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 1757 | // cache with the different Loc from the call above we save the last |
| 1758 | // info we queried here with SrcMgr.FindLineNumber(). |
| 1759 | unsigned CppHashLocLineNo; |
| 1760 | if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf) |
| 1761 | CppHashLocLineNo = LastQueryLine; |
| 1762 | else { |
| 1763 | CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf); |
| 1764 | LastQueryLine = CppHashLocLineNo; |
| 1765 | LastQueryIDLoc = CppHashLoc; |
| 1766 | LastQueryBuffer = CppHashBuf; |
| 1767 | } |
| 1768 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1769 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1770 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1771 | getStreamer().EmitDwarfLocDirective( |
| 1772 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 1773 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 1774 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1777 | // If parsing succeeded, match the instruction. |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1778 | if (!HadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1779 | uint64_t ErrorInfo; |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1780 | getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1781 | Info.ParsedOperands, Out, |
Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1782 | ErrorInfo, ParsingInlineAsm); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1783 | } |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1785 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1786 | // that. |
| 1787 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1788 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1789 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1790 | /// 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] | 1791 | /// 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] | 1792 | void AsmParser::eatToEndOfLine() { |
Rafael Espindola | e0d0908 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1793 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1794 | Lexer.LexUntilEndOfLine(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1795 | // Eat EOL. |
| 1796 | Lex(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1799 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1800 | /// ::= # number "filename" |
| 1801 | /// or just as a full line comment if it doesn't have a number and a string. |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1802 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1803 | Lex(); // Eat the hash token. |
| 1804 | |
| 1805 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1806 | // Consume the line since in cases it is not a well-formed line directive, |
| 1807 | // as if were simply a full line comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1808 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1809 | return false; |
| 1810 | } |
| 1811 | |
| 1812 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1813 | Lex(); |
| 1814 | |
| 1815 | if (getLexer().isNot(AsmToken::String)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1816 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
| 1819 | |
| 1820 | StringRef Filename = getTok().getString(); |
| 1821 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1822 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1823 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1824 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1825 | CppHashLoc = L; |
| 1826 | CppHashFilename = Filename; |
| 1827 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1828 | CppHashBuf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1829 | |
| 1830 | // Ignore any trailing characters, they're just comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1831 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1832 | return false; |
| 1833 | } |
| 1834 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1835 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1836 | /// for the Filename and LineNo if any in the diagnostic. |
| 1837 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1838 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1839 | raw_ostream &OS = errs(); |
| 1840 | |
| 1841 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1842 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1843 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1844 | unsigned CppHashBuf = |
| 1845 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1846 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1847 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1848 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1849 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1850 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 1851 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1852 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1853 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1856 | // 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] | 1857 | // manager changed or buffer changed (like in a nested include) then just |
| 1858 | // print the normal diagnostic using its Filename and LineNo. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1859 | if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1860 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1861 | if (Parser->SavedDiagHandler) |
| 1862 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1863 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1864 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1865 | return; |
| 1866 | } |
| 1867 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1868 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1869 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1870 | // the diagnostic. |
Jakub Staszak | ec2ffa9 | 2013-09-16 22:03:38 +0000 | [diff] [blame] | 1871 | const std::string &Filename = Parser->CppHashFilename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1872 | |
| 1873 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1874 | int CppHashLocLineNo = |
| 1875 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1876 | int LineNo = |
| 1877 | Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1878 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1879 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 1880 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1881 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1882 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1883 | if (Parser->SavedDiagHandler) |
| 1884 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1885 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1886 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1889 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1890 | // difference being that that function accepts '@' as part of identifiers and |
| 1891 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1892 | // '@' as a special case when needed. |
| 1893 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1894 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 1895 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1898 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 1899 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1900 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1901 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1902 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1903 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1904 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1905 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1906 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1907 | // A macro without parameters is handled differently on Darwin: |
| 1908 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1909 | while (!Body.empty()) { |
| 1910 | // Scan for the next substitution. |
| 1911 | std::size_t End = Body.size(), Pos = 0; |
| 1912 | for (; Pos != End; ++Pos) { |
| 1913 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1914 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1915 | // This macro has no parameters, look for $0, $1, etc. |
| 1916 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1917 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1918 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1919 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1920 | if (Next == '$' || Next == 'n' || |
| 1921 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | } else { |
| 1924 | // This macro has parameters, look for \foo, \bar, etc. |
| 1925 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1926 | break; |
| 1927 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | // Add the prefix. |
| 1931 | OS << Body.slice(0, Pos); |
| 1932 | |
| 1933 | // Check if we reached the end. |
| 1934 | if (Pos == End) |
| 1935 | break; |
| 1936 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1937 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1938 | switch (Body[Pos + 1]) { |
| 1939 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1940 | case '$': |
| 1941 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1944 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1945 | case 'n': |
| 1946 | OS << A.size(); |
| 1947 | break; |
| 1948 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1949 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1950 | default: { |
| 1951 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1952 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1953 | if (Index >= A.size()) |
| 1954 | break; |
| 1955 | |
| 1956 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 1957 | for (const AsmToken &Token : A[Index]) |
| 1958 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
| 1961 | } |
| 1962 | Pos += 2; |
| 1963 | } else { |
| 1964 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Check for the \@ pseudo-variable. |
| 1967 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1968 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1969 | else |
| 1970 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 1971 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1972 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1973 | const char *Begin = Body.data() + Pos + 1; |
| 1974 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1975 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1976 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1977 | if (Argument == "@") { |
| 1978 | OS << NumOfMacroInstantiations; |
| 1979 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1980 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1981 | for (; Index < NParameters; ++Index) |
| 1982 | if (Parameters[Index].Name == Argument) |
| 1983 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1984 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1985 | if (Index == NParameters) { |
| 1986 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 1987 | Pos += 3; |
| 1988 | else { |
| 1989 | OS << '\\' << Argument; |
| 1990 | Pos = I; |
| 1991 | } |
| 1992 | } else { |
| 1993 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 1994 | for (const AsmToken &Token : A[Index]) |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1995 | // We expect no quotes around the string's contents when |
| 1996 | // parsing for varargs. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 1997 | if (Token.getKind() != AsmToken::String || VarargParameter) |
| 1998 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1999 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2000 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2001 | |
| 2002 | Pos += 1 + Argument.size(); |
| 2003 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2004 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2005 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2006 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2007 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2008 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2009 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2010 | return false; |
| 2011 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2012 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2013 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2014 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2015 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2016 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2017 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2018 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2019 | switch (kind) { |
| 2020 | default: |
| 2021 | return false; |
| 2022 | case AsmToken::Plus: |
| 2023 | case AsmToken::Minus: |
| 2024 | case AsmToken::Tilde: |
| 2025 | case AsmToken::Slash: |
| 2026 | case AsmToken::Star: |
| 2027 | case AsmToken::Dot: |
| 2028 | case AsmToken::Equal: |
| 2029 | case AsmToken::EqualEqual: |
| 2030 | case AsmToken::Pipe: |
| 2031 | case AsmToken::PipePipe: |
| 2032 | case AsmToken::Caret: |
| 2033 | case AsmToken::Amp: |
| 2034 | case AsmToken::AmpAmp: |
| 2035 | case AsmToken::Exclaim: |
| 2036 | case AsmToken::ExclaimEqual: |
| 2037 | case AsmToken::Percent: |
| 2038 | case AsmToken::Less: |
| 2039 | case AsmToken::LessEqual: |
| 2040 | case AsmToken::LessLess: |
| 2041 | case AsmToken::LessGreater: |
| 2042 | case AsmToken::Greater: |
| 2043 | case AsmToken::GreaterEqual: |
| 2044 | case AsmToken::GreaterGreater: |
| 2045 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2049 | namespace { |
| 2050 | class AsmLexerSkipSpaceRAII { |
| 2051 | public: |
| 2052 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2053 | Lexer.setSkipSpace(SkipSpace); |
| 2054 | } |
| 2055 | |
| 2056 | ~AsmLexerSkipSpaceRAII() { |
| 2057 | Lexer.setSkipSpace(true); |
| 2058 | } |
| 2059 | |
| 2060 | private: |
| 2061 | AsmLexer &Lexer; |
| 2062 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 2063 | } |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2064 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2065 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2066 | |
| 2067 | if (Vararg) { |
| 2068 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2069 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2070 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2071 | } |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2075 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2076 | unsigned AddTokens = 0; |
| 2077 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2078 | // Darwin doesn't use spaces to delmit arguments. |
| 2079 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2080 | |
| 2081 | for (;;) { |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2082 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2083 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2084 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2085 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2086 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2087 | |
| 2088 | if (Lexer.is(AsmToken::Space)) { |
| 2089 | Lex(); // Eat spaces |
| 2090 | |
| 2091 | // Spaces can delimit parameters, but could also be part an expression. |
| 2092 | // If the token after a space is an operator, add the token and the next |
| 2093 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2094 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2095 | if (isOperator(Lexer.getKind())) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2096 | // Check to see whether the token is used as an operator, |
| 2097 | // or part of an identifier |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 2098 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2099 | if (*NextChar == ' ') |
| 2100 | AddTokens = 2; |
| 2101 | } |
| 2102 | |
| 2103 | if (!AddTokens && ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2104 | break; |
| 2105 | } |
| 2106 | } |
| 2107 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2108 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2109 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2110 | // to be able to fill in the remaining default parameter values |
| 2111 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2112 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2113 | |
| 2114 | // Adjust the current parentheses level. |
| 2115 | if (Lexer.is(AsmToken::LParen)) |
| 2116 | ++ParenLevel; |
| 2117 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2118 | --ParenLevel; |
| 2119 | |
| 2120 | // Append the token to the current argument list. |
| 2121 | MA.push_back(getTok()); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2122 | if (AddTokens) |
| 2123 | AddTokens--; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2124 | Lex(); |
| 2125 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2126 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2127 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2128 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2129 | return false; |
| 2130 | } |
| 2131 | |
| 2132 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2133 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2134 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2135 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2136 | bool NamedParametersFound = false; |
| 2137 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2138 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2139 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2140 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2141 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2142 | // Parse two kinds of macro invocations: |
| 2143 | // - macros defined without any parameters accept an arbitrary number of them |
| 2144 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2145 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2146 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2147 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2148 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2149 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2150 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2151 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2152 | if (parseIdentifier(FA.Name)) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2153 | Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2154 | eatToEndOfStatement(); |
| 2155 | return true; |
| 2156 | } |
| 2157 | |
| 2158 | if (!Lexer.is(AsmToken::Equal)) { |
| 2159 | TokError("expected '=' after formal parameter identifier"); |
| 2160 | eatToEndOfStatement(); |
| 2161 | return true; |
| 2162 | } |
| 2163 | Lex(); |
| 2164 | |
| 2165 | NamedParametersFound = true; |
| 2166 | } |
| 2167 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2168 | if (NamedParametersFound && FA.Name.empty()) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2169 | Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2170 | eatToEndOfStatement(); |
| 2171 | return true; |
| 2172 | } |
| 2173 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2174 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
| 2175 | if (parseMacroArgument(FA.Value, Vararg)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2176 | return true; |
| 2177 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2178 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2179 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2180 | unsigned FAI = 0; |
| 2181 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2182 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2183 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2184 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2185 | if (FAI >= NParameters) { |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2186 | assert(M && "expected macro to be defined"); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2187 | Error(IDLoc, |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2188 | "parameter named '" + FA.Name + "' does not exist for macro '" + |
Saleem Abdulrasool | 3f44cd7 | 2014-03-17 17:13:57 +0000 | [diff] [blame] | 2189 | M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2190 | return true; |
| 2191 | } |
| 2192 | PI = FAI; |
| 2193 | } |
| 2194 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2195 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2196 | if (A.size() <= PI) |
| 2197 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2198 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2199 | |
| 2200 | if (FALocs.size() <= PI) |
| 2201 | FALocs.resize(PI + 1); |
| 2202 | |
| 2203 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2204 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2205 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2206 | // At the end of the statement, fill in remaining arguments that have |
| 2207 | // default values. If there aren't any, then the next argument is |
| 2208 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2209 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2210 | bool Failure = false; |
| 2211 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2212 | if (A[FAI].empty()) { |
| 2213 | if (M->Parameters[FAI].Required) { |
| 2214 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2215 | "missing value for required parameter " |
| 2216 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2217 | Failure = true; |
| 2218 | } |
| 2219 | |
| 2220 | if (!M->Parameters[FAI].Value.empty()) |
| 2221 | A[FAI] = M->Parameters[FAI].Value; |
| 2222 | } |
| 2223 | } |
| 2224 | return Failure; |
| 2225 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2226 | |
| 2227 | if (Lexer.is(AsmToken::Comma)) |
| 2228 | Lex(); |
| 2229 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2230 | |
| 2231 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2234 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2235 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 2236 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2239 | void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 2240 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2243 | void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2244 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2245 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2246 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 2247 | // this, although we should protect against infinite loops. |
| 2248 | if (ActiveMacros.size() == 20) |
| 2249 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 2250 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2251 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2252 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2253 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2254 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2255 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2256 | // to hold the macro body with substitutions. |
| 2257 | SmallString<256> Buf; |
| 2258 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2259 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2260 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2261 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2262 | return true; |
| 2263 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2264 | // 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] | 2265 | // instantiation. |
| 2266 | OS << ".endmacro\n"; |
| 2267 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2268 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2269 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2270 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2271 | // Create the macro instantiation object and add to the current macro |
| 2272 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2273 | MacroInstantiation *MI = new MacroInstantiation( |
| 2274 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2275 | ActiveMacros.push_back(MI); |
| 2276 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2277 | ++NumOfMacroInstantiations; |
| 2278 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2279 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2280 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2281 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2282 | Lex(); |
| 2283 | |
| 2284 | return false; |
| 2285 | } |
| 2286 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2287 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2288 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2289 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2290 | Lex(); |
| 2291 | |
| 2292 | // Pop the instantiation entry. |
| 2293 | delete ActiveMacros.back(); |
| 2294 | ActiveMacros.pop_back(); |
| 2295 | } |
| 2296 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2297 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2298 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2299 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2300 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2301 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| 2302 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2303 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2304 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2305 | if (!Sym) { |
| 2306 | // In the case where we parse an expression starting with a '.', we will |
| 2307 | // not generate an error, nor will we create a symbol. In this case we |
| 2308 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2309 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2310 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2311 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2312 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2313 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2314 | if (NoDeadStrip) |
| 2315 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2316 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2317 | return false; |
| 2318 | } |
| 2319 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2320 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2321 | /// ::= identifier |
| 2322 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2323 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2324 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2325 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2326 | // 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] | 2327 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2328 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2329 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2330 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2331 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2332 | // Consume the prefix character, and check for a following identifier. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2333 | Lex(); |
| 2334 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2335 | return true; |
| 2336 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2337 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| 2338 | if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2339 | return true; |
| 2340 | |
| 2341 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2342 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2343 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2344 | Lex(); |
| 2345 | return false; |
| 2346 | } |
| 2347 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2348 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2349 | return true; |
| 2350 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2351 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2352 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2353 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2354 | |
| 2355 | return false; |
| 2356 | } |
| 2357 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2358 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2359 | /// ::= .equ identifier ',' expression |
| 2360 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2361 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2362 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2363 | StringRef Name; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2364 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2365 | if (parseIdentifier(Name)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2366 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2367 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2368 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2369 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2370 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2371 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2372 | return parseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2375 | bool AsmParser::parseEscapedString(std::string &Data) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2376 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2377 | |
| 2378 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2379 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2380 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2381 | if (Str[i] != '\\') { |
| 2382 | Data += Str[i]; |
| 2383 | continue; |
| 2384 | } |
| 2385 | |
| 2386 | // Recognize escaped characters. Note that this escape semantics currently |
| 2387 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2388 | ++i; |
| 2389 | if (i == e) |
| 2390 | return TokError("unexpected backslash at end of string"); |
| 2391 | |
| 2392 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2393 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2394 | // Consume up to three octal characters. |
| 2395 | unsigned Value = Str[i] - '0'; |
| 2396 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2397 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2398 | ++i; |
| 2399 | Value = Value * 8 + (Str[i] - '0'); |
| 2400 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2401 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2402 | ++i; |
| 2403 | Value = Value * 8 + (Str[i] - '0'); |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | if (Value > 255) |
| 2408 | return TokError("invalid octal escape sequence (out of range)"); |
| 2409 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2410 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | // Otherwise recognize individual escapes. |
| 2415 | switch (Str[i]) { |
| 2416 | default: |
| 2417 | // Just reject invalid escape sequences for now. |
| 2418 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2419 | |
| 2420 | case 'b': Data += '\b'; break; |
| 2421 | case 'f': Data += '\f'; break; |
| 2422 | case 'n': Data += '\n'; break; |
| 2423 | case 'r': Data += '\r'; break; |
| 2424 | case 't': Data += '\t'; break; |
| 2425 | case '"': Data += '"'; break; |
| 2426 | case '\\': Data += '\\'; break; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | return false; |
| 2431 | } |
| 2432 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2433 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2434 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2435 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2436 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2437 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2438 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2439 | for (;;) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2440 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2441 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2442 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2443 | std::string Data; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2444 | if (parseEscapedString(Data)) |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2445 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2446 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2447 | getStreamer().EmitBytes(Data); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2448 | if (ZeroTerminated) |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2449 | getStreamer().EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2450 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2451 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2452 | |
| 2453 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2456 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2457 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2458 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2462 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2463 | return false; |
| 2464 | } |
| 2465 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2466 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2467 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2468 | bool AsmParser::parseDirectiveValue(unsigned Size) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2469 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2470 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2471 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2472 | for (;;) { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2473 | const MCExpr *Value; |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2474 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2475 | if (parseExpression(Value)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2476 | return true; |
| 2477 | |
Daniel Dunbar | 6738a2e | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2478 | // Special case constant expressions to match code generator. |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2479 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2480 | assert(Size <= 8 && "Invalid size"); |
| 2481 | uint64_t IntValue = MCE->getValue(); |
| 2482 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2483 | return Error(ExprLoc, "literal value out of range for directive"); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2484 | getStreamer().EmitIntValue(IntValue, Size); |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2485 | } else |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 2486 | getStreamer().EmitValue(Value, Size, ExprLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2487 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2488 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2489 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2490 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2491 | // FIXME: Improve diagnostic. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2492 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2493 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2494 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2498 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2499 | return false; |
| 2500 | } |
| 2501 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2502 | /// ParseDirectiveOctaValue |
| 2503 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
| 2504 | bool AsmParser::parseDirectiveOctaValue() { |
| 2505 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2506 | checkForValidSection(); |
| 2507 | |
| 2508 | for (;;) { |
| 2509 | if (Lexer.getKind() == AsmToken::Error) |
| 2510 | return true; |
| 2511 | if (Lexer.getKind() != AsmToken::Integer && |
| 2512 | Lexer.getKind() != AsmToken::BigNum) |
| 2513 | return TokError("unknown token in expression"); |
| 2514 | |
| 2515 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2516 | APInt IntValue = getTok().getAPIntVal(); |
| 2517 | Lex(); |
| 2518 | |
| 2519 | uint64_t hi, lo; |
| 2520 | if (IntValue.isIntN(64)) { |
| 2521 | hi = 0; |
| 2522 | lo = IntValue.getZExtValue(); |
| 2523 | } else if (IntValue.isIntN(128)) { |
David Woodhouse | 6c9a6f9 | 2014-02-01 16:52:33 +0000 | [diff] [blame] | 2524 | // It might actually have more than 128 bits, but the top ones are zero. |
| 2525 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2526 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2527 | } else |
| 2528 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2529 | |
| 2530 | if (MAI.isLittleEndian()) { |
| 2531 | getStreamer().EmitIntValue(lo, 8); |
| 2532 | getStreamer().EmitIntValue(hi, 8); |
| 2533 | } else { |
| 2534 | getStreamer().EmitIntValue(hi, 8); |
| 2535 | getStreamer().EmitIntValue(lo, 8); |
| 2536 | } |
| 2537 | |
| 2538 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2539 | break; |
| 2540 | |
| 2541 | // FIXME: Improve diagnostic. |
| 2542 | if (getLexer().isNot(AsmToken::Comma)) |
| 2543 | return TokError("unexpected token in directive"); |
| 2544 | Lex(); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | Lex(); |
| 2549 | return false; |
| 2550 | } |
| 2551 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2552 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2553 | /// ::= (.single | .double) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2554 | bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) { |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2555 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2556 | checkForValidSection(); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2557 | |
| 2558 | for (;;) { |
| 2559 | // We don't truly support arithmetic on floating point expressions, so we |
| 2560 | // have to manually parse unary prefixes. |
| 2561 | bool IsNeg = false; |
| 2562 | if (getLexer().is(AsmToken::Minus)) { |
| 2563 | Lex(); |
| 2564 | IsNeg = true; |
| 2565 | } else if (getLexer().is(AsmToken::Plus)) |
| 2566 | Lex(); |
| 2567 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2568 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2569 | getLexer().isNot(AsmToken::Real) && |
| 2570 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2571 | return TokError("unexpected token in directive"); |
| 2572 | |
| 2573 | // Convert to an APFloat. |
| 2574 | APFloat Value(Semantics); |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2575 | StringRef IDVal = getTok().getString(); |
| 2576 | if (getLexer().is(AsmToken::Identifier)) { |
| 2577 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2578 | Value = APFloat::getInf(Semantics); |
| 2579 | else if (!IDVal.compare_lower("nan")) |
| 2580 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2581 | else |
| 2582 | return TokError("invalid floating point literal"); |
| 2583 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2584 | APFloat::opInvalidOp) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2585 | return TokError("invalid floating point literal"); |
| 2586 | if (IsNeg) |
| 2587 | Value.changeSign(); |
| 2588 | |
| 2589 | // Consume the numeric token. |
| 2590 | Lex(); |
| 2591 | |
| 2592 | // Emit the value as an integer. |
| 2593 | APInt AsInt = Value.bitcastToAPInt(); |
| 2594 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2595 | AsInt.getBitWidth() / 8); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2596 | |
| 2597 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2598 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2599 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2600 | if (getLexer().isNot(AsmToken::Comma)) |
| 2601 | return TokError("unexpected token in directive"); |
| 2602 | Lex(); |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | Lex(); |
| 2607 | return false; |
| 2608 | } |
| 2609 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2610 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2611 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2612 | bool AsmParser::parseDirectiveZero() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2613 | checkForValidSection(); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2614 | |
| 2615 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2616 | if (parseAbsoluteExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2617 | return true; |
| 2618 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2619 | int64_t Val = 0; |
| 2620 | if (getLexer().is(AsmToken::Comma)) { |
| 2621 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2622 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2623 | return true; |
| 2624 | } |
| 2625 | |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2626 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2627 | return TokError("unexpected token in '.zero' directive"); |
| 2628 | |
| 2629 | Lex(); |
| 2630 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2631 | getStreamer().EmitFill(NumBytes, Val); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2632 | |
| 2633 | return false; |
| 2634 | } |
| 2635 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2636 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2637 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2638 | bool AsmParser::parseDirectiveFill() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2639 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2640 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2641 | SMLoc RepeatLoc = getLexer().getLoc(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2642 | int64_t NumValues; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2643 | if (parseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2644 | return true; |
| 2645 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2646 | if (NumValues < 0) { |
| 2647 | Warning(RepeatLoc, |
| 2648 | "'.fill' directive with negative repeat count has no effect"); |
| 2649 | NumValues = 0; |
| 2650 | } |
| 2651 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2652 | int64_t FillSize = 1; |
| 2653 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2654 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2655 | SMLoc SizeLoc, ExprLoc; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2656 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2657 | if (getLexer().isNot(AsmToken::Comma)) |
| 2658 | return TokError("unexpected token in '.fill' directive"); |
| 2659 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2660 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2661 | SizeLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2662 | if (parseAbsoluteExpression(FillSize)) |
| 2663 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2664 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2665 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2666 | if (getLexer().isNot(AsmToken::Comma)) |
| 2667 | return TokError("unexpected token in '.fill' directive"); |
| 2668 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2669 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2670 | ExprLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2671 | if (parseAbsoluteExpression(FillExpr)) |
| 2672 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2673 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2674 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2675 | return TokError("unexpected token in '.fill' directive"); |
| 2676 | |
| 2677 | Lex(); |
| 2678 | } |
| 2679 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2680 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2681 | if (FillSize < 0) { |
| 2682 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
| 2683 | NumValues = 0; |
| 2684 | } |
| 2685 | if (FillSize > 8) { |
| 2686 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 2687 | FillSize = 8; |
| 2688 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2689 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2690 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 2691 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 2692 | |
Alexey Samsonov | 1b0713c | 2014-09-02 17:25:29 +0000 | [diff] [blame] | 2693 | if (NumValues > 0) { |
| 2694 | int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize; |
| 2695 | FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8); |
| 2696 | for (uint64_t i = 0, e = NumValues; i != e; ++i) { |
| 2697 | getStreamer().EmitIntValue(FillExpr, NonZeroFillSize); |
| 2698 | if (NonZeroFillSize < FillSize) |
| 2699 | getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize); |
| 2700 | } |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2701 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2702 | |
| 2703 | return false; |
| 2704 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2705 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2706 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2707 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2708 | bool AsmParser::parseDirectiveOrg() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2709 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2710 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2711 | const MCExpr *Offset; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2712 | if (parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2713 | return true; |
| 2714 | |
| 2715 | // Parse optional fill expression. |
| 2716 | int64_t FillExpr = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2717 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2718 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2719 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2720 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2721 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2722 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2723 | return true; |
| 2724 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2725 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2726 | return TokError("unexpected token in '.org' directive"); |
| 2727 | } |
| 2728 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2729 | Lex(); |
Rafael Espindola | 7ae65d8 | 2015-11-04 23:59:18 +0000 | [diff] [blame] | 2730 | getStreamer().emitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2731 | return false; |
| 2732 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2733 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2734 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2735 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2736 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2737 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2738 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2739 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2740 | int64_t Alignment; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2741 | if (parseAbsoluteExpression(Alignment)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2742 | return true; |
| 2743 | |
| 2744 | SMLoc MaxBytesLoc; |
| 2745 | bool HasFillExpr = false; |
| 2746 | int64_t FillExpr = 0; |
| 2747 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2748 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2749 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2750 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2751 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2752 | |
| 2753 | // The fill expression can be omitted while specifying a maximum number of |
| 2754 | // alignment bytes, e.g: |
| 2755 | // .align 3,,4 |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2756 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2757 | HasFillExpr = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2758 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2759 | return true; |
| 2760 | } |
| 2761 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2762 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2763 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2764 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2765 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2766 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2767 | MaxBytesLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2768 | if (parseAbsoluteExpression(MaxBytesToFill)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2769 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2770 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2771 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2772 | return TokError("unexpected token in directive"); |
| 2773 | } |
| 2774 | } |
| 2775 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2776 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2777 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2778 | if (!HasFillExpr) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2779 | FillExpr = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2780 | |
| 2781 | // Compute alignment in bytes. |
| 2782 | if (IsPow2) { |
| 2783 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2784 | if (Alignment >= 32) { |
| 2785 | Error(AlignmentLoc, "invalid alignment value"); |
| 2786 | Alignment = 31; |
| 2787 | } |
| 2788 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2789 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2790 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 2791 | // Reject alignments that aren't either a power of two or zero, |
| 2792 | // for gas compatibility. Alignment of zero is silently rounded |
| 2793 | // up to one. |
| 2794 | if (Alignment == 0) |
| 2795 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2796 | if (!isPowerOf2_64(Alignment)) |
| 2797 | Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2800 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2801 | if (MaxBytesLoc.isValid()) { |
| 2802 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2803 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2804 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2805 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2809 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2810 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2811 | MaxBytesToFill = 0; |
| 2812 | } |
| 2813 | } |
| 2814 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2815 | // Check whether we should use optimal code alignment for this .align |
| 2816 | // directive. |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 2817 | const MCSection *Section = getStreamer().getCurrentSection().first; |
| 2818 | assert(Section && "must have section to emit alignment"); |
| 2819 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2820 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2821 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2822 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2823 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2824 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2825 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2826 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2827 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2828 | |
| 2829 | return false; |
| 2830 | } |
| 2831 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2832 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2833 | /// ::= .file [number] filename |
| 2834 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2835 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2836 | // FIXME: I'm not sure what this is. |
| 2837 | int64_t FileNumber = -1; |
| 2838 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2839 | if (getLexer().is(AsmToken::Integer)) { |
| 2840 | FileNumber = getTok().getIntVal(); |
| 2841 | Lex(); |
| 2842 | |
| 2843 | if (FileNumber < 1) |
| 2844 | return TokError("file number less than one"); |
| 2845 | } |
| 2846 | |
| 2847 | if (getLexer().isNot(AsmToken::String)) |
| 2848 | return TokError("unexpected token in '.file' directive"); |
| 2849 | |
| 2850 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2851 | // Allow the strings to have escaped octal character sequence. |
| 2852 | std::string Path = getTok().getString(); |
| 2853 | if (parseEscapedString(Path)) |
| 2854 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2855 | Lex(); |
| 2856 | |
| 2857 | StringRef Directory; |
| 2858 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2859 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2860 | if (getLexer().is(AsmToken::String)) { |
| 2861 | if (FileNumber == -1) |
| 2862 | return TokError("explicit path specified, but no file number"); |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2863 | if (parseEscapedString(FilenameData)) |
| 2864 | return true; |
| 2865 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2866 | Directory = Path; |
| 2867 | Lex(); |
| 2868 | } else { |
| 2869 | Filename = Path; |
| 2870 | } |
| 2871 | |
| 2872 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2873 | return TokError("unexpected token in '.file' directive"); |
| 2874 | |
| 2875 | if (FileNumber == -1) |
| 2876 | getStreamer().EmitFileDirective(Filename); |
| 2877 | else { |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 2878 | if (getContext().getGenDwarfForAssembly()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2879 | Error(DirectiveLoc, |
| 2880 | "input can't have .file dwarf directives when -g is " |
| 2881 | "used to generate dwarf debug info for assembly code"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2882 | |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2883 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) == |
| 2884 | 0) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2885 | Error(FileNumberLoc, "file number already allocated"); |
| 2886 | } |
| 2887 | |
| 2888 | return false; |
| 2889 | } |
| 2890 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2891 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2892 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2893 | bool AsmParser::parseDirectiveLine() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2894 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2895 | if (getLexer().isNot(AsmToken::Integer)) |
| 2896 | return TokError("unexpected token in '.line' directive"); |
| 2897 | |
| 2898 | int64_t LineNumber = getTok().getIntVal(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2899 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2900 | Lex(); |
| 2901 | |
| 2902 | // FIXME: Do something with the .line. |
| 2903 | } |
| 2904 | |
| 2905 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2906 | return TokError("unexpected token in '.line' directive"); |
| 2907 | |
| 2908 | return false; |
| 2909 | } |
| 2910 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2911 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2912 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2913 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2914 | /// The first number is a file number, must have been previously assigned with |
| 2915 | /// a .file directive, the second number is the line number and optionally the |
| 2916 | /// third number is a column position (zero if not specified). The remaining |
| 2917 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2918 | bool AsmParser::parseDirectiveLoc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2919 | if (getLexer().isNot(AsmToken::Integer)) |
| 2920 | return TokError("unexpected token in '.loc' directive"); |
| 2921 | int64_t FileNumber = getTok().getIntVal(); |
| 2922 | if (FileNumber < 1) |
| 2923 | return TokError("file number less than one in '.loc' directive"); |
| 2924 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2925 | return TokError("unassigned file number in '.loc' directive"); |
| 2926 | Lex(); |
| 2927 | |
| 2928 | int64_t LineNumber = 0; |
| 2929 | if (getLexer().is(AsmToken::Integer)) { |
| 2930 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 2931 | if (LineNumber < 0) |
| 2932 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2933 | Lex(); |
| 2934 | } |
| 2935 | |
| 2936 | int64_t ColumnPos = 0; |
| 2937 | if (getLexer().is(AsmToken::Integer)) { |
| 2938 | ColumnPos = getTok().getIntVal(); |
| 2939 | if (ColumnPos < 0) |
| 2940 | return TokError("column position less than zero in '.loc' directive"); |
| 2941 | Lex(); |
| 2942 | } |
| 2943 | |
| 2944 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2945 | unsigned Isa = 0; |
| 2946 | int64_t Discriminator = 0; |
| 2947 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2948 | for (;;) { |
| 2949 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2950 | break; |
| 2951 | |
| 2952 | StringRef Name; |
| 2953 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2954 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2955 | return TokError("unexpected token in '.loc' directive"); |
| 2956 | |
| 2957 | if (Name == "basic_block") |
| 2958 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2959 | else if (Name == "prologue_end") |
| 2960 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2961 | else if (Name == "epilogue_begin") |
| 2962 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2963 | else if (Name == "is_stmt") { |
| 2964 | Loc = getTok().getLoc(); |
| 2965 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2966 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2967 | return true; |
| 2968 | // The expression must be the constant 0 or 1. |
| 2969 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2970 | int Value = MCE->getValue(); |
| 2971 | if (Value == 0) |
| 2972 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2973 | else if (Value == 1) |
| 2974 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2975 | else |
| 2976 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2977 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2978 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2979 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2980 | } else if (Name == "isa") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2981 | Loc = getTok().getLoc(); |
| 2982 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2983 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2984 | return true; |
| 2985 | // The expression must be a constant greater or equal to 0. |
| 2986 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2987 | int Value = MCE->getValue(); |
| 2988 | if (Value < 0) |
| 2989 | return Error(Loc, "isa number less than zero"); |
| 2990 | Isa = Value; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2991 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2992 | return Error(Loc, "isa number not a constant value"); |
| 2993 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2994 | } else if (Name == "discriminator") { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2995 | if (parseAbsoluteExpression(Discriminator)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2996 | return true; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2997 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2998 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2999 | } |
| 3000 | |
| 3001 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3002 | break; |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3007 | Isa, Discriminator, StringRef()); |
| 3008 | |
| 3009 | return false; |
| 3010 | } |
| 3011 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3012 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3013 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3014 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3015 | return TokError("unsupported directive '.stabs'"); |
| 3016 | } |
| 3017 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3018 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3019 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3020 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3021 | StringRef Name; |
| 3022 | bool EH = false; |
| 3023 | bool Debug = false; |
| 3024 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3025 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3026 | return TokError("Expected an identifier"); |
| 3027 | |
| 3028 | if (Name == ".eh_frame") |
| 3029 | EH = true; |
| 3030 | else if (Name == ".debug_frame") |
| 3031 | Debug = true; |
| 3032 | |
| 3033 | if (getLexer().is(AsmToken::Comma)) { |
| 3034 | Lex(); |
| 3035 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3036 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3037 | return TokError("Expected an identifier"); |
| 3038 | |
| 3039 | if (Name == ".eh_frame") |
| 3040 | EH = true; |
| 3041 | else if (Name == ".debug_frame") |
| 3042 | Debug = true; |
| 3043 | } |
| 3044 | |
| 3045 | getStreamer().EmitCFISections(EH, Debug); |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3049 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3050 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3051 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3052 | StringRef Simple; |
| 3053 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3054 | if (parseIdentifier(Simple) || Simple != "simple") |
| 3055 | return TokError("unexpected token in .cfi_startproc directive"); |
| 3056 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 3057 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3058 | return false; |
| 3059 | } |
| 3060 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3061 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3062 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3063 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3064 | getStreamer().EmitCFIEndProc(); |
| 3065 | return false; |
| 3066 | } |
| 3067 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3068 | /// \brief parse register name or number. |
| 3069 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3070 | SMLoc DirectiveLoc) { |
| 3071 | unsigned RegNo; |
| 3072 | |
| 3073 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3074 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3075 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3076 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3077 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3078 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3079 | |
| 3080 | return false; |
| 3081 | } |
| 3082 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3083 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3084 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3085 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3086 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3087 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3088 | return true; |
| 3089 | |
| 3090 | if (getLexer().isNot(AsmToken::Comma)) |
| 3091 | return TokError("unexpected token in directive"); |
| 3092 | Lex(); |
| 3093 | |
| 3094 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3095 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3096 | return true; |
| 3097 | |
| 3098 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3102 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3103 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3104 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3105 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3106 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3107 | return true; |
| 3108 | |
| 3109 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3110 | return false; |
| 3111 | } |
| 3112 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3113 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3114 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3115 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3116 | int64_t Register1 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3117 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3118 | return true; |
| 3119 | |
| 3120 | if (getLexer().isNot(AsmToken::Comma)) |
| 3121 | return TokError("unexpected token in directive"); |
| 3122 | Lex(); |
| 3123 | |
| 3124 | int64_t Register2 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3125 | if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3126 | return true; |
| 3127 | |
| 3128 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3129 | return false; |
| 3130 | } |
| 3131 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3132 | /// parseDirectiveCFIWindowSave |
| 3133 | /// ::= .cfi_window_save |
| 3134 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3135 | getStreamer().EmitCFIWindowSave(); |
| 3136 | return false; |
| 3137 | } |
| 3138 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3139 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3140 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3141 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3142 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3143 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3144 | return true; |
| 3145 | |
| 3146 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3147 | return false; |
| 3148 | } |
| 3149 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3150 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3151 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3152 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3153 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3154 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3155 | return true; |
| 3156 | |
| 3157 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3158 | return false; |
| 3159 | } |
| 3160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3161 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3162 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3163 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3164 | int64_t Register = 0; |
| 3165 | int64_t Offset = 0; |
| 3166 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3167 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3168 | return true; |
| 3169 | |
| 3170 | if (getLexer().isNot(AsmToken::Comma)) |
| 3171 | return TokError("unexpected token in directive"); |
| 3172 | Lex(); |
| 3173 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3174 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3175 | return true; |
| 3176 | |
| 3177 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3178 | return false; |
| 3179 | } |
| 3180 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3181 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3182 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3183 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3184 | int64_t Register = 0; |
| 3185 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3186 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3187 | return true; |
| 3188 | |
| 3189 | if (getLexer().isNot(AsmToken::Comma)) |
| 3190 | return TokError("unexpected token in directive"); |
| 3191 | Lex(); |
| 3192 | |
| 3193 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3194 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3195 | return true; |
| 3196 | |
| 3197 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3198 | return false; |
| 3199 | } |
| 3200 | |
| 3201 | static bool isValidEncoding(int64_t Encoding) { |
| 3202 | if (Encoding & ~0xff) |
| 3203 | return false; |
| 3204 | |
| 3205 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3206 | return true; |
| 3207 | |
| 3208 | const unsigned Format = Encoding & 0xf; |
| 3209 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3210 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3211 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3212 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3213 | return false; |
| 3214 | |
| 3215 | const unsigned Application = Encoding & 0x70; |
| 3216 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3217 | Application != dwarf::DW_EH_PE_pcrel) |
| 3218 | return false; |
| 3219 | |
| 3220 | return true; |
| 3221 | } |
| 3222 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3223 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3224 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3225 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3226 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3227 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3228 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3229 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3230 | return true; |
| 3231 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3232 | return false; |
| 3233 | |
| 3234 | if (!isValidEncoding(Encoding)) |
| 3235 | return TokError("unsupported encoding."); |
| 3236 | |
| 3237 | if (getLexer().isNot(AsmToken::Comma)) |
| 3238 | return TokError("unexpected token in directive"); |
| 3239 | Lex(); |
| 3240 | |
| 3241 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3242 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3243 | return TokError("expected identifier in directive"); |
| 3244 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3245 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3246 | |
| 3247 | if (IsPersonality) |
| 3248 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3249 | else |
| 3250 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3251 | return false; |
| 3252 | } |
| 3253 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3254 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3255 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3256 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3257 | getStreamer().EmitCFIRememberState(); |
| 3258 | return false; |
| 3259 | } |
| 3260 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3261 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3262 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3263 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3264 | getStreamer().EmitCFIRestoreState(); |
| 3265 | return false; |
| 3266 | } |
| 3267 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3268 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3269 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3270 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3271 | int64_t Register = 0; |
| 3272 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3273 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3274 | return true; |
| 3275 | |
| 3276 | getStreamer().EmitCFISameValue(Register); |
| 3277 | return false; |
| 3278 | } |
| 3279 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3280 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3281 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3282 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3283 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3284 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3285 | return true; |
| 3286 | |
| 3287 | getStreamer().EmitCFIRestore(Register); |
| 3288 | return false; |
| 3289 | } |
| 3290 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3291 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3292 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3293 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3294 | std::string Values; |
| 3295 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3296 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3297 | return true; |
| 3298 | |
| 3299 | Values.push_back((uint8_t)CurrValue); |
| 3300 | |
| 3301 | while (getLexer().is(AsmToken::Comma)) { |
| 3302 | Lex(); |
| 3303 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3304 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3305 | return true; |
| 3306 | |
| 3307 | Values.push_back((uint8_t)CurrValue); |
| 3308 | } |
| 3309 | |
| 3310 | getStreamer().EmitCFIEscape(Values); |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3314 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3315 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3316 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3317 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3318 | return Error(getLexer().getLoc(), |
| 3319 | "unexpected token in '.cfi_signal_frame'"); |
| 3320 | |
| 3321 | getStreamer().EmitCFISignalFrame(); |
| 3322 | return false; |
| 3323 | } |
| 3324 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3325 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3326 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3327 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3328 | int64_t Register = 0; |
| 3329 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3330 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3331 | return true; |
| 3332 | |
| 3333 | getStreamer().EmitCFIUndefined(Register); |
| 3334 | return false; |
| 3335 | } |
| 3336 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3337 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3338 | /// ::= .macros_on |
| 3339 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3340 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3341 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3342 | return Error(getLexer().getLoc(), |
| 3343 | "unexpected token in '" + Directive + "' directive"); |
| 3344 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3345 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3346 | return false; |
| 3347 | } |
| 3348 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3349 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3350 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3351 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3352 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3353 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3354 | return TokError("expected identifier in '.macro' directive"); |
| 3355 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3356 | if (getLexer().is(AsmToken::Comma)) |
| 3357 | Lex(); |
| 3358 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3359 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3360 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3361 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3362 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3363 | return Error(Lexer.getLoc(), |
| 3364 | "Vararg parameter '" + Parameters.back().Name + |
| 3365 | "' should be last one in the list of parameters."); |
| 3366 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3367 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3368 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3369 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3370 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3371 | if (Lexer.is(AsmToken::Colon)) { |
| 3372 | Lex(); // consume ':' |
| 3373 | |
| 3374 | SMLoc QualLoc; |
| 3375 | StringRef Qualifier; |
| 3376 | |
| 3377 | QualLoc = Lexer.getLoc(); |
| 3378 | if (parseIdentifier(Qualifier)) |
| 3379 | return Error(QualLoc, "missing parameter qualifier for " |
| 3380 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3381 | |
| 3382 | if (Qualifier == "req") |
| 3383 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 3384 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3385 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3386 | else |
| 3387 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 3388 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3389 | } |
| 3390 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3391 | if (getLexer().is(AsmToken::Equal)) { |
| 3392 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3393 | |
| 3394 | SMLoc ParamLoc; |
| 3395 | |
| 3396 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3397 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3398 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3399 | |
| 3400 | if (Parameter.Required) |
| 3401 | Warning(ParamLoc, "pointless default value for required parameter " |
| 3402 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3403 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3404 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3405 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (getLexer().is(AsmToken::Comma)) |
| 3408 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | // Eat the end of statement. |
| 3412 | Lex(); |
| 3413 | |
| 3414 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3415 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3416 | |
| 3417 | // Lex the macro definition. |
| 3418 | for (;;) { |
| 3419 | // Check whether we have reached the end of the file. |
| 3420 | if (getLexer().is(AsmToken::Eof)) |
| 3421 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3422 | |
| 3423 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3424 | if (getLexer().is(AsmToken::Identifier)) { |
| 3425 | if (getTok().getIdentifier() == ".endm" || |
| 3426 | getTok().getIdentifier() == ".endmacro") { |
| 3427 | if (MacroDepth == 0) { // Outermost macro. |
| 3428 | EndToken = getTok(); |
| 3429 | Lex(); |
| 3430 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3431 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3432 | "' directive"); |
| 3433 | break; |
| 3434 | } else { |
| 3435 | // Otherwise we just found the end of an inner macro. |
| 3436 | --MacroDepth; |
| 3437 | } |
| 3438 | } else if (getTok().getIdentifier() == ".macro") { |
| 3439 | // We allow nested macros. Those aren't instantiated until the outermost |
| 3440 | // macro is expanded so just ignore them for now. |
| 3441 | ++MacroDepth; |
| 3442 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3446 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3449 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3450 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3451 | } |
| 3452 | |
| 3453 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3454 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3455 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3456 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3457 | defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters))); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3458 | return false; |
| 3459 | } |
| 3460 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3461 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3462 | /// |
| 3463 | /// With the support added for named parameters there may be code out there that |
| 3464 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3465 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3466 | /// 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] | 3467 | /// 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] | 3468 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3469 | /// warning that the positional parameter found in body which have no effect. |
| 3470 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3471 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3472 | /// intended or change the macro to use the named parameters. It is possible |
| 3473 | /// this warning will trigger when the none of the named parameters are used |
| 3474 | /// 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] | 3475 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3476 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 3477 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3478 | // If this macro is not defined with named parameters the warning we are |
| 3479 | // checking for here doesn't apply. |
| 3480 | unsigned NParameters = Parameters.size(); |
| 3481 | if (NParameters == 0) |
| 3482 | return; |
| 3483 | |
| 3484 | bool NamedParametersFound = false; |
| 3485 | bool PositionalParametersFound = false; |
| 3486 | |
| 3487 | // Look at the body of the macro for use of both the named parameters and what |
| 3488 | // are likely to be positional parameters. This is what expandMacro() is |
| 3489 | // doing when it finds the parameters in the body. |
| 3490 | while (!Body.empty()) { |
| 3491 | // Scan for the next possible parameter. |
| 3492 | std::size_t End = Body.size(), Pos = 0; |
| 3493 | for (; Pos != End; ++Pos) { |
| 3494 | // Check for a substitution or escape. |
| 3495 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3496 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3497 | break; |
| 3498 | |
| 3499 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3500 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3501 | continue; |
| 3502 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 3503 | if (Next == '$' || Next == 'n' || |
| 3504 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | // Check if we reached the end. |
| 3509 | if (Pos == End) |
| 3510 | break; |
| 3511 | |
| 3512 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3513 | switch (Body[Pos + 1]) { |
| 3514 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3515 | case '$': |
| 3516 | break; |
| 3517 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3518 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3519 | case 'n': |
| 3520 | PositionalParametersFound = true; |
| 3521 | break; |
| 3522 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3523 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3524 | default: { |
| 3525 | PositionalParametersFound = true; |
| 3526 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3527 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3528 | } |
| 3529 | Pos += 2; |
| 3530 | } else { |
| 3531 | unsigned I = Pos + 1; |
| 3532 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3533 | ++I; |
| 3534 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3535 | const char *Begin = Body.data() + Pos + 1; |
| 3536 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3537 | unsigned Index = 0; |
| 3538 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3539 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3540 | break; |
| 3541 | |
| 3542 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3543 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 3544 | Pos += 3; |
| 3545 | else { |
| 3546 | Pos = I; |
| 3547 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3548 | } else { |
| 3549 | NamedParametersFound = true; |
| 3550 | Pos += 1 + Argument.size(); |
| 3551 | } |
| 3552 | } |
| 3553 | // Update the scan point. |
| 3554 | Body = Body.substr(Pos); |
| 3555 | } |
| 3556 | |
| 3557 | if (!NamedParametersFound && PositionalParametersFound) |
| 3558 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3559 | "used in macro body, possible positional parameter " |
| 3560 | "found in body which will have no effect"); |
| 3561 | } |
| 3562 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 3563 | /// parseDirectiveExitMacro |
| 3564 | /// ::= .exitm |
| 3565 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
| 3566 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3567 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3568 | |
| 3569 | if (!isInsideMacroInstantiation()) |
| 3570 | return TokError("unexpected '" + Directive + "' in file, " |
| 3571 | "no current macro definition"); |
| 3572 | |
| 3573 | // Exit all conditionals that are active in the current macro. |
| 3574 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 3575 | TheCondState = TheCondStack.back(); |
| 3576 | TheCondStack.pop_back(); |
| 3577 | } |
| 3578 | |
| 3579 | handleMacroExit(); |
| 3580 | return false; |
| 3581 | } |
| 3582 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3583 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3584 | /// ::= .endm |
| 3585 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3586 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3587 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3588 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3589 | |
| 3590 | // If we are inside a macro instantiation, terminate the current |
| 3591 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3592 | if (isInsideMacroInstantiation()) { |
| 3593 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3594 | return false; |
| 3595 | } |
| 3596 | |
| 3597 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3598 | // .endmacro directives are handled during the macro definition parsing. |
| 3599 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3600 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3603 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3604 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3605 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3606 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3607 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3608 | return TokError("expected identifier in '.purgem' directive"); |
| 3609 | |
| 3610 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3611 | return TokError("unexpected token in '.purgem' directive"); |
| 3612 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3613 | if (!lookupMacro(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3614 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3615 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3616 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3617 | return false; |
| 3618 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3619 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3620 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3621 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3622 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3623 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3624 | |
| 3625 | // Expect a single argument: an expression that evaluates to a constant |
| 3626 | // in the inclusive range 0-30. |
| 3627 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3628 | int64_t AlignSizePow2; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3629 | if (parseAbsoluteExpression(AlignSizePow2)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3630 | return true; |
| 3631 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3632 | return TokError("unexpected token after expression in" |
| 3633 | " '.bundle_align_mode' directive"); |
| 3634 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3635 | return Error(ExprLoc, |
| 3636 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3637 | |
| 3638 | Lex(); |
| 3639 | |
| 3640 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3641 | // unsigned. |
| 3642 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3643 | return false; |
| 3644 | } |
| 3645 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3646 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3647 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3648 | bool AsmParser::parseDirectiveBundleLock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3649 | checkForValidSection(); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3650 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3651 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3652 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3653 | StringRef Option; |
| 3654 | SMLoc Loc = getTok().getLoc(); |
| 3655 | const char *kInvalidOptionError = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3656 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3657 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3658 | if (parseIdentifier(Option)) |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3659 | return Error(Loc, kInvalidOptionError); |
| 3660 | |
| 3661 | if (Option != "align_to_end") |
| 3662 | return Error(Loc, kInvalidOptionError); |
| 3663 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3664 | return Error(Loc, |
| 3665 | "unexpected token after '.bundle_lock' directive option"); |
| 3666 | AlignToEnd = true; |
| 3667 | } |
| 3668 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3669 | Lex(); |
| 3670 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3671 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3672 | return false; |
| 3673 | } |
| 3674 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3675 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3676 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3677 | bool AsmParser::parseDirectiveBundleUnlock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3678 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3679 | |
| 3680 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3681 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3682 | Lex(); |
| 3683 | |
| 3684 | getStreamer().EmitBundleUnlock(); |
| 3685 | return false; |
| 3686 | } |
| 3687 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3688 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3689 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3690 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3691 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3692 | |
| 3693 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3694 | if (parseAbsoluteExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3695 | return true; |
| 3696 | |
| 3697 | int64_t FillExpr = 0; |
| 3698 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3699 | if (getLexer().isNot(AsmToken::Comma)) |
| 3700 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3701 | Lex(); |
| 3702 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3703 | if (parseAbsoluteExpression(FillExpr)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3704 | return true; |
| 3705 | |
| 3706 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3707 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3708 | } |
| 3709 | |
| 3710 | Lex(); |
| 3711 | |
| 3712 | if (NumBytes <= 0) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3713 | return TokError("invalid number of bytes in '" + Twine(IDVal) + |
| 3714 | "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3715 | |
| 3716 | // 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] | 3717 | getStreamer().EmitFill(NumBytes, FillExpr); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3718 | |
| 3719 | return false; |
| 3720 | } |
| 3721 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3722 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3723 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3724 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3725 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3726 | const MCExpr *Value; |
| 3727 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3728 | for (;;) { |
| 3729 | if (parseExpression(Value)) |
| 3730 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3731 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3732 | if (Signed) |
| 3733 | getStreamer().EmitSLEB128Value(Value); |
| 3734 | else |
| 3735 | getStreamer().EmitULEB128Value(Value); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3736 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3737 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3738 | break; |
| 3739 | |
| 3740 | if (getLexer().isNot(AsmToken::Comma)) |
| 3741 | return TokError("unexpected token in directive"); |
| 3742 | Lex(); |
| 3743 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3744 | |
| 3745 | return false; |
| 3746 | } |
| 3747 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3748 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3749 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3750 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3751 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3752 | for (;;) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3753 | StringRef Name; |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3754 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3755 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3756 | if (parseIdentifier(Name)) |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3757 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3758 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3759 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3760 | |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3761 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3762 | if (Sym->isTemporary()) |
| 3763 | return Error(Loc, "non-local symbol required in directive"); |
| 3764 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 3765 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 3766 | return Error(Loc, "unable to emit symbol attribute"); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3767 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3768 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3769 | break; |
| 3770 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3771 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3772 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3773 | Lex(); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3777 | Lex(); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3778 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3779 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3780 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3781 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3782 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3783 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3784 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3785 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3786 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3787 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3788 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3789 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3790 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3791 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3792 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3793 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3794 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3795 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3796 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3797 | |
| 3798 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3799 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3800 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3801 | return true; |
| 3802 | |
| 3803 | int64_t Pow2Alignment = 0; |
| 3804 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3805 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3806 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3807 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3808 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3809 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3810 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3811 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3812 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3813 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3814 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3815 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3816 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3817 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3818 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3819 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3820 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3821 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3822 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3823 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3824 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3825 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3826 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3827 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3828 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3829 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3830 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3831 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3832 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3833 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3834 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3835 | // 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] | 3836 | // may internally end up wanting an alignment in bytes. |
| 3837 | // FIXME: Diagnose overflow. |
| 3838 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3839 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3840 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3841 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3842 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3843 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3844 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3845 | // 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] | 3846 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3847 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3848 | return false; |
| 3849 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3850 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3851 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3852 | return false; |
| 3853 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3854 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3855 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3856 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3857 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3858 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3859 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3860 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3861 | StringRef Str = parseStringToEndOfStatement(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3862 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3863 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3864 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3865 | Lex(); |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3866 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3867 | if (Str.empty()) |
| 3868 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3869 | else |
| 3870 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3871 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3872 | |
| 3873 | return false; |
| 3874 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3875 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3876 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3877 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3878 | bool AsmParser::parseDirectiveInclude() { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3879 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3880 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3881 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3882 | // Allow the strings to have escaped octal character sequence. |
| 3883 | std::string Filename; |
| 3884 | if (parseEscapedString(Filename)) |
| 3885 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3886 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3887 | Lex(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3888 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3889 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3890 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3891 | |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3892 | // Attempt to switch the lexer to the included file before consuming the end |
| 3893 | // of statement to avoid losing it when we switch. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3894 | if (enterIncludeFile(Filename)) { |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3895 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3896 | return true; |
| 3897 | } |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3898 | |
| 3899 | return false; |
| 3900 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3901 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3902 | /// parseDirectiveIncbin |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3903 | /// ::= .incbin "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3904 | bool AsmParser::parseDirectiveIncbin() { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3905 | if (getLexer().isNot(AsmToken::String)) |
| 3906 | return TokError("expected string in '.incbin' directive"); |
| 3907 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3908 | // Allow the strings to have escaped octal character sequence. |
| 3909 | std::string Filename; |
| 3910 | if (parseEscapedString(Filename)) |
| 3911 | return true; |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3912 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3913 | Lex(); |
| 3914 | |
| 3915 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3916 | return TokError("unexpected token in '.incbin' directive"); |
| 3917 | |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3918 | // Attempt to process the included file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3919 | if (processIncbinFile(Filename)) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3920 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3921 | return true; |
| 3922 | } |
| 3923 | |
| 3924 | return false; |
| 3925 | } |
| 3926 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3927 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3928 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 3929 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3930 | TheCondStack.push_back(TheCondState); |
| 3931 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3932 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3933 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3934 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3935 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3936 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3937 | return true; |
| 3938 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3939 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3940 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3941 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3942 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3943 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3944 | switch (DirKind) { |
| 3945 | default: |
| 3946 | llvm_unreachable("unsupported directive"); |
| 3947 | case DK_IF: |
| 3948 | case DK_IFNE: |
| 3949 | break; |
| 3950 | case DK_IFEQ: |
| 3951 | ExprValue = ExprValue == 0; |
| 3952 | break; |
| 3953 | case DK_IFGE: |
| 3954 | ExprValue = ExprValue >= 0; |
| 3955 | break; |
| 3956 | case DK_IFGT: |
| 3957 | ExprValue = ExprValue > 0; |
| 3958 | break; |
| 3959 | case DK_IFLE: |
| 3960 | ExprValue = ExprValue <= 0; |
| 3961 | break; |
| 3962 | case DK_IFLT: |
| 3963 | ExprValue = ExprValue < 0; |
| 3964 | break; |
| 3965 | } |
| 3966 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3967 | TheCondState.CondMet = ExprValue; |
| 3968 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3969 | } |
| 3970 | |
| 3971 | return false; |
| 3972 | } |
| 3973 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3974 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3975 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3976 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3977 | TheCondStack.push_back(TheCondState); |
| 3978 | TheCondState.TheCond = AsmCond::IfCond; |
| 3979 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3980 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3981 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3982 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3983 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3984 | |
| 3985 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3986 | return TokError("unexpected token in '.ifb' directive"); |
| 3987 | |
| 3988 | Lex(); |
| 3989 | |
| 3990 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 3991 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3992 | } |
| 3993 | |
| 3994 | return false; |
| 3995 | } |
| 3996 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3997 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3998 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 3999 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4000 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4001 | TheCondStack.push_back(TheCondState); |
| 4002 | TheCondState.TheCond = AsmCond::IfCond; |
| 4003 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4004 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4005 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4006 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4007 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4008 | |
| 4009 | if (getLexer().isNot(AsmToken::Comma)) |
| 4010 | return TokError("unexpected token in '.ifc' directive"); |
| 4011 | |
| 4012 | Lex(); |
| 4013 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4014 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4015 | |
| 4016 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4017 | return TokError("unexpected token in '.ifc' directive"); |
| 4018 | |
| 4019 | Lex(); |
| 4020 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4021 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4022 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4023 | } |
| 4024 | |
| 4025 | return false; |
| 4026 | } |
| 4027 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4028 | /// parseDirectiveIfeqs |
| 4029 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4030 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4031 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4032 | if (ExpectEqual) |
| 4033 | TokError("expected string parameter for '.ifeqs' directive"); |
| 4034 | else |
| 4035 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4036 | eatToEndOfStatement(); |
| 4037 | return true; |
| 4038 | } |
| 4039 | |
| 4040 | StringRef String1 = getTok().getStringContents(); |
| 4041 | Lex(); |
| 4042 | |
| 4043 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4044 | if (ExpectEqual) |
| 4045 | TokError("expected comma after first string for '.ifeqs' directive"); |
| 4046 | else |
| 4047 | TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4048 | eatToEndOfStatement(); |
| 4049 | return true; |
| 4050 | } |
| 4051 | |
| 4052 | Lex(); |
| 4053 | |
| 4054 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4055 | if (ExpectEqual) |
| 4056 | TokError("expected string parameter for '.ifeqs' directive"); |
| 4057 | else |
| 4058 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4059 | eatToEndOfStatement(); |
| 4060 | return true; |
| 4061 | } |
| 4062 | |
| 4063 | StringRef String2 = getTok().getStringContents(); |
| 4064 | Lex(); |
| 4065 | |
| 4066 | TheCondStack.push_back(TheCondState); |
| 4067 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4068 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4069 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4070 | |
| 4071 | return false; |
| 4072 | } |
| 4073 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4074 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4075 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4076 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4077 | StringRef Name; |
| 4078 | TheCondStack.push_back(TheCondState); |
| 4079 | TheCondState.TheCond = AsmCond::IfCond; |
| 4080 | |
| 4081 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4082 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4083 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4084 | if (parseIdentifier(Name)) |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4085 | return TokError("expected identifier after '.ifdef'"); |
| 4086 | |
| 4087 | Lex(); |
| 4088 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4089 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4092 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4093 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4094 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4095 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4096 | } |
| 4097 | |
| 4098 | return false; |
| 4099 | } |
| 4100 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4101 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4102 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4103 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4104 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4105 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4106 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 4107 | " an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4108 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4109 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4110 | bool LastIgnoreState = false; |
| 4111 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4112 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4113 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4114 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4115 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4116 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4117 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4118 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4119 | return true; |
| 4120 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4121 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4122 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4123 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4124 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4125 | TheCondState.CondMet = ExprValue; |
| 4126 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4127 | } |
| 4128 | |
| 4129 | return false; |
| 4130 | } |
| 4131 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4132 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4133 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4134 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4135 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4136 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4137 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4138 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4139 | |
| 4140 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4141 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4142 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 4143 | ".elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4144 | TheCondState.TheCond = AsmCond::ElseCond; |
| 4145 | bool LastIgnoreState = false; |
| 4146 | if (!TheCondStack.empty()) |
| 4147 | LastIgnoreState = TheCondStack.back().Ignore; |
| 4148 | if (LastIgnoreState || TheCondState.CondMet) |
| 4149 | TheCondState.Ignore = true; |
| 4150 | else |
| 4151 | TheCondState.Ignore = false; |
| 4152 | |
| 4153 | return false; |
| 4154 | } |
| 4155 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4156 | /// parseDirectiveEnd |
| 4157 | /// ::= .end |
| 4158 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
| 4159 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4160 | return TokError("unexpected token in '.end' directive"); |
| 4161 | |
| 4162 | Lex(); |
| 4163 | |
| 4164 | while (Lexer.isNot(AsmToken::Eof)) |
| 4165 | Lex(); |
| 4166 | |
| 4167 | return false; |
| 4168 | } |
| 4169 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4170 | /// parseDirectiveError |
| 4171 | /// ::= .err |
| 4172 | /// ::= .error [string] |
| 4173 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 4174 | if (!TheCondStack.empty()) { |
| 4175 | if (TheCondStack.back().Ignore) { |
| 4176 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4177 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | if (!WithMessage) |
| 4182 | return Error(L, ".err encountered"); |
| 4183 | |
| 4184 | StringRef Message = ".error directive invoked in source file"; |
| 4185 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4186 | if (Lexer.isNot(AsmToken::String)) { |
| 4187 | TokError(".error argument must be a string"); |
| 4188 | eatToEndOfStatement(); |
| 4189 | return true; |
| 4190 | } |
| 4191 | |
| 4192 | Message = getTok().getStringContents(); |
| 4193 | Lex(); |
| 4194 | } |
| 4195 | |
| 4196 | Error(L, Message); |
| 4197 | return true; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4200 | /// parseDirectiveWarning |
| 4201 | /// ::= .warning [string] |
| 4202 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 4203 | if (!TheCondStack.empty()) { |
| 4204 | if (TheCondStack.back().Ignore) { |
| 4205 | eatToEndOfStatement(); |
| 4206 | return false; |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | StringRef Message = ".warning directive invoked in source file"; |
| 4211 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4212 | if (Lexer.isNot(AsmToken::String)) { |
| 4213 | TokError(".warning argument must be a string"); |
| 4214 | eatToEndOfStatement(); |
| 4215 | return true; |
| 4216 | } |
| 4217 | |
| 4218 | Message = getTok().getStringContents(); |
| 4219 | Lex(); |
| 4220 | } |
| 4221 | |
| 4222 | Warning(L, Message); |
| 4223 | return false; |
| 4224 | } |
| 4225 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4226 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4227 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4228 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4229 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4230 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4231 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4232 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4233 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4234 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4235 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 4236 | ".else"); |
| 4237 | if (!TheCondStack.empty()) { |
| 4238 | TheCondState = TheCondStack.back(); |
| 4239 | TheCondStack.pop_back(); |
| 4240 | } |
| 4241 | |
| 4242 | return false; |
| 4243 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 4244 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4245 | void AsmParser::initializeDirectiveKindMap() { |
| 4246 | DirectiveKindMap[".set"] = DK_SET; |
| 4247 | DirectiveKindMap[".equ"] = DK_EQU; |
| 4248 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 4249 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 4250 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 4251 | DirectiveKindMap[".string"] = DK_STRING; |
| 4252 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 4253 | DirectiveKindMap[".short"] = DK_SHORT; |
| 4254 | DirectiveKindMap[".value"] = DK_VALUE; |
| 4255 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 4256 | DirectiveKindMap[".long"] = DK_LONG; |
| 4257 | DirectiveKindMap[".int"] = DK_INT; |
| 4258 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 4259 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 4260 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 4261 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4262 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 4263 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 4264 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 4265 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 4266 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 4267 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 4268 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 4269 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 4270 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 4271 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 4272 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 4273 | DirectiveKindMap[".org"] = DK_ORG; |
| 4274 | DirectiveKindMap[".fill"] = DK_FILL; |
| 4275 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 4276 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 4277 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 4278 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4279 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 4280 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 4281 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 4282 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 4283 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 4284 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 4285 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 4286 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 4287 | DirectiveKindMap[".comm"] = DK_COMM; |
| 4288 | DirectiveKindMap[".common"] = DK_COMMON; |
| 4289 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 4290 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 4291 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 4292 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 4293 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 4294 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 4295 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4296 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4297 | DirectiveKindMap[".irp"] = DK_IRP; |
| 4298 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 4299 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 4300 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 4301 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 4302 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 4303 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4304 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 4305 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 4306 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 4307 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 4308 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 4309 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4310 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 4311 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 4312 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4313 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4314 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4315 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4316 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 4317 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 4318 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 4319 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 4320 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4321 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4322 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 4323 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 4324 | DirectiveKindMap[".space"] = DK_SPACE; |
| 4325 | DirectiveKindMap[".file"] = DK_FILE; |
| 4326 | DirectiveKindMap[".line"] = DK_LINE; |
| 4327 | DirectiveKindMap[".loc"] = DK_LOC; |
| 4328 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 4329 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 4330 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 4331 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 4332 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 4333 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 4334 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 4335 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 4336 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 4337 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 4338 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 4339 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 4340 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 4341 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 4342 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 4343 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 4344 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 4345 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 4346 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 4347 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 4348 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 4349 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 4350 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4351 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 4352 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 4353 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4354 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4355 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 4356 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 4357 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4358 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4359 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4360 | DirectiveKindMap[".warning"] = DK_WARNING; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4363 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4364 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4365 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4366 | unsigned NestLevel = 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4367 | for (;;) { |
| 4368 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4369 | if (getLexer().is(AsmToken::Eof)) { |
| 4370 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4371 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4374 | if (Lexer.is(AsmToken::Identifier) && |
| 4375 | (getTok().getIdentifier() == ".rept")) { |
| 4376 | ++NestLevel; |
| 4377 | } |
| 4378 | |
| 4379 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4380 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4381 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4382 | EndToken = getTok(); |
| 4383 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4384 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4385 | TokError("unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4386 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4387 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4390 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4393 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4394 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4398 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4399 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 4400 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4401 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4402 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 4403 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4406 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4407 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4408 | OS << ".endr\n"; |
| 4409 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 4410 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 4411 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4412 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4413 | // Create the macro instantiation object and add to the current macro |
| 4414 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 4415 | MacroInstantiation *MI = new MacroInstantiation( |
| 4416 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4417 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4418 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4419 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 4420 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 4421 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4422 | Lex(); |
| 4423 | } |
| 4424 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4425 | /// parseDirectiveRept |
| 4426 | /// ::= .rep | .rept count |
| 4427 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4428 | const MCExpr *CountExpr; |
| 4429 | SMLoc CountLoc = getTok().getLoc(); |
| 4430 | if (parseExpression(CountExpr)) |
| 4431 | return true; |
| 4432 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4433 | int64_t Count; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 4434 | if (!CountExpr->evaluateAsAbsolute(Count)) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4435 | eatToEndOfStatement(); |
| 4436 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 4437 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4438 | |
| 4439 | if (Count < 0) |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4440 | return Error(CountLoc, "Count is negative"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4441 | |
| 4442 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4443 | return TokError("unexpected token in '" + Dir + "' directive"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4444 | |
| 4445 | // Eat the end of statement. |
| 4446 | Lex(); |
| 4447 | |
| 4448 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4449 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4450 | if (!M) |
| 4451 | return true; |
| 4452 | |
| 4453 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4454 | // to hold the macro body with substitutions. |
| 4455 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4456 | raw_svector_ostream OS(Buf); |
| 4457 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4458 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 4459 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4460 | return true; |
| 4461 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4462 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4463 | |
| 4464 | return false; |
| 4465 | } |
| 4466 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4467 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4468 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4469 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4470 | MCAsmMacroParameter Parameter; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4471 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4472 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4473 | return TokError("expected identifier in '.irp' directive"); |
| 4474 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4475 | if (Lexer.isNot(AsmToken::Comma)) |
| 4476 | return TokError("expected comma in '.irp' directive"); |
| 4477 | |
| 4478 | Lex(); |
| 4479 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4480 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4481 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4482 | return true; |
| 4483 | |
| 4484 | // Eat the end of statement. |
| 4485 | Lex(); |
| 4486 | |
| 4487 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4488 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4489 | if (!M) |
| 4490 | return true; |
| 4491 | |
| 4492 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4493 | // to hold the macro body with substitutions. |
| 4494 | SmallString<256> Buf; |
| 4495 | raw_svector_ostream OS(Buf); |
| 4496 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 4497 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4498 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 4499 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 4500 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4501 | return true; |
| 4502 | } |
| 4503 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4504 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4505 | |
| 4506 | return false; |
| 4507 | } |
| 4508 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4509 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4510 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4511 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4512 | MCAsmMacroParameter Parameter; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4513 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4514 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4515 | return TokError("expected identifier in '.irpc' directive"); |
| 4516 | |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4517 | if (Lexer.isNot(AsmToken::Comma)) |
| 4518 | return TokError("expected comma in '.irpc' directive"); |
| 4519 | |
| 4520 | Lex(); |
| 4521 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4522 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4523 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4524 | return true; |
| 4525 | |
| 4526 | if (A.size() != 1 || A.front().size() != 1) |
| 4527 | return TokError("unexpected token in '.irpc' directive"); |
| 4528 | |
| 4529 | // Eat the end of statement. |
| 4530 | Lex(); |
| 4531 | |
| 4532 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4533 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4534 | if (!M) |
| 4535 | return true; |
| 4536 | |
| 4537 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4538 | // to hold the macro body with substitutions. |
| 4539 | SmallString<256> Buf; |
| 4540 | raw_svector_ostream OS(Buf); |
| 4541 | |
| 4542 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4543 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 4544 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4545 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4546 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4547 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 4548 | // This is undocumented, but GAS seems to support it. |
| 4549 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4550 | return true; |
| 4551 | } |
| 4552 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4553 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4554 | |
| 4555 | return false; |
| 4556 | } |
| 4557 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4558 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4559 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 4560 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4561 | |
| 4562 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4563 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4564 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 4565 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4566 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4567 | return false; |
| 4568 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 4569 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4570 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4571 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4572 | const MCExpr *Value; |
| 4573 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4574 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4575 | return true; |
| 4576 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4577 | if (!MCE) |
| 4578 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 4579 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 4580 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4581 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4582 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4583 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4584 | return false; |
| 4585 | } |
| 4586 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4587 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4588 | const MCExpr *Value; |
| 4589 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4590 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4591 | return true; |
| 4592 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4593 | if (!MCE) |
| 4594 | return Error(ExprLoc, "unexpected expression in align"); |
| 4595 | uint64_t IntValue = MCE->getValue(); |
| 4596 | if (!isPowerOf2_64(IntValue)) |
| 4597 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 4598 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4599 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4600 | return false; |
| 4601 | } |
| 4602 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 4603 | // We are comparing pointers, but the pointers are relative to a single string. |
| 4604 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 4605 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 4606 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 4607 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 4608 | return -1; |
| 4609 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 4610 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4611 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4612 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 4613 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 4614 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 4615 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4616 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 4617 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4618 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4619 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4620 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 4621 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4622 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4623 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4626 | bool AsmParser::parseMSInlineAsm( |
| 4627 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
| 4628 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
| 4629 | SmallVectorImpl<std::string> &Constraints, |
| 4630 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 4631 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4632 | SmallVector<void *, 4> InputDecls; |
| 4633 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4634 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4635 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4636 | SmallVector<std::string, 4> InputConstraints; |
| 4637 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4638 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4639 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4640 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Prime the lexer. |
| 4643 | Lex(); |
| 4644 | |
| 4645 | // While we have input, parse each statement. |
| 4646 | unsigned InputIdx = 0; |
| 4647 | unsigned OutputIdx = 0; |
| 4648 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4649 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4650 | if (parseStatement(Info, &SI)) |
Chad Rosier | f1f6a72 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4651 | return true; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4652 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4653 | if (Info.ParseError) |
| 4654 | return true; |
| 4655 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4656 | if (Info.Opcode == ~0U) |
| 4657 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4658 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4659 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4660 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4661 | // Build the list of clobbers, outputs and inputs. |
| 4662 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4663 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4664 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4665 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4666 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4667 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4668 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4669 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 4670 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 4671 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4672 | unsigned NumDefs = Desc.getNumDefs(); |
| 4673 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4674 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 4675 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4676 | continue; |
| 4677 | } |
| 4678 | |
| 4679 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4680 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4681 | if (SymName.empty()) |
| 4682 | continue; |
| 4683 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4684 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4685 | if (!OpDecl) |
| 4686 | continue; |
| 4687 | |
| 4688 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4689 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4690 | if (isOutput) { |
| 4691 | ++InputIdx; |
| 4692 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4693 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 4694 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4695 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4696 | } else { |
| 4697 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4698 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 4699 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4700 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4701 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4702 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4705 | ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(), |
| 4706 | Desc.getNumImplicitDefs()); |
| 4707 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4711 | NumOutputs = OutputDecls.size(); |
| 4712 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4713 | |
| 4714 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4715 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 4716 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 4717 | ClobberRegs.end()); |
| 4718 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 4719 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 4720 | raw_string_ostream OS(Clobbers[I]); |
| 4721 | IP->printRegName(OS, ClobberRegs[I]); |
| 4722 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4723 | |
| 4724 | // Merge the various outputs and inputs. Output are expected first. |
| 4725 | if (NumOutputs || NumInputs) { |
| 4726 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4727 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4728 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4729 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4730 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4731 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4732 | } |
| 4733 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4734 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4735 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4740 | std::string AsmStringIR; |
| 4741 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 4742 | StringRef ASMString = |
| 4743 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 4744 | const char *AsmStart = ASMString.begin(); |
| 4745 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4746 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4747 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 4748 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4749 | if (Kind == AOK_Delete) |
| 4750 | continue; |
| 4751 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4752 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4753 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4754 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 4755 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4756 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4757 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4758 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4759 | // Skip the original expression. |
| 4760 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4761 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4762 | continue; |
| 4763 | } |
| 4764 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4765 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4766 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4767 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4768 | default: |
| 4769 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4770 | case AOK_Imm: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4771 | OS << "$$" << AR.Val; |
Chad Rosier | 11c42f2 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4772 | break; |
| 4773 | case AOK_ImmPrefix: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4774 | OS << "$$"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4775 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4776 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 4777 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4778 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4779 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4780 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4781 | break; |
| 4782 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4783 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4784 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4785 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4786 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4787 | default: break; |
| 4788 | case 8: OS << "byte ptr "; break; |
| 4789 | case 16: OS << "word ptr "; break; |
| 4790 | case 32: OS << "dword ptr "; break; |
| 4791 | case 64: OS << "qword ptr "; break; |
| 4792 | case 80: OS << "xword ptr "; break; |
| 4793 | case 128: OS << "xmmword ptr "; break; |
| 4794 | case 256: OS << "ymmword ptr "; break; |
| 4795 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4796 | break; |
| 4797 | case AOK_Emit: |
| 4798 | OS << ".byte"; |
| 4799 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4800 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 4801 | // MS alignment directives are measured in bytes. If the native assembler |
| 4802 | // measures alignment in bytes, we can pass it straight through. |
| 4803 | OS << ".align"; |
| 4804 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 4805 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4806 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 4807 | // Alignment is in log2 form, so print that instead and skip the original |
| 4808 | // immediate. |
| 4809 | unsigned Val = AR.Val; |
| 4810 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4811 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4812 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 4813 | break; |
| 4814 | } |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4815 | case AOK_DotOperator: |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4816 | // Insert the dot if the user omitted it. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4817 | OS.flush(); |
| 4818 | if (AsmStringIR.back() != '.') |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4819 | OS << '.'; |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4820 | OS << AR.Val; |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4821 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4822 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4823 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4824 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4825 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4829 | if (AsmStart != AsmEnd) |
| 4830 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4831 | |
| 4832 | AsmString = OS.str(); |
| 4833 | return false; |
| 4834 | } |
| 4835 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4836 | namespace llvm { |
| 4837 | namespace MCParserUtils { |
| 4838 | |
| 4839 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 4840 | /// or subexpressions. |
| 4841 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 4842 | switch (Value->getKind()) { |
| 4843 | case MCExpr::Binary: { |
| 4844 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 4845 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 4846 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 4847 | } |
| 4848 | case MCExpr::Target: |
| 4849 | case MCExpr::Constant: |
| 4850 | return false; |
| 4851 | case MCExpr::SymbolRef: { |
| 4852 | const MCSymbol &S = |
| 4853 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 4854 | if (S.isVariable()) |
| 4855 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 4856 | return &S == Sym; |
| 4857 | } |
| 4858 | case MCExpr::Unary: |
| 4859 | return isSymbolUsedInExpression( |
| 4860 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 4861 | } |
| 4862 | |
| 4863 | llvm_unreachable("Unknown expr kind!"); |
| 4864 | } |
| 4865 | |
| 4866 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 4867 | MCAsmParser &Parser, MCSymbol *&Sym, |
| 4868 | const MCExpr *&Value) { |
| 4869 | MCAsmLexer &Lexer = Parser.getLexer(); |
| 4870 | |
| 4871 | // FIXME: Use better location, we should use proper tokens. |
| 4872 | SMLoc EqualLoc = Lexer.getLoc(); |
| 4873 | |
| 4874 | if (Parser.parseExpression(Value)) { |
| 4875 | Parser.TokError("missing expression"); |
| 4876 | Parser.eatToEndOfStatement(); |
| 4877 | return true; |
| 4878 | } |
| 4879 | |
| 4880 | // Note: we don't count b as used in "a = b". This is to allow |
| 4881 | // a = b |
| 4882 | // b = c |
| 4883 | |
| 4884 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 4885 | return Parser.TokError("unexpected token in assignment"); |
| 4886 | |
| 4887 | // Eat the end of statement marker. |
| 4888 | Parser.Lex(); |
| 4889 | |
| 4890 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 4891 | // used as a symbol, or it is an absolute symbol). |
| 4892 | Sym = Parser.getContext().lookupSymbol(Name); |
| 4893 | if (Sym) { |
| 4894 | // Diagnose assignment to a label. |
| 4895 | // |
| 4896 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 4897 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 4898 | if (isSymbolUsedInExpression(Sym, Value)) |
| 4899 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 4900 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 4901 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4902 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 4903 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 4904 | ; // Allow redefinitions of variables that haven't yet been used. |
| 4905 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 4906 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 4907 | else if (!Sym->isVariable()) |
| 4908 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 4909 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 4910 | return Parser.Error(EqualLoc, |
| 4911 | "invalid reassignment of non-absolute variable '" + |
| 4912 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4913 | } else if (Name == ".") { |
Rafael Espindola | 7ae65d8 | 2015-11-04 23:59:18 +0000 | [diff] [blame] | 4914 | Parser.getStreamer().emitValueToOffset(Value, 0); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4915 | return false; |
| 4916 | } else |
| 4917 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 4918 | |
| 4919 | Sym->setRedefinable(allow_redef); |
| 4920 | |
| 4921 | return false; |
| 4922 | } |
| 4923 | |
| 4924 | } // namespace MCParserUtils |
| 4925 | } // namespace llvm |
| 4926 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4927 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4928 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
| 4929 | MCStreamer &Out, const MCAsmInfo &MAI) { |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4930 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4931 | } |