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