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" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/None.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 24 | #include "llvm/BinaryFormat/Dwarf.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAsmInfo.h" |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCCodeView.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCDirectives.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCInstPrinter.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstrDesc.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrInfo.h" |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCObjectFileInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCParser/AsmCond.h" |
| 36 | #include "llvm/MC/MCParser/AsmLexer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCParser/MCAsmParserUtils.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Benjamin Kramer | b3e8a6d | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 42 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCRegisterInfo.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 45 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 46 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCTargetOptions.h" |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Casting.h" |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 50 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 51 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 52 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 54 | #include "llvm/Support/SMLoc.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 55 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 57 | #include <algorithm> |
| 58 | #include <cassert> |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 59 | #include <cctype> |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 60 | #include <climits> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 61 | #include <cstddef> |
| 62 | #include <cstdint> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 63 | #include <deque> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 64 | #include <memory> |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 65 | #include <sstream> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 66 | #include <string> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 67 | #include <tuple> |
| 68 | #include <utility> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 69 | #include <vector> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 70 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 71 | using namespace llvm; |
| 72 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 73 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 74 | |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 75 | static cl::opt<unsigned> AsmMacroMaxNestingDepth( |
| 76 | "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden, |
| 77 | cl::desc("The maximum nesting depth allowed for assembly macros.")); |
| 78 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 79 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 80 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 81 | /// \brief Helper types for tracking macro definitions. |
| 82 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 83 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 84 | |
| 85 | struct MCAsmMacroParameter { |
| 86 | StringRef Name; |
| 87 | MCAsmMacroArgument Value; |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 88 | bool Required = false; |
| 89 | bool Vararg = false; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 90 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 91 | MCAsmMacroParameter() = default; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 94 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 95 | |
| 96 | struct MCAsmMacro { |
| 97 | StringRef Name; |
| 98 | StringRef Body; |
| 99 | MCAsmMacroParameters Parameters; |
| 100 | |
| 101 | public: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 102 | MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P) |
| 103 | : Name(N), Body(B), Parameters(std::move(P)) {} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 106 | /// \brief Helper class for storing information about an active macro |
| 107 | /// instantiation. |
| 108 | struct MacroInstantiation { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 109 | /// The location of the instantiation. |
| 110 | SMLoc InstantiationLoc; |
| 111 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 112 | /// The buffer where parsing should resume upon instantiation completion. |
| 113 | int ExitBuffer; |
| 114 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 115 | /// The location where parsing should resume upon instantiation completion. |
| 116 | SMLoc ExitLoc; |
| 117 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 118 | /// The depth of TheCondStack at the start of the instantiation. |
| 119 | size_t CondStackDepth; |
| 120 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 121 | public: |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 122 | MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 125 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 126 | /// \brief The parsed operands from the last parsed statement. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 127 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 128 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 129 | /// \brief The opcode from the last parsed instruction. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 130 | unsigned Opcode = ~0U; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 131 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 132 | /// \brief Was there an error parsing the inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 133 | bool ParseError = false; |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 134 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 135 | SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 136 | |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 137 | ParseStatementInfo() = delete; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 138 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 139 | : AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 142 | /// \brief The concrete assembly parser instance. |
| 143 | class AsmParser : public MCAsmParser { |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 144 | private: |
| 145 | AsmLexer Lexer; |
| 146 | MCContext &Ctx; |
| 147 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 148 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 149 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 150 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 151 | void *SavedDiagContext; |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 152 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 154 | /// This is the current buffer index we're lexing from as managed by the |
| 155 | /// SourceMgr object. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 156 | unsigned CurBuffer; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 157 | |
| 158 | AsmCond TheCondState; |
| 159 | std::vector<AsmCond> TheCondStack; |
| 160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 161 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 162 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 163 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 164 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 165 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 166 | /// \brief Map of currently defined macros. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 167 | StringMap<MCAsmMacro> MacroMap; |
Daniel Dunbar | c1f58ec | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 168 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 169 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 170 | std::vector<MacroInstantiation*> ActiveMacros; |
| 171 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 172 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 173 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 174 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 175 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 176 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 177 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 178 | /// \brief Keeps track of how many .macro's have been instantiated. |
| 179 | unsigned NumOfMacroInstantiations; |
| 180 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 181 | /// The values from the last parsed cpp hash file line comment if any. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 182 | struct CppHashInfoTy { |
| 183 | StringRef Filename; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 184 | int64_t LineNumber = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 185 | SMLoc Loc; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 186 | unsigned Buf = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 187 | }; |
| 188 | CppHashInfoTy CppHashInfo; |
| 189 | |
| 190 | /// \brief List of forward directional labels for diagnosis at the end. |
| 191 | SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
| 192 | |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 193 | /// When generating dwarf for assembly source files we need to calculate the |
| 194 | /// 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] | 195 | /// 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] | 196 | /// cache we save the last info we queried with SrcMgr.FindLineNumber(). |
| 197 | SMLoc LastQueryIDLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 198 | unsigned LastQueryBuffer; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 199 | unsigned LastQueryLine; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 200 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 201 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 202 | unsigned AssemblerDialect = ~0U; |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 203 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 204 | /// \brief is Darwin compatibility enabled? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 205 | bool IsDarwin = false; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 206 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 207 | /// \brief Are we parsing ms-style inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 208 | bool ParsingInlineAsm = false; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 210 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 211 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 212 | const MCAsmInfo &MAI, unsigned CB); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 213 | AsmParser(const AsmParser &) = delete; |
| 214 | AsmParser &operator=(const AsmParser &) = delete; |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 215 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 216 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 217 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 218 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 219 | void addDirectiveHandler(StringRef Directive, |
| 220 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 221 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 224 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 225 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 226 | } |
| 227 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 228 | /// @name MCAsmParser Interface |
| 229 | /// { |
| 230 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 231 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 232 | MCAsmLexer &getLexer() override { return Lexer; } |
| 233 | MCContext &getContext() override { return Ctx; } |
| 234 | MCStreamer &getStreamer() override { return Out; } |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 235 | |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 236 | CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
| 237 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 238 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 239 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 240 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 241 | else |
| 242 | return AssemblerDialect; |
| 243 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 244 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 245 | AssemblerDialect = i; |
| 246 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 247 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 248 | void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 249 | bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 250 | bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 251 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 252 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 253 | |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 254 | void setParsingInlineAsm(bool V) override { |
| 255 | ParsingInlineAsm = V; |
| 256 | Lexer.setParsingMSInlineAsm(V); |
| 257 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 258 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 259 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 260 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 261 | unsigned &NumOutputs, unsigned &NumInputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 262 | SmallVectorImpl<std::pair<void *,bool>> &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 263 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 264 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 265 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 266 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 267 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 268 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 269 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 270 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 271 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 272 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 273 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 274 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 275 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 276 | /// \brief Parse a floating point expression using the float \p Semantics |
| 277 | /// and set \p Res to the value. |
| 278 | bool parseRealValue(const fltSemantics &Semantics, APInt &Res); |
| 279 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 280 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 281 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 282 | bool parseIdentifier(StringRef &Res) override; |
| 283 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 284 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 285 | bool checkForValidSection() override; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 287 | /// } |
| 288 | |
| 289 | private: |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 290 | bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 291 | void altMacroString(StringRef AltMacroStr, std::string &Res); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 292 | bool parseStatement(ParseStatementInfo &Info, |
| 293 | MCAsmParserSemaCallback *SI); |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 294 | bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 295 | bool parseCppHashLineFilenameComment(SMLoc L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 296 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 297 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 298 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 299 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 300 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 301 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 302 | SMLoc L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 303 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 304 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 305 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 306 | |
| 307 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 308 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 309 | |
| 310 | /// \brief Lookup a previously defined macro. |
| 311 | /// \param Name Macro name. |
| 312 | /// \returns Pointer to macro. NULL if no such macro was defined. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 313 | const MCAsmMacro* lookupMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 314 | |
| 315 | /// \brief Define a new macro with the given name and information. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 316 | void defineMacro(StringRef Name, MCAsmMacro Macro); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 317 | |
| 318 | /// \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] | 319 | void undefineMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 320 | |
| 321 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 322 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 323 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 324 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 325 | /// |
| 326 | /// \param M The macro. |
| 327 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 328 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 329 | |
| 330 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 331 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 332 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 333 | /// \brief Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 334 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 335 | |
| 336 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 337 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 338 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 339 | void printMacroInstantiations(); |
| 340 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 341 | SMRange Range = None) const { |
| 342 | ArrayRef<SMRange> Ranges(Range); |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 343 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 344 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 345 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 346 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 347 | /// \brief Enter the specified file. This returns true on failure. |
| 348 | bool enterIncludeFile(const std::string &Filename); |
| 349 | |
| 350 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 351 | /// This returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 352 | bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, |
| 353 | const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 354 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 355 | /// \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] | 356 | /// current token is not set; clients should ensure Lex() is called |
| 357 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 358 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 359 | /// \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] | 360 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 361 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 363 | /// \brief Parse up to the end of statement and a return the contents from the |
| 364 | /// current token until the end of the statement; the current token on exit |
| 365 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 366 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 367 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 368 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 369 | /// 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] | 370 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 371 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 372 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 373 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 374 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 375 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 376 | MCBinaryExpr::Opcode &Kind); |
| 377 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 378 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 379 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 380 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 381 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 382 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 383 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 384 | bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); |
| 385 | bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); |
| 386 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 387 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 388 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 389 | DK_NO_DIRECTIVE, // Placeholder |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 390 | DK_SET, |
| 391 | DK_EQU, |
| 392 | DK_EQUIV, |
| 393 | DK_ASCII, |
| 394 | DK_ASCIZ, |
| 395 | DK_STRING, |
| 396 | DK_BYTE, |
| 397 | DK_SHORT, |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 398 | DK_RELOC, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 399 | DK_VALUE, |
| 400 | DK_2BYTE, |
| 401 | DK_LONG, |
| 402 | DK_INT, |
| 403 | DK_4BYTE, |
| 404 | DK_QUAD, |
| 405 | DK_8BYTE, |
| 406 | DK_OCTA, |
| 407 | DK_DC, |
| 408 | DK_DC_A, |
| 409 | DK_DC_B, |
| 410 | DK_DC_D, |
| 411 | DK_DC_L, |
| 412 | DK_DC_S, |
| 413 | DK_DC_W, |
| 414 | DK_DC_X, |
| 415 | DK_DCB, |
| 416 | DK_DCB_B, |
| 417 | DK_DCB_D, |
| 418 | DK_DCB_L, |
| 419 | DK_DCB_S, |
| 420 | DK_DCB_W, |
| 421 | DK_DCB_X, |
| 422 | DK_DS, |
| 423 | DK_DS_B, |
| 424 | DK_DS_D, |
| 425 | DK_DS_L, |
| 426 | DK_DS_P, |
| 427 | DK_DS_S, |
| 428 | DK_DS_W, |
| 429 | DK_DS_X, |
| 430 | DK_SINGLE, |
| 431 | DK_FLOAT, |
| 432 | DK_DOUBLE, |
| 433 | DK_ALIGN, |
| 434 | DK_ALIGN32, |
| 435 | DK_BALIGN, |
| 436 | DK_BALIGNW, |
| 437 | DK_BALIGNL, |
| 438 | DK_P2ALIGN, |
| 439 | DK_P2ALIGNW, |
| 440 | DK_P2ALIGNL, |
| 441 | DK_ORG, |
| 442 | DK_FILL, |
| 443 | DK_ENDR, |
| 444 | DK_BUNDLE_ALIGN_MODE, |
| 445 | DK_BUNDLE_LOCK, |
| 446 | DK_BUNDLE_UNLOCK, |
| 447 | DK_ZERO, |
| 448 | DK_EXTERN, |
| 449 | DK_GLOBL, |
| 450 | DK_GLOBAL, |
| 451 | DK_LAZY_REFERENCE, |
| 452 | DK_NO_DEAD_STRIP, |
| 453 | DK_SYMBOL_RESOLVER, |
| 454 | DK_PRIVATE_EXTERN, |
| 455 | DK_REFERENCE, |
| 456 | DK_WEAK_DEFINITION, |
| 457 | DK_WEAK_REFERENCE, |
| 458 | DK_WEAK_DEF_CAN_BE_HIDDEN, |
| 459 | DK_COMM, |
| 460 | DK_COMMON, |
| 461 | DK_LCOMM, |
| 462 | DK_ABORT, |
| 463 | DK_INCLUDE, |
| 464 | DK_INCBIN, |
| 465 | DK_CODE16, |
| 466 | DK_CODE16GCC, |
| 467 | DK_REPT, |
| 468 | DK_IRP, |
| 469 | DK_IRPC, |
| 470 | DK_IF, |
| 471 | DK_IFEQ, |
| 472 | DK_IFGE, |
| 473 | DK_IFGT, |
| 474 | DK_IFLE, |
| 475 | DK_IFLT, |
| 476 | DK_IFNE, |
| 477 | DK_IFB, |
| 478 | DK_IFNB, |
| 479 | DK_IFC, |
| 480 | DK_IFEQS, |
| 481 | DK_IFNC, |
| 482 | DK_IFNES, |
| 483 | DK_IFDEF, |
| 484 | DK_IFNDEF, |
| 485 | DK_IFNOTDEF, |
| 486 | DK_ELSEIF, |
| 487 | DK_ELSE, |
| 488 | DK_ENDIF, |
| 489 | DK_SPACE, |
| 490 | DK_SKIP, |
| 491 | DK_FILE, |
| 492 | DK_LINE, |
| 493 | DK_LOC, |
| 494 | DK_STABS, |
| 495 | DK_CV_FILE, |
| 496 | DK_CV_FUNC_ID, |
| 497 | DK_CV_INLINE_SITE_ID, |
| 498 | DK_CV_LOC, |
| 499 | DK_CV_LINETABLE, |
| 500 | DK_CV_INLINE_LINETABLE, |
| 501 | DK_CV_DEF_RANGE, |
| 502 | DK_CV_STRINGTABLE, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 503 | DK_CV_FILECHECKSUMS, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 504 | DK_CFI_SECTIONS, |
| 505 | DK_CFI_STARTPROC, |
| 506 | DK_CFI_ENDPROC, |
| 507 | DK_CFI_DEF_CFA, |
| 508 | DK_CFI_DEF_CFA_OFFSET, |
| 509 | DK_CFI_ADJUST_CFA_OFFSET, |
| 510 | DK_CFI_DEF_CFA_REGISTER, |
| 511 | DK_CFI_OFFSET, |
| 512 | DK_CFI_REL_OFFSET, |
| 513 | DK_CFI_PERSONALITY, |
| 514 | DK_CFI_LSDA, |
| 515 | DK_CFI_REMEMBER_STATE, |
| 516 | DK_CFI_RESTORE_STATE, |
| 517 | DK_CFI_SAME_VALUE, |
| 518 | DK_CFI_RESTORE, |
| 519 | DK_CFI_ESCAPE, |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 520 | DK_CFI_RETURN_COLUMN, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 521 | DK_CFI_SIGNAL_FRAME, |
| 522 | DK_CFI_UNDEFINED, |
| 523 | DK_CFI_REGISTER, |
| 524 | DK_CFI_WINDOW_SAVE, |
| 525 | DK_MACROS_ON, |
| 526 | DK_MACROS_OFF, |
| 527 | DK_ALTMACRO, |
| 528 | DK_NOALTMACRO, |
| 529 | DK_MACRO, |
| 530 | DK_EXITM, |
| 531 | DK_ENDM, |
| 532 | DK_ENDMACRO, |
| 533 | DK_PURGEM, |
| 534 | DK_SLEB128, |
| 535 | DK_ULEB128, |
| 536 | DK_ERR, |
| 537 | DK_ERROR, |
| 538 | DK_WARNING, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 539 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 542 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 543 | /// directives parsed by this class. |
| 544 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 545 | |
| 546 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 547 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 548 | bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 549 | bool parseDirectiveValue(StringRef IDVal, |
| 550 | unsigned Size); // ".byte", ".long", ... |
| 551 | bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
| 552 | bool parseDirectiveRealValue(StringRef IDVal, |
| 553 | const fltSemantics &); // ".single", ... |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 554 | bool parseDirectiveFill(); // ".fill" |
| 555 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 556 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 557 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 558 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 559 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 560 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 561 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 562 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 563 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 564 | bool parseDirectiveLine(); |
| 565 | bool parseDirectiveLoc(); |
| 566 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 567 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 568 | // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", |
| 569 | // ".cv_inline_linetable", ".cv_def_range" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 570 | bool parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 571 | bool parseDirectiveCVFuncId(); |
| 572 | bool parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 573 | bool parseDirectiveCVLoc(); |
| 574 | bool parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 575 | bool parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 576 | bool parseDirectiveCVDefRange(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 577 | bool parseDirectiveCVStringTable(); |
| 578 | bool parseDirectiveCVFileChecksums(); |
| 579 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 580 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 581 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 582 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 583 | bool parseDirectiveCFISections(); |
| 584 | bool parseDirectiveCFIStartProc(); |
| 585 | bool parseDirectiveCFIEndProc(); |
| 586 | bool parseDirectiveCFIDefCfaOffset(); |
| 587 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 588 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 589 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 590 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 591 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 592 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 593 | bool parseDirectiveCFIRememberState(); |
| 594 | bool parseDirectiveCFIRestoreState(); |
| 595 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 596 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 597 | bool parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 598 | bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 599 | bool parseDirectiveCFISignalFrame(); |
| 600 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 601 | |
| 602 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 603 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 604 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 605 | bool parseDirectiveEndMacro(StringRef Directive); |
| 606 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 607 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 608 | // alternate macro mode directives |
| 609 | bool parseDirectiveAltmacro(StringRef Directive); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 610 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 611 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 612 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 613 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 614 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 615 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 616 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 617 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 618 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 619 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 620 | // ".dcb" |
| 621 | bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
| 622 | bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 623 | // ".ds" |
| 624 | bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 625 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 626 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 627 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 628 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 629 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 630 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 631 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 633 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 634 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 635 | bool parseDirectiveAbort(); // ".abort" |
| 636 | bool parseDirectiveInclude(); // ".include" |
| 637 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 638 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 639 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 640 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 641 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 642 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 643 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 644 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 645 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 646 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 647 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 648 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 649 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 650 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 651 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 652 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 653 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 654 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 655 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 656 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 657 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 658 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 659 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 660 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 661 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 662 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 663 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 664 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 665 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 666 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 667 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 668 | size_t Len); |
| 669 | |
| 670 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 671 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 672 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 673 | // "end" |
| 674 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 675 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 676 | // ".err" or ".error" |
| 677 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 678 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 679 | // ".warning" |
| 680 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 681 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 682 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 683 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 684 | |
| 685 | } // end anonymous namespace |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 686 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 687 | namespace llvm { |
| 688 | |
| 689 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 690 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 691 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 692 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 693 | } // end namespace llvm |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 694 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 695 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 696 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 697 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 698 | const MCAsmInfo &MAI, unsigned CB = 0) |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 699 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 700 | CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 701 | HadError = false; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 702 | // Save the old handler. |
| 703 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 704 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 705 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 706 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 707 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 708 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 709 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 710 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 711 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 712 | PlatformParser.reset(createCOFFAsmParser()); |
| 713 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 714 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 715 | PlatformParser.reset(createDarwinAsmParser()); |
| 716 | IsDarwin = true; |
| 717 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 718 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 719 | PlatformParser.reset(createELFAsmParser()); |
| 720 | break; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 721 | case MCObjectFileInfo::IsWasm: |
| 722 | llvm_unreachable("Wasm parsing not supported yet"); |
| 723 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 724 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 725 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 726 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 727 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 728 | |
| 729 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 732 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 733 | assert((HadError || ActiveMacros.empty()) && |
| 734 | "Unexpected active macro instantiation!"); |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 735 | |
| 736 | // Restore the saved diagnostics handler and context for use during |
| 737 | // finalization. |
| 738 | SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 741 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 742 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 743 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 744 | it = ActiveMacros.rbegin(), |
| 745 | ie = ActiveMacros.rend(); |
| 746 | it != ie; ++it) |
| 747 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 748 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 751 | void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
| 752 | printPendingErrors(); |
| 753 | printMessage(L, SourceMgr::DK_Note, Msg, Range); |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 754 | printMacroInstantiations(); |
| 755 | } |
| 756 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 757 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 758 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 759 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 760 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 761 | return Error(L, Msg, Range); |
| 762 | printMessage(L, SourceMgr::DK_Warning, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 763 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 764 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 767 | bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 768 | HadError = true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 769 | printMessage(L, SourceMgr::DK_Error, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 770 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 771 | return true; |
| 772 | } |
| 773 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 774 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 775 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 776 | unsigned NewBuf = |
| 777 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 778 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 779 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 780 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 781 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 782 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 783 | return false; |
| 784 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 785 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 786 | /// 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] | 787 | /// 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] | 788 | /// returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 789 | bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
| 790 | const MCExpr *Count, SMLoc Loc) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 791 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 792 | unsigned NewBuf = |
| 793 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 794 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 795 | return true; |
| 796 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 797 | // Pick up the bytes from the file and emit them. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 798 | StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); |
| 799 | Bytes = Bytes.drop_front(Skip); |
| 800 | if (Count) { |
| 801 | int64_t Res; |
| 802 | if (!Count->evaluateAsAbsolute(Res)) |
| 803 | return Error(Loc, "expected absolute expression"); |
| 804 | if (Res < 0) |
| 805 | return Warning(Loc, "negative count has no effect"); |
| 806 | Bytes = Bytes.take_front(Res); |
| 807 | } |
| 808 | getStreamer().EmitBytes(Bytes); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 812 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 813 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 814 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 815 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 818 | const AsmToken &AsmParser::Lex() { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 819 | if (Lexer.getTok().is(AsmToken::Error)) |
| 820 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
| 821 | |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 822 | // if it's a end of statement with a comment in it |
| 823 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 824 | // if this is a line comment output it. |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 825 | if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 826 | getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
| 827 | Out.addExplicitComment(Twine(getTok().getString())); |
| 828 | } |
| 829 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 830 | const AsmToken *tok = &Lexer.Lex(); |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 831 | |
| 832 | // Parse comments here to be deferred until end of next statement. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 833 | while (tok->is(AsmToken::Comment)) { |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 834 | if (MAI.preserveAsmComments()) |
| 835 | Out.addExplicitComment(Twine(tok->getString())); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 836 | tok = &Lexer.Lex(); |
| 837 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 838 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 839 | if (tok->is(AsmToken::Eof)) { |
| 840 | // If this is the end of an included file, pop the parent file off the |
| 841 | // include stack. |
| 842 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 843 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 844 | jumpToLoc(ParentIncludeLoc); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 845 | return Lex(); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 848 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 849 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 852 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 853 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 854 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 855 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 857 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 858 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 859 | |
| 860 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 861 | AsmCond StartingCondState = TheCondState; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 862 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 863 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 864 | // If we are generating dwarf for assembly source files save the initial text |
| 865 | // section and generate a .file directive. |
| 866 | if (getContext().getGenDwarfForAssembly()) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 867 | MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 868 | if (!Sec->getBeginSymbol()) { |
| 869 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 870 | getStreamer().EmitLabel(SectionStartSym); |
| 871 | Sec->setBeginSymbol(SectionStartSym); |
| 872 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 873 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 874 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 875 | (void)InsertResult; |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 876 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 877 | 0, StringRef(), getContext().getMainFileName())); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 880 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 881 | while (Lexer.isNot(AsmToken::Eof)) { |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 882 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 883 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 884 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 885 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 886 | // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
| 887 | // for printing ErrMsg via Lex() only if no (presumably better) parser error |
| 888 | // exists. |
| 889 | if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 890 | Lex(); |
| 891 | } |
| 892 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 893 | // parseStatement returned true so may need to emit an error. |
| 894 | printPendingErrors(); |
| 895 | |
| 896 | // Skipping to the next line if needed. |
| 897 | if (!getLexer().isAtStartOfStatement()) |
| 898 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 899 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 900 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 901 | // All errors should have been emitted. |
| 902 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
| 903 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 904 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 905 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 906 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 907 | TheCondState.Ignore != StartingCondState.Ignore) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 908 | printError(getTok().getLoc(), "unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 909 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 910 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 911 | if (!LineTables.empty()) { |
| 912 | unsigned Index = 0; |
| 913 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 914 | if (File.Name.empty() && Index != 0) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 915 | printError(getTok().getLoc(), "unassigned file number: " + |
| 916 | Twine(Index) + |
| 917 | " for .file directives"); |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 918 | ++Index; |
| 919 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 920 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 921 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 922 | // Check to see that all assembler local symbols were actually defined. |
| 923 | // Targets that don't do subsections via symbols may not want this, though, |
| 924 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 925 | // as otherwise we won't necessarilly have seen everything yet. |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 926 | if (!NoFinalize) { |
| 927 | if (MAI.hasSubsectionsViaSymbols()) { |
| 928 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 929 | MCSymbol *Sym = TableEntry.getValue(); |
| 930 | // Variable symbols may not be marked as defined, so check those |
| 931 | // explicitly. If we know it's a variable, we have a definition for |
| 932 | // the purposes of this check. |
| 933 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 934 | // FIXME: We would really like to refer back to where the symbol was |
| 935 | // first referenced for a source location. We need to add something |
| 936 | // to track that. Currently, we just point to the end of the file. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 937 | printError(getTok().getLoc(), "assembler local symbol '" + |
| 938 | Sym->getName() + "' not defined"); |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | // Temporary symbols like the ones for directional jumps don't go in the |
| 943 | // symbol table. They also need to be diagnosed in all (final) cases. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 944 | for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
| 945 | if (std::get<2>(LocSym)->isUndefined()) { |
| 946 | // Reset the state of any "# line file" directives we've seen to the |
| 947 | // context as it was at the diagnostic site. |
| 948 | CppHashInfo = std::get<1>(LocSym); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 949 | printError(std::get<0>(LocSym), "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 950 | } |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 954 | // Finalize the output stream if there are no errors and if the client wants |
| 955 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 956 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 957 | Out.Finish(); |
| 958 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 959 | return HadError || getContext().hadError(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 962 | bool AsmParser::checkForValidSection() { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 963 | if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 964 | Out.InitSections(false); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 965 | return Error(getTok().getLoc(), |
| 966 | "expected section directive before assembly directive"); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 967 | } |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 968 | return false; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 971 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 972 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 973 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 974 | Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 975 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 976 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 977 | if (Lexer.is(AsmToken::EndOfStatement)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 978 | Lexer.Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 981 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 982 | const char *Start = getTok().getLoc().getPointer(); |
| 983 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 984 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 985 | Lexer.Lex(); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 986 | |
| 987 | const char *End = getTok().getLoc().getPointer(); |
| 988 | return StringRef(Start, End - Start); |
| 989 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 990 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 991 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 992 | const char *Start = getTok().getLoc().getPointer(); |
| 993 | |
| 994 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 995 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 996 | Lexer.Lex(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 997 | |
| 998 | const char *End = getTok().getLoc().getPointer(); |
| 999 | return StringRef(Start, End - Start); |
| 1000 | } |
| 1001 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1002 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1003 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 1004 | /// |
| 1005 | /// parenexpr ::= expr) |
| 1006 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1007 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1008 | if (parseExpression(Res)) |
| 1009 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1010 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1011 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1012 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1013 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1014 | return false; |
| 1015 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1016 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1017 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1018 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 1019 | /// |
| 1020 | /// bracketexpr ::= expr] |
| 1021 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1022 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1023 | if (parseExpression(Res)) |
| 1024 | return true; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1025 | EndLoc = getTok().getEndLoc(); |
| 1026 | if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) |
| 1027 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
| 1030 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1031 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1032 | /// primaryexpr ::= (parenexpr |
| 1033 | /// primaryexpr ::= symbol |
| 1034 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1035 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1036 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1037 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1038 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 1039 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 1040 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1041 | default: |
| 1042 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 1043 | // If we have an error assume that we've already handled it. |
| 1044 | case AsmToken::Error: |
| 1045 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1046 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1047 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1048 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1049 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1050 | Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1051 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1052 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1053 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1054 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1055 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1056 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1057 | if (parseIdentifier(Identifier)) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1058 | // We may have failed but $ may be a valid token. |
| 1059 | if (getTok().is(AsmToken::Dollar)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1060 | if (Lexer.getMAI().getDollarIsPC()) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1061 | Lex(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1062 | // This is a '$' reference, which references the current PC. Emit a |
| 1063 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1064 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1065 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1066 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 1067 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1068 | EndLoc = FirstTokenLoc; |
| 1069 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 1070 | } |
| 1071 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1072 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1073 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1074 | // Parse symbol variant |
| 1075 | std::pair<StringRef, StringRef> Split; |
| 1076 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1077 | if (FirstTokenKind == AsmToken::String) { |
| 1078 | if (Lexer.is(AsmToken::At)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1079 | Lex(); // eat @ |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1080 | SMLoc AtLoc = getLexer().getLoc(); |
| 1081 | StringRef VName; |
| 1082 | if (parseIdentifier(VName)) |
| 1083 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 1084 | |
| 1085 | Split = std::make_pair(Identifier, VName); |
| 1086 | } |
| 1087 | } else { |
| 1088 | Split = Identifier.split('@'); |
| 1089 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1090 | } else if (Lexer.is(AsmToken::LParen)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1091 | Lex(); // eat '('. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1092 | StringRef VName; |
| 1093 | parseIdentifier(VName); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1094 | // eat ')'. |
| 1095 | if (parseToken(AsmToken::RParen, |
| 1096 | "unexpected token in variant, expected ')'")) |
| 1097 | return true; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1098 | Split = std::make_pair(Identifier, VName); |
| 1099 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1100 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1101 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 1102 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1103 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1104 | StringRef SymbolName = Identifier; |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1105 | if (SymbolName.empty()) |
| 1106 | return true; |
| 1107 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1108 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 1109 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1110 | // Lookup the symbol variant if used. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1111 | if (!Split.second.empty()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1112 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1113 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 1114 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1115 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1116 | Variant = MCSymbolRefExpr::VK_None; |
| 1117 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 1118 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 1119 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1122 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1123 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1124 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1125 | // If this is an absolute variable reference, substitute it now to preserve |
| 1126 | // semantics in the face of reassignment. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 1127 | if (Sym->isVariable() && |
| 1128 | isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1129 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 1130 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1131 | |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 1132 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1133 | return false; |
| 1134 | } |
| 1135 | |
| 1136 | // Otherwise create a symbol ref. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1137 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1138 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1139 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 1140 | case AsmToken::BigNum: |
| 1141 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1142 | case AsmToken::Integer: { |
| 1143 | SMLoc Loc = getTok().getLoc(); |
| 1144 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1145 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1146 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1147 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1148 | // Look for 'b' or 'f' following an Integer as a directional label |
| 1149 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 1150 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1151 | // Lookup the symbol variant if used. |
| 1152 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 1153 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 1154 | if (Split.first.size() != IDVal.size()) { |
| 1155 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1156 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1157 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1158 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1159 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1160 | if (IDVal == "f" || IDVal == "b") { |
| 1161 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1162 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1163 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 1164 | if (IDVal == "b" && Sym->isUndefined()) |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 1165 | return Error(Loc, "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 1166 | DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1167 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1168 | Lex(); // Eat identifier. |
| 1169 | } |
| 1170 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1171 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1172 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1173 | case AsmToken::Real: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1174 | APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 1175 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1176 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1177 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1178 | Lex(); // Eat token. |
| 1179 | return false; |
| 1180 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1181 | case AsmToken::Dot: { |
| 1182 | // This is a '.' reference, which references the current PC. Emit a |
| 1183 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1184 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1185 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1186 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1187 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1188 | Lex(); // Eat identifier. |
| 1189 | return false; |
| 1190 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1191 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1192 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1193 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1194 | case AsmToken::LBrac: |
| 1195 | if (!PlatformParser->HasBracketExpressions()) |
| 1196 | return TokError("brackets expression not supported on this target"); |
| 1197 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1198 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1199 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1200 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1201 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1202 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1203 | Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1204 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1205 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1206 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1207 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1208 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1209 | Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1210 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1211 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1212 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1213 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1214 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1215 | Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1216 | return false; |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 1217 | // MIPS unary expression operators. The lexer won't generate these tokens if |
| 1218 | // MCAsmInfo::HasMipsExpressions is false for the target. |
| 1219 | case AsmToken::PercentCall16: |
| 1220 | case AsmToken::PercentCall_Hi: |
| 1221 | case AsmToken::PercentCall_Lo: |
| 1222 | case AsmToken::PercentDtprel_Hi: |
| 1223 | case AsmToken::PercentDtprel_Lo: |
| 1224 | case AsmToken::PercentGot: |
| 1225 | case AsmToken::PercentGot_Disp: |
| 1226 | case AsmToken::PercentGot_Hi: |
| 1227 | case AsmToken::PercentGot_Lo: |
| 1228 | case AsmToken::PercentGot_Ofst: |
| 1229 | case AsmToken::PercentGot_Page: |
| 1230 | case AsmToken::PercentGottprel: |
| 1231 | case AsmToken::PercentGp_Rel: |
| 1232 | case AsmToken::PercentHi: |
| 1233 | case AsmToken::PercentHigher: |
| 1234 | case AsmToken::PercentHighest: |
| 1235 | case AsmToken::PercentLo: |
| 1236 | case AsmToken::PercentNeg: |
| 1237 | case AsmToken::PercentPcrel_Hi: |
| 1238 | case AsmToken::PercentPcrel_Lo: |
| 1239 | case AsmToken::PercentTlsgd: |
| 1240 | case AsmToken::PercentTlsldm: |
| 1241 | case AsmToken::PercentTprel_Hi: |
| 1242 | case AsmToken::PercentTprel_Lo: |
| 1243 | Lex(); // Eat the operator. |
| 1244 | if (Lexer.isNot(AsmToken::LParen)) |
| 1245 | return TokError("expected '(' after operator"); |
| 1246 | Lex(); // Eat the operator. |
| 1247 | if (parseExpression(Res, EndLoc)) |
| 1248 | return true; |
| 1249 | if (Lexer.isNot(AsmToken::RParen)) |
| 1250 | return TokError("expected ')'"); |
| 1251 | Lex(); // Eat the operator. |
| 1252 | Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); |
| 1253 | return !Res; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1254 | } |
| 1255 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1256 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1257 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 1258 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1259 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1262 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1263 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1264 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 1265 | // Ask the target implementation about this expression first. |
| 1266 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 1267 | if (NewE) |
| 1268 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1269 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 1270 | // if there is exactly one symbol. |
| 1271 | switch (E->getKind()) { |
| 1272 | case MCExpr::Target: |
| 1273 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1274 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1275 | |
| 1276 | case MCExpr::SymbolRef: { |
| 1277 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 1278 | |
| 1279 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1280 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 1281 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1282 | return E; |
| 1283 | } |
| 1284 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1285 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | case MCExpr::Unary: { |
| 1289 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1290 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1291 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1292 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1293 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | case MCExpr::Binary: { |
| 1297 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1298 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1299 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1302 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1303 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1304 | if (!LHS) |
| 1305 | LHS = BE->getLHS(); |
| 1306 | if (!RHS) |
| 1307 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1308 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1309 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1312 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1313 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1316 | /// This function checks if the next token is <string> type or arithmetic. |
| 1317 | /// string that begin with character '<' must end with character '>'. |
| 1318 | /// otherwise it is arithmetics. |
| 1319 | /// If the function returns a 'true' value, |
| 1320 | /// the End argument will be filled with the last location pointed to the '>' |
| 1321 | /// character. |
| 1322 | |
| 1323 | /// There is a gap between the AltMacro's documentation and the single quote implementation. |
| 1324 | /// GCC does not fully support this feature and so we will not support it. |
| 1325 | /// TODO: Adding single quote as a string. |
| 1326 | bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { |
| 1327 | assert((StrLoc.getPointer() != NULL) && |
| 1328 | "Argument to the function cannot be a NULL value"); |
| 1329 | const char *CharPtr = StrLoc.getPointer(); |
| 1330 | while ((*CharPtr != '>') && (*CharPtr != '\n') && |
| 1331 | (*CharPtr != '\r') && (*CharPtr != '\0')){ |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1332 | if(*CharPtr == '!') |
| 1333 | CharPtr++; |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1334 | CharPtr++; |
| 1335 | } |
| 1336 | if (*CharPtr == '>') { |
| 1337 | EndLoc = StrLoc.getFromPointer(CharPtr + 1); |
| 1338 | return true; |
| 1339 | } |
| 1340 | return false; |
| 1341 | } |
| 1342 | |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1343 | /// \brief creating a string without the escape characters '!'. |
| 1344 | void AsmParser::altMacroString(StringRef AltMacroStr,std::string &Res) { |
Michael Zuckerman | ff29879 | 2017-05-10 14:00:57 +0000 | [diff] [blame] | 1345 | for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1346 | if (AltMacroStr[Pos] == '!') |
| 1347 | Pos++; |
| 1348 | Res += AltMacroStr[Pos]; |
| 1349 | } |
| 1350 | } |
| 1351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1352 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1353 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1354 | /// expr ::= expr &&,|| expr -> lowest. |
| 1355 | /// expr ::= expr |,^,&,! expr |
| 1356 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1357 | /// expr ::= expr <<,>> expr |
| 1358 | /// expr ::= expr +,- expr |
| 1359 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1360 | /// expr ::= primaryexpr |
| 1361 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1362 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1363 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1364 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1365 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1366 | return true; |
| 1367 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1368 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1369 | // expression to include the modifier. This is inefficient, but in general we |
| 1370 | // expect users to use 'a@modifier op b'. |
| 1371 | if (Lexer.getKind() == AsmToken::At) { |
| 1372 | Lex(); |
| 1373 | |
| 1374 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1375 | return TokError("unexpected symbol modifier following '@'"); |
| 1376 | |
| 1377 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1378 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1379 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1380 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1381 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1382 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1383 | if (!ModifiedRes) { |
| 1384 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1385 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1386 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1387 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1388 | Res = ModifiedRes; |
| 1389 | Lex(); |
| 1390 | } |
| 1391 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1392 | // Try to constant fold it up front, if possible. |
| 1393 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1394 | if (Res->evaluateAsAbsolute(Value)) |
| 1395 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1396 | |
| 1397 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1398 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1399 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1400 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1401 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1402 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1405 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1406 | SMLoc &EndLoc) { |
| 1407 | if (parseParenExpr(Res, EndLoc)) |
| 1408 | return true; |
| 1409 | |
| 1410 | for (; ParenDepth > 0; --ParenDepth) { |
| 1411 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1412 | return true; |
| 1413 | |
| 1414 | // We don't Lex() the last RParen. |
| 1415 | // This is the same behavior as parseParenExpression(). |
| 1416 | if (ParenDepth - 1 > 0) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1417 | EndLoc = getTok().getEndLoc(); |
| 1418 | if (parseToken(AsmToken::RParen, |
| 1419 | "expected ')' in parentheses expression")) |
| 1420 | return true; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1426 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1427 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1428 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1429 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1430 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1431 | return true; |
| 1432 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1433 | if (!Expr->evaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1434 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1435 | |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1439 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1440 | MCBinaryExpr::Opcode &Kind, |
| 1441 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1442 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1443 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1444 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1445 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1446 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1447 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1448 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1449 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1450 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1451 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1452 | return 1; |
| 1453 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1454 | // Low Precedence: |, &, ^ |
| 1455 | // |
| 1456 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1457 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1458 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1459 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1460 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1461 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1462 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1463 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1464 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1465 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1466 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1467 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1468 | case AsmToken::EqualEqual: |
| 1469 | Kind = MCBinaryExpr::EQ; |
| 1470 | return 3; |
| 1471 | case AsmToken::ExclaimEqual: |
| 1472 | case AsmToken::LessGreater: |
| 1473 | Kind = MCBinaryExpr::NE; |
| 1474 | return 3; |
| 1475 | case AsmToken::Less: |
| 1476 | Kind = MCBinaryExpr::LT; |
| 1477 | return 3; |
| 1478 | case AsmToken::LessEqual: |
| 1479 | Kind = MCBinaryExpr::LTE; |
| 1480 | return 3; |
| 1481 | case AsmToken::Greater: |
| 1482 | Kind = MCBinaryExpr::GT; |
| 1483 | return 3; |
| 1484 | case AsmToken::GreaterEqual: |
| 1485 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1486 | return 3; |
| 1487 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1488 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1489 | case AsmToken::LessLess: |
| 1490 | Kind = MCBinaryExpr::Shl; |
| 1491 | return 4; |
| 1492 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1493 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1494 | return 4; |
| 1495 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1496 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1497 | case AsmToken::Plus: |
| 1498 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1499 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1500 | case AsmToken::Minus: |
| 1501 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1502 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1503 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1504 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1505 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1506 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1507 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1508 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1509 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1510 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1511 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1512 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1513 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1517 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1518 | MCBinaryExpr::Opcode &Kind, |
| 1519 | bool ShouldUseLogicalShr) { |
| 1520 | switch (K) { |
| 1521 | default: |
| 1522 | return 0; // not a binop. |
| 1523 | |
| 1524 | // Lowest Precedence: &&, || |
| 1525 | case AsmToken::AmpAmp: |
| 1526 | Kind = MCBinaryExpr::LAnd; |
| 1527 | return 2; |
| 1528 | case AsmToken::PipePipe: |
| 1529 | Kind = MCBinaryExpr::LOr; |
| 1530 | return 1; |
| 1531 | |
| 1532 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1533 | case AsmToken::EqualEqual: |
| 1534 | Kind = MCBinaryExpr::EQ; |
| 1535 | return 3; |
| 1536 | case AsmToken::ExclaimEqual: |
| 1537 | case AsmToken::LessGreater: |
| 1538 | Kind = MCBinaryExpr::NE; |
| 1539 | return 3; |
| 1540 | case AsmToken::Less: |
| 1541 | Kind = MCBinaryExpr::LT; |
| 1542 | return 3; |
| 1543 | case AsmToken::LessEqual: |
| 1544 | Kind = MCBinaryExpr::LTE; |
| 1545 | return 3; |
| 1546 | case AsmToken::Greater: |
| 1547 | Kind = MCBinaryExpr::GT; |
| 1548 | return 3; |
| 1549 | case AsmToken::GreaterEqual: |
| 1550 | Kind = MCBinaryExpr::GTE; |
| 1551 | return 3; |
| 1552 | |
| 1553 | // Low Intermediate Precedence: +, - |
| 1554 | case AsmToken::Plus: |
| 1555 | Kind = MCBinaryExpr::Add; |
| 1556 | return 4; |
| 1557 | case AsmToken::Minus: |
| 1558 | Kind = MCBinaryExpr::Sub; |
| 1559 | return 4; |
| 1560 | |
| 1561 | // High Intermediate Precedence: |, &, ^ |
| 1562 | // |
| 1563 | // FIXME: gas seems to support '!' as an infix operator? |
| 1564 | case AsmToken::Pipe: |
| 1565 | Kind = MCBinaryExpr::Or; |
| 1566 | return 5; |
| 1567 | case AsmToken::Caret: |
| 1568 | Kind = MCBinaryExpr::Xor; |
| 1569 | return 5; |
| 1570 | case AsmToken::Amp: |
| 1571 | Kind = MCBinaryExpr::And; |
| 1572 | return 5; |
| 1573 | |
| 1574 | // Highest Precedence: *, /, %, <<, >> |
| 1575 | case AsmToken::Star: |
| 1576 | Kind = MCBinaryExpr::Mul; |
| 1577 | return 6; |
| 1578 | case AsmToken::Slash: |
| 1579 | Kind = MCBinaryExpr::Div; |
| 1580 | return 6; |
| 1581 | case AsmToken::Percent: |
| 1582 | Kind = MCBinaryExpr::Mod; |
| 1583 | return 6; |
| 1584 | case AsmToken::LessLess: |
| 1585 | Kind = MCBinaryExpr::Shl; |
| 1586 | return 6; |
| 1587 | case AsmToken::GreaterGreater: |
| 1588 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1589 | return 6; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1594 | MCBinaryExpr::Opcode &Kind) { |
| 1595 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1596 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1597 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1598 | } |
| 1599 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1600 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1601 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1602 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1603 | SMLoc &EndLoc) { |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1604 | SMLoc StartLoc = Lexer.getLoc(); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 1605 | while (true) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1606 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1607 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1608 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1609 | // If the next token is lower precedence than we are allowed to eat, return |
| 1610 | // successfully with what we ate already. |
| 1611 | if (TokPrec < Precedence) |
| 1612 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1613 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1614 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1616 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1617 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1618 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1619 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1620 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1621 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1622 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1623 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1624 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1625 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1626 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1628 | // Merge LHS and RHS according to operator. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1629 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1633 | /// ParseStatement: |
| 1634 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1635 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1636 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1637 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1638 | MCAsmParserSemaCallback *SI) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1639 | assert(!hasPendingError() && "parseStatement started with pending error"); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1640 | // Eat initial spaces and comments |
| 1641 | while (Lexer.is(AsmToken::Space)) |
| 1642 | Lex(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1643 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1644 | // if this is a line comment we can drop it safely |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 1645 | if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1646 | getTok().getString().front() == '\n') |
| 1647 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1648 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1649 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1650 | } |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1651 | // Statements always start with an identifier. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1652 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1653 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1654 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1655 | int64_t LocalLabelVal = -1; |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1656 | if (Lexer.is(AsmToken::HashDirective)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1657 | return parseCppHashLineFilenameComment(IDLoc); |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1658 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1659 | if (Lexer.is(AsmToken::Integer)) { |
| 1660 | LocalLabelVal = getTok().getIntVal(); |
| 1661 | if (LocalLabelVal < 0) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1662 | if (!TheCondState.Ignore) { |
| 1663 | Lex(); // always eat a token |
| 1664 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1665 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1666 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1667 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1668 | IDVal = getTok().getString(); |
| 1669 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1670 | if (Lexer.getKind() != AsmToken::Colon) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1671 | if (!TheCondState.Ignore) { |
| 1672 | Lex(); // always eat a token |
| 1673 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1674 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1675 | } |
| 1676 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1677 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1678 | // Treat '.' as a valid identifier in this context. |
| 1679 | Lex(); |
| 1680 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1681 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1682 | // Treat '{' as a valid identifier in this context. |
| 1683 | Lex(); |
| 1684 | IDVal = "{"; |
| 1685 | |
| 1686 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1687 | // Treat '}' as a valid identifier in this context. |
| 1688 | Lex(); |
| 1689 | IDVal = "}"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1690 | } else if (parseIdentifier(IDVal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1691 | if (!TheCondState.Ignore) { |
| 1692 | Lex(); // always eat a token |
| 1693 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1694 | } |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1695 | IDVal = ""; |
| 1696 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1698 | // Handle conditional assembly here before checking for skipping. We |
| 1699 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1700 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1701 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1702 | DirectiveKindMap.find(IDVal); |
| 1703 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1704 | ? DK_NO_DIRECTIVE |
| 1705 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1706 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1707 | default: |
| 1708 | break; |
| 1709 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1710 | case DK_IFEQ: |
| 1711 | case DK_IFGE: |
| 1712 | case DK_IFGT: |
| 1713 | case DK_IFLE: |
| 1714 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1715 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1716 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1717 | case DK_IFB: |
| 1718 | return parseDirectiveIfb(IDLoc, true); |
| 1719 | case DK_IFNB: |
| 1720 | return parseDirectiveIfb(IDLoc, false); |
| 1721 | case DK_IFC: |
| 1722 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1723 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1724 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1725 | case DK_IFNC: |
| 1726 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1727 | case DK_IFNES: |
| 1728 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1729 | case DK_IFDEF: |
| 1730 | return parseDirectiveIfdef(IDLoc, true); |
| 1731 | case DK_IFNDEF: |
| 1732 | case DK_IFNOTDEF: |
| 1733 | return parseDirectiveIfdef(IDLoc, false); |
| 1734 | case DK_ELSEIF: |
| 1735 | return parseDirectiveElseIf(IDLoc); |
| 1736 | case DK_ELSE: |
| 1737 | return parseDirectiveElse(IDLoc); |
| 1738 | case DK_ENDIF: |
| 1739 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1740 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1741 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1742 | // Ignore the statement if in the middle of inactive conditional |
| 1743 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1744 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1745 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1748 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1749 | // FIXME: Recurse on local labels? |
| 1750 | |
| 1751 | // See what kind of statement we have. |
| 1752 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1753 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1754 | if (!getTargetParser().isLabel(ID)) |
| 1755 | break; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1756 | if (checkForValidSection()) |
| 1757 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1759 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1760 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1761 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1762 | // Diagnose attempt to use '.' as a label. |
| 1763 | if (IDVal == ".") |
| 1764 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1765 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1766 | // Diagnose attempt to use a variable as a label. |
| 1767 | // |
| 1768 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1769 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1770 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1771 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1772 | if (LocalLabelVal == -1) { |
| 1773 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1774 | StringRef RewrittenLabel = |
| 1775 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1776 | assert(!RewrittenLabel.empty() && |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1777 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1778 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1779 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1780 | IDVal = RewrittenLabel; |
| 1781 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1782 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1783 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1784 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
Nirav Dave | 9263ae3 | 2016-08-02 19:17:54 +0000 | [diff] [blame] | 1785 | // End of Labels should be treated as end of line for lexing |
| 1786 | // purposes but that information is not available to the Lexer who |
| 1787 | // does not understand Labels. This may cause us to see a Hash |
| 1788 | // here instead of a preprocessor line comment. |
| 1789 | if (getTok().is(AsmToken::Hash)) { |
| 1790 | StringRef CommentStr = parseStringToEndOfStatement(); |
| 1791 | Lexer.Lex(); |
| 1792 | Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); |
| 1793 | } |
| 1794 | |
Nirav Dave | 8ea792d | 2016-07-13 14:03:12 +0000 | [diff] [blame] | 1795 | // Consume any end of statement token, if present, to avoid spurious |
| 1796 | // AddBlankLine calls(). |
| 1797 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 1798 | Lex(); |
| 1799 | } |
| 1800 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1801 | // Emit the label. |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 1802 | if (!getTargetParser().isParsingInlineAsm()) |
Rafael Espindola | be99157 | 2017-02-10 15:13:12 +0000 | [diff] [blame] | 1803 | Out.EmitLabel(Sym, IDLoc); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1804 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1805 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1806 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1807 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1808 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1809 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1810 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1811 | getTargetParser().onLabelParsed(Sym); |
| 1812 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1813 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1814 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1815 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1816 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1817 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1818 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1819 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1820 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1821 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1822 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1823 | |
| 1824 | default: // Normal instruction or directive. |
| 1825 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1826 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1827 | |
| 1828 | // 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] | 1829 | if (areMacrosEnabled()) |
| 1830 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1831 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1832 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1833 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1834 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1835 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1836 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1837 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1838 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1839 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1840 | // 1. The target-specific assembly parser. Some directives are target |
| 1841 | // specific or may potentially behave differently on certain targets. |
| 1842 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1843 | // (like the ELF parser) register themselves as extensions. |
| 1844 | // 3. The generic directive parser implemented by this class. These are |
| 1845 | // all the directives that behave in a target and platform independent |
| 1846 | // manner, or at least have a default behavior that's shared between |
| 1847 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1848 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 1849 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 1850 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1851 | SMLoc StartTokLoc = getTok().getLoc(); |
| 1852 | bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); |
| 1853 | |
| 1854 | if (hasPendingError()) |
| 1855 | return true; |
| 1856 | // Currently the return value should be true if we are |
| 1857 | // uninterested but as this is at odds with the standard parsing |
| 1858 | // convention (return true = error) we have instances of a parsed |
| 1859 | // directive that fails returning true as an error. Catch these |
| 1860 | // cases as best as possible errors here. |
| 1861 | if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) |
| 1862 | return true; |
| 1863 | // Return if we did some parsing or believe we succeeded. |
| 1864 | if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1865 | return false; |
| 1866 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1867 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1868 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1869 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1870 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1871 | if (Handler.first) |
| 1872 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1873 | |
| 1874 | // Finally, if no one else is interested in this directive, it must be |
| 1875 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1876 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1877 | default: |
| 1878 | break; |
| 1879 | case DK_SET: |
| 1880 | case DK_EQU: |
| 1881 | return parseDirectiveSet(IDVal, true); |
| 1882 | case DK_EQUIV: |
| 1883 | return parseDirectiveSet(IDVal, false); |
| 1884 | case DK_ASCII: |
| 1885 | return parseDirectiveAscii(IDVal, false); |
| 1886 | case DK_ASCIZ: |
| 1887 | case DK_STRING: |
| 1888 | return parseDirectiveAscii(IDVal, true); |
| 1889 | case DK_BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1890 | case DK_DC_B: |
| 1891 | return parseDirectiveValue(IDVal, 1); |
| 1892 | case DK_DC: |
| 1893 | case DK_DC_W: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1894 | case DK_SHORT: |
| 1895 | case DK_VALUE: |
| 1896 | case DK_2BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1897 | return parseDirectiveValue(IDVal, 2); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1898 | case DK_LONG: |
| 1899 | case DK_INT: |
| 1900 | case DK_4BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1901 | case DK_DC_L: |
| 1902 | return parseDirectiveValue(IDVal, 4); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1903 | case DK_QUAD: |
| 1904 | case DK_8BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1905 | return parseDirectiveValue(IDVal, 8); |
| 1906 | case DK_DC_A: |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1907 | return parseDirectiveValue( |
| 1908 | IDVal, getContext().getAsmInfo()->getCodePointerSize()); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1909 | case DK_OCTA: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1910 | return parseDirectiveOctaValue(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1911 | case DK_SINGLE: |
| 1912 | case DK_FLOAT: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1913 | case DK_DC_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1914 | return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1915 | case DK_DOUBLE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1916 | case DK_DC_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1917 | return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1918 | case DK_ALIGN: { |
| 1919 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1920 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1921 | } |
| 1922 | case DK_ALIGN32: { |
| 1923 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1924 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1925 | } |
| 1926 | case DK_BALIGN: |
| 1927 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1928 | case DK_BALIGNW: |
| 1929 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1930 | case DK_BALIGNL: |
| 1931 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1932 | case DK_P2ALIGN: |
| 1933 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1934 | case DK_P2ALIGNW: |
| 1935 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1936 | case DK_P2ALIGNL: |
| 1937 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1938 | case DK_ORG: |
| 1939 | return parseDirectiveOrg(); |
| 1940 | case DK_FILL: |
| 1941 | return parseDirectiveFill(); |
| 1942 | case DK_ZERO: |
| 1943 | return parseDirectiveZero(); |
| 1944 | case DK_EXTERN: |
| 1945 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1946 | return false; |
| 1947 | case DK_GLOBL: |
| 1948 | case DK_GLOBAL: |
| 1949 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1950 | case DK_LAZY_REFERENCE: |
| 1951 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1952 | case DK_NO_DEAD_STRIP: |
| 1953 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1954 | case DK_SYMBOL_RESOLVER: |
| 1955 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1956 | case DK_PRIVATE_EXTERN: |
| 1957 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1958 | case DK_REFERENCE: |
| 1959 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1960 | case DK_WEAK_DEFINITION: |
| 1961 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1962 | case DK_WEAK_REFERENCE: |
| 1963 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1964 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1965 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1966 | case DK_COMM: |
| 1967 | case DK_COMMON: |
| 1968 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1969 | case DK_LCOMM: |
| 1970 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1971 | case DK_ABORT: |
| 1972 | return parseDirectiveAbort(); |
| 1973 | case DK_INCLUDE: |
| 1974 | return parseDirectiveInclude(); |
| 1975 | case DK_INCBIN: |
| 1976 | return parseDirectiveIncbin(); |
| 1977 | case DK_CODE16: |
| 1978 | case DK_CODE16GCC: |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1979 | return TokError(Twine(IDVal) + |
| 1980 | " not currently supported for this target"); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1981 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1982 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1983 | case DK_IRP: |
| 1984 | return parseDirectiveIrp(IDLoc); |
| 1985 | case DK_IRPC: |
| 1986 | return parseDirectiveIrpc(IDLoc); |
| 1987 | case DK_ENDR: |
| 1988 | return parseDirectiveEndr(IDLoc); |
| 1989 | case DK_BUNDLE_ALIGN_MODE: |
| 1990 | return parseDirectiveBundleAlignMode(); |
| 1991 | case DK_BUNDLE_LOCK: |
| 1992 | return parseDirectiveBundleLock(); |
| 1993 | case DK_BUNDLE_UNLOCK: |
| 1994 | return parseDirectiveBundleUnlock(); |
| 1995 | case DK_SLEB128: |
| 1996 | return parseDirectiveLEB128(true); |
| 1997 | case DK_ULEB128: |
| 1998 | return parseDirectiveLEB128(false); |
| 1999 | case DK_SPACE: |
| 2000 | case DK_SKIP: |
| 2001 | return parseDirectiveSpace(IDVal); |
| 2002 | case DK_FILE: |
| 2003 | return parseDirectiveFile(IDLoc); |
| 2004 | case DK_LINE: |
| 2005 | return parseDirectiveLine(); |
| 2006 | case DK_LOC: |
| 2007 | return parseDirectiveLoc(); |
| 2008 | case DK_STABS: |
| 2009 | return parseDirectiveStabs(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2010 | case DK_CV_FILE: |
| 2011 | return parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 2012 | case DK_CV_FUNC_ID: |
| 2013 | return parseDirectiveCVFuncId(); |
| 2014 | case DK_CV_INLINE_SITE_ID: |
| 2015 | return parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2016 | case DK_CV_LOC: |
| 2017 | return parseDirectiveCVLoc(); |
| 2018 | case DK_CV_LINETABLE: |
| 2019 | return parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 2020 | case DK_CV_INLINE_LINETABLE: |
| 2021 | return parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 2022 | case DK_CV_DEF_RANGE: |
| 2023 | return parseDirectiveCVDefRange(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2024 | case DK_CV_STRINGTABLE: |
| 2025 | return parseDirectiveCVStringTable(); |
| 2026 | case DK_CV_FILECHECKSUMS: |
| 2027 | return parseDirectiveCVFileChecksums(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2028 | case DK_CFI_SECTIONS: |
| 2029 | return parseDirectiveCFISections(); |
| 2030 | case DK_CFI_STARTPROC: |
| 2031 | return parseDirectiveCFIStartProc(); |
| 2032 | case DK_CFI_ENDPROC: |
| 2033 | return parseDirectiveCFIEndProc(); |
| 2034 | case DK_CFI_DEF_CFA: |
| 2035 | return parseDirectiveCFIDefCfa(IDLoc); |
| 2036 | case DK_CFI_DEF_CFA_OFFSET: |
| 2037 | return parseDirectiveCFIDefCfaOffset(); |
| 2038 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 2039 | return parseDirectiveCFIAdjustCfaOffset(); |
| 2040 | case DK_CFI_DEF_CFA_REGISTER: |
| 2041 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 2042 | case DK_CFI_OFFSET: |
| 2043 | return parseDirectiveCFIOffset(IDLoc); |
| 2044 | case DK_CFI_REL_OFFSET: |
| 2045 | return parseDirectiveCFIRelOffset(IDLoc); |
| 2046 | case DK_CFI_PERSONALITY: |
| 2047 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 2048 | case DK_CFI_LSDA: |
| 2049 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 2050 | case DK_CFI_REMEMBER_STATE: |
| 2051 | return parseDirectiveCFIRememberState(); |
| 2052 | case DK_CFI_RESTORE_STATE: |
| 2053 | return parseDirectiveCFIRestoreState(); |
| 2054 | case DK_CFI_SAME_VALUE: |
| 2055 | return parseDirectiveCFISameValue(IDLoc); |
| 2056 | case DK_CFI_RESTORE: |
| 2057 | return parseDirectiveCFIRestore(IDLoc); |
| 2058 | case DK_CFI_ESCAPE: |
| 2059 | return parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 2060 | case DK_CFI_RETURN_COLUMN: |
| 2061 | return parseDirectiveCFIReturnColumn(IDLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2062 | case DK_CFI_SIGNAL_FRAME: |
| 2063 | return parseDirectiveCFISignalFrame(); |
| 2064 | case DK_CFI_UNDEFINED: |
| 2065 | return parseDirectiveCFIUndefined(IDLoc); |
| 2066 | case DK_CFI_REGISTER: |
| 2067 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 2068 | case DK_CFI_WINDOW_SAVE: |
| 2069 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2070 | case DK_MACROS_ON: |
| 2071 | case DK_MACROS_OFF: |
| 2072 | return parseDirectiveMacrosOnOff(IDVal); |
| 2073 | case DK_MACRO: |
| 2074 | return parseDirectiveMacro(IDLoc); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2075 | case DK_ALTMACRO: |
| 2076 | case DK_NOALTMACRO: |
| 2077 | return parseDirectiveAltmacro(IDVal); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2078 | case DK_EXITM: |
| 2079 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2080 | case DK_ENDM: |
| 2081 | case DK_ENDMACRO: |
| 2082 | return parseDirectiveEndMacro(IDVal); |
| 2083 | case DK_PURGEM: |
| 2084 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 2085 | case DK_END: |
| 2086 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 2087 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 2088 | return parseDirectiveError(IDLoc, false); |
| 2089 | case DK_ERROR: |
| 2090 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 2091 | case DK_WARNING: |
| 2092 | return parseDirectiveWarning(IDLoc); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2093 | case DK_RELOC: |
| 2094 | return parseDirectiveReloc(IDLoc); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2095 | case DK_DCB: |
| 2096 | case DK_DCB_W: |
| 2097 | return parseDirectiveDCB(IDVal, 2); |
| 2098 | case DK_DCB_B: |
| 2099 | return parseDirectiveDCB(IDVal, 1); |
| 2100 | case DK_DCB_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2101 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2102 | case DK_DCB_L: |
| 2103 | return parseDirectiveDCB(IDVal, 4); |
| 2104 | case DK_DCB_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2105 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2106 | case DK_DC_X: |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2107 | case DK_DCB_X: |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2108 | return TokError(Twine(IDVal) + |
| 2109 | " not currently supported for this target"); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 2110 | case DK_DS: |
| 2111 | case DK_DS_W: |
| 2112 | return parseDirectiveDS(IDVal, 2); |
| 2113 | case DK_DS_B: |
| 2114 | return parseDirectiveDS(IDVal, 1); |
| 2115 | case DK_DS_D: |
| 2116 | return parseDirectiveDS(IDVal, 8); |
| 2117 | case DK_DS_L: |
| 2118 | case DK_DS_S: |
| 2119 | return parseDirectiveDS(IDVal, 4); |
| 2120 | case DK_DS_P: |
| 2121 | case DK_DS_X: |
| 2122 | return parseDirectiveDS(IDVal, 12); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 2123 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2124 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 2125 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2126 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 2127 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2128 | // __asm _emit or __asm __emit |
| 2129 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 2130 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2131 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2132 | |
| 2133 | // __asm align |
| 2134 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2135 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2136 | |
Coby Tayree | 3847be9 | 2017-04-04 17:57:23 +0000 | [diff] [blame] | 2137 | if (ParsingInlineAsm && (IDVal == "even" || IDVal == "EVEN")) |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 2138 | Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2139 | if (checkForValidSection()) |
| 2140 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2141 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 2142 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2143 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 2144 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2145 | bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
| 2146 | Info.ParsedOperands); |
| 2147 | Info.ParseError = ParseHadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2148 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2149 | // Dump the parsed representation, if requested. |
| 2150 | if (getShowParsedOperands()) { |
| 2151 | SmallString<256> Str; |
| 2152 | raw_svector_ostream OS(Str); |
| 2153 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2154 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2155 | if (i != 0) |
| 2156 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2157 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2158 | } |
| 2159 | OS << "]"; |
| 2160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2161 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2164 | // Fail even if ParseInstruction erroneously returns false. |
| 2165 | if (hasPendingError() || ParseHadError) |
| 2166 | return true; |
| 2167 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2168 | // If we are generating dwarf for the current section then generate a .loc |
| 2169 | // directive for the instruction. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2170 | if (!ParseHadError && getContext().getGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2171 | getContext().getGenDwarfSectionSyms().count( |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 2172 | getStreamer().getCurrentSectionOnly())) { |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 2173 | unsigned Line; |
| 2174 | if (ActiveMacros.empty()) |
| 2175 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 2176 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 2177 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 2178 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2179 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2180 | // If we previously parsed a cpp hash file line comment then make sure the |
| 2181 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 2182 | // Dwarf File table for it and adjust the line number for the .loc. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 2183 | if (!CppHashInfo.Filename.empty()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2184 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2185 | 0, StringRef(), CppHashInfo.Filename); |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2186 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2187 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2188 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 2189 | // cache with the different Loc from the call above we save the last |
| 2190 | // info we queried here with SrcMgr.FindLineNumber(). |
| 2191 | unsigned CppHashLocLineNo; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2192 | if (LastQueryIDLoc == CppHashInfo.Loc && |
| 2193 | LastQueryBuffer == CppHashInfo.Buf) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2194 | CppHashLocLineNo = LastQueryLine; |
| 2195 | else { |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2196 | CppHashLocLineNo = |
| 2197 | SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2198 | LastQueryLine = CppHashLocLineNo; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2199 | LastQueryIDLoc = CppHashInfo.Loc; |
| 2200 | LastQueryBuffer = CppHashInfo.Buf; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2201 | } |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2202 | Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2203 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2204 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2205 | getStreamer().EmitDwarfLocDirective( |
| 2206 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 2207 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 2208 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 2211 | // If parsing succeeded, match the instruction. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2212 | if (!ParseHadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 2213 | uint64_t ErrorInfo; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 2214 | if (getTargetParser().MatchAndEmitInstruction( |
| 2215 | IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| 2216 | getTargetParser().isParsingInlineAsm())) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2217 | return true; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 2218 | } |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 2219 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 2220 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 2221 | |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2222 | // Parse and erase curly braces marking block start/end |
| 2223 | bool |
| 2224 | AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { |
| 2225 | // Identify curly brace marking block start/end |
| 2226 | if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) |
| 2227 | return false; |
| 2228 | |
| 2229 | SMLoc StartLoc = Lexer.getLoc(); |
| 2230 | Lex(); // Eat the brace |
| 2231 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2232 | Lex(); // Eat EndOfStatement following the brace |
| 2233 | |
| 2234 | // Erase the block start/end brace from the output asm string |
| 2235 | AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - |
| 2236 | StartLoc.getPointer()); |
| 2237 | return true; |
| 2238 | } |
| 2239 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2240 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2241 | /// ::= # number "filename" |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2242 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2243 | Lex(); // Eat the hash token. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2244 | // Lexer only ever emits HashDirective if it fully formed if it's |
| 2245 | // done the checking already so this is an internal error. |
| 2246 | assert(getTok().is(AsmToken::Integer) && |
| 2247 | "Lexing Cpp line comment: Expected Integer"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2248 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2249 | Lex(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2250 | assert(getTok().is(AsmToken::String) && |
| 2251 | "Lexing Cpp line comment: Expected String"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2252 | StringRef Filename = getTok().getString(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2253 | Lex(); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2254 | |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2255 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2256 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2257 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2258 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2259 | CppHashInfo.Loc = L; |
| 2260 | CppHashInfo.Filename = Filename; |
| 2261 | CppHashInfo.LineNumber = LineNumber; |
| 2262 | CppHashInfo.Buf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2263 | return false; |
| 2264 | } |
| 2265 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2266 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2267 | /// for the Filename and LineNo if any in the diagnostic. |
| 2268 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2269 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2270 | raw_ostream &OS = errs(); |
| 2271 | |
| 2272 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2273 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2274 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2275 | unsigned CppHashBuf = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2276 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2277 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2278 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2279 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2280 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2281 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 2282 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2283 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 2284 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2287 | // 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] | 2288 | // manager changed or buffer changed (like in a nested include) then just |
| 2289 | // print the normal diagnostic using its Filename and LineNo. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2290 | if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2291 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2292 | if (Parser->SavedDiagHandler) |
| 2293 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 2294 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2295 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2296 | return; |
| 2297 | } |
| 2298 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2299 | // Use the CppHashFilename and calculate a line number based on the |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2300 | // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc |
| 2301 | // for the diagnostic. |
| 2302 | const std::string &Filename = Parser->CppHashInfo.Filename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2303 | |
| 2304 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 2305 | int CppHashLocLineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2306 | Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2307 | int LineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2308 | Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2309 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2310 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 2311 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 2312 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2313 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2314 | if (Parser->SavedDiagHandler) |
| 2315 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 2316 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2317 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2320 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 2321 | // difference being that that function accepts '@' as part of identifiers and |
| 2322 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 2323 | // '@' as a special case when needed. |
| 2324 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2325 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 2326 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2329 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 2330 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2331 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2332 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2333 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2334 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2335 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2336 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2337 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2338 | // A macro without parameters is handled differently on Darwin: |
| 2339 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2340 | while (!Body.empty()) { |
| 2341 | // Scan for the next substitution. |
| 2342 | std::size_t End = Body.size(), Pos = 0; |
| 2343 | for (; Pos != End; ++Pos) { |
| 2344 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2345 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2346 | // This macro has no parameters, look for $0, $1, etc. |
| 2347 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 2348 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2349 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2350 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2351 | if (Next == '$' || Next == 'n' || |
| 2352 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | } else { |
| 2355 | // This macro has parameters, look for \foo, \bar, etc. |
| 2356 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 2357 | break; |
| 2358 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | // Add the prefix. |
| 2362 | OS << Body.slice(0, Pos); |
| 2363 | |
| 2364 | // Check if we reached the end. |
| 2365 | if (Pos == End) |
| 2366 | break; |
| 2367 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2368 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2369 | switch (Body[Pos + 1]) { |
| 2370 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2371 | case '$': |
| 2372 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2373 | break; |
| 2374 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2375 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2376 | case 'n': |
| 2377 | OS << A.size(); |
| 2378 | break; |
| 2379 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2380 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2381 | default: { |
| 2382 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2383 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2384 | if (Index >= A.size()) |
| 2385 | break; |
| 2386 | |
| 2387 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2388 | for (const AsmToken &Token : A[Index]) |
| 2389 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2390 | break; |
| 2391 | } |
| 2392 | } |
| 2393 | Pos += 2; |
| 2394 | } else { |
| 2395 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2396 | |
| 2397 | // Check for the \@ pseudo-variable. |
| 2398 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2399 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2400 | else |
| 2401 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 2402 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2403 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2404 | const char *Begin = Body.data() + Pos + 1; |
| 2405 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2406 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2407 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2408 | if (Argument == "@") { |
| 2409 | OS << NumOfMacroInstantiations; |
| 2410 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2411 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2412 | for (; Index < NParameters; ++Index) |
| 2413 | if (Parameters[Index].Name == Argument) |
| 2414 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2415 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2416 | if (Index == NParameters) { |
| 2417 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 2418 | Pos += 3; |
| 2419 | else { |
| 2420 | OS << '\\' << Argument; |
| 2421 | Pos = I; |
| 2422 | } |
| 2423 | } else { |
| 2424 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2425 | for (const AsmToken &Token : A[Index]) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2426 | // For altmacro mode, you can write '%expr'. |
| 2427 | // The prefix '%' evaluates the expression 'expr' |
| 2428 | // and uses the result as a string (e.g. replace %(1+2) with the string "3"). |
| 2429 | // Here, we identify the integer token which is the result of the |
| 2430 | // absolute expression evaluation and replace it with its string representation. |
| 2431 | if ((Lexer.IsaAltMacroMode()) && |
| 2432 | (*(Token.getString().begin()) == '%') && Token.is(AsmToken::Integer)) |
| 2433 | // Emit an integer value to the buffer. |
| 2434 | OS << Token.getIntVal(); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2435 | // Only Token that was validated as a string and begins with '<' |
| 2436 | // is considered altMacroString!!! |
| 2437 | else if ((Lexer.IsaAltMacroMode()) && |
| 2438 | (*(Token.getString().begin()) == '<') && |
| 2439 | Token.is(AsmToken::String)) { |
| 2440 | std::string Res; |
| 2441 | altMacroString(Token.getStringContents(), Res); |
| 2442 | OS << Res; |
| 2443 | } |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2444 | // We expect no quotes around the string's contents when |
| 2445 | // parsing for varargs. |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2446 | else if (Token.isNot(AsmToken::String) || VarargParameter) |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2447 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2448 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2449 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2450 | |
| 2451 | Pos += 1 + Argument.size(); |
| 2452 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2453 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2454 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2455 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2456 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2457 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2458 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2459 | return false; |
| 2460 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2461 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2462 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2463 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2464 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2465 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2466 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2467 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2468 | switch (kind) { |
| 2469 | default: |
| 2470 | return false; |
| 2471 | case AsmToken::Plus: |
| 2472 | case AsmToken::Minus: |
| 2473 | case AsmToken::Tilde: |
| 2474 | case AsmToken::Slash: |
| 2475 | case AsmToken::Star: |
| 2476 | case AsmToken::Dot: |
| 2477 | case AsmToken::Equal: |
| 2478 | case AsmToken::EqualEqual: |
| 2479 | case AsmToken::Pipe: |
| 2480 | case AsmToken::PipePipe: |
| 2481 | case AsmToken::Caret: |
| 2482 | case AsmToken::Amp: |
| 2483 | case AsmToken::AmpAmp: |
| 2484 | case AsmToken::Exclaim: |
| 2485 | case AsmToken::ExclaimEqual: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2486 | case AsmToken::Less: |
| 2487 | case AsmToken::LessEqual: |
| 2488 | case AsmToken::LessLess: |
| 2489 | case AsmToken::LessGreater: |
| 2490 | case AsmToken::Greater: |
| 2491 | case AsmToken::GreaterEqual: |
| 2492 | case AsmToken::GreaterGreater: |
| 2493 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2497 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2498 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2499 | class AsmLexerSkipSpaceRAII { |
| 2500 | public: |
| 2501 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2502 | Lexer.setSkipSpace(SkipSpace); |
| 2503 | } |
| 2504 | |
| 2505 | ~AsmLexerSkipSpaceRAII() { |
| 2506 | Lexer.setSkipSpace(true); |
| 2507 | } |
| 2508 | |
| 2509 | private: |
| 2510 | AsmLexer &Lexer; |
| 2511 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2512 | |
| 2513 | } // end anonymous namespace |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2514 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2515 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2516 | |
| 2517 | if (Vararg) { |
| 2518 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2519 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2520 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2521 | } |
| 2522 | return false; |
| 2523 | } |
| 2524 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2525 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2526 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2527 | // Darwin doesn't use spaces to delmit arguments. |
| 2528 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2529 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2530 | bool SpaceEaten; |
| 2531 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2532 | while (true) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2533 | SpaceEaten = false; |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2534 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2535 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2536 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2537 | if (ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2538 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2539 | if (Lexer.is(AsmToken::Comma)) |
| 2540 | break; |
| 2541 | |
| 2542 | if (Lexer.is(AsmToken::Space)) { |
| 2543 | SpaceEaten = true; |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2544 | Lexer.Lex(); // Eat spaces |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2545 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Spaces can delimit parameters, but could also be part an expression. |
| 2548 | // If the token after a space is an operator, add the token and the next |
| 2549 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2550 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2551 | if (isOperator(Lexer.getKind())) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2552 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2553 | Lexer.Lex(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2554 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2555 | // Whitespace after an operator can be ignored. |
| 2556 | if (Lexer.is(AsmToken::Space)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2557 | Lexer.Lex(); |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2558 | |
| 2559 | continue; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2562 | if (SpaceEaten) |
| 2563 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2564 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2565 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2566 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2567 | // to be able to fill in the remaining default parameter values |
| 2568 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2569 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Adjust the current parentheses level. |
| 2572 | if (Lexer.is(AsmToken::LParen)) |
| 2573 | ++ParenLevel; |
| 2574 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2575 | --ParenLevel; |
| 2576 | |
| 2577 | // Append the token to the current argument list. |
| 2578 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2579 | Lexer.Lex(); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2580 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2581 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2582 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2583 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2584 | return false; |
| 2585 | } |
| 2586 | |
| 2587 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2588 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2589 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2590 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2591 | bool NamedParametersFound = false; |
| 2592 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2593 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2594 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2595 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2596 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2597 | // Parse two kinds of macro invocations: |
| 2598 | // - macros defined without any parameters accept an arbitrary number of them |
| 2599 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2600 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2601 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2602 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2603 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2604 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2605 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2606 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2607 | if (parseIdentifier(FA.Name)) |
| 2608 | return Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2609 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2610 | if (Lexer.isNot(AsmToken::Equal)) |
| 2611 | return TokError("expected '=' after formal parameter identifier"); |
| 2612 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2613 | Lex(); |
| 2614 | |
| 2615 | NamedParametersFound = true; |
| 2616 | } |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2617 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2618 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2619 | if (NamedParametersFound && FA.Name.empty()) |
| 2620 | return Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2621 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2622 | SMLoc StrLoc = Lexer.getLoc(); |
| 2623 | SMLoc EndLoc; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2624 | if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Percent)) { |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2625 | const MCExpr *AbsoluteExp; |
| 2626 | int64_t Value; |
| 2627 | /// Eat '%' |
| 2628 | Lex(); |
| 2629 | if (parseExpression(AbsoluteExp, EndLoc)) |
| 2630 | return false; |
| 2631 | if (!AbsoluteExp->evaluateAsAbsolute(Value)) |
| 2632 | return Error(StrLoc, "expected absolute expression"); |
| 2633 | const char *StrChar = StrLoc.getPointer(); |
| 2634 | const char *EndChar = EndLoc.getPointer(); |
| 2635 | AsmToken newToken(AsmToken::Integer, StringRef(StrChar , EndChar - StrChar), Value); |
| 2636 | FA.Value.push_back(newToken); |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2637 | } else if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Less) && |
| 2638 | isAltmacroString(StrLoc, EndLoc)) { |
| 2639 | const char *StrChar = StrLoc.getPointer(); |
| 2640 | const char *EndChar = EndLoc.getPointer(); |
| 2641 | jumpToLoc(EndLoc, CurBuffer); |
| 2642 | /// Eat from '<' to '>' |
| 2643 | Lex(); |
| 2644 | AsmToken newToken(AsmToken::String, StringRef(StrChar, EndChar - StrChar)); |
| 2645 | FA.Value.push_back(newToken); |
| 2646 | } else if(parseMacroArgument(FA.Value, Vararg)) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2647 | return true; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2648 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2649 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2650 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2651 | unsigned FAI = 0; |
| 2652 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2653 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2654 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2655 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2656 | if (FAI >= NParameters) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2657 | assert(M && "expected macro to be defined"); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2658 | return Error(IDLoc, "parameter named '" + FA.Name + |
| 2659 | "' does not exist for macro '" + M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2660 | } |
| 2661 | PI = FAI; |
| 2662 | } |
| 2663 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2664 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2665 | if (A.size() <= PI) |
| 2666 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2667 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2668 | |
| 2669 | if (FALocs.size() <= PI) |
| 2670 | FALocs.resize(PI + 1); |
| 2671 | |
| 2672 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2673 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2674 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2675 | // At the end of the statement, fill in remaining arguments that have |
| 2676 | // default values. If there aren't any, then the next argument is |
| 2677 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2678 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2679 | bool Failure = false; |
| 2680 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2681 | if (A[FAI].empty()) { |
| 2682 | if (M->Parameters[FAI].Required) { |
| 2683 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2684 | "missing value for required parameter " |
| 2685 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2686 | Failure = true; |
| 2687 | } |
| 2688 | |
| 2689 | if (!M->Parameters[FAI].Value.empty()) |
| 2690 | A[FAI] = M->Parameters[FAI].Value; |
| 2691 | } |
| 2692 | } |
| 2693 | return Failure; |
| 2694 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2695 | |
| 2696 | if (Lexer.is(AsmToken::Comma)) |
| 2697 | Lex(); |
| 2698 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2699 | |
| 2700 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2703 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2704 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 2705 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2708 | void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 2709 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2712 | void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2713 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2714 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 2715 | // Arbitrarily limit macro nesting depth (default matches 'as'). We can |
| 2716 | // eliminate this, although we should protect against infinite loops. |
| 2717 | unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; |
| 2718 | if (ActiveMacros.size() == MaxNestingDepth) { |
| 2719 | std::ostringstream MaxNestingDepthError; |
| 2720 | MaxNestingDepthError << "macros cannot be nested more than " |
| 2721 | << MaxNestingDepth << " levels deep." |
| 2722 | << " Use -asm-macro-max-nesting-depth to increase " |
| 2723 | "this limit."; |
| 2724 | return TokError(MaxNestingDepthError.str()); |
| 2725 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2726 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2727 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2728 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2729 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2730 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2731 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2732 | // to hold the macro body with substitutions. |
| 2733 | SmallString<256> Buf; |
| 2734 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2735 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2736 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2737 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2738 | return true; |
| 2739 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2740 | // 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] | 2741 | // instantiation. |
| 2742 | OS << ".endmacro\n"; |
| 2743 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2744 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2745 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2746 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2747 | // Create the macro instantiation object and add to the current macro |
| 2748 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2749 | MacroInstantiation *MI = new MacroInstantiation( |
| 2750 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2751 | ActiveMacros.push_back(MI); |
| 2752 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2753 | ++NumOfMacroInstantiations; |
| 2754 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2755 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2756 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2757 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2758 | Lex(); |
| 2759 | |
| 2760 | return false; |
| 2761 | } |
| 2762 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2763 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2764 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2765 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2766 | Lex(); |
| 2767 | |
| 2768 | // Pop the instantiation entry. |
| 2769 | delete ActiveMacros.back(); |
| 2770 | ActiveMacros.pop_back(); |
| 2771 | } |
| 2772 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2773 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2774 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2775 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2776 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2777 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| 2778 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2779 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2780 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2781 | if (!Sym) { |
| 2782 | // In the case where we parse an expression starting with a '.', we will |
| 2783 | // not generate an error, nor will we create a symbol. In this case we |
| 2784 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2785 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2786 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2787 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2788 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2789 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2790 | if (NoDeadStrip) |
| 2791 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2792 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2793 | return false; |
| 2794 | } |
| 2795 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2796 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2797 | /// ::= identifier |
| 2798 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2799 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2800 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2801 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2802 | // 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] | 2803 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2804 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2805 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2806 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2807 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2808 | // Consume the prefix character, and check for a following identifier. |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2809 | |
| 2810 | AsmToken Buf[1]; |
| 2811 | Lexer.peekTokens(Buf, false); |
| 2812 | |
| 2813 | if (Buf[0].isNot(AsmToken::Identifier)) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2814 | return true; |
| 2815 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2816 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2817 | if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2818 | return true; |
| 2819 | |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2820 | // eat $ or @ |
| 2821 | Lexer.Lex(); // Lexer's Lex guarantees consecutive token. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2822 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2823 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2824 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2825 | Lex(); // Parser Lex to maintain invariants. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2826 | return false; |
| 2827 | } |
| 2828 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2829 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2830 | return true; |
| 2831 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2832 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2833 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2834 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2835 | |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2839 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2840 | /// ::= .equ identifier ',' expression |
| 2841 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2842 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2843 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2844 | StringRef Name; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2845 | if (check(parseIdentifier(Name), "expected identifier") || |
| 2846 | parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) |
| 2847 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
| 2848 | return false; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2851 | bool AsmParser::parseEscapedString(std::string &Data) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2852 | if (check(getTok().isNot(AsmToken::String), "expected string")) |
| 2853 | return true; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2854 | |
| 2855 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2856 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2857 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2858 | if (Str[i] != '\\') { |
| 2859 | Data += Str[i]; |
| 2860 | continue; |
| 2861 | } |
| 2862 | |
| 2863 | // Recognize escaped characters. Note that this escape semantics currently |
| 2864 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2865 | ++i; |
| 2866 | if (i == e) |
| 2867 | return TokError("unexpected backslash at end of string"); |
| 2868 | |
| 2869 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2870 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2871 | // Consume up to three octal characters. |
| 2872 | unsigned Value = Str[i] - '0'; |
| 2873 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2874 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2875 | ++i; |
| 2876 | Value = Value * 8 + (Str[i] - '0'); |
| 2877 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2878 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2879 | ++i; |
| 2880 | Value = Value * 8 + (Str[i] - '0'); |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | if (Value > 255) |
| 2885 | return TokError("invalid octal escape sequence (out of range)"); |
| 2886 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2887 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2888 | continue; |
| 2889 | } |
| 2890 | |
| 2891 | // Otherwise recognize individual escapes. |
| 2892 | switch (Str[i]) { |
| 2893 | default: |
| 2894 | // Just reject invalid escape sequences for now. |
| 2895 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2896 | |
| 2897 | case 'b': Data += '\b'; break; |
| 2898 | case 'f': Data += '\f'; break; |
| 2899 | case 'n': Data += '\n'; break; |
| 2900 | case 'r': Data += '\r'; break; |
| 2901 | case 't': Data += '\t'; break; |
| 2902 | case '"': Data += '"'; break; |
| 2903 | case '\\': Data += '\\'; break; |
| 2904 | } |
| 2905 | } |
| 2906 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2907 | Lex(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2908 | return false; |
| 2909 | } |
| 2910 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2911 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2912 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2913 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2914 | auto parseOp = [&]() -> bool { |
| 2915 | std::string Data; |
| 2916 | if (checkForValidSection() || parseEscapedString(Data)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2917 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2918 | getStreamer().EmitBytes(Data); |
| 2919 | if (ZeroTerminated) |
| 2920 | getStreamer().EmitBytes(StringRef("\0", 1)); |
| 2921 | return false; |
| 2922 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2923 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2924 | if (parseMany(parseOp)) |
| 2925 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2926 | return false; |
| 2927 | } |
| 2928 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2929 | /// parseDirectiveReloc |
| 2930 | /// ::= .reloc expression , identifier [ , expression ] |
| 2931 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
| 2932 | const MCExpr *Offset; |
| 2933 | const MCExpr *Expr = nullptr; |
| 2934 | |
| 2935 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2936 | int64_t OffsetValue; |
| 2937 | // We can only deal with constant expressions at the moment. |
| 2938 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2939 | if (parseExpression(Offset)) |
| 2940 | return true; |
| 2941 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2942 | if (check(!Offset->evaluateAsAbsolute(OffsetValue), OffsetLoc, |
| 2943 | "expression is not a constant value") || |
| 2944 | check(OffsetValue < 0, OffsetLoc, "expression is negative") || |
| 2945 | parseToken(AsmToken::Comma, "expected comma") || |
| 2946 | check(getTok().isNot(AsmToken::Identifier), "expected relocation name")) |
| 2947 | return true; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2948 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2949 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
| 2950 | StringRef Name = Lexer.getTok().getIdentifier(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2951 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2952 | |
| 2953 | if (Lexer.is(AsmToken::Comma)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2954 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2955 | SMLoc ExprLoc = Lexer.getLoc(); |
| 2956 | if (parseExpression(Expr)) |
| 2957 | return true; |
| 2958 | |
| 2959 | MCValue Value; |
| 2960 | if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) |
| 2961 | return Error(ExprLoc, "expression must be relocatable"); |
| 2962 | } |
| 2963 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2964 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 2965 | "unexpected token in .reloc directive")) |
| 2966 | return true; |
| 2967 | |
| 2968 | if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc)) |
| 2969 | return Error(NameLoc, "unknown relocation name"); |
| 2970 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2971 | return false; |
| 2972 | } |
| 2973 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2974 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2975 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2976 | bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { |
| 2977 | auto parseOp = [&]() -> bool { |
| 2978 | const MCExpr *Value; |
| 2979 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2980 | if (checkForValidSection() || parseExpression(Value)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2981 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2982 | // Special case constant expressions to match code generator. |
| 2983 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2984 | assert(Size <= 8 && "Invalid size"); |
| 2985 | uint64_t IntValue = MCE->getValue(); |
| 2986 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2987 | return Error(ExprLoc, "out of range literal value"); |
| 2988 | getStreamer().EmitIntValue(IntValue, Size); |
| 2989 | } else |
| 2990 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 2991 | return false; |
| 2992 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2993 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2994 | if (parseMany(parseOp)) |
| 2995 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2996 | return false; |
| 2997 | } |
| 2998 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2999 | /// ParseDirectiveOctaValue |
| 3000 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3001 | |
| 3002 | bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { |
| 3003 | auto parseOp = [&]() -> bool { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3004 | if (checkForValidSection()) |
| 3005 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3006 | if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::BigNum)) |
| 3007 | return TokError("unknown token in expression"); |
| 3008 | SMLoc ExprLoc = getTok().getLoc(); |
| 3009 | APInt IntValue = getTok().getAPIntVal(); |
| 3010 | uint64_t hi, lo; |
| 3011 | Lex(); |
| 3012 | if (!IntValue.isIntN(128)) |
| 3013 | return Error(ExprLoc, "out of range literal value"); |
| 3014 | if (!IntValue.isIntN(64)) { |
| 3015 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
| 3016 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 3017 | } else { |
| 3018 | hi = 0; |
| 3019 | lo = IntValue.getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3020 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3021 | if (MAI.isLittleEndian()) { |
| 3022 | getStreamer().EmitIntValue(lo, 8); |
| 3023 | getStreamer().EmitIntValue(hi, 8); |
| 3024 | } else { |
| 3025 | getStreamer().EmitIntValue(hi, 8); |
| 3026 | getStreamer().EmitIntValue(lo, 8); |
| 3027 | } |
| 3028 | return false; |
| 3029 | }; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3030 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3031 | if (parseMany(parseOp)) |
| 3032 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3033 | return false; |
| 3034 | } |
| 3035 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 3036 | bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { |
| 3037 | // We don't truly support arithmetic on floating point expressions, so we |
| 3038 | // have to manually parse unary prefixes. |
| 3039 | bool IsNeg = false; |
| 3040 | if (getLexer().is(AsmToken::Minus)) { |
| 3041 | Lexer.Lex(); |
| 3042 | IsNeg = true; |
| 3043 | } else if (getLexer().is(AsmToken::Plus)) |
| 3044 | Lexer.Lex(); |
| 3045 | |
| 3046 | if (Lexer.is(AsmToken::Error)) |
| 3047 | return TokError(Lexer.getErr()); |
| 3048 | if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && |
| 3049 | Lexer.isNot(AsmToken::Identifier)) |
| 3050 | return TokError("unexpected token in directive"); |
| 3051 | |
| 3052 | // Convert to an APFloat. |
| 3053 | APFloat Value(Semantics); |
| 3054 | StringRef IDVal = getTok().getString(); |
| 3055 | if (getLexer().is(AsmToken::Identifier)) { |
| 3056 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 3057 | Value = APFloat::getInf(Semantics); |
| 3058 | else if (!IDVal.compare_lower("nan")) |
| 3059 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 3060 | else |
| 3061 | return TokError("invalid floating point literal"); |
| 3062 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
| 3063 | APFloat::opInvalidOp) |
| 3064 | return TokError("invalid floating point literal"); |
| 3065 | if (IsNeg) |
| 3066 | Value.changeSign(); |
| 3067 | |
| 3068 | // Consume the numeric token. |
| 3069 | Lex(); |
| 3070 | |
| 3071 | Res = Value.bitcastToAPInt(); |
| 3072 | |
| 3073 | return false; |
| 3074 | } |
| 3075 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3076 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3077 | /// ::= (.single | .double) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3078 | bool AsmParser::parseDirectiveRealValue(StringRef IDVal, |
| 3079 | const fltSemantics &Semantics) { |
| 3080 | auto parseOp = [&]() -> bool { |
| 3081 | APInt AsInt; |
| 3082 | if (checkForValidSection() || parseRealValue(Semantics, AsInt)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3083 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3084 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 3085 | AsInt.getBitWidth() / 8); |
| 3086 | return false; |
| 3087 | }; |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3088 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3089 | if (parseMany(parseOp)) |
| 3090 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3091 | return false; |
| 3092 | } |
| 3093 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3094 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3095 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3096 | bool AsmParser::parseDirectiveZero() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3097 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 3098 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3099 | if (checkForValidSection() || parseExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3100 | return true; |
| 3101 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3102 | int64_t Val = 0; |
| 3103 | if (getLexer().is(AsmToken::Comma)) { |
| 3104 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3105 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3106 | return true; |
| 3107 | } |
| 3108 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3109 | if (parseToken(AsmToken::EndOfStatement, |
| 3110 | "unexpected token in '.zero' directive")) |
| 3111 | return true; |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3112 | getStreamer().emitFill(*NumBytes, Val, NumBytesLoc); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3113 | |
| 3114 | return false; |
| 3115 | } |
| 3116 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3117 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3118 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3119 | bool AsmParser::parseDirectiveFill() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3120 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 3121 | const MCExpr *NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3122 | if (checkForValidSection() || parseExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3123 | return true; |
| 3124 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3125 | int64_t FillSize = 1; |
| 3126 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3127 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3128 | SMLoc SizeLoc, ExprLoc; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3129 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3130 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3131 | SizeLoc = getTok().getLoc(); |
| 3132 | if (parseAbsoluteExpression(FillSize)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3133 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3134 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3135 | ExprLoc = getTok().getLoc(); |
| 3136 | if (parseAbsoluteExpression(FillExpr)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3137 | return true; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3138 | } |
| 3139 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3140 | if (parseToken(AsmToken::EndOfStatement, |
| 3141 | "unexpected token in '.fill' directive")) |
| 3142 | return true; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3143 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3144 | if (FillSize < 0) { |
| 3145 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
Petr Hosek | 6abd38b | 2016-05-28 08:20:08 +0000 | [diff] [blame] | 3146 | return false; |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3147 | } |
| 3148 | if (FillSize > 8) { |
| 3149 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 3150 | FillSize = 8; |
| 3151 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3152 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3153 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 3154 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 3155 | |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3156 | getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3157 | |
| 3158 | return false; |
| 3159 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3161 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3162 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3163 | bool AsmParser::parseDirectiveOrg() { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 3164 | const MCExpr *Offset; |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3165 | SMLoc OffsetLoc = Lexer.getLoc(); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3166 | if (checkForValidSection() || parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3167 | return true; |
| 3168 | |
| 3169 | // Parse optional fill expression. |
| 3170 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3171 | if (parseOptionalToken(AsmToken::Comma)) |
| 3172 | if (parseAbsoluteExpression(FillExpr)) |
| 3173 | return addErrorSuffix(" in '.org' directive"); |
| 3174 | if (parseToken(AsmToken::EndOfStatement)) |
| 3175 | return addErrorSuffix(" in '.org' directive"); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3176 | |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3177 | getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3178 | return false; |
| 3179 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3180 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3181 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3182 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3183 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3184 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3185 | int64_t Alignment; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3186 | SMLoc MaxBytesLoc; |
| 3187 | bool HasFillExpr = false; |
| 3188 | int64_t FillExpr = 0; |
| 3189 | int64_t MaxBytesToFill = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3190 | |
| 3191 | auto parseAlign = [&]() -> bool { |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3192 | if (parseAbsoluteExpression(Alignment)) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3193 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3194 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3195 | // The fill expression can be omitted while specifying a maximum number of |
| 3196 | // alignment bytes, e.g: |
| 3197 | // .align 3,,4 |
| 3198 | if (getTok().isNot(AsmToken::Comma)) { |
| 3199 | HasFillExpr = true; |
| 3200 | if (parseAbsoluteExpression(FillExpr)) |
| 3201 | return true; |
| 3202 | } |
| 3203 | if (parseOptionalToken(AsmToken::Comma)) |
| 3204 | if (parseTokenLoc(MaxBytesLoc) || |
| 3205 | parseAbsoluteExpression(MaxBytesToFill)) |
| 3206 | return true; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3207 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3208 | return parseToken(AsmToken::EndOfStatement); |
| 3209 | }; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3210 | |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3211 | if (checkForValidSection()) |
| 3212 | return addErrorSuffix(" in directive"); |
| 3213 | // Ignore empty '.p2align' directives for GNU-as compatibility |
| 3214 | if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) { |
| 3215 | Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored"); |
| 3216 | return parseToken(AsmToken::EndOfStatement); |
| 3217 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3218 | if (parseAlign()) |
| 3219 | return addErrorSuffix(" in directive"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3220 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3221 | // Always emit an alignment here even if we thrown an error. |
| 3222 | bool ReturnVal = false; |
| 3223 | |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3224 | // Compute alignment in bytes. |
| 3225 | if (IsPow2) { |
| 3226 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3227 | if (Alignment >= 32) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3228 | ReturnVal |= Error(AlignmentLoc, "invalid alignment value"); |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3229 | Alignment = 31; |
| 3230 | } |
| 3231 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 3232 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3233 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 3234 | // Reject alignments that aren't either a power of two or zero, |
| 3235 | // for gas compatibility. Alignment of zero is silently rounded |
| 3236 | // up to one. |
| 3237 | if (Alignment == 0) |
| 3238 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3239 | if (!isPowerOf2_64(Alignment)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3240 | ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3243 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3244 | if (MaxBytesLoc.isValid()) { |
| 3245 | if (MaxBytesToFill < 1) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3246 | ReturnVal |= Error(MaxBytesLoc, |
| 3247 | "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3248 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 3249 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 3253 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3254 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3255 | MaxBytesToFill = 0; |
| 3256 | } |
| 3257 | } |
| 3258 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3259 | // Check whether we should use optimal code alignment for this .align |
| 3260 | // directive. |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 3261 | const MCSection *Section = getStreamer().getCurrentSectionOnly(); |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 3262 | assert(Section && "must have section to emit alignment"); |
| 3263 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3264 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 3265 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3266 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3267 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 3268 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 3269 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 3270 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3271 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3272 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3273 | return ReturnVal; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3276 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3277 | /// ::= .file [number] filename |
| 3278 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3279 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3280 | // FIXME: I'm not sure what this is. |
| 3281 | int64_t FileNumber = -1; |
| 3282 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 3283 | if (getLexer().is(AsmToken::Integer)) { |
| 3284 | FileNumber = getTok().getIntVal(); |
| 3285 | Lex(); |
| 3286 | |
| 3287 | if (FileNumber < 1) |
| 3288 | return TokError("file number less than one"); |
| 3289 | } |
| 3290 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3291 | std::string Path = getTok().getString(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3292 | |
| 3293 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3294 | // Allow the strings to have escaped octal character sequence. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3295 | if (check(getTok().isNot(AsmToken::String), |
| 3296 | "unexpected token in '.file' directive") || |
| 3297 | parseEscapedString(Path)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3298 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3299 | |
| 3300 | StringRef Directory; |
| 3301 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3302 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3303 | if (getLexer().is(AsmToken::String)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3304 | if (check(FileNumber == -1, |
| 3305 | "explicit path specified, but no file number") || |
| 3306 | parseEscapedString(FilenameData)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3307 | return true; |
| 3308 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3309 | Directory = Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3310 | } else { |
| 3311 | Filename = Path; |
| 3312 | } |
| 3313 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3314 | if (parseToken(AsmToken::EndOfStatement, |
| 3315 | "unexpected token in '.file' directive")) |
| 3316 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (FileNumber == -1) |
| 3319 | getStreamer().EmitFileDirective(Filename); |
| 3320 | else { |
David Blaikie | 2274808 | 2016-05-26 00:22:26 +0000 | [diff] [blame] | 3321 | // If there is -g option as well as debug info from directive file, |
| 3322 | // we turn off -g option, directly use the existing debug info instead. |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 3323 | if (getContext().getGenDwarfForAssembly()) |
David Blaikie | 2274808 | 2016-05-26 00:22:26 +0000 | [diff] [blame] | 3324 | getContext().setGenDwarfForAssembly(false); |
| 3325 | else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) == |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 3326 | 0) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3327 | return Error(FileNumberLoc, "file number already allocated"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | return false; |
| 3331 | } |
| 3332 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3333 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3334 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3335 | bool AsmParser::parseDirectiveLine() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3336 | int64_t LineNumber; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3337 | if (getLexer().is(AsmToken::Integer)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3338 | if (parseIntToken(LineNumber, "unexpected token in '.line' directive")) |
| 3339 | return true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3340 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3341 | // FIXME: Do something with the .line. |
| 3342 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3343 | if (parseToken(AsmToken::EndOfStatement, |
| 3344 | "unexpected token in '.line' directive")) |
| 3345 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3346 | |
| 3347 | return false; |
| 3348 | } |
| 3349 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3350 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3351 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 3352 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 3353 | /// The first number is a file number, must have been previously assigned with |
| 3354 | /// a .file directive, the second number is the line number and optionally the |
| 3355 | /// third number is a column position (zero if not specified). The remaining |
| 3356 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3357 | bool AsmParser::parseDirectiveLoc() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3358 | int64_t FileNumber = 0, LineNumber = 0; |
| 3359 | SMLoc Loc = getTok().getLoc(); |
| 3360 | if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") || |
| 3361 | check(FileNumber < 1, Loc, |
| 3362 | "file number less than one in '.loc' directive") || |
| 3363 | check(!getContext().isValidDwarfFileNumber(FileNumber), Loc, |
| 3364 | "unassigned file number in '.loc' directive")) |
| 3365 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3366 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3367 | // optional |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3368 | if (getLexer().is(AsmToken::Integer)) { |
| 3369 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 3370 | if (LineNumber < 0) |
| 3371 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3372 | Lex(); |
| 3373 | } |
| 3374 | |
| 3375 | int64_t ColumnPos = 0; |
| 3376 | if (getLexer().is(AsmToken::Integer)) { |
| 3377 | ColumnPos = getTok().getIntVal(); |
| 3378 | if (ColumnPos < 0) |
| 3379 | return TokError("column position less than zero in '.loc' directive"); |
| 3380 | Lex(); |
| 3381 | } |
| 3382 | |
| 3383 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 3384 | unsigned Isa = 0; |
| 3385 | int64_t Discriminator = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3386 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3387 | auto parseLocOp = [&]() -> bool { |
| 3388 | StringRef Name; |
| 3389 | SMLoc Loc = getTok().getLoc(); |
| 3390 | if (parseIdentifier(Name)) |
| 3391 | return TokError("unexpected token in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3392 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3393 | if (Name == "basic_block") |
| 3394 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 3395 | else if (Name == "prologue_end") |
| 3396 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 3397 | else if (Name == "epilogue_begin") |
| 3398 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 3399 | else if (Name == "is_stmt") { |
| 3400 | Loc = getTok().getLoc(); |
| 3401 | const MCExpr *Value; |
| 3402 | if (parseExpression(Value)) |
| 3403 | return true; |
| 3404 | // The expression must be the constant 0 or 1. |
| 3405 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3406 | int Value = MCE->getValue(); |
| 3407 | if (Value == 0) |
| 3408 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 3409 | else if (Value == 1) |
| 3410 | Flags |= DWARF2_FLAG_IS_STMT; |
| 3411 | else |
| 3412 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3413 | } else { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3414 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3415 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3416 | } else if (Name == "isa") { |
| 3417 | Loc = getTok().getLoc(); |
| 3418 | const MCExpr *Value; |
| 3419 | if (parseExpression(Value)) |
| 3420 | return true; |
| 3421 | // The expression must be a constant greater or equal to 0. |
| 3422 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3423 | int Value = MCE->getValue(); |
| 3424 | if (Value < 0) |
| 3425 | return Error(Loc, "isa number less than zero"); |
| 3426 | Isa = Value; |
| 3427 | } else { |
| 3428 | return Error(Loc, "isa number not a constant value"); |
| 3429 | } |
| 3430 | } else if (Name == "discriminator") { |
| 3431 | if (parseAbsoluteExpression(Discriminator)) |
| 3432 | return true; |
| 3433 | } else { |
| 3434 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3435 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3436 | return false; |
| 3437 | }; |
| 3438 | |
Nirav Dave | e2369aa | 2016-10-26 17:28:58 +0000 | [diff] [blame] | 3439 | if (parseMany(parseLocOp, false /*hasComma*/)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3440 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3441 | |
| 3442 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3443 | Isa, Discriminator, StringRef()); |
| 3444 | |
| 3445 | return false; |
| 3446 | } |
| 3447 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3448 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3449 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3450 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3451 | return TokError("unsupported directive '.stabs'"); |
| 3452 | } |
| 3453 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3454 | /// parseDirectiveCVFile |
| 3455 | /// ::= .cv_file number filename |
| 3456 | bool AsmParser::parseDirectiveCVFile() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3457 | SMLoc FileNumberLoc = getTok().getLoc(); |
| 3458 | int64_t FileNumber; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3459 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3460 | |
| 3461 | if (parseIntToken(FileNumber, |
| 3462 | "expected file number in '.cv_file' directive") || |
| 3463 | check(FileNumber < 1, FileNumberLoc, "file number less than one") || |
| 3464 | check(getTok().isNot(AsmToken::String), |
| 3465 | "unexpected token in '.cv_file' directive") || |
| 3466 | // Usually directory and filename are together, otherwise just |
| 3467 | // directory. Allow the strings to have escaped octal character sequence. |
| 3468 | parseEscapedString(Filename) || |
| 3469 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3470 | "unexpected token in '.cv_file' directive")) |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 3471 | return true; |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3472 | |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 3473 | if (!getStreamer().EmitCVFileDirective(FileNumber, Filename)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3474 | return Error(FileNumberLoc, "file number already allocated"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3475 | |
| 3476 | return false; |
| 3477 | } |
| 3478 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3479 | bool AsmParser::parseCVFunctionId(int64_t &FunctionId, |
| 3480 | StringRef DirectiveName) { |
| 3481 | SMLoc Loc; |
| 3482 | return parseTokenLoc(Loc) || |
| 3483 | parseIntToken(FunctionId, "expected function id in '" + DirectiveName + |
| 3484 | "' directive") || |
| 3485 | check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc, |
| 3486 | "expected function id within range [0, UINT_MAX)"); |
| 3487 | } |
| 3488 | |
| 3489 | bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { |
| 3490 | SMLoc Loc; |
| 3491 | return parseTokenLoc(Loc) || |
| 3492 | parseIntToken(FileNumber, "expected integer in '" + DirectiveName + |
| 3493 | "' directive") || |
| 3494 | check(FileNumber < 1, Loc, "file number less than one in '" + |
| 3495 | DirectiveName + "' directive") || |
| 3496 | check(!getCVContext().isValidFileNumber(FileNumber), Loc, |
| 3497 | "unassigned file number in '" + DirectiveName + "' directive"); |
| 3498 | } |
| 3499 | |
| 3500 | /// parseDirectiveCVFuncId |
| 3501 | /// ::= .cv_func_id FunctionId |
| 3502 | /// |
| 3503 | /// Introduces a function ID that can be used with .cv_loc. |
| 3504 | bool AsmParser::parseDirectiveCVFuncId() { |
| 3505 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3506 | int64_t FunctionId; |
| 3507 | |
| 3508 | if (parseCVFunctionId(FunctionId, ".cv_func_id") || |
| 3509 | parseToken(AsmToken::EndOfStatement, |
| 3510 | "unexpected token in '.cv_func_id' directive")) |
| 3511 | return true; |
| 3512 | |
| 3513 | if (!getStreamer().EmitCVFuncIdDirective(FunctionId)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3514 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3515 | |
| 3516 | return false; |
| 3517 | } |
| 3518 | |
| 3519 | /// parseDirectiveCVInlineSiteId |
| 3520 | /// ::= .cv_inline_site_id FunctionId |
| 3521 | /// "within" IAFunc |
| 3522 | /// "inlined_at" IAFile IALine [IACol] |
| 3523 | /// |
| 3524 | /// Introduces a function ID that can be used with .cv_loc. Includes "inlined |
| 3525 | /// at" source location information for use in the line table of the caller, |
| 3526 | /// whether the caller is a real function or another inlined call site. |
| 3527 | bool AsmParser::parseDirectiveCVInlineSiteId() { |
| 3528 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3529 | int64_t FunctionId; |
| 3530 | int64_t IAFunc; |
| 3531 | int64_t IAFile; |
| 3532 | int64_t IALine; |
| 3533 | int64_t IACol = 0; |
| 3534 | |
| 3535 | // FunctionId |
| 3536 | if (parseCVFunctionId(FunctionId, ".cv_inline_site_id")) |
| 3537 | return true; |
| 3538 | |
| 3539 | // "within" |
| 3540 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3541 | getTok().getIdentifier() != "within"), |
| 3542 | "expected 'within' identifier in '.cv_inline_site_id' directive")) |
| 3543 | return true; |
| 3544 | Lex(); |
| 3545 | |
| 3546 | // IAFunc |
| 3547 | if (parseCVFunctionId(IAFunc, ".cv_inline_site_id")) |
| 3548 | return true; |
| 3549 | |
| 3550 | // "inlined_at" |
| 3551 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3552 | getTok().getIdentifier() != "inlined_at"), |
| 3553 | "expected 'inlined_at' identifier in '.cv_inline_site_id' " |
| 3554 | "directive") ) |
| 3555 | return true; |
| 3556 | Lex(); |
| 3557 | |
| 3558 | // IAFile IALine |
| 3559 | if (parseCVFileId(IAFile, ".cv_inline_site_id") || |
| 3560 | parseIntToken(IALine, "expected line number after 'inlined_at'")) |
| 3561 | return true; |
| 3562 | |
| 3563 | // [IACol] |
| 3564 | if (getLexer().is(AsmToken::Integer)) { |
| 3565 | IACol = getTok().getIntVal(); |
| 3566 | Lex(); |
| 3567 | } |
| 3568 | |
| 3569 | if (parseToken(AsmToken::EndOfStatement, |
| 3570 | "unexpected token in '.cv_inline_site_id' directive")) |
| 3571 | return true; |
| 3572 | |
| 3573 | if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, |
| 3574 | IALine, IACol, FunctionIdLoc)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3575 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3576 | |
| 3577 | return false; |
| 3578 | } |
| 3579 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3580 | /// parseDirectiveCVLoc |
| 3581 | /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] |
| 3582 | /// [is_stmt VALUE] |
| 3583 | /// The first number is a file number, must have been previously assigned with |
| 3584 | /// a .file directive, the second number is the line number and optionally the |
| 3585 | /// third number is a column position (zero if not specified). The remaining |
| 3586 | /// optional items are .loc sub-directives. |
| 3587 | bool AsmParser::parseDirectiveCVLoc() { |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3588 | SMLoc DirectiveLoc = getTok().getLoc(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3589 | SMLoc Loc; |
| 3590 | int64_t FunctionId, FileNumber; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3591 | if (parseCVFunctionId(FunctionId, ".cv_loc") || |
| 3592 | parseCVFileId(FileNumber, ".cv_loc")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3593 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3594 | |
| 3595 | int64_t LineNumber = 0; |
| 3596 | if (getLexer().is(AsmToken::Integer)) { |
| 3597 | LineNumber = getTok().getIntVal(); |
| 3598 | if (LineNumber < 0) |
| 3599 | return TokError("line number less than zero in '.cv_loc' directive"); |
| 3600 | Lex(); |
| 3601 | } |
| 3602 | |
| 3603 | int64_t ColumnPos = 0; |
| 3604 | if (getLexer().is(AsmToken::Integer)) { |
| 3605 | ColumnPos = getTok().getIntVal(); |
| 3606 | if (ColumnPos < 0) |
| 3607 | return TokError("column position less than zero in '.cv_loc' directive"); |
| 3608 | Lex(); |
| 3609 | } |
| 3610 | |
| 3611 | bool PrologueEnd = false; |
| 3612 | uint64_t IsStmt = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3613 | |
| 3614 | auto parseOp = [&]() -> bool { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3615 | StringRef Name; |
| 3616 | SMLoc Loc = getTok().getLoc(); |
| 3617 | if (parseIdentifier(Name)) |
| 3618 | return TokError("unexpected token in '.cv_loc' directive"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3619 | if (Name == "prologue_end") |
| 3620 | PrologueEnd = true; |
| 3621 | else if (Name == "is_stmt") { |
| 3622 | Loc = getTok().getLoc(); |
| 3623 | const MCExpr *Value; |
| 3624 | if (parseExpression(Value)) |
| 3625 | return true; |
| 3626 | // The expression must be the constant 0 or 1. |
| 3627 | IsStmt = ~0ULL; |
| 3628 | if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) |
| 3629 | IsStmt = MCE->getValue(); |
| 3630 | |
| 3631 | if (IsStmt > 1) |
| 3632 | return Error(Loc, "is_stmt value not 0 or 1"); |
| 3633 | } else { |
| 3634 | return Error(Loc, "unknown sub-directive in '.cv_loc' directive"); |
| 3635 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3636 | return false; |
| 3637 | }; |
| 3638 | |
| 3639 | if (parseMany(parseOp, false /*hasComma*/)) |
| 3640 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3641 | |
| 3642 | getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3643 | ColumnPos, PrologueEnd, IsStmt, StringRef(), |
| 3644 | DirectiveLoc); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3645 | return false; |
| 3646 | } |
| 3647 | |
| 3648 | /// parseDirectiveCVLinetable |
| 3649 | /// ::= .cv_linetable FunctionId, FnStart, FnEnd |
| 3650 | bool AsmParser::parseDirectiveCVLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3651 | int64_t FunctionId; |
| 3652 | StringRef FnStartName, FnEndName; |
| 3653 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3654 | if (parseCVFunctionId(FunctionId, ".cv_linetable") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3655 | parseToken(AsmToken::Comma, |
| 3656 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3657 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3658 | "expected identifier in directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3659 | parseToken(AsmToken::Comma, |
| 3660 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3661 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3662 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3663 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3664 | |
| 3665 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3666 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
| 3667 | |
| 3668 | getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym); |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3672 | /// parseDirectiveCVInlineLinetable |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3673 | /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3674 | bool AsmParser::parseDirectiveCVInlineLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3675 | int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; |
| 3676 | StringRef FnStartName, FnEndName; |
| 3677 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3678 | if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3679 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3680 | parseIntToken( |
| 3681 | SourceFileId, |
| 3682 | "expected SourceField in '.cv_inline_linetable' directive") || |
| 3683 | check(SourceFileId <= 0, Loc, |
| 3684 | "File id less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3685 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3686 | parseIntToken( |
| 3687 | SourceLineNum, |
| 3688 | "expected SourceLineNum in '.cv_inline_linetable' directive") || |
| 3689 | check(SourceLineNum < 0, Loc, |
| 3690 | "Line number less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3691 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3692 | "expected identifier in directive") || |
| 3693 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3694 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3695 | return true; |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3696 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3697 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3698 | return true; |
| 3699 | |
| 3700 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3701 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 3702 | getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, |
| 3703 | SourceLineNum, FnStartSym, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3704 | FnEndSym); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3705 | return false; |
| 3706 | } |
| 3707 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3708 | /// parseDirectiveCVDefRange |
| 3709 | /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* |
| 3710 | bool AsmParser::parseDirectiveCVDefRange() { |
| 3711 | SMLoc Loc; |
| 3712 | std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; |
| 3713 | while (getLexer().is(AsmToken::Identifier)) { |
| 3714 | Loc = getLexer().getLoc(); |
| 3715 | StringRef GapStartName; |
| 3716 | if (parseIdentifier(GapStartName)) |
| 3717 | return Error(Loc, "expected identifier in directive"); |
| 3718 | MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName); |
| 3719 | |
| 3720 | Loc = getLexer().getLoc(); |
| 3721 | StringRef GapEndName; |
| 3722 | if (parseIdentifier(GapEndName)) |
| 3723 | return Error(Loc, "expected identifier in directive"); |
| 3724 | MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName); |
| 3725 | |
| 3726 | Ranges.push_back({GapStartSym, GapEndSym}); |
| 3727 | } |
| 3728 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3729 | std::string FixedSizePortion; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3730 | if (parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3731 | parseEscapedString(FixedSizePortion)) |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3732 | return true; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3733 | |
| 3734 | getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion); |
| 3735 | return false; |
| 3736 | } |
| 3737 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3738 | /// parseDirectiveCVStringTable |
| 3739 | /// ::= .cv_stringtable |
| 3740 | bool AsmParser::parseDirectiveCVStringTable() { |
| 3741 | getStreamer().EmitCVStringTableDirective(); |
| 3742 | return false; |
| 3743 | } |
| 3744 | |
| 3745 | /// parseDirectiveCVFileChecksums |
| 3746 | /// ::= .cv_filechecksums |
| 3747 | bool AsmParser::parseDirectiveCVFileChecksums() { |
| 3748 | getStreamer().EmitCVFileChecksumsDirective(); |
| 3749 | return false; |
| 3750 | } |
| 3751 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3752 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3753 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3754 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3755 | StringRef Name; |
| 3756 | bool EH = false; |
| 3757 | bool Debug = false; |
| 3758 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3759 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3760 | return TokError("Expected an identifier"); |
| 3761 | |
| 3762 | if (Name == ".eh_frame") |
| 3763 | EH = true; |
| 3764 | else if (Name == ".debug_frame") |
| 3765 | Debug = true; |
| 3766 | |
| 3767 | if (getLexer().is(AsmToken::Comma)) { |
| 3768 | Lex(); |
| 3769 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3770 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3771 | return TokError("Expected an identifier"); |
| 3772 | |
| 3773 | if (Name == ".eh_frame") |
| 3774 | EH = true; |
| 3775 | else if (Name == ".debug_frame") |
| 3776 | Debug = true; |
| 3777 | } |
| 3778 | |
| 3779 | getStreamer().EmitCFISections(EH, Debug); |
| 3780 | return false; |
| 3781 | } |
| 3782 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3783 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3784 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3785 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3786 | StringRef Simple; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3787 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3788 | if (check(parseIdentifier(Simple) || Simple != "simple", |
| 3789 | "unexpected token") || |
| 3790 | parseToken(AsmToken::EndOfStatement)) |
| 3791 | return addErrorSuffix(" in '.cfi_startproc' directive"); |
| 3792 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3793 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 3794 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3795 | return false; |
| 3796 | } |
| 3797 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3798 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3799 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3800 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3801 | getStreamer().EmitCFIEndProc(); |
| 3802 | return false; |
| 3803 | } |
| 3804 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3805 | /// \brief parse register name or number. |
| 3806 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3807 | SMLoc DirectiveLoc) { |
| 3808 | unsigned RegNo; |
| 3809 | |
| 3810 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3811 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3812 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3813 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3814 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3815 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3816 | |
| 3817 | return false; |
| 3818 | } |
| 3819 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3820 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3821 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3822 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3823 | int64_t Register = 0, Offset = 0; |
| 3824 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3825 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3826 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3827 | return true; |
| 3828 | |
| 3829 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3830 | return false; |
| 3831 | } |
| 3832 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3833 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3834 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3835 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3836 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3837 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3838 | return true; |
| 3839 | |
| 3840 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3841 | return false; |
| 3842 | } |
| 3843 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3844 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3845 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3846 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3847 | int64_t Register1 = 0, Register2 = 0; |
| 3848 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || |
| 3849 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3850 | parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3851 | return true; |
| 3852 | |
| 3853 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3854 | return false; |
| 3855 | } |
| 3856 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3857 | /// parseDirectiveCFIWindowSave |
| 3858 | /// ::= .cfi_window_save |
| 3859 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3860 | getStreamer().EmitCFIWindowSave(); |
| 3861 | return false; |
| 3862 | } |
| 3863 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3864 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3865 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3866 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3867 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3868 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3869 | return true; |
| 3870 | |
| 3871 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3872 | return false; |
| 3873 | } |
| 3874 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3875 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3876 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3877 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3878 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3879 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3880 | return true; |
| 3881 | |
| 3882 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3883 | return false; |
| 3884 | } |
| 3885 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3886 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3887 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3888 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3889 | int64_t Register = 0; |
| 3890 | int64_t Offset = 0; |
| 3891 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3892 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3893 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3894 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3895 | return true; |
| 3896 | |
| 3897 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3898 | return false; |
| 3899 | } |
| 3900 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3901 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3902 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3903 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3904 | int64_t Register = 0, Offset = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3905 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3906 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3907 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3908 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3909 | return true; |
| 3910 | |
| 3911 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3912 | return false; |
| 3913 | } |
| 3914 | |
| 3915 | static bool isValidEncoding(int64_t Encoding) { |
| 3916 | if (Encoding & ~0xff) |
| 3917 | return false; |
| 3918 | |
| 3919 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3920 | return true; |
| 3921 | |
| 3922 | const unsigned Format = Encoding & 0xf; |
| 3923 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3924 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3925 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3926 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3927 | return false; |
| 3928 | |
| 3929 | const unsigned Application = Encoding & 0x70; |
| 3930 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3931 | Application != dwarf::DW_EH_PE_pcrel) |
| 3932 | return false; |
| 3933 | |
| 3934 | return true; |
| 3935 | } |
| 3936 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3937 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3938 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3939 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3940 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3941 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3942 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3943 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3944 | return true; |
| 3945 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3946 | return false; |
| 3947 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3948 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3949 | if (check(!isValidEncoding(Encoding), "unsupported encoding.") || |
| 3950 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3951 | check(parseIdentifier(Name), "expected identifier in directive")) |
| 3952 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3953 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3954 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3955 | |
| 3956 | if (IsPersonality) |
| 3957 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3958 | else |
| 3959 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3960 | return false; |
| 3961 | } |
| 3962 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3963 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3964 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3965 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3966 | getStreamer().EmitCFIRememberState(); |
| 3967 | return false; |
| 3968 | } |
| 3969 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3970 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3971 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3972 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3973 | getStreamer().EmitCFIRestoreState(); |
| 3974 | return false; |
| 3975 | } |
| 3976 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3977 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3978 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3979 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3980 | int64_t Register = 0; |
| 3981 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3982 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3983 | return true; |
| 3984 | |
| 3985 | getStreamer().EmitCFISameValue(Register); |
| 3986 | return false; |
| 3987 | } |
| 3988 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3989 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3990 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3991 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3992 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3993 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3994 | return true; |
| 3995 | |
| 3996 | getStreamer().EmitCFIRestore(Register); |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4000 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4001 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4002 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4003 | std::string Values; |
| 4004 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4005 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4006 | return true; |
| 4007 | |
| 4008 | Values.push_back((uint8_t)CurrValue); |
| 4009 | |
| 4010 | while (getLexer().is(AsmToken::Comma)) { |
| 4011 | Lex(); |
| 4012 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4013 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4014 | return true; |
| 4015 | |
| 4016 | Values.push_back((uint8_t)CurrValue); |
| 4017 | } |
| 4018 | |
| 4019 | getStreamer().EmitCFIEscape(Values); |
| 4020 | return false; |
| 4021 | } |
| 4022 | |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 4023 | /// parseDirectiveCFIReturnColumn |
| 4024 | /// ::= .cfi_return_column register |
| 4025 | bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { |
| 4026 | int64_t Register = 0; |
| 4027 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 4028 | return true; |
| 4029 | getStreamer().EmitCFIReturnColumn(Register); |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4033 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4034 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4035 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4036 | if (parseToken(AsmToken::EndOfStatement, |
| 4037 | "unexpected token in '.cfi_signal_frame'")) |
| 4038 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4039 | |
| 4040 | getStreamer().EmitCFISignalFrame(); |
| 4041 | return false; |
| 4042 | } |
| 4043 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4044 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4045 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4046 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4047 | int64_t Register = 0; |
| 4048 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4049 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4050 | return true; |
| 4051 | |
| 4052 | getStreamer().EmitCFIUndefined(Register); |
| 4053 | return false; |
| 4054 | } |
| 4055 | |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4056 | /// parseDirectiveAltmacro |
| 4057 | /// ::= .altmacro |
| 4058 | /// ::= .noaltmacro |
| 4059 | bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { |
| 4060 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4061 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4062 | if (Directive == ".altmacro") |
| 4063 | getLexer().SetAltMacroMode(true); |
| 4064 | else |
| 4065 | getLexer().SetAltMacroMode(false); |
| 4066 | return false; |
| 4067 | } |
| 4068 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4069 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4070 | /// ::= .macros_on |
| 4071 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4072 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4073 | if (parseToken(AsmToken::EndOfStatement, |
| 4074 | "unexpected token in '" + Directive + "' directive")) |
| 4075 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4076 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4077 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4078 | return false; |
| 4079 | } |
| 4080 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4081 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4082 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4083 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4084 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4085 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4086 | return TokError("expected identifier in '.macro' directive"); |
| 4087 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4088 | if (getLexer().is(AsmToken::Comma)) |
| 4089 | Lex(); |
| 4090 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4091 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4092 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4093 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 4094 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4095 | return Error(Lexer.getLoc(), |
| 4096 | "Vararg parameter '" + Parameters.back().Name + |
| 4097 | "' should be last one in the list of parameters."); |
| 4098 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4099 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4100 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4101 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4102 | |
Coby Tayree | bedaae0 | 2017-04-08 20:29:03 +0000 | [diff] [blame] | 4103 | // Emit an error if two (or more) named parameters share the same name |
| 4104 | for (const MCAsmMacroParameter& CurrParam : Parameters) |
| 4105 | if (CurrParam.Name.equals(Parameter.Name)) |
| 4106 | return TokError("macro '" + Name + "' has multiple parameters" |
| 4107 | " named '" + Parameter.Name + "'"); |
| 4108 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4109 | if (Lexer.is(AsmToken::Colon)) { |
| 4110 | Lex(); // consume ':' |
| 4111 | |
| 4112 | SMLoc QualLoc; |
| 4113 | StringRef Qualifier; |
| 4114 | |
| 4115 | QualLoc = Lexer.getLoc(); |
| 4116 | if (parseIdentifier(Qualifier)) |
| 4117 | return Error(QualLoc, "missing parameter qualifier for " |
| 4118 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4119 | |
| 4120 | if (Qualifier == "req") |
| 4121 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 4122 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4123 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4124 | else |
| 4125 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 4126 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4127 | } |
| 4128 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4129 | if (getLexer().is(AsmToken::Equal)) { |
| 4130 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4131 | |
| 4132 | SMLoc ParamLoc; |
| 4133 | |
| 4134 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4135 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4136 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4137 | |
| 4138 | if (Parameter.Required) |
| 4139 | Warning(ParamLoc, "pointless default value for required parameter " |
| 4140 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4141 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4142 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4143 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4144 | |
| 4145 | if (getLexer().is(AsmToken::Comma)) |
| 4146 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4149 | // Eat just the end of statement. |
| 4150 | Lexer.Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4151 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4152 | // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4153 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4154 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4155 | // Lex the macro definition. |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 4156 | while (true) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4157 | // Ignore Lexing errors in macros. |
| 4158 | while (Lexer.is(AsmToken::Error)) { |
| 4159 | Lexer.Lex(); |
| 4160 | } |
| 4161 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4162 | // Check whether we have reached the end of the file. |
| 4163 | if (getLexer().is(AsmToken::Eof)) |
| 4164 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 4165 | |
| 4166 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4167 | if (getLexer().is(AsmToken::Identifier)) { |
| 4168 | if (getTok().getIdentifier() == ".endm" || |
| 4169 | getTok().getIdentifier() == ".endmacro") { |
| 4170 | if (MacroDepth == 0) { // Outermost macro. |
| 4171 | EndToken = getTok(); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4172 | Lexer.Lex(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4173 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4174 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 4175 | "' directive"); |
| 4176 | break; |
| 4177 | } else { |
| 4178 | // Otherwise we just found the end of an inner macro. |
| 4179 | --MacroDepth; |
| 4180 | } |
| 4181 | } else if (getTok().getIdentifier() == ".macro") { |
| 4182 | // We allow nested macros. Those aren't instantiated until the outermost |
| 4183 | // macro is expanded so just ignore them for now. |
| 4184 | ++MacroDepth; |
| 4185 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4189 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4192 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4193 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 4194 | } |
| 4195 | |
| 4196 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4197 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4198 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4199 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4200 | defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters))); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4201 | return false; |
| 4202 | } |
| 4203 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4204 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4205 | /// |
| 4206 | /// With the support added for named parameters there may be code out there that |
| 4207 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4208 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4209 | /// 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] | 4210 | /// 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] | 4211 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 4212 | /// warning that the positional parameter found in body which have no effect. |
| 4213 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4214 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4215 | /// intended or change the macro to use the named parameters. It is possible |
| 4216 | /// this warning will trigger when the none of the named parameters are used |
| 4217 | /// 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] | 4218 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4219 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4220 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4221 | // If this macro is not defined with named parameters the warning we are |
| 4222 | // checking for here doesn't apply. |
| 4223 | unsigned NParameters = Parameters.size(); |
| 4224 | if (NParameters == 0) |
| 4225 | return; |
| 4226 | |
| 4227 | bool NamedParametersFound = false; |
| 4228 | bool PositionalParametersFound = false; |
| 4229 | |
| 4230 | // Look at the body of the macro for use of both the named parameters and what |
| 4231 | // are likely to be positional parameters. This is what expandMacro() is |
| 4232 | // doing when it finds the parameters in the body. |
| 4233 | while (!Body.empty()) { |
| 4234 | // Scan for the next possible parameter. |
| 4235 | std::size_t End = Body.size(), Pos = 0; |
| 4236 | for (; Pos != End; ++Pos) { |
| 4237 | // Check for a substitution or escape. |
| 4238 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 4239 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 4240 | break; |
| 4241 | |
| 4242 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 4243 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 4244 | continue; |
| 4245 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 4246 | if (Next == '$' || Next == 'n' || |
| 4247 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4248 | break; |
| 4249 | } |
| 4250 | |
| 4251 | // Check if we reached the end. |
| 4252 | if (Pos == End) |
| 4253 | break; |
| 4254 | |
| 4255 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4256 | switch (Body[Pos + 1]) { |
| 4257 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4258 | case '$': |
| 4259 | break; |
| 4260 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4261 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4262 | case 'n': |
| 4263 | PositionalParametersFound = true; |
| 4264 | break; |
| 4265 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4266 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4267 | default: { |
| 4268 | PositionalParametersFound = true; |
| 4269 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4270 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4271 | } |
| 4272 | Pos += 2; |
| 4273 | } else { |
| 4274 | unsigned I = Pos + 1; |
| 4275 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 4276 | ++I; |
| 4277 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4278 | const char *Begin = Body.data() + Pos + 1; |
| 4279 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4280 | unsigned Index = 0; |
| 4281 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4282 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4283 | break; |
| 4284 | |
| 4285 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4286 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 4287 | Pos += 3; |
| 4288 | else { |
| 4289 | Pos = I; |
| 4290 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4291 | } else { |
| 4292 | NamedParametersFound = true; |
| 4293 | Pos += 1 + Argument.size(); |
| 4294 | } |
| 4295 | } |
| 4296 | // Update the scan point. |
| 4297 | Body = Body.substr(Pos); |
| 4298 | } |
| 4299 | |
| 4300 | if (!NamedParametersFound && PositionalParametersFound) |
| 4301 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 4302 | "used in macro body, possible positional parameter " |
| 4303 | "found in body which will have no effect"); |
| 4304 | } |
| 4305 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4306 | /// parseDirectiveExitMacro |
| 4307 | /// ::= .exitm |
| 4308 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4309 | if (parseToken(AsmToken::EndOfStatement, |
| 4310 | "unexpected token in '" + Directive + "' directive")) |
| 4311 | return true; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4312 | |
| 4313 | if (!isInsideMacroInstantiation()) |
| 4314 | return TokError("unexpected '" + Directive + "' in file, " |
| 4315 | "no current macro definition"); |
| 4316 | |
| 4317 | // Exit all conditionals that are active in the current macro. |
| 4318 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 4319 | TheCondState = TheCondStack.back(); |
| 4320 | TheCondStack.pop_back(); |
| 4321 | } |
| 4322 | |
| 4323 | handleMacroExit(); |
| 4324 | return false; |
| 4325 | } |
| 4326 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4327 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4328 | /// ::= .endm |
| 4329 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4330 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4331 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4332 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4333 | |
| 4334 | // If we are inside a macro instantiation, terminate the current |
| 4335 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4336 | if (isInsideMacroInstantiation()) { |
| 4337 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4338 | return false; |
| 4339 | } |
| 4340 | |
| 4341 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 4342 | // .endmacro directives are handled during the macro definition parsing. |
| 4343 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4344 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4347 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4348 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4349 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4350 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4351 | SMLoc Loc; |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 4352 | if (parseTokenLoc(Loc) || |
| 4353 | check(parseIdentifier(Name), Loc, |
| 4354 | "expected identifier in '.purgem' directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4355 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4356 | "unexpected token in '.purgem' directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4357 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4358 | |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4359 | if (!lookupMacro(Name)) |
| 4360 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 4361 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4362 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4363 | return false; |
| 4364 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4365 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4366 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4367 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4368 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4369 | // Expect a single argument: an expression that evaluates to a constant |
| 4370 | // in the inclusive range 0-30. |
| 4371 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4372 | int64_t AlignSizePow2; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4373 | if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4374 | parseToken(AsmToken::EndOfStatement, "unexpected token after expression " |
| 4375 | "in '.bundle_align_mode' " |
| 4376 | "directive") || |
| 4377 | check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc, |
| 4378 | "invalid bundle alignment size (expected between 0 and 30)")) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4379 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4380 | |
| 4381 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 4382 | // unsigned. |
| 4383 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4387 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4388 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4389 | bool AsmParser::parseDirectiveBundleLock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4390 | if (checkForValidSection()) |
| 4391 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4392 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4393 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4394 | StringRef Option; |
| 4395 | SMLoc Loc = getTok().getLoc(); |
| 4396 | const char *kInvalidOptionError = |
| 4397 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4398 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4399 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4400 | if (check(parseIdentifier(Option), Loc, kInvalidOptionError) || |
| 4401 | check(Option != "align_to_end", Loc, kInvalidOptionError) || |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4402 | parseToken(AsmToken::EndOfStatement, |
| 4403 | "unexpected token after '.bundle_lock' directive option")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4404 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4405 | AlignToEnd = true; |
| 4406 | } |
| 4407 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4408 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4409 | return false; |
| 4410 | } |
| 4411 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4412 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4413 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4414 | bool AsmParser::parseDirectiveBundleUnlock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4415 | if (checkForValidSection() || |
| 4416 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4417 | "unexpected token in '.bundle_unlock' directive")) |
| 4418 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4419 | |
| 4420 | getStreamer().EmitBundleUnlock(); |
| 4421 | return false; |
| 4422 | } |
| 4423 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4424 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4425 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4426 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 4427 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 4428 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4429 | if (checkForValidSection() || parseExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4430 | return true; |
| 4431 | |
| 4432 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4433 | if (parseOptionalToken(AsmToken::Comma)) |
| 4434 | if (parseAbsoluteExpression(FillExpr)) |
| 4435 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
| 4436 | if (parseToken(AsmToken::EndOfStatement)) |
| 4437 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4438 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4439 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 4440 | getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4441 | |
| 4442 | return false; |
| 4443 | } |
| 4444 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4445 | /// parseDirectiveDCB |
| 4446 | /// ::= .dcb.{b, l, w} expression, expression |
| 4447 | bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4448 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4449 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4450 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4451 | return true; |
| 4452 | |
| 4453 | if (NumValues < 0) { |
| 4454 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4455 | return false; |
| 4456 | } |
| 4457 | |
| 4458 | if (parseToken(AsmToken::Comma, |
| 4459 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4460 | return true; |
| 4461 | |
| 4462 | const MCExpr *Value; |
| 4463 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4464 | if (parseExpression(Value)) |
| 4465 | return true; |
| 4466 | |
| 4467 | // Special case constant expressions to match code generator. |
| 4468 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 4469 | assert(Size <= 8 && "Invalid size"); |
| 4470 | uint64_t IntValue = MCE->getValue(); |
| 4471 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 4472 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4473 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4474 | getStreamer().EmitIntValue(IntValue, Size); |
| 4475 | } else { |
| 4476 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4477 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 4478 | } |
| 4479 | |
| 4480 | if (parseToken(AsmToken::EndOfStatement, |
| 4481 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4482 | return true; |
| 4483 | |
| 4484 | return false; |
| 4485 | } |
| 4486 | |
| 4487 | /// parseDirectiveRealDCB |
| 4488 | /// ::= .dcb.{d, s} expression, expression |
| 4489 | bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4490 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4491 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4492 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4493 | return true; |
| 4494 | |
| 4495 | if (NumValues < 0) { |
| 4496 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4497 | return false; |
| 4498 | } |
| 4499 | |
| 4500 | if (parseToken(AsmToken::Comma, |
| 4501 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4502 | return true; |
| 4503 | |
| 4504 | APInt AsInt; |
| 4505 | if (parseRealValue(Semantics, AsInt)) |
| 4506 | return true; |
| 4507 | |
| 4508 | if (parseToken(AsmToken::EndOfStatement, |
| 4509 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4510 | return true; |
| 4511 | |
| 4512 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4513 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 4514 | AsInt.getBitWidth() / 8); |
| 4515 | |
| 4516 | return false; |
| 4517 | } |
| 4518 | |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4519 | /// parseDirectiveDS |
| 4520 | /// ::= .ds.{b, d, l, p, s, w, x} expression |
| 4521 | bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4522 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4523 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4524 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4525 | return true; |
| 4526 | |
| 4527 | if (NumValues < 0) { |
| 4528 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4529 | return false; |
| 4530 | } |
| 4531 | |
| 4532 | if (parseToken(AsmToken::EndOfStatement, |
| 4533 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4534 | return true; |
| 4535 | |
| 4536 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4537 | getStreamer().emitFill(Size, 0); |
| 4538 | |
| 4539 | return false; |
| 4540 | } |
| 4541 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4542 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4543 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4544 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4545 | if (checkForValidSection()) |
| 4546 | return true; |
| 4547 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4548 | auto parseOp = [&]() -> bool { |
| 4549 | const MCExpr *Value; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4550 | if (parseExpression(Value)) |
| 4551 | return true; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4552 | if (Signed) |
| 4553 | getStreamer().EmitSLEB128Value(Value); |
| 4554 | else |
| 4555 | getStreamer().EmitULEB128Value(Value); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4556 | return false; |
| 4557 | }; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4558 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4559 | if (parseMany(parseOp)) |
| 4560 | return addErrorSuffix(" in directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4561 | |
| 4562 | return false; |
| 4563 | } |
| 4564 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4565 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4566 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4567 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4568 | auto parseOp = [&]() -> bool { |
| 4569 | StringRef Name; |
| 4570 | SMLoc Loc = getTok().getLoc(); |
| 4571 | if (parseIdentifier(Name)) |
| 4572 | return Error(Loc, "expected identifier"); |
| 4573 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4574 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4575 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 4576 | if (Sym->isTemporary()) |
| 4577 | return Error(Loc, "non-local symbol required"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4578 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4579 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 4580 | return Error(Loc, "unable to emit symbol attribute"); |
| 4581 | return false; |
| 4582 | }; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4583 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4584 | if (parseMany(parseOp)) |
| 4585 | return addErrorSuffix(" in directive"); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 4586 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4587 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4588 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4589 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4590 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4591 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4592 | if (checkForValidSection()) |
| 4593 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 4594 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4595 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4596 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4597 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4598 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4599 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 4600 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4601 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4602 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4603 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4604 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4605 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4606 | |
| 4607 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4608 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4609 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4610 | return true; |
| 4611 | |
| 4612 | int64_t Pow2Alignment = 0; |
| 4613 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4614 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4615 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4616 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4617 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4618 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4619 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4620 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 4621 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4622 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 4623 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4624 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4625 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 4626 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4627 | if (!isPowerOf2_64(Pow2Alignment)) |
| 4628 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 4629 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 4630 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4631 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4632 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4633 | if (parseToken(AsmToken::EndOfStatement, |
| 4634 | "unexpected token in '.comm' or '.lcomm' directive")) |
| 4635 | return true; |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4636 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4637 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 4638 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4639 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4640 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4641 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4642 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 4643 | // 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] | 4644 | // may internally end up wanting an alignment in bytes. |
| 4645 | // FIXME: Diagnose overflow. |
| 4646 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4647 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4648 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4649 | |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 4650 | Sym->redefineIfPossible(); |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 4651 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4652 | return Error(IDLoc, "invalid symbol redefinition"); |
| 4653 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4654 | // 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] | 4655 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4656 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4659 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4660 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4661 | return false; |
| 4662 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 4663 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4664 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4665 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4666 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4667 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4668 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4669 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4670 | StringRef Str = parseStringToEndOfStatement(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4671 | if (parseToken(AsmToken::EndOfStatement, |
| 4672 | "unexpected token in '.abort' directive")) |
| 4673 | return true; |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4674 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4675 | if (Str.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4676 | return Error(Loc, ".abort detected. Assembly stopping."); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4677 | else |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4678 | return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4679 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4680 | |
| 4681 | return false; |
| 4682 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 4683 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4684 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4685 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4686 | bool AsmParser::parseDirectiveInclude() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4687 | // Allow the strings to have escaped octal character sequence. |
| 4688 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4689 | SMLoc IncludeLoc = getTok().getLoc(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4690 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4691 | if (check(getTok().isNot(AsmToken::String), |
| 4692 | "expected string in '.include' directive") || |
| 4693 | parseEscapedString(Filename) || |
| 4694 | check(getTok().isNot(AsmToken::EndOfStatement), |
| 4695 | "unexpected token in '.include' directive") || |
| 4696 | // Attempt to switch the lexer to the included file before consuming the |
| 4697 | // end of statement to avoid losing it when we switch. |
| 4698 | check(enterIncludeFile(Filename), IncludeLoc, |
| 4699 | "Could not find include file '" + Filename + "'")) |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 4700 | return true; |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4701 | |
| 4702 | return false; |
| 4703 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 4704 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4705 | /// parseDirectiveIncbin |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4706 | /// ::= .incbin "filename" [ , skip [ , count ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4707 | bool AsmParser::parseDirectiveIncbin() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4708 | // Allow the strings to have escaped octal character sequence. |
| 4709 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4710 | SMLoc IncbinLoc = getTok().getLoc(); |
| 4711 | if (check(getTok().isNot(AsmToken::String), |
| 4712 | "expected string in '.incbin' directive") || |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4713 | parseEscapedString(Filename)) |
| 4714 | return true; |
| 4715 | |
| 4716 | int64_t Skip = 0; |
| 4717 | const MCExpr *Count = nullptr; |
| 4718 | SMLoc SkipLoc, CountLoc; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4719 | if (parseOptionalToken(AsmToken::Comma)) { |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4720 | // The skip expression can be omitted while specifying the count, e.g: |
| 4721 | // .incbin "filename",,4 |
| 4722 | if (getTok().isNot(AsmToken::Comma)) { |
| 4723 | if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip)) |
| 4724 | return true; |
| 4725 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4726 | if (parseOptionalToken(AsmToken::Comma)) { |
| 4727 | CountLoc = getTok().getLoc(); |
| 4728 | if (parseExpression(Count)) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4729 | return true; |
| 4730 | } |
| 4731 | } |
| 4732 | |
| 4733 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4734 | "unexpected token in '.incbin' directive")) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4735 | return true; |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4736 | |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4737 | if (check(Skip < 0, SkipLoc, "skip is negative")) |
| 4738 | return true; |
| 4739 | |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4740 | // Attempt to process the included file. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4741 | if (processIncbinFile(Filename, Skip, Count, CountLoc)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4742 | return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 4743 | return false; |
| 4744 | } |
| 4745 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4746 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4747 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 4748 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4749 | TheCondStack.push_back(TheCondState); |
| 4750 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4751 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4752 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4753 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4754 | int64_t ExprValue; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4755 | if (parseAbsoluteExpression(ExprValue) || |
| 4756 | parseToken(AsmToken::EndOfStatement, |
| 4757 | "unexpected token in '.if' directive")) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4758 | return true; |
| 4759 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4760 | switch (DirKind) { |
| 4761 | default: |
| 4762 | llvm_unreachable("unsupported directive"); |
| 4763 | case DK_IF: |
| 4764 | case DK_IFNE: |
| 4765 | break; |
| 4766 | case DK_IFEQ: |
| 4767 | ExprValue = ExprValue == 0; |
| 4768 | break; |
| 4769 | case DK_IFGE: |
| 4770 | ExprValue = ExprValue >= 0; |
| 4771 | break; |
| 4772 | case DK_IFGT: |
| 4773 | ExprValue = ExprValue > 0; |
| 4774 | break; |
| 4775 | case DK_IFLE: |
| 4776 | ExprValue = ExprValue <= 0; |
| 4777 | break; |
| 4778 | case DK_IFLT: |
| 4779 | ExprValue = ExprValue < 0; |
| 4780 | break; |
| 4781 | } |
| 4782 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4783 | TheCondState.CondMet = ExprValue; |
| 4784 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4785 | } |
| 4786 | |
| 4787 | return false; |
| 4788 | } |
| 4789 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4790 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4791 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4792 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4793 | TheCondStack.push_back(TheCondState); |
| 4794 | TheCondState.TheCond = AsmCond::IfCond; |
| 4795 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4796 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4797 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4798 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4799 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4800 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4801 | if (parseToken(AsmToken::EndOfStatement, |
| 4802 | "unexpected token in '.ifb' directive")) |
| 4803 | return true; |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4804 | |
| 4805 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 4806 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4807 | } |
| 4808 | |
| 4809 | return false; |
| 4810 | } |
| 4811 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4812 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4813 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4814 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4815 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4816 | TheCondStack.push_back(TheCondState); |
| 4817 | TheCondState.TheCond = AsmCond::IfCond; |
| 4818 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4819 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4820 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4821 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4822 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4823 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4824 | if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive")) |
| 4825 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4826 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4827 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4828 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4829 | if (parseToken(AsmToken::EndOfStatement, |
| 4830 | "unexpected token in '.ifc' directive")) |
| 4831 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4832 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4833 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4834 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4835 | } |
| 4836 | |
| 4837 | return false; |
| 4838 | } |
| 4839 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4840 | /// parseDirectiveIfeqs |
| 4841 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4842 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4843 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4844 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4845 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 4846 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | StringRef String1 = getTok().getStringContents(); |
| 4850 | Lex(); |
| 4851 | |
| 4852 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4853 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4854 | return TokError( |
| 4855 | "expected comma after first string for '.ifeqs' directive"); |
| 4856 | return TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | Lex(); |
| 4860 | |
| 4861 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4862 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4863 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 4864 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | StringRef String2 = getTok().getStringContents(); |
| 4868 | Lex(); |
| 4869 | |
| 4870 | TheCondStack.push_back(TheCondState); |
| 4871 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4872 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4873 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4874 | |
| 4875 | return false; |
| 4876 | } |
| 4877 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4878 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4879 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4880 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4881 | StringRef Name; |
| 4882 | TheCondStack.push_back(TheCondState); |
| 4883 | TheCondState.TheCond = AsmCond::IfCond; |
| 4884 | |
| 4885 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4886 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4887 | } else { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4888 | if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") || |
| 4889 | parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'")) |
| 4890 | return true; |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4891 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4892 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4895 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4896 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4897 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4898 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4899 | } |
| 4900 | |
| 4901 | return false; |
| 4902 | } |
| 4903 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4904 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4905 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4906 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4907 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4908 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4909 | return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an" |
| 4910 | " .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4911 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4912 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4913 | bool LastIgnoreState = false; |
| 4914 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4915 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4916 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4917 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4918 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4919 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4920 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4921 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4922 | return true; |
| 4923 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4924 | if (parseToken(AsmToken::EndOfStatement, |
| 4925 | "unexpected token in '.elseif' directive")) |
| 4926 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4927 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4928 | TheCondState.CondMet = ExprValue; |
| 4929 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4930 | } |
| 4931 | |
| 4932 | return false; |
| 4933 | } |
| 4934 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4935 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4936 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4937 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4938 | if (parseToken(AsmToken::EndOfStatement, |
| 4939 | "unexpected token in '.else' directive")) |
| 4940 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4941 | |
| 4942 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4943 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4944 | return Error(DirectiveLoc, "Encountered a .else that doesn't follow " |
| 4945 | " an .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4946 | TheCondState.TheCond = AsmCond::ElseCond; |
| 4947 | bool LastIgnoreState = false; |
| 4948 | if (!TheCondStack.empty()) |
| 4949 | LastIgnoreState = TheCondStack.back().Ignore; |
| 4950 | if (LastIgnoreState || TheCondState.CondMet) |
| 4951 | TheCondState.Ignore = true; |
| 4952 | else |
| 4953 | TheCondState.Ignore = false; |
| 4954 | |
| 4955 | return false; |
| 4956 | } |
| 4957 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4958 | /// parseDirectiveEnd |
| 4959 | /// ::= .end |
| 4960 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4961 | if (parseToken(AsmToken::EndOfStatement, |
| 4962 | "unexpected token in '.end' directive")) |
| 4963 | return true; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4964 | |
| 4965 | while (Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4966 | Lexer.Lex(); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4967 | |
| 4968 | return false; |
| 4969 | } |
| 4970 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4971 | /// parseDirectiveError |
| 4972 | /// ::= .err |
| 4973 | /// ::= .error [string] |
| 4974 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 4975 | if (!TheCondStack.empty()) { |
| 4976 | if (TheCondStack.back().Ignore) { |
| 4977 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4978 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | if (!WithMessage) |
| 4983 | return Error(L, ".err encountered"); |
| 4984 | |
| 4985 | StringRef Message = ".error directive invoked in source file"; |
| 4986 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4987 | if (Lexer.isNot(AsmToken::String)) |
| 4988 | return TokError(".error argument must be a string"); |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4989 | |
| 4990 | Message = getTok().getStringContents(); |
| 4991 | Lex(); |
| 4992 | } |
| 4993 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4994 | return Error(L, Message); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4997 | /// parseDirectiveWarning |
| 4998 | /// ::= .warning [string] |
| 4999 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 5000 | if (!TheCondStack.empty()) { |
| 5001 | if (TheCondStack.back().Ignore) { |
| 5002 | eatToEndOfStatement(); |
| 5003 | return false; |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | StringRef Message = ".warning directive invoked in source file"; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5008 | |
| 5009 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5010 | if (Lexer.isNot(AsmToken::String)) |
| 5011 | return TokError(".warning argument must be a string"); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5012 | |
| 5013 | Message = getTok().getStringContents(); |
| 5014 | Lex(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5015 | if (parseToken(AsmToken::EndOfStatement, |
| 5016 | "expected end of statement in '.warning' directive")) |
| 5017 | return true; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5020 | return Warning(L, Message); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5023 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5024 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5025 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5026 | if (parseToken(AsmToken::EndOfStatement, |
| 5027 | "unexpected token in '.endif' directive")) |
| 5028 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5029 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5030 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5031 | return Error(DirectiveLoc, "Encountered a .endif that doesn't follow " |
| 5032 | "an .if or .else"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5033 | if (!TheCondStack.empty()) { |
| 5034 | TheCondState = TheCondStack.back(); |
| 5035 | TheCondStack.pop_back(); |
| 5036 | } |
| 5037 | |
| 5038 | return false; |
| 5039 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 5040 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5041 | void AsmParser::initializeDirectiveKindMap() { |
| 5042 | DirectiveKindMap[".set"] = DK_SET; |
| 5043 | DirectiveKindMap[".equ"] = DK_EQU; |
| 5044 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 5045 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 5046 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 5047 | DirectiveKindMap[".string"] = DK_STRING; |
| 5048 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 5049 | DirectiveKindMap[".short"] = DK_SHORT; |
| 5050 | DirectiveKindMap[".value"] = DK_VALUE; |
| 5051 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 5052 | DirectiveKindMap[".long"] = DK_LONG; |
| 5053 | DirectiveKindMap[".int"] = DK_INT; |
| 5054 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 5055 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 5056 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 5057 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5058 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 5059 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 5060 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 5061 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 5062 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 5063 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 5064 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 5065 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 5066 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 5067 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 5068 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 5069 | DirectiveKindMap[".org"] = DK_ORG; |
| 5070 | DirectiveKindMap[".fill"] = DK_FILL; |
| 5071 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 5072 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 5073 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 5074 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5075 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 5076 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 5077 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 5078 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 5079 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 5080 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 5081 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 5082 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 5083 | DirectiveKindMap[".comm"] = DK_COMM; |
| 5084 | DirectiveKindMap[".common"] = DK_COMMON; |
| 5085 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 5086 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 5087 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 5088 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 5089 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 5090 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 5091 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5092 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5093 | DirectiveKindMap[".irp"] = DK_IRP; |
| 5094 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 5095 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 5096 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 5097 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 5098 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 5099 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5100 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 5101 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 5102 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 5103 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 5104 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 5105 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5106 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 5107 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 5108 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5109 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5110 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5111 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5112 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 5113 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 5114 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 5115 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 5116 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5117 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5118 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 5119 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 5120 | DirectiveKindMap[".space"] = DK_SPACE; |
| 5121 | DirectiveKindMap[".file"] = DK_FILE; |
| 5122 | DirectiveKindMap[".line"] = DK_LINE; |
| 5123 | DirectiveKindMap[".loc"] = DK_LOC; |
| 5124 | DirectiveKindMap[".stabs"] = DK_STABS; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5125 | DirectiveKindMap[".cv_file"] = DK_CV_FILE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5126 | DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5127 | DirectiveKindMap[".cv_loc"] = DK_CV_LOC; |
| 5128 | DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 5129 | DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5130 | DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 5131 | DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5132 | DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE; |
| 5133 | DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5134 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 5135 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 5136 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 5137 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 5138 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 5139 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 5140 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 5141 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 5142 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 5143 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 5144 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 5145 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 5146 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 5147 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 5148 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 5149 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 5150 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 5151 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 5152 | DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5153 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 5154 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 5155 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 5156 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5157 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 5158 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 5159 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 5160 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5161 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 5162 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 5163 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5164 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5165 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5166 | DirectiveKindMap[".warning"] = DK_WARNING; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 5167 | DirectiveKindMap[".altmacro"] = DK_ALTMACRO; |
| 5168 | DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 5169 | DirectiveKindMap[".reloc"] = DK_RELOC; |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 5170 | DirectiveKindMap[".dc"] = DK_DC; |
| 5171 | DirectiveKindMap[".dc.a"] = DK_DC_A; |
| 5172 | DirectiveKindMap[".dc.b"] = DK_DC_B; |
| 5173 | DirectiveKindMap[".dc.d"] = DK_DC_D; |
| 5174 | DirectiveKindMap[".dc.l"] = DK_DC_L; |
| 5175 | DirectiveKindMap[".dc.s"] = DK_DC_S; |
| 5176 | DirectiveKindMap[".dc.w"] = DK_DC_W; |
| 5177 | DirectiveKindMap[".dc.x"] = DK_DC_X; |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 5178 | DirectiveKindMap[".dcb"] = DK_DCB; |
| 5179 | DirectiveKindMap[".dcb.b"] = DK_DCB_B; |
| 5180 | DirectiveKindMap[".dcb.d"] = DK_DCB_D; |
| 5181 | DirectiveKindMap[".dcb.l"] = DK_DCB_L; |
| 5182 | DirectiveKindMap[".dcb.s"] = DK_DCB_S; |
| 5183 | DirectiveKindMap[".dcb.w"] = DK_DCB_W; |
| 5184 | DirectiveKindMap[".dcb.x"] = DK_DCB_X; |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 5185 | DirectiveKindMap[".ds"] = DK_DS; |
| 5186 | DirectiveKindMap[".ds.b"] = DK_DS_B; |
| 5187 | DirectiveKindMap[".ds.d"] = DK_DS_D; |
| 5188 | DirectiveKindMap[".ds.l"] = DK_DS_L; |
| 5189 | DirectiveKindMap[".ds.p"] = DK_DS_P; |
| 5190 | DirectiveKindMap[".ds.s"] = DK_DS_S; |
| 5191 | DirectiveKindMap[".ds.w"] = DK_DS_W; |
| 5192 | DirectiveKindMap[".ds.x"] = DK_DS_X; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5195 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5196 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5197 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5198 | unsigned NestLevel = 0; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5199 | while (true) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5200 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5201 | if (getLexer().is(AsmToken::Eof)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5202 | printError(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5203 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5206 | if (Lexer.is(AsmToken::Identifier) && |
Nikolay Haustov | 95b4fcd | 2016-03-01 08:18:28 +0000 | [diff] [blame] | 5207 | (getTok().getIdentifier() == ".rept" || |
| 5208 | getTok().getIdentifier() == ".irp" || |
| 5209 | getTok().getIdentifier() == ".irpc")) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5210 | ++NestLevel; |
| 5211 | } |
| 5212 | |
| 5213 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5214 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5215 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5216 | EndToken = getTok(); |
| 5217 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5218 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5219 | printError(getTok().getLoc(), |
| 5220 | "unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5221 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5222 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5223 | break; |
| 5224 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5225 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5228 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5229 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 5233 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 5234 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 5235 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5236 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5237 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 5238 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5241 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5242 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5243 | OS << ".endr\n"; |
| 5244 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 5245 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 5246 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5247 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5248 | // Create the macro instantiation object and add to the current macro |
| 5249 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 5250 | MacroInstantiation *MI = new MacroInstantiation( |
| 5251 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5252 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5253 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5254 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 5255 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 5256 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5257 | Lex(); |
| 5258 | } |
| 5259 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5260 | /// parseDirectiveRept |
| 5261 | /// ::= .rep | .rept count |
| 5262 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5263 | const MCExpr *CountExpr; |
| 5264 | SMLoc CountLoc = getTok().getLoc(); |
| 5265 | if (parseExpression(CountExpr)) |
| 5266 | return true; |
| 5267 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5268 | int64_t Count; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 5269 | if (!CountExpr->evaluateAsAbsolute(Count)) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5270 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 5271 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5272 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5273 | if (check(Count < 0, CountLoc, "Count is negative") || |
| 5274 | parseToken(AsmToken::EndOfStatement, |
| 5275 | "unexpected token in '" + Dir + "' directive")) |
| 5276 | return true; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5277 | |
| 5278 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5279 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5280 | if (!M) |
| 5281 | return true; |
| 5282 | |
| 5283 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5284 | // to hold the macro body with substitutions. |
| 5285 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5286 | raw_svector_ostream OS(Buf); |
| 5287 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5288 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 5289 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5290 | return true; |
| 5291 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5292 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5293 | |
| 5294 | return false; |
| 5295 | } |
| 5296 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5297 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5298 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5299 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5300 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5301 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5302 | if (check(parseIdentifier(Parameter.Name), |
| 5303 | "expected identifier in '.irp' directive") || |
| 5304 | parseToken(AsmToken::Comma, "expected comma in '.irp' directive") || |
| 5305 | parseMacroArguments(nullptr, A) || |
| 5306 | parseToken(AsmToken::EndOfStatement, "expected End of Statement")) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5307 | return true; |
| 5308 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5309 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5310 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5311 | if (!M) |
| 5312 | return true; |
| 5313 | |
| 5314 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5315 | // to hold the macro body with substitutions. |
| 5316 | SmallString<256> Buf; |
| 5317 | raw_svector_ostream OS(Buf); |
| 5318 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5319 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5320 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 5321 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5322 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5323 | return true; |
| 5324 | } |
| 5325 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5326 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5327 | |
| 5328 | return false; |
| 5329 | } |
| 5330 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5331 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5332 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5333 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5334 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5335 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5336 | |
| 5337 | if (check(parseIdentifier(Parameter.Name), |
| 5338 | "expected identifier in '.irpc' directive") || |
| 5339 | parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") || |
| 5340 | parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5341 | return true; |
| 5342 | |
| 5343 | if (A.size() != 1 || A.front().size() != 1) |
| 5344 | return TokError("unexpected token in '.irpc' directive"); |
| 5345 | |
| 5346 | // Eat the end of statement. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5347 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5348 | return true; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5349 | |
| 5350 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5351 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5352 | if (!M) |
| 5353 | return true; |
| 5354 | |
| 5355 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5356 | // to hold the macro body with substitutions. |
| 5357 | SmallString<256> Buf; |
| 5358 | raw_svector_ostream OS(Buf); |
| 5359 | |
| 5360 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 5361 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 5362 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5363 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5364 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5365 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 5366 | // This is undocumented, but GAS seems to support it. |
| 5367 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5368 | return true; |
| 5369 | } |
| 5370 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5371 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5372 | |
| 5373 | return false; |
| 5374 | } |
| 5375 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5376 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5377 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 5378 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5379 | |
| 5380 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5381 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5382 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 5383 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5384 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5385 | return false; |
| 5386 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 5387 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5388 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5389 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5390 | const MCExpr *Value; |
| 5391 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5392 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5393 | return true; |
| 5394 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5395 | if (!MCE) |
| 5396 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 5397 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 5398 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5399 | return Error(ExprLoc, "literal value out of range for directive"); |
| 5400 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5401 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5402 | return false; |
| 5403 | } |
| 5404 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5405 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5406 | const MCExpr *Value; |
| 5407 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5408 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5409 | return true; |
| 5410 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5411 | if (!MCE) |
| 5412 | return Error(ExprLoc, "unexpected expression in align"); |
| 5413 | uint64_t IntValue = MCE->getValue(); |
| 5414 | if (!isPowerOf2_64(IntValue)) |
| 5415 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 5416 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5417 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5418 | return false; |
| 5419 | } |
| 5420 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 5421 | // We are comparing pointers, but the pointers are relative to a single string. |
| 5422 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 5423 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 5424 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 5425 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 5426 | return -1; |
| 5427 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 5428 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5429 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5430 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 5431 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 5432 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 5433 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5434 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 5435 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5436 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5437 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5438 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 5439 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5440 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5441 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5444 | bool AsmParser::parseMSInlineAsm( |
| 5445 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 5446 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5447 | SmallVectorImpl<std::string> &Constraints, |
| 5448 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 5449 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5450 | SmallVector<void *, 4> InputDecls; |
| 5451 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5452 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 5453 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5454 | SmallVector<std::string, 4> InputConstraints; |
| 5455 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5456 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5457 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5458 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5459 | |
| 5460 | // Prime the lexer. |
| 5461 | Lex(); |
| 5462 | |
| 5463 | // While we have input, parse each statement. |
| 5464 | unsigned InputIdx = 0; |
| 5465 | unsigned OutputIdx = 0; |
| 5466 | while (getLexer().isNot(AsmToken::Eof)) { |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5467 | // Parse curly braces marking block start/end |
| 5468 | if (parseCurlyBlockScope(AsmStrRewrites)) |
| 5469 | continue; |
| 5470 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5471 | ParseStatementInfo Info(&AsmStrRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5472 | bool StatementErr = parseStatement(Info, &SI); |
Nirav Dave | 9fa8af2 | 2016-09-13 13:55:06 +0000 | [diff] [blame] | 5473 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5474 | if (StatementErr || Info.ParseError) { |
| 5475 | // Emit pending errors if any exist. |
| 5476 | printPendingErrors(); |
Nico Weber | e204c48 | 2016-09-13 18:17:00 +0000 | [diff] [blame] | 5477 | return true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | // No pending error should exist here. |
| 5481 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 5482 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5483 | if (Info.Opcode == ~0U) |
| 5484 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5485 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5486 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5487 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5488 | // Build the list of clobbers, outputs and inputs. |
| 5489 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5490 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5491 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5492 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5493 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5494 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5495 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5496 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 5497 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 5498 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5499 | unsigned NumDefs = Desc.getNumDefs(); |
| 5500 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5501 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 5502 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5503 | continue; |
| 5504 | } |
| 5505 | |
| 5506 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5507 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5508 | if (SymName.empty()) |
| 5509 | continue; |
| 5510 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5511 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5512 | if (!OpDecl) |
| 5513 | continue; |
| 5514 | |
| 5515 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5516 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5517 | if (isOutput) { |
| 5518 | ++InputIdx; |
| 5519 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5520 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 5521 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5522 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5523 | } else { |
| 5524 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5525 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 5526 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5527 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5528 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5529 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 5530 | |
| 5531 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 5532 | ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), |
| 5533 | Desc.getNumImplicitDefs()); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5534 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5538 | NumOutputs = OutputDecls.size(); |
| 5539 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5540 | |
| 5541 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5542 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 5543 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 5544 | ClobberRegs.end()); |
| 5545 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 5546 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 5547 | raw_string_ostream OS(Clobbers[I]); |
| 5548 | IP->printRegName(OS, ClobberRegs[I]); |
| 5549 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5550 | |
| 5551 | // Merge the various outputs and inputs. Output are expected first. |
| 5552 | if (NumOutputs || NumInputs) { |
| 5553 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5554 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5555 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5556 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5557 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5558 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5559 | } |
| 5560 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5561 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5562 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
| 5566 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 5567 | std::string AsmStringIR; |
| 5568 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 5569 | StringRef ASMString = |
| 5570 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 5571 | const char *AsmStart = ASMString.begin(); |
| 5572 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5573 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5574 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 5575 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5576 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5577 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5578 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5579 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 5580 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5581 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5582 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5583 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5584 | // Skip the original expression. |
| 5585 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5586 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5587 | continue; |
| 5588 | } |
| 5589 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5590 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5591 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5592 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5593 | default: |
| 5594 | break; |
Coby Tayree | d891289 | 2017-08-24 08:46:25 +0000 | [diff] [blame^] | 5595 | case AOK_IntelExpr: |
| 5596 | assert(AR.IntelExp.isValid() && "cannot write invalid intel expression"); |
| 5597 | if (AR.IntelExp.NeedBracs) |
| 5598 | OS << "["; |
| 5599 | if (AR.IntelExp.hasBaseReg()) |
| 5600 | OS << AR.IntelExp.BaseReg; |
| 5601 | if (AR.IntelExp.hasIndexReg()) |
| 5602 | OS << (AR.IntelExp.hasBaseReg() ? " + " : "") |
| 5603 | << AR.IntelExp.IndexReg; |
| 5604 | if (AR.IntelExp.Scale > 1) |
| 5605 | OS << " * $$" << AR.IntelExp.Scale; |
| 5606 | if (AR.IntelExp.Imm || !AR.IntelExp.hasRegs()) |
| 5607 | OS << (AR.IntelExp.hasRegs() ? " + $$" : "$$") << AR.IntelExp.Imm; |
| 5608 | if (AR.IntelExp.NeedBracs) |
| 5609 | OS << "]"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5610 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5611 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 5612 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5613 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5614 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5615 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5616 | break; |
| 5617 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5618 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5619 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5620 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5621 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5622 | default: break; |
| 5623 | case 8: OS << "byte ptr "; break; |
| 5624 | case 16: OS << "word ptr "; break; |
| 5625 | case 32: OS << "dword ptr "; break; |
| 5626 | case 64: OS << "qword ptr "; break; |
| 5627 | case 80: OS << "xword ptr "; break; |
| 5628 | case 128: OS << "xmmword ptr "; break; |
| 5629 | case 256: OS << "ymmword ptr "; break; |
| 5630 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5631 | break; |
| 5632 | case AOK_Emit: |
| 5633 | OS << ".byte"; |
| 5634 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5635 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5636 | // MS alignment directives are measured in bytes. If the native assembler |
| 5637 | // measures alignment in bytes, we can pass it straight through. |
| 5638 | OS << ".align"; |
| 5639 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 5640 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5641 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5642 | // Alignment is in log2 form, so print that instead and skip the original |
| 5643 | // immediate. |
| 5644 | unsigned Val = AR.Val; |
| 5645 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5646 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5647 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 5648 | break; |
| 5649 | } |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 5650 | case AOK_EVEN: |
| 5651 | OS << ".even"; |
| 5652 | break; |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5653 | case AOK_EndOfStatement: |
| 5654 | OS << "\n\t"; |
| 5655 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5656 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5657 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5658 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5659 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5663 | if (AsmStart != AsmEnd) |
| 5664 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5665 | |
| 5666 | AsmString = OS.str(); |
| 5667 | return false; |
| 5668 | } |
| 5669 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5670 | namespace llvm { |
| 5671 | namespace MCParserUtils { |
| 5672 | |
| 5673 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 5674 | /// or subexpressions. |
| 5675 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 5676 | switch (Value->getKind()) { |
| 5677 | case MCExpr::Binary: { |
| 5678 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 5679 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 5680 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 5681 | } |
| 5682 | case MCExpr::Target: |
| 5683 | case MCExpr::Constant: |
| 5684 | return false; |
| 5685 | case MCExpr::SymbolRef: { |
| 5686 | const MCSymbol &S = |
| 5687 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 5688 | if (S.isVariable()) |
| 5689 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 5690 | return &S == Sym; |
| 5691 | } |
| 5692 | case MCExpr::Unary: |
| 5693 | return isSymbolUsedInExpression( |
| 5694 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 5695 | } |
| 5696 | |
| 5697 | llvm_unreachable("Unknown expr kind!"); |
| 5698 | } |
| 5699 | |
| 5700 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 5701 | MCAsmParser &Parser, MCSymbol *&Sym, |
| 5702 | const MCExpr *&Value) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5703 | |
| 5704 | // FIXME: Use better location, we should use proper tokens. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 5705 | SMLoc EqualLoc = Parser.getTok().getLoc(); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5706 | |
| 5707 | if (Parser.parseExpression(Value)) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5708 | return Parser.TokError("missing expression"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | // Note: we don't count b as used in "a = b". This is to allow |
| 5712 | // a = b |
| 5713 | // b = c |
| 5714 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5715 | if (Parser.parseToken(AsmToken::EndOfStatement)) |
| 5716 | return true; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5717 | |
| 5718 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 5719 | // used as a symbol, or it is an absolute symbol). |
| 5720 | Sym = Parser.getContext().lookupSymbol(Name); |
| 5721 | if (Sym) { |
| 5722 | // Diagnose assignment to a label. |
| 5723 | // |
| 5724 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 5725 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 5726 | if (isSymbolUsedInExpression(Sym, Value)) |
| 5727 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 5728 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 5729 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5730 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 5731 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 5732 | ; // Allow redefinitions of variables that haven't yet been used. |
| 5733 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 5734 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 5735 | else if (!Sym->isVariable()) |
| 5736 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 5737 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 5738 | return Parser.Error(EqualLoc, |
| 5739 | "invalid reassignment of non-absolute variable '" + |
| 5740 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5741 | } else if (Name == ".") { |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 5742 | Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5743 | return false; |
| 5744 | } else |
| 5745 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 5746 | |
| 5747 | Sym->setRedefinable(allow_redef); |
| 5748 | |
| 5749 | return false; |
| 5750 | } |
| 5751 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5752 | } // end namespace MCParserUtils |
| 5753 | } // end namespace llvm |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5754 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 5755 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5756 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 5757 | MCStreamer &Out, const MCAsmInfo &MAI, |
| 5758 | unsigned CB) { |
| 5759 | return new AsmParser(SM, C, Out, MAI, CB); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 5760 | } |