Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This class implements the parser for assembly files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/APFloat.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APInt.h" |
| 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/None.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 24 | #include "llvm/BinaryFormat/Dwarf.h" |
Reid Kleckner | 7bbe711 | 2019-10-19 01:44:09 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/CodeView/SymbolRecord.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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 83 | /// Helper types for tracking macro definitions. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 87 | /// Helper class for storing information about an active macro |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 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 { |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 107 | /// 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 110 | /// 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 113 | /// 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 123 | /// The concrete assembly parser instance. |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 142 | /// 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 147 | /// Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 148 | std::vector<MacroInstantiation*> ActiveMacros; |
| 149 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 150 | /// 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 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 156 | /// Keeps track of how many .macro's have been instantiated. |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 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; |
Serge Guelton | 836aa27 | 2019-01-20 23:06:45 +0000 | [diff] [blame] | 162 | int64_t LineNumber; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 163 | SMLoc Loc; |
Serge Guelton | 836aa27 | 2019-01-20 23:06:45 +0000 | [diff] [blame] | 164 | unsigned Buf; |
| 165 | CppHashInfoTy() : Filename(), LineNumber(0), Loc(), Buf(0) {} |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 166 | }; |
| 167 | CppHashInfoTy CppHashInfo; |
| 168 | |
Paul Robinson | 9c56326 | 2019-05-21 11:59:03 +0000 | [diff] [blame] | 169 | /// The filename from the first cpp hash file line comment, if any. |
| 170 | StringRef FirstCppHashFilename; |
| 171 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 172 | /// List of forward directional labels for diagnosis at the end. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 173 | SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
| 174 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 175 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 176 | unsigned AssemblerDialect = ~0U; |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 177 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 178 | /// is Darwin compatibility enabled? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 179 | bool IsDarwin = false; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 180 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 181 | /// Are we parsing ms-style inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 182 | bool ParsingInlineAsm = false; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 183 | |
Paul Robinson | cc7344a | 2018-06-14 13:38:20 +0000 | [diff] [blame] | 184 | /// Did we already inform the user about inconsistent MD5 usage? |
| 185 | bool ReportedInconsistentMD5 = false; |
| 186 | |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 187 | // Is alt macro mode enabled. |
| 188 | bool AltMacroMode = false; |
| 189 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 190 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 191 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 192 | const MCAsmInfo &MAI, unsigned CB); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 193 | AsmParser(const AsmParser &) = delete; |
| 194 | AsmParser &operator=(const AsmParser &) = delete; |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 195 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 196 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 197 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 198 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 199 | void addDirectiveHandler(StringRef Directive, |
| 200 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 201 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 204 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 205 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 206 | } |
| 207 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 208 | /// @name MCAsmParser Interface |
| 209 | /// { |
| 210 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 211 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 212 | MCAsmLexer &getLexer() override { return Lexer; } |
| 213 | MCContext &getContext() override { return Ctx; } |
| 214 | MCStreamer &getStreamer() override { return Out; } |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 215 | |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 216 | CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
| 217 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 218 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 219 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 220 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 221 | else |
| 222 | return AssemblerDialect; |
| 223 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 224 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 225 | AssemblerDialect = i; |
| 226 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 227 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 228 | void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 229 | bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 230 | bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 231 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 232 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 233 | |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 234 | void setParsingInlineAsm(bool V) override { |
| 235 | ParsingInlineAsm = V; |
Reid Kleckner | 953bdce | 2018-10-24 20:23:57 +0000 | [diff] [blame] | 236 | // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and |
| 237 | // hex integer literals. |
| 238 | Lexer.setLexMasmIntegers(V); |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 239 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 240 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 241 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 242 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 243 | unsigned &NumOutputs, unsigned &NumInputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 244 | SmallVectorImpl<std::pair<void *,bool>> &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 245 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 246 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 247 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 248 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 249 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 250 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 251 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 252 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 253 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 254 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 255 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 256 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 257 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 258 | /// Parse a floating point expression using the float \p Semantics |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 259 | /// and set \p Res to the value. |
| 260 | bool parseRealValue(const fltSemantics &Semantics, APInt &Res); |
| 261 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 262 | /// Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 263 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 264 | bool parseIdentifier(StringRef &Res) override; |
| 265 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 266 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 267 | bool checkForValidSection() override; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 268 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 269 | /// } |
| 270 | |
| 271 | private: |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 272 | bool parseStatement(ParseStatementInfo &Info, |
| 273 | MCAsmParserSemaCallback *SI); |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 274 | bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 275 | bool parseCppHashLineFilenameComment(SMLoc L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 276 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 277 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 278 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 279 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 280 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 281 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 282 | SMLoc L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 283 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 284 | /// Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 285 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 286 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 287 | /// Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 288 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 289 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 290 | /// Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 291 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 292 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 293 | /// Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 294 | /// |
| 295 | /// \param M The macro. |
| 296 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 297 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 298 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 299 | /// Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 300 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 301 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 302 | /// Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 303 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 304 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 305 | /// Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 306 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 307 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 308 | void printMacroInstantiations(); |
| 309 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 310 | SMRange Range = None) const { |
| 311 | ArrayRef<SMRange> Ranges(Range); |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 312 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 313 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 314 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 315 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 316 | /// Should we emit DWARF describing this assembler source? (Returns false if |
| 317 | /// the source has .file directives, which means we don't want to generate |
| 318 | /// info describing the assembler source itself.) |
| 319 | bool enabledGenDwarfForAssembly(); |
| 320 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 321 | /// Enter the specified file. This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 322 | bool enterIncludeFile(const std::string &Filename); |
| 323 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 324 | /// Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 325 | /// This returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 326 | bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, |
| 327 | const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 328 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 329 | /// Reset the current lexer position to that given by \p Loc. The |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 330 | /// current token is not set; clients should ensure Lex() is called |
| 331 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 332 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 333 | /// \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] | 334 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 335 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 336 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 337 | /// Parse up to the end of statement and a return the contents from the |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 338 | /// current token until the end of the statement; the current token on exit |
| 339 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 340 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 341 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 342 | /// Parse until the end of a statement or a comma is encountered, |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 343 | /// 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] | 344 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 345 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 346 | bool parseAssignment(StringRef Name, bool allow_redef, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 347 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 348 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 349 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 350 | MCBinaryExpr::Opcode &Kind); |
| 351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 352 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 353 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 354 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 355 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 356 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 357 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 358 | bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); |
| 359 | bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); |
| 360 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 361 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 362 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 363 | DK_NO_DIRECTIVE, // Placeholder |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 364 | DK_SET, |
| 365 | DK_EQU, |
| 366 | DK_EQUIV, |
| 367 | DK_ASCII, |
| 368 | DK_ASCIZ, |
| 369 | DK_STRING, |
| 370 | DK_BYTE, |
| 371 | DK_SHORT, |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 372 | DK_RELOC, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 373 | DK_VALUE, |
| 374 | DK_2BYTE, |
| 375 | DK_LONG, |
| 376 | DK_INT, |
| 377 | DK_4BYTE, |
| 378 | DK_QUAD, |
| 379 | DK_8BYTE, |
| 380 | DK_OCTA, |
| 381 | DK_DC, |
| 382 | DK_DC_A, |
| 383 | DK_DC_B, |
| 384 | DK_DC_D, |
| 385 | DK_DC_L, |
| 386 | DK_DC_S, |
| 387 | DK_DC_W, |
| 388 | DK_DC_X, |
| 389 | DK_DCB, |
| 390 | DK_DCB_B, |
| 391 | DK_DCB_D, |
| 392 | DK_DCB_L, |
| 393 | DK_DCB_S, |
| 394 | DK_DCB_W, |
| 395 | DK_DCB_X, |
| 396 | DK_DS, |
| 397 | DK_DS_B, |
| 398 | DK_DS_D, |
| 399 | DK_DS_L, |
| 400 | DK_DS_P, |
| 401 | DK_DS_S, |
| 402 | DK_DS_W, |
| 403 | DK_DS_X, |
| 404 | DK_SINGLE, |
| 405 | DK_FLOAT, |
| 406 | DK_DOUBLE, |
| 407 | DK_ALIGN, |
| 408 | DK_ALIGN32, |
| 409 | DK_BALIGN, |
| 410 | DK_BALIGNW, |
| 411 | DK_BALIGNL, |
| 412 | DK_P2ALIGN, |
| 413 | DK_P2ALIGNW, |
| 414 | DK_P2ALIGNL, |
| 415 | DK_ORG, |
| 416 | DK_FILL, |
| 417 | DK_ENDR, |
| 418 | DK_BUNDLE_ALIGN_MODE, |
| 419 | DK_BUNDLE_LOCK, |
| 420 | DK_BUNDLE_UNLOCK, |
| 421 | DK_ZERO, |
| 422 | DK_EXTERN, |
| 423 | DK_GLOBL, |
| 424 | DK_GLOBAL, |
| 425 | DK_LAZY_REFERENCE, |
| 426 | DK_NO_DEAD_STRIP, |
| 427 | DK_SYMBOL_RESOLVER, |
| 428 | DK_PRIVATE_EXTERN, |
| 429 | DK_REFERENCE, |
| 430 | DK_WEAK_DEFINITION, |
| 431 | DK_WEAK_REFERENCE, |
| 432 | DK_WEAK_DEF_CAN_BE_HIDDEN, |
Vedant Kumar | 13ef84f | 2019-01-25 18:30:22 +0000 | [diff] [blame] | 433 | DK_COLD, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 434 | DK_COMM, |
| 435 | DK_COMMON, |
| 436 | DK_LCOMM, |
| 437 | DK_ABORT, |
| 438 | DK_INCLUDE, |
| 439 | DK_INCBIN, |
| 440 | DK_CODE16, |
| 441 | DK_CODE16GCC, |
| 442 | DK_REPT, |
| 443 | DK_IRP, |
| 444 | DK_IRPC, |
| 445 | DK_IF, |
| 446 | DK_IFEQ, |
| 447 | DK_IFGE, |
| 448 | DK_IFGT, |
| 449 | DK_IFLE, |
| 450 | DK_IFLT, |
| 451 | DK_IFNE, |
| 452 | DK_IFB, |
| 453 | DK_IFNB, |
| 454 | DK_IFC, |
| 455 | DK_IFEQS, |
| 456 | DK_IFNC, |
| 457 | DK_IFNES, |
| 458 | DK_IFDEF, |
| 459 | DK_IFNDEF, |
| 460 | DK_IFNOTDEF, |
| 461 | DK_ELSEIF, |
| 462 | DK_ELSE, |
| 463 | DK_ENDIF, |
| 464 | DK_SPACE, |
| 465 | DK_SKIP, |
| 466 | DK_FILE, |
| 467 | DK_LINE, |
| 468 | DK_LOC, |
| 469 | DK_STABS, |
| 470 | DK_CV_FILE, |
| 471 | DK_CV_FUNC_ID, |
| 472 | DK_CV_INLINE_SITE_ID, |
| 473 | DK_CV_LOC, |
| 474 | DK_CV_LINETABLE, |
| 475 | DK_CV_INLINE_LINETABLE, |
| 476 | DK_CV_DEF_RANGE, |
| 477 | DK_CV_STRINGTABLE, |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 478 | DK_CV_STRING, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 479 | DK_CV_FILECHECKSUMS, |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 480 | DK_CV_FILECHECKSUM_OFFSET, |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 481 | DK_CV_FPO_DATA, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 482 | DK_CFI_SECTIONS, |
| 483 | DK_CFI_STARTPROC, |
| 484 | DK_CFI_ENDPROC, |
| 485 | DK_CFI_DEF_CFA, |
| 486 | DK_CFI_DEF_CFA_OFFSET, |
| 487 | DK_CFI_ADJUST_CFA_OFFSET, |
| 488 | DK_CFI_DEF_CFA_REGISTER, |
| 489 | DK_CFI_OFFSET, |
| 490 | DK_CFI_REL_OFFSET, |
| 491 | DK_CFI_PERSONALITY, |
| 492 | DK_CFI_LSDA, |
| 493 | DK_CFI_REMEMBER_STATE, |
| 494 | DK_CFI_RESTORE_STATE, |
| 495 | DK_CFI_SAME_VALUE, |
| 496 | DK_CFI_RESTORE, |
| 497 | DK_CFI_ESCAPE, |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 498 | DK_CFI_RETURN_COLUMN, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 499 | DK_CFI_SIGNAL_FRAME, |
| 500 | DK_CFI_UNDEFINED, |
| 501 | DK_CFI_REGISTER, |
| 502 | DK_CFI_WINDOW_SAVE, |
Luke Cheeseman | 41a9e53 | 2018-12-21 10:45:08 +0000 | [diff] [blame] | 503 | DK_CFI_B_KEY_FRAME, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 504 | DK_MACROS_ON, |
| 505 | DK_MACROS_OFF, |
| 506 | DK_ALTMACRO, |
| 507 | DK_NOALTMACRO, |
| 508 | DK_MACRO, |
| 509 | DK_EXITM, |
| 510 | DK_ENDM, |
| 511 | DK_ENDMACRO, |
| 512 | DK_PURGEM, |
| 513 | DK_SLEB128, |
| 514 | DK_ULEB128, |
| 515 | DK_ERR, |
| 516 | DK_ERROR, |
| 517 | DK_WARNING, |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 518 | DK_PRINT, |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 519 | DK_ADDRSIG, |
| 520 | DK_ADDRSIG_SYM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 521 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 524 | /// Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 525 | /// directives parsed by this class. |
| 526 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 527 | |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 528 | // Codeview def_range type parsing. |
| 529 | enum CVDefRangeType { |
| 530 | CVDR_DEFRANGE = 0, // Placeholder |
| 531 | CVDR_DEFRANGE_REGISTER, |
| 532 | CVDR_DEFRANGE_FRAMEPOINTER_REL, |
| 533 | CVDR_DEFRANGE_SUBFIELD_REGISTER, |
| 534 | CVDR_DEFRANGE_REGISTER_REL |
| 535 | }; |
| 536 | |
| 537 | /// Maps Codeview def_range types --> CVDefRangeType enum, for |
| 538 | /// Codeview def_range types parsed by this class. |
| 539 | StringMap<CVDefRangeType> CVDefRangeTypeMap; |
| 540 | |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 541 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 542 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 543 | bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 544 | bool parseDirectiveValue(StringRef IDVal, |
| 545 | unsigned Size); // ".byte", ".long", ... |
| 546 | bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
| 547 | bool parseDirectiveRealValue(StringRef IDVal, |
| 548 | const fltSemantics &); // ".single", ... |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 549 | bool parseDirectiveFill(); // ".fill" |
| 550 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 551 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 552 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 553 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 554 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 555 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 556 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 557 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 558 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 559 | bool parseDirectiveLine(); |
| 560 | bool parseDirectiveLoc(); |
| 561 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 562 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 563 | // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 564 | // ".cv_inline_linetable", ".cv_def_range", ".cv_string" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 565 | bool parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 566 | bool parseDirectiveCVFuncId(); |
| 567 | bool parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 568 | bool parseDirectiveCVLoc(); |
| 569 | bool parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 570 | bool parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 571 | bool parseDirectiveCVDefRange(); |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 572 | bool parseDirectiveCVString(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 573 | bool parseDirectiveCVStringTable(); |
| 574 | bool parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 575 | bool parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 576 | bool parseDirectiveCVFPOData(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 577 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 578 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 579 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 580 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 581 | bool parseDirectiveCFISections(); |
| 582 | bool parseDirectiveCFIStartProc(); |
| 583 | bool parseDirectiveCFIEndProc(); |
| 584 | bool parseDirectiveCFIDefCfaOffset(); |
| 585 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 586 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 587 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 588 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 589 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 590 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 591 | bool parseDirectiveCFIRememberState(); |
| 592 | bool parseDirectiveCFIRestoreState(); |
| 593 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 594 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 595 | bool parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 596 | bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 597 | bool parseDirectiveCFISignalFrame(); |
| 598 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 599 | |
| 600 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 601 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 602 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 603 | bool parseDirectiveEndMacro(StringRef Directive); |
| 604 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 605 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 606 | // alternate macro mode directives |
| 607 | bool parseDirectiveAltmacro(StringRef Directive); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 608 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 609 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 610 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 611 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 612 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 613 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 614 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 615 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 616 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 617 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 618 | // ".dcb" |
| 619 | bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
| 620 | bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 621 | // ".ds" |
| 622 | bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 623 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 624 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 625 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 626 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 627 | /// Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 628 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 629 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 630 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 631 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 633 | bool parseDirectiveAbort(); // ".abort" |
| 634 | bool parseDirectiveInclude(); // ".include" |
| 635 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 636 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 637 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 638 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 639 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 640 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 641 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 642 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 643 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 644 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 645 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 646 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 647 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 648 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 649 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 650 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 651 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 652 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 653 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 654 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 655 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 656 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 657 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 658 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 659 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 660 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 661 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 662 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 663 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 664 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 665 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 666 | size_t Len); |
| 667 | |
| 668 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 669 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 670 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 671 | // "end" |
| 672 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 673 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 674 | // ".err" or ".error" |
| 675 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 676 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 677 | // ".warning" |
| 678 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 679 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 680 | // .print <double-quotes-string> |
| 681 | bool parseDirectivePrint(SMLoc DirectiveLoc); |
| 682 | |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 683 | // Directives to support address-significance tables. |
| 684 | bool parseDirectiveAddrsig(); |
| 685 | bool parseDirectiveAddrsigSym(); |
| 686 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 687 | void initializeDirectiveKindMap(); |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 688 | void initializeCVDefRangeTypeMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 689 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 690 | |
| 691 | } // end anonymous namespace |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 692 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 693 | namespace llvm { |
| 694 | |
| 695 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 696 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 697 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 698 | extern MCAsmParserExtension *createWasmAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 699 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 700 | } // end namespace llvm |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 701 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 702 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 703 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 704 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 705 | const MCAsmInfo &MAI, unsigned CB = 0) |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 706 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 707 | CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 708 | HadError = false; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 709 | // Save the old handler. |
| 710 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 711 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 712 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 713 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 714 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 715 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 716 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 717 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 718 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 719 | PlatformParser.reset(createCOFFAsmParser()); |
| 720 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 721 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 722 | PlatformParser.reset(createDarwinAsmParser()); |
| 723 | IsDarwin = true; |
| 724 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 725 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 726 | PlatformParser.reset(createELFAsmParser()); |
| 727 | break; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 728 | case MCObjectFileInfo::IsWasm: |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 729 | PlatformParser.reset(createWasmAsmParser()); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 730 | break; |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 731 | case MCObjectFileInfo::IsXCOFF: |
Hubert Tong | fc34a53 | 2019-08-06 15:05:20 +0000 | [diff] [blame] | 732 | report_fatal_error( |
| 733 | "Need to implement createXCOFFAsmParser for XCOFF format."); |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 734 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 735 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 736 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 737 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 738 | initializeDirectiveKindMap(); |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 739 | initializeCVDefRangeTypeMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 740 | |
| 741 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 744 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 745 | assert((HadError || ActiveMacros.empty()) && |
| 746 | "Unexpected active macro instantiation!"); |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 747 | |
| 748 | // Restore the saved diagnostics handler and context for use during |
| 749 | // finalization. |
| 750 | SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 753 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 754 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 755 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 756 | it = ActiveMacros.rbegin(), |
| 757 | ie = ActiveMacros.rend(); |
| 758 | it != ie; ++it) |
| 759 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 760 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 763 | void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
| 764 | printPendingErrors(); |
| 765 | printMessage(L, SourceMgr::DK_Note, Msg, Range); |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 766 | printMacroInstantiations(); |
| 767 | } |
| 768 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 769 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 770 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 771 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 772 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 773 | return Error(L, Msg, Range); |
| 774 | printMessage(L, SourceMgr::DK_Warning, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 775 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 776 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 779 | bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 780 | HadError = true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 781 | printMessage(L, SourceMgr::DK_Error, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 782 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 783 | return true; |
| 784 | } |
| 785 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 786 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 787 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 788 | unsigned NewBuf = |
| 789 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 790 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 791 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 792 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 793 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 794 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 795 | return false; |
| 796 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 797 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 798 | /// 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] | 799 | /// 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] | 800 | /// returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 801 | bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
| 802 | const MCExpr *Count, SMLoc Loc) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 803 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 804 | unsigned NewBuf = |
| 805 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 806 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 807 | return true; |
| 808 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 809 | // Pick up the bytes from the file and emit them. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 810 | StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); |
| 811 | Bytes = Bytes.drop_front(Skip); |
| 812 | if (Count) { |
| 813 | int64_t Res; |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 814 | if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 815 | return Error(Loc, "expected absolute expression"); |
| 816 | if (Res < 0) |
| 817 | return Warning(Loc, "negative count has no effect"); |
| 818 | Bytes = Bytes.take_front(Res); |
| 819 | } |
| 820 | getStreamer().EmitBytes(Bytes); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 821 | return false; |
| 822 | } |
| 823 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 824 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 825 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 826 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 827 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 830 | const AsmToken &AsmParser::Lex() { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 831 | if (Lexer.getTok().is(AsmToken::Error)) |
| 832 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
| 833 | |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 834 | // if it's a end of statement with a comment in it |
| 835 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 836 | // if this is a line comment output it. |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 837 | if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 838 | getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
| 839 | Out.addExplicitComment(Twine(getTok().getString())); |
| 840 | } |
| 841 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 842 | const AsmToken *tok = &Lexer.Lex(); |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 843 | |
| 844 | // Parse comments here to be deferred until end of next statement. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 845 | while (tok->is(AsmToken::Comment)) { |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 846 | if (MAI.preserveAsmComments()) |
| 847 | Out.addExplicitComment(Twine(tok->getString())); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 848 | tok = &Lexer.Lex(); |
| 849 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 850 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 851 | if (tok->is(AsmToken::Eof)) { |
| 852 | // If this is the end of an included file, pop the parent file off the |
| 853 | // include stack. |
| 854 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 855 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 856 | jumpToLoc(ParentIncludeLoc); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 857 | return Lex(); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 860 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 861 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 864 | bool AsmParser::enabledGenDwarfForAssembly() { |
| 865 | // Check whether the user specified -g. |
| 866 | if (!getContext().getGenDwarfForAssembly()) |
| 867 | return false; |
| 868 | // If we haven't encountered any .file directives (which would imply that |
| 869 | // the assembler source was produced with debug info already) then emit one |
| 870 | // describing the assembler source file itself. |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 871 | if (getContext().getGenDwarfFileNumber() == 0) { |
Paul Robinson | 9c56326 | 2019-05-21 11:59:03 +0000 | [diff] [blame] | 872 | // Use the first #line directive for this, if any. It's preprocessed, so |
| 873 | // there is no checksum, and of course no source directive. |
| 874 | if (!FirstCppHashFilename.empty()) |
| 875 | getContext().setMCLineTableRootFile(/*CUID=*/0, |
| 876 | getContext().getCompilationDir(), |
| 877 | FirstCppHashFilename, |
| 878 | /*Cksum=*/None, /*Source=*/None); |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 879 | const MCDwarfFile &RootFile = |
| 880 | getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile(); |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 881 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 882 | /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name, |
| 883 | RootFile.Checksum, RootFile.Source)); |
| 884 | } |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 885 | return true; |
| 886 | } |
| 887 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 888 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 889 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 890 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 891 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 892 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 893 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 894 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 895 | |
| 896 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 897 | AsmCond StartingCondState = TheCondState; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 898 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 899 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 900 | // 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] | 901 | // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't |
| 902 | // emitting any actual debug info yet and haven't had a chance to parse any |
| 903 | // embedded .file directives.) |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 904 | if (getContext().getGenDwarfForAssembly()) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 905 | MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 906 | if (!Sec->getBeginSymbol()) { |
| 907 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 908 | getStreamer().EmitLabel(SectionStartSym); |
| 909 | Sec->setBeginSymbol(SectionStartSym); |
| 910 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 911 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 912 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 913 | (void)InsertResult; |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 916 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 917 | while (Lexer.isNot(AsmToken::Eof)) { |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 918 | ParseStatementInfo Info(&AsmStrRewrites); |
Joerg Sonnenberger | 60b403e | 2019-10-23 15:39:10 +0200 | [diff] [blame] | 919 | bool Parsed = parseStatement(Info, nullptr); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 920 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 921 | // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
| 922 | // for printing ErrMsg via Lex() only if no (presumably better) parser error |
| 923 | // exists. |
Joerg Sonnenberger | 60b403e | 2019-10-23 15:39:10 +0200 | [diff] [blame] | 924 | if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 925 | Lex(); |
| 926 | } |
| 927 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 928 | // parseStatement returned true so may need to emit an error. |
| 929 | printPendingErrors(); |
| 930 | |
| 931 | // Skipping to the next line if needed. |
Joerg Sonnenberger | 60b403e | 2019-10-23 15:39:10 +0200 | [diff] [blame] | 932 | if (Parsed && !getLexer().isAtStartOfStatement()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 933 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 934 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 935 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 936 | getTargetParser().onEndOfFile(); |
| 937 | printPendingErrors(); |
| 938 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 939 | // All errors should have been emitted. |
| 940 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
| 941 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 942 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 943 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 944 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 945 | TheCondState.Ignore != StartingCondState.Ignore) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 946 | printError(getTok().getLoc(), "unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 947 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 948 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 949 | if (!LineTables.empty()) { |
| 950 | unsigned Index = 0; |
| 951 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 952 | if (File.Name.empty() && Index != 0) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 953 | printError(getTok().getLoc(), "unassigned file number: " + |
| 954 | Twine(Index) + |
| 955 | " for .file directives"); |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 956 | ++Index; |
| 957 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 958 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 959 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 960 | // Check to see that all assembler local symbols were actually defined. |
| 961 | // Targets that don't do subsections via symbols may not want this, though, |
| 962 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 963 | // as otherwise we won't necessarilly have seen everything yet. |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 964 | if (!NoFinalize) { |
| 965 | if (MAI.hasSubsectionsViaSymbols()) { |
| 966 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 967 | MCSymbol *Sym = TableEntry.getValue(); |
| 968 | // Variable symbols may not be marked as defined, so check those |
| 969 | // explicitly. If we know it's a variable, we have a definition for |
| 970 | // the purposes of this check. |
| 971 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 972 | // FIXME: We would really like to refer back to where the symbol was |
| 973 | // first referenced for a source location. We need to add something |
| 974 | // 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] | 975 | printError(getTok().getLoc(), "assembler local symbol '" + |
| 976 | Sym->getName() + "' not defined"); |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | // Temporary symbols like the ones for directional jumps don't go in the |
| 981 | // symbol table. They also need to be diagnosed in all (final) cases. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 982 | for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
| 983 | if (std::get<2>(LocSym)->isUndefined()) { |
| 984 | // Reset the state of any "# line file" directives we've seen to the |
| 985 | // context as it was at the diagnostic site. |
| 986 | CppHashInfo = std::get<1>(LocSym); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 987 | printError(std::get<0>(LocSym), "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 988 | } |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 992 | // Finalize the output stream if there are no errors and if the client wants |
| 993 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 994 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 995 | Out.Finish(); |
| 996 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 997 | return HadError || getContext().hadError(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1000 | bool AsmParser::checkForValidSection() { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 1001 | if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 1002 | Out.InitSections(false); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1003 | return Error(getTok().getLoc(), |
| 1004 | "expected section directive before assembly directive"); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1005 | } |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1006 | return false; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1009 | /// Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1010 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1011 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 1012 | Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1014 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1015 | if (Lexer.is(AsmToken::EndOfStatement)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 1016 | Lexer.Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1019 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 1020 | const char *Start = getTok().getLoc().getPointer(); |
| 1021 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1022 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1023 | Lexer.Lex(); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 1024 | |
| 1025 | const char *End = getTok().getLoc().getPointer(); |
| 1026 | return StringRef(Start, End - Start); |
| 1027 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1028 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1029 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 1030 | const char *Start = getTok().getLoc().getPointer(); |
| 1031 | |
| 1032 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1033 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1034 | Lexer.Lex(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 1035 | |
| 1036 | const char *End = getTok().getLoc().getPointer(); |
| 1037 | return StringRef(Start, End - Start); |
| 1038 | } |
| 1039 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1040 | /// Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1041 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 1042 | /// |
| 1043 | /// parenexpr ::= expr) |
| 1044 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1045 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1046 | if (parseExpression(Res)) |
| 1047 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1048 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1049 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1050 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1051 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1054 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1055 | /// Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1056 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 1057 | /// |
| 1058 | /// bracketexpr ::= expr] |
| 1059 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1060 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1061 | if (parseExpression(Res)) |
| 1062 | return true; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1063 | EndLoc = getTok().getEndLoc(); |
| 1064 | if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) |
| 1065 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1066 | return false; |
| 1067 | } |
| 1068 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1069 | /// Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1070 | /// primaryexpr ::= (parenexpr |
| 1071 | /// primaryexpr ::= symbol |
| 1072 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1073 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1074 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1075 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1076 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 1077 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 1078 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1079 | default: |
| 1080 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 1081 | // If we have an error assume that we've already handled it. |
| 1082 | case AsmToken::Error: |
| 1083 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1084 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1085 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1086 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1087 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1088 | Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1089 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1090 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1091 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1092 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1093 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1094 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1095 | if (parseIdentifier(Identifier)) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1096 | // We may have failed but $ may be a valid token. |
| 1097 | if (getTok().is(AsmToken::Dollar)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1098 | if (Lexer.getMAI().getDollarIsPC()) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1099 | Lex(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1100 | // This is a '$' reference, which references the current PC. Emit a |
| 1101 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1102 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1103 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1104 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 1105 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1106 | EndLoc = FirstTokenLoc; |
| 1107 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 1108 | } |
| 1109 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1110 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1111 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1112 | // Parse symbol variant |
| 1113 | std::pair<StringRef, StringRef> Split; |
| 1114 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1115 | if (FirstTokenKind == AsmToken::String) { |
| 1116 | if (Lexer.is(AsmToken::At)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1117 | Lex(); // eat @ |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1118 | SMLoc AtLoc = getLexer().getLoc(); |
| 1119 | StringRef VName; |
| 1120 | if (parseIdentifier(VName)) |
| 1121 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 1122 | |
| 1123 | Split = std::make_pair(Identifier, VName); |
| 1124 | } |
| 1125 | } else { |
| 1126 | Split = Identifier.split('@'); |
| 1127 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1128 | } else if (Lexer.is(AsmToken::LParen)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1129 | Lex(); // eat '('. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1130 | StringRef VName; |
| 1131 | parseIdentifier(VName); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1132 | // eat ')'. |
| 1133 | if (parseToken(AsmToken::RParen, |
| 1134 | "unexpected token in variant, expected ')'")) |
| 1135 | return true; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1136 | Split = std::make_pair(Identifier, VName); |
| 1137 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1138 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1139 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 1140 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1141 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1142 | StringRef SymbolName = Identifier; |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1143 | if (SymbolName.empty()) |
Francis Visoiu Mistrih | 627d146 | 2018-10-08 10:28:11 +0000 | [diff] [blame] | 1144 | return Error(getLexer().getLoc(), "expected a symbol reference"); |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1145 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1146 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 1147 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1148 | // Lookup the symbol variant if used. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1149 | if (!Split.second.empty()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1150 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1151 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 1152 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1153 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1154 | Variant = MCSymbolRefExpr::VK_None; |
| 1155 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 1156 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 1157 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1160 | |
Bill Wendling | 1b10438 | 2019-08-09 20:16:31 +0000 | [diff] [blame] | 1161 | MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName); |
| 1162 | if (!Sym) |
| 1163 | Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1164 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1165 | // If this is an absolute variable reference, substitute it now to preserve |
| 1166 | // semantics in the face of reassignment. |
Nirav Dave | 05b5891 | 2018-06-05 15:13:39 +0000 | [diff] [blame] | 1167 | if (Sym->isVariable()) { |
| 1168 | auto V = Sym->getVariableValue(/*SetUsed*/ false); |
Nirav Dave | b1f9e4d | 2018-09-17 20:34:26 +0000 | [diff] [blame] | 1169 | bool DoInline = isa<MCConstantExpr>(V) && !Variant; |
Nirav Dave | 05b5891 | 2018-06-05 15:13:39 +0000 | [diff] [blame] | 1170 | if (auto TV = dyn_cast<MCTargetExpr>(V)) |
| 1171 | DoInline = TV->inlineAssignedExpr(); |
| 1172 | if (DoInline) { |
| 1173 | if (Variant) |
| 1174 | return Error(EndLoc, "unexpected modifier on variable reference"); |
| 1175 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
| 1176 | return false; |
| 1177 | } |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | // Otherwise create a symbol ref. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1181 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1182 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1183 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 1184 | case AsmToken::BigNum: |
| 1185 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1186 | case AsmToken::Integer: { |
| 1187 | SMLoc Loc = getTok().getLoc(); |
| 1188 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1189 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1190 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1191 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1192 | // Look for 'b' or 'f' following an Integer as a directional label |
| 1193 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 1194 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1195 | // Lookup the symbol variant if used. |
| 1196 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 1197 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 1198 | if (Split.first.size() != IDVal.size()) { |
| 1199 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1200 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1201 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1202 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1203 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1204 | if (IDVal == "f" || IDVal == "b") { |
| 1205 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1206 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1207 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 1208 | if (IDVal == "b" && Sym->isUndefined()) |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 1209 | return Error(Loc, "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 1210 | DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1211 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1212 | Lex(); // Eat identifier. |
| 1213 | } |
| 1214 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1215 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1216 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1217 | case AsmToken::Real: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1218 | APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 1219 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1220 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1221 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1222 | Lex(); // Eat token. |
| 1223 | return false; |
| 1224 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1225 | case AsmToken::Dot: { |
| 1226 | // This is a '.' reference, which references the current PC. Emit a |
| 1227 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1228 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1229 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1230 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1231 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1232 | Lex(); // Eat identifier. |
| 1233 | return false; |
| 1234 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1235 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1236 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1237 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1238 | case AsmToken::LBrac: |
| 1239 | if (!PlatformParser->HasBracketExpressions()) |
| 1240 | return TokError("brackets expression not supported on this target"); |
| 1241 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1242 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1243 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1244 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1245 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1246 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1247 | Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1248 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1249 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1250 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1251 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1252 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1253 | Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1254 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1255 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1256 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1257 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1258 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1259 | Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1260 | return false; |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 1261 | // MIPS unary expression operators. The lexer won't generate these tokens if |
| 1262 | // MCAsmInfo::HasMipsExpressions is false for the target. |
| 1263 | case AsmToken::PercentCall16: |
| 1264 | case AsmToken::PercentCall_Hi: |
| 1265 | case AsmToken::PercentCall_Lo: |
| 1266 | case AsmToken::PercentDtprel_Hi: |
| 1267 | case AsmToken::PercentDtprel_Lo: |
| 1268 | case AsmToken::PercentGot: |
| 1269 | case AsmToken::PercentGot_Disp: |
| 1270 | case AsmToken::PercentGot_Hi: |
| 1271 | case AsmToken::PercentGot_Lo: |
| 1272 | case AsmToken::PercentGot_Ofst: |
| 1273 | case AsmToken::PercentGot_Page: |
| 1274 | case AsmToken::PercentGottprel: |
| 1275 | case AsmToken::PercentGp_Rel: |
| 1276 | case AsmToken::PercentHi: |
| 1277 | case AsmToken::PercentHigher: |
| 1278 | case AsmToken::PercentHighest: |
| 1279 | case AsmToken::PercentLo: |
| 1280 | case AsmToken::PercentNeg: |
| 1281 | case AsmToken::PercentPcrel_Hi: |
| 1282 | case AsmToken::PercentPcrel_Lo: |
| 1283 | case AsmToken::PercentTlsgd: |
| 1284 | case AsmToken::PercentTlsldm: |
| 1285 | case AsmToken::PercentTprel_Hi: |
| 1286 | case AsmToken::PercentTprel_Lo: |
| 1287 | Lex(); // Eat the operator. |
| 1288 | if (Lexer.isNot(AsmToken::LParen)) |
| 1289 | return TokError("expected '(' after operator"); |
| 1290 | Lex(); // Eat the operator. |
| 1291 | if (parseExpression(Res, EndLoc)) |
| 1292 | return true; |
| 1293 | if (Lexer.isNot(AsmToken::RParen)) |
| 1294 | return TokError("expected ')'"); |
| 1295 | Lex(); // Eat the operator. |
| 1296 | Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); |
| 1297 | return !Res; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1300 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1301 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 1302 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1303 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1306 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1307 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1308 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 1309 | // Ask the target implementation about this expression first. |
| 1310 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 1311 | if (NewE) |
| 1312 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1313 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 1314 | // if there is exactly one symbol. |
| 1315 | switch (E->getKind()) { |
| 1316 | case MCExpr::Target: |
| 1317 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1318 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1319 | |
| 1320 | case MCExpr::SymbolRef: { |
| 1321 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 1322 | |
| 1323 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1324 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 1325 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1326 | return E; |
| 1327 | } |
| 1328 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1329 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | case MCExpr::Unary: { |
| 1333 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1334 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1335 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1336 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1337 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | case MCExpr::Binary: { |
| 1341 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1342 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1343 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1344 | |
| 1345 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1346 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1347 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1348 | if (!LHS) |
| 1349 | LHS = BE->getLHS(); |
| 1350 | if (!RHS) |
| 1351 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1352 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1353 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1356 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1357 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1360 | /// This function checks if the next token is <string> type or arithmetic. |
| 1361 | /// string that begin with character '<' must end with character '>'. |
| 1362 | /// otherwise it is arithmetics. |
| 1363 | /// If the function returns a 'true' value, |
| 1364 | /// the End argument will be filled with the last location pointed to the '>' |
| 1365 | /// character. |
| 1366 | |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 1367 | /// There is a gap between the AltMacro's documentation and the single quote |
| 1368 | /// implementation. GCC does not fully support this feature and so we will not |
| 1369 | /// support it. |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1370 | /// TODO: Adding single quote as a string. |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1371 | static bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { |
Craig Topper | c06ee83 | 2018-09-25 18:33:00 +0000 | [diff] [blame] | 1372 | assert((StrLoc.getPointer() != nullptr) && |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1373 | "Argument to the function cannot be a NULL value"); |
| 1374 | const char *CharPtr = StrLoc.getPointer(); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 1375 | while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && |
| 1376 | (*CharPtr != '\0')) { |
| 1377 | if (*CharPtr == '!') |
| 1378 | CharPtr++; |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1379 | CharPtr++; |
| 1380 | } |
| 1381 | if (*CharPtr == '>') { |
| 1382 | EndLoc = StrLoc.getFromPointer(CharPtr + 1); |
| 1383 | return true; |
| 1384 | } |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1388 | /// creating a string without the escape characters '!'. |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1389 | static std::string altMacroString(StringRef AltMacroStr) { |
| 1390 | std::string Res; |
Michael Zuckerman | ff29879 | 2017-05-10 14:00:57 +0000 | [diff] [blame] | 1391 | for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1392 | if (AltMacroStr[Pos] == '!') |
| 1393 | Pos++; |
| 1394 | Res += AltMacroStr[Pos]; |
| 1395 | } |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1396 | return Res; |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1399 | /// Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1400 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1401 | /// expr ::= expr &&,|| expr -> lowest. |
| 1402 | /// expr ::= expr |,^,&,! expr |
| 1403 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1404 | /// expr ::= expr <<,>> expr |
| 1405 | /// expr ::= expr +,- expr |
| 1406 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1407 | /// expr ::= primaryexpr |
| 1408 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1409 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1410 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1411 | Res = nullptr; |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1412 | if (getTargetParser().parsePrimaryExpr(Res, EndLoc) || |
| 1413 | parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1414 | return true; |
| 1415 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1416 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1417 | // expression to include the modifier. This is inefficient, but in general we |
| 1418 | // expect users to use 'a@modifier op b'. |
| 1419 | if (Lexer.getKind() == AsmToken::At) { |
| 1420 | Lex(); |
| 1421 | |
| 1422 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1423 | return TokError("unexpected symbol modifier following '@'"); |
| 1424 | |
| 1425 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1426 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1427 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1428 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1429 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1430 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1431 | if (!ModifiedRes) { |
| 1432 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1433 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1434 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1435 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1436 | Res = ModifiedRes; |
| 1437 | Lex(); |
| 1438 | } |
| 1439 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 1440 | // Try to constant fold it up front, if possible. Do not exploit |
| 1441 | // assembler here. |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1442 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1443 | if (Res->evaluateAsAbsolute(Value)) |
| 1444 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1445 | |
| 1446 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1447 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1448 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1449 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1450 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1451 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1454 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1455 | SMLoc &EndLoc) { |
| 1456 | if (parseParenExpr(Res, EndLoc)) |
| 1457 | return true; |
| 1458 | |
| 1459 | for (; ParenDepth > 0; --ParenDepth) { |
| 1460 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1461 | return true; |
| 1462 | |
| 1463 | // We don't Lex() the last RParen. |
| 1464 | // This is the same behavior as parseParenExpression(). |
| 1465 | if (ParenDepth - 1 > 0) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1466 | EndLoc = getTok().getEndLoc(); |
| 1467 | if (parseToken(AsmToken::RParen, |
| 1468 | "expected ')' in parentheses expression")) |
| 1469 | return true; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1475 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1476 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1477 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1478 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1479 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 1482 | if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1483 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1484 | |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1488 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1489 | MCBinaryExpr::Opcode &Kind, |
| 1490 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1491 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1492 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1493 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1494 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1495 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1496 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1497 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1498 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1499 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1500 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1501 | return 1; |
| 1502 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1503 | // Low Precedence: |, &, ^ |
| 1504 | // |
| 1505 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1506 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1507 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1508 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1509 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1510 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1511 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1512 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1513 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1514 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1515 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1516 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1517 | case AsmToken::EqualEqual: |
| 1518 | Kind = MCBinaryExpr::EQ; |
| 1519 | return 3; |
| 1520 | case AsmToken::ExclaimEqual: |
| 1521 | case AsmToken::LessGreater: |
| 1522 | Kind = MCBinaryExpr::NE; |
| 1523 | return 3; |
| 1524 | case AsmToken::Less: |
| 1525 | Kind = MCBinaryExpr::LT; |
| 1526 | return 3; |
| 1527 | case AsmToken::LessEqual: |
| 1528 | Kind = MCBinaryExpr::LTE; |
| 1529 | return 3; |
| 1530 | case AsmToken::Greater: |
| 1531 | Kind = MCBinaryExpr::GT; |
| 1532 | return 3; |
| 1533 | case AsmToken::GreaterEqual: |
| 1534 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1535 | return 3; |
| 1536 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1537 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1538 | case AsmToken::LessLess: |
| 1539 | Kind = MCBinaryExpr::Shl; |
| 1540 | return 4; |
| 1541 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1542 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1543 | return 4; |
| 1544 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1545 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1546 | case AsmToken::Plus: |
| 1547 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1548 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1549 | case AsmToken::Minus: |
| 1550 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1551 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1552 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1553 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1554 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1555 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1556 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1557 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1558 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1559 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1560 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1561 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1562 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1566 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1567 | MCBinaryExpr::Opcode &Kind, |
| 1568 | bool ShouldUseLogicalShr) { |
| 1569 | switch (K) { |
| 1570 | default: |
| 1571 | return 0; // not a binop. |
| 1572 | |
| 1573 | // Lowest Precedence: &&, || |
| 1574 | case AsmToken::AmpAmp: |
| 1575 | Kind = MCBinaryExpr::LAnd; |
| 1576 | return 2; |
| 1577 | case AsmToken::PipePipe: |
| 1578 | Kind = MCBinaryExpr::LOr; |
| 1579 | return 1; |
| 1580 | |
| 1581 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1582 | case AsmToken::EqualEqual: |
| 1583 | Kind = MCBinaryExpr::EQ; |
| 1584 | return 3; |
| 1585 | case AsmToken::ExclaimEqual: |
| 1586 | case AsmToken::LessGreater: |
| 1587 | Kind = MCBinaryExpr::NE; |
| 1588 | return 3; |
| 1589 | case AsmToken::Less: |
| 1590 | Kind = MCBinaryExpr::LT; |
| 1591 | return 3; |
| 1592 | case AsmToken::LessEqual: |
| 1593 | Kind = MCBinaryExpr::LTE; |
| 1594 | return 3; |
| 1595 | case AsmToken::Greater: |
| 1596 | Kind = MCBinaryExpr::GT; |
| 1597 | return 3; |
| 1598 | case AsmToken::GreaterEqual: |
| 1599 | Kind = MCBinaryExpr::GTE; |
| 1600 | return 3; |
| 1601 | |
| 1602 | // Low Intermediate Precedence: +, - |
| 1603 | case AsmToken::Plus: |
| 1604 | Kind = MCBinaryExpr::Add; |
| 1605 | return 4; |
| 1606 | case AsmToken::Minus: |
| 1607 | Kind = MCBinaryExpr::Sub; |
| 1608 | return 4; |
| 1609 | |
| 1610 | // High Intermediate Precedence: |, &, ^ |
| 1611 | // |
| 1612 | // FIXME: gas seems to support '!' as an infix operator? |
| 1613 | case AsmToken::Pipe: |
| 1614 | Kind = MCBinaryExpr::Or; |
| 1615 | return 5; |
| 1616 | case AsmToken::Caret: |
| 1617 | Kind = MCBinaryExpr::Xor; |
| 1618 | return 5; |
| 1619 | case AsmToken::Amp: |
| 1620 | Kind = MCBinaryExpr::And; |
| 1621 | return 5; |
| 1622 | |
| 1623 | // Highest Precedence: *, /, %, <<, >> |
| 1624 | case AsmToken::Star: |
| 1625 | Kind = MCBinaryExpr::Mul; |
| 1626 | return 6; |
| 1627 | case AsmToken::Slash: |
| 1628 | Kind = MCBinaryExpr::Div; |
| 1629 | return 6; |
| 1630 | case AsmToken::Percent: |
| 1631 | Kind = MCBinaryExpr::Mod; |
| 1632 | return 6; |
| 1633 | case AsmToken::LessLess: |
| 1634 | Kind = MCBinaryExpr::Shl; |
| 1635 | return 6; |
| 1636 | case AsmToken::GreaterGreater: |
| 1637 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1638 | return 6; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1643 | MCBinaryExpr::Opcode &Kind) { |
| 1644 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1645 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1646 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1647 | } |
| 1648 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1649 | /// Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1650 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1651 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1652 | SMLoc &EndLoc) { |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1653 | SMLoc StartLoc = Lexer.getLoc(); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 1654 | while (true) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1655 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1656 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1657 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1658 | // If the next token is lower precedence than we are allowed to eat, return |
| 1659 | // successfully with what we ate already. |
| 1660 | if (TokPrec < Precedence) |
| 1661 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1662 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1663 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1664 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1665 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1666 | const MCExpr *RHS; |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1667 | if (getTargetParser().parsePrimaryExpr(RHS, EndLoc)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1668 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1670 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1671 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1672 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1673 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1674 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1675 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1676 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1677 | // Merge LHS and RHS according to operator. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1678 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1682 | /// ParseStatement: |
| 1683 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1684 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1685 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1686 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1687 | MCAsmParserSemaCallback *SI) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1688 | assert(!hasPendingError() && "parseStatement started with pending error"); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1689 | // Eat initial spaces and comments |
| 1690 | while (Lexer.is(AsmToken::Space)) |
| 1691 | Lex(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1692 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1693 | // if this is a line comment we can drop it safely |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 1694 | if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1695 | getTok().getString().front() == '\n') |
| 1696 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1697 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1698 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1699 | } |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1700 | // Statements always start with an identifier. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1701 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1702 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1703 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1704 | int64_t LocalLabelVal = -1; |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1705 | if (Lexer.is(AsmToken::HashDirective)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1706 | return parseCppHashLineFilenameComment(IDLoc); |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1707 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1708 | if (Lexer.is(AsmToken::Integer)) { |
| 1709 | LocalLabelVal = getTok().getIntVal(); |
| 1710 | if (LocalLabelVal < 0) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1711 | if (!TheCondState.Ignore) { |
| 1712 | Lex(); // always eat a token |
| 1713 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1714 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1715 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1716 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1717 | IDVal = getTok().getString(); |
| 1718 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1719 | if (Lexer.getKind() != AsmToken::Colon) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1720 | if (!TheCondState.Ignore) { |
| 1721 | Lex(); // always eat a token |
| 1722 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1723 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1724 | } |
| 1725 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1726 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1727 | // Treat '.' as a valid identifier in this context. |
| 1728 | Lex(); |
| 1729 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1730 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1731 | // Treat '{' as a valid identifier in this context. |
| 1732 | Lex(); |
| 1733 | IDVal = "{"; |
| 1734 | |
| 1735 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1736 | // Treat '}' as a valid identifier in this context. |
| 1737 | Lex(); |
| 1738 | IDVal = "}"; |
Yonghong Song | 06ff655 | 2017-09-12 17:55:23 +0000 | [diff] [blame] | 1739 | } else if (Lexer.is(AsmToken::Star) && |
| 1740 | getTargetParser().starIsStartOfStatement()) { |
| 1741 | // Accept '*' as a valid start of statement. |
| 1742 | Lex(); |
| 1743 | IDVal = "*"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1744 | } else if (parseIdentifier(IDVal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1745 | if (!TheCondState.Ignore) { |
| 1746 | Lex(); // always eat a token |
| 1747 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1748 | } |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1749 | IDVal = ""; |
| 1750 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1752 | // Handle conditional assembly here before checking for skipping. We |
| 1753 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1754 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1755 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1756 | DirectiveKindMap.find(IDVal); |
| 1757 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 1758 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1759 | ? DK_NO_DIRECTIVE |
| 1760 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1761 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1762 | default: |
| 1763 | break; |
| 1764 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1765 | case DK_IFEQ: |
| 1766 | case DK_IFGE: |
| 1767 | case DK_IFGT: |
| 1768 | case DK_IFLE: |
| 1769 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1770 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1771 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1772 | case DK_IFB: |
| 1773 | return parseDirectiveIfb(IDLoc, true); |
| 1774 | case DK_IFNB: |
| 1775 | return parseDirectiveIfb(IDLoc, false); |
| 1776 | case DK_IFC: |
| 1777 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1778 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1779 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1780 | case DK_IFNC: |
| 1781 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1782 | case DK_IFNES: |
| 1783 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1784 | case DK_IFDEF: |
| 1785 | return parseDirectiveIfdef(IDLoc, true); |
| 1786 | case DK_IFNDEF: |
| 1787 | case DK_IFNOTDEF: |
| 1788 | return parseDirectiveIfdef(IDLoc, false); |
| 1789 | case DK_ELSEIF: |
| 1790 | return parseDirectiveElseIf(IDLoc); |
| 1791 | case DK_ELSE: |
| 1792 | return parseDirectiveElse(IDLoc); |
| 1793 | case DK_ENDIF: |
| 1794 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1795 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1796 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1797 | // Ignore the statement if in the middle of inactive conditional |
| 1798 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1799 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1800 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1803 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1804 | // FIXME: Recurse on local labels? |
| 1805 | |
| 1806 | // See what kind of statement we have. |
| 1807 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1808 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1809 | if (!getTargetParser().isLabel(ID)) |
| 1810 | break; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1811 | if (checkForValidSection()) |
| 1812 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1813 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1814 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1815 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1816 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1817 | // Diagnose attempt to use '.' as a label. |
| 1818 | if (IDVal == ".") |
| 1819 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1820 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1821 | // Diagnose attempt to use a variable as a label. |
| 1822 | // |
| 1823 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1824 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1825 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1826 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1827 | if (LocalLabelVal == -1) { |
| 1828 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1829 | StringRef RewrittenLabel = |
| 1830 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1831 | assert(!RewrittenLabel.empty() && |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1832 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1833 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1834 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1835 | IDVal = RewrittenLabel; |
| 1836 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1837 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1838 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1839 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
Nirav Dave | 9263ae3 | 2016-08-02 19:17:54 +0000 | [diff] [blame] | 1840 | // End of Labels should be treated as end of line for lexing |
| 1841 | // purposes but that information is not available to the Lexer who |
| 1842 | // does not understand Labels. This may cause us to see a Hash |
| 1843 | // here instead of a preprocessor line comment. |
| 1844 | if (getTok().is(AsmToken::Hash)) { |
| 1845 | StringRef CommentStr = parseStringToEndOfStatement(); |
| 1846 | Lexer.Lex(); |
| 1847 | Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); |
| 1848 | } |
| 1849 | |
Nirav Dave | 8ea792d | 2016-07-13 14:03:12 +0000 | [diff] [blame] | 1850 | // Consume any end of statement token, if present, to avoid spurious |
| 1851 | // AddBlankLine calls(). |
| 1852 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 1853 | Lex(); |
| 1854 | } |
| 1855 | |
Maya Madhavan | ec1efe4 | 2018-09-20 05:11:42 +0000 | [diff] [blame] | 1856 | getTargetParser().doBeforeLabelEmit(Sym); |
| 1857 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1858 | // Emit the label. |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 1859 | if (!getTargetParser().isParsingInlineAsm()) |
Rafael Espindola | be99157 | 2017-02-10 15:13:12 +0000 | [diff] [blame] | 1860 | Out.EmitLabel(Sym, IDLoc); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1861 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1862 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1863 | // info to make a dwarf label entry for this label if needed. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 1864 | if (enabledGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1865 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1866 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1867 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1868 | getTargetParser().onLabelParsed(Sym); |
| 1869 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1870 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1871 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1872 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1873 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1874 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1875 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1876 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1877 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1878 | |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1879 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1880 | |
| 1881 | default: // Normal instruction or directive. |
| 1882 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1883 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1884 | |
| 1885 | // 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] | 1886 | if (areMacrosEnabled()) |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 1887 | if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1888 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1889 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1890 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1891 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1892 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1893 | // Directives start with "." |
Ana Pazos | 353f67a | 2018-08-25 01:34:32 +0000 | [diff] [blame] | 1894 | if (IDVal.startswith(".") && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1895 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1896 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1897 | // 1. The target-specific assembly parser. Some directives are target |
| 1898 | // specific or may potentially behave differently on certain targets. |
| 1899 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1900 | // (like the ELF parser) register themselves as extensions. |
| 1901 | // 3. The generic directive parser implemented by this class. These are |
| 1902 | // all the directives that behave in a target and platform independent |
| 1903 | // manner, or at least have a default behavior that's shared between |
| 1904 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1905 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 1906 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 1907 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1908 | SMLoc StartTokLoc = getTok().getLoc(); |
| 1909 | bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); |
| 1910 | |
| 1911 | if (hasPendingError()) |
| 1912 | return true; |
| 1913 | // Currently the return value should be true if we are |
| 1914 | // uninterested but as this is at odds with the standard parsing |
| 1915 | // convention (return true = error) we have instances of a parsed |
| 1916 | // directive that fails returning true as an error. Catch these |
| 1917 | // cases as best as possible errors here. |
| 1918 | if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) |
| 1919 | return true; |
| 1920 | // Return if we did some parsing or believe we succeeded. |
| 1921 | if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1922 | return false; |
| 1923 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1924 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1925 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1926 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1927 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1928 | if (Handler.first) |
| 1929 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1930 | |
| 1931 | // Finally, if no one else is interested in this directive, it must be |
| 1932 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1933 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1934 | default: |
| 1935 | break; |
| 1936 | case DK_SET: |
| 1937 | case DK_EQU: |
| 1938 | return parseDirectiveSet(IDVal, true); |
| 1939 | case DK_EQUIV: |
| 1940 | return parseDirectiveSet(IDVal, false); |
| 1941 | case DK_ASCII: |
| 1942 | return parseDirectiveAscii(IDVal, false); |
| 1943 | case DK_ASCIZ: |
| 1944 | case DK_STRING: |
| 1945 | return parseDirectiveAscii(IDVal, true); |
| 1946 | case DK_BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1947 | case DK_DC_B: |
| 1948 | return parseDirectiveValue(IDVal, 1); |
| 1949 | case DK_DC: |
| 1950 | case DK_DC_W: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1951 | case DK_SHORT: |
| 1952 | case DK_VALUE: |
| 1953 | case DK_2BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1954 | return parseDirectiveValue(IDVal, 2); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1955 | case DK_LONG: |
| 1956 | case DK_INT: |
| 1957 | case DK_4BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1958 | case DK_DC_L: |
| 1959 | return parseDirectiveValue(IDVal, 4); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1960 | case DK_QUAD: |
| 1961 | case DK_8BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1962 | return parseDirectiveValue(IDVal, 8); |
| 1963 | case DK_DC_A: |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1964 | return parseDirectiveValue( |
| 1965 | IDVal, getContext().getAsmInfo()->getCodePointerSize()); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1966 | case DK_OCTA: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1967 | return parseDirectiveOctaValue(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1968 | case DK_SINGLE: |
| 1969 | case DK_FLOAT: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1970 | case DK_DC_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1971 | return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1972 | case DK_DOUBLE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1973 | case DK_DC_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1974 | return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1975 | case DK_ALIGN: { |
| 1976 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1977 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1978 | } |
| 1979 | case DK_ALIGN32: { |
| 1980 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1981 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1982 | } |
| 1983 | case DK_BALIGN: |
| 1984 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1985 | case DK_BALIGNW: |
| 1986 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1987 | case DK_BALIGNL: |
| 1988 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1989 | case DK_P2ALIGN: |
| 1990 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1991 | case DK_P2ALIGNW: |
| 1992 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1993 | case DK_P2ALIGNL: |
| 1994 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1995 | case DK_ORG: |
| 1996 | return parseDirectiveOrg(); |
| 1997 | case DK_FILL: |
| 1998 | return parseDirectiveFill(); |
| 1999 | case DK_ZERO: |
| 2000 | return parseDirectiveZero(); |
| 2001 | case DK_EXTERN: |
| 2002 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 2003 | return false; |
| 2004 | case DK_GLOBL: |
| 2005 | case DK_GLOBAL: |
| 2006 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 2007 | case DK_LAZY_REFERENCE: |
| 2008 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 2009 | case DK_NO_DEAD_STRIP: |
| 2010 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 2011 | case DK_SYMBOL_RESOLVER: |
| 2012 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 2013 | case DK_PRIVATE_EXTERN: |
| 2014 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 2015 | case DK_REFERENCE: |
| 2016 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 2017 | case DK_WEAK_DEFINITION: |
| 2018 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 2019 | case DK_WEAK_REFERENCE: |
| 2020 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 2021 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 2022 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
Vedant Kumar | 13ef84f | 2019-01-25 18:30:22 +0000 | [diff] [blame] | 2023 | case DK_COLD: |
| 2024 | return parseDirectiveSymbolAttribute(MCSA_Cold); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2025 | case DK_COMM: |
| 2026 | case DK_COMMON: |
| 2027 | return parseDirectiveComm(/*IsLocal=*/false); |
| 2028 | case DK_LCOMM: |
| 2029 | return parseDirectiveComm(/*IsLocal=*/true); |
| 2030 | case DK_ABORT: |
| 2031 | return parseDirectiveAbort(); |
| 2032 | case DK_INCLUDE: |
| 2033 | return parseDirectiveInclude(); |
| 2034 | case DK_INCBIN: |
| 2035 | return parseDirectiveIncbin(); |
| 2036 | case DK_CODE16: |
| 2037 | case DK_CODE16GCC: |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2038 | return TokError(Twine(IDVal) + |
| 2039 | " not currently supported for this target"); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2040 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 2041 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2042 | case DK_IRP: |
| 2043 | return parseDirectiveIrp(IDLoc); |
| 2044 | case DK_IRPC: |
| 2045 | return parseDirectiveIrpc(IDLoc); |
| 2046 | case DK_ENDR: |
| 2047 | return parseDirectiveEndr(IDLoc); |
| 2048 | case DK_BUNDLE_ALIGN_MODE: |
| 2049 | return parseDirectiveBundleAlignMode(); |
| 2050 | case DK_BUNDLE_LOCK: |
| 2051 | return parseDirectiveBundleLock(); |
| 2052 | case DK_BUNDLE_UNLOCK: |
| 2053 | return parseDirectiveBundleUnlock(); |
| 2054 | case DK_SLEB128: |
| 2055 | return parseDirectiveLEB128(true); |
| 2056 | case DK_ULEB128: |
| 2057 | return parseDirectiveLEB128(false); |
| 2058 | case DK_SPACE: |
| 2059 | case DK_SKIP: |
| 2060 | return parseDirectiveSpace(IDVal); |
| 2061 | case DK_FILE: |
| 2062 | return parseDirectiveFile(IDLoc); |
| 2063 | case DK_LINE: |
| 2064 | return parseDirectiveLine(); |
| 2065 | case DK_LOC: |
| 2066 | return parseDirectiveLoc(); |
| 2067 | case DK_STABS: |
| 2068 | return parseDirectiveStabs(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2069 | case DK_CV_FILE: |
| 2070 | return parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 2071 | case DK_CV_FUNC_ID: |
| 2072 | return parseDirectiveCVFuncId(); |
| 2073 | case DK_CV_INLINE_SITE_ID: |
| 2074 | return parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2075 | case DK_CV_LOC: |
| 2076 | return parseDirectiveCVLoc(); |
| 2077 | case DK_CV_LINETABLE: |
| 2078 | return parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 2079 | case DK_CV_INLINE_LINETABLE: |
| 2080 | return parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 2081 | case DK_CV_DEF_RANGE: |
| 2082 | return parseDirectiveCVDefRange(); |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 2083 | case DK_CV_STRING: |
| 2084 | return parseDirectiveCVString(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2085 | case DK_CV_STRINGTABLE: |
| 2086 | return parseDirectiveCVStringTable(); |
| 2087 | case DK_CV_FILECHECKSUMS: |
| 2088 | return parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 2089 | case DK_CV_FILECHECKSUM_OFFSET: |
| 2090 | return parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 2091 | case DK_CV_FPO_DATA: |
| 2092 | return parseDirectiveCVFPOData(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2093 | case DK_CFI_SECTIONS: |
| 2094 | return parseDirectiveCFISections(); |
| 2095 | case DK_CFI_STARTPROC: |
| 2096 | return parseDirectiveCFIStartProc(); |
| 2097 | case DK_CFI_ENDPROC: |
| 2098 | return parseDirectiveCFIEndProc(); |
| 2099 | case DK_CFI_DEF_CFA: |
| 2100 | return parseDirectiveCFIDefCfa(IDLoc); |
| 2101 | case DK_CFI_DEF_CFA_OFFSET: |
| 2102 | return parseDirectiveCFIDefCfaOffset(); |
| 2103 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 2104 | return parseDirectiveCFIAdjustCfaOffset(); |
| 2105 | case DK_CFI_DEF_CFA_REGISTER: |
| 2106 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 2107 | case DK_CFI_OFFSET: |
| 2108 | return parseDirectiveCFIOffset(IDLoc); |
| 2109 | case DK_CFI_REL_OFFSET: |
| 2110 | return parseDirectiveCFIRelOffset(IDLoc); |
| 2111 | case DK_CFI_PERSONALITY: |
| 2112 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 2113 | case DK_CFI_LSDA: |
| 2114 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 2115 | case DK_CFI_REMEMBER_STATE: |
| 2116 | return parseDirectiveCFIRememberState(); |
| 2117 | case DK_CFI_RESTORE_STATE: |
| 2118 | return parseDirectiveCFIRestoreState(); |
| 2119 | case DK_CFI_SAME_VALUE: |
| 2120 | return parseDirectiveCFISameValue(IDLoc); |
| 2121 | case DK_CFI_RESTORE: |
| 2122 | return parseDirectiveCFIRestore(IDLoc); |
| 2123 | case DK_CFI_ESCAPE: |
| 2124 | return parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 2125 | case DK_CFI_RETURN_COLUMN: |
| 2126 | return parseDirectiveCFIReturnColumn(IDLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2127 | case DK_CFI_SIGNAL_FRAME: |
| 2128 | return parseDirectiveCFISignalFrame(); |
| 2129 | case DK_CFI_UNDEFINED: |
| 2130 | return parseDirectiveCFIUndefined(IDLoc); |
| 2131 | case DK_CFI_REGISTER: |
| 2132 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 2133 | case DK_CFI_WINDOW_SAVE: |
| 2134 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2135 | case DK_MACROS_ON: |
| 2136 | case DK_MACROS_OFF: |
| 2137 | return parseDirectiveMacrosOnOff(IDVal); |
| 2138 | case DK_MACRO: |
| 2139 | return parseDirectiveMacro(IDLoc); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2140 | case DK_ALTMACRO: |
| 2141 | case DK_NOALTMACRO: |
| 2142 | return parseDirectiveAltmacro(IDVal); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2143 | case DK_EXITM: |
| 2144 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2145 | case DK_ENDM: |
| 2146 | case DK_ENDMACRO: |
| 2147 | return parseDirectiveEndMacro(IDVal); |
| 2148 | case DK_PURGEM: |
| 2149 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 2150 | case DK_END: |
| 2151 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 2152 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 2153 | return parseDirectiveError(IDLoc, false); |
| 2154 | case DK_ERROR: |
| 2155 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 2156 | case DK_WARNING: |
| 2157 | return parseDirectiveWarning(IDLoc); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2158 | case DK_RELOC: |
| 2159 | return parseDirectiveReloc(IDLoc); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2160 | case DK_DCB: |
| 2161 | case DK_DCB_W: |
| 2162 | return parseDirectiveDCB(IDVal, 2); |
| 2163 | case DK_DCB_B: |
| 2164 | return parseDirectiveDCB(IDVal, 1); |
| 2165 | case DK_DCB_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2166 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2167 | case DK_DCB_L: |
| 2168 | return parseDirectiveDCB(IDVal, 4); |
| 2169 | case DK_DCB_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2170 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2171 | case DK_DC_X: |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2172 | case DK_DCB_X: |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2173 | return TokError(Twine(IDVal) + |
| 2174 | " not currently supported for this target"); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 2175 | case DK_DS: |
| 2176 | case DK_DS_W: |
| 2177 | return parseDirectiveDS(IDVal, 2); |
| 2178 | case DK_DS_B: |
| 2179 | return parseDirectiveDS(IDVal, 1); |
| 2180 | case DK_DS_D: |
| 2181 | return parseDirectiveDS(IDVal, 8); |
| 2182 | case DK_DS_L: |
| 2183 | case DK_DS_S: |
| 2184 | return parseDirectiveDS(IDVal, 4); |
| 2185 | case DK_DS_P: |
| 2186 | case DK_DS_X: |
| 2187 | return parseDirectiveDS(IDVal, 12); |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 2188 | case DK_PRINT: |
| 2189 | return parseDirectivePrint(IDLoc); |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 2190 | case DK_ADDRSIG: |
| 2191 | return parseDirectiveAddrsig(); |
| 2192 | case DK_ADDRSIG_SYM: |
| 2193 | return parseDirectiveAddrsigSym(); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 2194 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2195 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 2196 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2197 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 2198 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2199 | // __asm _emit or __asm __emit |
| 2200 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 2201 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2202 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2203 | |
| 2204 | // __asm align |
| 2205 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2206 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2207 | |
Coby Tayree | 3847be9 | 2017-04-04 17:57:23 +0000 | [diff] [blame] | 2208 | if (ParsingInlineAsm && (IDVal == "even" || IDVal == "EVEN")) |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 2209 | Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2210 | if (checkForValidSection()) |
| 2211 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 2213 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2214 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 2215 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2216 | bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
| 2217 | Info.ParsedOperands); |
| 2218 | Info.ParseError = ParseHadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2219 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2220 | // Dump the parsed representation, if requested. |
| 2221 | if (getShowParsedOperands()) { |
| 2222 | SmallString<256> Str; |
| 2223 | raw_svector_ostream OS(Str); |
| 2224 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2225 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2226 | if (i != 0) |
| 2227 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2228 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2229 | } |
| 2230 | OS << "]"; |
| 2231 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2232 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2235 | // Fail even if ParseInstruction erroneously returns false. |
| 2236 | if (hasPendingError() || ParseHadError) |
| 2237 | return true; |
| 2238 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2239 | // If we are generating dwarf for the current section then generate a .loc |
| 2240 | // directive for the instruction. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 2241 | if (!ParseHadError && enabledGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2242 | getContext().getGenDwarfSectionSyms().count( |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 2243 | getStreamer().getCurrentSectionOnly())) { |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 2244 | unsigned Line; |
| 2245 | if (ActiveMacros.empty()) |
| 2246 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 2247 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 2248 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 2249 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2250 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2251 | // If we previously parsed a cpp hash file line comment then make sure the |
| 2252 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 2253 | // 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] | 2254 | if (!CppHashInfo.Filename.empty()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2255 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2256 | 0, StringRef(), CppHashInfo.Filename); |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2257 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2258 | |
Graydon Hoare | 54fe208 | 2018-04-07 00:44:02 +0000 | [diff] [blame] | 2259 | unsigned CppHashLocLineNo = |
| 2260 | SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2261 | Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2262 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2263 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2264 | getStreamer().EmitDwarfLocDirective( |
| 2265 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 2266 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 2267 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 2270 | // If parsing succeeded, match the instruction. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2271 | if (!ParseHadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 2272 | uint64_t ErrorInfo; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 2273 | if (getTargetParser().MatchAndEmitInstruction( |
| 2274 | IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| 2275 | getTargetParser().isParsingInlineAsm())) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2276 | return true; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 2277 | } |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 2278 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 2279 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 2280 | |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2281 | // Parse and erase curly braces marking block start/end |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame] | 2282 | bool |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2283 | AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { |
| 2284 | // Identify curly brace marking block start/end |
| 2285 | if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) |
| 2286 | return false; |
| 2287 | |
| 2288 | SMLoc StartLoc = Lexer.getLoc(); |
| 2289 | Lex(); // Eat the brace |
| 2290 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2291 | Lex(); // Eat EndOfStatement following the brace |
| 2292 | |
| 2293 | // Erase the block start/end brace from the output asm string |
| 2294 | AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - |
| 2295 | StartLoc.getPointer()); |
| 2296 | return true; |
| 2297 | } |
| 2298 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2299 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2300 | /// ::= # number "filename" |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2301 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2302 | Lex(); // Eat the hash token. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2303 | // Lexer only ever emits HashDirective if it fully formed if it's |
| 2304 | // done the checking already so this is an internal error. |
| 2305 | assert(getTok().is(AsmToken::Integer) && |
| 2306 | "Lexing Cpp line comment: Expected Integer"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2307 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2308 | Lex(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2309 | assert(getTok().is(AsmToken::String) && |
| 2310 | "Lexing Cpp line comment: Expected String"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2311 | StringRef Filename = getTok().getString(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2312 | Lex(); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2313 | |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2314 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2315 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2316 | |
Paul Robinson | 9c56326 | 2019-05-21 11:59:03 +0000 | [diff] [blame] | 2317 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics |
| 2318 | // and possibly DWARF file info. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2319 | CppHashInfo.Loc = L; |
| 2320 | CppHashInfo.Filename = Filename; |
| 2321 | CppHashInfo.LineNumber = LineNumber; |
| 2322 | CppHashInfo.Buf = CurBuffer; |
Paul Robinson | 9c56326 | 2019-05-21 11:59:03 +0000 | [diff] [blame] | 2323 | if (FirstCppHashFilename.empty()) |
| 2324 | FirstCppHashFilename = Filename; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2325 | return false; |
| 2326 | } |
| 2327 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2328 | /// will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2329 | /// for the Filename and LineNo if any in the diagnostic. |
| 2330 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2331 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2332 | raw_ostream &OS = errs(); |
| 2333 | |
| 2334 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2335 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2336 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2337 | unsigned CppHashBuf = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2338 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2339 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2340 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2341 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2342 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2343 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 2344 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2345 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 2346 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2349 | // 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] | 2350 | // manager changed or buffer changed (like in a nested include) then just |
| 2351 | // print the normal diagnostic using its Filename and LineNo. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2352 | if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2353 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2354 | if (Parser->SavedDiagHandler) |
| 2355 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 2356 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2357 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2358 | return; |
| 2359 | } |
| 2360 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2361 | // Use the CppHashFilename and calculate a line number based on the |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2362 | // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc |
| 2363 | // for the diagnostic. |
| 2364 | const std::string &Filename = Parser->CppHashInfo.Filename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2365 | |
| 2366 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 2367 | int CppHashLocLineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2368 | Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2369 | int LineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2370 | Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2371 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2372 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 2373 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 2374 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2375 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2376 | if (Parser->SavedDiagHandler) |
| 2377 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 2378 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2379 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2382 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 2383 | // difference being that that function accepts '@' as part of identifiers and |
| 2384 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 2385 | // '@' as a special case when needed. |
| 2386 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2387 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 2388 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2391 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 2392 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2393 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2394 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2395 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2396 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2397 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2398 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2399 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2400 | // A macro without parameters is handled differently on Darwin: |
| 2401 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2402 | while (!Body.empty()) { |
| 2403 | // Scan for the next substitution. |
| 2404 | std::size_t End = Body.size(), Pos = 0; |
| 2405 | for (; Pos != End; ++Pos) { |
| 2406 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2407 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2408 | // This macro has no parameters, look for $0, $1, etc. |
| 2409 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 2410 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2411 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2412 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2413 | if (Next == '$' || Next == 'n' || |
| 2414 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | } else { |
| 2417 | // This macro has parameters, look for \foo, \bar, etc. |
| 2418 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 2419 | break; |
| 2420 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | // Add the prefix. |
| 2424 | OS << Body.slice(0, Pos); |
| 2425 | |
| 2426 | // Check if we reached the end. |
| 2427 | if (Pos == End) |
| 2428 | break; |
| 2429 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2430 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2431 | switch (Body[Pos + 1]) { |
| 2432 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2433 | case '$': |
| 2434 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2435 | break; |
| 2436 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2437 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2438 | case 'n': |
| 2439 | OS << A.size(); |
| 2440 | break; |
| 2441 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2442 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2443 | default: { |
| 2444 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2445 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2446 | if (Index >= A.size()) |
| 2447 | break; |
| 2448 | |
| 2449 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2450 | for (const AsmToken &Token : A[Index]) |
| 2451 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | } |
| 2454 | } |
| 2455 | Pos += 2; |
| 2456 | } else { |
| 2457 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2458 | |
| 2459 | // Check for the \@ pseudo-variable. |
| 2460 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2461 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2462 | else |
| 2463 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 2464 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2465 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2466 | const char *Begin = Body.data() + Pos + 1; |
| 2467 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2468 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2469 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2470 | if (Argument == "@") { |
| 2471 | OS << NumOfMacroInstantiations; |
| 2472 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2473 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2474 | for (; Index < NParameters; ++Index) |
| 2475 | if (Parameters[Index].Name == Argument) |
| 2476 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2477 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2478 | if (Index == NParameters) { |
| 2479 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 2480 | Pos += 3; |
| 2481 | else { |
| 2482 | OS << '\\' << Argument; |
| 2483 | Pos = I; |
| 2484 | } |
| 2485 | } else { |
| 2486 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2487 | for (const AsmToken &Token : A[Index]) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2488 | // For altmacro mode, you can write '%expr'. |
| 2489 | // The prefix '%' evaluates the expression 'expr' |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2490 | // and uses the result as a string (e.g. replace %(1+2) with the |
| 2491 | // string "3"). |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2492 | // Here, we identify the integer token which is the result of the |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2493 | // absolute expression evaluation and replace it with its string |
| 2494 | // representation. |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2495 | if (AltMacroMode && Token.getString().front() == '%' && |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2496 | Token.is(AsmToken::Integer)) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2497 | // Emit an integer value to the buffer. |
| 2498 | OS << Token.getIntVal(); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2499 | // Only Token that was validated as a string and begins with '<' |
| 2500 | // is considered altMacroString!!! |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2501 | else if (AltMacroMode && Token.getString().front() == '<' && |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2502 | Token.is(AsmToken::String)) { |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 2503 | OS << altMacroString(Token.getStringContents()); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2504 | } |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2505 | // We expect no quotes around the string's contents when |
| 2506 | // parsing for varargs. |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2507 | else if (Token.isNot(AsmToken::String) || VarargParameter) |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2508 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2509 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2510 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2511 | |
| 2512 | Pos += 1 + Argument.size(); |
| 2513 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2514 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2515 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2516 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2517 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2518 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2519 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2520 | return false; |
| 2521 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2522 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2523 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2524 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2525 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2526 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2527 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2528 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2529 | switch (kind) { |
| 2530 | default: |
| 2531 | return false; |
| 2532 | case AsmToken::Plus: |
| 2533 | case AsmToken::Minus: |
| 2534 | case AsmToken::Tilde: |
| 2535 | case AsmToken::Slash: |
| 2536 | case AsmToken::Star: |
| 2537 | case AsmToken::Dot: |
| 2538 | case AsmToken::Equal: |
| 2539 | case AsmToken::EqualEqual: |
| 2540 | case AsmToken::Pipe: |
| 2541 | case AsmToken::PipePipe: |
| 2542 | case AsmToken::Caret: |
| 2543 | case AsmToken::Amp: |
| 2544 | case AsmToken::AmpAmp: |
| 2545 | case AsmToken::Exclaim: |
| 2546 | case AsmToken::ExclaimEqual: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2547 | case AsmToken::Less: |
| 2548 | case AsmToken::LessEqual: |
| 2549 | case AsmToken::LessLess: |
| 2550 | case AsmToken::LessGreater: |
| 2551 | case AsmToken::Greater: |
| 2552 | case AsmToken::GreaterEqual: |
| 2553 | case AsmToken::GreaterGreater: |
| 2554 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2558 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2559 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2560 | class AsmLexerSkipSpaceRAII { |
| 2561 | public: |
| 2562 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2563 | Lexer.setSkipSpace(SkipSpace); |
| 2564 | } |
| 2565 | |
| 2566 | ~AsmLexerSkipSpaceRAII() { |
| 2567 | Lexer.setSkipSpace(true); |
| 2568 | } |
| 2569 | |
| 2570 | private: |
| 2571 | AsmLexer &Lexer; |
| 2572 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2573 | |
| 2574 | } // end anonymous namespace |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2575 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2576 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2577 | |
| 2578 | if (Vararg) { |
| 2579 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2580 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2581 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2582 | } |
| 2583 | return false; |
| 2584 | } |
| 2585 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2586 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2587 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2588 | // Darwin doesn't use spaces to delmit arguments. |
| 2589 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2590 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2591 | bool SpaceEaten; |
| 2592 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2593 | while (true) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2594 | SpaceEaten = false; |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2595 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2596 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2597 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2598 | if (ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2599 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2600 | if (Lexer.is(AsmToken::Comma)) |
| 2601 | break; |
| 2602 | |
| 2603 | if (Lexer.is(AsmToken::Space)) { |
| 2604 | SpaceEaten = true; |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2605 | Lexer.Lex(); // Eat spaces |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2606 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2607 | |
| 2608 | // Spaces can delimit parameters, but could also be part an expression. |
| 2609 | // If the token after a space is an operator, add the token and the next |
| 2610 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2611 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2612 | if (isOperator(Lexer.getKind())) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2613 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2614 | Lexer.Lex(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2615 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2616 | // Whitespace after an operator can be ignored. |
| 2617 | if (Lexer.is(AsmToken::Space)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2618 | Lexer.Lex(); |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2619 | |
| 2620 | continue; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2623 | if (SpaceEaten) |
| 2624 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2625 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2626 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2627 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2628 | // to be able to fill in the remaining default parameter values |
| 2629 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2630 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Adjust the current parentheses level. |
| 2633 | if (Lexer.is(AsmToken::LParen)) |
| 2634 | ++ParenLevel; |
| 2635 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2636 | --ParenLevel; |
| 2637 | |
| 2638 | // Append the token to the current argument list. |
| 2639 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2640 | Lexer.Lex(); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2641 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2642 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2643 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2644 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2645 | return false; |
| 2646 | } |
| 2647 | |
| 2648 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2649 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2650 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2651 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2652 | bool NamedParametersFound = false; |
| 2653 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2654 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2655 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2656 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2657 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2658 | // Parse two kinds of macro invocations: |
| 2659 | // - macros defined without any parameters accept an arbitrary number of them |
| 2660 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2661 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2662 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2663 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2664 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2665 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2666 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2667 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2668 | if (parseIdentifier(FA.Name)) |
| 2669 | return Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2670 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2671 | if (Lexer.isNot(AsmToken::Equal)) |
| 2672 | return TokError("expected '=' after formal parameter identifier"); |
| 2673 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2674 | Lex(); |
| 2675 | |
| 2676 | NamedParametersFound = true; |
| 2677 | } |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2678 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2679 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2680 | if (NamedParametersFound && FA.Name.empty()) |
| 2681 | return Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2682 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2683 | SMLoc StrLoc = Lexer.getLoc(); |
| 2684 | SMLoc EndLoc; |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2685 | if (AltMacroMode && Lexer.is(AsmToken::Percent)) { |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2686 | const MCExpr *AbsoluteExp; |
| 2687 | int64_t Value; |
| 2688 | /// Eat '%' |
| 2689 | Lex(); |
| 2690 | if (parseExpression(AbsoluteExp, EndLoc)) |
| 2691 | return false; |
| 2692 | if (!AbsoluteExp->evaluateAsAbsolute(Value, |
| 2693 | getStreamer().getAssemblerPtr())) |
| 2694 | return Error(StrLoc, "expected absolute expression"); |
| 2695 | const char *StrChar = StrLoc.getPointer(); |
| 2696 | const char *EndChar = EndLoc.getPointer(); |
| 2697 | AsmToken newToken(AsmToken::Integer, |
| 2698 | StringRef(StrChar, EndChar - StrChar), Value); |
| 2699 | FA.Value.push_back(newToken); |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2700 | } else if (AltMacroMode && Lexer.is(AsmToken::Less) && |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2701 | isAltmacroString(StrLoc, EndLoc)) { |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2702 | const char *StrChar = StrLoc.getPointer(); |
| 2703 | const char *EndChar = EndLoc.getPointer(); |
| 2704 | jumpToLoc(EndLoc, CurBuffer); |
| 2705 | /// Eat from '<' to '>' |
| 2706 | Lex(); |
| 2707 | AsmToken newToken(AsmToken::String, |
| 2708 | StringRef(StrChar, EndChar - StrChar)); |
| 2709 | FA.Value.push_back(newToken); |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2710 | } else if(parseMacroArgument(FA.Value, Vararg)) |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2711 | return true; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2712 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2713 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2714 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2715 | unsigned FAI = 0; |
| 2716 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2717 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2718 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2719 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2720 | if (FAI >= NParameters) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2721 | assert(M && "expected macro to be defined"); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2722 | return Error(IDLoc, "parameter named '" + FA.Name + |
| 2723 | "' does not exist for macro '" + M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2724 | } |
| 2725 | PI = FAI; |
| 2726 | } |
| 2727 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2728 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2729 | if (A.size() <= PI) |
| 2730 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2731 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2732 | |
| 2733 | if (FALocs.size() <= PI) |
| 2734 | FALocs.resize(PI + 1); |
| 2735 | |
| 2736 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2737 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2738 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2739 | // At the end of the statement, fill in remaining arguments that have |
| 2740 | // default values. If there aren't any, then the next argument is |
| 2741 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2742 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2743 | bool Failure = false; |
| 2744 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2745 | if (A[FAI].empty()) { |
| 2746 | if (M->Parameters[FAI].Required) { |
| 2747 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2748 | "missing value for required parameter " |
| 2749 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2750 | Failure = true; |
| 2751 | } |
| 2752 | |
| 2753 | if (!M->Parameters[FAI].Value.empty()) |
| 2754 | A[FAI] = M->Parameters[FAI].Value; |
| 2755 | } |
| 2756 | } |
| 2757 | return Failure; |
| 2758 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2759 | |
| 2760 | if (Lexer.is(AsmToken::Comma)) |
| 2761 | Lex(); |
| 2762 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2763 | |
| 2764 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2767 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 2768 | // Arbitrarily limit macro nesting depth (default matches 'as'). We can |
| 2769 | // eliminate this, although we should protect against infinite loops. |
| 2770 | unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; |
| 2771 | if (ActiveMacros.size() == MaxNestingDepth) { |
| 2772 | std::ostringstream MaxNestingDepthError; |
| 2773 | MaxNestingDepthError << "macros cannot be nested more than " |
| 2774 | << MaxNestingDepth << " levels deep." |
| 2775 | << " Use -asm-macro-max-nesting-depth to increase " |
| 2776 | "this limit."; |
| 2777 | return TokError(MaxNestingDepthError.str()); |
| 2778 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2779 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2780 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2781 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2782 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2783 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2784 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2785 | // to hold the macro body with substitutions. |
| 2786 | SmallString<256> Buf; |
| 2787 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2788 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2789 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2790 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2791 | return true; |
| 2792 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2793 | // 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] | 2794 | // instantiation. |
| 2795 | OS << ".endmacro\n"; |
| 2796 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2797 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2798 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2799 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2800 | // Create the macro instantiation object and add to the current macro |
| 2801 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2802 | MacroInstantiation *MI = new MacroInstantiation( |
| 2803 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2804 | ActiveMacros.push_back(MI); |
| 2805 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2806 | ++NumOfMacroInstantiations; |
| 2807 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2808 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2809 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2810 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2811 | Lex(); |
| 2812 | |
| 2813 | return false; |
| 2814 | } |
| 2815 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2816 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2817 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2818 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2819 | Lex(); |
| 2820 | |
| 2821 | // Pop the instantiation entry. |
| 2822 | delete ActiveMacros.back(); |
| 2823 | ActiveMacros.pop_back(); |
| 2824 | } |
| 2825 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2826 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 2827 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2828 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2829 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2830 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 2831 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2832 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2833 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2834 | if (!Sym) { |
| 2835 | // In the case where we parse an expression starting with a '.', we will |
| 2836 | // not generate an error, nor will we create a symbol. In this case we |
| 2837 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2838 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2839 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2840 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2841 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2842 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2843 | if (NoDeadStrip) |
| 2844 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2845 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2846 | return false; |
| 2847 | } |
| 2848 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2849 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2850 | /// ::= identifier |
| 2851 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2852 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2853 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2854 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2855 | // 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] | 2856 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2857 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2858 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2859 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2860 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2861 | // Consume the prefix character, and check for a following identifier. |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2862 | |
| 2863 | AsmToken Buf[1]; |
| 2864 | Lexer.peekTokens(Buf, false); |
| 2865 | |
| 2866 | if (Buf[0].isNot(AsmToken::Identifier)) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2867 | return true; |
| 2868 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2869 | // 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] | 2870 | if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2871 | return true; |
| 2872 | |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2873 | // eat $ or @ |
| 2874 | Lexer.Lex(); // Lexer's Lex guarantees consecutive token. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2875 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2876 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2877 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2878 | Lex(); // Parser Lex to maintain invariants. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2879 | return false; |
| 2880 | } |
| 2881 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2882 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2883 | return true; |
| 2884 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2885 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2886 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2887 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2888 | |
| 2889 | return false; |
| 2890 | } |
| 2891 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2892 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2893 | /// ::= .equ identifier ',' expression |
| 2894 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2895 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2896 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2897 | StringRef Name; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2898 | if (check(parseIdentifier(Name), "expected identifier") || |
| 2899 | parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) |
| 2900 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
| 2901 | return false; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2904 | bool AsmParser::parseEscapedString(std::string &Data) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2905 | if (check(getTok().isNot(AsmToken::String), "expected string")) |
| 2906 | return true; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2907 | |
| 2908 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2909 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2910 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2911 | if (Str[i] != '\\') { |
| 2912 | Data += Str[i]; |
| 2913 | continue; |
| 2914 | } |
| 2915 | |
| 2916 | // Recognize escaped characters. Note that this escape semantics currently |
Bill Wendling | 411f188 | 2019-10-08 04:39:52 +0000 | [diff] [blame] | 2917 | // loosely follows Darwin 'as'. |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2918 | ++i; |
| 2919 | if (i == e) |
| 2920 | return TokError("unexpected backslash at end of string"); |
| 2921 | |
Bill Wendling | 411f188 | 2019-10-08 04:39:52 +0000 | [diff] [blame] | 2922 | // Recognize hex sequences similarly to GNU 'as'. |
| 2923 | if (Str[i] == 'x' || Str[i] == 'X') { |
| 2924 | size_t length = Str.size(); |
| 2925 | if (i + 1 >= length || !isHexDigit(Str[i + 1])) |
| 2926 | return TokError("invalid hexadecimal escape sequence"); |
| 2927 | |
| 2928 | // Consume hex characters. GNU 'as' reads all hexadecimal characters and |
| 2929 | // then truncates to the lower 16 bits. Seems reasonable. |
| 2930 | unsigned Value = 0; |
| 2931 | while (i + 1 < length && isHexDigit(Str[i + 1])) |
| 2932 | Value = Value * 16 + hexDigitValue(Str[++i]); |
| 2933 | |
| 2934 | Data += (unsigned char)(Value & 0xFF); |
| 2935 | continue; |
| 2936 | } |
| 2937 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2938 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2939 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2940 | // Consume up to three octal characters. |
| 2941 | unsigned Value = Str[i] - '0'; |
| 2942 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2943 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2944 | ++i; |
| 2945 | Value = Value * 8 + (Str[i] - '0'); |
| 2946 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2947 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2948 | ++i; |
| 2949 | Value = Value * 8 + (Str[i] - '0'); |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | if (Value > 255) |
| 2954 | return TokError("invalid octal escape sequence (out of range)"); |
| 2955 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2956 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2957 | continue; |
| 2958 | } |
| 2959 | |
| 2960 | // Otherwise recognize individual escapes. |
| 2961 | switch (Str[i]) { |
| 2962 | default: |
| 2963 | // Just reject invalid escape sequences for now. |
| 2964 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2965 | |
| 2966 | case 'b': Data += '\b'; break; |
| 2967 | case 'f': Data += '\f'; break; |
| 2968 | case 'n': Data += '\n'; break; |
| 2969 | case 'r': Data += '\r'; break; |
| 2970 | case 't': Data += '\t'; break; |
| 2971 | case '"': Data += '"'; break; |
| 2972 | case '\\': Data += '\\'; break; |
| 2973 | } |
| 2974 | } |
| 2975 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2976 | Lex(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2977 | return false; |
| 2978 | } |
| 2979 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2980 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2981 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2982 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2983 | auto parseOp = [&]() -> bool { |
| 2984 | std::string Data; |
| 2985 | if (checkForValidSection() || parseEscapedString(Data)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2986 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2987 | getStreamer().EmitBytes(Data); |
| 2988 | if (ZeroTerminated) |
| 2989 | getStreamer().EmitBytes(StringRef("\0", 1)); |
| 2990 | return false; |
| 2991 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2992 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2993 | if (parseMany(parseOp)) |
| 2994 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2995 | return false; |
| 2996 | } |
| 2997 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2998 | /// parseDirectiveReloc |
| 2999 | /// ::= .reloc expression , identifier [ , expression ] |
| 3000 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
| 3001 | const MCExpr *Offset; |
| 3002 | const MCExpr *Expr = nullptr; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3003 | int64_t OffsetValue; |
Vladimir Stefanovic | 1d2714b | 2018-11-21 16:28:39 +0000 | [diff] [blame] | 3004 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3005 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3006 | if (parseExpression(Offset)) |
| 3007 | return true; |
| 3008 | |
Vladimir Stefanovic | 1d2714b | 2018-11-21 16:28:39 +0000 | [diff] [blame] | 3009 | if ((Offset->evaluateAsAbsolute(OffsetValue, |
| 3010 | getStreamer().getAssemblerPtr()) && |
| 3011 | check(OffsetValue < 0, OffsetLoc, "expression is negative")) || |
| 3012 | (check(Offset->getKind() != llvm::MCExpr::Constant && |
| 3013 | Offset->getKind() != llvm::MCExpr::SymbolRef, |
| 3014 | OffsetLoc, "expected non-negative number or a label")) || |
| 3015 | (parseToken(AsmToken::Comma, "expected comma") || |
| 3016 | check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3017 | return true; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3018 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3019 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
| 3020 | StringRef Name = Lexer.getTok().getIdentifier(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 3021 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3022 | |
| 3023 | if (Lexer.is(AsmToken::Comma)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 3024 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3025 | SMLoc ExprLoc = Lexer.getLoc(); |
| 3026 | if (parseExpression(Expr)) |
| 3027 | return true; |
| 3028 | |
| 3029 | MCValue Value; |
| 3030 | if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) |
| 3031 | return Error(ExprLoc, "expression must be relocatable"); |
| 3032 | } |
| 3033 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3034 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3035 | "unexpected token in .reloc directive")) |
| 3036 | return true; |
| 3037 | |
Peter Smith | 57f661b | 2018-06-06 09:40:06 +0000 | [diff] [blame] | 3038 | const MCTargetAsmParser &MCT = getTargetParser(); |
| 3039 | const MCSubtargetInfo &STI = MCT.getSTI(); |
| 3040 | if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc, STI)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3041 | return Error(NameLoc, "unknown relocation name"); |
| 3042 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 3043 | return false; |
| 3044 | } |
| 3045 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3046 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3047 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3048 | bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { |
| 3049 | auto parseOp = [&]() -> bool { |
| 3050 | const MCExpr *Value; |
| 3051 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3052 | if (checkForValidSection() || parseExpression(Value)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3053 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3054 | // Special case constant expressions to match code generator. |
| 3055 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3056 | assert(Size <= 8 && "Invalid size"); |
| 3057 | uint64_t IntValue = MCE->getValue(); |
| 3058 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 3059 | return Error(ExprLoc, "out of range literal value"); |
| 3060 | getStreamer().EmitIntValue(IntValue, Size); |
| 3061 | } else |
| 3062 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 3063 | return false; |
| 3064 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3065 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3066 | if (parseMany(parseOp)) |
| 3067 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3068 | return false; |
| 3069 | } |
| 3070 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3071 | static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) { |
| 3072 | if (Asm.getTok().isNot(AsmToken::Integer) && |
| 3073 | Asm.getTok().isNot(AsmToken::BigNum)) |
| 3074 | return Asm.TokError("unknown token in expression"); |
| 3075 | SMLoc ExprLoc = Asm.getTok().getLoc(); |
| 3076 | APInt IntValue = Asm.getTok().getAPIntVal(); |
| 3077 | Asm.Lex(); |
| 3078 | if (!IntValue.isIntN(128)) |
| 3079 | return Asm.Error(ExprLoc, "out of range literal value"); |
| 3080 | if (!IntValue.isIntN(64)) { |
| 3081 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
| 3082 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 3083 | } else { |
| 3084 | hi = 0; |
| 3085 | lo = IntValue.getZExtValue(); |
| 3086 | } |
| 3087 | return false; |
| 3088 | } |
| 3089 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3090 | /// ParseDirectiveOctaValue |
| 3091 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3092 | |
| 3093 | bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { |
| 3094 | auto parseOp = [&]() -> bool { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3095 | if (checkForValidSection()) |
| 3096 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3097 | uint64_t hi, lo; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3098 | if (parseHexOcta(*this, hi, lo)) |
| 3099 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3100 | if (MAI.isLittleEndian()) { |
| 3101 | getStreamer().EmitIntValue(lo, 8); |
| 3102 | getStreamer().EmitIntValue(hi, 8); |
| 3103 | } else { |
| 3104 | getStreamer().EmitIntValue(hi, 8); |
| 3105 | getStreamer().EmitIntValue(lo, 8); |
| 3106 | } |
| 3107 | return false; |
| 3108 | }; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3109 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3110 | if (parseMany(parseOp)) |
| 3111 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3112 | return false; |
| 3113 | } |
| 3114 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 3115 | bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { |
| 3116 | // We don't truly support arithmetic on floating point expressions, so we |
| 3117 | // have to manually parse unary prefixes. |
| 3118 | bool IsNeg = false; |
| 3119 | if (getLexer().is(AsmToken::Minus)) { |
| 3120 | Lexer.Lex(); |
| 3121 | IsNeg = true; |
| 3122 | } else if (getLexer().is(AsmToken::Plus)) |
| 3123 | Lexer.Lex(); |
| 3124 | |
| 3125 | if (Lexer.is(AsmToken::Error)) |
| 3126 | return TokError(Lexer.getErr()); |
| 3127 | if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && |
| 3128 | Lexer.isNot(AsmToken::Identifier)) |
| 3129 | return TokError("unexpected token in directive"); |
| 3130 | |
| 3131 | // Convert to an APFloat. |
| 3132 | APFloat Value(Semantics); |
| 3133 | StringRef IDVal = getTok().getString(); |
| 3134 | if (getLexer().is(AsmToken::Identifier)) { |
| 3135 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 3136 | Value = APFloat::getInf(Semantics); |
| 3137 | else if (!IDVal.compare_lower("nan")) |
| 3138 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 3139 | else |
| 3140 | return TokError("invalid floating point literal"); |
| 3141 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
| 3142 | APFloat::opInvalidOp) |
| 3143 | return TokError("invalid floating point literal"); |
| 3144 | if (IsNeg) |
| 3145 | Value.changeSign(); |
| 3146 | |
| 3147 | // Consume the numeric token. |
| 3148 | Lex(); |
| 3149 | |
| 3150 | Res = Value.bitcastToAPInt(); |
| 3151 | |
| 3152 | return false; |
| 3153 | } |
| 3154 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3155 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3156 | /// ::= (.single | .double) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3157 | bool AsmParser::parseDirectiveRealValue(StringRef IDVal, |
| 3158 | const fltSemantics &Semantics) { |
| 3159 | auto parseOp = [&]() -> bool { |
| 3160 | APInt AsInt; |
| 3161 | if (checkForValidSection() || parseRealValue(Semantics, AsInt)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3162 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3163 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 3164 | AsInt.getBitWidth() / 8); |
| 3165 | return false; |
| 3166 | }; |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3167 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3168 | if (parseMany(parseOp)) |
| 3169 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3170 | return false; |
| 3171 | } |
| 3172 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3173 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3174 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3175 | bool AsmParser::parseDirectiveZero() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3176 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 3177 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3178 | if (checkForValidSection() || parseExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3179 | return true; |
| 3180 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3181 | int64_t Val = 0; |
| 3182 | if (getLexer().is(AsmToken::Comma)) { |
| 3183 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3184 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3185 | return true; |
| 3186 | } |
| 3187 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3188 | if (parseToken(AsmToken::EndOfStatement, |
| 3189 | "unexpected token in '.zero' directive")) |
| 3190 | return true; |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3191 | getStreamer().emitFill(*NumBytes, Val, NumBytesLoc); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3192 | |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3196 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3197 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3198 | bool AsmParser::parseDirectiveFill() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3199 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 3200 | const MCExpr *NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3201 | if (checkForValidSection() || parseExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3202 | return true; |
| 3203 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3204 | int64_t FillSize = 1; |
| 3205 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3206 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3207 | SMLoc SizeLoc, ExprLoc; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3208 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3209 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3210 | SizeLoc = getTok().getLoc(); |
| 3211 | if (parseAbsoluteExpression(FillSize)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3212 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3213 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3214 | ExprLoc = getTok().getLoc(); |
| 3215 | if (parseAbsoluteExpression(FillExpr)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3216 | return true; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3219 | if (parseToken(AsmToken::EndOfStatement, |
| 3220 | "unexpected token in '.fill' directive")) |
| 3221 | return true; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3222 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3223 | if (FillSize < 0) { |
| 3224 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
Petr Hosek | 6abd38b | 2016-05-28 08:20:08 +0000 | [diff] [blame] | 3225 | return false; |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3226 | } |
| 3227 | if (FillSize > 8) { |
| 3228 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 3229 | FillSize = 8; |
| 3230 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3231 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3232 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 3233 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 3234 | |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3235 | getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3236 | |
| 3237 | return false; |
| 3238 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3239 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3240 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3241 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3242 | bool AsmParser::parseDirectiveOrg() { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 3243 | const MCExpr *Offset; |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3244 | SMLoc OffsetLoc = Lexer.getLoc(); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3245 | if (checkForValidSection() || parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3246 | return true; |
| 3247 | |
| 3248 | // Parse optional fill expression. |
| 3249 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3250 | if (parseOptionalToken(AsmToken::Comma)) |
| 3251 | if (parseAbsoluteExpression(FillExpr)) |
| 3252 | return addErrorSuffix(" in '.org' directive"); |
| 3253 | if (parseToken(AsmToken::EndOfStatement)) |
| 3254 | return addErrorSuffix(" in '.org' directive"); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3255 | |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3256 | getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3257 | return false; |
| 3258 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3259 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3260 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3261 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3262 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3263 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3264 | int64_t Alignment; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3265 | SMLoc MaxBytesLoc; |
| 3266 | bool HasFillExpr = false; |
| 3267 | int64_t FillExpr = 0; |
| 3268 | int64_t MaxBytesToFill = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3269 | |
| 3270 | auto parseAlign = [&]() -> bool { |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3271 | if (parseAbsoluteExpression(Alignment)) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3272 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3273 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3274 | // The fill expression can be omitted while specifying a maximum number of |
| 3275 | // alignment bytes, e.g: |
| 3276 | // .align 3,,4 |
| 3277 | if (getTok().isNot(AsmToken::Comma)) { |
| 3278 | HasFillExpr = true; |
| 3279 | if (parseAbsoluteExpression(FillExpr)) |
| 3280 | return true; |
| 3281 | } |
| 3282 | if (parseOptionalToken(AsmToken::Comma)) |
| 3283 | if (parseTokenLoc(MaxBytesLoc) || |
| 3284 | parseAbsoluteExpression(MaxBytesToFill)) |
| 3285 | return true; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3286 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3287 | return parseToken(AsmToken::EndOfStatement); |
| 3288 | }; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3289 | |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3290 | if (checkForValidSection()) |
| 3291 | return addErrorSuffix(" in directive"); |
| 3292 | // Ignore empty '.p2align' directives for GNU-as compatibility |
| 3293 | if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) { |
| 3294 | Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored"); |
| 3295 | return parseToken(AsmToken::EndOfStatement); |
| 3296 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3297 | if (parseAlign()) |
| 3298 | return addErrorSuffix(" in directive"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3299 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3300 | // Always emit an alignment here even if we thrown an error. |
| 3301 | bool ReturnVal = false; |
| 3302 | |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3303 | // Compute alignment in bytes. |
| 3304 | if (IsPow2) { |
| 3305 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3306 | if (Alignment >= 32) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3307 | ReturnVal |= Error(AlignmentLoc, "invalid alignment value"); |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3308 | Alignment = 31; |
| 3309 | } |
| 3310 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 3311 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3312 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 3313 | // Reject alignments that aren't either a power of two or zero, |
| 3314 | // for gas compatibility. Alignment of zero is silently rounded |
| 3315 | // up to one. |
| 3316 | if (Alignment == 0) |
| 3317 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3318 | if (!isPowerOf2_64(Alignment)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3319 | ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3322 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3323 | if (MaxBytesLoc.isValid()) { |
| 3324 | if (MaxBytesToFill < 1) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3325 | ReturnVal |= Error(MaxBytesLoc, |
| 3326 | "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3327 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 3328 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 3332 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3333 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3334 | MaxBytesToFill = 0; |
| 3335 | } |
| 3336 | } |
| 3337 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3338 | // Check whether we should use optimal code alignment for this .align |
| 3339 | // directive. |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 3340 | const MCSection *Section = getStreamer().getCurrentSectionOnly(); |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 3341 | assert(Section && "must have section to emit alignment"); |
| 3342 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3343 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 3344 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3345 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3346 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 3347 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 3348 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 3349 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3350 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3351 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3352 | return ReturnVal; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3355 | /// parseDirectiveFile |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3356 | /// ::= .file filename |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3357 | /// ::= .file number [directory] filename [md5 checksum] [source source-text] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3358 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3359 | // FIXME: I'm not sure what this is. |
| 3360 | int64_t FileNumber = -1; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3361 | if (getLexer().is(AsmToken::Integer)) { |
| 3362 | FileNumber = getTok().getIntVal(); |
| 3363 | Lex(); |
| 3364 | |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3365 | if (FileNumber < 0) |
| 3366 | return TokError("negative file number"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
Paul Robinson | 37fbb92 | 2018-06-20 16:12:03 +0000 | [diff] [blame] | 3369 | std::string Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3370 | |
| 3371 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3372 | // Allow the strings to have escaped octal character sequence. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3373 | if (check(getTok().isNot(AsmToken::String), |
| 3374 | "unexpected token in '.file' directive") || |
| 3375 | parseEscapedString(Path)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3376 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3377 | |
| 3378 | StringRef Directory; |
| 3379 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3380 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3381 | if (getLexer().is(AsmToken::String)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3382 | if (check(FileNumber == -1, |
| 3383 | "explicit path specified, but no file number") || |
| 3384 | parseEscapedString(FilenameData)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3385 | return true; |
| 3386 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3387 | Directory = Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3388 | } else { |
| 3389 | Filename = Path; |
| 3390 | } |
| 3391 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3392 | uint64_t MD5Hi, MD5Lo; |
| 3393 | bool HasMD5 = false; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3394 | |
| 3395 | Optional<StringRef> Source; |
| 3396 | bool HasSource = false; |
| 3397 | std::string SourceString; |
| 3398 | |
| 3399 | while (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3400 | StringRef Keyword; |
| 3401 | if (check(getTok().isNot(AsmToken::Identifier), |
| 3402 | "unexpected token in '.file' directive") || |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3403 | parseIdentifier(Keyword)) |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3404 | return true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3405 | if (Keyword == "md5") { |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3406 | HasMD5 = true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3407 | if (check(FileNumber == -1, |
| 3408 | "MD5 checksum specified, but no file number") || |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3409 | parseHexOcta(*this, MD5Hi, MD5Lo)) |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3410 | return true; |
| 3411 | } else if (Keyword == "source") { |
| 3412 | HasSource = true; |
| 3413 | if (check(FileNumber == -1, |
| 3414 | "source specified, but no file number") || |
| 3415 | check(getTok().isNot(AsmToken::String), |
| 3416 | "unexpected token in '.file' directive") || |
| 3417 | parseEscapedString(SourceString)) |
| 3418 | return true; |
| 3419 | } else { |
| 3420 | return TokError("unexpected token in '.file' directive"); |
| 3421 | } |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3422 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3423 | |
Reid Kleckner | 98bbd07 | 2018-12-21 23:35:48 +0000 | [diff] [blame] | 3424 | if (FileNumber == -1) { |
Reid Kleckner | f38bc4f | 2019-02-05 21:14:09 +0000 | [diff] [blame] | 3425 | // Ignore the directive if there is no number and the target doesn't support |
| 3426 | // numberless .file directives. This allows some portability of assembler |
| 3427 | // between different object file formats. |
| 3428 | if (getContext().getAsmInfo()->hasSingleParameterDotFile()) |
| 3429 | getStreamer().EmitFileDirective(Filename); |
Reid Kleckner | 98bbd07 | 2018-12-21 23:35:48 +0000 | [diff] [blame] | 3430 | } else { |
Brian Cain | 3e0ca57 | 2018-08-28 16:23:39 +0000 | [diff] [blame] | 3431 | // In case there is a -g option as well as debug info from directive .file, |
| 3432 | // we turn off the -g option, directly use the existing debug info instead. |
Paul Robinson | 26cc5bc | 2019-06-21 13:10:19 +0000 | [diff] [blame] | 3433 | // Throw away any implicit file table for the assembler source. |
Brian Cain | 3e0ca57 | 2018-08-28 16:23:39 +0000 | [diff] [blame] | 3434 | if (Ctx.getGenDwarfForAssembly()) { |
Paul Robinson | 26cc5bc | 2019-06-21 13:10:19 +0000 | [diff] [blame] | 3435 | Ctx.getMCDwarfLineTable(0).resetFileTable(); |
Brian Cain | 3e0ca57 | 2018-08-28 16:23:39 +0000 | [diff] [blame] | 3436 | Ctx.setGenDwarfForAssembly(false); |
| 3437 | } |
| 3438 | |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 3439 | Optional<MD5::MD5Result> CKMem; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3440 | if (HasMD5) { |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 3441 | MD5::MD5Result Sum; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3442 | for (unsigned i = 0; i != 8; ++i) { |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 3443 | Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8)); |
| 3444 | Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8)); |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3445 | } |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 3446 | CKMem = Sum; |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3447 | } |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3448 | if (HasSource) { |
| 3449 | char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size())); |
| 3450 | memcpy(SourceBuf, SourceString.data(), SourceString.size()); |
| 3451 | Source = StringRef(SourceBuf, SourceString.size()); |
| 3452 | } |
Paul Robinson | 547adca | 2018-06-21 16:42:03 +0000 | [diff] [blame] | 3453 | if (FileNumber == 0) { |
| 3454 | if (Ctx.getDwarfVersion() < 5) |
| 3455 | return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5"); |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3456 | getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source); |
Paul Robinson | 547adca | 2018-06-21 16:42:03 +0000 | [diff] [blame] | 3457 | } else { |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3458 | Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective( |
| 3459 | FileNumber, Directory, Filename, CKMem, Source); |
| 3460 | if (!FileNumOrErr) |
| 3461 | return Error(DirectiveLoc, toString(FileNumOrErr.takeError())); |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3462 | } |
Paul Robinson | cc7344a | 2018-06-14 13:38:20 +0000 | [diff] [blame] | 3463 | // Alert the user if there are some .file directives with MD5 and some not. |
| 3464 | // But only do that once. |
| 3465 | if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) { |
| 3466 | ReportedInconsistentMD5 = true; |
| 3467 | return Warning(DirectiveLoc, "inconsistent use of MD5 checksums"); |
| 3468 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | return false; |
| 3472 | } |
| 3473 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3474 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3475 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3476 | bool AsmParser::parseDirectiveLine() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3477 | int64_t LineNumber; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3478 | if (getLexer().is(AsmToken::Integer)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3479 | if (parseIntToken(LineNumber, "unexpected token in '.line' directive")) |
| 3480 | return true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3481 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3482 | // FIXME: Do something with the .line. |
| 3483 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3484 | if (parseToken(AsmToken::EndOfStatement, |
| 3485 | "unexpected token in '.line' directive")) |
| 3486 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3487 | |
| 3488 | return false; |
| 3489 | } |
| 3490 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3491 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3492 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 3493 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 3494 | /// The first number is a file number, must have been previously assigned with |
| 3495 | /// a .file directive, the second number is the line number and optionally the |
| 3496 | /// third number is a column position (zero if not specified). The remaining |
| 3497 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3498 | bool AsmParser::parseDirectiveLoc() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3499 | int64_t FileNumber = 0, LineNumber = 0; |
| 3500 | SMLoc Loc = getTok().getLoc(); |
| 3501 | if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") || |
Paul Robinson | 11539b0 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 3502 | check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3503 | "file number less than one in '.loc' directive") || |
| 3504 | check(!getContext().isValidDwarfFileNumber(FileNumber), Loc, |
| 3505 | "unassigned file number in '.loc' directive")) |
| 3506 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3507 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3508 | // optional |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3509 | if (getLexer().is(AsmToken::Integer)) { |
| 3510 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 3511 | if (LineNumber < 0) |
| 3512 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3513 | Lex(); |
| 3514 | } |
| 3515 | |
| 3516 | int64_t ColumnPos = 0; |
| 3517 | if (getLexer().is(AsmToken::Integer)) { |
| 3518 | ColumnPos = getTok().getIntVal(); |
| 3519 | if (ColumnPos < 0) |
| 3520 | return TokError("column position less than zero in '.loc' directive"); |
| 3521 | Lex(); |
| 3522 | } |
| 3523 | |
| 3524 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 3525 | unsigned Isa = 0; |
| 3526 | int64_t Discriminator = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3527 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3528 | auto parseLocOp = [&]() -> bool { |
| 3529 | StringRef Name; |
| 3530 | SMLoc Loc = getTok().getLoc(); |
| 3531 | if (parseIdentifier(Name)) |
| 3532 | return TokError("unexpected token in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3533 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3534 | if (Name == "basic_block") |
| 3535 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 3536 | else if (Name == "prologue_end") |
| 3537 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 3538 | else if (Name == "epilogue_begin") |
| 3539 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 3540 | else if (Name == "is_stmt") { |
| 3541 | Loc = getTok().getLoc(); |
| 3542 | const MCExpr *Value; |
| 3543 | if (parseExpression(Value)) |
| 3544 | return true; |
| 3545 | // The expression must be the constant 0 or 1. |
| 3546 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3547 | int Value = MCE->getValue(); |
| 3548 | if (Value == 0) |
| 3549 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 3550 | else if (Value == 1) |
| 3551 | Flags |= DWARF2_FLAG_IS_STMT; |
| 3552 | else |
| 3553 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3554 | } else { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3555 | 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] | 3556 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3557 | } else if (Name == "isa") { |
| 3558 | Loc = getTok().getLoc(); |
| 3559 | const MCExpr *Value; |
| 3560 | if (parseExpression(Value)) |
| 3561 | return true; |
| 3562 | // The expression must be a constant greater or equal to 0. |
| 3563 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3564 | int Value = MCE->getValue(); |
| 3565 | if (Value < 0) |
| 3566 | return Error(Loc, "isa number less than zero"); |
| 3567 | Isa = Value; |
| 3568 | } else { |
| 3569 | return Error(Loc, "isa number not a constant value"); |
| 3570 | } |
| 3571 | } else if (Name == "discriminator") { |
| 3572 | if (parseAbsoluteExpression(Discriminator)) |
| 3573 | return true; |
| 3574 | } else { |
| 3575 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3576 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3577 | return false; |
| 3578 | }; |
| 3579 | |
Nirav Dave | e2369aa | 2016-10-26 17:28:58 +0000 | [diff] [blame] | 3580 | if (parseMany(parseLocOp, false /*hasComma*/)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3581 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3582 | |
| 3583 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3584 | Isa, Discriminator, StringRef()); |
| 3585 | |
| 3586 | return false; |
| 3587 | } |
| 3588 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3589 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3590 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3591 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3592 | return TokError("unsupported directive '.stabs'"); |
| 3593 | } |
| 3594 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3595 | /// parseDirectiveCVFile |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3596 | /// ::= .cv_file number filename [checksum] [checksumkind] |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3597 | bool AsmParser::parseDirectiveCVFile() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3598 | SMLoc FileNumberLoc = getTok().getLoc(); |
| 3599 | int64_t FileNumber; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3600 | std::string Filename; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3601 | std::string Checksum; |
| 3602 | int64_t ChecksumKind = 0; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3603 | |
| 3604 | if (parseIntToken(FileNumber, |
| 3605 | "expected file number in '.cv_file' directive") || |
| 3606 | check(FileNumber < 1, FileNumberLoc, "file number less than one") || |
| 3607 | check(getTok().isNot(AsmToken::String), |
| 3608 | "unexpected token in '.cv_file' directive") || |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3609 | parseEscapedString(Filename)) |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 3610 | return true; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3611 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3612 | if (check(getTok().isNot(AsmToken::String), |
| 3613 | "unexpected token in '.cv_file' directive") || |
| 3614 | parseEscapedString(Checksum) || |
| 3615 | parseIntToken(ChecksumKind, |
| 3616 | "expected checksum kind in '.cv_file' directive") || |
| 3617 | parseToken(AsmToken::EndOfStatement, |
| 3618 | "unexpected token in '.cv_file' directive")) |
| 3619 | return true; |
| 3620 | } |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3621 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3622 | Checksum = fromHex(Checksum); |
| 3623 | void *CKMem = Ctx.allocate(Checksum.size(), 1); |
| 3624 | memcpy(CKMem, Checksum.data(), Checksum.size()); |
| 3625 | ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem), |
| 3626 | Checksum.size()); |
| 3627 | |
| 3628 | if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes, |
| 3629 | static_cast<uint8_t>(ChecksumKind))) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3630 | return Error(FileNumberLoc, "file number already allocated"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3631 | |
| 3632 | return false; |
| 3633 | } |
| 3634 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3635 | bool AsmParser::parseCVFunctionId(int64_t &FunctionId, |
| 3636 | StringRef DirectiveName) { |
| 3637 | SMLoc Loc; |
| 3638 | return parseTokenLoc(Loc) || |
| 3639 | parseIntToken(FunctionId, "expected function id in '" + DirectiveName + |
| 3640 | "' directive") || |
| 3641 | check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc, |
| 3642 | "expected function id within range [0, UINT_MAX)"); |
| 3643 | } |
| 3644 | |
| 3645 | bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { |
| 3646 | SMLoc Loc; |
| 3647 | return parseTokenLoc(Loc) || |
| 3648 | parseIntToken(FileNumber, "expected integer in '" + DirectiveName + |
| 3649 | "' directive") || |
| 3650 | check(FileNumber < 1, Loc, "file number less than one in '" + |
| 3651 | DirectiveName + "' directive") || |
| 3652 | check(!getCVContext().isValidFileNumber(FileNumber), Loc, |
| 3653 | "unassigned file number in '" + DirectiveName + "' directive"); |
| 3654 | } |
| 3655 | |
| 3656 | /// parseDirectiveCVFuncId |
| 3657 | /// ::= .cv_func_id FunctionId |
| 3658 | /// |
| 3659 | /// Introduces a function ID that can be used with .cv_loc. |
| 3660 | bool AsmParser::parseDirectiveCVFuncId() { |
| 3661 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3662 | int64_t FunctionId; |
| 3663 | |
| 3664 | if (parseCVFunctionId(FunctionId, ".cv_func_id") || |
| 3665 | parseToken(AsmToken::EndOfStatement, |
| 3666 | "unexpected token in '.cv_func_id' directive")) |
| 3667 | return true; |
| 3668 | |
| 3669 | if (!getStreamer().EmitCVFuncIdDirective(FunctionId)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3670 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3671 | |
| 3672 | return false; |
| 3673 | } |
| 3674 | |
| 3675 | /// parseDirectiveCVInlineSiteId |
| 3676 | /// ::= .cv_inline_site_id FunctionId |
| 3677 | /// "within" IAFunc |
| 3678 | /// "inlined_at" IAFile IALine [IACol] |
| 3679 | /// |
| 3680 | /// Introduces a function ID that can be used with .cv_loc. Includes "inlined |
| 3681 | /// at" source location information for use in the line table of the caller, |
| 3682 | /// whether the caller is a real function or another inlined call site. |
| 3683 | bool AsmParser::parseDirectiveCVInlineSiteId() { |
| 3684 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3685 | int64_t FunctionId; |
| 3686 | int64_t IAFunc; |
| 3687 | int64_t IAFile; |
| 3688 | int64_t IALine; |
| 3689 | int64_t IACol = 0; |
| 3690 | |
| 3691 | // FunctionId |
| 3692 | if (parseCVFunctionId(FunctionId, ".cv_inline_site_id")) |
| 3693 | return true; |
| 3694 | |
| 3695 | // "within" |
| 3696 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3697 | getTok().getIdentifier() != "within"), |
| 3698 | "expected 'within' identifier in '.cv_inline_site_id' directive")) |
| 3699 | return true; |
| 3700 | Lex(); |
| 3701 | |
| 3702 | // IAFunc |
| 3703 | if (parseCVFunctionId(IAFunc, ".cv_inline_site_id")) |
| 3704 | return true; |
| 3705 | |
| 3706 | // "inlined_at" |
| 3707 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3708 | getTok().getIdentifier() != "inlined_at"), |
| 3709 | "expected 'inlined_at' identifier in '.cv_inline_site_id' " |
| 3710 | "directive") ) |
| 3711 | return true; |
| 3712 | Lex(); |
| 3713 | |
| 3714 | // IAFile IALine |
| 3715 | if (parseCVFileId(IAFile, ".cv_inline_site_id") || |
| 3716 | parseIntToken(IALine, "expected line number after 'inlined_at'")) |
| 3717 | return true; |
| 3718 | |
| 3719 | // [IACol] |
| 3720 | if (getLexer().is(AsmToken::Integer)) { |
| 3721 | IACol = getTok().getIntVal(); |
| 3722 | Lex(); |
| 3723 | } |
| 3724 | |
| 3725 | if (parseToken(AsmToken::EndOfStatement, |
| 3726 | "unexpected token in '.cv_inline_site_id' directive")) |
| 3727 | return true; |
| 3728 | |
| 3729 | if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, |
| 3730 | IALine, IACol, FunctionIdLoc)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3731 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3732 | |
| 3733 | return false; |
| 3734 | } |
| 3735 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3736 | /// parseDirectiveCVLoc |
| 3737 | /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] |
| 3738 | /// [is_stmt VALUE] |
| 3739 | /// The first number is a file number, must have been previously assigned with |
| 3740 | /// a .file directive, the second number is the line number and optionally the |
| 3741 | /// third number is a column position (zero if not specified). The remaining |
| 3742 | /// optional items are .loc sub-directives. |
| 3743 | bool AsmParser::parseDirectiveCVLoc() { |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3744 | SMLoc DirectiveLoc = getTok().getLoc(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3745 | int64_t FunctionId, FileNumber; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3746 | if (parseCVFunctionId(FunctionId, ".cv_loc") || |
| 3747 | parseCVFileId(FileNumber, ".cv_loc")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3748 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3749 | |
| 3750 | int64_t LineNumber = 0; |
| 3751 | if (getLexer().is(AsmToken::Integer)) { |
| 3752 | LineNumber = getTok().getIntVal(); |
| 3753 | if (LineNumber < 0) |
| 3754 | return TokError("line number less than zero in '.cv_loc' directive"); |
| 3755 | Lex(); |
| 3756 | } |
| 3757 | |
| 3758 | int64_t ColumnPos = 0; |
| 3759 | if (getLexer().is(AsmToken::Integer)) { |
| 3760 | ColumnPos = getTok().getIntVal(); |
| 3761 | if (ColumnPos < 0) |
| 3762 | return TokError("column position less than zero in '.cv_loc' directive"); |
| 3763 | Lex(); |
| 3764 | } |
| 3765 | |
| 3766 | bool PrologueEnd = false; |
| 3767 | uint64_t IsStmt = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3768 | |
| 3769 | auto parseOp = [&]() -> bool { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3770 | StringRef Name; |
| 3771 | SMLoc Loc = getTok().getLoc(); |
| 3772 | if (parseIdentifier(Name)) |
| 3773 | return TokError("unexpected token in '.cv_loc' directive"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3774 | if (Name == "prologue_end") |
| 3775 | PrologueEnd = true; |
| 3776 | else if (Name == "is_stmt") { |
| 3777 | Loc = getTok().getLoc(); |
| 3778 | const MCExpr *Value; |
| 3779 | if (parseExpression(Value)) |
| 3780 | return true; |
| 3781 | // The expression must be the constant 0 or 1. |
| 3782 | IsStmt = ~0ULL; |
| 3783 | if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) |
| 3784 | IsStmt = MCE->getValue(); |
| 3785 | |
| 3786 | if (IsStmt > 1) |
| 3787 | return Error(Loc, "is_stmt value not 0 or 1"); |
| 3788 | } else { |
| 3789 | return Error(Loc, "unknown sub-directive in '.cv_loc' directive"); |
| 3790 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3791 | return false; |
| 3792 | }; |
| 3793 | |
| 3794 | if (parseMany(parseOp, false /*hasComma*/)) |
| 3795 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3796 | |
| 3797 | getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3798 | ColumnPos, PrologueEnd, IsStmt, StringRef(), |
| 3799 | DirectiveLoc); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3800 | return false; |
| 3801 | } |
| 3802 | |
| 3803 | /// parseDirectiveCVLinetable |
| 3804 | /// ::= .cv_linetable FunctionId, FnStart, FnEnd |
| 3805 | bool AsmParser::parseDirectiveCVLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3806 | int64_t FunctionId; |
| 3807 | StringRef FnStartName, FnEndName; |
| 3808 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3809 | if (parseCVFunctionId(FunctionId, ".cv_linetable") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3810 | parseToken(AsmToken::Comma, |
| 3811 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3812 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3813 | "expected identifier in directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3814 | parseToken(AsmToken::Comma, |
| 3815 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3816 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3817 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3818 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3819 | |
| 3820 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3821 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
| 3822 | |
| 3823 | getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym); |
| 3824 | return false; |
| 3825 | } |
| 3826 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3827 | /// parseDirectiveCVInlineLinetable |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3828 | /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3829 | bool AsmParser::parseDirectiveCVInlineLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3830 | int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; |
| 3831 | StringRef FnStartName, FnEndName; |
| 3832 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3833 | if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3834 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3835 | parseIntToken( |
| 3836 | SourceFileId, |
| 3837 | "expected SourceField in '.cv_inline_linetable' directive") || |
| 3838 | check(SourceFileId <= 0, Loc, |
| 3839 | "File id less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3840 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3841 | parseIntToken( |
| 3842 | SourceLineNum, |
| 3843 | "expected SourceLineNum in '.cv_inline_linetable' directive") || |
| 3844 | check(SourceLineNum < 0, Loc, |
| 3845 | "Line number less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3846 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3847 | "expected identifier in directive") || |
| 3848 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3849 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3850 | return true; |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3851 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3852 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3853 | return true; |
| 3854 | |
| 3855 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3856 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 3857 | getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, |
| 3858 | SourceLineNum, FnStartSym, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3859 | FnEndSym); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3860 | return false; |
| 3861 | } |
| 3862 | |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3863 | void AsmParser::initializeCVDefRangeTypeMap() { |
| 3864 | CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER; |
| 3865 | CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL; |
| 3866 | CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER; |
| 3867 | CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL; |
| 3868 | } |
| 3869 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3870 | /// parseDirectiveCVDefRange |
| 3871 | /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* |
| 3872 | bool AsmParser::parseDirectiveCVDefRange() { |
| 3873 | SMLoc Loc; |
| 3874 | std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; |
| 3875 | while (getLexer().is(AsmToken::Identifier)) { |
| 3876 | Loc = getLexer().getLoc(); |
| 3877 | StringRef GapStartName; |
| 3878 | if (parseIdentifier(GapStartName)) |
| 3879 | return Error(Loc, "expected identifier in directive"); |
| 3880 | MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName); |
| 3881 | |
| 3882 | Loc = getLexer().getLoc(); |
| 3883 | StringRef GapEndName; |
| 3884 | if (parseIdentifier(GapEndName)) |
| 3885 | return Error(Loc, "expected identifier in directive"); |
| 3886 | MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName); |
| 3887 | |
| 3888 | Ranges.push_back({GapStartSym, GapEndSym}); |
| 3889 | } |
| 3890 | |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3891 | StringRef CVDefRangeTypeStr; |
| 3892 | if (parseToken( |
| 3893 | AsmToken::Comma, |
| 3894 | "expected comma before def_range type in .cv_def_range directive") || |
| 3895 | parseIdentifier(CVDefRangeTypeStr)) |
| 3896 | return Error(Loc, "expected def_range type in directive"); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3897 | |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3898 | StringMap<CVDefRangeType>::const_iterator CVTypeIt = |
| 3899 | CVDefRangeTypeMap.find(CVDefRangeTypeStr); |
| 3900 | CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end()) |
| 3901 | ? CVDR_DEFRANGE |
| 3902 | : CVTypeIt->getValue(); |
| 3903 | switch (CVDRType) { |
| 3904 | case CVDR_DEFRANGE_REGISTER: { |
| 3905 | int64_t DRRegister; |
| 3906 | if (parseToken(AsmToken::Comma, "expected comma before register number in " |
| 3907 | ".cv_def_range directive") || |
| 3908 | parseAbsoluteExpression(DRRegister)) |
| 3909 | return Error(Loc, "expected register number"); |
| 3910 | |
Reid Kleckner | 7bbe711 | 2019-10-19 01:44:09 +0000 | [diff] [blame] | 3911 | codeview::DefRangeRegisterHeader DRHdr; |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3912 | DRHdr.Register = DRRegister; |
| 3913 | DRHdr.MayHaveNoName = 0; |
| 3914 | getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); |
| 3915 | break; |
| 3916 | } |
| 3917 | case CVDR_DEFRANGE_FRAMEPOINTER_REL: { |
| 3918 | int64_t DROffset; |
| 3919 | if (parseToken(AsmToken::Comma, |
| 3920 | "expected comma before offset in .cv_def_range directive") || |
| 3921 | parseAbsoluteExpression(DROffset)) |
| 3922 | return Error(Loc, "expected offset value"); |
| 3923 | |
Reid Kleckner | 7bbe711 | 2019-10-19 01:44:09 +0000 | [diff] [blame] | 3924 | codeview::DefRangeFramePointerRelHeader DRHdr; |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3925 | DRHdr.Offset = DROffset; |
| 3926 | getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); |
| 3927 | break; |
| 3928 | } |
| 3929 | case CVDR_DEFRANGE_SUBFIELD_REGISTER: { |
| 3930 | int64_t DRRegister; |
| 3931 | int64_t DROffsetInParent; |
| 3932 | if (parseToken(AsmToken::Comma, "expected comma before register number in " |
| 3933 | ".cv_def_range directive") || |
| 3934 | parseAbsoluteExpression(DRRegister)) |
| 3935 | return Error(Loc, "expected register number"); |
| 3936 | if (parseToken(AsmToken::Comma, |
| 3937 | "expected comma before offset in .cv_def_range directive") || |
| 3938 | parseAbsoluteExpression(DROffsetInParent)) |
| 3939 | return Error(Loc, "expected offset value"); |
| 3940 | |
Reid Kleckner | 7bbe711 | 2019-10-19 01:44:09 +0000 | [diff] [blame] | 3941 | codeview::DefRangeSubfieldRegisterHeader DRHdr; |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3942 | DRHdr.Register = DRRegister; |
| 3943 | DRHdr.MayHaveNoName = 0; |
| 3944 | DRHdr.OffsetInParent = DROffsetInParent; |
| 3945 | getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); |
| 3946 | break; |
| 3947 | } |
| 3948 | case CVDR_DEFRANGE_REGISTER_REL: { |
| 3949 | int64_t DRRegister; |
| 3950 | int64_t DRFlags; |
| 3951 | int64_t DRBasePointerOffset; |
| 3952 | if (parseToken(AsmToken::Comma, "expected comma before register number in " |
| 3953 | ".cv_def_range directive") || |
| 3954 | parseAbsoluteExpression(DRRegister)) |
| 3955 | return Error(Loc, "expected register value"); |
| 3956 | if (parseToken( |
| 3957 | AsmToken::Comma, |
| 3958 | "expected comma before flag value in .cv_def_range directive") || |
| 3959 | parseAbsoluteExpression(DRFlags)) |
| 3960 | return Error(Loc, "expected flag value"); |
| 3961 | if (parseToken(AsmToken::Comma, "expected comma before base pointer offset " |
| 3962 | "in .cv_def_range directive") || |
| 3963 | parseAbsoluteExpression(DRBasePointerOffset)) |
| 3964 | return Error(Loc, "expected base pointer offset value"); |
| 3965 | |
Reid Kleckner | 7bbe711 | 2019-10-19 01:44:09 +0000 | [diff] [blame] | 3966 | codeview::DefRangeRegisterRelHeader DRHdr; |
Nilanjana Basu | da60fc8 | 2019-08-05 14:16:58 +0000 | [diff] [blame] | 3967 | DRHdr.Register = DRRegister; |
| 3968 | DRHdr.Flags = DRFlags; |
| 3969 | DRHdr.BasePointerOffset = DRBasePointerOffset; |
| 3970 | getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); |
| 3971 | break; |
| 3972 | } |
| 3973 | default: |
| 3974 | return Error(Loc, "unexpected def_range type in .cv_def_range directive"); |
| 3975 | } |
| 3976 | return true; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 3979 | /// parseDirectiveCVString |
| 3980 | /// ::= .cv_stringtable "string" |
| 3981 | bool AsmParser::parseDirectiveCVString() { |
| 3982 | std::string Data; |
| 3983 | if (checkForValidSection() || parseEscapedString(Data)) |
| 3984 | return addErrorSuffix(" in '.cv_string' directive"); |
| 3985 | |
| 3986 | // Put the string in the table and emit the offset. |
| 3987 | std::pair<StringRef, unsigned> Insertion = |
| 3988 | getCVContext().addToStringTable(Data); |
| 3989 | getStreamer().EmitIntValue(Insertion.second, 4); |
| 3990 | return false; |
| 3991 | } |
| 3992 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3993 | /// parseDirectiveCVStringTable |
| 3994 | /// ::= .cv_stringtable |
| 3995 | bool AsmParser::parseDirectiveCVStringTable() { |
| 3996 | getStreamer().EmitCVStringTableDirective(); |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
| 4000 | /// parseDirectiveCVFileChecksums |
| 4001 | /// ::= .cv_filechecksums |
| 4002 | bool AsmParser::parseDirectiveCVFileChecksums() { |
| 4003 | getStreamer().EmitCVFileChecksumsDirective(); |
| 4004 | return false; |
| 4005 | } |
| 4006 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 4007 | /// parseDirectiveCVFileChecksumOffset |
| 4008 | /// ::= .cv_filechecksumoffset fileno |
| 4009 | bool AsmParser::parseDirectiveCVFileChecksumOffset() { |
| 4010 | int64_t FileNo; |
| 4011 | if (parseIntToken(FileNo, "expected identifier in directive")) |
| 4012 | return true; |
| 4013 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 4014 | return true; |
| 4015 | getStreamer().EmitCVFileChecksumOffsetDirective(FileNo); |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 4019 | /// parseDirectiveCVFPOData |
| 4020 | /// ::= .cv_fpo_data procsym |
| 4021 | bool AsmParser::parseDirectiveCVFPOData() { |
| 4022 | SMLoc DirLoc = getLexer().getLoc(); |
| 4023 | StringRef ProcName; |
| 4024 | if (parseIdentifier(ProcName)) |
| 4025 | return TokError("expected symbol name"); |
| 4026 | if (parseEOL("unexpected tokens")) |
| 4027 | return addErrorSuffix(" in '.cv_fpo_data' directive"); |
| 4028 | MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); |
| 4029 | getStreamer().EmitCVFPOData(ProcSym, DirLoc); |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4033 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4034 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4035 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4036 | StringRef Name; |
| 4037 | bool EH = false; |
| 4038 | bool Debug = false; |
| 4039 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4040 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4041 | return TokError("Expected an identifier"); |
| 4042 | |
| 4043 | if (Name == ".eh_frame") |
| 4044 | EH = true; |
| 4045 | else if (Name == ".debug_frame") |
| 4046 | Debug = true; |
| 4047 | |
| 4048 | if (getLexer().is(AsmToken::Comma)) { |
| 4049 | Lex(); |
| 4050 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4051 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4052 | return TokError("Expected an identifier"); |
| 4053 | |
| 4054 | if (Name == ".eh_frame") |
| 4055 | EH = true; |
| 4056 | else if (Name == ".debug_frame") |
| 4057 | Debug = true; |
| 4058 | } |
| 4059 | |
| 4060 | getStreamer().EmitCFISections(EH, Debug); |
| 4061 | return false; |
| 4062 | } |
| 4063 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4064 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 4065 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4066 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 4067 | StringRef Simple; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4068 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 4069 | if (check(parseIdentifier(Simple) || Simple != "simple", |
| 4070 | "unexpected token") || |
| 4071 | parseToken(AsmToken::EndOfStatement)) |
| 4072 | return addErrorSuffix(" in '.cfi_startproc' directive"); |
| 4073 | } |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 4074 | |
Kristina Brooks | 1a41a11 | 2018-10-19 12:14:30 +0000 | [diff] [blame] | 4075 | // TODO(kristina): Deal with a corner case of incorrect diagnostic context |
| 4076 | // being produced if this directive is emitted as part of preprocessor macro |
| 4077 | // expansion which can *ONLY* happen if Clang's cc1as is the API consumer. |
| 4078 | // Tools like llvm-mc on the other hand are not affected by it, and report |
| 4079 | // correct context information. |
| 4080 | getStreamer().EmitCFIStartProc(!Simple.empty(), Lexer.getLoc()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
| 4083 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4084 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4085 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4086 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4087 | getStreamer().EmitCFIEndProc(); |
| 4088 | return false; |
| 4089 | } |
| 4090 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4091 | /// parse register name or number. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4092 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4093 | SMLoc DirectiveLoc) { |
| 4094 | unsigned RegNo; |
| 4095 | |
| 4096 | if (getLexer().isNot(AsmToken::Integer)) { |
| 4097 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 4098 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 4099 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4100 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4101 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4102 | |
| 4103 | return false; |
| 4104 | } |
| 4105 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4106 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4107 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4108 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4109 | int64_t Register = 0, Offset = 0; |
| 4110 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 4111 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4112 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4113 | return true; |
| 4114 | |
| 4115 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 4116 | return false; |
| 4117 | } |
| 4118 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4119 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4120 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4121 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4122 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4123 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4124 | return true; |
| 4125 | |
| 4126 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 4127 | return false; |
| 4128 | } |
| 4129 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4130 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4131 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4132 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4133 | int64_t Register1 = 0, Register2 = 0; |
| 4134 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || |
| 4135 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4136 | parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4137 | return true; |
| 4138 | |
| 4139 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 4140 | return false; |
| 4141 | } |
| 4142 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 4143 | /// parseDirectiveCFIWindowSave |
| 4144 | /// ::= .cfi_window_save |
| 4145 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 4146 | getStreamer().EmitCFIWindowSave(); |
| 4147 | return false; |
| 4148 | } |
| 4149 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4150 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4151 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4152 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4153 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4154 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4155 | return true; |
| 4156 | |
| 4157 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 4158 | return false; |
| 4159 | } |
| 4160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4161 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4162 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4163 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4164 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4165 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4166 | return true; |
| 4167 | |
| 4168 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 4169 | return false; |
| 4170 | } |
| 4171 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4172 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4173 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4174 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4175 | int64_t Register = 0; |
| 4176 | int64_t Offset = 0; |
| 4177 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4178 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 4179 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4180 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4181 | return true; |
| 4182 | |
| 4183 | getStreamer().EmitCFIOffset(Register, Offset); |
| 4184 | return false; |
| 4185 | } |
| 4186 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4187 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4188 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4189 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4190 | int64_t Register = 0, Offset = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4191 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4192 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 4193 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4194 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4195 | return true; |
| 4196 | |
| 4197 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 4198 | return false; |
| 4199 | } |
| 4200 | |
| 4201 | static bool isValidEncoding(int64_t Encoding) { |
| 4202 | if (Encoding & ~0xff) |
| 4203 | return false; |
| 4204 | |
| 4205 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 4206 | return true; |
| 4207 | |
| 4208 | const unsigned Format = Encoding & 0xf; |
| 4209 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 4210 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 4211 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 4212 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 4213 | return false; |
| 4214 | |
| 4215 | const unsigned Application = Encoding & 0x70; |
| 4216 | if (Application != dwarf::DW_EH_PE_absptr && |
| 4217 | Application != dwarf::DW_EH_PE_pcrel) |
| 4218 | return false; |
| 4219 | |
| 4220 | return true; |
| 4221 | } |
| 4222 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4223 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4224 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 4225 | /// ::= .cfi_personality encoding, [symbol_name] |
| 4226 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4227 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4228 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4229 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4230 | return true; |
| 4231 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 4232 | return false; |
| 4233 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4234 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4235 | if (check(!isValidEncoding(Encoding), "unsupported encoding.") || |
| 4236 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4237 | check(parseIdentifier(Name), "expected identifier in directive")) |
| 4238 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4239 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4240 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4241 | |
| 4242 | if (IsPersonality) |
| 4243 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 4244 | else |
| 4245 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 4246 | return false; |
| 4247 | } |
| 4248 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4249 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4250 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4251 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4252 | getStreamer().EmitCFIRememberState(); |
| 4253 | return false; |
| 4254 | } |
| 4255 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4256 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4257 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4258 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4259 | getStreamer().EmitCFIRestoreState(); |
| 4260 | return false; |
| 4261 | } |
| 4262 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4263 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4264 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4265 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4266 | int64_t Register = 0; |
| 4267 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4268 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4269 | return true; |
| 4270 | |
| 4271 | getStreamer().EmitCFISameValue(Register); |
| 4272 | return false; |
| 4273 | } |
| 4274 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4275 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4276 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4277 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4278 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4279 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4280 | return true; |
| 4281 | |
| 4282 | getStreamer().EmitCFIRestore(Register); |
| 4283 | return false; |
| 4284 | } |
| 4285 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4286 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4287 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4288 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4289 | std::string Values; |
| 4290 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4291 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4292 | return true; |
| 4293 | |
| 4294 | Values.push_back((uint8_t)CurrValue); |
| 4295 | |
| 4296 | while (getLexer().is(AsmToken::Comma)) { |
| 4297 | Lex(); |
| 4298 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4299 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4300 | return true; |
| 4301 | |
| 4302 | Values.push_back((uint8_t)CurrValue); |
| 4303 | } |
| 4304 | |
| 4305 | getStreamer().EmitCFIEscape(Values); |
| 4306 | return false; |
| 4307 | } |
| 4308 | |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 4309 | /// parseDirectiveCFIReturnColumn |
| 4310 | /// ::= .cfi_return_column register |
| 4311 | bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { |
| 4312 | int64_t Register = 0; |
| 4313 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 4314 | return true; |
| 4315 | getStreamer().EmitCFIReturnColumn(Register); |
| 4316 | return false; |
| 4317 | } |
| 4318 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4319 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4320 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4321 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4322 | if (parseToken(AsmToken::EndOfStatement, |
| 4323 | "unexpected token in '.cfi_signal_frame'")) |
| 4324 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4325 | |
| 4326 | getStreamer().EmitCFISignalFrame(); |
| 4327 | return false; |
| 4328 | } |
| 4329 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4330 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4331 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4332 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4333 | int64_t Register = 0; |
| 4334 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4335 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4336 | return true; |
| 4337 | |
| 4338 | getStreamer().EmitCFIUndefined(Register); |
| 4339 | return false; |
| 4340 | } |
| 4341 | |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4342 | /// parseDirectiveAltmacro |
| 4343 | /// ::= .altmacro |
| 4344 | /// ::= .noaltmacro |
| 4345 | bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { |
| 4346 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4347 | return TokError("unexpected token in '" + Directive + "' directive"); |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 4348 | AltMacroMode = (Directive == ".altmacro"); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4349 | return false; |
| 4350 | } |
| 4351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4352 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4353 | /// ::= .macros_on |
| 4354 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4355 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4356 | if (parseToken(AsmToken::EndOfStatement, |
| 4357 | "unexpected token in '" + Directive + "' directive")) |
| 4358 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4359 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4360 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4361 | return false; |
| 4362 | } |
| 4363 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4364 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4365 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4366 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4367 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4368 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4369 | return TokError("expected identifier in '.macro' directive"); |
| 4370 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4371 | if (getLexer().is(AsmToken::Comma)) |
| 4372 | Lex(); |
| 4373 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4374 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4375 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4376 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 4377 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4378 | return Error(Lexer.getLoc(), |
| 4379 | "Vararg parameter '" + Parameters.back().Name + |
| 4380 | "' should be last one in the list of parameters."); |
| 4381 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4382 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4383 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4384 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4385 | |
Coby Tayree | bedaae0 | 2017-04-08 20:29:03 +0000 | [diff] [blame] | 4386 | // Emit an error if two (or more) named parameters share the same name |
| 4387 | for (const MCAsmMacroParameter& CurrParam : Parameters) |
| 4388 | if (CurrParam.Name.equals(Parameter.Name)) |
| 4389 | return TokError("macro '" + Name + "' has multiple parameters" |
| 4390 | " named '" + Parameter.Name + "'"); |
| 4391 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4392 | if (Lexer.is(AsmToken::Colon)) { |
| 4393 | Lex(); // consume ':' |
| 4394 | |
| 4395 | SMLoc QualLoc; |
| 4396 | StringRef Qualifier; |
| 4397 | |
| 4398 | QualLoc = Lexer.getLoc(); |
| 4399 | if (parseIdentifier(Qualifier)) |
| 4400 | return Error(QualLoc, "missing parameter qualifier for " |
| 4401 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4402 | |
| 4403 | if (Qualifier == "req") |
| 4404 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 4405 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4406 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4407 | else |
| 4408 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 4409 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4410 | } |
| 4411 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4412 | if (getLexer().is(AsmToken::Equal)) { |
| 4413 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4414 | |
| 4415 | SMLoc ParamLoc; |
| 4416 | |
| 4417 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4418 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4419 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4420 | |
| 4421 | if (Parameter.Required) |
| 4422 | Warning(ParamLoc, "pointless default value for required parameter " |
| 4423 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4424 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4425 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4426 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4427 | |
| 4428 | if (getLexer().is(AsmToken::Comma)) |
| 4429 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4432 | // Eat just the end of statement. |
| 4433 | Lexer.Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4434 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4435 | // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4436 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4437 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4438 | // Lex the macro definition. |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 4439 | while (true) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4440 | // Ignore Lexing errors in macros. |
| 4441 | while (Lexer.is(AsmToken::Error)) { |
| 4442 | Lexer.Lex(); |
| 4443 | } |
| 4444 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4445 | // Check whether we have reached the end of the file. |
| 4446 | if (getLexer().is(AsmToken::Eof)) |
| 4447 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 4448 | |
| 4449 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4450 | if (getLexer().is(AsmToken::Identifier)) { |
| 4451 | if (getTok().getIdentifier() == ".endm" || |
| 4452 | getTok().getIdentifier() == ".endmacro") { |
| 4453 | if (MacroDepth == 0) { // Outermost macro. |
| 4454 | EndToken = getTok(); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4455 | Lexer.Lex(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4456 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4457 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 4458 | "' directive"); |
| 4459 | break; |
| 4460 | } else { |
| 4461 | // Otherwise we just found the end of an inner macro. |
| 4462 | --MacroDepth; |
| 4463 | } |
| 4464 | } else if (getTok().getIdentifier() == ".macro") { |
| 4465 | // We allow nested macros. Those aren't instantiated until the outermost |
| 4466 | // macro is expanded so just ignore them for now. |
| 4467 | ++MacroDepth; |
| 4468 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4472 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4475 | if (getContext().lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4476 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 4477 | } |
| 4478 | |
| 4479 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4480 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4481 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4482 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4483 | MCAsmMacro Macro(Name, Body, std::move(Parameters)); |
| 4484 | DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n"; |
| 4485 | Macro.dump()); |
| 4486 | getContext().defineMacro(Name, std::move(Macro)); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4487 | return false; |
| 4488 | } |
| 4489 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4490 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4491 | /// |
| 4492 | /// With the support added for named parameters there may be code out there that |
| 4493 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4494 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4495 | /// 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] | 4496 | /// 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] | 4497 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 4498 | /// warning that the positional parameter found in body which have no effect. |
| 4499 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4500 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4501 | /// intended or change the macro to use the named parameters. It is possible |
| 4502 | /// this warning will trigger when the none of the named parameters are used |
| 4503 | /// 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] | 4504 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4505 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4506 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4507 | // If this macro is not defined with named parameters the warning we are |
| 4508 | // checking for here doesn't apply. |
| 4509 | unsigned NParameters = Parameters.size(); |
| 4510 | if (NParameters == 0) |
| 4511 | return; |
| 4512 | |
| 4513 | bool NamedParametersFound = false; |
| 4514 | bool PositionalParametersFound = false; |
| 4515 | |
| 4516 | // Look at the body of the macro for use of both the named parameters and what |
| 4517 | // are likely to be positional parameters. This is what expandMacro() is |
| 4518 | // doing when it finds the parameters in the body. |
| 4519 | while (!Body.empty()) { |
| 4520 | // Scan for the next possible parameter. |
| 4521 | std::size_t End = Body.size(), Pos = 0; |
| 4522 | for (; Pos != End; ++Pos) { |
| 4523 | // Check for a substitution or escape. |
| 4524 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 4525 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 4526 | break; |
| 4527 | |
| 4528 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 4529 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 4530 | continue; |
| 4531 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 4532 | if (Next == '$' || Next == 'n' || |
| 4533 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4534 | break; |
| 4535 | } |
| 4536 | |
| 4537 | // Check if we reached the end. |
| 4538 | if (Pos == End) |
| 4539 | break; |
| 4540 | |
| 4541 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4542 | switch (Body[Pos + 1]) { |
| 4543 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4544 | case '$': |
| 4545 | break; |
| 4546 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4547 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4548 | case 'n': |
| 4549 | PositionalParametersFound = true; |
| 4550 | break; |
| 4551 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4552 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4553 | default: { |
| 4554 | PositionalParametersFound = true; |
| 4555 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4556 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4557 | } |
| 4558 | Pos += 2; |
| 4559 | } else { |
| 4560 | unsigned I = Pos + 1; |
| 4561 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 4562 | ++I; |
| 4563 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4564 | const char *Begin = Body.data() + Pos + 1; |
| 4565 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4566 | unsigned Index = 0; |
| 4567 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4568 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4569 | break; |
| 4570 | |
| 4571 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4572 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 4573 | Pos += 3; |
| 4574 | else { |
| 4575 | Pos = I; |
| 4576 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4577 | } else { |
| 4578 | NamedParametersFound = true; |
| 4579 | Pos += 1 + Argument.size(); |
| 4580 | } |
| 4581 | } |
| 4582 | // Update the scan point. |
| 4583 | Body = Body.substr(Pos); |
| 4584 | } |
| 4585 | |
| 4586 | if (!NamedParametersFound && PositionalParametersFound) |
| 4587 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 4588 | "used in macro body, possible positional parameter " |
| 4589 | "found in body which will have no effect"); |
| 4590 | } |
| 4591 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4592 | /// parseDirectiveExitMacro |
| 4593 | /// ::= .exitm |
| 4594 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4595 | if (parseToken(AsmToken::EndOfStatement, |
| 4596 | "unexpected token in '" + Directive + "' directive")) |
| 4597 | return true; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4598 | |
| 4599 | if (!isInsideMacroInstantiation()) |
| 4600 | return TokError("unexpected '" + Directive + "' in file, " |
| 4601 | "no current macro definition"); |
| 4602 | |
| 4603 | // Exit all conditionals that are active in the current macro. |
| 4604 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 4605 | TheCondState = TheCondStack.back(); |
| 4606 | TheCondStack.pop_back(); |
| 4607 | } |
| 4608 | |
| 4609 | handleMacroExit(); |
| 4610 | return false; |
| 4611 | } |
| 4612 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4613 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4614 | /// ::= .endm |
| 4615 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4616 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4617 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4618 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4619 | |
| 4620 | // If we are inside a macro instantiation, terminate the current |
| 4621 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4622 | if (isInsideMacroInstantiation()) { |
| 4623 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4624 | return false; |
| 4625 | } |
| 4626 | |
| 4627 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 4628 | // .endmacro directives are handled during the macro definition parsing. |
| 4629 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4630 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4633 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4634 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4635 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4636 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4637 | SMLoc Loc; |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 4638 | if (parseTokenLoc(Loc) || |
| 4639 | check(parseIdentifier(Name), Loc, |
| 4640 | "expected identifier in '.purgem' directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4641 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4642 | "unexpected token in '.purgem' directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4643 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4644 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4645 | if (!getContext().lookupMacro(Name)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4646 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 4647 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4648 | getContext().undefineMacro(Name); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4649 | DEBUG_WITH_TYPE("asm-macros", dbgs() |
| 4650 | << "Un-defining macro: " << Name << "\n"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4651 | return false; |
| 4652 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4653 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4654 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4655 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4656 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4657 | // Expect a single argument: an expression that evaluates to a constant |
| 4658 | // in the inclusive range 0-30. |
| 4659 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4660 | int64_t AlignSizePow2; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4661 | if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4662 | parseToken(AsmToken::EndOfStatement, "unexpected token after expression " |
| 4663 | "in '.bundle_align_mode' " |
| 4664 | "directive") || |
| 4665 | check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc, |
| 4666 | "invalid bundle alignment size (expected between 0 and 30)")) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4667 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4668 | |
| 4669 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 4670 | // unsigned. |
| 4671 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 4672 | return false; |
| 4673 | } |
| 4674 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4675 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4676 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4677 | bool AsmParser::parseDirectiveBundleLock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4678 | if (checkForValidSection()) |
| 4679 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4680 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4681 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4682 | StringRef Option; |
| 4683 | SMLoc Loc = getTok().getLoc(); |
| 4684 | const char *kInvalidOptionError = |
| 4685 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4686 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4687 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4688 | if (check(parseIdentifier(Option), Loc, kInvalidOptionError) || |
| 4689 | check(Option != "align_to_end", Loc, kInvalidOptionError) || |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4690 | parseToken(AsmToken::EndOfStatement, |
| 4691 | "unexpected token after '.bundle_lock' directive option")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4692 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4693 | AlignToEnd = true; |
| 4694 | } |
| 4695 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4696 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4697 | return false; |
| 4698 | } |
| 4699 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4700 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4701 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4702 | bool AsmParser::parseDirectiveBundleUnlock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4703 | if (checkForValidSection() || |
| 4704 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4705 | "unexpected token in '.bundle_unlock' directive")) |
| 4706 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4707 | |
| 4708 | getStreamer().EmitBundleUnlock(); |
| 4709 | return false; |
| 4710 | } |
| 4711 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4712 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4713 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4714 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 4715 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 4716 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4717 | if (checkForValidSection() || parseExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4718 | return true; |
| 4719 | |
| 4720 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4721 | if (parseOptionalToken(AsmToken::Comma)) |
| 4722 | if (parseAbsoluteExpression(FillExpr)) |
| 4723 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
| 4724 | if (parseToken(AsmToken::EndOfStatement)) |
| 4725 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4726 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4727 | // 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] | 4728 | getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4729 | |
| 4730 | return false; |
| 4731 | } |
| 4732 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4733 | /// parseDirectiveDCB |
| 4734 | /// ::= .dcb.{b, l, w} expression, expression |
| 4735 | bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4736 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4737 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4738 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4739 | return true; |
| 4740 | |
| 4741 | if (NumValues < 0) { |
| 4742 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4743 | return false; |
| 4744 | } |
| 4745 | |
| 4746 | if (parseToken(AsmToken::Comma, |
| 4747 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4748 | return true; |
| 4749 | |
| 4750 | const MCExpr *Value; |
| 4751 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4752 | if (parseExpression(Value)) |
| 4753 | return true; |
| 4754 | |
| 4755 | // Special case constant expressions to match code generator. |
| 4756 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 4757 | assert(Size <= 8 && "Invalid size"); |
| 4758 | uint64_t IntValue = MCE->getValue(); |
| 4759 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 4760 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4761 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4762 | getStreamer().EmitIntValue(IntValue, Size); |
| 4763 | } else { |
| 4764 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4765 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 4766 | } |
| 4767 | |
| 4768 | if (parseToken(AsmToken::EndOfStatement, |
| 4769 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4770 | return true; |
| 4771 | |
| 4772 | return false; |
| 4773 | } |
| 4774 | |
| 4775 | /// parseDirectiveRealDCB |
| 4776 | /// ::= .dcb.{d, s} expression, expression |
| 4777 | bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4778 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4779 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4780 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4781 | return true; |
| 4782 | |
| 4783 | if (NumValues < 0) { |
| 4784 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4785 | return false; |
| 4786 | } |
| 4787 | |
| 4788 | if (parseToken(AsmToken::Comma, |
| 4789 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4790 | return true; |
| 4791 | |
| 4792 | APInt AsInt; |
| 4793 | if (parseRealValue(Semantics, AsInt)) |
| 4794 | return true; |
| 4795 | |
| 4796 | if (parseToken(AsmToken::EndOfStatement, |
| 4797 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4798 | return true; |
| 4799 | |
| 4800 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4801 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 4802 | AsInt.getBitWidth() / 8); |
| 4803 | |
| 4804 | return false; |
| 4805 | } |
| 4806 | |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4807 | /// parseDirectiveDS |
| 4808 | /// ::= .ds.{b, d, l, p, s, w, x} expression |
| 4809 | bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4810 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4811 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4812 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4813 | return true; |
| 4814 | |
| 4815 | if (NumValues < 0) { |
| 4816 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4817 | return false; |
| 4818 | } |
| 4819 | |
| 4820 | if (parseToken(AsmToken::EndOfStatement, |
| 4821 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4822 | return true; |
| 4823 | |
| 4824 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4825 | getStreamer().emitFill(Size, 0); |
| 4826 | |
| 4827 | return false; |
| 4828 | } |
| 4829 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4830 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4831 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4832 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4833 | if (checkForValidSection()) |
| 4834 | return true; |
| 4835 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4836 | auto parseOp = [&]() -> bool { |
| 4837 | const MCExpr *Value; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4838 | if (parseExpression(Value)) |
| 4839 | return true; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4840 | if (Signed) |
| 4841 | getStreamer().EmitSLEB128Value(Value); |
| 4842 | else |
| 4843 | getStreamer().EmitULEB128Value(Value); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4844 | return false; |
| 4845 | }; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4846 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4847 | if (parseMany(parseOp)) |
| 4848 | return addErrorSuffix(" in directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4849 | |
| 4850 | return false; |
| 4851 | } |
| 4852 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4853 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4854 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4855 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4856 | auto parseOp = [&]() -> bool { |
| 4857 | StringRef Name; |
| 4858 | SMLoc Loc = getTok().getLoc(); |
| 4859 | if (parseIdentifier(Name)) |
| 4860 | return Error(Loc, "expected identifier"); |
| 4861 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4862 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4863 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 4864 | if (Sym->isTemporary()) |
| 4865 | return Error(Loc, "non-local symbol required"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4866 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4867 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 4868 | return Error(Loc, "unable to emit symbol attribute"); |
| 4869 | return false; |
| 4870 | }; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4871 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4872 | if (parseMany(parseOp)) |
| 4873 | return addErrorSuffix(" in directive"); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 4874 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4875 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4876 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4877 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4878 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4879 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4880 | if (checkForValidSection()) |
| 4881 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 4882 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4883 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4884 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4885 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4886 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4887 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 4888 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4889 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4890 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4891 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4892 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4893 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4894 | |
| 4895 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4896 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4897 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4898 | return true; |
| 4899 | |
| 4900 | int64_t Pow2Alignment = 0; |
| 4901 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4902 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4903 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4904 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4905 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4906 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4907 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4908 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 4909 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4910 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 4911 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4912 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4913 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 4914 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4915 | if (!isPowerOf2_64(Pow2Alignment)) |
| 4916 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 4917 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 4918 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4919 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4920 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4921 | if (parseToken(AsmToken::EndOfStatement, |
| 4922 | "unexpected token in '.comm' or '.lcomm' directive")) |
| 4923 | return true; |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4924 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4925 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 4926 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4927 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4928 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4929 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4930 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 4931 | // 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] | 4932 | // may internally end up wanting an alignment in bytes. |
| 4933 | // FIXME: Diagnose overflow. |
| 4934 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4935 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4936 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4937 | |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 4938 | Sym->redefineIfPossible(); |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 4939 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4940 | return Error(IDLoc, "invalid symbol redefinition"); |
| 4941 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4942 | // 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] | 4943 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4944 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 4945 | return false; |
| 4946 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4947 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4948 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4949 | return false; |
| 4950 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 4951 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4952 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4953 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4954 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4955 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4956 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4957 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4958 | StringRef Str = parseStringToEndOfStatement(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4959 | if (parseToken(AsmToken::EndOfStatement, |
| 4960 | "unexpected token in '.abort' directive")) |
| 4961 | return true; |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4962 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4963 | if (Str.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4964 | return Error(Loc, ".abort detected. Assembly stopping."); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4965 | else |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4966 | return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4967 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4968 | |
| 4969 | return false; |
| 4970 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 4971 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4972 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4973 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4974 | bool AsmParser::parseDirectiveInclude() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4975 | // Allow the strings to have escaped octal character sequence. |
| 4976 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4977 | SMLoc IncludeLoc = getTok().getLoc(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4978 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4979 | if (check(getTok().isNot(AsmToken::String), |
| 4980 | "expected string in '.include' directive") || |
| 4981 | parseEscapedString(Filename) || |
| 4982 | check(getTok().isNot(AsmToken::EndOfStatement), |
| 4983 | "unexpected token in '.include' directive") || |
| 4984 | // Attempt to switch the lexer to the included file before consuming the |
| 4985 | // end of statement to avoid losing it when we switch. |
| 4986 | check(enterIncludeFile(Filename), IncludeLoc, |
| 4987 | "Could not find include file '" + Filename + "'")) |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 4988 | return true; |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4989 | |
| 4990 | return false; |
| 4991 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 4992 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4993 | /// parseDirectiveIncbin |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4994 | /// ::= .incbin "filename" [ , skip [ , count ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4995 | bool AsmParser::parseDirectiveIncbin() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4996 | // Allow the strings to have escaped octal character sequence. |
| 4997 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4998 | SMLoc IncbinLoc = getTok().getLoc(); |
| 4999 | if (check(getTok().isNot(AsmToken::String), |
| 5000 | "expected string in '.incbin' directive") || |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 5001 | parseEscapedString(Filename)) |
| 5002 | return true; |
| 5003 | |
| 5004 | int64_t Skip = 0; |
| 5005 | const MCExpr *Count = nullptr; |
| 5006 | SMLoc SkipLoc, CountLoc; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5007 | if (parseOptionalToken(AsmToken::Comma)) { |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 5008 | // The skip expression can be omitted while specifying the count, e.g: |
| 5009 | // .incbin "filename",,4 |
| 5010 | if (getTok().isNot(AsmToken::Comma)) { |
| 5011 | if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip)) |
| 5012 | return true; |
| 5013 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5014 | if (parseOptionalToken(AsmToken::Comma)) { |
| 5015 | CountLoc = getTok().getLoc(); |
| 5016 | if (parseExpression(Count)) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 5017 | return true; |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 5022 | "unexpected token in '.incbin' directive")) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 5023 | return true; |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 5024 | |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 5025 | if (check(Skip < 0, SkipLoc, "skip is negative")) |
| 5026 | return true; |
| 5027 | |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 5028 | // Attempt to process the included file. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 5029 | if (processIncbinFile(Filename, Skip, Count, CountLoc)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 5030 | return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 5031 | return false; |
| 5032 | } |
| 5033 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5034 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5035 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 5036 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5037 | TheCondStack.push_back(TheCondState); |
| 5038 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 5039 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5040 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 5041 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5042 | int64_t ExprValue; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5043 | if (parseAbsoluteExpression(ExprValue) || |
| 5044 | parseToken(AsmToken::EndOfStatement, |
| 5045 | "unexpected token in '.if' directive")) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5046 | return true; |
| 5047 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5048 | switch (DirKind) { |
| 5049 | default: |
| 5050 | llvm_unreachable("unsupported directive"); |
| 5051 | case DK_IF: |
| 5052 | case DK_IFNE: |
| 5053 | break; |
| 5054 | case DK_IFEQ: |
| 5055 | ExprValue = ExprValue == 0; |
| 5056 | break; |
| 5057 | case DK_IFGE: |
| 5058 | ExprValue = ExprValue >= 0; |
| 5059 | break; |
| 5060 | case DK_IFGT: |
| 5061 | ExprValue = ExprValue > 0; |
| 5062 | break; |
| 5063 | case DK_IFLE: |
| 5064 | ExprValue = ExprValue <= 0; |
| 5065 | break; |
| 5066 | case DK_IFLT: |
| 5067 | ExprValue = ExprValue < 0; |
| 5068 | break; |
| 5069 | } |
| 5070 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5071 | TheCondState.CondMet = ExprValue; |
| 5072 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5073 | } |
| 5074 | |
| 5075 | return false; |
| 5076 | } |
| 5077 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5078 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 5079 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5080 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 5081 | TheCondStack.push_back(TheCondState); |
| 5082 | TheCondState.TheCond = AsmCond::IfCond; |
| 5083 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 5084 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5085 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 5086 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5087 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 5088 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5089 | if (parseToken(AsmToken::EndOfStatement, |
| 5090 | "unexpected token in '.ifb' directive")) |
| 5091 | return true; |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 5092 | |
| 5093 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 5094 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5095 | } |
| 5096 | |
| 5097 | return false; |
| 5098 | } |
| 5099 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5100 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5101 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 5102 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5103 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5104 | TheCondStack.push_back(TheCondState); |
| 5105 | TheCondState.TheCond = AsmCond::IfCond; |
| 5106 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 5107 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5108 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5109 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5110 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5111 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5112 | if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive")) |
| 5113 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5114 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5115 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5116 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5117 | if (parseToken(AsmToken::EndOfStatement, |
| 5118 | "unexpected token in '.ifc' directive")) |
| 5119 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5120 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 5121 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5122 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5123 | } |
| 5124 | |
| 5125 | return false; |
| 5126 | } |
| 5127 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5128 | /// parseDirectiveIfeqs |
| 5129 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5130 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5131 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5132 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5133 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 5134 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | StringRef String1 = getTok().getStringContents(); |
| 5138 | Lex(); |
| 5139 | |
| 5140 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5141 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5142 | return TokError( |
| 5143 | "expected comma after first string for '.ifeqs' directive"); |
| 5144 | return TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | Lex(); |
| 5148 | |
| 5149 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5150 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5151 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 5152 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | StringRef String2 = getTok().getStringContents(); |
| 5156 | Lex(); |
| 5157 | |
| 5158 | TheCondStack.push_back(TheCondState); |
| 5159 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5160 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5161 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5162 | |
| 5163 | return false; |
| 5164 | } |
| 5165 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5166 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5167 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5168 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5169 | StringRef Name; |
| 5170 | TheCondStack.push_back(TheCondState); |
| 5171 | TheCondState.TheCond = AsmCond::IfCond; |
| 5172 | |
| 5173 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5174 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5175 | } else { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5176 | if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") || |
| 5177 | parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'")) |
| 5178 | return true; |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5179 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 5180 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5181 | |
| 5182 | if (expect_defined) |
Scott Linder | b5d6292 | 2019-01-28 19:32:08 +0000 | [diff] [blame] | 5183 | TheCondState.CondMet = (Sym && !Sym->isUndefined(false)); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5184 | else |
Scott Linder | b5d6292 | 2019-01-28 19:32:08 +0000 | [diff] [blame] | 5185 | TheCondState.CondMet = (!Sym || Sym->isUndefined(false)); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5186 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5187 | } |
| 5188 | |
| 5189 | return false; |
| 5190 | } |
| 5191 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5192 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5193 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5194 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5195 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 5196 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5197 | return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an" |
| 5198 | " .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5199 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 5200 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5201 | bool LastIgnoreState = false; |
| 5202 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 5203 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5204 | if (LastIgnoreState || TheCondState.CondMet) { |
| 5205 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5206 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 5207 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5208 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5209 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5210 | return true; |
| 5211 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5212 | if (parseToken(AsmToken::EndOfStatement, |
| 5213 | "unexpected token in '.elseif' directive")) |
| 5214 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 5215 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5216 | TheCondState.CondMet = ExprValue; |
| 5217 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5218 | } |
| 5219 | |
| 5220 | return false; |
| 5221 | } |
| 5222 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5223 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5224 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5225 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5226 | if (parseToken(AsmToken::EndOfStatement, |
| 5227 | "unexpected token in '.else' directive")) |
| 5228 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5229 | |
| 5230 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 5231 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5232 | return Error(DirectiveLoc, "Encountered a .else that doesn't follow " |
| 5233 | " an .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5234 | TheCondState.TheCond = AsmCond::ElseCond; |
| 5235 | bool LastIgnoreState = false; |
| 5236 | if (!TheCondStack.empty()) |
| 5237 | LastIgnoreState = TheCondStack.back().Ignore; |
| 5238 | if (LastIgnoreState || TheCondState.CondMet) |
| 5239 | TheCondState.Ignore = true; |
| 5240 | else |
| 5241 | TheCondState.Ignore = false; |
| 5242 | |
| 5243 | return false; |
| 5244 | } |
| 5245 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5246 | /// parseDirectiveEnd |
| 5247 | /// ::= .end |
| 5248 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5249 | if (parseToken(AsmToken::EndOfStatement, |
| 5250 | "unexpected token in '.end' directive")) |
| 5251 | return true; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5252 | |
| 5253 | while (Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5254 | Lexer.Lex(); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5255 | |
| 5256 | return false; |
| 5257 | } |
| 5258 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5259 | /// parseDirectiveError |
| 5260 | /// ::= .err |
| 5261 | /// ::= .error [string] |
| 5262 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 5263 | if (!TheCondStack.empty()) { |
| 5264 | if (TheCondStack.back().Ignore) { |
| 5265 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5266 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | if (!WithMessage) |
| 5271 | return Error(L, ".err encountered"); |
| 5272 | |
| 5273 | StringRef Message = ".error directive invoked in source file"; |
| 5274 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5275 | if (Lexer.isNot(AsmToken::String)) |
| 5276 | return TokError(".error argument must be a string"); |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5277 | |
| 5278 | Message = getTok().getStringContents(); |
| 5279 | Lex(); |
| 5280 | } |
| 5281 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5282 | return Error(L, Message); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5285 | /// parseDirectiveWarning |
| 5286 | /// ::= .warning [string] |
| 5287 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 5288 | if (!TheCondStack.empty()) { |
| 5289 | if (TheCondStack.back().Ignore) { |
| 5290 | eatToEndOfStatement(); |
| 5291 | return false; |
| 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | StringRef Message = ".warning directive invoked in source file"; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5296 | |
| 5297 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5298 | if (Lexer.isNot(AsmToken::String)) |
| 5299 | return TokError(".warning argument must be a string"); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5300 | |
| 5301 | Message = getTok().getStringContents(); |
| 5302 | Lex(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5303 | if (parseToken(AsmToken::EndOfStatement, |
| 5304 | "expected end of statement in '.warning' directive")) |
| 5305 | return true; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5308 | return Warning(L, Message); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5311 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5312 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5313 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5314 | if (parseToken(AsmToken::EndOfStatement, |
| 5315 | "unexpected token in '.endif' directive")) |
| 5316 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5317 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5318 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5319 | return Error(DirectiveLoc, "Encountered a .endif that doesn't follow " |
| 5320 | "an .if or .else"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5321 | if (!TheCondStack.empty()) { |
| 5322 | TheCondState = TheCondStack.back(); |
| 5323 | TheCondStack.pop_back(); |
| 5324 | } |
| 5325 | |
| 5326 | return false; |
| 5327 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 5328 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5329 | void AsmParser::initializeDirectiveKindMap() { |
| 5330 | DirectiveKindMap[".set"] = DK_SET; |
| 5331 | DirectiveKindMap[".equ"] = DK_EQU; |
| 5332 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 5333 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 5334 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 5335 | DirectiveKindMap[".string"] = DK_STRING; |
| 5336 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 5337 | DirectiveKindMap[".short"] = DK_SHORT; |
| 5338 | DirectiveKindMap[".value"] = DK_VALUE; |
| 5339 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 5340 | DirectiveKindMap[".long"] = DK_LONG; |
| 5341 | DirectiveKindMap[".int"] = DK_INT; |
| 5342 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 5343 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 5344 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 5345 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5346 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 5347 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 5348 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 5349 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 5350 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 5351 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 5352 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 5353 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 5354 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 5355 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 5356 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 5357 | DirectiveKindMap[".org"] = DK_ORG; |
| 5358 | DirectiveKindMap[".fill"] = DK_FILL; |
| 5359 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 5360 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 5361 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 5362 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5363 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 5364 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 5365 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 5366 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 5367 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 5368 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 5369 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 5370 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
Vedant Kumar | 13ef84f | 2019-01-25 18:30:22 +0000 | [diff] [blame] | 5371 | DirectiveKindMap[".cold"] = DK_COLD; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5372 | DirectiveKindMap[".comm"] = DK_COMM; |
| 5373 | DirectiveKindMap[".common"] = DK_COMMON; |
| 5374 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 5375 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 5376 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 5377 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 5378 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 5379 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 5380 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5381 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5382 | DirectiveKindMap[".irp"] = DK_IRP; |
| 5383 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 5384 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 5385 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 5386 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 5387 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 5388 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5389 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 5390 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 5391 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 5392 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 5393 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 5394 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5395 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 5396 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 5397 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5398 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5399 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5400 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5401 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 5402 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 5403 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 5404 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 5405 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5406 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5407 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 5408 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 5409 | DirectiveKindMap[".space"] = DK_SPACE; |
| 5410 | DirectiveKindMap[".file"] = DK_FILE; |
| 5411 | DirectiveKindMap[".line"] = DK_LINE; |
| 5412 | DirectiveKindMap[".loc"] = DK_LOC; |
| 5413 | DirectiveKindMap[".stabs"] = DK_STABS; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5414 | DirectiveKindMap[".cv_file"] = DK_CV_FILE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5415 | DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5416 | DirectiveKindMap[".cv_loc"] = DK_CV_LOC; |
| 5417 | DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 5418 | DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5419 | DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 5420 | DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE; |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 5421 | DirectiveKindMap[".cv_string"] = DK_CV_STRING; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5422 | DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE; |
| 5423 | DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 5424 | DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET; |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 5425 | DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5426 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 5427 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 5428 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 5429 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 5430 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 5431 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 5432 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 5433 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 5434 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 5435 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 5436 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 5437 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 5438 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 5439 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 5440 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 5441 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 5442 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 5443 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 5444 | DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5445 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 5446 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 5447 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 5448 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Luke Cheeseman | 41a9e53 | 2018-12-21 10:45:08 +0000 | [diff] [blame] | 5449 | DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5450 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 5451 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 5452 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 5453 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5454 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 5455 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 5456 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5457 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5458 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5459 | DirectiveKindMap[".warning"] = DK_WARNING; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 5460 | DirectiveKindMap[".altmacro"] = DK_ALTMACRO; |
| 5461 | DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 5462 | DirectiveKindMap[".reloc"] = DK_RELOC; |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 5463 | DirectiveKindMap[".dc"] = DK_DC; |
| 5464 | DirectiveKindMap[".dc.a"] = DK_DC_A; |
| 5465 | DirectiveKindMap[".dc.b"] = DK_DC_B; |
| 5466 | DirectiveKindMap[".dc.d"] = DK_DC_D; |
| 5467 | DirectiveKindMap[".dc.l"] = DK_DC_L; |
| 5468 | DirectiveKindMap[".dc.s"] = DK_DC_S; |
| 5469 | DirectiveKindMap[".dc.w"] = DK_DC_W; |
| 5470 | DirectiveKindMap[".dc.x"] = DK_DC_X; |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 5471 | DirectiveKindMap[".dcb"] = DK_DCB; |
| 5472 | DirectiveKindMap[".dcb.b"] = DK_DCB_B; |
| 5473 | DirectiveKindMap[".dcb.d"] = DK_DCB_D; |
| 5474 | DirectiveKindMap[".dcb.l"] = DK_DCB_L; |
| 5475 | DirectiveKindMap[".dcb.s"] = DK_DCB_S; |
| 5476 | DirectiveKindMap[".dcb.w"] = DK_DCB_W; |
| 5477 | DirectiveKindMap[".dcb.x"] = DK_DCB_X; |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 5478 | DirectiveKindMap[".ds"] = DK_DS; |
| 5479 | DirectiveKindMap[".ds.b"] = DK_DS_B; |
| 5480 | DirectiveKindMap[".ds.d"] = DK_DS_D; |
| 5481 | DirectiveKindMap[".ds.l"] = DK_DS_L; |
| 5482 | DirectiveKindMap[".ds.p"] = DK_DS_P; |
| 5483 | DirectiveKindMap[".ds.s"] = DK_DS_S; |
| 5484 | DirectiveKindMap[".ds.w"] = DK_DS_W; |
| 5485 | DirectiveKindMap[".ds.x"] = DK_DS_X; |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5486 | DirectiveKindMap[".print"] = DK_PRINT; |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 5487 | DirectiveKindMap[".addrsig"] = DK_ADDRSIG; |
| 5488 | DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5491 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5492 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5493 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5494 | unsigned NestLevel = 0; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5495 | while (true) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5496 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5497 | if (getLexer().is(AsmToken::Eof)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5498 | printError(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5499 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5502 | if (Lexer.is(AsmToken::Identifier) && |
Nirav Dave | e24fcd5 | 2018-07-18 16:17:03 +0000 | [diff] [blame] | 5503 | (getTok().getIdentifier() == ".rep" || |
| 5504 | getTok().getIdentifier() == ".rept" || |
Nikolay Haustov | 95b4fcd | 2016-03-01 08:18:28 +0000 | [diff] [blame] | 5505 | getTok().getIdentifier() == ".irp" || |
| 5506 | getTok().getIdentifier() == ".irpc")) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5507 | ++NestLevel; |
| 5508 | } |
| 5509 | |
| 5510 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5511 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5512 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5513 | EndToken = getTok(); |
| 5514 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5515 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5516 | printError(getTok().getLoc(), |
| 5517 | "unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5518 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5519 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5520 | break; |
| 5521 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5522 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5525 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5526 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 5530 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 5531 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 5532 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5533 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5534 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 5535 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5538 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5539 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5540 | OS << ".endr\n"; |
| 5541 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 5542 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 5543 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5544 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5545 | // Create the macro instantiation object and add to the current macro |
| 5546 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 5547 | MacroInstantiation *MI = new MacroInstantiation( |
| 5548 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5549 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5550 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5551 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 5552 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 5553 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5554 | Lex(); |
| 5555 | } |
| 5556 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5557 | /// parseDirectiveRept |
| 5558 | /// ::= .rep | .rept count |
| 5559 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5560 | const MCExpr *CountExpr; |
| 5561 | SMLoc CountLoc = getTok().getLoc(); |
| 5562 | if (parseExpression(CountExpr)) |
| 5563 | return true; |
| 5564 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5565 | int64_t Count; |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 5566 | if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5567 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 5568 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5569 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5570 | if (check(Count < 0, CountLoc, "Count is negative") || |
| 5571 | parseToken(AsmToken::EndOfStatement, |
| 5572 | "unexpected token in '" + Dir + "' directive")) |
| 5573 | return true; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5574 | |
| 5575 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5576 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5577 | if (!M) |
| 5578 | return true; |
| 5579 | |
| 5580 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5581 | // to hold the macro body with substitutions. |
| 5582 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5583 | raw_svector_ostream OS(Buf); |
| 5584 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5585 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 5586 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5587 | return true; |
| 5588 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5589 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5590 | |
| 5591 | return false; |
| 5592 | } |
| 5593 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5594 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5595 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5596 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5597 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5598 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5599 | if (check(parseIdentifier(Parameter.Name), |
| 5600 | "expected identifier in '.irp' directive") || |
| 5601 | parseToken(AsmToken::Comma, "expected comma in '.irp' directive") || |
| 5602 | parseMacroArguments(nullptr, A) || |
| 5603 | parseToken(AsmToken::EndOfStatement, "expected End of Statement")) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5604 | return true; |
| 5605 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5606 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5607 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5608 | if (!M) |
| 5609 | return true; |
| 5610 | |
| 5611 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5612 | // to hold the macro body with substitutions. |
| 5613 | SmallString<256> Buf; |
| 5614 | raw_svector_ostream OS(Buf); |
| 5615 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5616 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5617 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 5618 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5619 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5620 | return true; |
| 5621 | } |
| 5622 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5623 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5624 | |
| 5625 | return false; |
| 5626 | } |
| 5627 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5628 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5629 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5630 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5631 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5632 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5633 | |
| 5634 | if (check(parseIdentifier(Parameter.Name), |
| 5635 | "expected identifier in '.irpc' directive") || |
| 5636 | parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") || |
| 5637 | parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5638 | return true; |
| 5639 | |
| 5640 | if (A.size() != 1 || A.front().size() != 1) |
| 5641 | return TokError("unexpected token in '.irpc' directive"); |
| 5642 | |
| 5643 | // Eat the end of statement. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5644 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5645 | return true; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5646 | |
| 5647 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5648 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5649 | if (!M) |
| 5650 | return true; |
| 5651 | |
| 5652 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5653 | // to hold the macro body with substitutions. |
| 5654 | SmallString<256> Buf; |
| 5655 | raw_svector_ostream OS(Buf); |
| 5656 | |
| 5657 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 5658 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 5659 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5660 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5661 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5662 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 5663 | // This is undocumented, but GAS seems to support it. |
| 5664 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5665 | return true; |
| 5666 | } |
| 5667 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5668 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5669 | |
| 5670 | return false; |
| 5671 | } |
| 5672 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5673 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5674 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 5675 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5676 | |
| 5677 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5678 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5679 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 5680 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5681 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5682 | return false; |
| 5683 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 5684 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5685 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5686 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5687 | const MCExpr *Value; |
| 5688 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5689 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5690 | return true; |
| 5691 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5692 | if (!MCE) |
| 5693 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 5694 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 5695 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5696 | return Error(ExprLoc, "literal value out of range for directive"); |
| 5697 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5698 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5699 | return false; |
| 5700 | } |
| 5701 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5702 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5703 | const MCExpr *Value; |
| 5704 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5705 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5706 | return true; |
| 5707 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5708 | if (!MCE) |
| 5709 | return Error(ExprLoc, "unexpected expression in align"); |
| 5710 | uint64_t IntValue = MCE->getValue(); |
| 5711 | if (!isPowerOf2_64(IntValue)) |
| 5712 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 5713 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5714 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5715 | return false; |
| 5716 | } |
| 5717 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5718 | bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) { |
| 5719 | const AsmToken StrTok = getTok(); |
| 5720 | Lex(); |
| 5721 | if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"') |
| 5722 | return Error(DirectiveLoc, "expected double quoted string after .print"); |
| 5723 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5724 | return true; |
| 5725 | llvm::outs() << StrTok.getStringContents() << '\n'; |
| 5726 | return false; |
| 5727 | } |
| 5728 | |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 5729 | bool AsmParser::parseDirectiveAddrsig() { |
| 5730 | getStreamer().EmitAddrsig(); |
| 5731 | return false; |
| 5732 | } |
| 5733 | |
| 5734 | bool AsmParser::parseDirectiveAddrsigSym() { |
| 5735 | StringRef Name; |
| 5736 | if (check(parseIdentifier(Name), |
| 5737 | "expected identifier in '.addrsig_sym' directive")) |
| 5738 | return true; |
| 5739 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
| 5740 | getStreamer().EmitAddrsigSym(Sym); |
| 5741 | return false; |
| 5742 | } |
| 5743 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 5744 | // We are comparing pointers, but the pointers are relative to a single string. |
| 5745 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 5746 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 5747 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 5748 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 5749 | return -1; |
| 5750 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 5751 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5752 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5753 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 5754 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 5755 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 5756 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5757 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 5758 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5759 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5760 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5761 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 5762 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5763 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5764 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5767 | bool AsmParser::parseMSInlineAsm( |
| 5768 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 5769 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5770 | SmallVectorImpl<std::string> &Constraints, |
| 5771 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 5772 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5773 | SmallVector<void *, 4> InputDecls; |
| 5774 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5775 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 5776 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5777 | SmallVector<std::string, 4> InputConstraints; |
| 5778 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5779 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5780 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5781 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5782 | |
| 5783 | // Prime the lexer. |
| 5784 | Lex(); |
| 5785 | |
| 5786 | // While we have input, parse each statement. |
| 5787 | unsigned InputIdx = 0; |
| 5788 | unsigned OutputIdx = 0; |
| 5789 | while (getLexer().isNot(AsmToken::Eof)) { |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5790 | // Parse curly braces marking block start/end |
| 5791 | if (parseCurlyBlockScope(AsmStrRewrites)) |
| 5792 | continue; |
| 5793 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5794 | ParseStatementInfo Info(&AsmStrRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5795 | bool StatementErr = parseStatement(Info, &SI); |
Nirav Dave | 9fa8af2 | 2016-09-13 13:55:06 +0000 | [diff] [blame] | 5796 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5797 | if (StatementErr || Info.ParseError) { |
| 5798 | // Emit pending errors if any exist. |
| 5799 | printPendingErrors(); |
Nico Weber | e204c48 | 2016-09-13 18:17:00 +0000 | [diff] [blame] | 5800 | return true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | // No pending error should exist here. |
| 5804 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 5805 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5806 | if (Info.Opcode == ~0U) |
| 5807 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5808 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5809 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5810 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5811 | // Build the list of clobbers, outputs and inputs. |
| 5812 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5813 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5814 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5815 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5816 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5817 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5818 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5819 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 5820 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 5821 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5822 | unsigned NumDefs = Desc.getNumDefs(); |
| 5823 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5824 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 5825 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5826 | continue; |
| 5827 | } |
| 5828 | |
| 5829 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5830 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5831 | if (SymName.empty()) |
| 5832 | continue; |
| 5833 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5834 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5835 | if (!OpDecl) |
| 5836 | continue; |
| 5837 | |
| 5838 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5839 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5840 | if (isOutput) { |
| 5841 | ++InputIdx; |
| 5842 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5843 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 5844 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5845 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5846 | } else { |
| 5847 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5848 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 5849 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5850 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5851 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5852 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 5853 | |
| 5854 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 5855 | ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), |
| 5856 | Desc.getNumImplicitDefs()); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5857 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5861 | NumOutputs = OutputDecls.size(); |
| 5862 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5863 | |
| 5864 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5865 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 5866 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 5867 | ClobberRegs.end()); |
| 5868 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 5869 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 5870 | raw_string_ostream OS(Clobbers[I]); |
| 5871 | IP->printRegName(OS, ClobberRegs[I]); |
| 5872 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5873 | |
| 5874 | // Merge the various outputs and inputs. Output are expected first. |
| 5875 | if (NumOutputs || NumInputs) { |
| 5876 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5877 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5878 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5879 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5880 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5881 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5882 | } |
| 5883 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5884 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5885 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5886 | } |
| 5887 | } |
| 5888 | |
| 5889 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 5890 | std::string AsmStringIR; |
| 5891 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 5892 | StringRef ASMString = |
| 5893 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 5894 | const char *AsmStart = ASMString.begin(); |
| 5895 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5896 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5897 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 5898 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5899 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5900 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5901 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5902 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 5903 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5904 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5905 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5906 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5907 | // Skip the original expression. |
| 5908 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5909 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5910 | continue; |
| 5911 | } |
| 5912 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5913 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5914 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5915 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5916 | default: |
| 5917 | break; |
Coby Tayree | d891289 | 2017-08-24 08:46:25 +0000 | [diff] [blame] | 5918 | case AOK_IntelExpr: |
| 5919 | assert(AR.IntelExp.isValid() && "cannot write invalid intel expression"); |
| 5920 | if (AR.IntelExp.NeedBracs) |
| 5921 | OS << "["; |
| 5922 | if (AR.IntelExp.hasBaseReg()) |
| 5923 | OS << AR.IntelExp.BaseReg; |
| 5924 | if (AR.IntelExp.hasIndexReg()) |
| 5925 | OS << (AR.IntelExp.hasBaseReg() ? " + " : "") |
| 5926 | << AR.IntelExp.IndexReg; |
| 5927 | if (AR.IntelExp.Scale > 1) |
| 5928 | OS << " * $$" << AR.IntelExp.Scale; |
| 5929 | if (AR.IntelExp.Imm || !AR.IntelExp.hasRegs()) |
| 5930 | OS << (AR.IntelExp.hasRegs() ? " + $$" : "$$") << AR.IntelExp.Imm; |
| 5931 | if (AR.IntelExp.NeedBracs) |
| 5932 | OS << "]"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5933 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5934 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 5935 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5936 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5937 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5938 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5939 | break; |
| 5940 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5941 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5942 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5943 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5944 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5945 | default: break; |
| 5946 | case 8: OS << "byte ptr "; break; |
| 5947 | case 16: OS << "word ptr "; break; |
| 5948 | case 32: OS << "dword ptr "; break; |
| 5949 | case 64: OS << "qword ptr "; break; |
| 5950 | case 80: OS << "xword ptr "; break; |
| 5951 | case 128: OS << "xmmword ptr "; break; |
| 5952 | case 256: OS << "ymmword ptr "; break; |
| 5953 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | case AOK_Emit: |
| 5956 | OS << ".byte"; |
| 5957 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5958 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5959 | // MS alignment directives are measured in bytes. If the native assembler |
| 5960 | // measures alignment in bytes, we can pass it straight through. |
| 5961 | OS << ".align"; |
| 5962 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 5963 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5964 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5965 | // Alignment is in log2 form, so print that instead and skip the original |
| 5966 | // immediate. |
| 5967 | unsigned Val = AR.Val; |
| 5968 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5969 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5970 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 5971 | break; |
| 5972 | } |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 5973 | case AOK_EVEN: |
| 5974 | OS << ".even"; |
| 5975 | break; |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5976 | case AOK_EndOfStatement: |
| 5977 | OS << "\n\t"; |
| 5978 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5979 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5980 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5981 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5982 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5986 | if (AsmStart != AsmEnd) |
| 5987 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5988 | |
| 5989 | AsmString = OS.str(); |
| 5990 | return false; |
| 5991 | } |
| 5992 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5993 | namespace llvm { |
| 5994 | namespace MCParserUtils { |
| 5995 | |
| 5996 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 5997 | /// or subexpressions. |
| 5998 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 5999 | switch (Value->getKind()) { |
| 6000 | case MCExpr::Binary: { |
| 6001 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 6002 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 6003 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 6004 | } |
| 6005 | case MCExpr::Target: |
| 6006 | case MCExpr::Constant: |
| 6007 | return false; |
| 6008 | case MCExpr::SymbolRef: { |
| 6009 | const MCSymbol &S = |
| 6010 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 6011 | if (S.isVariable()) |
| 6012 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 6013 | return &S == Sym; |
| 6014 | } |
| 6015 | case MCExpr::Unary: |
| 6016 | return isSymbolUsedInExpression( |
| 6017 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 6018 | } |
| 6019 | |
| 6020 | llvm_unreachable("Unknown expr kind!"); |
| 6021 | } |
| 6022 | |
| 6023 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 6024 | MCAsmParser &Parser, MCSymbol *&Sym, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 6025 | const MCExpr *&Value) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6026 | |
| 6027 | // FIXME: Use better location, we should use proper tokens. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 6028 | SMLoc EqualLoc = Parser.getTok().getLoc(); |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 6029 | if (Parser.parseExpression(Value)) |
| 6030 | return Parser.TokError("missing expression"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6031 | |
| 6032 | // Note: we don't count b as used in "a = b". This is to allow |
| 6033 | // a = b |
| 6034 | // b = c |
| 6035 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 6036 | if (Parser.parseToken(AsmToken::EndOfStatement)) |
| 6037 | return true; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6038 | |
| 6039 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 6040 | // used as a symbol, or it is an absolute symbol). |
| 6041 | Sym = Parser.getContext().lookupSymbol(Name); |
| 6042 | if (Sym) { |
| 6043 | // Diagnose assignment to a label. |
| 6044 | // |
| 6045 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 6046 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 6047 | if (isSymbolUsedInExpression(Sym, Value)) |
| 6048 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 6049 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 6050 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6051 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 6052 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 6053 | ; // Allow redefinitions of variables that haven't yet been used. |
| 6054 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 6055 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 6056 | else if (!Sym->isVariable()) |
| 6057 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 6058 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 6059 | return Parser.Error(EqualLoc, |
| 6060 | "invalid reassignment of non-absolute variable '" + |
| 6061 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6062 | } else if (Name == ".") { |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 6063 | Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6064 | return false; |
| 6065 | } else |
| 6066 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 6067 | |
| 6068 | Sym->setRedefinable(allow_redef); |
| 6069 | |
| 6070 | return false; |
| 6071 | } |
| 6072 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 6073 | } // end namespace MCParserUtils |
| 6074 | } // end namespace llvm |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 6075 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 6076 | /// Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 6077 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 6078 | MCStreamer &Out, const MCAsmInfo &MAI, |
| 6079 | unsigned CB) { |
| 6080 | return new AsmParser(SM, C, Out, MAI, CB); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 6081 | } |