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" |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 25 | #include "llvm/BinaryFormat/Dwarf.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCCodeView.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCDirectives.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstPrinter.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrDesc.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCInstrInfo.h" |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCObjectFileInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCParser/AsmCond.h" |
| 37 | #include "llvm/MC/MCParser/AsmLexer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCParser/MCAsmParserUtils.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 42 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Benjamin Kramer | b3e8a6d | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCRegisterInfo.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 45 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 46 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCTargetOptions.h" |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 49 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Casting.h" |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MD5.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 54 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 56 | #include "llvm/Support/SMLoc.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 57 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 59 | #include <algorithm> |
| 60 | #include <cassert> |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 61 | #include <cctype> |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 62 | #include <climits> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 63 | #include <cstddef> |
| 64 | #include <cstdint> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 65 | #include <deque> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 66 | #include <memory> |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 67 | #include <sstream> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 68 | #include <string> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 69 | #include <tuple> |
| 70 | #include <utility> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 71 | #include <vector> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 72 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 73 | using namespace llvm; |
| 74 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 75 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 76 | |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 77 | static cl::opt<unsigned> AsmMacroMaxNestingDepth( |
| 78 | "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden, |
| 79 | cl::desc("The maximum nesting depth allowed for assembly macros.")); |
| 80 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 81 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 82 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 83 | /// \brief Helper types for tracking macro definitions. |
| 84 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 85 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 87 | /// \brief Helper class for storing information about an active macro |
| 88 | /// instantiation. |
| 89 | struct MacroInstantiation { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 90 | /// The location of the instantiation. |
| 91 | SMLoc InstantiationLoc; |
| 92 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 93 | /// The buffer where parsing should resume upon instantiation completion. |
| 94 | int ExitBuffer; |
| 95 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 96 | /// The location where parsing should resume upon instantiation completion. |
| 97 | SMLoc ExitLoc; |
| 98 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 99 | /// The depth of TheCondStack at the start of the instantiation. |
| 100 | size_t CondStackDepth; |
| 101 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 102 | public: |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 103 | MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 106 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 107 | /// \brief The parsed operands from the last parsed statement. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 108 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 109 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 110 | /// \brief The opcode from the last parsed instruction. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 111 | unsigned Opcode = ~0U; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 112 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 113 | /// \brief Was there an error parsing the inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 114 | bool ParseError = false; |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 115 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 116 | SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 117 | |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 118 | ParseStatementInfo() = delete; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 119 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 120 | : AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 123 | /// \brief The concrete assembly parser instance. |
| 124 | class AsmParser : public MCAsmParser { |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 125 | private: |
| 126 | AsmLexer Lexer; |
| 127 | MCContext &Ctx; |
| 128 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 129 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 130 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 131 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 132 | void *SavedDiagContext; |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 133 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 135 | /// This is the current buffer index we're lexing from as managed by the |
| 136 | /// SourceMgr object. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 137 | unsigned CurBuffer; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 138 | |
| 139 | AsmCond TheCondState; |
| 140 | std::vector<AsmCond> TheCondStack; |
| 141 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 142 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 143 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 144 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 145 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 146 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 147 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 148 | std::vector<MacroInstantiation*> ActiveMacros; |
| 149 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 150 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 151 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 152 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 153 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 154 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 155 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 156 | /// \brief Keeps track of how many .macro's have been instantiated. |
| 157 | unsigned NumOfMacroInstantiations; |
| 158 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 159 | /// 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] | 160 | struct CppHashInfoTy { |
| 161 | StringRef Filename; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 162 | int64_t LineNumber = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 163 | SMLoc Loc; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 164 | unsigned Buf = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 165 | }; |
| 166 | CppHashInfoTy CppHashInfo; |
| 167 | |
| 168 | /// \brief List of forward directional labels for diagnosis at the end. |
| 169 | SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
| 170 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 171 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 172 | unsigned AssemblerDialect = ~0U; |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 173 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 174 | /// \brief is Darwin compatibility enabled? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 175 | bool IsDarwin = false; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 176 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 177 | /// \brief Are we parsing ms-style inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 178 | bool ParsingInlineAsm = false; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 179 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 180 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 181 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 182 | const MCAsmInfo &MAI, unsigned CB); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 183 | AsmParser(const AsmParser &) = delete; |
| 184 | AsmParser &operator=(const AsmParser &) = delete; |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 185 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 186 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 187 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 188 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 189 | void addDirectiveHandler(StringRef Directive, |
| 190 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 191 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 194 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 195 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 196 | } |
| 197 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 198 | /// @name MCAsmParser Interface |
| 199 | /// { |
| 200 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 201 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 202 | MCAsmLexer &getLexer() override { return Lexer; } |
| 203 | MCContext &getContext() override { return Ctx; } |
| 204 | MCStreamer &getStreamer() override { return Out; } |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 205 | |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 206 | CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
| 207 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 208 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 209 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 210 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 211 | else |
| 212 | return AssemblerDialect; |
| 213 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 214 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 215 | AssemblerDialect = i; |
| 216 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 217 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 218 | void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 219 | bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 220 | bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 221 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 222 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 223 | |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 224 | void setParsingInlineAsm(bool V) override { |
| 225 | ParsingInlineAsm = V; |
| 226 | Lexer.setParsingMSInlineAsm(V); |
| 227 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 228 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 229 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 230 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 231 | unsigned &NumOutputs, unsigned &NumInputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 232 | SmallVectorImpl<std::pair<void *,bool>> &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 233 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 234 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 235 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 236 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 237 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 238 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 239 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 240 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 241 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 242 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 243 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 244 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 245 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 246 | /// \brief Parse a floating point expression using the float \p Semantics |
| 247 | /// and set \p Res to the value. |
| 248 | bool parseRealValue(const fltSemantics &Semantics, APInt &Res); |
| 249 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 250 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 251 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 252 | bool parseIdentifier(StringRef &Res) override; |
| 253 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 254 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 255 | bool checkForValidSection() override; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 256 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 257 | /// } |
| 258 | |
| 259 | private: |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 260 | bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 261 | void altMacroString(StringRef AltMacroStr, std::string &Res); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 262 | bool parseStatement(ParseStatementInfo &Info, |
| 263 | MCAsmParserSemaCallback *SI); |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 264 | bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 265 | bool parseCppHashLineFilenameComment(SMLoc L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 266 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 267 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 268 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 269 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 270 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 271 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 272 | SMLoc L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 273 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 274 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 275 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 276 | |
| 277 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 278 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 279 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 280 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 281 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 282 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 283 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 284 | /// |
| 285 | /// \param M The macro. |
| 286 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 287 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 288 | |
| 289 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 290 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 291 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 292 | /// \brief Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 293 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 294 | |
| 295 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 296 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 297 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 298 | void printMacroInstantiations(); |
| 299 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 300 | SMRange Range = None) const { |
| 301 | ArrayRef<SMRange> Ranges(Range); |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 302 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 303 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 304 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 305 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 306 | /// Should we emit DWARF describing this assembler source? (Returns false if |
| 307 | /// the source has .file directives, which means we don't want to generate |
| 308 | /// info describing the assembler source itself.) |
| 309 | bool enabledGenDwarfForAssembly(); |
| 310 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 311 | /// \brief Enter the specified file. This returns true on failure. |
| 312 | bool enterIncludeFile(const std::string &Filename); |
| 313 | |
| 314 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 315 | /// This returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 316 | bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, |
| 317 | const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 318 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 319 | /// \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] | 320 | /// current token is not set; clients should ensure Lex() is called |
| 321 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 322 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 323 | /// \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] | 324 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 325 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 326 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 327 | /// \brief Parse up to the end of statement and a return the contents from the |
| 328 | /// current token until the end of the statement; the current token on exit |
| 329 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 330 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 331 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 332 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 333 | /// 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] | 334 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 335 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 336 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 337 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 338 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 339 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 340 | MCBinaryExpr::Opcode &Kind); |
| 341 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 342 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 343 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 344 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 345 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 346 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 347 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 348 | bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); |
| 349 | bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); |
| 350 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 351 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 352 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 353 | DK_NO_DIRECTIVE, // Placeholder |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 354 | DK_SET, |
| 355 | DK_EQU, |
| 356 | DK_EQUIV, |
| 357 | DK_ASCII, |
| 358 | DK_ASCIZ, |
| 359 | DK_STRING, |
| 360 | DK_BYTE, |
| 361 | DK_SHORT, |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 362 | DK_RELOC, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 363 | DK_VALUE, |
| 364 | DK_2BYTE, |
| 365 | DK_LONG, |
| 366 | DK_INT, |
| 367 | DK_4BYTE, |
| 368 | DK_QUAD, |
| 369 | DK_8BYTE, |
| 370 | DK_OCTA, |
| 371 | DK_DC, |
| 372 | DK_DC_A, |
| 373 | DK_DC_B, |
| 374 | DK_DC_D, |
| 375 | DK_DC_L, |
| 376 | DK_DC_S, |
| 377 | DK_DC_W, |
| 378 | DK_DC_X, |
| 379 | DK_DCB, |
| 380 | DK_DCB_B, |
| 381 | DK_DCB_D, |
| 382 | DK_DCB_L, |
| 383 | DK_DCB_S, |
| 384 | DK_DCB_W, |
| 385 | DK_DCB_X, |
| 386 | DK_DS, |
| 387 | DK_DS_B, |
| 388 | DK_DS_D, |
| 389 | DK_DS_L, |
| 390 | DK_DS_P, |
| 391 | DK_DS_S, |
| 392 | DK_DS_W, |
| 393 | DK_DS_X, |
| 394 | DK_SINGLE, |
| 395 | DK_FLOAT, |
| 396 | DK_DOUBLE, |
| 397 | DK_ALIGN, |
| 398 | DK_ALIGN32, |
| 399 | DK_BALIGN, |
| 400 | DK_BALIGNW, |
| 401 | DK_BALIGNL, |
| 402 | DK_P2ALIGN, |
| 403 | DK_P2ALIGNW, |
| 404 | DK_P2ALIGNL, |
| 405 | DK_ORG, |
| 406 | DK_FILL, |
| 407 | DK_ENDR, |
| 408 | DK_BUNDLE_ALIGN_MODE, |
| 409 | DK_BUNDLE_LOCK, |
| 410 | DK_BUNDLE_UNLOCK, |
| 411 | DK_ZERO, |
| 412 | DK_EXTERN, |
| 413 | DK_GLOBL, |
| 414 | DK_GLOBAL, |
| 415 | DK_LAZY_REFERENCE, |
| 416 | DK_NO_DEAD_STRIP, |
| 417 | DK_SYMBOL_RESOLVER, |
| 418 | DK_PRIVATE_EXTERN, |
| 419 | DK_REFERENCE, |
| 420 | DK_WEAK_DEFINITION, |
| 421 | DK_WEAK_REFERENCE, |
| 422 | DK_WEAK_DEF_CAN_BE_HIDDEN, |
| 423 | DK_COMM, |
| 424 | DK_COMMON, |
| 425 | DK_LCOMM, |
| 426 | DK_ABORT, |
| 427 | DK_INCLUDE, |
| 428 | DK_INCBIN, |
| 429 | DK_CODE16, |
| 430 | DK_CODE16GCC, |
| 431 | DK_REPT, |
| 432 | DK_IRP, |
| 433 | DK_IRPC, |
| 434 | DK_IF, |
| 435 | DK_IFEQ, |
| 436 | DK_IFGE, |
| 437 | DK_IFGT, |
| 438 | DK_IFLE, |
| 439 | DK_IFLT, |
| 440 | DK_IFNE, |
| 441 | DK_IFB, |
| 442 | DK_IFNB, |
| 443 | DK_IFC, |
| 444 | DK_IFEQS, |
| 445 | DK_IFNC, |
| 446 | DK_IFNES, |
| 447 | DK_IFDEF, |
| 448 | DK_IFNDEF, |
| 449 | DK_IFNOTDEF, |
| 450 | DK_ELSEIF, |
| 451 | DK_ELSE, |
| 452 | DK_ENDIF, |
| 453 | DK_SPACE, |
| 454 | DK_SKIP, |
| 455 | DK_FILE, |
| 456 | DK_LINE, |
| 457 | DK_LOC, |
| 458 | DK_STABS, |
| 459 | DK_CV_FILE, |
| 460 | DK_CV_FUNC_ID, |
| 461 | DK_CV_INLINE_SITE_ID, |
| 462 | DK_CV_LOC, |
| 463 | DK_CV_LINETABLE, |
| 464 | DK_CV_INLINE_LINETABLE, |
| 465 | DK_CV_DEF_RANGE, |
| 466 | DK_CV_STRINGTABLE, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 467 | DK_CV_FILECHECKSUMS, |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 468 | DK_CV_FILECHECKSUM_OFFSET, |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 469 | DK_CV_FPO_DATA, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 470 | DK_CFI_SECTIONS, |
| 471 | DK_CFI_STARTPROC, |
| 472 | DK_CFI_ENDPROC, |
| 473 | DK_CFI_DEF_CFA, |
| 474 | DK_CFI_DEF_CFA_OFFSET, |
| 475 | DK_CFI_ADJUST_CFA_OFFSET, |
| 476 | DK_CFI_DEF_CFA_REGISTER, |
| 477 | DK_CFI_OFFSET, |
| 478 | DK_CFI_REL_OFFSET, |
| 479 | DK_CFI_PERSONALITY, |
| 480 | DK_CFI_LSDA, |
| 481 | DK_CFI_REMEMBER_STATE, |
| 482 | DK_CFI_RESTORE_STATE, |
| 483 | DK_CFI_SAME_VALUE, |
| 484 | DK_CFI_RESTORE, |
| 485 | DK_CFI_ESCAPE, |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 486 | DK_CFI_RETURN_COLUMN, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 487 | DK_CFI_SIGNAL_FRAME, |
| 488 | DK_CFI_UNDEFINED, |
| 489 | DK_CFI_REGISTER, |
| 490 | DK_CFI_WINDOW_SAVE, |
| 491 | DK_MACROS_ON, |
| 492 | DK_MACROS_OFF, |
| 493 | DK_ALTMACRO, |
| 494 | DK_NOALTMACRO, |
| 495 | DK_MACRO, |
| 496 | DK_EXITM, |
| 497 | DK_ENDM, |
| 498 | DK_ENDMACRO, |
| 499 | DK_PURGEM, |
| 500 | DK_SLEB128, |
| 501 | DK_ULEB128, |
| 502 | DK_ERR, |
| 503 | DK_ERROR, |
| 504 | DK_WARNING, |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 505 | DK_PRINT, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 506 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 507 | }; |
| 508 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 509 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 510 | /// directives parsed by this class. |
| 511 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 512 | |
| 513 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 514 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 515 | bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 516 | bool parseDirectiveValue(StringRef IDVal, |
| 517 | unsigned Size); // ".byte", ".long", ... |
| 518 | bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
| 519 | bool parseDirectiveRealValue(StringRef IDVal, |
| 520 | const fltSemantics &); // ".single", ... |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 521 | bool parseDirectiveFill(); // ".fill" |
| 522 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 523 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 524 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 525 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 526 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 527 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 528 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 529 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 530 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 531 | bool parseDirectiveLine(); |
| 532 | bool parseDirectiveLoc(); |
| 533 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 534 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 535 | // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", |
| 536 | // ".cv_inline_linetable", ".cv_def_range" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 537 | bool parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 538 | bool parseDirectiveCVFuncId(); |
| 539 | bool parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 540 | bool parseDirectiveCVLoc(); |
| 541 | bool parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 542 | bool parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 543 | bool parseDirectiveCVDefRange(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 544 | bool parseDirectiveCVStringTable(); |
| 545 | bool parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 546 | bool parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 547 | bool parseDirectiveCVFPOData(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 548 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 549 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 550 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 551 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 552 | bool parseDirectiveCFISections(); |
| 553 | bool parseDirectiveCFIStartProc(); |
| 554 | bool parseDirectiveCFIEndProc(); |
| 555 | bool parseDirectiveCFIDefCfaOffset(); |
| 556 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 557 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 558 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 559 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 560 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 561 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 562 | bool parseDirectiveCFIRememberState(); |
| 563 | bool parseDirectiveCFIRestoreState(); |
| 564 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 565 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 566 | bool parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 567 | bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 568 | bool parseDirectiveCFISignalFrame(); |
| 569 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 570 | |
| 571 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 572 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 573 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 574 | bool parseDirectiveEndMacro(StringRef Directive); |
| 575 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 576 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 577 | // alternate macro mode directives |
| 578 | bool parseDirectiveAltmacro(StringRef Directive); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 579 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 580 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 581 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 582 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 583 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 584 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 585 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 586 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 587 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 588 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 589 | // ".dcb" |
| 590 | bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
| 591 | bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 592 | // ".ds" |
| 593 | bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 594 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 595 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 596 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 597 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 598 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 599 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 600 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 601 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 602 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 603 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 604 | bool parseDirectiveAbort(); // ".abort" |
| 605 | bool parseDirectiveInclude(); // ".include" |
| 606 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 607 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 608 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 609 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 610 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 611 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 612 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 613 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 614 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 615 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 616 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 617 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 618 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 619 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 620 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 621 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 622 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 623 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 624 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 626 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 627 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 628 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 629 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 630 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 631 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 632 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 633 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 634 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 635 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 636 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 637 | size_t Len); |
| 638 | |
| 639 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 640 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 641 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 642 | // "end" |
| 643 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 644 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 645 | // ".err" or ".error" |
| 646 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 647 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 648 | // ".warning" |
| 649 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 650 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 651 | // .print <double-quotes-string> |
| 652 | bool parseDirectivePrint(SMLoc DirectiveLoc); |
| 653 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 654 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 655 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 656 | |
| 657 | } // end anonymous namespace |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 658 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 659 | namespace llvm { |
| 660 | |
| 661 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 662 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 663 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 664 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 665 | } // end namespace llvm |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 666 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 667 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 668 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 669 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 670 | const MCAsmInfo &MAI, unsigned CB = 0) |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 671 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 672 | CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 673 | HadError = false; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 674 | // Save the old handler. |
| 675 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 676 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 677 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 678 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 679 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 680 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 681 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 682 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 683 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 684 | PlatformParser.reset(createCOFFAsmParser()); |
| 685 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 686 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 687 | PlatformParser.reset(createDarwinAsmParser()); |
| 688 | IsDarwin = true; |
| 689 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 690 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 691 | PlatformParser.reset(createELFAsmParser()); |
| 692 | break; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 693 | case MCObjectFileInfo::IsWasm: |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame^] | 694 | // TODO: WASM will need its own MCAsmParserExtension implementation, but |
| 695 | // for now we can re-use the ELF one, since the directives can be the |
| 696 | // same for now, and this makes the -triple=wasm32-unknown-unknown-wasm |
| 697 | // path work. |
| 698 | PlatformParser.reset(createELFAsmParser()); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 699 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 700 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 701 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 702 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 703 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 704 | |
| 705 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 708 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 709 | assert((HadError || ActiveMacros.empty()) && |
| 710 | "Unexpected active macro instantiation!"); |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 711 | |
| 712 | // Restore the saved diagnostics handler and context for use during |
| 713 | // finalization. |
| 714 | SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 717 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 718 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 719 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 720 | it = ActiveMacros.rbegin(), |
| 721 | ie = ActiveMacros.rend(); |
| 722 | it != ie; ++it) |
| 723 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 724 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 727 | void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
| 728 | printPendingErrors(); |
| 729 | printMessage(L, SourceMgr::DK_Note, Msg, Range); |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 730 | printMacroInstantiations(); |
| 731 | } |
| 732 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 733 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 734 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 735 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 736 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 737 | return Error(L, Msg, Range); |
| 738 | printMessage(L, SourceMgr::DK_Warning, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 739 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 740 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 743 | bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 744 | HadError = true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 745 | printMessage(L, SourceMgr::DK_Error, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 746 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 750 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 751 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 752 | unsigned NewBuf = |
| 753 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 754 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 755 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 756 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 757 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 758 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 759 | return false; |
| 760 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 761 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 762 | /// 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] | 763 | /// 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] | 764 | /// returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 765 | bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
| 766 | const MCExpr *Count, SMLoc Loc) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 767 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 768 | unsigned NewBuf = |
| 769 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 770 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 771 | return true; |
| 772 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 773 | // Pick up the bytes from the file and emit them. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 774 | StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); |
| 775 | Bytes = Bytes.drop_front(Skip); |
| 776 | if (Count) { |
| 777 | int64_t Res; |
| 778 | if (!Count->evaluateAsAbsolute(Res)) |
| 779 | return Error(Loc, "expected absolute expression"); |
| 780 | if (Res < 0) |
| 781 | return Warning(Loc, "negative count has no effect"); |
| 782 | Bytes = Bytes.take_front(Res); |
| 783 | } |
| 784 | getStreamer().EmitBytes(Bytes); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 788 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 789 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 790 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 791 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 794 | const AsmToken &AsmParser::Lex() { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 795 | if (Lexer.getTok().is(AsmToken::Error)) |
| 796 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
| 797 | |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 798 | // if it's a end of statement with a comment in it |
| 799 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 800 | // if this is a line comment output it. |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 801 | if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 802 | getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
| 803 | Out.addExplicitComment(Twine(getTok().getString())); |
| 804 | } |
| 805 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 806 | const AsmToken *tok = &Lexer.Lex(); |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 807 | |
| 808 | // Parse comments here to be deferred until end of next statement. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 809 | while (tok->is(AsmToken::Comment)) { |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 810 | if (MAI.preserveAsmComments()) |
| 811 | Out.addExplicitComment(Twine(tok->getString())); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 812 | tok = &Lexer.Lex(); |
| 813 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 814 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 815 | if (tok->is(AsmToken::Eof)) { |
| 816 | // If this is the end of an included file, pop the parent file off the |
| 817 | // include stack. |
| 818 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 819 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 820 | jumpToLoc(ParentIncludeLoc); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 821 | return Lex(); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 824 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 825 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 828 | bool AsmParser::enabledGenDwarfForAssembly() { |
| 829 | // Check whether the user specified -g. |
| 830 | if (!getContext().getGenDwarfForAssembly()) |
| 831 | return false; |
| 832 | // If we haven't encountered any .file directives (which would imply that |
| 833 | // the assembler source was produced with debug info already) then emit one |
| 834 | // describing the assembler source file itself. |
| 835 | if (getContext().getGenDwarfFileNumber() == 0) |
| 836 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 837 | 0, StringRef(), getContext().getMainFileName())); |
| 838 | return true; |
| 839 | } |
| 840 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 841 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 842 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 843 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 844 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 846 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 847 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 848 | |
| 849 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 850 | AsmCond StartingCondState = TheCondState; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 851 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 852 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 853 | // If we are generating dwarf for assembly source files save the initial text |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 854 | // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't |
| 855 | // emitting any actual debug info yet and haven't had a chance to parse any |
| 856 | // embedded .file directives.) |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 857 | if (getContext().getGenDwarfForAssembly()) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 858 | MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 859 | if (!Sec->getBeginSymbol()) { |
| 860 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 861 | getStreamer().EmitLabel(SectionStartSym); |
| 862 | Sec->setBeginSymbol(SectionStartSym); |
| 863 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 864 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 865 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 866 | (void)InsertResult; |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 869 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 870 | while (Lexer.isNot(AsmToken::Eof)) { |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 871 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 872 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 873 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 874 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 875 | // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
| 876 | // for printing ErrMsg via Lex() only if no (presumably better) parser error |
| 877 | // exists. |
| 878 | if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 879 | Lex(); |
| 880 | } |
| 881 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 882 | // parseStatement returned true so may need to emit an error. |
| 883 | printPendingErrors(); |
| 884 | |
| 885 | // Skipping to the next line if needed. |
| 886 | if (!getLexer().isAtStartOfStatement()) |
| 887 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 888 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 889 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 890 | // All errors should have been emitted. |
| 891 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
| 892 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 893 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 894 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 895 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 896 | TheCondState.Ignore != StartingCondState.Ignore) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 897 | printError(getTok().getLoc(), "unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 898 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 899 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 900 | if (!LineTables.empty()) { |
| 901 | unsigned Index = 0; |
| 902 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 903 | if (File.Name.empty() && Index != 0) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 904 | printError(getTok().getLoc(), "unassigned file number: " + |
| 905 | Twine(Index) + |
| 906 | " for .file directives"); |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 907 | ++Index; |
| 908 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 909 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 910 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 911 | // Check to see that all assembler local symbols were actually defined. |
| 912 | // Targets that don't do subsections via symbols may not want this, though, |
| 913 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 914 | // as otherwise we won't necessarilly have seen everything yet. |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 915 | if (!NoFinalize) { |
| 916 | if (MAI.hasSubsectionsViaSymbols()) { |
| 917 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 918 | MCSymbol *Sym = TableEntry.getValue(); |
| 919 | // Variable symbols may not be marked as defined, so check those |
| 920 | // explicitly. If we know it's a variable, we have a definition for |
| 921 | // the purposes of this check. |
| 922 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 923 | // FIXME: We would really like to refer back to where the symbol was |
| 924 | // first referenced for a source location. We need to add something |
| 925 | // 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] | 926 | printError(getTok().getLoc(), "assembler local symbol '" + |
| 927 | Sym->getName() + "' not defined"); |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
| 931 | // Temporary symbols like the ones for directional jumps don't go in the |
| 932 | // symbol table. They also need to be diagnosed in all (final) cases. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 933 | for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
| 934 | if (std::get<2>(LocSym)->isUndefined()) { |
| 935 | // Reset the state of any "# line file" directives we've seen to the |
| 936 | // context as it was at the diagnostic site. |
| 937 | CppHashInfo = std::get<1>(LocSym); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 938 | printError(std::get<0>(LocSym), "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 939 | } |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 943 | // Finalize the output stream if there are no errors and if the client wants |
| 944 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 945 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 946 | Out.Finish(); |
| 947 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 948 | return HadError || getContext().hadError(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 951 | bool AsmParser::checkForValidSection() { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 952 | if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 953 | Out.InitSections(false); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 954 | return Error(getTok().getLoc(), |
| 955 | "expected section directive before assembly directive"); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 956 | } |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 957 | return false; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 960 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 961 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 962 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 963 | Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 964 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 965 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 966 | if (Lexer.is(AsmToken::EndOfStatement)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 967 | Lexer.Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 970 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 971 | const char *Start = getTok().getLoc().getPointer(); |
| 972 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 973 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 974 | Lexer.Lex(); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 975 | |
| 976 | const char *End = getTok().getLoc().getPointer(); |
| 977 | return StringRef(Start, End - Start); |
| 978 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 979 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 980 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 981 | const char *Start = getTok().getLoc().getPointer(); |
| 982 | |
| 983 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 984 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 985 | Lexer.Lex(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 986 | |
| 987 | const char *End = getTok().getLoc().getPointer(); |
| 988 | return StringRef(Start, End - Start); |
| 989 | } |
| 990 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 991 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 992 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 993 | /// |
| 994 | /// parenexpr ::= expr) |
| 995 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 996 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 997 | if (parseExpression(Res)) |
| 998 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 999 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1000 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1001 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1002 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1005 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1006 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1007 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 1008 | /// |
| 1009 | /// bracketexpr ::= expr] |
| 1010 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1011 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1012 | if (parseExpression(Res)) |
| 1013 | return true; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1014 | EndLoc = getTok().getEndLoc(); |
| 1015 | if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) |
| 1016 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1017 | return false; |
| 1018 | } |
| 1019 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1020 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1021 | /// primaryexpr ::= (parenexpr |
| 1022 | /// primaryexpr ::= symbol |
| 1023 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1024 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1025 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1026 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1027 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 1028 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 1029 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1030 | default: |
| 1031 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 1032 | // If we have an error assume that we've already handled it. |
| 1033 | case AsmToken::Error: |
| 1034 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1035 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1036 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1037 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1038 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1039 | Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1040 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1041 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1042 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1043 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1044 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1045 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1046 | if (parseIdentifier(Identifier)) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1047 | // We may have failed but $ may be a valid token. |
| 1048 | if (getTok().is(AsmToken::Dollar)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1049 | if (Lexer.getMAI().getDollarIsPC()) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1050 | Lex(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1051 | // This is a '$' reference, which references the current PC. Emit a |
| 1052 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1053 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1054 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1055 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 1056 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1057 | EndLoc = FirstTokenLoc; |
| 1058 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 1059 | } |
| 1060 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1061 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1062 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1063 | // Parse symbol variant |
| 1064 | std::pair<StringRef, StringRef> Split; |
| 1065 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1066 | if (FirstTokenKind == AsmToken::String) { |
| 1067 | if (Lexer.is(AsmToken::At)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1068 | Lex(); // eat @ |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1069 | SMLoc AtLoc = getLexer().getLoc(); |
| 1070 | StringRef VName; |
| 1071 | if (parseIdentifier(VName)) |
| 1072 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 1073 | |
| 1074 | Split = std::make_pair(Identifier, VName); |
| 1075 | } |
| 1076 | } else { |
| 1077 | Split = Identifier.split('@'); |
| 1078 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1079 | } else if (Lexer.is(AsmToken::LParen)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1080 | Lex(); // eat '('. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1081 | StringRef VName; |
| 1082 | parseIdentifier(VName); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1083 | // eat ')'. |
| 1084 | if (parseToken(AsmToken::RParen, |
| 1085 | "unexpected token in variant, expected ')'")) |
| 1086 | return true; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1087 | Split = std::make_pair(Identifier, VName); |
| 1088 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1089 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1090 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 1091 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1092 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1093 | StringRef SymbolName = Identifier; |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1094 | if (SymbolName.empty()) |
| 1095 | return true; |
| 1096 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1097 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 1098 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1099 | // Lookup the symbol variant if used. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1100 | if (!Split.second.empty()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1101 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1102 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 1103 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1104 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1105 | Variant = MCSymbolRefExpr::VK_None; |
| 1106 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 1107 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 1108 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1111 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1112 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1113 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1114 | // If this is an absolute variable reference, substitute it now to preserve |
| 1115 | // semantics in the face of reassignment. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 1116 | if (Sym->isVariable() && |
| 1117 | isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1118 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 1119 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1120 | |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 1121 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | // Otherwise create a symbol ref. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1126 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1127 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1128 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 1129 | case AsmToken::BigNum: |
| 1130 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1131 | case AsmToken::Integer: { |
| 1132 | SMLoc Loc = getTok().getLoc(); |
| 1133 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1134 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1135 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1136 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1137 | // Look for 'b' or 'f' following an Integer as a directional label |
| 1138 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 1139 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1140 | // Lookup the symbol variant if used. |
| 1141 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 1142 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 1143 | if (Split.first.size() != IDVal.size()) { |
| 1144 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1145 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1146 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1147 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1148 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1149 | if (IDVal == "f" || IDVal == "b") { |
| 1150 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1151 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1152 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 1153 | if (IDVal == "b" && Sym->isUndefined()) |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 1154 | return Error(Loc, "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 1155 | DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1156 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1157 | Lex(); // Eat identifier. |
| 1158 | } |
| 1159 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1160 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1161 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1162 | case AsmToken::Real: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1163 | APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 1164 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1165 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1166 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1167 | Lex(); // Eat token. |
| 1168 | return false; |
| 1169 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1170 | case AsmToken::Dot: { |
| 1171 | // This is a '.' reference, which references the current PC. Emit a |
| 1172 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1173 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1174 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1175 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1176 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1177 | Lex(); // Eat identifier. |
| 1178 | return false; |
| 1179 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1180 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1181 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1182 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1183 | case AsmToken::LBrac: |
| 1184 | if (!PlatformParser->HasBracketExpressions()) |
| 1185 | return TokError("brackets expression not supported on this target"); |
| 1186 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1187 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1188 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1189 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1190 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1191 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1192 | Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1193 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1194 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1195 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1196 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1197 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1198 | Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1199 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1200 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1201 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1202 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1203 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1204 | Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1205 | return false; |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 1206 | // MIPS unary expression operators. The lexer won't generate these tokens if |
| 1207 | // MCAsmInfo::HasMipsExpressions is false for the target. |
| 1208 | case AsmToken::PercentCall16: |
| 1209 | case AsmToken::PercentCall_Hi: |
| 1210 | case AsmToken::PercentCall_Lo: |
| 1211 | case AsmToken::PercentDtprel_Hi: |
| 1212 | case AsmToken::PercentDtprel_Lo: |
| 1213 | case AsmToken::PercentGot: |
| 1214 | case AsmToken::PercentGot_Disp: |
| 1215 | case AsmToken::PercentGot_Hi: |
| 1216 | case AsmToken::PercentGot_Lo: |
| 1217 | case AsmToken::PercentGot_Ofst: |
| 1218 | case AsmToken::PercentGot_Page: |
| 1219 | case AsmToken::PercentGottprel: |
| 1220 | case AsmToken::PercentGp_Rel: |
| 1221 | case AsmToken::PercentHi: |
| 1222 | case AsmToken::PercentHigher: |
| 1223 | case AsmToken::PercentHighest: |
| 1224 | case AsmToken::PercentLo: |
| 1225 | case AsmToken::PercentNeg: |
| 1226 | case AsmToken::PercentPcrel_Hi: |
| 1227 | case AsmToken::PercentPcrel_Lo: |
| 1228 | case AsmToken::PercentTlsgd: |
| 1229 | case AsmToken::PercentTlsldm: |
| 1230 | case AsmToken::PercentTprel_Hi: |
| 1231 | case AsmToken::PercentTprel_Lo: |
| 1232 | Lex(); // Eat the operator. |
| 1233 | if (Lexer.isNot(AsmToken::LParen)) |
| 1234 | return TokError("expected '(' after operator"); |
| 1235 | Lex(); // Eat the operator. |
| 1236 | if (parseExpression(Res, EndLoc)) |
| 1237 | return true; |
| 1238 | if (Lexer.isNot(AsmToken::RParen)) |
| 1239 | return TokError("expected ')'"); |
| 1240 | Lex(); // Eat the operator. |
| 1241 | Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); |
| 1242 | return !Res; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1245 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1246 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 1247 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1248 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1251 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1252 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1253 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 1254 | // Ask the target implementation about this expression first. |
| 1255 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 1256 | if (NewE) |
| 1257 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1258 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 1259 | // if there is exactly one symbol. |
| 1260 | switch (E->getKind()) { |
| 1261 | case MCExpr::Target: |
| 1262 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1263 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1264 | |
| 1265 | case MCExpr::SymbolRef: { |
| 1266 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 1267 | |
| 1268 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1269 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 1270 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1271 | return E; |
| 1272 | } |
| 1273 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1274 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | case MCExpr::Unary: { |
| 1278 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1279 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1280 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1281 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1282 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | case MCExpr::Binary: { |
| 1286 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1287 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1288 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1291 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1292 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1293 | if (!LHS) |
| 1294 | LHS = BE->getLHS(); |
| 1295 | if (!RHS) |
| 1296 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1297 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1298 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1301 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1302 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1305 | /// This function checks if the next token is <string> type or arithmetic. |
| 1306 | /// string that begin with character '<' must end with character '>'. |
| 1307 | /// otherwise it is arithmetics. |
| 1308 | /// If the function returns a 'true' value, |
| 1309 | /// the End argument will be filled with the last location pointed to the '>' |
| 1310 | /// character. |
| 1311 | |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame^] | 1312 | /// There is a gap between the AltMacro's documentation and the single quote implementation. |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1313 | /// GCC does not fully support this feature and so we will not support it. |
| 1314 | /// TODO: Adding single quote as a string. |
| 1315 | bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { |
| 1316 | assert((StrLoc.getPointer() != NULL) && |
| 1317 | "Argument to the function cannot be a NULL value"); |
| 1318 | const char *CharPtr = StrLoc.getPointer(); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 1319 | while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && |
| 1320 | (*CharPtr != '\0')) { |
| 1321 | if (*CharPtr == '!') |
| 1322 | CharPtr++; |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1323 | CharPtr++; |
| 1324 | } |
| 1325 | if (*CharPtr == '>') { |
| 1326 | EndLoc = StrLoc.getFromPointer(CharPtr + 1); |
| 1327 | return true; |
| 1328 | } |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1332 | /// \brief creating a string without the escape characters '!'. |
| 1333 | void AsmParser::altMacroString(StringRef AltMacroStr,std::string &Res) { |
Michael Zuckerman | ff29879 | 2017-05-10 14:00:57 +0000 | [diff] [blame] | 1334 | for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1335 | if (AltMacroStr[Pos] == '!') |
| 1336 | Pos++; |
| 1337 | Res += AltMacroStr[Pos]; |
| 1338 | } |
| 1339 | } |
| 1340 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1341 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1342 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1343 | /// expr ::= expr &&,|| expr -> lowest. |
| 1344 | /// expr ::= expr |,^,&,! expr |
| 1345 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1346 | /// expr ::= expr <<,>> expr |
| 1347 | /// expr ::= expr +,- expr |
| 1348 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1349 | /// expr ::= primaryexpr |
| 1350 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1351 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1352 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1353 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1354 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1355 | return true; |
| 1356 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1357 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1358 | // expression to include the modifier. This is inefficient, but in general we |
| 1359 | // expect users to use 'a@modifier op b'. |
| 1360 | if (Lexer.getKind() == AsmToken::At) { |
| 1361 | Lex(); |
| 1362 | |
| 1363 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1364 | return TokError("unexpected symbol modifier following '@'"); |
| 1365 | |
| 1366 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1367 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1368 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1369 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1370 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1371 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1372 | if (!ModifiedRes) { |
| 1373 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1374 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1375 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1376 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1377 | Res = ModifiedRes; |
| 1378 | Lex(); |
| 1379 | } |
| 1380 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1381 | // Try to constant fold it up front, if possible. |
| 1382 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1383 | if (Res->evaluateAsAbsolute(Value)) |
| 1384 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1385 | |
| 1386 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1387 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1388 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1389 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1390 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1391 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1394 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1395 | SMLoc &EndLoc) { |
| 1396 | if (parseParenExpr(Res, EndLoc)) |
| 1397 | return true; |
| 1398 | |
| 1399 | for (; ParenDepth > 0; --ParenDepth) { |
| 1400 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1401 | return true; |
| 1402 | |
| 1403 | // We don't Lex() the last RParen. |
| 1404 | // This is the same behavior as parseParenExpression(). |
| 1405 | if (ParenDepth - 1 > 0) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1406 | EndLoc = getTok().getEndLoc(); |
| 1407 | if (parseToken(AsmToken::RParen, |
| 1408 | "expected ')' in parentheses expression")) |
| 1409 | return true; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1415 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1416 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1417 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1418 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1419 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1420 | return true; |
| 1421 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1422 | if (!Expr->evaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1423 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1424 | |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1428 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1429 | MCBinaryExpr::Opcode &Kind, |
| 1430 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1431 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1432 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1433 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1434 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1435 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1436 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1437 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1438 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1439 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1440 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1441 | return 1; |
| 1442 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1443 | // Low Precedence: |, &, ^ |
| 1444 | // |
| 1445 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1446 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1447 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1448 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1449 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1450 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1451 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1452 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1453 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1454 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1455 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1456 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1457 | case AsmToken::EqualEqual: |
| 1458 | Kind = MCBinaryExpr::EQ; |
| 1459 | return 3; |
| 1460 | case AsmToken::ExclaimEqual: |
| 1461 | case AsmToken::LessGreater: |
| 1462 | Kind = MCBinaryExpr::NE; |
| 1463 | return 3; |
| 1464 | case AsmToken::Less: |
| 1465 | Kind = MCBinaryExpr::LT; |
| 1466 | return 3; |
| 1467 | case AsmToken::LessEqual: |
| 1468 | Kind = MCBinaryExpr::LTE; |
| 1469 | return 3; |
| 1470 | case AsmToken::Greater: |
| 1471 | Kind = MCBinaryExpr::GT; |
| 1472 | return 3; |
| 1473 | case AsmToken::GreaterEqual: |
| 1474 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1475 | return 3; |
| 1476 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1477 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1478 | case AsmToken::LessLess: |
| 1479 | Kind = MCBinaryExpr::Shl; |
| 1480 | return 4; |
| 1481 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1482 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1483 | return 4; |
| 1484 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1485 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1486 | case AsmToken::Plus: |
| 1487 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1488 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1489 | case AsmToken::Minus: |
| 1490 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1491 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1492 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1493 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1494 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1495 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1496 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1497 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1498 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1499 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1500 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1501 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1502 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1506 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1507 | MCBinaryExpr::Opcode &Kind, |
| 1508 | bool ShouldUseLogicalShr) { |
| 1509 | switch (K) { |
| 1510 | default: |
| 1511 | return 0; // not a binop. |
| 1512 | |
| 1513 | // Lowest Precedence: &&, || |
| 1514 | case AsmToken::AmpAmp: |
| 1515 | Kind = MCBinaryExpr::LAnd; |
| 1516 | return 2; |
| 1517 | case AsmToken::PipePipe: |
| 1518 | Kind = MCBinaryExpr::LOr; |
| 1519 | return 1; |
| 1520 | |
| 1521 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1522 | case AsmToken::EqualEqual: |
| 1523 | Kind = MCBinaryExpr::EQ; |
| 1524 | return 3; |
| 1525 | case AsmToken::ExclaimEqual: |
| 1526 | case AsmToken::LessGreater: |
| 1527 | Kind = MCBinaryExpr::NE; |
| 1528 | return 3; |
| 1529 | case AsmToken::Less: |
| 1530 | Kind = MCBinaryExpr::LT; |
| 1531 | return 3; |
| 1532 | case AsmToken::LessEqual: |
| 1533 | Kind = MCBinaryExpr::LTE; |
| 1534 | return 3; |
| 1535 | case AsmToken::Greater: |
| 1536 | Kind = MCBinaryExpr::GT; |
| 1537 | return 3; |
| 1538 | case AsmToken::GreaterEqual: |
| 1539 | Kind = MCBinaryExpr::GTE; |
| 1540 | return 3; |
| 1541 | |
| 1542 | // Low Intermediate Precedence: +, - |
| 1543 | case AsmToken::Plus: |
| 1544 | Kind = MCBinaryExpr::Add; |
| 1545 | return 4; |
| 1546 | case AsmToken::Minus: |
| 1547 | Kind = MCBinaryExpr::Sub; |
| 1548 | return 4; |
| 1549 | |
| 1550 | // High Intermediate Precedence: |, &, ^ |
| 1551 | // |
| 1552 | // FIXME: gas seems to support '!' as an infix operator? |
| 1553 | case AsmToken::Pipe: |
| 1554 | Kind = MCBinaryExpr::Or; |
| 1555 | return 5; |
| 1556 | case AsmToken::Caret: |
| 1557 | Kind = MCBinaryExpr::Xor; |
| 1558 | return 5; |
| 1559 | case AsmToken::Amp: |
| 1560 | Kind = MCBinaryExpr::And; |
| 1561 | return 5; |
| 1562 | |
| 1563 | // Highest Precedence: *, /, %, <<, >> |
| 1564 | case AsmToken::Star: |
| 1565 | Kind = MCBinaryExpr::Mul; |
| 1566 | return 6; |
| 1567 | case AsmToken::Slash: |
| 1568 | Kind = MCBinaryExpr::Div; |
| 1569 | return 6; |
| 1570 | case AsmToken::Percent: |
| 1571 | Kind = MCBinaryExpr::Mod; |
| 1572 | return 6; |
| 1573 | case AsmToken::LessLess: |
| 1574 | Kind = MCBinaryExpr::Shl; |
| 1575 | return 6; |
| 1576 | case AsmToken::GreaterGreater: |
| 1577 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1578 | return 6; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1583 | MCBinaryExpr::Opcode &Kind) { |
| 1584 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1585 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1586 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1587 | } |
| 1588 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1589 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1590 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1591 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1592 | SMLoc &EndLoc) { |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1593 | SMLoc StartLoc = Lexer.getLoc(); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 1594 | while (true) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1595 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1596 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1598 | // If the next token is lower precedence than we are allowed to eat, return |
| 1599 | // successfully with what we ate already. |
| 1600 | if (TokPrec < Precedence) |
| 1601 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1602 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1603 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1604 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1605 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1606 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1607 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1608 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1609 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1610 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1611 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1612 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1613 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1614 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1615 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1616 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1617 | // Merge LHS and RHS according to operator. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1618 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1622 | /// ParseStatement: |
| 1623 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1624 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1625 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1626 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1627 | MCAsmParserSemaCallback *SI) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1628 | assert(!hasPendingError() && "parseStatement started with pending error"); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1629 | // Eat initial spaces and comments |
| 1630 | while (Lexer.is(AsmToken::Space)) |
| 1631 | Lex(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1632 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1633 | // if this is a line comment we can drop it safely |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 1634 | if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1635 | getTok().getString().front() == '\n') |
| 1636 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1637 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1638 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1639 | } |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1640 | // Statements always start with an identifier. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1641 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1642 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1643 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1644 | int64_t LocalLabelVal = -1; |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1645 | if (Lexer.is(AsmToken::HashDirective)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1646 | return parseCppHashLineFilenameComment(IDLoc); |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1647 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1648 | if (Lexer.is(AsmToken::Integer)) { |
| 1649 | LocalLabelVal = getTok().getIntVal(); |
| 1650 | if (LocalLabelVal < 0) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1651 | if (!TheCondState.Ignore) { |
| 1652 | Lex(); // always eat a token |
| 1653 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1654 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1655 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1656 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1657 | IDVal = getTok().getString(); |
| 1658 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1659 | if (Lexer.getKind() != AsmToken::Colon) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1660 | if (!TheCondState.Ignore) { |
| 1661 | Lex(); // always eat a token |
| 1662 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1663 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1666 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1667 | // Treat '.' as a valid identifier in this context. |
| 1668 | Lex(); |
| 1669 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1670 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1671 | // Treat '{' as a valid identifier in this context. |
| 1672 | Lex(); |
| 1673 | IDVal = "{"; |
| 1674 | |
| 1675 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1676 | // Treat '}' as a valid identifier in this context. |
| 1677 | Lex(); |
| 1678 | IDVal = "}"; |
Yonghong Song | 06ff655 | 2017-09-12 17:55:23 +0000 | [diff] [blame] | 1679 | } else if (Lexer.is(AsmToken::Star) && |
| 1680 | getTargetParser().starIsStartOfStatement()) { |
| 1681 | // Accept '*' as a valid start of statement. |
| 1682 | Lex(); |
| 1683 | IDVal = "*"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1684 | } else if (parseIdentifier(IDVal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1685 | if (!TheCondState.Ignore) { |
| 1686 | Lex(); // always eat a token |
| 1687 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1688 | } |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1689 | IDVal = ""; |
| 1690 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1692 | // Handle conditional assembly here before checking for skipping. We |
| 1693 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1694 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1695 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1696 | DirectiveKindMap.find(IDVal); |
| 1697 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1698 | ? DK_NO_DIRECTIVE |
| 1699 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1700 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1701 | default: |
| 1702 | break; |
| 1703 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1704 | case DK_IFEQ: |
| 1705 | case DK_IFGE: |
| 1706 | case DK_IFGT: |
| 1707 | case DK_IFLE: |
| 1708 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1709 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1710 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1711 | case DK_IFB: |
| 1712 | return parseDirectiveIfb(IDLoc, true); |
| 1713 | case DK_IFNB: |
| 1714 | return parseDirectiveIfb(IDLoc, false); |
| 1715 | case DK_IFC: |
| 1716 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1717 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1718 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1719 | case DK_IFNC: |
| 1720 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1721 | case DK_IFNES: |
| 1722 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1723 | case DK_IFDEF: |
| 1724 | return parseDirectiveIfdef(IDLoc, true); |
| 1725 | case DK_IFNDEF: |
| 1726 | case DK_IFNOTDEF: |
| 1727 | return parseDirectiveIfdef(IDLoc, false); |
| 1728 | case DK_ELSEIF: |
| 1729 | return parseDirectiveElseIf(IDLoc); |
| 1730 | case DK_ELSE: |
| 1731 | return parseDirectiveElse(IDLoc); |
| 1732 | case DK_ENDIF: |
| 1733 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1734 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1735 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1736 | // Ignore the statement if in the middle of inactive conditional |
| 1737 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1738 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1739 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1740 | return false; |
| 1741 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1742 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1743 | // FIXME: Recurse on local labels? |
| 1744 | |
| 1745 | // See what kind of statement we have. |
| 1746 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1747 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1748 | if (!getTargetParser().isLabel(ID)) |
| 1749 | break; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1750 | if (checkForValidSection()) |
| 1751 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1753 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1754 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1755 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1756 | // Diagnose attempt to use '.' as a label. |
| 1757 | if (IDVal == ".") |
| 1758 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1759 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1760 | // Diagnose attempt to use a variable as a label. |
| 1761 | // |
| 1762 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1763 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1764 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1765 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1766 | if (LocalLabelVal == -1) { |
| 1767 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1768 | StringRef RewrittenLabel = |
| 1769 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1770 | assert(!RewrittenLabel.empty() && |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1771 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1772 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1773 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1774 | IDVal = RewrittenLabel; |
| 1775 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1776 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1777 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1778 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
Nirav Dave | 9263ae3 | 2016-08-02 19:17:54 +0000 | [diff] [blame] | 1779 | // End of Labels should be treated as end of line for lexing |
| 1780 | // purposes but that information is not available to the Lexer who |
| 1781 | // does not understand Labels. This may cause us to see a Hash |
| 1782 | // here instead of a preprocessor line comment. |
| 1783 | if (getTok().is(AsmToken::Hash)) { |
| 1784 | StringRef CommentStr = parseStringToEndOfStatement(); |
| 1785 | Lexer.Lex(); |
| 1786 | Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); |
| 1787 | } |
| 1788 | |
Nirav Dave | 8ea792d | 2016-07-13 14:03:12 +0000 | [diff] [blame] | 1789 | // Consume any end of statement token, if present, to avoid spurious |
| 1790 | // AddBlankLine calls(). |
| 1791 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 1792 | Lex(); |
| 1793 | } |
| 1794 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1795 | // Emit the label. |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 1796 | if (!getTargetParser().isParsingInlineAsm()) |
Rafael Espindola | be99157 | 2017-02-10 15:13:12 +0000 | [diff] [blame] | 1797 | Out.EmitLabel(Sym, IDLoc); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1798 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1799 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1800 | // info to make a dwarf label entry for this label if needed. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 1801 | if (enabledGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1802 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1803 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1804 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1805 | getTargetParser().onLabelParsed(Sym); |
| 1806 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1807 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1808 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1809 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1810 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1811 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1812 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1813 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1814 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1815 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1816 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1817 | |
| 1818 | default: // Normal instruction or directive. |
| 1819 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1820 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1821 | |
| 1822 | // 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] | 1823 | if (areMacrosEnabled()) |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 1824 | if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1825 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1826 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1827 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1828 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1829 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1830 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1831 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1832 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1833 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1834 | // 1. The target-specific assembly parser. Some directives are target |
| 1835 | // specific or may potentially behave differently on certain targets. |
| 1836 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1837 | // (like the ELF parser) register themselves as extensions. |
| 1838 | // 3. The generic directive parser implemented by this class. These are |
| 1839 | // all the directives that behave in a target and platform independent |
| 1840 | // manner, or at least have a default behavior that's shared between |
| 1841 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1842 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 1843 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 1844 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1845 | SMLoc StartTokLoc = getTok().getLoc(); |
| 1846 | bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); |
| 1847 | |
| 1848 | if (hasPendingError()) |
| 1849 | return true; |
| 1850 | // Currently the return value should be true if we are |
| 1851 | // uninterested but as this is at odds with the standard parsing |
| 1852 | // convention (return true = error) we have instances of a parsed |
| 1853 | // directive that fails returning true as an error. Catch these |
| 1854 | // cases as best as possible errors here. |
| 1855 | if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) |
| 1856 | return true; |
| 1857 | // Return if we did some parsing or believe we succeeded. |
| 1858 | if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1859 | return false; |
| 1860 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1861 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1862 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1863 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1864 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1865 | if (Handler.first) |
| 1866 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1867 | |
| 1868 | // Finally, if no one else is interested in this directive, it must be |
| 1869 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1870 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1871 | default: |
| 1872 | break; |
| 1873 | case DK_SET: |
| 1874 | case DK_EQU: |
| 1875 | return parseDirectiveSet(IDVal, true); |
| 1876 | case DK_EQUIV: |
| 1877 | return parseDirectiveSet(IDVal, false); |
| 1878 | case DK_ASCII: |
| 1879 | return parseDirectiveAscii(IDVal, false); |
| 1880 | case DK_ASCIZ: |
| 1881 | case DK_STRING: |
| 1882 | return parseDirectiveAscii(IDVal, true); |
| 1883 | case DK_BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1884 | case DK_DC_B: |
| 1885 | return parseDirectiveValue(IDVal, 1); |
| 1886 | case DK_DC: |
| 1887 | case DK_DC_W: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1888 | case DK_SHORT: |
| 1889 | case DK_VALUE: |
| 1890 | case DK_2BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1891 | return parseDirectiveValue(IDVal, 2); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1892 | case DK_LONG: |
| 1893 | case DK_INT: |
| 1894 | case DK_4BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1895 | case DK_DC_L: |
| 1896 | return parseDirectiveValue(IDVal, 4); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1897 | case DK_QUAD: |
| 1898 | case DK_8BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1899 | return parseDirectiveValue(IDVal, 8); |
| 1900 | case DK_DC_A: |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1901 | return parseDirectiveValue( |
| 1902 | IDVal, getContext().getAsmInfo()->getCodePointerSize()); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1903 | case DK_OCTA: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1904 | return parseDirectiveOctaValue(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1905 | case DK_SINGLE: |
| 1906 | case DK_FLOAT: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1907 | case DK_DC_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1908 | return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1909 | case DK_DOUBLE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1910 | case DK_DC_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1911 | return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1912 | case DK_ALIGN: { |
| 1913 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1914 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1915 | } |
| 1916 | case DK_ALIGN32: { |
| 1917 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1918 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1919 | } |
| 1920 | case DK_BALIGN: |
| 1921 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1922 | case DK_BALIGNW: |
| 1923 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1924 | case DK_BALIGNL: |
| 1925 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1926 | case DK_P2ALIGN: |
| 1927 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1928 | case DK_P2ALIGNW: |
| 1929 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1930 | case DK_P2ALIGNL: |
| 1931 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1932 | case DK_ORG: |
| 1933 | return parseDirectiveOrg(); |
| 1934 | case DK_FILL: |
| 1935 | return parseDirectiveFill(); |
| 1936 | case DK_ZERO: |
| 1937 | return parseDirectiveZero(); |
| 1938 | case DK_EXTERN: |
| 1939 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1940 | return false; |
| 1941 | case DK_GLOBL: |
| 1942 | case DK_GLOBAL: |
| 1943 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1944 | case DK_LAZY_REFERENCE: |
| 1945 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1946 | case DK_NO_DEAD_STRIP: |
| 1947 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1948 | case DK_SYMBOL_RESOLVER: |
| 1949 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1950 | case DK_PRIVATE_EXTERN: |
| 1951 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1952 | case DK_REFERENCE: |
| 1953 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1954 | case DK_WEAK_DEFINITION: |
| 1955 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1956 | case DK_WEAK_REFERENCE: |
| 1957 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1958 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1959 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1960 | case DK_COMM: |
| 1961 | case DK_COMMON: |
| 1962 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1963 | case DK_LCOMM: |
| 1964 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1965 | case DK_ABORT: |
| 1966 | return parseDirectiveAbort(); |
| 1967 | case DK_INCLUDE: |
| 1968 | return parseDirectiveInclude(); |
| 1969 | case DK_INCBIN: |
| 1970 | return parseDirectiveIncbin(); |
| 1971 | case DK_CODE16: |
| 1972 | case DK_CODE16GCC: |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1973 | return TokError(Twine(IDVal) + |
| 1974 | " not currently supported for this target"); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1975 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1976 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1977 | case DK_IRP: |
| 1978 | return parseDirectiveIrp(IDLoc); |
| 1979 | case DK_IRPC: |
| 1980 | return parseDirectiveIrpc(IDLoc); |
| 1981 | case DK_ENDR: |
| 1982 | return parseDirectiveEndr(IDLoc); |
| 1983 | case DK_BUNDLE_ALIGN_MODE: |
| 1984 | return parseDirectiveBundleAlignMode(); |
| 1985 | case DK_BUNDLE_LOCK: |
| 1986 | return parseDirectiveBundleLock(); |
| 1987 | case DK_BUNDLE_UNLOCK: |
| 1988 | return parseDirectiveBundleUnlock(); |
| 1989 | case DK_SLEB128: |
| 1990 | return parseDirectiveLEB128(true); |
| 1991 | case DK_ULEB128: |
| 1992 | return parseDirectiveLEB128(false); |
| 1993 | case DK_SPACE: |
| 1994 | case DK_SKIP: |
| 1995 | return parseDirectiveSpace(IDVal); |
| 1996 | case DK_FILE: |
| 1997 | return parseDirectiveFile(IDLoc); |
| 1998 | case DK_LINE: |
| 1999 | return parseDirectiveLine(); |
| 2000 | case DK_LOC: |
| 2001 | return parseDirectiveLoc(); |
| 2002 | case DK_STABS: |
| 2003 | return parseDirectiveStabs(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2004 | case DK_CV_FILE: |
| 2005 | return parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 2006 | case DK_CV_FUNC_ID: |
| 2007 | return parseDirectiveCVFuncId(); |
| 2008 | case DK_CV_INLINE_SITE_ID: |
| 2009 | return parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2010 | case DK_CV_LOC: |
| 2011 | return parseDirectiveCVLoc(); |
| 2012 | case DK_CV_LINETABLE: |
| 2013 | return parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 2014 | case DK_CV_INLINE_LINETABLE: |
| 2015 | return parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 2016 | case DK_CV_DEF_RANGE: |
| 2017 | return parseDirectiveCVDefRange(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2018 | case DK_CV_STRINGTABLE: |
| 2019 | return parseDirectiveCVStringTable(); |
| 2020 | case DK_CV_FILECHECKSUMS: |
| 2021 | return parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 2022 | case DK_CV_FILECHECKSUM_OFFSET: |
| 2023 | return parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 2024 | case DK_CV_FPO_DATA: |
| 2025 | return parseDirectiveCVFPOData(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2026 | case DK_CFI_SECTIONS: |
| 2027 | return parseDirectiveCFISections(); |
| 2028 | case DK_CFI_STARTPROC: |
| 2029 | return parseDirectiveCFIStartProc(); |
| 2030 | case DK_CFI_ENDPROC: |
| 2031 | return parseDirectiveCFIEndProc(); |
| 2032 | case DK_CFI_DEF_CFA: |
| 2033 | return parseDirectiveCFIDefCfa(IDLoc); |
| 2034 | case DK_CFI_DEF_CFA_OFFSET: |
| 2035 | return parseDirectiveCFIDefCfaOffset(); |
| 2036 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 2037 | return parseDirectiveCFIAdjustCfaOffset(); |
| 2038 | case DK_CFI_DEF_CFA_REGISTER: |
| 2039 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 2040 | case DK_CFI_OFFSET: |
| 2041 | return parseDirectiveCFIOffset(IDLoc); |
| 2042 | case DK_CFI_REL_OFFSET: |
| 2043 | return parseDirectiveCFIRelOffset(IDLoc); |
| 2044 | case DK_CFI_PERSONALITY: |
| 2045 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 2046 | case DK_CFI_LSDA: |
| 2047 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 2048 | case DK_CFI_REMEMBER_STATE: |
| 2049 | return parseDirectiveCFIRememberState(); |
| 2050 | case DK_CFI_RESTORE_STATE: |
| 2051 | return parseDirectiveCFIRestoreState(); |
| 2052 | case DK_CFI_SAME_VALUE: |
| 2053 | return parseDirectiveCFISameValue(IDLoc); |
| 2054 | case DK_CFI_RESTORE: |
| 2055 | return parseDirectiveCFIRestore(IDLoc); |
| 2056 | case DK_CFI_ESCAPE: |
| 2057 | return parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 2058 | case DK_CFI_RETURN_COLUMN: |
| 2059 | return parseDirectiveCFIReturnColumn(IDLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2060 | case DK_CFI_SIGNAL_FRAME: |
| 2061 | return parseDirectiveCFISignalFrame(); |
| 2062 | case DK_CFI_UNDEFINED: |
| 2063 | return parseDirectiveCFIUndefined(IDLoc); |
| 2064 | case DK_CFI_REGISTER: |
| 2065 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 2066 | case DK_CFI_WINDOW_SAVE: |
| 2067 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2068 | case DK_MACROS_ON: |
| 2069 | case DK_MACROS_OFF: |
| 2070 | return parseDirectiveMacrosOnOff(IDVal); |
| 2071 | case DK_MACRO: |
| 2072 | return parseDirectiveMacro(IDLoc); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2073 | case DK_ALTMACRO: |
| 2074 | case DK_NOALTMACRO: |
| 2075 | return parseDirectiveAltmacro(IDVal); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2076 | case DK_EXITM: |
| 2077 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2078 | case DK_ENDM: |
| 2079 | case DK_ENDMACRO: |
| 2080 | return parseDirectiveEndMacro(IDVal); |
| 2081 | case DK_PURGEM: |
| 2082 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 2083 | case DK_END: |
| 2084 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 2085 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 2086 | return parseDirectiveError(IDLoc, false); |
| 2087 | case DK_ERROR: |
| 2088 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 2089 | case DK_WARNING: |
| 2090 | return parseDirectiveWarning(IDLoc); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2091 | case DK_RELOC: |
| 2092 | return parseDirectiveReloc(IDLoc); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2093 | case DK_DCB: |
| 2094 | case DK_DCB_W: |
| 2095 | return parseDirectiveDCB(IDVal, 2); |
| 2096 | case DK_DCB_B: |
| 2097 | return parseDirectiveDCB(IDVal, 1); |
| 2098 | case DK_DCB_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2099 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2100 | case DK_DCB_L: |
| 2101 | return parseDirectiveDCB(IDVal, 4); |
| 2102 | case DK_DCB_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2103 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2104 | case DK_DC_X: |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2105 | case DK_DCB_X: |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2106 | return TokError(Twine(IDVal) + |
| 2107 | " not currently supported for this target"); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 2108 | case DK_DS: |
| 2109 | case DK_DS_W: |
| 2110 | return parseDirectiveDS(IDVal, 2); |
| 2111 | case DK_DS_B: |
| 2112 | return parseDirectiveDS(IDVal, 1); |
| 2113 | case DK_DS_D: |
| 2114 | return parseDirectiveDS(IDVal, 8); |
| 2115 | case DK_DS_L: |
| 2116 | case DK_DS_S: |
| 2117 | return parseDirectiveDS(IDVal, 4); |
| 2118 | case DK_DS_P: |
| 2119 | case DK_DS_X: |
| 2120 | return parseDirectiveDS(IDVal, 12); |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 2121 | case DK_PRINT: |
| 2122 | return parseDirectivePrint(IDLoc); |
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. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 2170 | if (!ParseHadError && enabledGenDwarfForAssembly() && |
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 | |
Graydon Hoare | 54fe208 | 2018-04-07 00:44:02 +0000 | [diff] [blame] | 2188 | unsigned CppHashLocLineNo = |
| 2189 | SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2190 | Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2191 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2192 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2193 | getStreamer().EmitDwarfLocDirective( |
| 2194 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 2195 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 2196 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 2199 | // If parsing succeeded, match the instruction. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2200 | if (!ParseHadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 2201 | uint64_t ErrorInfo; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 2202 | if (getTargetParser().MatchAndEmitInstruction( |
| 2203 | IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| 2204 | getTargetParser().isParsingInlineAsm())) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2205 | return true; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 2206 | } |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 2207 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 2208 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 2209 | |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2210 | // Parse and erase curly braces marking block start/end |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame^] | 2211 | bool |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2212 | AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { |
| 2213 | // Identify curly brace marking block start/end |
| 2214 | if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) |
| 2215 | return false; |
| 2216 | |
| 2217 | SMLoc StartLoc = Lexer.getLoc(); |
| 2218 | Lex(); // Eat the brace |
| 2219 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2220 | Lex(); // Eat EndOfStatement following the brace |
| 2221 | |
| 2222 | // Erase the block start/end brace from the output asm string |
| 2223 | AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - |
| 2224 | StartLoc.getPointer()); |
| 2225 | return true; |
| 2226 | } |
| 2227 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2228 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2229 | /// ::= # number "filename" |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2230 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2231 | Lex(); // Eat the hash token. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2232 | // Lexer only ever emits HashDirective if it fully formed if it's |
| 2233 | // done the checking already so this is an internal error. |
| 2234 | assert(getTok().is(AsmToken::Integer) && |
| 2235 | "Lexing Cpp line comment: Expected Integer"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2236 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2237 | Lex(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2238 | assert(getTok().is(AsmToken::String) && |
| 2239 | "Lexing Cpp line comment: Expected String"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2240 | StringRef Filename = getTok().getString(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2241 | Lex(); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2242 | |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2243 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2244 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2245 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2246 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2247 | CppHashInfo.Loc = L; |
| 2248 | CppHashInfo.Filename = Filename; |
| 2249 | CppHashInfo.LineNumber = LineNumber; |
| 2250 | CppHashInfo.Buf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2251 | return false; |
| 2252 | } |
| 2253 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2254 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2255 | /// for the Filename and LineNo if any in the diagnostic. |
| 2256 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2257 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2258 | raw_ostream &OS = errs(); |
| 2259 | |
| 2260 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2261 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2262 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2263 | unsigned CppHashBuf = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2264 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2265 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2266 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2267 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2268 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2269 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 2270 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2271 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 2272 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2275 | // 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] | 2276 | // manager changed or buffer changed (like in a nested include) then just |
| 2277 | // print the normal diagnostic using its Filename and LineNo. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2278 | if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2279 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2280 | if (Parser->SavedDiagHandler) |
| 2281 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 2282 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2283 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2284 | return; |
| 2285 | } |
| 2286 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2287 | // Use the CppHashFilename and calculate a line number based on the |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2288 | // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc |
| 2289 | // for the diagnostic. |
| 2290 | const std::string &Filename = Parser->CppHashInfo.Filename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2291 | |
| 2292 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 2293 | int CppHashLocLineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2294 | Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2295 | int LineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2296 | Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2297 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2298 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 2299 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 2300 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2301 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2302 | if (Parser->SavedDiagHandler) |
| 2303 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 2304 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2305 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2308 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 2309 | // difference being that that function accepts '@' as part of identifiers and |
| 2310 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 2311 | // '@' as a special case when needed. |
| 2312 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2313 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 2314 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2317 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 2318 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2319 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2320 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2321 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2322 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2323 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2324 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2325 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2326 | // A macro without parameters is handled differently on Darwin: |
| 2327 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2328 | while (!Body.empty()) { |
| 2329 | // Scan for the next substitution. |
| 2330 | std::size_t End = Body.size(), Pos = 0; |
| 2331 | for (; Pos != End; ++Pos) { |
| 2332 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2333 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2334 | // This macro has no parameters, look for $0, $1, etc. |
| 2335 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 2336 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2337 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2338 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2339 | if (Next == '$' || Next == 'n' || |
| 2340 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } else { |
| 2343 | // This macro has parameters, look for \foo, \bar, etc. |
| 2344 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 2345 | break; |
| 2346 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | // Add the prefix. |
| 2350 | OS << Body.slice(0, Pos); |
| 2351 | |
| 2352 | // Check if we reached the end. |
| 2353 | if (Pos == End) |
| 2354 | break; |
| 2355 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2356 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2357 | switch (Body[Pos + 1]) { |
| 2358 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2359 | case '$': |
| 2360 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2361 | break; |
| 2362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2363 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2364 | case 'n': |
| 2365 | OS << A.size(); |
| 2366 | break; |
| 2367 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2368 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2369 | default: { |
| 2370 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2371 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2372 | if (Index >= A.size()) |
| 2373 | break; |
| 2374 | |
| 2375 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2376 | for (const AsmToken &Token : A[Index]) |
| 2377 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | Pos += 2; |
| 2382 | } else { |
| 2383 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2384 | |
| 2385 | // Check for the \@ pseudo-variable. |
| 2386 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2387 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2388 | else |
| 2389 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 2390 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2391 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2392 | const char *Begin = Body.data() + Pos + 1; |
| 2393 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2394 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2395 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2396 | if (Argument == "@") { |
| 2397 | OS << NumOfMacroInstantiations; |
| 2398 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2399 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2400 | for (; Index < NParameters; ++Index) |
| 2401 | if (Parameters[Index].Name == Argument) |
| 2402 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2403 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2404 | if (Index == NParameters) { |
| 2405 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 2406 | Pos += 3; |
| 2407 | else { |
| 2408 | OS << '\\' << Argument; |
| 2409 | Pos = I; |
| 2410 | } |
| 2411 | } else { |
| 2412 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2413 | for (const AsmToken &Token : A[Index]) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2414 | // For altmacro mode, you can write '%expr'. |
| 2415 | // The prefix '%' evaluates the expression 'expr' |
| 2416 | // and uses the result as a string (e.g. replace %(1+2) with the string "3"). |
| 2417 | // Here, we identify the integer token which is the result of the |
| 2418 | // absolute expression evaluation and replace it with its string representation. |
| 2419 | if ((Lexer.IsaAltMacroMode()) && |
| 2420 | (*(Token.getString().begin()) == '%') && Token.is(AsmToken::Integer)) |
| 2421 | // Emit an integer value to the buffer. |
| 2422 | OS << Token.getIntVal(); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2423 | // Only Token that was validated as a string and begins with '<' |
| 2424 | // is considered altMacroString!!! |
| 2425 | else if ((Lexer.IsaAltMacroMode()) && |
| 2426 | (*(Token.getString().begin()) == '<') && |
| 2427 | Token.is(AsmToken::String)) { |
| 2428 | std::string Res; |
| 2429 | altMacroString(Token.getStringContents(), Res); |
| 2430 | OS << Res; |
| 2431 | } |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2432 | // We expect no quotes around the string's contents when |
| 2433 | // parsing for varargs. |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2434 | else if (Token.isNot(AsmToken::String) || VarargParameter) |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2435 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2436 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2437 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2438 | |
| 2439 | Pos += 1 + Argument.size(); |
| 2440 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2441 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2442 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2443 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2444 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2445 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2446 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2447 | return false; |
| 2448 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2449 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2450 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2451 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2452 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2453 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2454 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2455 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2456 | switch (kind) { |
| 2457 | default: |
| 2458 | return false; |
| 2459 | case AsmToken::Plus: |
| 2460 | case AsmToken::Minus: |
| 2461 | case AsmToken::Tilde: |
| 2462 | case AsmToken::Slash: |
| 2463 | case AsmToken::Star: |
| 2464 | case AsmToken::Dot: |
| 2465 | case AsmToken::Equal: |
| 2466 | case AsmToken::EqualEqual: |
| 2467 | case AsmToken::Pipe: |
| 2468 | case AsmToken::PipePipe: |
| 2469 | case AsmToken::Caret: |
| 2470 | case AsmToken::Amp: |
| 2471 | case AsmToken::AmpAmp: |
| 2472 | case AsmToken::Exclaim: |
| 2473 | case AsmToken::ExclaimEqual: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2474 | case AsmToken::Less: |
| 2475 | case AsmToken::LessEqual: |
| 2476 | case AsmToken::LessLess: |
| 2477 | case AsmToken::LessGreater: |
| 2478 | case AsmToken::Greater: |
| 2479 | case AsmToken::GreaterEqual: |
| 2480 | case AsmToken::GreaterGreater: |
| 2481 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2485 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2486 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2487 | class AsmLexerSkipSpaceRAII { |
| 2488 | public: |
| 2489 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2490 | Lexer.setSkipSpace(SkipSpace); |
| 2491 | } |
| 2492 | |
| 2493 | ~AsmLexerSkipSpaceRAII() { |
| 2494 | Lexer.setSkipSpace(true); |
| 2495 | } |
| 2496 | |
| 2497 | private: |
| 2498 | AsmLexer &Lexer; |
| 2499 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2500 | |
| 2501 | } // end anonymous namespace |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2502 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2503 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2504 | |
| 2505 | if (Vararg) { |
| 2506 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2507 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2508 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2509 | } |
| 2510 | return false; |
| 2511 | } |
| 2512 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2513 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2514 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2515 | // Darwin doesn't use spaces to delmit arguments. |
| 2516 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2517 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2518 | bool SpaceEaten; |
| 2519 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2520 | while (true) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2521 | SpaceEaten = false; |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2522 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2523 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2524 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2525 | if (ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2526 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2527 | if (Lexer.is(AsmToken::Comma)) |
| 2528 | break; |
| 2529 | |
| 2530 | if (Lexer.is(AsmToken::Space)) { |
| 2531 | SpaceEaten = true; |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2532 | Lexer.Lex(); // Eat spaces |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2533 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2534 | |
| 2535 | // Spaces can delimit parameters, but could also be part an expression. |
| 2536 | // If the token after a space is an operator, add the token and the next |
| 2537 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2538 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2539 | if (isOperator(Lexer.getKind())) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2540 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2541 | Lexer.Lex(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2542 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2543 | // Whitespace after an operator can be ignored. |
| 2544 | if (Lexer.is(AsmToken::Space)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2545 | Lexer.Lex(); |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2546 | |
| 2547 | continue; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2550 | if (SpaceEaten) |
| 2551 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2552 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2553 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2554 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2555 | // to be able to fill in the remaining default parameter values |
| 2556 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2557 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Adjust the current parentheses level. |
| 2560 | if (Lexer.is(AsmToken::LParen)) |
| 2561 | ++ParenLevel; |
| 2562 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2563 | --ParenLevel; |
| 2564 | |
| 2565 | // Append the token to the current argument list. |
| 2566 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2567 | Lexer.Lex(); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2568 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2569 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2570 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2571 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2572 | return false; |
| 2573 | } |
| 2574 | |
| 2575 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2576 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2577 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2578 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2579 | bool NamedParametersFound = false; |
| 2580 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2581 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2582 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2583 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2584 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2585 | // Parse two kinds of macro invocations: |
| 2586 | // - macros defined without any parameters accept an arbitrary number of them |
| 2587 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2588 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2589 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2590 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2591 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2592 | MCAsmMacroParameter FA; |
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 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2595 | if (parseIdentifier(FA.Name)) |
| 2596 | return Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2597 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2598 | if (Lexer.isNot(AsmToken::Equal)) |
| 2599 | return TokError("expected '=' after formal parameter identifier"); |
| 2600 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2601 | Lex(); |
| 2602 | |
| 2603 | NamedParametersFound = true; |
| 2604 | } |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2605 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2606 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2607 | if (NamedParametersFound && FA.Name.empty()) |
| 2608 | return Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2609 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2610 | SMLoc StrLoc = Lexer.getLoc(); |
| 2611 | SMLoc EndLoc; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2612 | if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Percent)) { |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2613 | const MCExpr *AbsoluteExp; |
| 2614 | int64_t Value; |
| 2615 | /// Eat '%' |
| 2616 | Lex(); |
| 2617 | if (parseExpression(AbsoluteExp, EndLoc)) |
| 2618 | return false; |
| 2619 | if (!AbsoluteExp->evaluateAsAbsolute(Value)) |
| 2620 | return Error(StrLoc, "expected absolute expression"); |
| 2621 | const char *StrChar = StrLoc.getPointer(); |
| 2622 | const char *EndChar = EndLoc.getPointer(); |
| 2623 | AsmToken newToken(AsmToken::Integer, StringRef(StrChar , EndChar - StrChar), Value); |
| 2624 | FA.Value.push_back(newToken); |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2625 | } else if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Less) && |
| 2626 | isAltmacroString(StrLoc, EndLoc)) { |
| 2627 | const char *StrChar = StrLoc.getPointer(); |
| 2628 | const char *EndChar = EndLoc.getPointer(); |
| 2629 | jumpToLoc(EndLoc, CurBuffer); |
| 2630 | /// Eat from '<' to '>' |
| 2631 | Lex(); |
| 2632 | AsmToken newToken(AsmToken::String, StringRef(StrChar, EndChar - StrChar)); |
| 2633 | FA.Value.push_back(newToken); |
| 2634 | } else if(parseMacroArgument(FA.Value, Vararg)) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2635 | return true; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2636 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2637 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2638 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2639 | unsigned FAI = 0; |
| 2640 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2641 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2642 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2643 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2644 | if (FAI >= NParameters) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2645 | assert(M && "expected macro to be defined"); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2646 | return Error(IDLoc, "parameter named '" + FA.Name + |
| 2647 | "' does not exist for macro '" + M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2648 | } |
| 2649 | PI = FAI; |
| 2650 | } |
| 2651 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2652 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2653 | if (A.size() <= PI) |
| 2654 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2655 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2656 | |
| 2657 | if (FALocs.size() <= PI) |
| 2658 | FALocs.resize(PI + 1); |
| 2659 | |
| 2660 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2661 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2662 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2663 | // At the end of the statement, fill in remaining arguments that have |
| 2664 | // default values. If there aren't any, then the next argument is |
| 2665 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2666 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2667 | bool Failure = false; |
| 2668 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2669 | if (A[FAI].empty()) { |
| 2670 | if (M->Parameters[FAI].Required) { |
| 2671 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2672 | "missing value for required parameter " |
| 2673 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2674 | Failure = true; |
| 2675 | } |
| 2676 | |
| 2677 | if (!M->Parameters[FAI].Value.empty()) |
| 2678 | A[FAI] = M->Parameters[FAI].Value; |
| 2679 | } |
| 2680 | } |
| 2681 | return Failure; |
| 2682 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2683 | |
| 2684 | if (Lexer.is(AsmToken::Comma)) |
| 2685 | Lex(); |
| 2686 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2687 | |
| 2688 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2691 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 2692 | // Arbitrarily limit macro nesting depth (default matches 'as'). We can |
| 2693 | // eliminate this, although we should protect against infinite loops. |
| 2694 | unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; |
| 2695 | if (ActiveMacros.size() == MaxNestingDepth) { |
| 2696 | std::ostringstream MaxNestingDepthError; |
| 2697 | MaxNestingDepthError << "macros cannot be nested more than " |
| 2698 | << MaxNestingDepth << " levels deep." |
| 2699 | << " Use -asm-macro-max-nesting-depth to increase " |
| 2700 | "this limit."; |
| 2701 | return TokError(MaxNestingDepthError.str()); |
| 2702 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2703 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2704 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2705 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2706 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2707 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2708 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2709 | // to hold the macro body with substitutions. |
| 2710 | SmallString<256> Buf; |
| 2711 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2712 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2713 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2714 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2715 | return true; |
| 2716 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2717 | // 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] | 2718 | // instantiation. |
| 2719 | OS << ".endmacro\n"; |
| 2720 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2721 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2722 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2723 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2724 | // Create the macro instantiation object and add to the current macro |
| 2725 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2726 | MacroInstantiation *MI = new MacroInstantiation( |
| 2727 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2728 | ActiveMacros.push_back(MI); |
| 2729 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2730 | ++NumOfMacroInstantiations; |
| 2731 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2732 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2733 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2734 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2735 | Lex(); |
| 2736 | |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2740 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2741 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2742 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2743 | Lex(); |
| 2744 | |
| 2745 | // Pop the instantiation entry. |
| 2746 | delete ActiveMacros.back(); |
| 2747 | ActiveMacros.pop_back(); |
| 2748 | } |
| 2749 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2750 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2751 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2752 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2753 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2754 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| 2755 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2756 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2757 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2758 | if (!Sym) { |
| 2759 | // In the case where we parse an expression starting with a '.', we will |
| 2760 | // not generate an error, nor will we create a symbol. In this case we |
| 2761 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2762 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2763 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2764 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2765 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2766 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2767 | if (NoDeadStrip) |
| 2768 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2769 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2770 | return false; |
| 2771 | } |
| 2772 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2773 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2774 | /// ::= identifier |
| 2775 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2776 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2777 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2778 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2779 | // 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] | 2780 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2781 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2782 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2783 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2784 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2785 | // Consume the prefix character, and check for a following identifier. |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2786 | |
| 2787 | AsmToken Buf[1]; |
| 2788 | Lexer.peekTokens(Buf, false); |
| 2789 | |
| 2790 | if (Buf[0].isNot(AsmToken::Identifier)) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2791 | return true; |
| 2792 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2793 | // 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] | 2794 | if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2795 | return true; |
| 2796 | |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2797 | // eat $ or @ |
| 2798 | Lexer.Lex(); // Lexer's Lex guarantees consecutive token. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2799 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2800 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2801 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2802 | Lex(); // Parser Lex to maintain invariants. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2803 | return false; |
| 2804 | } |
| 2805 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2806 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2807 | return true; |
| 2808 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2809 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2810 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2811 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2812 | |
| 2813 | return false; |
| 2814 | } |
| 2815 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2816 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2817 | /// ::= .equ identifier ',' expression |
| 2818 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2819 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2820 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2821 | StringRef Name; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2822 | if (check(parseIdentifier(Name), "expected identifier") || |
| 2823 | parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) |
| 2824 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
| 2825 | return false; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2828 | bool AsmParser::parseEscapedString(std::string &Data) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2829 | if (check(getTok().isNot(AsmToken::String), "expected string")) |
| 2830 | return true; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2831 | |
| 2832 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2833 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2834 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2835 | if (Str[i] != '\\') { |
| 2836 | Data += Str[i]; |
| 2837 | continue; |
| 2838 | } |
| 2839 | |
| 2840 | // Recognize escaped characters. Note that this escape semantics currently |
| 2841 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2842 | ++i; |
| 2843 | if (i == e) |
| 2844 | return TokError("unexpected backslash at end of string"); |
| 2845 | |
| 2846 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2847 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2848 | // Consume up to three octal characters. |
| 2849 | unsigned Value = Str[i] - '0'; |
| 2850 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2851 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2852 | ++i; |
| 2853 | Value = Value * 8 + (Str[i] - '0'); |
| 2854 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2855 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2856 | ++i; |
| 2857 | Value = Value * 8 + (Str[i] - '0'); |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | if (Value > 255) |
| 2862 | return TokError("invalid octal escape sequence (out of range)"); |
| 2863 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2864 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2865 | continue; |
| 2866 | } |
| 2867 | |
| 2868 | // Otherwise recognize individual escapes. |
| 2869 | switch (Str[i]) { |
| 2870 | default: |
| 2871 | // Just reject invalid escape sequences for now. |
| 2872 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2873 | |
| 2874 | case 'b': Data += '\b'; break; |
| 2875 | case 'f': Data += '\f'; break; |
| 2876 | case 'n': Data += '\n'; break; |
| 2877 | case 'r': Data += '\r'; break; |
| 2878 | case 't': Data += '\t'; break; |
| 2879 | case '"': Data += '"'; break; |
| 2880 | case '\\': Data += '\\'; break; |
| 2881 | } |
| 2882 | } |
| 2883 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2884 | Lex(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2885 | return false; |
| 2886 | } |
| 2887 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2888 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2889 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2890 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2891 | auto parseOp = [&]() -> bool { |
| 2892 | std::string Data; |
| 2893 | if (checkForValidSection() || parseEscapedString(Data)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2894 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2895 | getStreamer().EmitBytes(Data); |
| 2896 | if (ZeroTerminated) |
| 2897 | getStreamer().EmitBytes(StringRef("\0", 1)); |
| 2898 | return false; |
| 2899 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2900 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2901 | if (parseMany(parseOp)) |
| 2902 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2903 | return false; |
| 2904 | } |
| 2905 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2906 | /// parseDirectiveReloc |
| 2907 | /// ::= .reloc expression , identifier [ , expression ] |
| 2908 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
| 2909 | const MCExpr *Offset; |
| 2910 | const MCExpr *Expr = nullptr; |
| 2911 | |
| 2912 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2913 | int64_t OffsetValue; |
| 2914 | // We can only deal with constant expressions at the moment. |
| 2915 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2916 | if (parseExpression(Offset)) |
| 2917 | return true; |
| 2918 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2919 | if (check(!Offset->evaluateAsAbsolute(OffsetValue), OffsetLoc, |
| 2920 | "expression is not a constant value") || |
| 2921 | check(OffsetValue < 0, OffsetLoc, "expression is negative") || |
| 2922 | parseToken(AsmToken::Comma, "expected comma") || |
| 2923 | check(getTok().isNot(AsmToken::Identifier), "expected relocation name")) |
| 2924 | return true; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2925 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2926 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
| 2927 | StringRef Name = Lexer.getTok().getIdentifier(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2928 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2929 | |
| 2930 | if (Lexer.is(AsmToken::Comma)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2931 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2932 | SMLoc ExprLoc = Lexer.getLoc(); |
| 2933 | if (parseExpression(Expr)) |
| 2934 | return true; |
| 2935 | |
| 2936 | MCValue Value; |
| 2937 | if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) |
| 2938 | return Error(ExprLoc, "expression must be relocatable"); |
| 2939 | } |
| 2940 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2941 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 2942 | "unexpected token in .reloc directive")) |
| 2943 | return true; |
| 2944 | |
| 2945 | if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc)) |
| 2946 | return Error(NameLoc, "unknown relocation name"); |
| 2947 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2948 | return false; |
| 2949 | } |
| 2950 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2951 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2952 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2953 | bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { |
| 2954 | auto parseOp = [&]() -> bool { |
| 2955 | const MCExpr *Value; |
| 2956 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2957 | if (checkForValidSection() || parseExpression(Value)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2958 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2959 | // Special case constant expressions to match code generator. |
| 2960 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2961 | assert(Size <= 8 && "Invalid size"); |
| 2962 | uint64_t IntValue = MCE->getValue(); |
| 2963 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2964 | return Error(ExprLoc, "out of range literal value"); |
| 2965 | getStreamer().EmitIntValue(IntValue, Size); |
| 2966 | } else |
| 2967 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 2968 | return false; |
| 2969 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2970 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2971 | if (parseMany(parseOp)) |
| 2972 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2973 | return false; |
| 2974 | } |
| 2975 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 2976 | static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) { |
| 2977 | if (Asm.getTok().isNot(AsmToken::Integer) && |
| 2978 | Asm.getTok().isNot(AsmToken::BigNum)) |
| 2979 | return Asm.TokError("unknown token in expression"); |
| 2980 | SMLoc ExprLoc = Asm.getTok().getLoc(); |
| 2981 | APInt IntValue = Asm.getTok().getAPIntVal(); |
| 2982 | Asm.Lex(); |
| 2983 | if (!IntValue.isIntN(128)) |
| 2984 | return Asm.Error(ExprLoc, "out of range literal value"); |
| 2985 | if (!IntValue.isIntN(64)) { |
| 2986 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
| 2987 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2988 | } else { |
| 2989 | hi = 0; |
| 2990 | lo = IntValue.getZExtValue(); |
| 2991 | } |
| 2992 | return false; |
| 2993 | } |
| 2994 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2995 | /// ParseDirectiveOctaValue |
| 2996 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2997 | |
| 2998 | bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { |
| 2999 | auto parseOp = [&]() -> bool { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3000 | if (checkForValidSection()) |
| 3001 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3002 | uint64_t hi, lo; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3003 | if (parseHexOcta(*this, hi, lo)) |
| 3004 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3005 | if (MAI.isLittleEndian()) { |
| 3006 | getStreamer().EmitIntValue(lo, 8); |
| 3007 | getStreamer().EmitIntValue(hi, 8); |
| 3008 | } else { |
| 3009 | getStreamer().EmitIntValue(hi, 8); |
| 3010 | getStreamer().EmitIntValue(lo, 8); |
| 3011 | } |
| 3012 | return false; |
| 3013 | }; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3014 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3015 | if (parseMany(parseOp)) |
| 3016 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3017 | return false; |
| 3018 | } |
| 3019 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 3020 | bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { |
| 3021 | // We don't truly support arithmetic on floating point expressions, so we |
| 3022 | // have to manually parse unary prefixes. |
| 3023 | bool IsNeg = false; |
| 3024 | if (getLexer().is(AsmToken::Minus)) { |
| 3025 | Lexer.Lex(); |
| 3026 | IsNeg = true; |
| 3027 | } else if (getLexer().is(AsmToken::Plus)) |
| 3028 | Lexer.Lex(); |
| 3029 | |
| 3030 | if (Lexer.is(AsmToken::Error)) |
| 3031 | return TokError(Lexer.getErr()); |
| 3032 | if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && |
| 3033 | Lexer.isNot(AsmToken::Identifier)) |
| 3034 | return TokError("unexpected token in directive"); |
| 3035 | |
| 3036 | // Convert to an APFloat. |
| 3037 | APFloat Value(Semantics); |
| 3038 | StringRef IDVal = getTok().getString(); |
| 3039 | if (getLexer().is(AsmToken::Identifier)) { |
| 3040 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 3041 | Value = APFloat::getInf(Semantics); |
| 3042 | else if (!IDVal.compare_lower("nan")) |
| 3043 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 3044 | else |
| 3045 | return TokError("invalid floating point literal"); |
| 3046 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
| 3047 | APFloat::opInvalidOp) |
| 3048 | return TokError("invalid floating point literal"); |
| 3049 | if (IsNeg) |
| 3050 | Value.changeSign(); |
| 3051 | |
| 3052 | // Consume the numeric token. |
| 3053 | Lex(); |
| 3054 | |
| 3055 | Res = Value.bitcastToAPInt(); |
| 3056 | |
| 3057 | return false; |
| 3058 | } |
| 3059 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3060 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3061 | /// ::= (.single | .double) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3062 | bool AsmParser::parseDirectiveRealValue(StringRef IDVal, |
| 3063 | const fltSemantics &Semantics) { |
| 3064 | auto parseOp = [&]() -> bool { |
| 3065 | APInt AsInt; |
| 3066 | if (checkForValidSection() || parseRealValue(Semantics, AsInt)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3067 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3068 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 3069 | AsInt.getBitWidth() / 8); |
| 3070 | return false; |
| 3071 | }; |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3072 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3073 | if (parseMany(parseOp)) |
| 3074 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3075 | return false; |
| 3076 | } |
| 3077 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3078 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3079 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3080 | bool AsmParser::parseDirectiveZero() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3081 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 3082 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3083 | if (checkForValidSection() || parseExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3084 | return true; |
| 3085 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3086 | int64_t Val = 0; |
| 3087 | if (getLexer().is(AsmToken::Comma)) { |
| 3088 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3089 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3090 | return true; |
| 3091 | } |
| 3092 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3093 | if (parseToken(AsmToken::EndOfStatement, |
| 3094 | "unexpected token in '.zero' directive")) |
| 3095 | return true; |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3096 | getStreamer().emitFill(*NumBytes, Val, NumBytesLoc); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3097 | |
| 3098 | return false; |
| 3099 | } |
| 3100 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3101 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3102 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3103 | bool AsmParser::parseDirectiveFill() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3104 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 3105 | const MCExpr *NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3106 | if (checkForValidSection() || parseExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3107 | return true; |
| 3108 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3109 | int64_t FillSize = 1; |
| 3110 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3111 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3112 | SMLoc SizeLoc, ExprLoc; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3113 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3114 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3115 | SizeLoc = getTok().getLoc(); |
| 3116 | if (parseAbsoluteExpression(FillSize)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3117 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3118 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3119 | ExprLoc = getTok().getLoc(); |
| 3120 | if (parseAbsoluteExpression(FillExpr)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3121 | return true; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3122 | } |
| 3123 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3124 | if (parseToken(AsmToken::EndOfStatement, |
| 3125 | "unexpected token in '.fill' directive")) |
| 3126 | return true; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3127 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3128 | if (FillSize < 0) { |
| 3129 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
Petr Hosek | 6abd38b | 2016-05-28 08:20:08 +0000 | [diff] [blame] | 3130 | return false; |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3131 | } |
| 3132 | if (FillSize > 8) { |
| 3133 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 3134 | FillSize = 8; |
| 3135 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3136 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3137 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 3138 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 3139 | |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3140 | getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3141 | |
| 3142 | return false; |
| 3143 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3144 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3145 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3146 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3147 | bool AsmParser::parseDirectiveOrg() { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 3148 | const MCExpr *Offset; |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3149 | SMLoc OffsetLoc = Lexer.getLoc(); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3150 | if (checkForValidSection() || parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3151 | return true; |
| 3152 | |
| 3153 | // Parse optional fill expression. |
| 3154 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3155 | if (parseOptionalToken(AsmToken::Comma)) |
| 3156 | if (parseAbsoluteExpression(FillExpr)) |
| 3157 | return addErrorSuffix(" in '.org' directive"); |
| 3158 | if (parseToken(AsmToken::EndOfStatement)) |
| 3159 | return addErrorSuffix(" in '.org' directive"); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3160 | |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3161 | getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3162 | return false; |
| 3163 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3164 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3165 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3166 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3167 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3168 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3169 | int64_t Alignment; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3170 | SMLoc MaxBytesLoc; |
| 3171 | bool HasFillExpr = false; |
| 3172 | int64_t FillExpr = 0; |
| 3173 | int64_t MaxBytesToFill = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3174 | |
| 3175 | auto parseAlign = [&]() -> bool { |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3176 | if (parseAbsoluteExpression(Alignment)) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3177 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3178 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3179 | // The fill expression can be omitted while specifying a maximum number of |
| 3180 | // alignment bytes, e.g: |
| 3181 | // .align 3,,4 |
| 3182 | if (getTok().isNot(AsmToken::Comma)) { |
| 3183 | HasFillExpr = true; |
| 3184 | if (parseAbsoluteExpression(FillExpr)) |
| 3185 | return true; |
| 3186 | } |
| 3187 | if (parseOptionalToken(AsmToken::Comma)) |
| 3188 | if (parseTokenLoc(MaxBytesLoc) || |
| 3189 | parseAbsoluteExpression(MaxBytesToFill)) |
| 3190 | return true; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3191 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3192 | return parseToken(AsmToken::EndOfStatement); |
| 3193 | }; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3194 | |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3195 | if (checkForValidSection()) |
| 3196 | return addErrorSuffix(" in directive"); |
| 3197 | // Ignore empty '.p2align' directives for GNU-as compatibility |
| 3198 | if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) { |
| 3199 | Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored"); |
| 3200 | return parseToken(AsmToken::EndOfStatement); |
| 3201 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3202 | if (parseAlign()) |
| 3203 | return addErrorSuffix(" in directive"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3204 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3205 | // Always emit an alignment here even if we thrown an error. |
| 3206 | bool ReturnVal = false; |
| 3207 | |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3208 | // Compute alignment in bytes. |
| 3209 | if (IsPow2) { |
| 3210 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3211 | if (Alignment >= 32) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3212 | ReturnVal |= Error(AlignmentLoc, "invalid alignment value"); |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3213 | Alignment = 31; |
| 3214 | } |
| 3215 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 3216 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3217 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 3218 | // Reject alignments that aren't either a power of two or zero, |
| 3219 | // for gas compatibility. Alignment of zero is silently rounded |
| 3220 | // up to one. |
| 3221 | if (Alignment == 0) |
| 3222 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3223 | if (!isPowerOf2_64(Alignment)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3224 | ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3227 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3228 | if (MaxBytesLoc.isValid()) { |
| 3229 | if (MaxBytesToFill < 1) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3230 | ReturnVal |= Error(MaxBytesLoc, |
| 3231 | "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3232 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 3233 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 3237 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3238 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3239 | MaxBytesToFill = 0; |
| 3240 | } |
| 3241 | } |
| 3242 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3243 | // Check whether we should use optimal code alignment for this .align |
| 3244 | // directive. |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 3245 | const MCSection *Section = getStreamer().getCurrentSectionOnly(); |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 3246 | assert(Section && "must have section to emit alignment"); |
| 3247 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3248 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 3249 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3250 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3251 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 3252 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 3253 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 3254 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3255 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3256 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3257 | return ReturnVal; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3260 | /// parseDirectiveFile |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3261 | /// ::= .file filename |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3262 | /// ::= .file number [directory] filename [md5 checksum] [source source-text] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3263 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3264 | // FIXME: I'm not sure what this is. |
| 3265 | int64_t FileNumber = -1; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3266 | if (getLexer().is(AsmToken::Integer)) { |
| 3267 | FileNumber = getTok().getIntVal(); |
| 3268 | Lex(); |
| 3269 | |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3270 | if (FileNumber < 0) |
| 3271 | return TokError("negative file number"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3274 | std::string Path = getTok().getString(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3275 | |
| 3276 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3277 | // Allow the strings to have escaped octal character sequence. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3278 | if (check(getTok().isNot(AsmToken::String), |
| 3279 | "unexpected token in '.file' directive") || |
| 3280 | parseEscapedString(Path)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3281 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3282 | |
| 3283 | StringRef Directory; |
| 3284 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3285 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3286 | if (getLexer().is(AsmToken::String)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3287 | if (check(FileNumber == -1, |
| 3288 | "explicit path specified, but no file number") || |
| 3289 | parseEscapedString(FilenameData)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3290 | return true; |
| 3291 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3292 | Directory = Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3293 | } else { |
| 3294 | Filename = Path; |
| 3295 | } |
| 3296 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3297 | uint64_t MD5Hi, MD5Lo; |
| 3298 | bool HasMD5 = false; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3299 | |
| 3300 | Optional<StringRef> Source; |
| 3301 | bool HasSource = false; |
| 3302 | std::string SourceString; |
| 3303 | |
| 3304 | while (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3305 | StringRef Keyword; |
| 3306 | if (check(getTok().isNot(AsmToken::Identifier), |
| 3307 | "unexpected token in '.file' directive") || |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3308 | parseIdentifier(Keyword)) |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3309 | return true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3310 | if (Keyword == "md5") { |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3311 | HasMD5 = true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3312 | if (check(FileNumber == -1, |
| 3313 | "MD5 checksum specified, but no file number") || |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3314 | parseHexOcta(*this, MD5Hi, MD5Lo)) |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3315 | return true; |
| 3316 | } else if (Keyword == "source") { |
| 3317 | HasSource = true; |
| 3318 | if (check(FileNumber == -1, |
| 3319 | "source specified, but no file number") || |
| 3320 | check(getTok().isNot(AsmToken::String), |
| 3321 | "unexpected token in '.file' directive") || |
| 3322 | parseEscapedString(SourceString)) |
| 3323 | return true; |
| 3324 | } else { |
| 3325 | return TokError("unexpected token in '.file' directive"); |
| 3326 | } |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3327 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3328 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 3329 | // In case there is a -g option as well as debug info from directive .file, |
| 3330 | // we turn off the -g option, directly use the existing debug info instead. |
| 3331 | getContext().setGenDwarfForAssembly(false); |
| 3332 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3333 | if (FileNumber == -1) |
| 3334 | getStreamer().EmitFileDirective(Filename); |
| 3335 | else { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3336 | MD5::MD5Result *CKMem = nullptr; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3337 | if (HasMD5) { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3338 | CKMem = (MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1); |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3339 | for (unsigned i = 0; i != 8; ++i) { |
| 3340 | CKMem->Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8)); |
| 3341 | CKMem->Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8)); |
| 3342 | } |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3343 | } |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3344 | if (HasSource) { |
| 3345 | char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size())); |
| 3346 | memcpy(SourceBuf, SourceString.data(), SourceString.size()); |
| 3347 | Source = StringRef(SourceBuf, SourceString.size()); |
| 3348 | } |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3349 | if (FileNumber == 0) |
| 3350 | getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source); |
| 3351 | else { |
| 3352 | Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective( |
| 3353 | FileNumber, Directory, Filename, CKMem, Source); |
| 3354 | if (!FileNumOrErr) |
| 3355 | return Error(DirectiveLoc, toString(FileNumOrErr.takeError())); |
| 3356 | FileNumber = FileNumOrErr.get(); |
| 3357 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | return false; |
| 3361 | } |
| 3362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3363 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3364 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3365 | bool AsmParser::parseDirectiveLine() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3366 | int64_t LineNumber; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3367 | if (getLexer().is(AsmToken::Integer)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3368 | if (parseIntToken(LineNumber, "unexpected token in '.line' directive")) |
| 3369 | return true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3370 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3371 | // FIXME: Do something with the .line. |
| 3372 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3373 | if (parseToken(AsmToken::EndOfStatement, |
| 3374 | "unexpected token in '.line' directive")) |
| 3375 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3376 | |
| 3377 | return false; |
| 3378 | } |
| 3379 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3380 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3381 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 3382 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 3383 | /// The first number is a file number, must have been previously assigned with |
| 3384 | /// a .file directive, the second number is the line number and optionally the |
| 3385 | /// third number is a column position (zero if not specified). The remaining |
| 3386 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3387 | bool AsmParser::parseDirectiveLoc() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3388 | int64_t FileNumber = 0, LineNumber = 0; |
| 3389 | SMLoc Loc = getTok().getLoc(); |
| 3390 | if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") || |
| 3391 | check(FileNumber < 1, Loc, |
| 3392 | "file number less than one in '.loc' directive") || |
| 3393 | check(!getContext().isValidDwarfFileNumber(FileNumber), Loc, |
| 3394 | "unassigned file number in '.loc' directive")) |
| 3395 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3396 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3397 | // optional |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3398 | if (getLexer().is(AsmToken::Integer)) { |
| 3399 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 3400 | if (LineNumber < 0) |
| 3401 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3402 | Lex(); |
| 3403 | } |
| 3404 | |
| 3405 | int64_t ColumnPos = 0; |
| 3406 | if (getLexer().is(AsmToken::Integer)) { |
| 3407 | ColumnPos = getTok().getIntVal(); |
| 3408 | if (ColumnPos < 0) |
| 3409 | return TokError("column position less than zero in '.loc' directive"); |
| 3410 | Lex(); |
| 3411 | } |
| 3412 | |
| 3413 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 3414 | unsigned Isa = 0; |
| 3415 | int64_t Discriminator = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3416 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3417 | auto parseLocOp = [&]() -> bool { |
| 3418 | StringRef Name; |
| 3419 | SMLoc Loc = getTok().getLoc(); |
| 3420 | if (parseIdentifier(Name)) |
| 3421 | return TokError("unexpected token in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3422 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3423 | if (Name == "basic_block") |
| 3424 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 3425 | else if (Name == "prologue_end") |
| 3426 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 3427 | else if (Name == "epilogue_begin") |
| 3428 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 3429 | else if (Name == "is_stmt") { |
| 3430 | Loc = getTok().getLoc(); |
| 3431 | const MCExpr *Value; |
| 3432 | if (parseExpression(Value)) |
| 3433 | return true; |
| 3434 | // The expression must be the constant 0 or 1. |
| 3435 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3436 | int Value = MCE->getValue(); |
| 3437 | if (Value == 0) |
| 3438 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 3439 | else if (Value == 1) |
| 3440 | Flags |= DWARF2_FLAG_IS_STMT; |
| 3441 | else |
| 3442 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3443 | } else { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3444 | 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] | 3445 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3446 | } else if (Name == "isa") { |
| 3447 | Loc = getTok().getLoc(); |
| 3448 | const MCExpr *Value; |
| 3449 | if (parseExpression(Value)) |
| 3450 | return true; |
| 3451 | // The expression must be a constant greater or equal to 0. |
| 3452 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3453 | int Value = MCE->getValue(); |
| 3454 | if (Value < 0) |
| 3455 | return Error(Loc, "isa number less than zero"); |
| 3456 | Isa = Value; |
| 3457 | } else { |
| 3458 | return Error(Loc, "isa number not a constant value"); |
| 3459 | } |
| 3460 | } else if (Name == "discriminator") { |
| 3461 | if (parseAbsoluteExpression(Discriminator)) |
| 3462 | return true; |
| 3463 | } else { |
| 3464 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3465 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3466 | return false; |
| 3467 | }; |
| 3468 | |
Nirav Dave | e2369aa | 2016-10-26 17:28:58 +0000 | [diff] [blame] | 3469 | if (parseMany(parseLocOp, false /*hasComma*/)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3470 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3471 | |
| 3472 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3473 | Isa, Discriminator, StringRef()); |
| 3474 | |
| 3475 | return false; |
| 3476 | } |
| 3477 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3478 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3479 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3480 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3481 | return TokError("unsupported directive '.stabs'"); |
| 3482 | } |
| 3483 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3484 | /// parseDirectiveCVFile |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3485 | /// ::= .cv_file number filename [checksum] [checksumkind] |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3486 | bool AsmParser::parseDirectiveCVFile() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3487 | SMLoc FileNumberLoc = getTok().getLoc(); |
| 3488 | int64_t FileNumber; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3489 | std::string Filename; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3490 | std::string Checksum; |
| 3491 | int64_t ChecksumKind = 0; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3492 | |
| 3493 | if (parseIntToken(FileNumber, |
| 3494 | "expected file number in '.cv_file' directive") || |
| 3495 | check(FileNumber < 1, FileNumberLoc, "file number less than one") || |
| 3496 | check(getTok().isNot(AsmToken::String), |
| 3497 | "unexpected token in '.cv_file' directive") || |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3498 | parseEscapedString(Filename)) |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 3499 | return true; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3500 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3501 | if (check(getTok().isNot(AsmToken::String), |
| 3502 | "unexpected token in '.cv_file' directive") || |
| 3503 | parseEscapedString(Checksum) || |
| 3504 | parseIntToken(ChecksumKind, |
| 3505 | "expected checksum kind in '.cv_file' directive") || |
| 3506 | parseToken(AsmToken::EndOfStatement, |
| 3507 | "unexpected token in '.cv_file' directive")) |
| 3508 | return true; |
| 3509 | } |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3510 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3511 | Checksum = fromHex(Checksum); |
| 3512 | void *CKMem = Ctx.allocate(Checksum.size(), 1); |
| 3513 | memcpy(CKMem, Checksum.data(), Checksum.size()); |
| 3514 | ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem), |
| 3515 | Checksum.size()); |
| 3516 | |
| 3517 | if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes, |
| 3518 | static_cast<uint8_t>(ChecksumKind))) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3519 | return Error(FileNumberLoc, "file number already allocated"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3520 | |
| 3521 | return false; |
| 3522 | } |
| 3523 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3524 | bool AsmParser::parseCVFunctionId(int64_t &FunctionId, |
| 3525 | StringRef DirectiveName) { |
| 3526 | SMLoc Loc; |
| 3527 | return parseTokenLoc(Loc) || |
| 3528 | parseIntToken(FunctionId, "expected function id in '" + DirectiveName + |
| 3529 | "' directive") || |
| 3530 | check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc, |
| 3531 | "expected function id within range [0, UINT_MAX)"); |
| 3532 | } |
| 3533 | |
| 3534 | bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { |
| 3535 | SMLoc Loc; |
| 3536 | return parseTokenLoc(Loc) || |
| 3537 | parseIntToken(FileNumber, "expected integer in '" + DirectiveName + |
| 3538 | "' directive") || |
| 3539 | check(FileNumber < 1, Loc, "file number less than one in '" + |
| 3540 | DirectiveName + "' directive") || |
| 3541 | check(!getCVContext().isValidFileNumber(FileNumber), Loc, |
| 3542 | "unassigned file number in '" + DirectiveName + "' directive"); |
| 3543 | } |
| 3544 | |
| 3545 | /// parseDirectiveCVFuncId |
| 3546 | /// ::= .cv_func_id FunctionId |
| 3547 | /// |
| 3548 | /// Introduces a function ID that can be used with .cv_loc. |
| 3549 | bool AsmParser::parseDirectiveCVFuncId() { |
| 3550 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3551 | int64_t FunctionId; |
| 3552 | |
| 3553 | if (parseCVFunctionId(FunctionId, ".cv_func_id") || |
| 3554 | parseToken(AsmToken::EndOfStatement, |
| 3555 | "unexpected token in '.cv_func_id' directive")) |
| 3556 | return true; |
| 3557 | |
| 3558 | if (!getStreamer().EmitCVFuncIdDirective(FunctionId)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3559 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3560 | |
| 3561 | return false; |
| 3562 | } |
| 3563 | |
| 3564 | /// parseDirectiveCVInlineSiteId |
| 3565 | /// ::= .cv_inline_site_id FunctionId |
| 3566 | /// "within" IAFunc |
| 3567 | /// "inlined_at" IAFile IALine [IACol] |
| 3568 | /// |
| 3569 | /// Introduces a function ID that can be used with .cv_loc. Includes "inlined |
| 3570 | /// at" source location information for use in the line table of the caller, |
| 3571 | /// whether the caller is a real function or another inlined call site. |
| 3572 | bool AsmParser::parseDirectiveCVInlineSiteId() { |
| 3573 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3574 | int64_t FunctionId; |
| 3575 | int64_t IAFunc; |
| 3576 | int64_t IAFile; |
| 3577 | int64_t IALine; |
| 3578 | int64_t IACol = 0; |
| 3579 | |
| 3580 | // FunctionId |
| 3581 | if (parseCVFunctionId(FunctionId, ".cv_inline_site_id")) |
| 3582 | return true; |
| 3583 | |
| 3584 | // "within" |
| 3585 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3586 | getTok().getIdentifier() != "within"), |
| 3587 | "expected 'within' identifier in '.cv_inline_site_id' directive")) |
| 3588 | return true; |
| 3589 | Lex(); |
| 3590 | |
| 3591 | // IAFunc |
| 3592 | if (parseCVFunctionId(IAFunc, ".cv_inline_site_id")) |
| 3593 | return true; |
| 3594 | |
| 3595 | // "inlined_at" |
| 3596 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3597 | getTok().getIdentifier() != "inlined_at"), |
| 3598 | "expected 'inlined_at' identifier in '.cv_inline_site_id' " |
| 3599 | "directive") ) |
| 3600 | return true; |
| 3601 | Lex(); |
| 3602 | |
| 3603 | // IAFile IALine |
| 3604 | if (parseCVFileId(IAFile, ".cv_inline_site_id") || |
| 3605 | parseIntToken(IALine, "expected line number after 'inlined_at'")) |
| 3606 | return true; |
| 3607 | |
| 3608 | // [IACol] |
| 3609 | if (getLexer().is(AsmToken::Integer)) { |
| 3610 | IACol = getTok().getIntVal(); |
| 3611 | Lex(); |
| 3612 | } |
| 3613 | |
| 3614 | if (parseToken(AsmToken::EndOfStatement, |
| 3615 | "unexpected token in '.cv_inline_site_id' directive")) |
| 3616 | return true; |
| 3617 | |
| 3618 | if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, |
| 3619 | IALine, IACol, FunctionIdLoc)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3620 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3621 | |
| 3622 | return false; |
| 3623 | } |
| 3624 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3625 | /// parseDirectiveCVLoc |
| 3626 | /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] |
| 3627 | /// [is_stmt VALUE] |
| 3628 | /// The first number is a file number, must have been previously assigned with |
| 3629 | /// a .file directive, the second number is the line number and optionally the |
| 3630 | /// third number is a column position (zero if not specified). The remaining |
| 3631 | /// optional items are .loc sub-directives. |
| 3632 | bool AsmParser::parseDirectiveCVLoc() { |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3633 | SMLoc DirectiveLoc = getTok().getLoc(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3634 | int64_t FunctionId, FileNumber; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3635 | if (parseCVFunctionId(FunctionId, ".cv_loc") || |
| 3636 | parseCVFileId(FileNumber, ".cv_loc")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3637 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3638 | |
| 3639 | int64_t LineNumber = 0; |
| 3640 | if (getLexer().is(AsmToken::Integer)) { |
| 3641 | LineNumber = getTok().getIntVal(); |
| 3642 | if (LineNumber < 0) |
| 3643 | return TokError("line number less than zero in '.cv_loc' directive"); |
| 3644 | Lex(); |
| 3645 | } |
| 3646 | |
| 3647 | int64_t ColumnPos = 0; |
| 3648 | if (getLexer().is(AsmToken::Integer)) { |
| 3649 | ColumnPos = getTok().getIntVal(); |
| 3650 | if (ColumnPos < 0) |
| 3651 | return TokError("column position less than zero in '.cv_loc' directive"); |
| 3652 | Lex(); |
| 3653 | } |
| 3654 | |
| 3655 | bool PrologueEnd = false; |
| 3656 | uint64_t IsStmt = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3657 | |
| 3658 | auto parseOp = [&]() -> bool { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3659 | StringRef Name; |
| 3660 | SMLoc Loc = getTok().getLoc(); |
| 3661 | if (parseIdentifier(Name)) |
| 3662 | return TokError("unexpected token in '.cv_loc' directive"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3663 | if (Name == "prologue_end") |
| 3664 | PrologueEnd = true; |
| 3665 | else if (Name == "is_stmt") { |
| 3666 | Loc = getTok().getLoc(); |
| 3667 | const MCExpr *Value; |
| 3668 | if (parseExpression(Value)) |
| 3669 | return true; |
| 3670 | // The expression must be the constant 0 or 1. |
| 3671 | IsStmt = ~0ULL; |
| 3672 | if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) |
| 3673 | IsStmt = MCE->getValue(); |
| 3674 | |
| 3675 | if (IsStmt > 1) |
| 3676 | return Error(Loc, "is_stmt value not 0 or 1"); |
| 3677 | } else { |
| 3678 | return Error(Loc, "unknown sub-directive in '.cv_loc' directive"); |
| 3679 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3680 | return false; |
| 3681 | }; |
| 3682 | |
| 3683 | if (parseMany(parseOp, false /*hasComma*/)) |
| 3684 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3685 | |
| 3686 | getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3687 | ColumnPos, PrologueEnd, IsStmt, StringRef(), |
| 3688 | DirectiveLoc); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3689 | return false; |
| 3690 | } |
| 3691 | |
| 3692 | /// parseDirectiveCVLinetable |
| 3693 | /// ::= .cv_linetable FunctionId, FnStart, FnEnd |
| 3694 | bool AsmParser::parseDirectiveCVLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3695 | int64_t FunctionId; |
| 3696 | StringRef FnStartName, FnEndName; |
| 3697 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3698 | if (parseCVFunctionId(FunctionId, ".cv_linetable") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3699 | parseToken(AsmToken::Comma, |
| 3700 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3701 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3702 | "expected identifier in directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3703 | parseToken(AsmToken::Comma, |
| 3704 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3705 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3706 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3707 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3708 | |
| 3709 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3710 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
| 3711 | |
| 3712 | getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym); |
| 3713 | return false; |
| 3714 | } |
| 3715 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3716 | /// parseDirectiveCVInlineLinetable |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3717 | /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3718 | bool AsmParser::parseDirectiveCVInlineLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3719 | int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; |
| 3720 | StringRef FnStartName, FnEndName; |
| 3721 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3722 | if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3723 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3724 | parseIntToken( |
| 3725 | SourceFileId, |
| 3726 | "expected SourceField in '.cv_inline_linetable' directive") || |
| 3727 | check(SourceFileId <= 0, Loc, |
| 3728 | "File id less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3729 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3730 | parseIntToken( |
| 3731 | SourceLineNum, |
| 3732 | "expected SourceLineNum in '.cv_inline_linetable' directive") || |
| 3733 | check(SourceLineNum < 0, Loc, |
| 3734 | "Line number less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3735 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3736 | "expected identifier in directive") || |
| 3737 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3738 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3739 | return true; |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3740 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3741 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3742 | return true; |
| 3743 | |
| 3744 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3745 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 3746 | getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, |
| 3747 | SourceLineNum, FnStartSym, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3748 | FnEndSym); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3749 | return false; |
| 3750 | } |
| 3751 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3752 | /// parseDirectiveCVDefRange |
| 3753 | /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* |
| 3754 | bool AsmParser::parseDirectiveCVDefRange() { |
| 3755 | SMLoc Loc; |
| 3756 | std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; |
| 3757 | while (getLexer().is(AsmToken::Identifier)) { |
| 3758 | Loc = getLexer().getLoc(); |
| 3759 | StringRef GapStartName; |
| 3760 | if (parseIdentifier(GapStartName)) |
| 3761 | return Error(Loc, "expected identifier in directive"); |
| 3762 | MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName); |
| 3763 | |
| 3764 | Loc = getLexer().getLoc(); |
| 3765 | StringRef GapEndName; |
| 3766 | if (parseIdentifier(GapEndName)) |
| 3767 | return Error(Loc, "expected identifier in directive"); |
| 3768 | MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName); |
| 3769 | |
| 3770 | Ranges.push_back({GapStartSym, GapEndSym}); |
| 3771 | } |
| 3772 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3773 | std::string FixedSizePortion; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3774 | if (parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3775 | parseEscapedString(FixedSizePortion)) |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3776 | return true; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3777 | |
| 3778 | getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion); |
| 3779 | return false; |
| 3780 | } |
| 3781 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3782 | /// parseDirectiveCVStringTable |
| 3783 | /// ::= .cv_stringtable |
| 3784 | bool AsmParser::parseDirectiveCVStringTable() { |
| 3785 | getStreamer().EmitCVStringTableDirective(); |
| 3786 | return false; |
| 3787 | } |
| 3788 | |
| 3789 | /// parseDirectiveCVFileChecksums |
| 3790 | /// ::= .cv_filechecksums |
| 3791 | bool AsmParser::parseDirectiveCVFileChecksums() { |
| 3792 | getStreamer().EmitCVFileChecksumsDirective(); |
| 3793 | return false; |
| 3794 | } |
| 3795 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3796 | /// parseDirectiveCVFileChecksumOffset |
| 3797 | /// ::= .cv_filechecksumoffset fileno |
| 3798 | bool AsmParser::parseDirectiveCVFileChecksumOffset() { |
| 3799 | int64_t FileNo; |
| 3800 | if (parseIntToken(FileNo, "expected identifier in directive")) |
| 3801 | return true; |
| 3802 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3803 | return true; |
| 3804 | getStreamer().EmitCVFileChecksumOffsetDirective(FileNo); |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 3808 | /// parseDirectiveCVFPOData |
| 3809 | /// ::= .cv_fpo_data procsym |
| 3810 | bool AsmParser::parseDirectiveCVFPOData() { |
| 3811 | SMLoc DirLoc = getLexer().getLoc(); |
| 3812 | StringRef ProcName; |
| 3813 | if (parseIdentifier(ProcName)) |
| 3814 | return TokError("expected symbol name"); |
| 3815 | if (parseEOL("unexpected tokens")) |
| 3816 | return addErrorSuffix(" in '.cv_fpo_data' directive"); |
| 3817 | MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); |
| 3818 | getStreamer().EmitCVFPOData(ProcSym, DirLoc); |
| 3819 | return false; |
| 3820 | } |
| 3821 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3822 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3823 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3824 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3825 | StringRef Name; |
| 3826 | bool EH = false; |
| 3827 | bool Debug = false; |
| 3828 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3829 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3830 | return TokError("Expected an identifier"); |
| 3831 | |
| 3832 | if (Name == ".eh_frame") |
| 3833 | EH = true; |
| 3834 | else if (Name == ".debug_frame") |
| 3835 | Debug = true; |
| 3836 | |
| 3837 | if (getLexer().is(AsmToken::Comma)) { |
| 3838 | Lex(); |
| 3839 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3840 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3841 | return TokError("Expected an identifier"); |
| 3842 | |
| 3843 | if (Name == ".eh_frame") |
| 3844 | EH = true; |
| 3845 | else if (Name == ".debug_frame") |
| 3846 | Debug = true; |
| 3847 | } |
| 3848 | |
| 3849 | getStreamer().EmitCFISections(EH, Debug); |
| 3850 | return false; |
| 3851 | } |
| 3852 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3853 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3854 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3855 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3856 | StringRef Simple; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3857 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3858 | if (check(parseIdentifier(Simple) || Simple != "simple", |
| 3859 | "unexpected token") || |
| 3860 | parseToken(AsmToken::EndOfStatement)) |
| 3861 | return addErrorSuffix(" in '.cfi_startproc' directive"); |
| 3862 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3863 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 3864 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3865 | return false; |
| 3866 | } |
| 3867 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3868 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3869 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3870 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3871 | getStreamer().EmitCFIEndProc(); |
| 3872 | return false; |
| 3873 | } |
| 3874 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3875 | /// \brief parse register name or number. |
| 3876 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3877 | SMLoc DirectiveLoc) { |
| 3878 | unsigned RegNo; |
| 3879 | |
| 3880 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3881 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3882 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3883 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3884 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3885 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3886 | |
| 3887 | return false; |
| 3888 | } |
| 3889 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3890 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3891 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3892 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3893 | int64_t Register = 0, Offset = 0; |
| 3894 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3895 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3896 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3897 | return true; |
| 3898 | |
| 3899 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3900 | return false; |
| 3901 | } |
| 3902 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3903 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3904 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3905 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3906 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3907 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3908 | return true; |
| 3909 | |
| 3910 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3911 | return false; |
| 3912 | } |
| 3913 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3914 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3915 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3916 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3917 | int64_t Register1 = 0, Register2 = 0; |
| 3918 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || |
| 3919 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3920 | parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3921 | return true; |
| 3922 | |
| 3923 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3924 | return false; |
| 3925 | } |
| 3926 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3927 | /// parseDirectiveCFIWindowSave |
| 3928 | /// ::= .cfi_window_save |
| 3929 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3930 | getStreamer().EmitCFIWindowSave(); |
| 3931 | return false; |
| 3932 | } |
| 3933 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3934 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3935 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3936 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3937 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3938 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3939 | return true; |
| 3940 | |
| 3941 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3942 | return false; |
| 3943 | } |
| 3944 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3945 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3946 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3947 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3948 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3949 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3950 | return true; |
| 3951 | |
| 3952 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3953 | return false; |
| 3954 | } |
| 3955 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3956 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3957 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3958 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3959 | int64_t Register = 0; |
| 3960 | int64_t Offset = 0; |
| 3961 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3962 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3963 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3964 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3965 | return true; |
| 3966 | |
| 3967 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3968 | return false; |
| 3969 | } |
| 3970 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3971 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3972 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3973 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3974 | int64_t Register = 0, Offset = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3975 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3976 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3977 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3978 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3979 | return true; |
| 3980 | |
| 3981 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3982 | return false; |
| 3983 | } |
| 3984 | |
| 3985 | static bool isValidEncoding(int64_t Encoding) { |
| 3986 | if (Encoding & ~0xff) |
| 3987 | return false; |
| 3988 | |
| 3989 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3990 | return true; |
| 3991 | |
| 3992 | const unsigned Format = Encoding & 0xf; |
| 3993 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3994 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3995 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3996 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3997 | return false; |
| 3998 | |
| 3999 | const unsigned Application = Encoding & 0x70; |
| 4000 | if (Application != dwarf::DW_EH_PE_absptr && |
| 4001 | Application != dwarf::DW_EH_PE_pcrel) |
| 4002 | return false; |
| 4003 | |
| 4004 | return true; |
| 4005 | } |
| 4006 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4007 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4008 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 4009 | /// ::= .cfi_personality encoding, [symbol_name] |
| 4010 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4011 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4012 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4013 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4014 | return true; |
| 4015 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 4016 | return false; |
| 4017 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4018 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4019 | if (check(!isValidEncoding(Encoding), "unsupported encoding.") || |
| 4020 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4021 | check(parseIdentifier(Name), "expected identifier in directive")) |
| 4022 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4023 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4024 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4025 | |
| 4026 | if (IsPersonality) |
| 4027 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 4028 | else |
| 4029 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4033 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4034 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4035 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4036 | getStreamer().EmitCFIRememberState(); |
| 4037 | return false; |
| 4038 | } |
| 4039 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4040 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4041 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4042 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4043 | getStreamer().EmitCFIRestoreState(); |
| 4044 | return false; |
| 4045 | } |
| 4046 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4047 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4048 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4049 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4050 | int64_t Register = 0; |
| 4051 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4052 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4053 | return true; |
| 4054 | |
| 4055 | getStreamer().EmitCFISameValue(Register); |
| 4056 | return false; |
| 4057 | } |
| 4058 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4059 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4060 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4061 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4062 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4063 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4064 | return true; |
| 4065 | |
| 4066 | getStreamer().EmitCFIRestore(Register); |
| 4067 | return false; |
| 4068 | } |
| 4069 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4070 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4071 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4072 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4073 | std::string Values; |
| 4074 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4075 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4076 | return true; |
| 4077 | |
| 4078 | Values.push_back((uint8_t)CurrValue); |
| 4079 | |
| 4080 | while (getLexer().is(AsmToken::Comma)) { |
| 4081 | Lex(); |
| 4082 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4083 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4084 | return true; |
| 4085 | |
| 4086 | Values.push_back((uint8_t)CurrValue); |
| 4087 | } |
| 4088 | |
| 4089 | getStreamer().EmitCFIEscape(Values); |
| 4090 | return false; |
| 4091 | } |
| 4092 | |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 4093 | /// parseDirectiveCFIReturnColumn |
| 4094 | /// ::= .cfi_return_column register |
| 4095 | bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { |
| 4096 | int64_t Register = 0; |
| 4097 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 4098 | return true; |
| 4099 | getStreamer().EmitCFIReturnColumn(Register); |
| 4100 | return false; |
| 4101 | } |
| 4102 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4103 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4104 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4105 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4106 | if (parseToken(AsmToken::EndOfStatement, |
| 4107 | "unexpected token in '.cfi_signal_frame'")) |
| 4108 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4109 | |
| 4110 | getStreamer().EmitCFISignalFrame(); |
| 4111 | return false; |
| 4112 | } |
| 4113 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4114 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4115 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4116 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4117 | int64_t Register = 0; |
| 4118 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4119 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4120 | return true; |
| 4121 | |
| 4122 | getStreamer().EmitCFIUndefined(Register); |
| 4123 | return false; |
| 4124 | } |
| 4125 | |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4126 | /// parseDirectiveAltmacro |
| 4127 | /// ::= .altmacro |
| 4128 | /// ::= .noaltmacro |
| 4129 | bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { |
| 4130 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4131 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4132 | if (Directive == ".altmacro") |
| 4133 | getLexer().SetAltMacroMode(true); |
| 4134 | else |
| 4135 | getLexer().SetAltMacroMode(false); |
| 4136 | return false; |
| 4137 | } |
| 4138 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4139 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4140 | /// ::= .macros_on |
| 4141 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4142 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4143 | if (parseToken(AsmToken::EndOfStatement, |
| 4144 | "unexpected token in '" + Directive + "' directive")) |
| 4145 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4146 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4147 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4148 | return false; |
| 4149 | } |
| 4150 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4151 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4152 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4153 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4154 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4155 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4156 | return TokError("expected identifier in '.macro' directive"); |
| 4157 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4158 | if (getLexer().is(AsmToken::Comma)) |
| 4159 | Lex(); |
| 4160 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4161 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4162 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4163 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 4164 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4165 | return Error(Lexer.getLoc(), |
| 4166 | "Vararg parameter '" + Parameters.back().Name + |
| 4167 | "' should be last one in the list of parameters."); |
| 4168 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4169 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4170 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4171 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4172 | |
Coby Tayree | bedaae0 | 2017-04-08 20:29:03 +0000 | [diff] [blame] | 4173 | // Emit an error if two (or more) named parameters share the same name |
| 4174 | for (const MCAsmMacroParameter& CurrParam : Parameters) |
| 4175 | if (CurrParam.Name.equals(Parameter.Name)) |
| 4176 | return TokError("macro '" + Name + "' has multiple parameters" |
| 4177 | " named '" + Parameter.Name + "'"); |
| 4178 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4179 | if (Lexer.is(AsmToken::Colon)) { |
| 4180 | Lex(); // consume ':' |
| 4181 | |
| 4182 | SMLoc QualLoc; |
| 4183 | StringRef Qualifier; |
| 4184 | |
| 4185 | QualLoc = Lexer.getLoc(); |
| 4186 | if (parseIdentifier(Qualifier)) |
| 4187 | return Error(QualLoc, "missing parameter qualifier for " |
| 4188 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4189 | |
| 4190 | if (Qualifier == "req") |
| 4191 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 4192 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4193 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4194 | else |
| 4195 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 4196 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4197 | } |
| 4198 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4199 | if (getLexer().is(AsmToken::Equal)) { |
| 4200 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4201 | |
| 4202 | SMLoc ParamLoc; |
| 4203 | |
| 4204 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4205 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4206 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4207 | |
| 4208 | if (Parameter.Required) |
| 4209 | Warning(ParamLoc, "pointless default value for required parameter " |
| 4210 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4211 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4212 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4213 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4214 | |
| 4215 | if (getLexer().is(AsmToken::Comma)) |
| 4216 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4219 | // Eat just the end of statement. |
| 4220 | Lexer.Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4221 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4222 | // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4223 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4224 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4225 | // Lex the macro definition. |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 4226 | while (true) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4227 | // Ignore Lexing errors in macros. |
| 4228 | while (Lexer.is(AsmToken::Error)) { |
| 4229 | Lexer.Lex(); |
| 4230 | } |
| 4231 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4232 | // Check whether we have reached the end of the file. |
| 4233 | if (getLexer().is(AsmToken::Eof)) |
| 4234 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 4235 | |
| 4236 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4237 | if (getLexer().is(AsmToken::Identifier)) { |
| 4238 | if (getTok().getIdentifier() == ".endm" || |
| 4239 | getTok().getIdentifier() == ".endmacro") { |
| 4240 | if (MacroDepth == 0) { // Outermost macro. |
| 4241 | EndToken = getTok(); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4242 | Lexer.Lex(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4243 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4244 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 4245 | "' directive"); |
| 4246 | break; |
| 4247 | } else { |
| 4248 | // Otherwise we just found the end of an inner macro. |
| 4249 | --MacroDepth; |
| 4250 | } |
| 4251 | } else if (getTok().getIdentifier() == ".macro") { |
| 4252 | // We allow nested macros. Those aren't instantiated until the outermost |
| 4253 | // macro is expanded so just ignore them for now. |
| 4254 | ++MacroDepth; |
| 4255 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4259 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4262 | if (getContext().lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4263 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 4264 | } |
| 4265 | |
| 4266 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4267 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4268 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4269 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4270 | MCAsmMacro Macro(Name, Body, std::move(Parameters)); |
| 4271 | DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n"; |
| 4272 | Macro.dump()); |
| 4273 | getContext().defineMacro(Name, std::move(Macro)); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4274 | return false; |
| 4275 | } |
| 4276 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4277 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4278 | /// |
| 4279 | /// With the support added for named parameters there may be code out there that |
| 4280 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4281 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4282 | /// 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] | 4283 | /// 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] | 4284 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 4285 | /// warning that the positional parameter found in body which have no effect. |
| 4286 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4287 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4288 | /// intended or change the macro to use the named parameters. It is possible |
| 4289 | /// this warning will trigger when the none of the named parameters are used |
| 4290 | /// 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] | 4291 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4292 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4293 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4294 | // If this macro is not defined with named parameters the warning we are |
| 4295 | // checking for here doesn't apply. |
| 4296 | unsigned NParameters = Parameters.size(); |
| 4297 | if (NParameters == 0) |
| 4298 | return; |
| 4299 | |
| 4300 | bool NamedParametersFound = false; |
| 4301 | bool PositionalParametersFound = false; |
| 4302 | |
| 4303 | // Look at the body of the macro for use of both the named parameters and what |
| 4304 | // are likely to be positional parameters. This is what expandMacro() is |
| 4305 | // doing when it finds the parameters in the body. |
| 4306 | while (!Body.empty()) { |
| 4307 | // Scan for the next possible parameter. |
| 4308 | std::size_t End = Body.size(), Pos = 0; |
| 4309 | for (; Pos != End; ++Pos) { |
| 4310 | // Check for a substitution or escape. |
| 4311 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 4312 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 4313 | break; |
| 4314 | |
| 4315 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 4316 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 4317 | continue; |
| 4318 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 4319 | if (Next == '$' || Next == 'n' || |
| 4320 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | // Check if we reached the end. |
| 4325 | if (Pos == End) |
| 4326 | break; |
| 4327 | |
| 4328 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4329 | switch (Body[Pos + 1]) { |
| 4330 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4331 | case '$': |
| 4332 | break; |
| 4333 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4334 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4335 | case 'n': |
| 4336 | PositionalParametersFound = true; |
| 4337 | break; |
| 4338 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4339 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4340 | default: { |
| 4341 | PositionalParametersFound = true; |
| 4342 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4343 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4344 | } |
| 4345 | Pos += 2; |
| 4346 | } else { |
| 4347 | unsigned I = Pos + 1; |
| 4348 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 4349 | ++I; |
| 4350 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4351 | const char *Begin = Body.data() + Pos + 1; |
| 4352 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4353 | unsigned Index = 0; |
| 4354 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4355 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4356 | break; |
| 4357 | |
| 4358 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4359 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 4360 | Pos += 3; |
| 4361 | else { |
| 4362 | Pos = I; |
| 4363 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4364 | } else { |
| 4365 | NamedParametersFound = true; |
| 4366 | Pos += 1 + Argument.size(); |
| 4367 | } |
| 4368 | } |
| 4369 | // Update the scan point. |
| 4370 | Body = Body.substr(Pos); |
| 4371 | } |
| 4372 | |
| 4373 | if (!NamedParametersFound && PositionalParametersFound) |
| 4374 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 4375 | "used in macro body, possible positional parameter " |
| 4376 | "found in body which will have no effect"); |
| 4377 | } |
| 4378 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4379 | /// parseDirectiveExitMacro |
| 4380 | /// ::= .exitm |
| 4381 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4382 | if (parseToken(AsmToken::EndOfStatement, |
| 4383 | "unexpected token in '" + Directive + "' directive")) |
| 4384 | return true; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4385 | |
| 4386 | if (!isInsideMacroInstantiation()) |
| 4387 | return TokError("unexpected '" + Directive + "' in file, " |
| 4388 | "no current macro definition"); |
| 4389 | |
| 4390 | // Exit all conditionals that are active in the current macro. |
| 4391 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 4392 | TheCondState = TheCondStack.back(); |
| 4393 | TheCondStack.pop_back(); |
| 4394 | } |
| 4395 | |
| 4396 | handleMacroExit(); |
| 4397 | return false; |
| 4398 | } |
| 4399 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4400 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4401 | /// ::= .endm |
| 4402 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4403 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4404 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4405 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4406 | |
| 4407 | // If we are inside a macro instantiation, terminate the current |
| 4408 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4409 | if (isInsideMacroInstantiation()) { |
| 4410 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4411 | return false; |
| 4412 | } |
| 4413 | |
| 4414 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 4415 | // .endmacro directives are handled during the macro definition parsing. |
| 4416 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4417 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4420 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4421 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4422 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4423 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4424 | SMLoc Loc; |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 4425 | if (parseTokenLoc(Loc) || |
| 4426 | check(parseIdentifier(Name), Loc, |
| 4427 | "expected identifier in '.purgem' directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4428 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4429 | "unexpected token in '.purgem' directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4430 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4431 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4432 | if (!getContext().lookupMacro(Name)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4433 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 4434 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4435 | getContext().undefineMacro(Name); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4436 | DEBUG_WITH_TYPE("asm-macros", dbgs() |
| 4437 | << "Un-defining macro: " << Name << "\n"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4438 | return false; |
| 4439 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4440 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4441 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4442 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4443 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4444 | // Expect a single argument: an expression that evaluates to a constant |
| 4445 | // in the inclusive range 0-30. |
| 4446 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4447 | int64_t AlignSizePow2; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4448 | if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4449 | parseToken(AsmToken::EndOfStatement, "unexpected token after expression " |
| 4450 | "in '.bundle_align_mode' " |
| 4451 | "directive") || |
| 4452 | check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc, |
| 4453 | "invalid bundle alignment size (expected between 0 and 30)")) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4454 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4455 | |
| 4456 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 4457 | // unsigned. |
| 4458 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 4459 | return false; |
| 4460 | } |
| 4461 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4462 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4463 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4464 | bool AsmParser::parseDirectiveBundleLock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4465 | if (checkForValidSection()) |
| 4466 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4467 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4468 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4469 | StringRef Option; |
| 4470 | SMLoc Loc = getTok().getLoc(); |
| 4471 | const char *kInvalidOptionError = |
| 4472 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4473 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4474 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4475 | if (check(parseIdentifier(Option), Loc, kInvalidOptionError) || |
| 4476 | check(Option != "align_to_end", Loc, kInvalidOptionError) || |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4477 | parseToken(AsmToken::EndOfStatement, |
| 4478 | "unexpected token after '.bundle_lock' directive option")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4479 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4480 | AlignToEnd = true; |
| 4481 | } |
| 4482 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4483 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4484 | return false; |
| 4485 | } |
| 4486 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4487 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4488 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4489 | bool AsmParser::parseDirectiveBundleUnlock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4490 | if (checkForValidSection() || |
| 4491 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4492 | "unexpected token in '.bundle_unlock' directive")) |
| 4493 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4494 | |
| 4495 | getStreamer().EmitBundleUnlock(); |
| 4496 | return false; |
| 4497 | } |
| 4498 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4499 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4500 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4501 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 4502 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 4503 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4504 | if (checkForValidSection() || parseExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4505 | return true; |
| 4506 | |
| 4507 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4508 | if (parseOptionalToken(AsmToken::Comma)) |
| 4509 | if (parseAbsoluteExpression(FillExpr)) |
| 4510 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
| 4511 | if (parseToken(AsmToken::EndOfStatement)) |
| 4512 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4513 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4514 | // 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] | 4515 | getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4516 | |
| 4517 | return false; |
| 4518 | } |
| 4519 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4520 | /// parseDirectiveDCB |
| 4521 | /// ::= .dcb.{b, l, w} expression, expression |
| 4522 | bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4523 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4524 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4525 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4526 | return true; |
| 4527 | |
| 4528 | if (NumValues < 0) { |
| 4529 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4530 | return false; |
| 4531 | } |
| 4532 | |
| 4533 | if (parseToken(AsmToken::Comma, |
| 4534 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4535 | return true; |
| 4536 | |
| 4537 | const MCExpr *Value; |
| 4538 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4539 | if (parseExpression(Value)) |
| 4540 | return true; |
| 4541 | |
| 4542 | // Special case constant expressions to match code generator. |
| 4543 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 4544 | assert(Size <= 8 && "Invalid size"); |
| 4545 | uint64_t IntValue = MCE->getValue(); |
| 4546 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 4547 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4548 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4549 | getStreamer().EmitIntValue(IntValue, Size); |
| 4550 | } else { |
| 4551 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4552 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 4553 | } |
| 4554 | |
| 4555 | if (parseToken(AsmToken::EndOfStatement, |
| 4556 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4557 | return true; |
| 4558 | |
| 4559 | return false; |
| 4560 | } |
| 4561 | |
| 4562 | /// parseDirectiveRealDCB |
| 4563 | /// ::= .dcb.{d, s} expression, expression |
| 4564 | bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4565 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4566 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4567 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4568 | return true; |
| 4569 | |
| 4570 | if (NumValues < 0) { |
| 4571 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4572 | return false; |
| 4573 | } |
| 4574 | |
| 4575 | if (parseToken(AsmToken::Comma, |
| 4576 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4577 | return true; |
| 4578 | |
| 4579 | APInt AsInt; |
| 4580 | if (parseRealValue(Semantics, AsInt)) |
| 4581 | return true; |
| 4582 | |
| 4583 | if (parseToken(AsmToken::EndOfStatement, |
| 4584 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4585 | return true; |
| 4586 | |
| 4587 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4588 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 4589 | AsInt.getBitWidth() / 8); |
| 4590 | |
| 4591 | return false; |
| 4592 | } |
| 4593 | |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4594 | /// parseDirectiveDS |
| 4595 | /// ::= .ds.{b, d, l, p, s, w, x} expression |
| 4596 | bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4597 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4598 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4599 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4600 | return true; |
| 4601 | |
| 4602 | if (NumValues < 0) { |
| 4603 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4604 | return false; |
| 4605 | } |
| 4606 | |
| 4607 | if (parseToken(AsmToken::EndOfStatement, |
| 4608 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4609 | return true; |
| 4610 | |
| 4611 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4612 | getStreamer().emitFill(Size, 0); |
| 4613 | |
| 4614 | return false; |
| 4615 | } |
| 4616 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4617 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4618 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4619 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4620 | if (checkForValidSection()) |
| 4621 | return true; |
| 4622 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4623 | auto parseOp = [&]() -> bool { |
| 4624 | const MCExpr *Value; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4625 | if (parseExpression(Value)) |
| 4626 | return true; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4627 | if (Signed) |
| 4628 | getStreamer().EmitSLEB128Value(Value); |
| 4629 | else |
| 4630 | getStreamer().EmitULEB128Value(Value); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4631 | return false; |
| 4632 | }; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4633 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4634 | if (parseMany(parseOp)) |
| 4635 | return addErrorSuffix(" in directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4636 | |
| 4637 | return false; |
| 4638 | } |
| 4639 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4640 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4641 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4642 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4643 | auto parseOp = [&]() -> bool { |
| 4644 | StringRef Name; |
| 4645 | SMLoc Loc = getTok().getLoc(); |
| 4646 | if (parseIdentifier(Name)) |
| 4647 | return Error(Loc, "expected identifier"); |
| 4648 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4649 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4650 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 4651 | if (Sym->isTemporary()) |
| 4652 | return Error(Loc, "non-local symbol required"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4653 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4654 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 4655 | return Error(Loc, "unable to emit symbol attribute"); |
| 4656 | return false; |
| 4657 | }; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4658 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4659 | if (parseMany(parseOp)) |
| 4660 | return addErrorSuffix(" in directive"); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 4661 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4662 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4663 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4664 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4665 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4666 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4667 | if (checkForValidSection()) |
| 4668 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 4669 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4670 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4671 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4672 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4673 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4674 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 4675 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4676 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4677 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4678 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4679 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4680 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4681 | |
| 4682 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4683 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4684 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4685 | return true; |
| 4686 | |
| 4687 | int64_t Pow2Alignment = 0; |
| 4688 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4689 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4690 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4691 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4692 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4693 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4694 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4695 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 4696 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4697 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 4698 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4699 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4700 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 4701 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4702 | if (!isPowerOf2_64(Pow2Alignment)) |
| 4703 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 4704 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 4705 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4706 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4707 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4708 | if (parseToken(AsmToken::EndOfStatement, |
| 4709 | "unexpected token in '.comm' or '.lcomm' directive")) |
| 4710 | return true; |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4711 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4712 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 4713 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4714 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4715 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4716 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4717 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 4718 | // 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] | 4719 | // may internally end up wanting an alignment in bytes. |
| 4720 | // FIXME: Diagnose overflow. |
| 4721 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4722 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4723 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4724 | |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 4725 | Sym->redefineIfPossible(); |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 4726 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4727 | return Error(IDLoc, "invalid symbol redefinition"); |
| 4728 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4729 | // 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] | 4730 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4731 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 4732 | return false; |
| 4733 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4734 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4735 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4736 | return false; |
| 4737 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 4738 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4739 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4740 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4741 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4742 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4743 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4744 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4745 | StringRef Str = parseStringToEndOfStatement(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4746 | if (parseToken(AsmToken::EndOfStatement, |
| 4747 | "unexpected token in '.abort' directive")) |
| 4748 | return true; |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4749 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4750 | if (Str.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4751 | return Error(Loc, ".abort detected. Assembly stopping."); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4752 | else |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4753 | return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4754 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4755 | |
| 4756 | return false; |
| 4757 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 4758 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4759 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4760 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4761 | bool AsmParser::parseDirectiveInclude() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4762 | // Allow the strings to have escaped octal character sequence. |
| 4763 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4764 | SMLoc IncludeLoc = getTok().getLoc(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4765 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4766 | if (check(getTok().isNot(AsmToken::String), |
| 4767 | "expected string in '.include' directive") || |
| 4768 | parseEscapedString(Filename) || |
| 4769 | check(getTok().isNot(AsmToken::EndOfStatement), |
| 4770 | "unexpected token in '.include' directive") || |
| 4771 | // Attempt to switch the lexer to the included file before consuming the |
| 4772 | // end of statement to avoid losing it when we switch. |
| 4773 | check(enterIncludeFile(Filename), IncludeLoc, |
| 4774 | "Could not find include file '" + Filename + "'")) |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 4775 | return true; |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4776 | |
| 4777 | return false; |
| 4778 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 4779 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4780 | /// parseDirectiveIncbin |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4781 | /// ::= .incbin "filename" [ , skip [ , count ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4782 | bool AsmParser::parseDirectiveIncbin() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4783 | // Allow the strings to have escaped octal character sequence. |
| 4784 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4785 | SMLoc IncbinLoc = getTok().getLoc(); |
| 4786 | if (check(getTok().isNot(AsmToken::String), |
| 4787 | "expected string in '.incbin' directive") || |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4788 | parseEscapedString(Filename)) |
| 4789 | return true; |
| 4790 | |
| 4791 | int64_t Skip = 0; |
| 4792 | const MCExpr *Count = nullptr; |
| 4793 | SMLoc SkipLoc, CountLoc; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4794 | if (parseOptionalToken(AsmToken::Comma)) { |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4795 | // The skip expression can be omitted while specifying the count, e.g: |
| 4796 | // .incbin "filename",,4 |
| 4797 | if (getTok().isNot(AsmToken::Comma)) { |
| 4798 | if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip)) |
| 4799 | return true; |
| 4800 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4801 | if (parseOptionalToken(AsmToken::Comma)) { |
| 4802 | CountLoc = getTok().getLoc(); |
| 4803 | if (parseExpression(Count)) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4804 | return true; |
| 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4809 | "unexpected token in '.incbin' directive")) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4810 | return true; |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4811 | |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4812 | if (check(Skip < 0, SkipLoc, "skip is negative")) |
| 4813 | return true; |
| 4814 | |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4815 | // Attempt to process the included file. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4816 | if (processIncbinFile(Filename, Skip, Count, CountLoc)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4817 | return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 4818 | return false; |
| 4819 | } |
| 4820 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4821 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4822 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 4823 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4824 | TheCondStack.push_back(TheCondState); |
| 4825 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4826 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4827 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4828 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4829 | int64_t ExprValue; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4830 | if (parseAbsoluteExpression(ExprValue) || |
| 4831 | parseToken(AsmToken::EndOfStatement, |
| 4832 | "unexpected token in '.if' directive")) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4833 | return true; |
| 4834 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4835 | switch (DirKind) { |
| 4836 | default: |
| 4837 | llvm_unreachable("unsupported directive"); |
| 4838 | case DK_IF: |
| 4839 | case DK_IFNE: |
| 4840 | break; |
| 4841 | case DK_IFEQ: |
| 4842 | ExprValue = ExprValue == 0; |
| 4843 | break; |
| 4844 | case DK_IFGE: |
| 4845 | ExprValue = ExprValue >= 0; |
| 4846 | break; |
| 4847 | case DK_IFGT: |
| 4848 | ExprValue = ExprValue > 0; |
| 4849 | break; |
| 4850 | case DK_IFLE: |
| 4851 | ExprValue = ExprValue <= 0; |
| 4852 | break; |
| 4853 | case DK_IFLT: |
| 4854 | ExprValue = ExprValue < 0; |
| 4855 | break; |
| 4856 | } |
| 4857 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4858 | TheCondState.CondMet = ExprValue; |
| 4859 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4860 | } |
| 4861 | |
| 4862 | return false; |
| 4863 | } |
| 4864 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4865 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4866 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4867 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4868 | TheCondStack.push_back(TheCondState); |
| 4869 | TheCondState.TheCond = AsmCond::IfCond; |
| 4870 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4871 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4872 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4873 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4874 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4875 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4876 | if (parseToken(AsmToken::EndOfStatement, |
| 4877 | "unexpected token in '.ifb' directive")) |
| 4878 | return true; |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4879 | |
| 4880 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 4881 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4882 | } |
| 4883 | |
| 4884 | return false; |
| 4885 | } |
| 4886 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4887 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4888 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4889 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4890 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4891 | TheCondStack.push_back(TheCondState); |
| 4892 | TheCondState.TheCond = AsmCond::IfCond; |
| 4893 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4894 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4895 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4896 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4897 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4898 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4899 | if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive")) |
| 4900 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4901 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4902 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4903 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4904 | if (parseToken(AsmToken::EndOfStatement, |
| 4905 | "unexpected token in '.ifc' directive")) |
| 4906 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4907 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4908 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4909 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4910 | } |
| 4911 | |
| 4912 | return false; |
| 4913 | } |
| 4914 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4915 | /// parseDirectiveIfeqs |
| 4916 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4917 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4918 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4919 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4920 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 4921 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | StringRef String1 = getTok().getStringContents(); |
| 4925 | Lex(); |
| 4926 | |
| 4927 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4928 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4929 | return TokError( |
| 4930 | "expected comma after first string for '.ifeqs' directive"); |
| 4931 | return TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | Lex(); |
| 4935 | |
| 4936 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4937 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4938 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 4939 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | StringRef String2 = getTok().getStringContents(); |
| 4943 | Lex(); |
| 4944 | |
| 4945 | TheCondStack.push_back(TheCondState); |
| 4946 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4947 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4948 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4949 | |
| 4950 | return false; |
| 4951 | } |
| 4952 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4953 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4954 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4955 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4956 | StringRef Name; |
| 4957 | TheCondStack.push_back(TheCondState); |
| 4958 | TheCondState.TheCond = AsmCond::IfCond; |
| 4959 | |
| 4960 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4961 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4962 | } else { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4963 | if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") || |
| 4964 | parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'")) |
| 4965 | return true; |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4966 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4967 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4968 | |
| 4969 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4970 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4971 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4972 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4973 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4974 | } |
| 4975 | |
| 4976 | return false; |
| 4977 | } |
| 4978 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4979 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4980 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4981 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4982 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4983 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4984 | return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an" |
| 4985 | " .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4986 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4987 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4988 | bool LastIgnoreState = false; |
| 4989 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4990 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4991 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4992 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4993 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4994 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4995 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4996 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4997 | return true; |
| 4998 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4999 | if (parseToken(AsmToken::EndOfStatement, |
| 5000 | "unexpected token in '.elseif' directive")) |
| 5001 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 5002 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5003 | TheCondState.CondMet = ExprValue; |
| 5004 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5005 | } |
| 5006 | |
| 5007 | return false; |
| 5008 | } |
| 5009 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5010 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5011 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5012 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5013 | if (parseToken(AsmToken::EndOfStatement, |
| 5014 | "unexpected token in '.else' directive")) |
| 5015 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5016 | |
| 5017 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 5018 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5019 | return Error(DirectiveLoc, "Encountered a .else that doesn't follow " |
| 5020 | " an .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5021 | TheCondState.TheCond = AsmCond::ElseCond; |
| 5022 | bool LastIgnoreState = false; |
| 5023 | if (!TheCondStack.empty()) |
| 5024 | LastIgnoreState = TheCondStack.back().Ignore; |
| 5025 | if (LastIgnoreState || TheCondState.CondMet) |
| 5026 | TheCondState.Ignore = true; |
| 5027 | else |
| 5028 | TheCondState.Ignore = false; |
| 5029 | |
| 5030 | return false; |
| 5031 | } |
| 5032 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5033 | /// parseDirectiveEnd |
| 5034 | /// ::= .end |
| 5035 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5036 | if (parseToken(AsmToken::EndOfStatement, |
| 5037 | "unexpected token in '.end' directive")) |
| 5038 | return true; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5039 | |
| 5040 | while (Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5041 | Lexer.Lex(); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5042 | |
| 5043 | return false; |
| 5044 | } |
| 5045 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5046 | /// parseDirectiveError |
| 5047 | /// ::= .err |
| 5048 | /// ::= .error [string] |
| 5049 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 5050 | if (!TheCondStack.empty()) { |
| 5051 | if (TheCondStack.back().Ignore) { |
| 5052 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5053 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | if (!WithMessage) |
| 5058 | return Error(L, ".err encountered"); |
| 5059 | |
| 5060 | StringRef Message = ".error directive invoked in source file"; |
| 5061 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5062 | if (Lexer.isNot(AsmToken::String)) |
| 5063 | return TokError(".error argument must be a string"); |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5064 | |
| 5065 | Message = getTok().getStringContents(); |
| 5066 | Lex(); |
| 5067 | } |
| 5068 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5069 | return Error(L, Message); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5072 | /// parseDirectiveWarning |
| 5073 | /// ::= .warning [string] |
| 5074 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 5075 | if (!TheCondStack.empty()) { |
| 5076 | if (TheCondStack.back().Ignore) { |
| 5077 | eatToEndOfStatement(); |
| 5078 | return false; |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | StringRef Message = ".warning directive invoked in source file"; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5083 | |
| 5084 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5085 | if (Lexer.isNot(AsmToken::String)) |
| 5086 | return TokError(".warning argument must be a string"); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5087 | |
| 5088 | Message = getTok().getStringContents(); |
| 5089 | Lex(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5090 | if (parseToken(AsmToken::EndOfStatement, |
| 5091 | "expected end of statement in '.warning' directive")) |
| 5092 | return true; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5095 | return Warning(L, Message); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5098 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5099 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5100 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5101 | if (parseToken(AsmToken::EndOfStatement, |
| 5102 | "unexpected token in '.endif' directive")) |
| 5103 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5104 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5105 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5106 | return Error(DirectiveLoc, "Encountered a .endif that doesn't follow " |
| 5107 | "an .if or .else"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5108 | if (!TheCondStack.empty()) { |
| 5109 | TheCondState = TheCondStack.back(); |
| 5110 | TheCondStack.pop_back(); |
| 5111 | } |
| 5112 | |
| 5113 | return false; |
| 5114 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 5115 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5116 | void AsmParser::initializeDirectiveKindMap() { |
| 5117 | DirectiveKindMap[".set"] = DK_SET; |
| 5118 | DirectiveKindMap[".equ"] = DK_EQU; |
| 5119 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 5120 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 5121 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 5122 | DirectiveKindMap[".string"] = DK_STRING; |
| 5123 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 5124 | DirectiveKindMap[".short"] = DK_SHORT; |
| 5125 | DirectiveKindMap[".value"] = DK_VALUE; |
| 5126 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 5127 | DirectiveKindMap[".long"] = DK_LONG; |
| 5128 | DirectiveKindMap[".int"] = DK_INT; |
| 5129 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 5130 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 5131 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 5132 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5133 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 5134 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 5135 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 5136 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 5137 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 5138 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 5139 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 5140 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 5141 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 5142 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 5143 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 5144 | DirectiveKindMap[".org"] = DK_ORG; |
| 5145 | DirectiveKindMap[".fill"] = DK_FILL; |
| 5146 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 5147 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 5148 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 5149 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5150 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 5151 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 5152 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 5153 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 5154 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 5155 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 5156 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 5157 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 5158 | DirectiveKindMap[".comm"] = DK_COMM; |
| 5159 | DirectiveKindMap[".common"] = DK_COMMON; |
| 5160 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 5161 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 5162 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 5163 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 5164 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 5165 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 5166 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5167 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5168 | DirectiveKindMap[".irp"] = DK_IRP; |
| 5169 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 5170 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 5171 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 5172 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 5173 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 5174 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5175 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 5176 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 5177 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 5178 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 5179 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 5180 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5181 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 5182 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 5183 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5184 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5185 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5186 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5187 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 5188 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 5189 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 5190 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 5191 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5192 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5193 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 5194 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 5195 | DirectiveKindMap[".space"] = DK_SPACE; |
| 5196 | DirectiveKindMap[".file"] = DK_FILE; |
| 5197 | DirectiveKindMap[".line"] = DK_LINE; |
| 5198 | DirectiveKindMap[".loc"] = DK_LOC; |
| 5199 | DirectiveKindMap[".stabs"] = DK_STABS; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5200 | DirectiveKindMap[".cv_file"] = DK_CV_FILE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5201 | DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5202 | DirectiveKindMap[".cv_loc"] = DK_CV_LOC; |
| 5203 | DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 5204 | DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5205 | DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 5206 | DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5207 | DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE; |
| 5208 | DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 5209 | DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET; |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 5210 | DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5211 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 5212 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 5213 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 5214 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 5215 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 5216 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 5217 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 5218 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 5219 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 5220 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 5221 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 5222 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 5223 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 5224 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 5225 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 5226 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 5227 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 5228 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 5229 | DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5230 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 5231 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 5232 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 5233 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5234 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 5235 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 5236 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 5237 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5238 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 5239 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 5240 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5241 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5242 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5243 | DirectiveKindMap[".warning"] = DK_WARNING; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 5244 | DirectiveKindMap[".altmacro"] = DK_ALTMACRO; |
| 5245 | DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 5246 | DirectiveKindMap[".reloc"] = DK_RELOC; |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 5247 | DirectiveKindMap[".dc"] = DK_DC; |
| 5248 | DirectiveKindMap[".dc.a"] = DK_DC_A; |
| 5249 | DirectiveKindMap[".dc.b"] = DK_DC_B; |
| 5250 | DirectiveKindMap[".dc.d"] = DK_DC_D; |
| 5251 | DirectiveKindMap[".dc.l"] = DK_DC_L; |
| 5252 | DirectiveKindMap[".dc.s"] = DK_DC_S; |
| 5253 | DirectiveKindMap[".dc.w"] = DK_DC_W; |
| 5254 | DirectiveKindMap[".dc.x"] = DK_DC_X; |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 5255 | DirectiveKindMap[".dcb"] = DK_DCB; |
| 5256 | DirectiveKindMap[".dcb.b"] = DK_DCB_B; |
| 5257 | DirectiveKindMap[".dcb.d"] = DK_DCB_D; |
| 5258 | DirectiveKindMap[".dcb.l"] = DK_DCB_L; |
| 5259 | DirectiveKindMap[".dcb.s"] = DK_DCB_S; |
| 5260 | DirectiveKindMap[".dcb.w"] = DK_DCB_W; |
| 5261 | DirectiveKindMap[".dcb.x"] = DK_DCB_X; |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 5262 | DirectiveKindMap[".ds"] = DK_DS; |
| 5263 | DirectiveKindMap[".ds.b"] = DK_DS_B; |
| 5264 | DirectiveKindMap[".ds.d"] = DK_DS_D; |
| 5265 | DirectiveKindMap[".ds.l"] = DK_DS_L; |
| 5266 | DirectiveKindMap[".ds.p"] = DK_DS_P; |
| 5267 | DirectiveKindMap[".ds.s"] = DK_DS_S; |
| 5268 | DirectiveKindMap[".ds.w"] = DK_DS_W; |
| 5269 | DirectiveKindMap[".ds.x"] = DK_DS_X; |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5270 | DirectiveKindMap[".print"] = DK_PRINT; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5273 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5274 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5275 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5276 | unsigned NestLevel = 0; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5277 | while (true) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5278 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5279 | if (getLexer().is(AsmToken::Eof)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5280 | printError(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5281 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5284 | if (Lexer.is(AsmToken::Identifier) && |
Nikolay Haustov | 95b4fcd | 2016-03-01 08:18:28 +0000 | [diff] [blame] | 5285 | (getTok().getIdentifier() == ".rept" || |
| 5286 | getTok().getIdentifier() == ".irp" || |
| 5287 | getTok().getIdentifier() == ".irpc")) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5288 | ++NestLevel; |
| 5289 | } |
| 5290 | |
| 5291 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5292 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5293 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5294 | EndToken = getTok(); |
| 5295 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5296 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5297 | printError(getTok().getLoc(), |
| 5298 | "unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5299 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5300 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5301 | break; |
| 5302 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5303 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5306 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5307 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 5311 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 5312 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 5313 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5314 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5315 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 5316 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5319 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5320 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5321 | OS << ".endr\n"; |
| 5322 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 5323 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 5324 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5325 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5326 | // Create the macro instantiation object and add to the current macro |
| 5327 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 5328 | MacroInstantiation *MI = new MacroInstantiation( |
| 5329 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5330 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5331 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5332 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 5333 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 5334 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5335 | Lex(); |
| 5336 | } |
| 5337 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5338 | /// parseDirectiveRept |
| 5339 | /// ::= .rep | .rept count |
| 5340 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5341 | const MCExpr *CountExpr; |
| 5342 | SMLoc CountLoc = getTok().getLoc(); |
| 5343 | if (parseExpression(CountExpr)) |
| 5344 | return true; |
| 5345 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5346 | int64_t Count; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 5347 | if (!CountExpr->evaluateAsAbsolute(Count)) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5348 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 5349 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5350 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5351 | if (check(Count < 0, CountLoc, "Count is negative") || |
| 5352 | parseToken(AsmToken::EndOfStatement, |
| 5353 | "unexpected token in '" + Dir + "' directive")) |
| 5354 | return true; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5355 | |
| 5356 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5357 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5358 | if (!M) |
| 5359 | return true; |
| 5360 | |
| 5361 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5362 | // to hold the macro body with substitutions. |
| 5363 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5364 | raw_svector_ostream OS(Buf); |
| 5365 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5366 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 5367 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5368 | return true; |
| 5369 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5370 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5371 | |
| 5372 | return false; |
| 5373 | } |
| 5374 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5375 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5376 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5377 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5378 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5379 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5380 | if (check(parseIdentifier(Parameter.Name), |
| 5381 | "expected identifier in '.irp' directive") || |
| 5382 | parseToken(AsmToken::Comma, "expected comma in '.irp' directive") || |
| 5383 | parseMacroArguments(nullptr, A) || |
| 5384 | parseToken(AsmToken::EndOfStatement, "expected End of Statement")) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5385 | return true; |
| 5386 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5387 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5388 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5389 | if (!M) |
| 5390 | return true; |
| 5391 | |
| 5392 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5393 | // to hold the macro body with substitutions. |
| 5394 | SmallString<256> Buf; |
| 5395 | raw_svector_ostream OS(Buf); |
| 5396 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5397 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5398 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 5399 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5400 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5401 | return true; |
| 5402 | } |
| 5403 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5404 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5405 | |
| 5406 | return false; |
| 5407 | } |
| 5408 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5409 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5410 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5411 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5412 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5413 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5414 | |
| 5415 | if (check(parseIdentifier(Parameter.Name), |
| 5416 | "expected identifier in '.irpc' directive") || |
| 5417 | parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") || |
| 5418 | parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5419 | return true; |
| 5420 | |
| 5421 | if (A.size() != 1 || A.front().size() != 1) |
| 5422 | return TokError("unexpected token in '.irpc' directive"); |
| 5423 | |
| 5424 | // Eat the end of statement. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5425 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5426 | return true; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5427 | |
| 5428 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5429 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5430 | if (!M) |
| 5431 | return true; |
| 5432 | |
| 5433 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5434 | // to hold the macro body with substitutions. |
| 5435 | SmallString<256> Buf; |
| 5436 | raw_svector_ostream OS(Buf); |
| 5437 | |
| 5438 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 5439 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 5440 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5441 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5442 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5443 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 5444 | // This is undocumented, but GAS seems to support it. |
| 5445 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5446 | return true; |
| 5447 | } |
| 5448 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5449 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5450 | |
| 5451 | return false; |
| 5452 | } |
| 5453 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5454 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5455 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 5456 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5457 | |
| 5458 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5459 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5460 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 5461 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5462 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5463 | return false; |
| 5464 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 5465 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5466 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5467 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5468 | const MCExpr *Value; |
| 5469 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5470 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5471 | return true; |
| 5472 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5473 | if (!MCE) |
| 5474 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 5475 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 5476 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5477 | return Error(ExprLoc, "literal value out of range for directive"); |
| 5478 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5479 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5480 | return false; |
| 5481 | } |
| 5482 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5483 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5484 | const MCExpr *Value; |
| 5485 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5486 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5487 | return true; |
| 5488 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5489 | if (!MCE) |
| 5490 | return Error(ExprLoc, "unexpected expression in align"); |
| 5491 | uint64_t IntValue = MCE->getValue(); |
| 5492 | if (!isPowerOf2_64(IntValue)) |
| 5493 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 5494 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5495 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5496 | return false; |
| 5497 | } |
| 5498 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5499 | bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) { |
| 5500 | const AsmToken StrTok = getTok(); |
| 5501 | Lex(); |
| 5502 | if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"') |
| 5503 | return Error(DirectiveLoc, "expected double quoted string after .print"); |
| 5504 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5505 | return true; |
| 5506 | llvm::outs() << StrTok.getStringContents() << '\n'; |
| 5507 | return false; |
| 5508 | } |
| 5509 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 5510 | // We are comparing pointers, but the pointers are relative to a single string. |
| 5511 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 5512 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 5513 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 5514 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 5515 | return -1; |
| 5516 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 5517 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5518 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5519 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 5520 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 5521 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 5522 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5523 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 5524 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5525 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5526 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5527 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 5528 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5529 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5530 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5533 | bool AsmParser::parseMSInlineAsm( |
| 5534 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 5535 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5536 | SmallVectorImpl<std::string> &Constraints, |
| 5537 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 5538 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5539 | SmallVector<void *, 4> InputDecls; |
| 5540 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5541 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 5542 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5543 | SmallVector<std::string, 4> InputConstraints; |
| 5544 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5545 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5546 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5547 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5548 | |
| 5549 | // Prime the lexer. |
| 5550 | Lex(); |
| 5551 | |
| 5552 | // While we have input, parse each statement. |
| 5553 | unsigned InputIdx = 0; |
| 5554 | unsigned OutputIdx = 0; |
| 5555 | while (getLexer().isNot(AsmToken::Eof)) { |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5556 | // Parse curly braces marking block start/end |
| 5557 | if (parseCurlyBlockScope(AsmStrRewrites)) |
| 5558 | continue; |
| 5559 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5560 | ParseStatementInfo Info(&AsmStrRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5561 | bool StatementErr = parseStatement(Info, &SI); |
Nirav Dave | 9fa8af2 | 2016-09-13 13:55:06 +0000 | [diff] [blame] | 5562 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5563 | if (StatementErr || Info.ParseError) { |
| 5564 | // Emit pending errors if any exist. |
| 5565 | printPendingErrors(); |
Nico Weber | e204c48 | 2016-09-13 18:17:00 +0000 | [diff] [blame] | 5566 | return true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | // No pending error should exist here. |
| 5570 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 5571 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5572 | if (Info.Opcode == ~0U) |
| 5573 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5574 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5575 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5576 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5577 | // Build the list of clobbers, outputs and inputs. |
| 5578 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5579 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5580 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5581 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5582 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5583 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5584 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5585 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 5586 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 5587 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5588 | unsigned NumDefs = Desc.getNumDefs(); |
| 5589 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5590 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 5591 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5592 | continue; |
| 5593 | } |
| 5594 | |
| 5595 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5596 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5597 | if (SymName.empty()) |
| 5598 | continue; |
| 5599 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5600 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5601 | if (!OpDecl) |
| 5602 | continue; |
| 5603 | |
| 5604 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5605 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5606 | if (isOutput) { |
| 5607 | ++InputIdx; |
| 5608 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5609 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 5610 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5611 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5612 | } else { |
| 5613 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5614 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 5615 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5616 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5617 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5618 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 5619 | |
| 5620 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 5621 | ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), |
| 5622 | Desc.getNumImplicitDefs()); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5623 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5627 | NumOutputs = OutputDecls.size(); |
| 5628 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5629 | |
| 5630 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5631 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 5632 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 5633 | ClobberRegs.end()); |
| 5634 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 5635 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 5636 | raw_string_ostream OS(Clobbers[I]); |
| 5637 | IP->printRegName(OS, ClobberRegs[I]); |
| 5638 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5639 | |
| 5640 | // Merge the various outputs and inputs. Output are expected first. |
| 5641 | if (NumOutputs || NumInputs) { |
| 5642 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5643 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5644 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5645 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5646 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5647 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5648 | } |
| 5649 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5650 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5651 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 5656 | std::string AsmStringIR; |
| 5657 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 5658 | StringRef ASMString = |
| 5659 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 5660 | const char *AsmStart = ASMString.begin(); |
| 5661 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5662 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5663 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 5664 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5665 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5666 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5667 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5668 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 5669 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5670 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5671 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5672 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5673 | // Skip the original expression. |
| 5674 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5675 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5676 | continue; |
| 5677 | } |
| 5678 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5679 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5680 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5681 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5682 | default: |
| 5683 | break; |
Coby Tayree | d891289 | 2017-08-24 08:46:25 +0000 | [diff] [blame] | 5684 | case AOK_IntelExpr: |
| 5685 | assert(AR.IntelExp.isValid() && "cannot write invalid intel expression"); |
| 5686 | if (AR.IntelExp.NeedBracs) |
| 5687 | OS << "["; |
| 5688 | if (AR.IntelExp.hasBaseReg()) |
| 5689 | OS << AR.IntelExp.BaseReg; |
| 5690 | if (AR.IntelExp.hasIndexReg()) |
| 5691 | OS << (AR.IntelExp.hasBaseReg() ? " + " : "") |
| 5692 | << AR.IntelExp.IndexReg; |
| 5693 | if (AR.IntelExp.Scale > 1) |
| 5694 | OS << " * $$" << AR.IntelExp.Scale; |
| 5695 | if (AR.IntelExp.Imm || !AR.IntelExp.hasRegs()) |
| 5696 | OS << (AR.IntelExp.hasRegs() ? " + $$" : "$$") << AR.IntelExp.Imm; |
| 5697 | if (AR.IntelExp.NeedBracs) |
| 5698 | OS << "]"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5699 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5700 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 5701 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5702 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5703 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5704 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5705 | break; |
| 5706 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5707 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5708 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5709 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5710 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5711 | default: break; |
| 5712 | case 8: OS << "byte ptr "; break; |
| 5713 | case 16: OS << "word ptr "; break; |
| 5714 | case 32: OS << "dword ptr "; break; |
| 5715 | case 64: OS << "qword ptr "; break; |
| 5716 | case 80: OS << "xword ptr "; break; |
| 5717 | case 128: OS << "xmmword ptr "; break; |
| 5718 | case 256: OS << "ymmword ptr "; break; |
| 5719 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5720 | break; |
| 5721 | case AOK_Emit: |
| 5722 | OS << ".byte"; |
| 5723 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5724 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5725 | // MS alignment directives are measured in bytes. If the native assembler |
| 5726 | // measures alignment in bytes, we can pass it straight through. |
| 5727 | OS << ".align"; |
| 5728 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 5729 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5730 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5731 | // Alignment is in log2 form, so print that instead and skip the original |
| 5732 | // immediate. |
| 5733 | unsigned Val = AR.Val; |
| 5734 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5735 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5736 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 5737 | break; |
| 5738 | } |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 5739 | case AOK_EVEN: |
| 5740 | OS << ".even"; |
| 5741 | break; |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5742 | case AOK_EndOfStatement: |
| 5743 | OS << "\n\t"; |
| 5744 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5745 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5746 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5747 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5748 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5752 | if (AsmStart != AsmEnd) |
| 5753 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5754 | |
| 5755 | AsmString = OS.str(); |
| 5756 | return false; |
| 5757 | } |
| 5758 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5759 | namespace llvm { |
| 5760 | namespace MCParserUtils { |
| 5761 | |
| 5762 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 5763 | /// or subexpressions. |
| 5764 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 5765 | switch (Value->getKind()) { |
| 5766 | case MCExpr::Binary: { |
| 5767 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 5768 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 5769 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 5770 | } |
| 5771 | case MCExpr::Target: |
| 5772 | case MCExpr::Constant: |
| 5773 | return false; |
| 5774 | case MCExpr::SymbolRef: { |
| 5775 | const MCSymbol &S = |
| 5776 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 5777 | if (S.isVariable()) |
| 5778 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 5779 | return &S == Sym; |
| 5780 | } |
| 5781 | case MCExpr::Unary: |
| 5782 | return isSymbolUsedInExpression( |
| 5783 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 5784 | } |
| 5785 | |
| 5786 | llvm_unreachable("Unknown expr kind!"); |
| 5787 | } |
| 5788 | |
| 5789 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 5790 | MCAsmParser &Parser, MCSymbol *&Sym, |
| 5791 | const MCExpr *&Value) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5792 | |
| 5793 | // FIXME: Use better location, we should use proper tokens. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 5794 | SMLoc EqualLoc = Parser.getTok().getLoc(); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5795 | |
| 5796 | if (Parser.parseExpression(Value)) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5797 | return Parser.TokError("missing expression"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5798 | } |
| 5799 | |
| 5800 | // Note: we don't count b as used in "a = b". This is to allow |
| 5801 | // a = b |
| 5802 | // b = c |
| 5803 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5804 | if (Parser.parseToken(AsmToken::EndOfStatement)) |
| 5805 | return true; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5806 | |
| 5807 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 5808 | // used as a symbol, or it is an absolute symbol). |
| 5809 | Sym = Parser.getContext().lookupSymbol(Name); |
| 5810 | if (Sym) { |
| 5811 | // Diagnose assignment to a label. |
| 5812 | // |
| 5813 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 5814 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 5815 | if (isSymbolUsedInExpression(Sym, Value)) |
| 5816 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 5817 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 5818 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5819 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 5820 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 5821 | ; // Allow redefinitions of variables that haven't yet been used. |
| 5822 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 5823 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 5824 | else if (!Sym->isVariable()) |
| 5825 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 5826 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 5827 | return Parser.Error(EqualLoc, |
| 5828 | "invalid reassignment of non-absolute variable '" + |
| 5829 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5830 | } else if (Name == ".") { |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 5831 | Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5832 | return false; |
| 5833 | } else |
| 5834 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 5835 | |
| 5836 | Sym->setRedefinable(allow_redef); |
| 5837 | |
| 5838 | return false; |
| 5839 | } |
| 5840 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5841 | } // end namespace MCParserUtils |
| 5842 | } // end namespace llvm |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5843 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 5844 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5845 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 5846 | MCStreamer &Out, const MCAsmInfo &MAI, |
| 5847 | unsigned CB) { |
| 5848 | return new AsmParser(SM, C, Out, MAI, CB); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 5849 | } |