Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class implements the parser for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/None.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 25 | #include "llvm/BinaryFormat/Dwarf.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCCodeView.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCDirectives.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstPrinter.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrDesc.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCInstrInfo.h" |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCObjectFileInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCParser/AsmCond.h" |
| 37 | #include "llvm/MC/MCParser/AsmLexer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCParser/MCAsmParserUtils.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 42 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Benjamin Kramer | b3e8a6d | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCRegisterInfo.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 45 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 46 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCTargetOptions.h" |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 49 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Casting.h" |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MD5.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 54 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 56 | #include "llvm/Support/SMLoc.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 57 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 59 | #include <algorithm> |
| 60 | #include <cassert> |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 61 | #include <cctype> |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 62 | #include <climits> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 63 | #include <cstddef> |
| 64 | #include <cstdint> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 65 | #include <deque> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 66 | #include <memory> |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 67 | #include <sstream> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 68 | #include <string> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 69 | #include <tuple> |
| 70 | #include <utility> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 71 | #include <vector> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 72 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 73 | using namespace llvm; |
| 74 | |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 75 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 76 | |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 77 | static cl::opt<unsigned> AsmMacroMaxNestingDepth( |
| 78 | "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden, |
| 79 | cl::desc("The maximum nesting depth allowed for assembly macros.")); |
| 80 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 81 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 82 | |
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; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 162 | int64_t LineNumber = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 163 | SMLoc Loc; |
Andrew Kaylor | ca19647 | 2016-04-21 20:09:35 +0000 | [diff] [blame] | 164 | unsigned Buf = 0; |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 165 | }; |
| 166 | CppHashInfoTy CppHashInfo; |
| 167 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 168 | /// List of forward directional labels for diagnosis at the end. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 169 | SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
| 170 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 171 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 172 | unsigned AssemblerDialect = ~0U; |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 173 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 174 | /// is Darwin compatibility enabled? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 175 | bool IsDarwin = false; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 176 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 177 | /// Are we parsing ms-style inline assembly? |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 178 | bool ParsingInlineAsm = false; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 179 | |
Paul Robinson | cc7344a | 2018-06-14 13:38:20 +0000 | [diff] [blame] | 180 | /// Did we already inform the user about inconsistent MD5 usage? |
| 181 | bool ReportedInconsistentMD5 = false; |
| 182 | |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 183 | // Is alt macro mode enabled. |
| 184 | bool AltMacroMode = false; |
| 185 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 186 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 187 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 188 | const MCAsmInfo &MAI, unsigned CB); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 189 | AsmParser(const AsmParser &) = delete; |
| 190 | AsmParser &operator=(const AsmParser &) = delete; |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 191 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 192 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 193 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 194 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 195 | void addDirectiveHandler(StringRef Directive, |
| 196 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 197 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 200 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 201 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 202 | } |
| 203 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 204 | /// @name MCAsmParser Interface |
| 205 | /// { |
| 206 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 207 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 208 | MCAsmLexer &getLexer() override { return Lexer; } |
| 209 | MCContext &getContext() override { return Ctx; } |
| 210 | MCStreamer &getStreamer() override { return Out; } |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 211 | |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 212 | CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
| 213 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 214 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 215 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 216 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 217 | else |
| 218 | return AssemblerDialect; |
| 219 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 220 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 221 | AssemblerDialect = i; |
| 222 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 223 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 224 | void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 225 | bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| 226 | bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 227 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 228 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 229 | |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 230 | void setParsingInlineAsm(bool V) override { |
| 231 | ParsingInlineAsm = V; |
Reid Kleckner | 953bdce | 2018-10-24 20:23:57 +0000 | [diff] [blame^] | 232 | // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and |
| 233 | // hex integer literals. |
| 234 | Lexer.setLexMasmIntegers(V); |
Yunzhong Gao | 27ea29b | 2016-09-02 23:15:29 +0000 | [diff] [blame] | 235 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 236 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 237 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 238 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 239 | unsigned &NumOutputs, unsigned &NumInputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 240 | SmallVectorImpl<std::pair<void *,bool>> &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 241 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 242 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 243 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 244 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 245 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 246 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 247 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 248 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 249 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 250 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 251 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 252 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 253 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 254 | /// Parse a floating point expression using the float \p Semantics |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 255 | /// and set \p Res to the value. |
| 256 | bool parseRealValue(const fltSemantics &Semantics, APInt &Res); |
| 257 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 258 | /// Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 259 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 260 | bool parseIdentifier(StringRef &Res) override; |
| 261 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 262 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 263 | bool checkForValidSection() override; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 265 | /// } |
| 266 | |
| 267 | private: |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 268 | bool parseStatement(ParseStatementInfo &Info, |
| 269 | MCAsmParserSemaCallback *SI); |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 270 | bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 271 | bool parseCppHashLineFilenameComment(SMLoc L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 272 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 273 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 274 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 275 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 276 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 277 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 278 | SMLoc L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 279 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 280 | /// Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 281 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 282 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 283 | /// Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 284 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 285 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 286 | /// Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 287 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 288 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 289 | /// Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 290 | /// |
| 291 | /// \param M The macro. |
| 292 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 293 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 294 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 295 | /// Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 296 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 297 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 298 | /// Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 299 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 300 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 301 | /// Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 302 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 303 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 304 | void printMacroInstantiations(); |
| 305 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 306 | SMRange Range = None) const { |
| 307 | ArrayRef<SMRange> Ranges(Range); |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 308 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 309 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 310 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 311 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 312 | /// Should we emit DWARF describing this assembler source? (Returns false if |
| 313 | /// the source has .file directives, which means we don't want to generate |
| 314 | /// info describing the assembler source itself.) |
| 315 | bool enabledGenDwarfForAssembly(); |
| 316 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 317 | /// Enter the specified file. This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 318 | bool enterIncludeFile(const std::string &Filename); |
| 319 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 320 | /// Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 321 | /// This returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 322 | bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, |
| 323 | const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 324 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 325 | /// Reset the current lexer position to that given by \p Loc. The |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 326 | /// current token is not set; clients should ensure Lex() is called |
| 327 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 328 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 329 | /// \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] | 330 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 331 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 332 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 333 | /// 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] | 334 | /// current token until the end of the statement; the current token on exit |
| 335 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 336 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 337 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 338 | /// Parse until the end of a statement or a comma is encountered, |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 339 | /// 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] | 340 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 341 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 342 | bool parseAssignment(StringRef Name, bool allow_redef, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 343 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 344 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 345 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 346 | MCBinaryExpr::Opcode &Kind); |
| 347 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 348 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 349 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 350 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 352 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 353 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 354 | bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); |
| 355 | bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); |
| 356 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 357 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 358 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 359 | DK_NO_DIRECTIVE, // Placeholder |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 360 | DK_SET, |
| 361 | DK_EQU, |
| 362 | DK_EQUIV, |
| 363 | DK_ASCII, |
| 364 | DK_ASCIZ, |
| 365 | DK_STRING, |
| 366 | DK_BYTE, |
| 367 | DK_SHORT, |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 368 | DK_RELOC, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 369 | DK_VALUE, |
| 370 | DK_2BYTE, |
| 371 | DK_LONG, |
| 372 | DK_INT, |
| 373 | DK_4BYTE, |
| 374 | DK_QUAD, |
| 375 | DK_8BYTE, |
| 376 | DK_OCTA, |
| 377 | DK_DC, |
| 378 | DK_DC_A, |
| 379 | DK_DC_B, |
| 380 | DK_DC_D, |
| 381 | DK_DC_L, |
| 382 | DK_DC_S, |
| 383 | DK_DC_W, |
| 384 | DK_DC_X, |
| 385 | DK_DCB, |
| 386 | DK_DCB_B, |
| 387 | DK_DCB_D, |
| 388 | DK_DCB_L, |
| 389 | DK_DCB_S, |
| 390 | DK_DCB_W, |
| 391 | DK_DCB_X, |
| 392 | DK_DS, |
| 393 | DK_DS_B, |
| 394 | DK_DS_D, |
| 395 | DK_DS_L, |
| 396 | DK_DS_P, |
| 397 | DK_DS_S, |
| 398 | DK_DS_W, |
| 399 | DK_DS_X, |
| 400 | DK_SINGLE, |
| 401 | DK_FLOAT, |
| 402 | DK_DOUBLE, |
| 403 | DK_ALIGN, |
| 404 | DK_ALIGN32, |
| 405 | DK_BALIGN, |
| 406 | DK_BALIGNW, |
| 407 | DK_BALIGNL, |
| 408 | DK_P2ALIGN, |
| 409 | DK_P2ALIGNW, |
| 410 | DK_P2ALIGNL, |
| 411 | DK_ORG, |
| 412 | DK_FILL, |
| 413 | DK_ENDR, |
| 414 | DK_BUNDLE_ALIGN_MODE, |
| 415 | DK_BUNDLE_LOCK, |
| 416 | DK_BUNDLE_UNLOCK, |
| 417 | DK_ZERO, |
| 418 | DK_EXTERN, |
| 419 | DK_GLOBL, |
| 420 | DK_GLOBAL, |
| 421 | DK_LAZY_REFERENCE, |
| 422 | DK_NO_DEAD_STRIP, |
| 423 | DK_SYMBOL_RESOLVER, |
| 424 | DK_PRIVATE_EXTERN, |
| 425 | DK_REFERENCE, |
| 426 | DK_WEAK_DEFINITION, |
| 427 | DK_WEAK_REFERENCE, |
| 428 | DK_WEAK_DEF_CAN_BE_HIDDEN, |
| 429 | DK_COMM, |
| 430 | DK_COMMON, |
| 431 | DK_LCOMM, |
| 432 | DK_ABORT, |
| 433 | DK_INCLUDE, |
| 434 | DK_INCBIN, |
| 435 | DK_CODE16, |
| 436 | DK_CODE16GCC, |
| 437 | DK_REPT, |
| 438 | DK_IRP, |
| 439 | DK_IRPC, |
| 440 | DK_IF, |
| 441 | DK_IFEQ, |
| 442 | DK_IFGE, |
| 443 | DK_IFGT, |
| 444 | DK_IFLE, |
| 445 | DK_IFLT, |
| 446 | DK_IFNE, |
| 447 | DK_IFB, |
| 448 | DK_IFNB, |
| 449 | DK_IFC, |
| 450 | DK_IFEQS, |
| 451 | DK_IFNC, |
| 452 | DK_IFNES, |
| 453 | DK_IFDEF, |
| 454 | DK_IFNDEF, |
| 455 | DK_IFNOTDEF, |
| 456 | DK_ELSEIF, |
| 457 | DK_ELSE, |
| 458 | DK_ENDIF, |
| 459 | DK_SPACE, |
| 460 | DK_SKIP, |
| 461 | DK_FILE, |
| 462 | DK_LINE, |
| 463 | DK_LOC, |
| 464 | DK_STABS, |
| 465 | DK_CV_FILE, |
| 466 | DK_CV_FUNC_ID, |
| 467 | DK_CV_INLINE_SITE_ID, |
| 468 | DK_CV_LOC, |
| 469 | DK_CV_LINETABLE, |
| 470 | DK_CV_INLINE_LINETABLE, |
| 471 | DK_CV_DEF_RANGE, |
| 472 | DK_CV_STRINGTABLE, |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 473 | DK_CV_STRING, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 474 | DK_CV_FILECHECKSUMS, |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 475 | DK_CV_FILECHECKSUM_OFFSET, |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 476 | DK_CV_FPO_DATA, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 477 | DK_CFI_SECTIONS, |
| 478 | DK_CFI_STARTPROC, |
| 479 | DK_CFI_ENDPROC, |
| 480 | DK_CFI_DEF_CFA, |
| 481 | DK_CFI_DEF_CFA_OFFSET, |
| 482 | DK_CFI_ADJUST_CFA_OFFSET, |
| 483 | DK_CFI_DEF_CFA_REGISTER, |
| 484 | DK_CFI_OFFSET, |
| 485 | DK_CFI_REL_OFFSET, |
| 486 | DK_CFI_PERSONALITY, |
| 487 | DK_CFI_LSDA, |
| 488 | DK_CFI_REMEMBER_STATE, |
| 489 | DK_CFI_RESTORE_STATE, |
| 490 | DK_CFI_SAME_VALUE, |
| 491 | DK_CFI_RESTORE, |
| 492 | DK_CFI_ESCAPE, |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 493 | DK_CFI_RETURN_COLUMN, |
Saleem Abdulrasool | b3c70c0 | 2017-07-28 03:39:18 +0000 | [diff] [blame] | 494 | DK_CFI_SIGNAL_FRAME, |
| 495 | DK_CFI_UNDEFINED, |
| 496 | DK_CFI_REGISTER, |
| 497 | DK_CFI_WINDOW_SAVE, |
| 498 | DK_MACROS_ON, |
| 499 | DK_MACROS_OFF, |
| 500 | DK_ALTMACRO, |
| 501 | DK_NOALTMACRO, |
| 502 | DK_MACRO, |
| 503 | DK_EXITM, |
| 504 | DK_ENDM, |
| 505 | DK_ENDMACRO, |
| 506 | DK_PURGEM, |
| 507 | DK_SLEB128, |
| 508 | DK_ULEB128, |
| 509 | DK_ERR, |
| 510 | DK_ERROR, |
| 511 | DK_WARNING, |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 512 | DK_PRINT, |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 513 | DK_ADDRSIG, |
| 514 | DK_ADDRSIG_SYM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 515 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 516 | }; |
| 517 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 518 | /// Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 519 | /// directives parsed by this class. |
| 520 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 521 | |
| 522 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 523 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 524 | bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 525 | bool parseDirectiveValue(StringRef IDVal, |
| 526 | unsigned Size); // ".byte", ".long", ... |
| 527 | bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
| 528 | bool parseDirectiveRealValue(StringRef IDVal, |
| 529 | const fltSemantics &); // ".single", ... |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 530 | bool parseDirectiveFill(); // ".fill" |
| 531 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 532 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 533 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 534 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 535 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 536 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 537 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 538 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 539 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 540 | bool parseDirectiveLine(); |
| 541 | bool parseDirectiveLoc(); |
| 542 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 543 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 544 | // ".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] | 545 | // ".cv_inline_linetable", ".cv_def_range", ".cv_string" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 546 | bool parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 547 | bool parseDirectiveCVFuncId(); |
| 548 | bool parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 549 | bool parseDirectiveCVLoc(); |
| 550 | bool parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 551 | bool parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 552 | bool parseDirectiveCVDefRange(); |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 553 | bool parseDirectiveCVString(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 554 | bool parseDirectiveCVStringTable(); |
| 555 | bool parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 556 | bool parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 557 | bool parseDirectiveCVFPOData(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 558 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 559 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 560 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 561 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 562 | bool parseDirectiveCFISections(); |
| 563 | bool parseDirectiveCFIStartProc(); |
| 564 | bool parseDirectiveCFIEndProc(); |
| 565 | bool parseDirectiveCFIDefCfaOffset(); |
| 566 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 567 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 568 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 569 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 570 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 571 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 572 | bool parseDirectiveCFIRememberState(); |
| 573 | bool parseDirectiveCFIRestoreState(); |
| 574 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 575 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 576 | bool parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 577 | bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 578 | bool parseDirectiveCFISignalFrame(); |
| 579 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 580 | |
| 581 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 582 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 583 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 584 | bool parseDirectiveEndMacro(StringRef Directive); |
| 585 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 586 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 587 | // alternate macro mode directives |
| 588 | bool parseDirectiveAltmacro(StringRef Directive); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 589 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 590 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 591 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 592 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 593 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 594 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 595 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 596 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 597 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 598 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 599 | // ".dcb" |
| 600 | bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
| 601 | bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 602 | // ".ds" |
| 603 | bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 604 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 605 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 606 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 607 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 608 | /// Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 609 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 610 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 611 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 612 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 613 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 614 | bool parseDirectiveAbort(); // ".abort" |
| 615 | bool parseDirectiveInclude(); // ".include" |
| 616 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 617 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 618 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 619 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 620 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 621 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 622 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 623 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 624 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 625 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 626 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 627 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 628 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 629 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 630 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 631 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 633 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 634 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 635 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 636 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 637 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 638 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 639 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 640 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 641 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 642 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 643 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 644 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 645 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 646 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 647 | size_t Len); |
| 648 | |
| 649 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 650 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 651 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 652 | // "end" |
| 653 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 654 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 655 | // ".err" or ".error" |
| 656 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 657 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 658 | // ".warning" |
| 659 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 660 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 661 | // .print <double-quotes-string> |
| 662 | bool parseDirectivePrint(SMLoc DirectiveLoc); |
| 663 | |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 664 | // Directives to support address-significance tables. |
| 665 | bool parseDirectiveAddrsig(); |
| 666 | bool parseDirectiveAddrsigSym(); |
| 667 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 668 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 669 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 670 | |
| 671 | } // end anonymous namespace |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 672 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 673 | namespace llvm { |
| 674 | |
| 675 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 676 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 677 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 678 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 679 | } // end namespace llvm |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 680 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 681 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 682 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 683 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 684 | const MCAsmInfo &MAI, unsigned CB = 0) |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 685 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 686 | CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 687 | HadError = false; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 688 | // Save the old handler. |
| 689 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 690 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 691 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 692 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 693 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 694 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 695 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 696 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 697 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 698 | PlatformParser.reset(createCOFFAsmParser()); |
| 699 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 700 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 701 | PlatformParser.reset(createDarwinAsmParser()); |
| 702 | IsDarwin = true; |
| 703 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 704 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 705 | PlatformParser.reset(createELFAsmParser()); |
| 706 | break; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 707 | case MCObjectFileInfo::IsWasm: |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame] | 708 | // TODO: WASM will need its own MCAsmParserExtension implementation, but |
| 709 | // for now we can re-use the ELF one, since the directives can be the |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 710 | // same for now. |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame] | 711 | PlatformParser.reset(createELFAsmParser()); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 712 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 713 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 714 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 715 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 716 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 717 | |
| 718 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 721 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 722 | assert((HadError || ActiveMacros.empty()) && |
| 723 | "Unexpected active macro instantiation!"); |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 724 | |
| 725 | // Restore the saved diagnostics handler and context for use during |
| 726 | // finalization. |
| 727 | SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 730 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 731 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 732 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 733 | it = ActiveMacros.rbegin(), |
| 734 | ie = ActiveMacros.rend(); |
| 735 | it != ie; ++it) |
| 736 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 737 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 740 | void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
| 741 | printPendingErrors(); |
| 742 | printMessage(L, SourceMgr::DK_Note, Msg, Range); |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 743 | printMacroInstantiations(); |
| 744 | } |
| 745 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 746 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 747 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 748 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 749 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 750 | return Error(L, Msg, Range); |
| 751 | printMessage(L, SourceMgr::DK_Warning, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 752 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 753 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 756 | bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 757 | HadError = true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 758 | printMessage(L, SourceMgr::DK_Error, Msg, Range); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 759 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 763 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 764 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 765 | unsigned NewBuf = |
| 766 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 767 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 768 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 769 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 770 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 771 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 772 | return false; |
| 773 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 774 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 775 | /// 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] | 776 | /// 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] | 777 | /// returns true on failure. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 778 | bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
| 779 | const MCExpr *Count, SMLoc Loc) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 780 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 781 | unsigned NewBuf = |
| 782 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 783 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 784 | return true; |
| 785 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 786 | // Pick up the bytes from the file and emit them. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 787 | StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); |
| 788 | Bytes = Bytes.drop_front(Skip); |
| 789 | if (Count) { |
| 790 | int64_t Res; |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 791 | if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 792 | return Error(Loc, "expected absolute expression"); |
| 793 | if (Res < 0) |
| 794 | return Warning(Loc, "negative count has no effect"); |
| 795 | Bytes = Bytes.take_front(Res); |
| 796 | } |
| 797 | getStreamer().EmitBytes(Bytes); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 801 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 802 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 803 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 804 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 807 | const AsmToken &AsmParser::Lex() { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 808 | if (Lexer.getTok().is(AsmToken::Error)) |
| 809 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
| 810 | |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 811 | // if it's a end of statement with a comment in it |
| 812 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 813 | // if this is a line comment output it. |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 814 | if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 815 | getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
| 816 | Out.addExplicitComment(Twine(getTok().getString())); |
| 817 | } |
| 818 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 819 | const AsmToken *tok = &Lexer.Lex(); |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 820 | |
| 821 | // Parse comments here to be deferred until end of next statement. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 822 | while (tok->is(AsmToken::Comment)) { |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 823 | if (MAI.preserveAsmComments()) |
| 824 | Out.addExplicitComment(Twine(tok->getString())); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 825 | tok = &Lexer.Lex(); |
| 826 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 827 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 828 | if (tok->is(AsmToken::Eof)) { |
| 829 | // If this is the end of an included file, pop the parent file off the |
| 830 | // include stack. |
| 831 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 832 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 833 | jumpToLoc(ParentIncludeLoc); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 834 | return Lex(); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 837 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 838 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 841 | bool AsmParser::enabledGenDwarfForAssembly() { |
| 842 | // Check whether the user specified -g. |
| 843 | if (!getContext().getGenDwarfForAssembly()) |
| 844 | return false; |
| 845 | // If we haven't encountered any .file directives (which would imply that |
| 846 | // the assembler source was produced with debug info already) then emit one |
| 847 | // describing the assembler source file itself. |
| 848 | if (getContext().getGenDwarfFileNumber() == 0) |
| 849 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 850 | 0, StringRef(), getContext().getMainFileName())); |
| 851 | return true; |
| 852 | } |
| 853 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 854 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 855 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 856 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 857 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 859 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 860 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 861 | |
| 862 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 863 | AsmCond StartingCondState = TheCondState; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 864 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 865 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 866 | // 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] | 867 | // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't |
| 868 | // emitting any actual debug info yet and haven't had a chance to parse any |
| 869 | // embedded .file directives.) |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 870 | if (getContext().getGenDwarfForAssembly()) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 871 | MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 872 | if (!Sec->getBeginSymbol()) { |
| 873 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 874 | getStreamer().EmitLabel(SectionStartSym); |
| 875 | Sec->setBeginSymbol(SectionStartSym); |
| 876 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 877 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 878 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 879 | (void)InsertResult; |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 882 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 883 | while (Lexer.isNot(AsmToken::Eof)) { |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 884 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 885 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 886 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 887 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 888 | // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
| 889 | // for printing ErrMsg via Lex() only if no (presumably better) parser error |
| 890 | // exists. |
| 891 | if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 892 | Lex(); |
| 893 | } |
| 894 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 895 | // parseStatement returned true so may need to emit an error. |
| 896 | printPendingErrors(); |
| 897 | |
| 898 | // Skipping to the next line if needed. |
| 899 | if (!getLexer().isAtStartOfStatement()) |
| 900 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 901 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 902 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 903 | // All errors should have been emitted. |
| 904 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
| 905 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 906 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 907 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 908 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 909 | TheCondState.Ignore != StartingCondState.Ignore) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 910 | printError(getTok().getLoc(), "unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 911 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 912 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 913 | if (!LineTables.empty()) { |
| 914 | unsigned Index = 0; |
| 915 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 916 | if (File.Name.empty() && Index != 0) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 917 | printError(getTok().getLoc(), "unassigned file number: " + |
| 918 | Twine(Index) + |
| 919 | " for .file directives"); |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 920 | ++Index; |
| 921 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 922 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 923 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 924 | // Check to see that all assembler local symbols were actually defined. |
| 925 | // Targets that don't do subsections via symbols may not want this, though, |
| 926 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 927 | // as otherwise we won't necessarilly have seen everything yet. |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 928 | if (!NoFinalize) { |
| 929 | if (MAI.hasSubsectionsViaSymbols()) { |
| 930 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 931 | MCSymbol *Sym = TableEntry.getValue(); |
| 932 | // Variable symbols may not be marked as defined, so check those |
| 933 | // explicitly. If we know it's a variable, we have a definition for |
| 934 | // the purposes of this check. |
| 935 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 936 | // FIXME: We would really like to refer back to where the symbol was |
| 937 | // first referenced for a source location. We need to add something |
| 938 | // 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] | 939 | printError(getTok().getLoc(), "assembler local symbol '" + |
| 940 | Sym->getName() + "' not defined"); |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
| 944 | // Temporary symbols like the ones for directional jumps don't go in the |
| 945 | // symbol table. They also need to be diagnosed in all (final) cases. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 946 | for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
| 947 | if (std::get<2>(LocSym)->isUndefined()) { |
| 948 | // Reset the state of any "# line file" directives we've seen to the |
| 949 | // context as it was at the diagnostic site. |
| 950 | CppHashInfo = std::get<1>(LocSym); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 951 | printError(std::get<0>(LocSym), "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 952 | } |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 956 | // Finalize the output stream if there are no errors and if the client wants |
| 957 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 958 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 959 | Out.Finish(); |
| 960 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 961 | return HadError || getContext().hadError(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 964 | bool AsmParser::checkForValidSection() { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 965 | if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 966 | Out.InitSections(false); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 967 | return Error(getTok().getLoc(), |
| 968 | "expected section directive before assembly directive"); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 969 | } |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 970 | return false; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 973 | /// Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 974 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 975 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 976 | Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 977 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 978 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 979 | if (Lexer.is(AsmToken::EndOfStatement)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 980 | Lexer.Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 983 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 984 | const char *Start = getTok().getLoc().getPointer(); |
| 985 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 986 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 987 | Lexer.Lex(); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 988 | |
| 989 | const char *End = getTok().getLoc().getPointer(); |
| 990 | return StringRef(Start, End - Start); |
| 991 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 992 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 993 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 994 | const char *Start = getTok().getLoc().getPointer(); |
| 995 | |
| 996 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 997 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 998 | Lexer.Lex(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 999 | |
| 1000 | const char *End = getTok().getLoc().getPointer(); |
| 1001 | return StringRef(Start, End - Start); |
| 1002 | } |
| 1003 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1004 | /// Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1005 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 1006 | /// |
| 1007 | /// parenexpr ::= expr) |
| 1008 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1009 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1010 | if (parseExpression(Res)) |
| 1011 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1012 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1013 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1014 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1015 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1018 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1019 | /// Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1020 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 1021 | /// |
| 1022 | /// bracketexpr ::= expr] |
| 1023 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1024 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 1025 | if (parseExpression(Res)) |
| 1026 | return true; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1027 | EndLoc = getTok().getEndLoc(); |
| 1028 | if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) |
| 1029 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | } |
| 1032 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1033 | /// Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1034 | /// primaryexpr ::= (parenexpr |
| 1035 | /// primaryexpr ::= symbol |
| 1036 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1037 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1038 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1039 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1040 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 1041 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 1042 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1043 | default: |
| 1044 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 1045 | // If we have an error assume that we've already handled it. |
| 1046 | case AsmToken::Error: |
| 1047 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1048 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1049 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1050 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1051 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1052 | Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1053 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1054 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1055 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1056 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1057 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1058 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1059 | if (parseIdentifier(Identifier)) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1060 | // We may have failed but $ may be a valid token. |
| 1061 | if (getTok().is(AsmToken::Dollar)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1062 | if (Lexer.getMAI().getDollarIsPC()) { |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 1063 | Lex(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1064 | // This is a '$' reference, which references the current PC. Emit a |
| 1065 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1066 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1067 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1068 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 1069 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1070 | EndLoc = FirstTokenLoc; |
| 1071 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 1072 | } |
| 1073 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 1074 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 1075 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1076 | // Parse symbol variant |
| 1077 | std::pair<StringRef, StringRef> Split; |
| 1078 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1079 | if (FirstTokenKind == AsmToken::String) { |
| 1080 | if (Lexer.is(AsmToken::At)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1081 | Lex(); // eat @ |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 1082 | SMLoc AtLoc = getLexer().getLoc(); |
| 1083 | StringRef VName; |
| 1084 | if (parseIdentifier(VName)) |
| 1085 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 1086 | |
| 1087 | Split = std::make_pair(Identifier, VName); |
| 1088 | } |
| 1089 | } else { |
| 1090 | Split = Identifier.split('@'); |
| 1091 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1092 | } else if (Lexer.is(AsmToken::LParen)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1093 | Lex(); // eat '('. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1094 | StringRef VName; |
| 1095 | parseIdentifier(VName); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1096 | // eat ')'. |
| 1097 | if (parseToken(AsmToken::RParen, |
| 1098 | "unexpected token in variant, expected ')'")) |
| 1099 | return true; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1100 | Split = std::make_pair(Identifier, VName); |
| 1101 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 1102 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1103 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 1104 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1105 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1106 | StringRef SymbolName = Identifier; |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1107 | if (SymbolName.empty()) |
Francis Visoiu Mistrih | 627d146 | 2018-10-08 10:28:11 +0000 | [diff] [blame] | 1108 | return Error(getLexer().getLoc(), "expected a symbol reference"); |
Weiming Zhao | cf26d56 | 2016-12-01 18:00:36 +0000 | [diff] [blame] | 1109 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1110 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 1111 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1112 | // Lookup the symbol variant if used. |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1113 | if (!Split.second.empty()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 1114 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1115 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 1116 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 1117 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1118 | Variant = MCSymbolRefExpr::VK_None; |
| 1119 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 1120 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 1121 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1122 | } |
| 1123 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1124 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1125 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 1126 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1127 | // If this is an absolute variable reference, substitute it now to preserve |
| 1128 | // semantics in the face of reassignment. |
Nirav Dave | 05b5891 | 2018-06-05 15:13:39 +0000 | [diff] [blame] | 1129 | if (Sym->isVariable()) { |
| 1130 | auto V = Sym->getVariableValue(/*SetUsed*/ false); |
Nirav Dave | b1f9e4d | 2018-09-17 20:34:26 +0000 | [diff] [blame] | 1131 | bool DoInline = isa<MCConstantExpr>(V) && !Variant; |
Nirav Dave | 05b5891 | 2018-06-05 15:13:39 +0000 | [diff] [blame] | 1132 | if (auto TV = dyn_cast<MCTargetExpr>(V)) |
| 1133 | DoInline = TV->inlineAssignedExpr(); |
| 1134 | if (DoInline) { |
| 1135 | if (Variant) |
| 1136 | return Error(EndLoc, "unexpected modifier on variable reference"); |
| 1137 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
| 1138 | return false; |
| 1139 | } |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | // Otherwise create a symbol ref. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1143 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1144 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 1145 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 1146 | case AsmToken::BigNum: |
| 1147 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1148 | case AsmToken::Integer: { |
| 1149 | SMLoc Loc = getTok().getLoc(); |
| 1150 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1151 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1152 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1153 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1154 | // Look for 'b' or 'f' following an Integer as a directional label |
| 1155 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 1156 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1157 | // Lookup the symbol variant if used. |
| 1158 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 1159 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 1160 | if (Split.first.size() != IDVal.size()) { |
| 1161 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1162 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1163 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1164 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 1165 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1166 | if (IDVal == "f" || IDVal == "b") { |
| 1167 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1168 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1169 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 1170 | if (IDVal == "b" && Sym->isUndefined()) |
Tim Northover | 6b3169b | 2016-04-11 19:50:46 +0000 | [diff] [blame] | 1171 | return Error(Loc, "directional label undefined"); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 1172 | DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1173 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1174 | Lex(); // Eat identifier. |
| 1175 | } |
| 1176 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1177 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1178 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1179 | case AsmToken::Real: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1180 | APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 1181 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1182 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1183 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 1184 | Lex(); // Eat token. |
| 1185 | return false; |
| 1186 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1187 | case AsmToken::Dot: { |
| 1188 | // This is a '.' reference, which references the current PC. Emit a |
| 1189 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1190 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1191 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1192 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1193 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 1194 | Lex(); // Eat identifier. |
| 1195 | return false; |
| 1196 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1197 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1198 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1199 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 1200 | case AsmToken::LBrac: |
| 1201 | if (!PlatformParser->HasBracketExpressions()) |
| 1202 | return TokError("brackets expression not supported on this target"); |
| 1203 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1204 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1205 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1206 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1207 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1208 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1209 | Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1210 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1211 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1212 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1213 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1214 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1215 | Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1216 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1217 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1218 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1219 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1220 | return true; |
Sanne Wouda | 9dfa6ad | 2017-03-10 13:08:20 +0000 | [diff] [blame] | 1221 | Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1222 | return false; |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 1223 | // MIPS unary expression operators. The lexer won't generate these tokens if |
| 1224 | // MCAsmInfo::HasMipsExpressions is false for the target. |
| 1225 | case AsmToken::PercentCall16: |
| 1226 | case AsmToken::PercentCall_Hi: |
| 1227 | case AsmToken::PercentCall_Lo: |
| 1228 | case AsmToken::PercentDtprel_Hi: |
| 1229 | case AsmToken::PercentDtprel_Lo: |
| 1230 | case AsmToken::PercentGot: |
| 1231 | case AsmToken::PercentGot_Disp: |
| 1232 | case AsmToken::PercentGot_Hi: |
| 1233 | case AsmToken::PercentGot_Lo: |
| 1234 | case AsmToken::PercentGot_Ofst: |
| 1235 | case AsmToken::PercentGot_Page: |
| 1236 | case AsmToken::PercentGottprel: |
| 1237 | case AsmToken::PercentGp_Rel: |
| 1238 | case AsmToken::PercentHi: |
| 1239 | case AsmToken::PercentHigher: |
| 1240 | case AsmToken::PercentHighest: |
| 1241 | case AsmToken::PercentLo: |
| 1242 | case AsmToken::PercentNeg: |
| 1243 | case AsmToken::PercentPcrel_Hi: |
| 1244 | case AsmToken::PercentPcrel_Lo: |
| 1245 | case AsmToken::PercentTlsgd: |
| 1246 | case AsmToken::PercentTlsldm: |
| 1247 | case AsmToken::PercentTprel_Hi: |
| 1248 | case AsmToken::PercentTprel_Lo: |
| 1249 | Lex(); // Eat the operator. |
| 1250 | if (Lexer.isNot(AsmToken::LParen)) |
| 1251 | return TokError("expected '(' after operator"); |
| 1252 | Lex(); // Eat the operator. |
| 1253 | if (parseExpression(Res, EndLoc)) |
| 1254 | return true; |
| 1255 | if (Lexer.isNot(AsmToken::RParen)) |
| 1256 | return TokError("expected ')'"); |
| 1257 | Lex(); // Eat the operator. |
| 1258 | Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); |
| 1259 | return !Res; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1262 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1263 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 1264 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1265 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1268 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1269 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1270 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 1271 | // Ask the target implementation about this expression first. |
| 1272 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 1273 | if (NewE) |
| 1274 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1275 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 1276 | // if there is exactly one symbol. |
| 1277 | switch (E->getKind()) { |
| 1278 | case MCExpr::Target: |
| 1279 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1280 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1281 | |
| 1282 | case MCExpr::SymbolRef: { |
| 1283 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 1284 | |
| 1285 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1286 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 1287 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1288 | return E; |
| 1289 | } |
| 1290 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1291 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | case MCExpr::Unary: { |
| 1295 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1296 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1297 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1298 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1299 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | case MCExpr::Binary: { |
| 1303 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1304 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1305 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1308 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1309 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1310 | if (!LHS) |
| 1311 | LHS = BE->getLHS(); |
| 1312 | if (!RHS) |
| 1313 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1314 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1315 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1318 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1319 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1322 | /// This function checks if the next token is <string> type or arithmetic. |
| 1323 | /// string that begin with character '<' must end with character '>'. |
| 1324 | /// otherwise it is arithmetics. |
| 1325 | /// If the function returns a 'true' value, |
| 1326 | /// the End argument will be filled with the last location pointed to the '>' |
| 1327 | /// character. |
| 1328 | |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 1329 | /// There is a gap between the AltMacro's documentation and the single quote |
| 1330 | /// implementation. GCC does not fully support this feature and so we will not |
| 1331 | /// support it. |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1332 | /// TODO: Adding single quote as a string. |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1333 | static bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { |
Craig Topper | c06ee83 | 2018-09-25 18:33:00 +0000 | [diff] [blame] | 1334 | assert((StrLoc.getPointer() != nullptr) && |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1335 | "Argument to the function cannot be a NULL value"); |
| 1336 | const char *CharPtr = StrLoc.getPointer(); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 1337 | while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && |
| 1338 | (*CharPtr != '\0')) { |
| 1339 | if (*CharPtr == '!') |
| 1340 | CharPtr++; |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 1341 | CharPtr++; |
| 1342 | } |
| 1343 | if (*CharPtr == '>') { |
| 1344 | EndLoc = StrLoc.getFromPointer(CharPtr + 1); |
| 1345 | return true; |
| 1346 | } |
| 1347 | return false; |
| 1348 | } |
| 1349 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1350 | /// creating a string without the escape characters '!'. |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1351 | static std::string altMacroString(StringRef AltMacroStr) { |
| 1352 | std::string Res; |
Michael Zuckerman | ff29879 | 2017-05-10 14:00:57 +0000 | [diff] [blame] | 1353 | for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1354 | if (AltMacroStr[Pos] == '!') |
| 1355 | Pos++; |
| 1356 | Res += AltMacroStr[Pos]; |
| 1357 | } |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 1358 | return Res; |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1361 | /// Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1362 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1363 | /// expr ::= expr &&,|| expr -> lowest. |
| 1364 | /// expr ::= expr |,^,&,! expr |
| 1365 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1366 | /// expr ::= expr <<,>> expr |
| 1367 | /// expr ::= expr +,- expr |
| 1368 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1369 | /// expr ::= primaryexpr |
| 1370 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1371 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1372 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1373 | Res = nullptr; |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1374 | if (getTargetParser().parsePrimaryExpr(Res, EndLoc) || |
| 1375 | parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1378 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1379 | // expression to include the modifier. This is inefficient, but in general we |
| 1380 | // expect users to use 'a@modifier op b'. |
| 1381 | if (Lexer.getKind() == AsmToken::At) { |
| 1382 | Lex(); |
| 1383 | |
| 1384 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1385 | return TokError("unexpected symbol modifier following '@'"); |
| 1386 | |
| 1387 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1388 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1389 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1390 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1391 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1392 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1393 | if (!ModifiedRes) { |
| 1394 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1395 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1396 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1397 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1398 | Res = ModifiedRes; |
| 1399 | Lex(); |
| 1400 | } |
| 1401 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 1402 | // Try to constant fold it up front, if possible. Do not exploit |
| 1403 | // assembler here. |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1404 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1405 | if (Res->evaluateAsAbsolute(Value)) |
| 1406 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1407 | |
| 1408 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1409 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1410 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1411 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1412 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1413 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1416 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1417 | SMLoc &EndLoc) { |
| 1418 | if (parseParenExpr(Res, EndLoc)) |
| 1419 | return true; |
| 1420 | |
| 1421 | for (; ParenDepth > 0; --ParenDepth) { |
| 1422 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1423 | return true; |
| 1424 | |
| 1425 | // We don't Lex() the last RParen. |
| 1426 | // This is the same behavior as parseParenExpression(). |
| 1427 | if (ParenDepth - 1 > 0) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 1428 | EndLoc = getTok().getEndLoc(); |
| 1429 | if (parseToken(AsmToken::RParen, |
| 1430 | "expected ')' in parentheses expression")) |
| 1431 | return true; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1437 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1438 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1439 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1440 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1441 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1442 | return true; |
| 1443 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 1444 | if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1445 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1446 | |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1450 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1451 | MCBinaryExpr::Opcode &Kind, |
| 1452 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1453 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1454 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1455 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1456 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1457 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1458 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1459 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1460 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1461 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1462 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1463 | return 1; |
| 1464 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1465 | // Low Precedence: |, &, ^ |
| 1466 | // |
| 1467 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1468 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1469 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1470 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1471 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1472 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1473 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1474 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1475 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1476 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1477 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1478 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1479 | case AsmToken::EqualEqual: |
| 1480 | Kind = MCBinaryExpr::EQ; |
| 1481 | return 3; |
| 1482 | case AsmToken::ExclaimEqual: |
| 1483 | case AsmToken::LessGreater: |
| 1484 | Kind = MCBinaryExpr::NE; |
| 1485 | return 3; |
| 1486 | case AsmToken::Less: |
| 1487 | Kind = MCBinaryExpr::LT; |
| 1488 | return 3; |
| 1489 | case AsmToken::LessEqual: |
| 1490 | Kind = MCBinaryExpr::LTE; |
| 1491 | return 3; |
| 1492 | case AsmToken::Greater: |
| 1493 | Kind = MCBinaryExpr::GT; |
| 1494 | return 3; |
| 1495 | case AsmToken::GreaterEqual: |
| 1496 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1497 | return 3; |
| 1498 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1499 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1500 | case AsmToken::LessLess: |
| 1501 | Kind = MCBinaryExpr::Shl; |
| 1502 | return 4; |
| 1503 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1504 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1505 | return 4; |
| 1506 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1507 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1508 | case AsmToken::Plus: |
| 1509 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1510 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1511 | case AsmToken::Minus: |
| 1512 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1513 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1514 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1515 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1516 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1517 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1518 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1519 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1520 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1521 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1522 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1523 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1524 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1528 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1529 | MCBinaryExpr::Opcode &Kind, |
| 1530 | bool ShouldUseLogicalShr) { |
| 1531 | switch (K) { |
| 1532 | default: |
| 1533 | return 0; // not a binop. |
| 1534 | |
| 1535 | // Lowest Precedence: &&, || |
| 1536 | case AsmToken::AmpAmp: |
| 1537 | Kind = MCBinaryExpr::LAnd; |
| 1538 | return 2; |
| 1539 | case AsmToken::PipePipe: |
| 1540 | Kind = MCBinaryExpr::LOr; |
| 1541 | return 1; |
| 1542 | |
| 1543 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1544 | case AsmToken::EqualEqual: |
| 1545 | Kind = MCBinaryExpr::EQ; |
| 1546 | return 3; |
| 1547 | case AsmToken::ExclaimEqual: |
| 1548 | case AsmToken::LessGreater: |
| 1549 | Kind = MCBinaryExpr::NE; |
| 1550 | return 3; |
| 1551 | case AsmToken::Less: |
| 1552 | Kind = MCBinaryExpr::LT; |
| 1553 | return 3; |
| 1554 | case AsmToken::LessEqual: |
| 1555 | Kind = MCBinaryExpr::LTE; |
| 1556 | return 3; |
| 1557 | case AsmToken::Greater: |
| 1558 | Kind = MCBinaryExpr::GT; |
| 1559 | return 3; |
| 1560 | case AsmToken::GreaterEqual: |
| 1561 | Kind = MCBinaryExpr::GTE; |
| 1562 | return 3; |
| 1563 | |
| 1564 | // Low Intermediate Precedence: +, - |
| 1565 | case AsmToken::Plus: |
| 1566 | Kind = MCBinaryExpr::Add; |
| 1567 | return 4; |
| 1568 | case AsmToken::Minus: |
| 1569 | Kind = MCBinaryExpr::Sub; |
| 1570 | return 4; |
| 1571 | |
| 1572 | // High Intermediate Precedence: |, &, ^ |
| 1573 | // |
| 1574 | // FIXME: gas seems to support '!' as an infix operator? |
| 1575 | case AsmToken::Pipe: |
| 1576 | Kind = MCBinaryExpr::Or; |
| 1577 | return 5; |
| 1578 | case AsmToken::Caret: |
| 1579 | Kind = MCBinaryExpr::Xor; |
| 1580 | return 5; |
| 1581 | case AsmToken::Amp: |
| 1582 | Kind = MCBinaryExpr::And; |
| 1583 | return 5; |
| 1584 | |
| 1585 | // Highest Precedence: *, /, %, <<, >> |
| 1586 | case AsmToken::Star: |
| 1587 | Kind = MCBinaryExpr::Mul; |
| 1588 | return 6; |
| 1589 | case AsmToken::Slash: |
| 1590 | Kind = MCBinaryExpr::Div; |
| 1591 | return 6; |
| 1592 | case AsmToken::Percent: |
| 1593 | Kind = MCBinaryExpr::Mod; |
| 1594 | return 6; |
| 1595 | case AsmToken::LessLess: |
| 1596 | Kind = MCBinaryExpr::Shl; |
| 1597 | return 6; |
| 1598 | case AsmToken::GreaterGreater: |
| 1599 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1600 | return 6; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1605 | MCBinaryExpr::Opcode &Kind) { |
| 1606 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1607 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1608 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1609 | } |
| 1610 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1611 | /// Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1612 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1613 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1614 | SMLoc &EndLoc) { |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1615 | SMLoc StartLoc = Lexer.getLoc(); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 1616 | while (true) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1617 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1618 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1620 | // If the next token is lower precedence than we are allowed to eat, return |
| 1621 | // successfully with what we ate already. |
| 1622 | if (TokPrec < Precedence) |
| 1623 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1624 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1625 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1627 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1628 | const MCExpr *RHS; |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1629 | if (getTargetParser().parsePrimaryExpr(RHS, EndLoc)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1630 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1632 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1633 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1634 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1635 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1636 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1637 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1638 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1639 | // Merge LHS and RHS according to operator. |
Chad Rosier | 9245e12 | 2017-01-19 20:06:32 +0000 | [diff] [blame] | 1640 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1644 | /// ParseStatement: |
| 1645 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1646 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1647 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1648 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1649 | MCAsmParserSemaCallback *SI) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1650 | assert(!hasPendingError() && "parseStatement started with pending error"); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1651 | // Eat initial spaces and comments |
| 1652 | while (Lexer.is(AsmToken::Space)) |
| 1653 | Lex(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1654 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1655 | // if this is a line comment we can drop it safely |
Nirav Dave | 670109d | 2017-06-09 14:04:03 +0000 | [diff] [blame] | 1656 | if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1657 | getTok().getString().front() == '\n') |
| 1658 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1659 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1660 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1661 | } |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1662 | // Statements always start with an identifier. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1663 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1664 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1665 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1666 | int64_t LocalLabelVal = -1; |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1667 | if (Lexer.is(AsmToken::HashDirective)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1668 | return parseCppHashLineFilenameComment(IDLoc); |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1669 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1670 | if (Lexer.is(AsmToken::Integer)) { |
| 1671 | LocalLabelVal = getTok().getIntVal(); |
| 1672 | if (LocalLabelVal < 0) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1673 | if (!TheCondState.Ignore) { |
| 1674 | Lex(); // always eat a token |
| 1675 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1676 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1677 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1678 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1679 | IDVal = getTok().getString(); |
| 1680 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1681 | if (Lexer.getKind() != AsmToken::Colon) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1682 | if (!TheCondState.Ignore) { |
| 1683 | Lex(); // always eat a token |
| 1684 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1685 | } |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1686 | } |
| 1687 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1688 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1689 | // Treat '.' as a valid identifier in this context. |
| 1690 | Lex(); |
| 1691 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1692 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1693 | // Treat '{' as a valid identifier in this context. |
| 1694 | Lex(); |
| 1695 | IDVal = "{"; |
| 1696 | |
| 1697 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1698 | // Treat '}' as a valid identifier in this context. |
| 1699 | Lex(); |
| 1700 | IDVal = "}"; |
Yonghong Song | 06ff655 | 2017-09-12 17:55:23 +0000 | [diff] [blame] | 1701 | } else if (Lexer.is(AsmToken::Star) && |
| 1702 | getTargetParser().starIsStartOfStatement()) { |
| 1703 | // Accept '*' as a valid start of statement. |
| 1704 | Lex(); |
| 1705 | IDVal = "*"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1706 | } else if (parseIdentifier(IDVal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1707 | if (!TheCondState.Ignore) { |
| 1708 | Lex(); // always eat a token |
| 1709 | return Error(IDLoc, "unexpected token at start of statement"); |
| 1710 | } |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1711 | IDVal = ""; |
| 1712 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1714 | // Handle conditional assembly here before checking for skipping. We |
| 1715 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1716 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1717 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1718 | DirectiveKindMap.find(IDVal); |
| 1719 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1720 | ? DK_NO_DIRECTIVE |
| 1721 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1722 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1723 | default: |
| 1724 | break; |
| 1725 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1726 | case DK_IFEQ: |
| 1727 | case DK_IFGE: |
| 1728 | case DK_IFGT: |
| 1729 | case DK_IFLE: |
| 1730 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1731 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1732 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1733 | case DK_IFB: |
| 1734 | return parseDirectiveIfb(IDLoc, true); |
| 1735 | case DK_IFNB: |
| 1736 | return parseDirectiveIfb(IDLoc, false); |
| 1737 | case DK_IFC: |
| 1738 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1739 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1740 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1741 | case DK_IFNC: |
| 1742 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1743 | case DK_IFNES: |
| 1744 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1745 | case DK_IFDEF: |
| 1746 | return parseDirectiveIfdef(IDLoc, true); |
| 1747 | case DK_IFNDEF: |
| 1748 | case DK_IFNOTDEF: |
| 1749 | return parseDirectiveIfdef(IDLoc, false); |
| 1750 | case DK_ELSEIF: |
| 1751 | return parseDirectiveElseIf(IDLoc); |
| 1752 | case DK_ELSE: |
| 1753 | return parseDirectiveElse(IDLoc); |
| 1754 | case DK_ENDIF: |
| 1755 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1756 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1757 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1758 | // Ignore the statement if in the middle of inactive conditional |
| 1759 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1760 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1761 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1762 | return false; |
| 1763 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1764 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1765 | // FIXME: Recurse on local labels? |
| 1766 | |
| 1767 | // See what kind of statement we have. |
| 1768 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1769 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1770 | if (!getTargetParser().isLabel(ID)) |
| 1771 | break; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 1772 | if (checkForValidSection()) |
| 1773 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1775 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1776 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1777 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1778 | // Diagnose attempt to use '.' as a label. |
| 1779 | if (IDVal == ".") |
| 1780 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1781 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1782 | // Diagnose attempt to use a variable as a label. |
| 1783 | // |
| 1784 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1785 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1786 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1787 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1788 | if (LocalLabelVal == -1) { |
| 1789 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1790 | StringRef RewrittenLabel = |
| 1791 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 1792 | assert(!RewrittenLabel.empty() && |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1793 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1794 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1795 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1796 | IDVal = RewrittenLabel; |
| 1797 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1798 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1799 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1800 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
Nirav Dave | 9263ae3 | 2016-08-02 19:17:54 +0000 | [diff] [blame] | 1801 | // End of Labels should be treated as end of line for lexing |
| 1802 | // purposes but that information is not available to the Lexer who |
| 1803 | // does not understand Labels. This may cause us to see a Hash |
| 1804 | // here instead of a preprocessor line comment. |
| 1805 | if (getTok().is(AsmToken::Hash)) { |
| 1806 | StringRef CommentStr = parseStringToEndOfStatement(); |
| 1807 | Lexer.Lex(); |
| 1808 | Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); |
| 1809 | } |
| 1810 | |
Nirav Dave | 8ea792d | 2016-07-13 14:03:12 +0000 | [diff] [blame] | 1811 | // Consume any end of statement token, if present, to avoid spurious |
| 1812 | // AddBlankLine calls(). |
| 1813 | if (getTok().is(AsmToken::EndOfStatement)) { |
| 1814 | Lex(); |
| 1815 | } |
| 1816 | |
Maya Madhavan | ec1efe4 | 2018-09-20 05:11:42 +0000 | [diff] [blame] | 1817 | getTargetParser().doBeforeLabelEmit(Sym); |
| 1818 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1819 | // Emit the label. |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 1820 | if (!getTargetParser().isParsingInlineAsm()) |
Rafael Espindola | be99157 | 2017-02-10 15:13:12 +0000 | [diff] [blame] | 1821 | Out.EmitLabel(Sym, IDLoc); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1822 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1823 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1824 | // info to make a dwarf label entry for this label if needed. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 1825 | if (enabledGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1826 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1827 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1828 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1829 | getTargetParser().onLabelParsed(Sym); |
| 1830 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1831 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1832 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1833 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1834 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1835 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1836 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1837 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1838 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1839 | |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 1840 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1841 | |
| 1842 | default: // Normal instruction or directive. |
| 1843 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1844 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1845 | |
| 1846 | // 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] | 1847 | if (areMacrosEnabled()) |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 1848 | if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1849 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1850 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1851 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1852 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1853 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1854 | // Directives start with "." |
Ana Pazos | 353f67a | 2018-08-25 01:34:32 +0000 | [diff] [blame] | 1855 | if (IDVal.startswith(".") && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1856 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1857 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1858 | // 1. The target-specific assembly parser. Some directives are target |
| 1859 | // specific or may potentially behave differently on certain targets. |
| 1860 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1861 | // (like the ELF parser) register themselves as extensions. |
| 1862 | // 3. The generic directive parser implemented by this class. These are |
| 1863 | // all the directives that behave in a target and platform independent |
| 1864 | // manner, or at least have a default behavior that's shared between |
| 1865 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1866 | |
Oliver Stannard | 2171828 | 2016-07-26 14:19:47 +0000 | [diff] [blame] | 1867 | getTargetParser().flushPendingInstructions(getStreamer()); |
| 1868 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 1869 | SMLoc StartTokLoc = getTok().getLoc(); |
| 1870 | bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); |
| 1871 | |
| 1872 | if (hasPendingError()) |
| 1873 | return true; |
| 1874 | // Currently the return value should be true if we are |
| 1875 | // uninterested but as this is at odds with the standard parsing |
| 1876 | // convention (return true = error) we have instances of a parsed |
| 1877 | // directive that fails returning true as an error. Catch these |
| 1878 | // cases as best as possible errors here. |
| 1879 | if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) |
| 1880 | return true; |
| 1881 | // Return if we did some parsing or believe we succeeded. |
| 1882 | if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1883 | return false; |
| 1884 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1885 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1886 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1887 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1888 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1889 | if (Handler.first) |
| 1890 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1891 | |
| 1892 | // Finally, if no one else is interested in this directive, it must be |
| 1893 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1894 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1895 | default: |
| 1896 | break; |
| 1897 | case DK_SET: |
| 1898 | case DK_EQU: |
| 1899 | return parseDirectiveSet(IDVal, true); |
| 1900 | case DK_EQUIV: |
| 1901 | return parseDirectiveSet(IDVal, false); |
| 1902 | case DK_ASCII: |
| 1903 | return parseDirectiveAscii(IDVal, false); |
| 1904 | case DK_ASCIZ: |
| 1905 | case DK_STRING: |
| 1906 | return parseDirectiveAscii(IDVal, true); |
| 1907 | case DK_BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1908 | case DK_DC_B: |
| 1909 | return parseDirectiveValue(IDVal, 1); |
| 1910 | case DK_DC: |
| 1911 | case DK_DC_W: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1912 | case DK_SHORT: |
| 1913 | case DK_VALUE: |
| 1914 | case DK_2BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1915 | return parseDirectiveValue(IDVal, 2); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1916 | case DK_LONG: |
| 1917 | case DK_INT: |
| 1918 | case DK_4BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1919 | case DK_DC_L: |
| 1920 | return parseDirectiveValue(IDVal, 4); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1921 | case DK_QUAD: |
| 1922 | case DK_8BYTE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1923 | return parseDirectiveValue(IDVal, 8); |
| 1924 | case DK_DC_A: |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1925 | return parseDirectiveValue( |
| 1926 | IDVal, getContext().getAsmInfo()->getCodePointerSize()); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1927 | case DK_OCTA: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1928 | return parseDirectiveOctaValue(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1929 | case DK_SINGLE: |
| 1930 | case DK_FLOAT: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1931 | case DK_DC_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1932 | return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1933 | case DK_DOUBLE: |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 1934 | case DK_DC_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1935 | return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1936 | case DK_ALIGN: { |
| 1937 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1938 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1939 | } |
| 1940 | case DK_ALIGN32: { |
| 1941 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1942 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1943 | } |
| 1944 | case DK_BALIGN: |
| 1945 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1946 | case DK_BALIGNW: |
| 1947 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1948 | case DK_BALIGNL: |
| 1949 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1950 | case DK_P2ALIGN: |
| 1951 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1952 | case DK_P2ALIGNW: |
| 1953 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1954 | case DK_P2ALIGNL: |
| 1955 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1956 | case DK_ORG: |
| 1957 | return parseDirectiveOrg(); |
| 1958 | case DK_FILL: |
| 1959 | return parseDirectiveFill(); |
| 1960 | case DK_ZERO: |
| 1961 | return parseDirectiveZero(); |
| 1962 | case DK_EXTERN: |
| 1963 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1964 | return false; |
| 1965 | case DK_GLOBL: |
| 1966 | case DK_GLOBAL: |
| 1967 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1968 | case DK_LAZY_REFERENCE: |
| 1969 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1970 | case DK_NO_DEAD_STRIP: |
| 1971 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1972 | case DK_SYMBOL_RESOLVER: |
| 1973 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1974 | case DK_PRIVATE_EXTERN: |
| 1975 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1976 | case DK_REFERENCE: |
| 1977 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1978 | case DK_WEAK_DEFINITION: |
| 1979 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1980 | case DK_WEAK_REFERENCE: |
| 1981 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1982 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1983 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1984 | case DK_COMM: |
| 1985 | case DK_COMMON: |
| 1986 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1987 | case DK_LCOMM: |
| 1988 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1989 | case DK_ABORT: |
| 1990 | return parseDirectiveAbort(); |
| 1991 | case DK_INCLUDE: |
| 1992 | return parseDirectiveInclude(); |
| 1993 | case DK_INCBIN: |
| 1994 | return parseDirectiveIncbin(); |
| 1995 | case DK_CODE16: |
| 1996 | case DK_CODE16GCC: |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 1997 | return TokError(Twine(IDVal) + |
| 1998 | " not currently supported for this target"); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1999 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 2000 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2001 | case DK_IRP: |
| 2002 | return parseDirectiveIrp(IDLoc); |
| 2003 | case DK_IRPC: |
| 2004 | return parseDirectiveIrpc(IDLoc); |
| 2005 | case DK_ENDR: |
| 2006 | return parseDirectiveEndr(IDLoc); |
| 2007 | case DK_BUNDLE_ALIGN_MODE: |
| 2008 | return parseDirectiveBundleAlignMode(); |
| 2009 | case DK_BUNDLE_LOCK: |
| 2010 | return parseDirectiveBundleLock(); |
| 2011 | case DK_BUNDLE_UNLOCK: |
| 2012 | return parseDirectiveBundleUnlock(); |
| 2013 | case DK_SLEB128: |
| 2014 | return parseDirectiveLEB128(true); |
| 2015 | case DK_ULEB128: |
| 2016 | return parseDirectiveLEB128(false); |
| 2017 | case DK_SPACE: |
| 2018 | case DK_SKIP: |
| 2019 | return parseDirectiveSpace(IDVal); |
| 2020 | case DK_FILE: |
| 2021 | return parseDirectiveFile(IDLoc); |
| 2022 | case DK_LINE: |
| 2023 | return parseDirectiveLine(); |
| 2024 | case DK_LOC: |
| 2025 | return parseDirectiveLoc(); |
| 2026 | case DK_STABS: |
| 2027 | return parseDirectiveStabs(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2028 | case DK_CV_FILE: |
| 2029 | return parseDirectiveCVFile(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 2030 | case DK_CV_FUNC_ID: |
| 2031 | return parseDirectiveCVFuncId(); |
| 2032 | case DK_CV_INLINE_SITE_ID: |
| 2033 | return parseDirectiveCVInlineSiteId(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2034 | case DK_CV_LOC: |
| 2035 | return parseDirectiveCVLoc(); |
| 2036 | case DK_CV_LINETABLE: |
| 2037 | return parseDirectiveCVLinetable(); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 2038 | case DK_CV_INLINE_LINETABLE: |
| 2039 | return parseDirectiveCVInlineLinetable(); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 2040 | case DK_CV_DEF_RANGE: |
| 2041 | return parseDirectiveCVDefRange(); |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 2042 | case DK_CV_STRING: |
| 2043 | return parseDirectiveCVString(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2044 | case DK_CV_STRINGTABLE: |
| 2045 | return parseDirectiveCVStringTable(); |
| 2046 | case DK_CV_FILECHECKSUMS: |
| 2047 | return parseDirectiveCVFileChecksums(); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 2048 | case DK_CV_FILECHECKSUM_OFFSET: |
| 2049 | return parseDirectiveCVFileChecksumOffset(); |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 2050 | case DK_CV_FPO_DATA: |
| 2051 | return parseDirectiveCVFPOData(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2052 | case DK_CFI_SECTIONS: |
| 2053 | return parseDirectiveCFISections(); |
| 2054 | case DK_CFI_STARTPROC: |
| 2055 | return parseDirectiveCFIStartProc(); |
| 2056 | case DK_CFI_ENDPROC: |
| 2057 | return parseDirectiveCFIEndProc(); |
| 2058 | case DK_CFI_DEF_CFA: |
| 2059 | return parseDirectiveCFIDefCfa(IDLoc); |
| 2060 | case DK_CFI_DEF_CFA_OFFSET: |
| 2061 | return parseDirectiveCFIDefCfaOffset(); |
| 2062 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 2063 | return parseDirectiveCFIAdjustCfaOffset(); |
| 2064 | case DK_CFI_DEF_CFA_REGISTER: |
| 2065 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 2066 | case DK_CFI_OFFSET: |
| 2067 | return parseDirectiveCFIOffset(IDLoc); |
| 2068 | case DK_CFI_REL_OFFSET: |
| 2069 | return parseDirectiveCFIRelOffset(IDLoc); |
| 2070 | case DK_CFI_PERSONALITY: |
| 2071 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 2072 | case DK_CFI_LSDA: |
| 2073 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 2074 | case DK_CFI_REMEMBER_STATE: |
| 2075 | return parseDirectiveCFIRememberState(); |
| 2076 | case DK_CFI_RESTORE_STATE: |
| 2077 | return parseDirectiveCFIRestoreState(); |
| 2078 | case DK_CFI_SAME_VALUE: |
| 2079 | return parseDirectiveCFISameValue(IDLoc); |
| 2080 | case DK_CFI_RESTORE: |
| 2081 | return parseDirectiveCFIRestore(IDLoc); |
| 2082 | case DK_CFI_ESCAPE: |
| 2083 | return parseDirectiveCFIEscape(); |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 2084 | case DK_CFI_RETURN_COLUMN: |
| 2085 | return parseDirectiveCFIReturnColumn(IDLoc); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2086 | case DK_CFI_SIGNAL_FRAME: |
| 2087 | return parseDirectiveCFISignalFrame(); |
| 2088 | case DK_CFI_UNDEFINED: |
| 2089 | return parseDirectiveCFIUndefined(IDLoc); |
| 2090 | case DK_CFI_REGISTER: |
| 2091 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 2092 | case DK_CFI_WINDOW_SAVE: |
| 2093 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2094 | case DK_MACROS_ON: |
| 2095 | case DK_MACROS_OFF: |
| 2096 | return parseDirectiveMacrosOnOff(IDVal); |
| 2097 | case DK_MACRO: |
| 2098 | return parseDirectiveMacro(IDLoc); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2099 | case DK_ALTMACRO: |
| 2100 | case DK_NOALTMACRO: |
| 2101 | return parseDirectiveAltmacro(IDVal); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2102 | case DK_EXITM: |
| 2103 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2104 | case DK_ENDM: |
| 2105 | case DK_ENDMACRO: |
| 2106 | return parseDirectiveEndMacro(IDVal); |
| 2107 | case DK_PURGEM: |
| 2108 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 2109 | case DK_END: |
| 2110 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 2111 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 2112 | return parseDirectiveError(IDLoc, false); |
| 2113 | case DK_ERROR: |
| 2114 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 2115 | case DK_WARNING: |
| 2116 | return parseDirectiveWarning(IDLoc); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2117 | case DK_RELOC: |
| 2118 | return parseDirectiveReloc(IDLoc); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2119 | case DK_DCB: |
| 2120 | case DK_DCB_W: |
| 2121 | return parseDirectiveDCB(IDVal, 2); |
| 2122 | case DK_DCB_B: |
| 2123 | return parseDirectiveDCB(IDVal, 1); |
| 2124 | case DK_DCB_D: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2125 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2126 | case DK_DCB_L: |
| 2127 | return parseDirectiveDCB(IDVal, 4); |
| 2128 | case DK_DCB_S: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2129 | return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2130 | case DK_DC_X: |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 2131 | case DK_DCB_X: |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 2132 | return TokError(Twine(IDVal) + |
| 2133 | " not currently supported for this target"); |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 2134 | case DK_DS: |
| 2135 | case DK_DS_W: |
| 2136 | return parseDirectiveDS(IDVal, 2); |
| 2137 | case DK_DS_B: |
| 2138 | return parseDirectiveDS(IDVal, 1); |
| 2139 | case DK_DS_D: |
| 2140 | return parseDirectiveDS(IDVal, 8); |
| 2141 | case DK_DS_L: |
| 2142 | case DK_DS_S: |
| 2143 | return parseDirectiveDS(IDVal, 4); |
| 2144 | case DK_DS_P: |
| 2145 | case DK_DS_X: |
| 2146 | return parseDirectiveDS(IDVal, 12); |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 2147 | case DK_PRINT: |
| 2148 | return parseDirectivePrint(IDLoc); |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 2149 | case DK_ADDRSIG: |
| 2150 | return parseDirectiveAddrsig(); |
| 2151 | case DK_ADDRSIG_SYM: |
| 2152 | return parseDirectiveAddrsigSym(); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 2153 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2154 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 2155 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2156 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 2157 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2158 | // __asm _emit or __asm __emit |
| 2159 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 2160 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2161 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 2162 | |
| 2163 | // __asm align |
| 2164 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2165 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2166 | |
Coby Tayree | 3847be9 | 2017-04-04 17:57:23 +0000 | [diff] [blame] | 2167 | if (ParsingInlineAsm && (IDVal == "even" || IDVal == "EVEN")) |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 2168 | Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2169 | if (checkForValidSection()) |
| 2170 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 2172 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2173 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 2174 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2175 | bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
| 2176 | Info.ParsedOperands); |
| 2177 | Info.ParseError = ParseHadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 2178 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2179 | // Dump the parsed representation, if requested. |
| 2180 | if (getShowParsedOperands()) { |
| 2181 | SmallString<256> Str; |
| 2182 | raw_svector_ostream OS(Str); |
| 2183 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2184 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2185 | if (i != 0) |
| 2186 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 2187 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2188 | } |
| 2189 | OS << "]"; |
| 2190 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2191 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2194 | // Fail even if ParseInstruction erroneously returns false. |
| 2195 | if (hasPendingError() || ParseHadError) |
| 2196 | return true; |
| 2197 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2198 | // If we are generating dwarf for the current section then generate a .loc |
| 2199 | // directive for the instruction. |
Paul Robinson | 938d9a0 | 2018-03-22 15:48:01 +0000 | [diff] [blame] | 2200 | if (!ParseHadError && enabledGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2201 | getContext().getGenDwarfSectionSyms().count( |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 2202 | getStreamer().getCurrentSectionOnly())) { |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 2203 | unsigned Line; |
| 2204 | if (ActiveMacros.empty()) |
| 2205 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 2206 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 2207 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 2208 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2209 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2210 | // If we previously parsed a cpp hash file line comment then make sure the |
| 2211 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 2212 | // 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] | 2213 | if (!CppHashInfo.Filename.empty()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2214 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2215 | 0, StringRef(), CppHashInfo.Filename); |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2216 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2217 | |
Graydon Hoare | 54fe208 | 2018-04-07 00:44:02 +0000 | [diff] [blame] | 2218 | unsigned CppHashLocLineNo = |
| 2219 | SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2220 | Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 2221 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 2222 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2223 | getStreamer().EmitDwarfLocDirective( |
| 2224 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 2225 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 2226 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 2229 | // If parsing succeeded, match the instruction. |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2230 | if (!ParseHadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 2231 | uint64_t ErrorInfo; |
Andrew V. Tischenko | c3c6723 | 2017-04-26 09:56:59 +0000 | [diff] [blame] | 2232 | if (getTargetParser().MatchAndEmitInstruction( |
| 2233 | IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| 2234 | getTargetParser().isParsingInlineAsm())) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2235 | return true; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 2236 | } |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 2237 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 2238 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 2239 | |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2240 | // Parse and erase curly braces marking block start/end |
Sam Clegg | f009da2 | 2018-04-19 22:00:53 +0000 | [diff] [blame] | 2241 | bool |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 2242 | AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { |
| 2243 | // Identify curly brace marking block start/end |
| 2244 | if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) |
| 2245 | return false; |
| 2246 | |
| 2247 | SMLoc StartLoc = Lexer.getLoc(); |
| 2248 | Lex(); // Eat the brace |
| 2249 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2250 | Lex(); // Eat EndOfStatement following the brace |
| 2251 | |
| 2252 | // Erase the block start/end brace from the output asm string |
| 2253 | AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - |
| 2254 | StartLoc.getPointer()); |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2258 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2259 | /// ::= # number "filename" |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2260 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2261 | Lex(); // Eat the hash token. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2262 | // Lexer only ever emits HashDirective if it fully formed if it's |
| 2263 | // done the checking already so this is an internal error. |
| 2264 | assert(getTok().is(AsmToken::Integer) && |
| 2265 | "Lexing Cpp line comment: Expected Integer"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2266 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2267 | Lex(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2268 | assert(getTok().is(AsmToken::String) && |
| 2269 | "Lexing Cpp line comment: Expected String"); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2270 | StringRef Filename = getTok().getString(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2271 | Lex(); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2272 | |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2273 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2274 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2275 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2276 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2277 | CppHashInfo.Loc = L; |
| 2278 | CppHashInfo.Filename = Filename; |
| 2279 | CppHashInfo.LineNumber = LineNumber; |
| 2280 | CppHashInfo.Buf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 2281 | return false; |
| 2282 | } |
| 2283 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2284 | /// will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2285 | /// for the Filename and LineNo if any in the diagnostic. |
| 2286 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2287 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2288 | raw_ostream &OS = errs(); |
| 2289 | |
| 2290 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2291 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2292 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2293 | unsigned CppHashBuf = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2294 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2295 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2296 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2297 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 2298 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 2299 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 2300 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2301 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 2302 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2305 | // 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] | 2306 | // manager changed or buffer changed (like in a nested include) then just |
| 2307 | // print the normal diagnostic using its Filename and LineNo. |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2308 | if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2309 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2310 | if (Parser->SavedDiagHandler) |
| 2311 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 2312 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2313 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2314 | return; |
| 2315 | } |
| 2316 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 2317 | // Use the CppHashFilename and calculate a line number based on the |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2318 | // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc |
| 2319 | // for the diagnostic. |
| 2320 | const std::string &Filename = Parser->CppHashInfo.Filename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2321 | |
| 2322 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 2323 | int CppHashLocLineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2324 | Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2325 | int LineNo = |
Tim Northover | c0bef99 | 2016-04-13 19:46:54 +0000 | [diff] [blame] | 2326 | Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2327 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2328 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 2329 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 2330 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2331 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 2332 | if (Parser->SavedDiagHandler) |
| 2333 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 2334 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2335 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2338 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 2339 | // difference being that that function accepts '@' as part of identifiers and |
| 2340 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 2341 | // '@' as a special case when needed. |
| 2342 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2343 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 2344 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2347 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 2348 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2349 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 2350 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2351 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2352 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2353 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2354 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2355 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2356 | // A macro without parameters is handled differently on Darwin: |
| 2357 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2358 | while (!Body.empty()) { |
| 2359 | // Scan for the next substitution. |
| 2360 | std::size_t End = Body.size(), Pos = 0; |
| 2361 | for (; Pos != End; ++Pos) { |
| 2362 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2363 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2364 | // This macro has no parameters, look for $0, $1, etc. |
| 2365 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 2366 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2367 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2368 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 2369 | if (Next == '$' || Next == 'n' || |
| 2370 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2371 | break; |
| 2372 | } else { |
| 2373 | // This macro has parameters, look for \foo, \bar, etc. |
| 2374 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 2375 | break; |
| 2376 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | // Add the prefix. |
| 2380 | OS << Body.slice(0, Pos); |
| 2381 | |
| 2382 | // Check if we reached the end. |
| 2383 | if (Pos == End) |
| 2384 | break; |
| 2385 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 2386 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2387 | switch (Body[Pos + 1]) { |
| 2388 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2389 | case '$': |
| 2390 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2391 | break; |
| 2392 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2393 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2394 | case 'n': |
| 2395 | OS << A.size(); |
| 2396 | break; |
| 2397 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2398 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2399 | default: { |
| 2400 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2401 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2402 | if (Index >= A.size()) |
| 2403 | break; |
| 2404 | |
| 2405 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2406 | for (const AsmToken &Token : A[Index]) |
| 2407 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2408 | break; |
| 2409 | } |
| 2410 | } |
| 2411 | Pos += 2; |
| 2412 | } else { |
| 2413 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2414 | |
| 2415 | // Check for the \@ pseudo-variable. |
| 2416 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2417 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2418 | else |
| 2419 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 2420 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2421 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2422 | const char *Begin = Body.data() + Pos + 1; |
| 2423 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2424 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2425 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2426 | if (Argument == "@") { |
| 2427 | OS << NumOfMacroInstantiations; |
| 2428 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2429 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2430 | for (; Index < NParameters; ++Index) |
| 2431 | if (Parameters[Index].Name == Argument) |
| 2432 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2433 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2434 | if (Index == NParameters) { |
| 2435 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 2436 | Pos += 3; |
| 2437 | else { |
| 2438 | OS << '\\' << Argument; |
| 2439 | Pos = I; |
| 2440 | } |
| 2441 | } else { |
| 2442 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2443 | for (const AsmToken &Token : A[Index]) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2444 | // For altmacro mode, you can write '%expr'. |
| 2445 | // The prefix '%' evaluates the expression 'expr' |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2446 | // and uses the result as a string (e.g. replace %(1+2) with the |
| 2447 | // string "3"). |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2448 | // Here, we identify the integer token which is the result of the |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2449 | // absolute expression evaluation and replace it with its string |
| 2450 | // representation. |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2451 | if (AltMacroMode && Token.getString().front() == '%' && |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2452 | Token.is(AsmToken::Integer)) |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2453 | // Emit an integer value to the buffer. |
| 2454 | OS << Token.getIntVal(); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2455 | // Only Token that was validated as a string and begins with '<' |
| 2456 | // is considered altMacroString!!! |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2457 | else if (AltMacroMode && Token.getString().front() == '<' && |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2458 | Token.is(AsmToken::String)) { |
Craig Topper | d2aab83 | 2018-09-25 20:13:55 +0000 | [diff] [blame] | 2459 | OS << altMacroString(Token.getStringContents()); |
Michael Zuckerman | 1f1a912 | 2017-05-10 13:08:11 +0000 | [diff] [blame] | 2460 | } |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2461 | // We expect no quotes around the string's contents when |
| 2462 | // parsing for varargs. |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2463 | else if (Token.isNot(AsmToken::String) || VarargParameter) |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2464 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2465 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2466 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2467 | |
| 2468 | Pos += 1 + Argument.size(); |
| 2469 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2470 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2471 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2472 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2473 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2474 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2475 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2476 | return false; |
| 2477 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2478 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2479 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2480 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2481 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2482 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2483 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2484 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2485 | switch (kind) { |
| 2486 | default: |
| 2487 | return false; |
| 2488 | case AsmToken::Plus: |
| 2489 | case AsmToken::Minus: |
| 2490 | case AsmToken::Tilde: |
| 2491 | case AsmToken::Slash: |
| 2492 | case AsmToken::Star: |
| 2493 | case AsmToken::Dot: |
| 2494 | case AsmToken::Equal: |
| 2495 | case AsmToken::EqualEqual: |
| 2496 | case AsmToken::Pipe: |
| 2497 | case AsmToken::PipePipe: |
| 2498 | case AsmToken::Caret: |
| 2499 | case AsmToken::Amp: |
| 2500 | case AsmToken::AmpAmp: |
| 2501 | case AsmToken::Exclaim: |
| 2502 | case AsmToken::ExclaimEqual: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2503 | case AsmToken::Less: |
| 2504 | case AsmToken::LessEqual: |
| 2505 | case AsmToken::LessLess: |
| 2506 | case AsmToken::LessGreater: |
| 2507 | case AsmToken::Greater: |
| 2508 | case AsmToken::GreaterEqual: |
| 2509 | case AsmToken::GreaterGreater: |
| 2510 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2514 | namespace { |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2515 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2516 | class AsmLexerSkipSpaceRAII { |
| 2517 | public: |
| 2518 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2519 | Lexer.setSkipSpace(SkipSpace); |
| 2520 | } |
| 2521 | |
| 2522 | ~AsmLexerSkipSpaceRAII() { |
| 2523 | Lexer.setSkipSpace(true); |
| 2524 | } |
| 2525 | |
| 2526 | private: |
| 2527 | AsmLexer &Lexer; |
| 2528 | }; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2529 | |
| 2530 | } // end anonymous namespace |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2531 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2532 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2533 | |
| 2534 | if (Vararg) { |
| 2535 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2536 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2537 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2538 | } |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2542 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2543 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2544 | // Darwin doesn't use spaces to delmit arguments. |
| 2545 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2546 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2547 | bool SpaceEaten; |
| 2548 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 2549 | while (true) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2550 | SpaceEaten = false; |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2551 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2552 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2553 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2554 | if (ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2555 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2556 | if (Lexer.is(AsmToken::Comma)) |
| 2557 | break; |
| 2558 | |
| 2559 | if (Lexer.is(AsmToken::Space)) { |
| 2560 | SpaceEaten = true; |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2561 | Lexer.Lex(); // Eat spaces |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2562 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Spaces can delimit parameters, but could also be part an expression. |
| 2565 | // If the token after a space is an operator, add the token and the next |
| 2566 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2567 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2568 | if (isOperator(Lexer.getKind())) { |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2569 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2570 | Lexer.Lex(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2571 | |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2572 | // Whitespace after an operator can be ignored. |
| 2573 | if (Lexer.is(AsmToken::Space)) |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2574 | Lexer.Lex(); |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2575 | |
| 2576 | continue; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2577 | } |
| 2578 | } |
Scott Egerton | a1fa68a | 2016-02-11 13:48:49 +0000 | [diff] [blame] | 2579 | if (SpaceEaten) |
| 2580 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2581 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2582 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2583 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2584 | // to be able to fill in the remaining default parameter values |
| 2585 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2586 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Adjust the current parentheses level. |
| 2589 | if (Lexer.is(AsmToken::LParen)) |
| 2590 | ++ParenLevel; |
| 2591 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2592 | --ParenLevel; |
| 2593 | |
| 2594 | // Append the token to the current argument list. |
| 2595 | MA.push_back(getTok()); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 2596 | Lexer.Lex(); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2597 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2598 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2599 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2600 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2601 | return false; |
| 2602 | } |
| 2603 | |
| 2604 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2605 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2606 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2607 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2608 | bool NamedParametersFound = false; |
| 2609 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2610 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2611 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2612 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2613 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2614 | // Parse two kinds of macro invocations: |
| 2615 | // - macros defined without any parameters accept an arbitrary number of them |
| 2616 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2617 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2618 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2619 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2620 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2621 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2622 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2623 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2624 | if (parseIdentifier(FA.Name)) |
| 2625 | return Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2626 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2627 | if (Lexer.isNot(AsmToken::Equal)) |
| 2628 | return TokError("expected '=' after formal parameter identifier"); |
| 2629 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2630 | Lex(); |
| 2631 | |
| 2632 | NamedParametersFound = true; |
| 2633 | } |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 2634 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2635 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2636 | if (NamedParametersFound && FA.Name.empty()) |
| 2637 | return Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2638 | |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2639 | SMLoc StrLoc = Lexer.getLoc(); |
| 2640 | SMLoc EndLoc; |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2641 | if (AltMacroMode && Lexer.is(AsmToken::Percent)) { |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2642 | const MCExpr *AbsoluteExp; |
| 2643 | int64_t Value; |
| 2644 | /// Eat '%' |
| 2645 | Lex(); |
| 2646 | if (parseExpression(AbsoluteExp, EndLoc)) |
| 2647 | return false; |
| 2648 | if (!AbsoluteExp->evaluateAsAbsolute(Value, |
| 2649 | getStreamer().getAssemblerPtr())) |
| 2650 | return Error(StrLoc, "expected absolute expression"); |
| 2651 | const char *StrChar = StrLoc.getPointer(); |
| 2652 | const char *EndChar = EndLoc.getPointer(); |
| 2653 | AsmToken newToken(AsmToken::Integer, |
| 2654 | StringRef(StrChar, EndChar - StrChar), Value); |
| 2655 | FA.Value.push_back(newToken); |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 2656 | } else if (AltMacroMode && Lexer.is(AsmToken::Less) && |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2657 | isAltmacroString(StrLoc, EndLoc)) { |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2658 | const char *StrChar = StrLoc.getPointer(); |
| 2659 | const char *EndChar = EndLoc.getPointer(); |
| 2660 | jumpToLoc(EndLoc, CurBuffer); |
| 2661 | /// Eat from '<' to '>' |
| 2662 | Lex(); |
| 2663 | AsmToken newToken(AsmToken::String, |
| 2664 | StringRef(StrChar, EndChar - StrChar)); |
| 2665 | FA.Value.push_back(newToken); |
Michael Zuckerman | 763e60e | 2017-05-04 10:37:00 +0000 | [diff] [blame] | 2666 | } else if(parseMacroArgument(FA.Value, Vararg)) |
Craig Topper | 3da977b | 2018-09-25 19:37:35 +0000 | [diff] [blame] | 2667 | return true; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2668 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2669 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2670 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2671 | unsigned FAI = 0; |
| 2672 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2673 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2674 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2675 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2676 | if (FAI >= NParameters) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2677 | assert(M && "expected macro to be defined"); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 2678 | return Error(IDLoc, "parameter named '" + FA.Name + |
| 2679 | "' does not exist for macro '" + M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2680 | } |
| 2681 | PI = FAI; |
| 2682 | } |
| 2683 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2684 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2685 | if (A.size() <= PI) |
| 2686 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2687 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2688 | |
| 2689 | if (FALocs.size() <= PI) |
| 2690 | FALocs.resize(PI + 1); |
| 2691 | |
| 2692 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2693 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2694 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2695 | // At the end of the statement, fill in remaining arguments that have |
| 2696 | // default values. If there aren't any, then the next argument is |
| 2697 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2698 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2699 | bool Failure = false; |
| 2700 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2701 | if (A[FAI].empty()) { |
| 2702 | if (M->Parameters[FAI].Required) { |
| 2703 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2704 | "missing value for required parameter " |
| 2705 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2706 | Failure = true; |
| 2707 | } |
| 2708 | |
| 2709 | if (!M->Parameters[FAI].Value.empty()) |
| 2710 | A[FAI] = M->Parameters[FAI].Value; |
| 2711 | } |
| 2712 | } |
| 2713 | return Failure; |
| 2714 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2715 | |
| 2716 | if (Lexer.is(AsmToken::Comma)) |
| 2717 | Lex(); |
| 2718 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2719 | |
| 2720 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2723 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Davide Italiano | 7c9fc73 | 2016-07-27 05:51:56 +0000 | [diff] [blame] | 2724 | // Arbitrarily limit macro nesting depth (default matches 'as'). We can |
| 2725 | // eliminate this, although we should protect against infinite loops. |
| 2726 | unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; |
| 2727 | if (ActiveMacros.size() == MaxNestingDepth) { |
| 2728 | std::ostringstream MaxNestingDepthError; |
| 2729 | MaxNestingDepthError << "macros cannot be nested more than " |
| 2730 | << MaxNestingDepth << " levels deep." |
| 2731 | << " Use -asm-macro-max-nesting-depth to increase " |
| 2732 | "this limit."; |
| 2733 | return TokError(MaxNestingDepthError.str()); |
| 2734 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2735 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2736 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2737 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2738 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2739 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2740 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2741 | // to hold the macro body with substitutions. |
| 2742 | SmallString<256> Buf; |
| 2743 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2744 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2745 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2746 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2747 | return true; |
| 2748 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2749 | // 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] | 2750 | // instantiation. |
| 2751 | OS << ".endmacro\n"; |
| 2752 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2753 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2754 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2755 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2756 | // Create the macro instantiation object and add to the current macro |
| 2757 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2758 | MacroInstantiation *MI = new MacroInstantiation( |
| 2759 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2760 | ActiveMacros.push_back(MI); |
| 2761 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2762 | ++NumOfMacroInstantiations; |
| 2763 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2764 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2765 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2766 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2767 | Lex(); |
| 2768 | |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2772 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2773 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2774 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2775 | Lex(); |
| 2776 | |
| 2777 | // Pop the instantiation entry. |
| 2778 | delete ActiveMacros.back(); |
| 2779 | ActiveMacros.pop_back(); |
| 2780 | } |
| 2781 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2782 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 2783 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2784 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2785 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2786 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 2787 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2788 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2789 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2790 | if (!Sym) { |
| 2791 | // In the case where we parse an expression starting with a '.', we will |
| 2792 | // not generate an error, nor will we create a symbol. In this case we |
| 2793 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2794 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2795 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2796 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2797 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2798 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2799 | if (NoDeadStrip) |
| 2800 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2801 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2802 | return false; |
| 2803 | } |
| 2804 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2805 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2806 | /// ::= identifier |
| 2807 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2808 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2809 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2810 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2811 | // 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] | 2812 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2813 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2814 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2815 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2816 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2817 | // Consume the prefix character, and check for a following identifier. |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2818 | |
| 2819 | AsmToken Buf[1]; |
| 2820 | Lexer.peekTokens(Buf, false); |
| 2821 | |
| 2822 | if (Buf[0].isNot(AsmToken::Identifier)) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2823 | return true; |
| 2824 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2825 | // 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] | 2826 | if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2827 | return true; |
| 2828 | |
Nirav Dave | d046332 | 2016-10-12 13:58:07 +0000 | [diff] [blame] | 2829 | // eat $ or @ |
| 2830 | Lexer.Lex(); // Lexer's Lex guarantees consecutive token. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2831 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2832 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2833 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2834 | Lex(); // Parser Lex to maintain invariants. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2835 | return false; |
| 2836 | } |
| 2837 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2838 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2839 | return true; |
| 2840 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2841 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2842 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2843 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2844 | |
| 2845 | return false; |
| 2846 | } |
| 2847 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2848 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2849 | /// ::= .equ identifier ',' expression |
| 2850 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2851 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2852 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2853 | StringRef Name; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2854 | if (check(parseIdentifier(Name), "expected identifier") || |
| 2855 | parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) |
| 2856 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
| 2857 | return false; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2860 | bool AsmParser::parseEscapedString(std::string &Data) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2861 | if (check(getTok().isNot(AsmToken::String), "expected string")) |
| 2862 | return true; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2863 | |
| 2864 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2865 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2866 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2867 | if (Str[i] != '\\') { |
| 2868 | Data += Str[i]; |
| 2869 | continue; |
| 2870 | } |
| 2871 | |
| 2872 | // Recognize escaped characters. Note that this escape semantics currently |
| 2873 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2874 | ++i; |
| 2875 | if (i == e) |
| 2876 | return TokError("unexpected backslash at end of string"); |
| 2877 | |
| 2878 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2879 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2880 | // Consume up to three octal characters. |
| 2881 | unsigned Value = Str[i] - '0'; |
| 2882 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2883 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2884 | ++i; |
| 2885 | Value = Value * 8 + (Str[i] - '0'); |
| 2886 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2887 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2888 | ++i; |
| 2889 | Value = Value * 8 + (Str[i] - '0'); |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | if (Value > 255) |
| 2894 | return TokError("invalid octal escape sequence (out of range)"); |
| 2895 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2896 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2897 | continue; |
| 2898 | } |
| 2899 | |
| 2900 | // Otherwise recognize individual escapes. |
| 2901 | switch (Str[i]) { |
| 2902 | default: |
| 2903 | // Just reject invalid escape sequences for now. |
| 2904 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2905 | |
| 2906 | case 'b': Data += '\b'; break; |
| 2907 | case 'f': Data += '\f'; break; |
| 2908 | case 'n': Data += '\n'; break; |
| 2909 | case 'r': Data += '\r'; break; |
| 2910 | case 't': Data += '\t'; break; |
| 2911 | case '"': Data += '"'; break; |
| 2912 | case '\\': Data += '\\'; break; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2916 | Lex(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2917 | return false; |
| 2918 | } |
| 2919 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2920 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2921 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2922 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2923 | auto parseOp = [&]() -> bool { |
| 2924 | std::string Data; |
| 2925 | if (checkForValidSection() || parseEscapedString(Data)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2926 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2927 | getStreamer().EmitBytes(Data); |
| 2928 | if (ZeroTerminated) |
| 2929 | getStreamer().EmitBytes(StringRef("\0", 1)); |
| 2930 | return false; |
| 2931 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2932 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2933 | if (parseMany(parseOp)) |
| 2934 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2935 | return false; |
| 2936 | } |
| 2937 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2938 | /// parseDirectiveReloc |
| 2939 | /// ::= .reloc expression , identifier [ , expression ] |
| 2940 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
| 2941 | const MCExpr *Offset; |
| 2942 | const MCExpr *Expr = nullptr; |
| 2943 | |
| 2944 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2945 | int64_t OffsetValue; |
| 2946 | // We can only deal with constant expressions at the moment. |
| 2947 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2948 | if (parseExpression(Offset)) |
| 2949 | return true; |
| 2950 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 2951 | if (check(!Offset->evaluateAsAbsolute(OffsetValue, |
| 2952 | getStreamer().getAssemblerPtr()), |
| 2953 | OffsetLoc, "expression is not a constant value") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2954 | check(OffsetValue < 0, OffsetLoc, "expression is negative") || |
| 2955 | parseToken(AsmToken::Comma, "expected comma") || |
| 2956 | check(getTok().isNot(AsmToken::Identifier), "expected relocation name")) |
| 2957 | return true; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2958 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2959 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
| 2960 | StringRef Name = Lexer.getTok().getIdentifier(); |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2961 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2962 | |
| 2963 | if (Lexer.is(AsmToken::Comma)) { |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 2964 | Lex(); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2965 | SMLoc ExprLoc = Lexer.getLoc(); |
| 2966 | if (parseExpression(Expr)) |
| 2967 | return true; |
| 2968 | |
| 2969 | MCValue Value; |
| 2970 | if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) |
| 2971 | return Error(ExprLoc, "expression must be relocatable"); |
| 2972 | } |
| 2973 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 2974 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 2975 | "unexpected token in .reloc directive")) |
| 2976 | return true; |
| 2977 | |
Peter Smith | 57f661b | 2018-06-06 09:40:06 +0000 | [diff] [blame] | 2978 | const MCTargetAsmParser &MCT = getTargetParser(); |
| 2979 | const MCSubtargetInfo &STI = MCT.getSTI(); |
| 2980 | if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc, STI)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 2981 | return Error(NameLoc, "unknown relocation name"); |
| 2982 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2983 | return false; |
| 2984 | } |
| 2985 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2986 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2987 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2988 | bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { |
| 2989 | auto parseOp = [&]() -> bool { |
| 2990 | const MCExpr *Value; |
| 2991 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2992 | if (checkForValidSection() || parseExpression(Value)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 2993 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 2994 | // Special case constant expressions to match code generator. |
| 2995 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2996 | assert(Size <= 8 && "Invalid size"); |
| 2997 | uint64_t IntValue = MCE->getValue(); |
| 2998 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2999 | return Error(ExprLoc, "out of range literal value"); |
| 3000 | getStreamer().EmitIntValue(IntValue, Size); |
| 3001 | } else |
| 3002 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 3003 | return false; |
| 3004 | }; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3005 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3006 | if (parseMany(parseOp)) |
| 3007 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3008 | return false; |
| 3009 | } |
| 3010 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3011 | static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) { |
| 3012 | if (Asm.getTok().isNot(AsmToken::Integer) && |
| 3013 | Asm.getTok().isNot(AsmToken::BigNum)) |
| 3014 | return Asm.TokError("unknown token in expression"); |
| 3015 | SMLoc ExprLoc = Asm.getTok().getLoc(); |
| 3016 | APInt IntValue = Asm.getTok().getAPIntVal(); |
| 3017 | Asm.Lex(); |
| 3018 | if (!IntValue.isIntN(128)) |
| 3019 | return Asm.Error(ExprLoc, "out of range literal value"); |
| 3020 | if (!IntValue.isIntN(64)) { |
| 3021 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
| 3022 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 3023 | } else { |
| 3024 | hi = 0; |
| 3025 | lo = IntValue.getZExtValue(); |
| 3026 | } |
| 3027 | return false; |
| 3028 | } |
| 3029 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3030 | /// ParseDirectiveOctaValue |
| 3031 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3032 | |
| 3033 | bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { |
| 3034 | auto parseOp = [&]() -> bool { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3035 | if (checkForValidSection()) |
| 3036 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3037 | uint64_t hi, lo; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3038 | if (parseHexOcta(*this, hi, lo)) |
| 3039 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3040 | if (MAI.isLittleEndian()) { |
| 3041 | getStreamer().EmitIntValue(lo, 8); |
| 3042 | getStreamer().EmitIntValue(hi, 8); |
| 3043 | } else { |
| 3044 | getStreamer().EmitIntValue(hi, 8); |
| 3045 | getStreamer().EmitIntValue(lo, 8); |
| 3046 | } |
| 3047 | return false; |
| 3048 | }; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3049 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3050 | if (parseMany(parseOp)) |
| 3051 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3052 | return false; |
| 3053 | } |
| 3054 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 3055 | bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { |
| 3056 | // We don't truly support arithmetic on floating point expressions, so we |
| 3057 | // have to manually parse unary prefixes. |
| 3058 | bool IsNeg = false; |
| 3059 | if (getLexer().is(AsmToken::Minus)) { |
| 3060 | Lexer.Lex(); |
| 3061 | IsNeg = true; |
| 3062 | } else if (getLexer().is(AsmToken::Plus)) |
| 3063 | Lexer.Lex(); |
| 3064 | |
| 3065 | if (Lexer.is(AsmToken::Error)) |
| 3066 | return TokError(Lexer.getErr()); |
| 3067 | if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && |
| 3068 | Lexer.isNot(AsmToken::Identifier)) |
| 3069 | return TokError("unexpected token in directive"); |
| 3070 | |
| 3071 | // Convert to an APFloat. |
| 3072 | APFloat Value(Semantics); |
| 3073 | StringRef IDVal = getTok().getString(); |
| 3074 | if (getLexer().is(AsmToken::Identifier)) { |
| 3075 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 3076 | Value = APFloat::getInf(Semantics); |
| 3077 | else if (!IDVal.compare_lower("nan")) |
| 3078 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 3079 | else |
| 3080 | return TokError("invalid floating point literal"); |
| 3081 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
| 3082 | APFloat::opInvalidOp) |
| 3083 | return TokError("invalid floating point literal"); |
| 3084 | if (IsNeg) |
| 3085 | Value.changeSign(); |
| 3086 | |
| 3087 | // Consume the numeric token. |
| 3088 | Lex(); |
| 3089 | |
| 3090 | Res = Value.bitcastToAPInt(); |
| 3091 | |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3095 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3096 | /// ::= (.single | .double) [ expression (, expression)* ] |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3097 | bool AsmParser::parseDirectiveRealValue(StringRef IDVal, |
| 3098 | const fltSemantics &Semantics) { |
| 3099 | auto parseOp = [&]() -> bool { |
| 3100 | APInt AsInt; |
| 3101 | if (checkForValidSection() || parseRealValue(Semantics, AsInt)) |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3102 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3103 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 3104 | AsInt.getBitWidth() / 8); |
| 3105 | return false; |
| 3106 | }; |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3107 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3108 | if (parseMany(parseOp)) |
| 3109 | return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 3110 | return false; |
| 3111 | } |
| 3112 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3113 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3114 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3115 | bool AsmParser::parseDirectiveZero() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3116 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 3117 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3118 | if (checkForValidSection() || parseExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3119 | return true; |
| 3120 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3121 | int64_t Val = 0; |
| 3122 | if (getLexer().is(AsmToken::Comma)) { |
| 3123 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3124 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 3125 | return true; |
| 3126 | } |
| 3127 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3128 | if (parseToken(AsmToken::EndOfStatement, |
| 3129 | "unexpected token in '.zero' directive")) |
| 3130 | return true; |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3131 | getStreamer().emitFill(*NumBytes, Val, NumBytesLoc); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 3132 | |
| 3133 | return false; |
| 3134 | } |
| 3135 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3136 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3137 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3138 | bool AsmParser::parseDirectiveFill() { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3139 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 3140 | const MCExpr *NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3141 | if (checkForValidSection() || parseExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3142 | return true; |
| 3143 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3144 | int64_t FillSize = 1; |
| 3145 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3146 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3147 | SMLoc SizeLoc, ExprLoc; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3148 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3149 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3150 | SizeLoc = getTok().getLoc(); |
| 3151 | if (parseAbsoluteExpression(FillSize)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3152 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3153 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3154 | ExprLoc = getTok().getLoc(); |
| 3155 | if (parseAbsoluteExpression(FillExpr)) |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3156 | return true; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 3157 | } |
| 3158 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3159 | if (parseToken(AsmToken::EndOfStatement, |
| 3160 | "unexpected token in '.fill' directive")) |
| 3161 | return true; |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3162 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3163 | if (FillSize < 0) { |
| 3164 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
Petr Hosek | 6abd38b | 2016-05-28 08:20:08 +0000 | [diff] [blame] | 3165 | return false; |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3166 | } |
| 3167 | if (FillSize > 8) { |
| 3168 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 3169 | FillSize = 8; |
| 3170 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3171 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 3172 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 3173 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 3174 | |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 3175 | getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 3176 | |
| 3177 | return false; |
| 3178 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3179 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3180 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3181 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3182 | bool AsmParser::parseDirectiveOrg() { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 3183 | const MCExpr *Offset; |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3184 | SMLoc OffsetLoc = Lexer.getLoc(); |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 3185 | if (checkForValidSection() || parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3186 | return true; |
| 3187 | |
| 3188 | // Parse optional fill expression. |
| 3189 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3190 | if (parseOptionalToken(AsmToken::Comma)) |
| 3191 | if (parseAbsoluteExpression(FillExpr)) |
| 3192 | return addErrorSuffix(" in '.org' directive"); |
| 3193 | if (parseToken(AsmToken::EndOfStatement)) |
| 3194 | return addErrorSuffix(" in '.org' directive"); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3195 | |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 3196 | getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 3197 | return false; |
| 3198 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3199 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3200 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3201 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3202 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3203 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3204 | int64_t Alignment; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3205 | SMLoc MaxBytesLoc; |
| 3206 | bool HasFillExpr = false; |
| 3207 | int64_t FillExpr = 0; |
| 3208 | int64_t MaxBytesToFill = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3209 | |
| 3210 | auto parseAlign = [&]() -> bool { |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3211 | if (parseAbsoluteExpression(Alignment)) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3212 | return true; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3213 | if (parseOptionalToken(AsmToken::Comma)) { |
| 3214 | // The fill expression can be omitted while specifying a maximum number of |
| 3215 | // alignment bytes, e.g: |
| 3216 | // .align 3,,4 |
| 3217 | if (getTok().isNot(AsmToken::Comma)) { |
| 3218 | HasFillExpr = true; |
| 3219 | if (parseAbsoluteExpression(FillExpr)) |
| 3220 | return true; |
| 3221 | } |
| 3222 | if (parseOptionalToken(AsmToken::Comma)) |
| 3223 | if (parseTokenLoc(MaxBytesLoc) || |
| 3224 | parseAbsoluteExpression(MaxBytesToFill)) |
| 3225 | return true; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3226 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3227 | return parseToken(AsmToken::EndOfStatement); |
| 3228 | }; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3229 | |
Coby Tayree | d483a10 | 2017-08-02 17:36:10 +0000 | [diff] [blame] | 3230 | if (checkForValidSection()) |
| 3231 | return addErrorSuffix(" in directive"); |
| 3232 | // Ignore empty '.p2align' directives for GNU-as compatibility |
| 3233 | if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) { |
| 3234 | Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored"); |
| 3235 | return parseToken(AsmToken::EndOfStatement); |
| 3236 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3237 | if (parseAlign()) |
| 3238 | return addErrorSuffix(" in directive"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3239 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3240 | // Always emit an alignment here even if we thrown an error. |
| 3241 | bool ReturnVal = false; |
| 3242 | |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3243 | // Compute alignment in bytes. |
| 3244 | if (IsPow2) { |
| 3245 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3246 | if (Alignment >= 32) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3247 | ReturnVal |= Error(AlignmentLoc, "invalid alignment value"); |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3248 | Alignment = 31; |
| 3249 | } |
| 3250 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 3251 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3252 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 3253 | // Reject alignments that aren't either a power of two or zero, |
| 3254 | // for gas compatibility. Alignment of zero is silently rounded |
| 3255 | // up to one. |
| 3256 | if (Alignment == 0) |
| 3257 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 3258 | if (!isPowerOf2_64(Alignment)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3259 | ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 3262 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3263 | if (MaxBytesLoc.isValid()) { |
| 3264 | if (MaxBytesToFill < 1) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3265 | ReturnVal |= Error(MaxBytesLoc, |
| 3266 | "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3267 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 3268 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 3272 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3273 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3274 | MaxBytesToFill = 0; |
| 3275 | } |
| 3276 | } |
| 3277 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3278 | // Check whether we should use optimal code alignment for this .align |
| 3279 | // directive. |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 3280 | const MCSection *Section = getStreamer().getCurrentSectionOnly(); |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 3281 | assert(Section && "must have section to emit alignment"); |
| 3282 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3283 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 3284 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3285 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3286 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 3287 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 3288 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 3289 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 3290 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3291 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3292 | return ReturnVal; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3295 | /// parseDirectiveFile |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3296 | /// ::= .file filename |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3297 | /// ::= .file number [directory] filename [md5 checksum] [source source-text] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3298 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3299 | // FIXME: I'm not sure what this is. |
| 3300 | int64_t FileNumber = -1; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3301 | if (getLexer().is(AsmToken::Integer)) { |
| 3302 | FileNumber = getTok().getIntVal(); |
| 3303 | Lex(); |
| 3304 | |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3305 | if (FileNumber < 0) |
| 3306 | return TokError("negative file number"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Paul Robinson | 37fbb92 | 2018-06-20 16:12:03 +0000 | [diff] [blame] | 3309 | std::string Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3310 | |
| 3311 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3312 | // Allow the strings to have escaped octal character sequence. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3313 | if (check(getTok().isNot(AsmToken::String), |
| 3314 | "unexpected token in '.file' directive") || |
| 3315 | parseEscapedString(Path)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3316 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3317 | |
| 3318 | StringRef Directory; |
| 3319 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3320 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3321 | if (getLexer().is(AsmToken::String)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3322 | if (check(FileNumber == -1, |
| 3323 | "explicit path specified, but no file number") || |
| 3324 | parseEscapedString(FilenameData)) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3325 | return true; |
| 3326 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3327 | Directory = Path; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3328 | } else { |
| 3329 | Filename = Path; |
| 3330 | } |
| 3331 | |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3332 | uint64_t MD5Hi, MD5Lo; |
| 3333 | bool HasMD5 = false; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3334 | |
| 3335 | Optional<StringRef> Source; |
| 3336 | bool HasSource = false; |
| 3337 | std::string SourceString; |
| 3338 | |
| 3339 | while (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3340 | StringRef Keyword; |
| 3341 | if (check(getTok().isNot(AsmToken::Identifier), |
| 3342 | "unexpected token in '.file' directive") || |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3343 | parseIdentifier(Keyword)) |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3344 | return true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3345 | if (Keyword == "md5") { |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3346 | HasMD5 = true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3347 | if (check(FileNumber == -1, |
| 3348 | "MD5 checksum specified, but no file number") || |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3349 | parseHexOcta(*this, MD5Hi, MD5Lo)) |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3350 | return true; |
| 3351 | } else if (Keyword == "source") { |
| 3352 | HasSource = true; |
| 3353 | if (check(FileNumber == -1, |
| 3354 | "source specified, but no file number") || |
| 3355 | check(getTok().isNot(AsmToken::String), |
| 3356 | "unexpected token in '.file' directive") || |
| 3357 | parseEscapedString(SourceString)) |
| 3358 | return true; |
| 3359 | } else { |
| 3360 | return TokError("unexpected token in '.file' directive"); |
| 3361 | } |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3362 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3363 | |
| 3364 | if (FileNumber == -1) |
| 3365 | getStreamer().EmitFileDirective(Filename); |
| 3366 | else { |
Brian Cain | 3e0ca57 | 2018-08-28 16:23:39 +0000 | [diff] [blame] | 3367 | // In case there is a -g option as well as debug info from directive .file, |
| 3368 | // we turn off the -g option, directly use the existing debug info instead. |
| 3369 | // Also reset any implicit ".file 0" for the assembler source. |
| 3370 | if (Ctx.getGenDwarfForAssembly()) { |
| 3371 | Ctx.getMCDwarfLineTable(0).resetRootFile(); |
| 3372 | Ctx.setGenDwarfForAssembly(false); |
| 3373 | } |
| 3374 | |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3375 | MD5::MD5Result *CKMem = nullptr; |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3376 | if (HasMD5) { |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3377 | CKMem = (MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1); |
Paul Robinson | 0195469 | 2018-04-11 15:14:05 +0000 | [diff] [blame] | 3378 | for (unsigned i = 0; i != 8; ++i) { |
| 3379 | CKMem->Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8)); |
| 3380 | CKMem->Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8)); |
| 3381 | } |
Paul Robinson | 29f5f98 | 2018-01-09 23:31:48 +0000 | [diff] [blame] | 3382 | } |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 3383 | if (HasSource) { |
| 3384 | char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size())); |
| 3385 | memcpy(SourceBuf, SourceString.data(), SourceString.size()); |
| 3386 | Source = StringRef(SourceBuf, SourceString.size()); |
| 3387 | } |
Paul Robinson | 547adca | 2018-06-21 16:42:03 +0000 | [diff] [blame] | 3388 | if (FileNumber == 0) { |
| 3389 | if (Ctx.getDwarfVersion() < 5) |
| 3390 | return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5"); |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3391 | getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source); |
Paul Robinson | 547adca | 2018-06-21 16:42:03 +0000 | [diff] [blame] | 3392 | } else { |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 3393 | Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective( |
| 3394 | FileNumber, Directory, Filename, CKMem, Source); |
| 3395 | if (!FileNumOrErr) |
| 3396 | return Error(DirectiveLoc, toString(FileNumOrErr.takeError())); |
| 3397 | FileNumber = FileNumOrErr.get(); |
| 3398 | } |
Paul Robinson | cc7344a | 2018-06-14 13:38:20 +0000 | [diff] [blame] | 3399 | // Alert the user if there are some .file directives with MD5 and some not. |
| 3400 | // But only do that once. |
| 3401 | if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) { |
| 3402 | ReportedInconsistentMD5 = true; |
| 3403 | return Warning(DirectiveLoc, "inconsistent use of MD5 checksums"); |
| 3404 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | return false; |
| 3408 | } |
| 3409 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3410 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3411 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3412 | bool AsmParser::parseDirectiveLine() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3413 | int64_t LineNumber; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3414 | if (getLexer().is(AsmToken::Integer)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3415 | if (parseIntToken(LineNumber, "unexpected token in '.line' directive")) |
| 3416 | return true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3417 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3418 | // FIXME: Do something with the .line. |
| 3419 | } |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3420 | if (parseToken(AsmToken::EndOfStatement, |
| 3421 | "unexpected token in '.line' directive")) |
| 3422 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3423 | |
| 3424 | return false; |
| 3425 | } |
| 3426 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3427 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3428 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 3429 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 3430 | /// The first number is a file number, must have been previously assigned with |
| 3431 | /// a .file directive, the second number is the line number and optionally the |
| 3432 | /// third number is a column position (zero if not specified). The remaining |
| 3433 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3434 | bool AsmParser::parseDirectiveLoc() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3435 | int64_t FileNumber = 0, LineNumber = 0; |
| 3436 | SMLoc Loc = getTok().getLoc(); |
| 3437 | if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") || |
Paul Robinson | 11539b0 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 3438 | check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3439 | "file number less than one in '.loc' directive") || |
| 3440 | check(!getContext().isValidDwarfFileNumber(FileNumber), Loc, |
| 3441 | "unassigned file number in '.loc' directive")) |
| 3442 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3443 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3444 | // optional |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3445 | if (getLexer().is(AsmToken::Integer)) { |
| 3446 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 3447 | if (LineNumber < 0) |
| 3448 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3449 | Lex(); |
| 3450 | } |
| 3451 | |
| 3452 | int64_t ColumnPos = 0; |
| 3453 | if (getLexer().is(AsmToken::Integer)) { |
| 3454 | ColumnPos = getTok().getIntVal(); |
| 3455 | if (ColumnPos < 0) |
| 3456 | return TokError("column position less than zero in '.loc' directive"); |
| 3457 | Lex(); |
| 3458 | } |
| 3459 | |
| 3460 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 3461 | unsigned Isa = 0; |
| 3462 | int64_t Discriminator = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3463 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3464 | auto parseLocOp = [&]() -> bool { |
| 3465 | StringRef Name; |
| 3466 | SMLoc Loc = getTok().getLoc(); |
| 3467 | if (parseIdentifier(Name)) |
| 3468 | return TokError("unexpected token in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3469 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3470 | if (Name == "basic_block") |
| 3471 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 3472 | else if (Name == "prologue_end") |
| 3473 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 3474 | else if (Name == "epilogue_begin") |
| 3475 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 3476 | else if (Name == "is_stmt") { |
| 3477 | Loc = getTok().getLoc(); |
| 3478 | const MCExpr *Value; |
| 3479 | if (parseExpression(Value)) |
| 3480 | return true; |
| 3481 | // The expression must be the constant 0 or 1. |
| 3482 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3483 | int Value = MCE->getValue(); |
| 3484 | if (Value == 0) |
| 3485 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 3486 | else if (Value == 1) |
| 3487 | Flags |= DWARF2_FLAG_IS_STMT; |
| 3488 | else |
| 3489 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3490 | } else { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3491 | 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] | 3492 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3493 | } else if (Name == "isa") { |
| 3494 | Loc = getTok().getLoc(); |
| 3495 | const MCExpr *Value; |
| 3496 | if (parseExpression(Value)) |
| 3497 | return true; |
| 3498 | // The expression must be a constant greater or equal to 0. |
| 3499 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3500 | int Value = MCE->getValue(); |
| 3501 | if (Value < 0) |
| 3502 | return Error(Loc, "isa number less than zero"); |
| 3503 | Isa = Value; |
| 3504 | } else { |
| 3505 | return Error(Loc, "isa number not a constant value"); |
| 3506 | } |
| 3507 | } else if (Name == "discriminator") { |
| 3508 | if (parseAbsoluteExpression(Discriminator)) |
| 3509 | return true; |
| 3510 | } else { |
| 3511 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3512 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3513 | return false; |
| 3514 | }; |
| 3515 | |
Nirav Dave | e2369aa | 2016-10-26 17:28:58 +0000 | [diff] [blame] | 3516 | if (parseMany(parseLocOp, false /*hasComma*/)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3517 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3518 | |
| 3519 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3520 | Isa, Discriminator, StringRef()); |
| 3521 | |
| 3522 | return false; |
| 3523 | } |
| 3524 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3525 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3526 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3527 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3528 | return TokError("unsupported directive '.stabs'"); |
| 3529 | } |
| 3530 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3531 | /// parseDirectiveCVFile |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3532 | /// ::= .cv_file number filename [checksum] [checksumkind] |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3533 | bool AsmParser::parseDirectiveCVFile() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3534 | SMLoc FileNumberLoc = getTok().getLoc(); |
| 3535 | int64_t FileNumber; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3536 | std::string Filename; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3537 | std::string Checksum; |
| 3538 | int64_t ChecksumKind = 0; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3539 | |
| 3540 | if (parseIntToken(FileNumber, |
| 3541 | "expected file number in '.cv_file' directive") || |
| 3542 | check(FileNumber < 1, FileNumberLoc, "file number less than one") || |
| 3543 | check(getTok().isNot(AsmToken::String), |
| 3544 | "unexpected token in '.cv_file' directive") || |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3545 | parseEscapedString(Filename)) |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 3546 | return true; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3547 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3548 | if (check(getTok().isNot(AsmToken::String), |
| 3549 | "unexpected token in '.cv_file' directive") || |
| 3550 | parseEscapedString(Checksum) || |
| 3551 | parseIntToken(ChecksumKind, |
| 3552 | "expected checksum kind in '.cv_file' directive") || |
| 3553 | parseToken(AsmToken::EndOfStatement, |
| 3554 | "unexpected token in '.cv_file' directive")) |
| 3555 | return true; |
| 3556 | } |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 3557 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3558 | Checksum = fromHex(Checksum); |
| 3559 | void *CKMem = Ctx.allocate(Checksum.size(), 1); |
| 3560 | memcpy(CKMem, Checksum.data(), Checksum.size()); |
| 3561 | ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem), |
| 3562 | Checksum.size()); |
| 3563 | |
| 3564 | if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes, |
| 3565 | static_cast<uint8_t>(ChecksumKind))) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3566 | return Error(FileNumberLoc, "file number already allocated"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3567 | |
| 3568 | return false; |
| 3569 | } |
| 3570 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3571 | bool AsmParser::parseCVFunctionId(int64_t &FunctionId, |
| 3572 | StringRef DirectiveName) { |
| 3573 | SMLoc Loc; |
| 3574 | return parseTokenLoc(Loc) || |
| 3575 | parseIntToken(FunctionId, "expected function id in '" + DirectiveName + |
| 3576 | "' directive") || |
| 3577 | check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc, |
| 3578 | "expected function id within range [0, UINT_MAX)"); |
| 3579 | } |
| 3580 | |
| 3581 | bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { |
| 3582 | SMLoc Loc; |
| 3583 | return parseTokenLoc(Loc) || |
| 3584 | parseIntToken(FileNumber, "expected integer in '" + DirectiveName + |
| 3585 | "' directive") || |
| 3586 | check(FileNumber < 1, Loc, "file number less than one in '" + |
| 3587 | DirectiveName + "' directive") || |
| 3588 | check(!getCVContext().isValidFileNumber(FileNumber), Loc, |
| 3589 | "unassigned file number in '" + DirectiveName + "' directive"); |
| 3590 | } |
| 3591 | |
| 3592 | /// parseDirectiveCVFuncId |
| 3593 | /// ::= .cv_func_id FunctionId |
| 3594 | /// |
| 3595 | /// Introduces a function ID that can be used with .cv_loc. |
| 3596 | bool AsmParser::parseDirectiveCVFuncId() { |
| 3597 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3598 | int64_t FunctionId; |
| 3599 | |
| 3600 | if (parseCVFunctionId(FunctionId, ".cv_func_id") || |
| 3601 | parseToken(AsmToken::EndOfStatement, |
| 3602 | "unexpected token in '.cv_func_id' directive")) |
| 3603 | return true; |
| 3604 | |
| 3605 | if (!getStreamer().EmitCVFuncIdDirective(FunctionId)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3606 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3607 | |
| 3608 | return false; |
| 3609 | } |
| 3610 | |
| 3611 | /// parseDirectiveCVInlineSiteId |
| 3612 | /// ::= .cv_inline_site_id FunctionId |
| 3613 | /// "within" IAFunc |
| 3614 | /// "inlined_at" IAFile IALine [IACol] |
| 3615 | /// |
| 3616 | /// Introduces a function ID that can be used with .cv_loc. Includes "inlined |
| 3617 | /// at" source location information for use in the line table of the caller, |
| 3618 | /// whether the caller is a real function or another inlined call site. |
| 3619 | bool AsmParser::parseDirectiveCVInlineSiteId() { |
| 3620 | SMLoc FunctionIdLoc = getTok().getLoc(); |
| 3621 | int64_t FunctionId; |
| 3622 | int64_t IAFunc; |
| 3623 | int64_t IAFile; |
| 3624 | int64_t IALine; |
| 3625 | int64_t IACol = 0; |
| 3626 | |
| 3627 | // FunctionId |
| 3628 | if (parseCVFunctionId(FunctionId, ".cv_inline_site_id")) |
| 3629 | return true; |
| 3630 | |
| 3631 | // "within" |
| 3632 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3633 | getTok().getIdentifier() != "within"), |
| 3634 | "expected 'within' identifier in '.cv_inline_site_id' directive")) |
| 3635 | return true; |
| 3636 | Lex(); |
| 3637 | |
| 3638 | // IAFunc |
| 3639 | if (parseCVFunctionId(IAFunc, ".cv_inline_site_id")) |
| 3640 | return true; |
| 3641 | |
| 3642 | // "inlined_at" |
| 3643 | if (check((getLexer().isNot(AsmToken::Identifier) || |
| 3644 | getTok().getIdentifier() != "inlined_at"), |
| 3645 | "expected 'inlined_at' identifier in '.cv_inline_site_id' " |
| 3646 | "directive") ) |
| 3647 | return true; |
| 3648 | Lex(); |
| 3649 | |
| 3650 | // IAFile IALine |
| 3651 | if (parseCVFileId(IAFile, ".cv_inline_site_id") || |
| 3652 | parseIntToken(IALine, "expected line number after 'inlined_at'")) |
| 3653 | return true; |
| 3654 | |
| 3655 | // [IACol] |
| 3656 | if (getLexer().is(AsmToken::Integer)) { |
| 3657 | IACol = getTok().getIntVal(); |
| 3658 | Lex(); |
| 3659 | } |
| 3660 | |
| 3661 | if (parseToken(AsmToken::EndOfStatement, |
| 3662 | "unexpected token in '.cv_inline_site_id' directive")) |
| 3663 | return true; |
| 3664 | |
| 3665 | if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, |
| 3666 | IALine, IACol, FunctionIdLoc)) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 3667 | return Error(FunctionIdLoc, "function id already allocated"); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3668 | |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3672 | /// parseDirectiveCVLoc |
| 3673 | /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] |
| 3674 | /// [is_stmt VALUE] |
| 3675 | /// The first number is a file number, must have been previously assigned with |
| 3676 | /// a .file directive, the second number is the line number and optionally the |
| 3677 | /// third number is a column position (zero if not specified). The remaining |
| 3678 | /// optional items are .loc sub-directives. |
| 3679 | bool AsmParser::parseDirectiveCVLoc() { |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3680 | SMLoc DirectiveLoc = getTok().getLoc(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3681 | int64_t FunctionId, FileNumber; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3682 | if (parseCVFunctionId(FunctionId, ".cv_loc") || |
| 3683 | parseCVFileId(FileNumber, ".cv_loc")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3684 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3685 | |
| 3686 | int64_t LineNumber = 0; |
| 3687 | if (getLexer().is(AsmToken::Integer)) { |
| 3688 | LineNumber = getTok().getIntVal(); |
| 3689 | if (LineNumber < 0) |
| 3690 | return TokError("line number less than zero in '.cv_loc' directive"); |
| 3691 | Lex(); |
| 3692 | } |
| 3693 | |
| 3694 | int64_t ColumnPos = 0; |
| 3695 | if (getLexer().is(AsmToken::Integer)) { |
| 3696 | ColumnPos = getTok().getIntVal(); |
| 3697 | if (ColumnPos < 0) |
| 3698 | return TokError("column position less than zero in '.cv_loc' directive"); |
| 3699 | Lex(); |
| 3700 | } |
| 3701 | |
| 3702 | bool PrologueEnd = false; |
| 3703 | uint64_t IsStmt = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3704 | |
| 3705 | auto parseOp = [&]() -> bool { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3706 | StringRef Name; |
| 3707 | SMLoc Loc = getTok().getLoc(); |
| 3708 | if (parseIdentifier(Name)) |
| 3709 | return TokError("unexpected token in '.cv_loc' directive"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3710 | if (Name == "prologue_end") |
| 3711 | PrologueEnd = true; |
| 3712 | else if (Name == "is_stmt") { |
| 3713 | Loc = getTok().getLoc(); |
| 3714 | const MCExpr *Value; |
| 3715 | if (parseExpression(Value)) |
| 3716 | return true; |
| 3717 | // The expression must be the constant 0 or 1. |
| 3718 | IsStmt = ~0ULL; |
| 3719 | if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) |
| 3720 | IsStmt = MCE->getValue(); |
| 3721 | |
| 3722 | if (IsStmt > 1) |
| 3723 | return Error(Loc, "is_stmt value not 0 or 1"); |
| 3724 | } else { |
| 3725 | return Error(Loc, "unknown sub-directive in '.cv_loc' directive"); |
| 3726 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3727 | return false; |
| 3728 | }; |
| 3729 | |
| 3730 | if (parseMany(parseOp, false /*hasComma*/)) |
| 3731 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3732 | |
| 3733 | getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3734 | ColumnPos, PrologueEnd, IsStmt, StringRef(), |
| 3735 | DirectiveLoc); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3736 | return false; |
| 3737 | } |
| 3738 | |
| 3739 | /// parseDirectiveCVLinetable |
| 3740 | /// ::= .cv_linetable FunctionId, FnStart, FnEnd |
| 3741 | bool AsmParser::parseDirectiveCVLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3742 | int64_t FunctionId; |
| 3743 | StringRef FnStartName, FnEndName; |
| 3744 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3745 | if (parseCVFunctionId(FunctionId, ".cv_linetable") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3746 | parseToken(AsmToken::Comma, |
| 3747 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3748 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3749 | "expected identifier in directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3750 | parseToken(AsmToken::Comma, |
| 3751 | "unexpected token in '.cv_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3752 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3753 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3754 | return true; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3755 | |
| 3756 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3757 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
| 3758 | |
| 3759 | getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym); |
| 3760 | return false; |
| 3761 | } |
| 3762 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3763 | /// parseDirectiveCVInlineLinetable |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3764 | /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3765 | bool AsmParser::parseDirectiveCVInlineLinetable() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3766 | int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; |
| 3767 | StringRef FnStartName, FnEndName; |
| 3768 | SMLoc Loc = getTok().getLoc(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3769 | if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3770 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3771 | parseIntToken( |
| 3772 | SourceFileId, |
| 3773 | "expected SourceField in '.cv_inline_linetable' directive") || |
| 3774 | check(SourceFileId <= 0, Loc, |
| 3775 | "File id less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3776 | parseTokenLoc(Loc) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3777 | parseIntToken( |
| 3778 | SourceLineNum, |
| 3779 | "expected SourceLineNum in '.cv_inline_linetable' directive") || |
| 3780 | check(SourceLineNum < 0, Loc, |
| 3781 | "Line number less than zero in '.cv_inline_linetable' directive") || |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 3782 | parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, |
| 3783 | "expected identifier in directive") || |
| 3784 | parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, |
| 3785 | "expected identifier in directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3786 | return true; |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 3787 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3788 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3789 | return true; |
| 3790 | |
| 3791 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); |
| 3792 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 3793 | getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, |
| 3794 | SourceLineNum, FnStartSym, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 3795 | FnEndSym); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 3796 | return false; |
| 3797 | } |
| 3798 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3799 | /// parseDirectiveCVDefRange |
| 3800 | /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* |
| 3801 | bool AsmParser::parseDirectiveCVDefRange() { |
| 3802 | SMLoc Loc; |
| 3803 | std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; |
| 3804 | while (getLexer().is(AsmToken::Identifier)) { |
| 3805 | Loc = getLexer().getLoc(); |
| 3806 | StringRef GapStartName; |
| 3807 | if (parseIdentifier(GapStartName)) |
| 3808 | return Error(Loc, "expected identifier in directive"); |
| 3809 | MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName); |
| 3810 | |
| 3811 | Loc = getLexer().getLoc(); |
| 3812 | StringRef GapEndName; |
| 3813 | if (parseIdentifier(GapEndName)) |
| 3814 | return Error(Loc, "expected identifier in directive"); |
| 3815 | MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName); |
| 3816 | |
| 3817 | Ranges.push_back({GapStartSym, GapEndSym}); |
| 3818 | } |
| 3819 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3820 | std::string FixedSizePortion; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3821 | if (parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3822 | parseEscapedString(FixedSizePortion)) |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3823 | return true; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 3824 | |
| 3825 | getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion); |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 3829 | /// parseDirectiveCVString |
| 3830 | /// ::= .cv_stringtable "string" |
| 3831 | bool AsmParser::parseDirectiveCVString() { |
| 3832 | std::string Data; |
| 3833 | if (checkForValidSection() || parseEscapedString(Data)) |
| 3834 | return addErrorSuffix(" in '.cv_string' directive"); |
| 3835 | |
| 3836 | // Put the string in the table and emit the offset. |
| 3837 | std::pair<StringRef, unsigned> Insertion = |
| 3838 | getCVContext().addToStringTable(Data); |
| 3839 | getStreamer().EmitIntValue(Insertion.second, 4); |
| 3840 | return false; |
| 3841 | } |
| 3842 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 3843 | /// parseDirectiveCVStringTable |
| 3844 | /// ::= .cv_stringtable |
| 3845 | bool AsmParser::parseDirectiveCVStringTable() { |
| 3846 | getStreamer().EmitCVStringTableDirective(); |
| 3847 | return false; |
| 3848 | } |
| 3849 | |
| 3850 | /// parseDirectiveCVFileChecksums |
| 3851 | /// ::= .cv_filechecksums |
| 3852 | bool AsmParser::parseDirectiveCVFileChecksums() { |
| 3853 | getStreamer().EmitCVFileChecksumsDirective(); |
| 3854 | return false; |
| 3855 | } |
| 3856 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 3857 | /// parseDirectiveCVFileChecksumOffset |
| 3858 | /// ::= .cv_filechecksumoffset fileno |
| 3859 | bool AsmParser::parseDirectiveCVFileChecksumOffset() { |
| 3860 | int64_t FileNo; |
| 3861 | if (parseIntToken(FileNo, "expected identifier in directive")) |
| 3862 | return true; |
| 3863 | if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) |
| 3864 | return true; |
| 3865 | getStreamer().EmitCVFileChecksumOffsetDirective(FileNo); |
| 3866 | return false; |
| 3867 | } |
| 3868 | |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 3869 | /// parseDirectiveCVFPOData |
| 3870 | /// ::= .cv_fpo_data procsym |
| 3871 | bool AsmParser::parseDirectiveCVFPOData() { |
| 3872 | SMLoc DirLoc = getLexer().getLoc(); |
| 3873 | StringRef ProcName; |
| 3874 | if (parseIdentifier(ProcName)) |
| 3875 | return TokError("expected symbol name"); |
| 3876 | if (parseEOL("unexpected tokens")) |
| 3877 | return addErrorSuffix(" in '.cv_fpo_data' directive"); |
| 3878 | MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); |
| 3879 | getStreamer().EmitCVFPOData(ProcSym, DirLoc); |
| 3880 | return false; |
| 3881 | } |
| 3882 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3883 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3884 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3885 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3886 | StringRef Name; |
| 3887 | bool EH = false; |
| 3888 | bool Debug = false; |
| 3889 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3890 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3891 | return TokError("Expected an identifier"); |
| 3892 | |
| 3893 | if (Name == ".eh_frame") |
| 3894 | EH = true; |
| 3895 | else if (Name == ".debug_frame") |
| 3896 | Debug = true; |
| 3897 | |
| 3898 | if (getLexer().is(AsmToken::Comma)) { |
| 3899 | Lex(); |
| 3900 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3901 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3902 | return TokError("Expected an identifier"); |
| 3903 | |
| 3904 | if (Name == ".eh_frame") |
| 3905 | EH = true; |
| 3906 | else if (Name == ".debug_frame") |
| 3907 | Debug = true; |
| 3908 | } |
| 3909 | |
| 3910 | getStreamer().EmitCFISections(EH, Debug); |
| 3911 | return false; |
| 3912 | } |
| 3913 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3914 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3915 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3916 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3917 | StringRef Simple; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 3918 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
| 3919 | if (check(parseIdentifier(Simple) || Simple != "simple", |
| 3920 | "unexpected token") || |
| 3921 | parseToken(AsmToken::EndOfStatement)) |
| 3922 | return addErrorSuffix(" in '.cfi_startproc' directive"); |
| 3923 | } |
Kristina Brooks | 1a41a11 | 2018-10-19 12:14:30 +0000 | [diff] [blame] | 3924 | |
| 3925 | // TODO(kristina): Deal with a corner case of incorrect diagnostic context |
| 3926 | // being produced if this directive is emitted as part of preprocessor macro |
| 3927 | // expansion which can *ONLY* happen if Clang's cc1as is the API consumer. |
| 3928 | // Tools like llvm-mc on the other hand are not affected by it, and report |
| 3929 | // correct context information. |
| 3930 | getStreamer().EmitCFIStartProc(!Simple.empty(), Lexer.getLoc()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3931 | return false; |
| 3932 | } |
| 3933 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3934 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3935 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3936 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3937 | getStreamer().EmitCFIEndProc(); |
| 3938 | return false; |
| 3939 | } |
| 3940 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3941 | /// parse register name or number. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3942 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3943 | SMLoc DirectiveLoc) { |
| 3944 | unsigned RegNo; |
| 3945 | |
| 3946 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3947 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3948 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3949 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3950 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3951 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3952 | |
| 3953 | return false; |
| 3954 | } |
| 3955 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3956 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3957 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3958 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3959 | int64_t Register = 0, Offset = 0; |
| 3960 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 3961 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3962 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3963 | return true; |
| 3964 | |
| 3965 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3966 | return false; |
| 3967 | } |
| 3968 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3969 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3970 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3971 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3972 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3973 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3974 | return true; |
| 3975 | |
| 3976 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3977 | return false; |
| 3978 | } |
| 3979 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3980 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3981 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3982 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 3983 | int64_t Register1 = 0, Register2 = 0; |
| 3984 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || |
| 3985 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 3986 | parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3987 | return true; |
| 3988 | |
| 3989 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3990 | return false; |
| 3991 | } |
| 3992 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3993 | /// parseDirectiveCFIWindowSave |
| 3994 | /// ::= .cfi_window_save |
| 3995 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3996 | getStreamer().EmitCFIWindowSave(); |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4000 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4001 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4002 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4003 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4004 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4005 | return true; |
| 4006 | |
| 4007 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 4008 | return false; |
| 4009 | } |
| 4010 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4011 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4012 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4013 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4014 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4015 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4016 | return true; |
| 4017 | |
| 4018 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 4019 | return false; |
| 4020 | } |
| 4021 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4022 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4023 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4024 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4025 | int64_t Register = 0; |
| 4026 | int64_t Offset = 0; |
| 4027 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4028 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 4029 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4030 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4031 | return true; |
| 4032 | |
| 4033 | getStreamer().EmitCFIOffset(Register, Offset); |
| 4034 | return false; |
| 4035 | } |
| 4036 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4037 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4038 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4039 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4040 | int64_t Register = 0, Offset = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4041 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4042 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || |
| 4043 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4044 | parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4045 | return true; |
| 4046 | |
| 4047 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 4048 | return false; |
| 4049 | } |
| 4050 | |
| 4051 | static bool isValidEncoding(int64_t Encoding) { |
| 4052 | if (Encoding & ~0xff) |
| 4053 | return false; |
| 4054 | |
| 4055 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 4056 | return true; |
| 4057 | |
| 4058 | const unsigned Format = Encoding & 0xf; |
| 4059 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 4060 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 4061 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 4062 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 4063 | return false; |
| 4064 | |
| 4065 | const unsigned Application = Encoding & 0x70; |
| 4066 | if (Application != dwarf::DW_EH_PE_absptr && |
| 4067 | Application != dwarf::DW_EH_PE_pcrel) |
| 4068 | return false; |
| 4069 | |
| 4070 | return true; |
| 4071 | } |
| 4072 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4073 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4074 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 4075 | /// ::= .cfi_personality encoding, [symbol_name] |
| 4076 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4077 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4078 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4079 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4080 | return true; |
| 4081 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 4082 | return false; |
| 4083 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4084 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4085 | if (check(!isValidEncoding(Encoding), "unsupported encoding.") || |
| 4086 | parseToken(AsmToken::Comma, "unexpected token in directive") || |
| 4087 | check(parseIdentifier(Name), "expected identifier in directive")) |
| 4088 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4089 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4090 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4091 | |
| 4092 | if (IsPersonality) |
| 4093 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 4094 | else |
| 4095 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 4096 | return false; |
| 4097 | } |
| 4098 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4099 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4100 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4101 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4102 | getStreamer().EmitCFIRememberState(); |
| 4103 | return false; |
| 4104 | } |
| 4105 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4106 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4107 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4108 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4109 | getStreamer().EmitCFIRestoreState(); |
| 4110 | return false; |
| 4111 | } |
| 4112 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4113 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4114 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4115 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4116 | int64_t Register = 0; |
| 4117 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4118 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4119 | return true; |
| 4120 | |
| 4121 | getStreamer().EmitCFISameValue(Register); |
| 4122 | return false; |
| 4123 | } |
| 4124 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4125 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4126 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4127 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4128 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4129 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4130 | return true; |
| 4131 | |
| 4132 | getStreamer().EmitCFIRestore(Register); |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4136 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4137 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4138 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4139 | std::string Values; |
| 4140 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4141 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4142 | return true; |
| 4143 | |
| 4144 | Values.push_back((uint8_t)CurrValue); |
| 4145 | |
| 4146 | while (getLexer().is(AsmToken::Comma)) { |
| 4147 | Lex(); |
| 4148 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4149 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4150 | return true; |
| 4151 | |
| 4152 | Values.push_back((uint8_t)CurrValue); |
| 4153 | } |
| 4154 | |
| 4155 | getStreamer().EmitCFIEscape(Values); |
| 4156 | return false; |
| 4157 | } |
| 4158 | |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 4159 | /// parseDirectiveCFIReturnColumn |
| 4160 | /// ::= .cfi_return_column register |
| 4161 | bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { |
| 4162 | int64_t Register = 0; |
| 4163 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 4164 | return true; |
| 4165 | getStreamer().EmitCFIReturnColumn(Register); |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4169 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4170 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4171 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4172 | if (parseToken(AsmToken::EndOfStatement, |
| 4173 | "unexpected token in '.cfi_signal_frame'")) |
| 4174 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4175 | |
| 4176 | getStreamer().EmitCFISignalFrame(); |
| 4177 | return false; |
| 4178 | } |
| 4179 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4180 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4181 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4182 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4183 | int64_t Register = 0; |
| 4184 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4185 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4186 | return true; |
| 4187 | |
| 4188 | getStreamer().EmitCFIUndefined(Register); |
| 4189 | return false; |
| 4190 | } |
| 4191 | |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4192 | /// parseDirectiveAltmacro |
| 4193 | /// ::= .altmacro |
| 4194 | /// ::= .noaltmacro |
| 4195 | bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { |
| 4196 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4197 | return TokError("unexpected token in '" + Directive + "' directive"); |
Craig Topper | c81aff7 | 2018-09-25 20:55:55 +0000 | [diff] [blame] | 4198 | AltMacroMode = (Directive == ".altmacro"); |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 4199 | return false; |
| 4200 | } |
| 4201 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4202 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4203 | /// ::= .macros_on |
| 4204 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4205 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4206 | if (parseToken(AsmToken::EndOfStatement, |
| 4207 | "unexpected token in '" + Directive + "' directive")) |
| 4208 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4209 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4210 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4211 | return false; |
| 4212 | } |
| 4213 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4214 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4215 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4216 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4217 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4218 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4219 | return TokError("expected identifier in '.macro' directive"); |
| 4220 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 4221 | if (getLexer().is(AsmToken::Comma)) |
| 4222 | Lex(); |
| 4223 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4224 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4225 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4226 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 4227 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4228 | return Error(Lexer.getLoc(), |
| 4229 | "Vararg parameter '" + Parameters.back().Name + |
| 4230 | "' should be last one in the list of parameters."); |
| 4231 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4232 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4233 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4234 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4235 | |
Coby Tayree | bedaae0 | 2017-04-08 20:29:03 +0000 | [diff] [blame] | 4236 | // Emit an error if two (or more) named parameters share the same name |
| 4237 | for (const MCAsmMacroParameter& CurrParam : Parameters) |
| 4238 | if (CurrParam.Name.equals(Parameter.Name)) |
| 4239 | return TokError("macro '" + Name + "' has multiple parameters" |
| 4240 | " named '" + Parameter.Name + "'"); |
| 4241 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4242 | if (Lexer.is(AsmToken::Colon)) { |
| 4243 | Lex(); // consume ':' |
| 4244 | |
| 4245 | SMLoc QualLoc; |
| 4246 | StringRef Qualifier; |
| 4247 | |
| 4248 | QualLoc = Lexer.getLoc(); |
| 4249 | if (parseIdentifier(Qualifier)) |
| 4250 | return Error(QualLoc, "missing parameter qualifier for " |
| 4251 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4252 | |
| 4253 | if (Qualifier == "req") |
| 4254 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 4255 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4256 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4257 | else |
| 4258 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 4259 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 4260 | } |
| 4261 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4262 | if (getLexer().is(AsmToken::Equal)) { |
| 4263 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4264 | |
| 4265 | SMLoc ParamLoc; |
| 4266 | |
| 4267 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 4268 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4269 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 4270 | |
| 4271 | if (Parameter.Required) |
| 4272 | Warning(ParamLoc, "pointless default value for required parameter " |
| 4273 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4274 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4275 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4276 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 4277 | |
| 4278 | if (getLexer().is(AsmToken::Comma)) |
| 4279 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4282 | // Eat just the end of statement. |
| 4283 | Lexer.Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4284 | |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4285 | // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4286 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4287 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4288 | // Lex the macro definition. |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 4289 | while (true) { |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4290 | // Ignore Lexing errors in macros. |
| 4291 | while (Lexer.is(AsmToken::Error)) { |
| 4292 | Lexer.Lex(); |
| 4293 | } |
| 4294 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4295 | // Check whether we have reached the end of the file. |
| 4296 | if (getLexer().is(AsmToken::Eof)) |
| 4297 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 4298 | |
| 4299 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4300 | if (getLexer().is(AsmToken::Identifier)) { |
| 4301 | if (getTok().getIdentifier() == ".endm" || |
| 4302 | getTok().getIdentifier() == ".endmacro") { |
| 4303 | if (MacroDepth == 0) { // Outermost macro. |
| 4304 | EndToken = getTok(); |
Nirav Dave | 1180e689 | 2016-06-02 17:15:05 +0000 | [diff] [blame] | 4305 | Lexer.Lex(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 4306 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4307 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 4308 | "' directive"); |
| 4309 | break; |
| 4310 | } else { |
| 4311 | // Otherwise we just found the end of an inner macro. |
| 4312 | --MacroDepth; |
| 4313 | } |
| 4314 | } else if (getTok().getIdentifier() == ".macro") { |
| 4315 | // We allow nested macros. Those aren't instantiated until the outermost |
| 4316 | // macro is expanded so just ignore them for now. |
| 4317 | ++MacroDepth; |
| 4318 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4322 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4325 | if (getContext().lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4326 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 4327 | } |
| 4328 | |
| 4329 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4330 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4331 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4332 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4333 | MCAsmMacro Macro(Name, Body, std::move(Parameters)); |
| 4334 | DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n"; |
| 4335 | Macro.dump()); |
| 4336 | getContext().defineMacro(Name, std::move(Macro)); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4337 | return false; |
| 4338 | } |
| 4339 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4340 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4341 | /// |
| 4342 | /// With the support added for named parameters there may be code out there that |
| 4343 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4344 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4345 | /// 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] | 4346 | /// 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] | 4347 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 4348 | /// warning that the positional parameter found in body which have no effect. |
| 4349 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 4350 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4351 | /// intended or change the macro to use the named parameters. It is possible |
| 4352 | /// this warning will trigger when the none of the named parameters are used |
| 4353 | /// 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] | 4354 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4355 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4356 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4357 | // If this macro is not defined with named parameters the warning we are |
| 4358 | // checking for here doesn't apply. |
| 4359 | unsigned NParameters = Parameters.size(); |
| 4360 | if (NParameters == 0) |
| 4361 | return; |
| 4362 | |
| 4363 | bool NamedParametersFound = false; |
| 4364 | bool PositionalParametersFound = false; |
| 4365 | |
| 4366 | // Look at the body of the macro for use of both the named parameters and what |
| 4367 | // are likely to be positional parameters. This is what expandMacro() is |
| 4368 | // doing when it finds the parameters in the body. |
| 4369 | while (!Body.empty()) { |
| 4370 | // Scan for the next possible parameter. |
| 4371 | std::size_t End = Body.size(), Pos = 0; |
| 4372 | for (; Pos != End; ++Pos) { |
| 4373 | // Check for a substitution or escape. |
| 4374 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 4375 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 4376 | break; |
| 4377 | |
| 4378 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 4379 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 4380 | continue; |
| 4381 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 4382 | if (Next == '$' || Next == 'n' || |
| 4383 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4384 | break; |
| 4385 | } |
| 4386 | |
| 4387 | // Check if we reached the end. |
| 4388 | if (Pos == End) |
| 4389 | break; |
| 4390 | |
| 4391 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4392 | switch (Body[Pos + 1]) { |
| 4393 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4394 | case '$': |
| 4395 | break; |
| 4396 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4397 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4398 | case 'n': |
| 4399 | PositionalParametersFound = true; |
| 4400 | break; |
| 4401 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4402 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4403 | default: { |
| 4404 | PositionalParametersFound = true; |
| 4405 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4406 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4407 | } |
| 4408 | Pos += 2; |
| 4409 | } else { |
| 4410 | unsigned I = Pos + 1; |
| 4411 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 4412 | ++I; |
| 4413 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4414 | const char *Begin = Body.data() + Pos + 1; |
| 4415 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4416 | unsigned Index = 0; |
| 4417 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4418 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4419 | break; |
| 4420 | |
| 4421 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4422 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 4423 | Pos += 3; |
| 4424 | else { |
| 4425 | Pos = I; |
| 4426 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 4427 | } else { |
| 4428 | NamedParametersFound = true; |
| 4429 | Pos += 1 + Argument.size(); |
| 4430 | } |
| 4431 | } |
| 4432 | // Update the scan point. |
| 4433 | Body = Body.substr(Pos); |
| 4434 | } |
| 4435 | |
| 4436 | if (!NamedParametersFound && PositionalParametersFound) |
| 4437 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 4438 | "used in macro body, possible positional parameter " |
| 4439 | "found in body which will have no effect"); |
| 4440 | } |
| 4441 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4442 | /// parseDirectiveExitMacro |
| 4443 | /// ::= .exitm |
| 4444 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4445 | if (parseToken(AsmToken::EndOfStatement, |
| 4446 | "unexpected token in '" + Directive + "' directive")) |
| 4447 | return true; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4448 | |
| 4449 | if (!isInsideMacroInstantiation()) |
| 4450 | return TokError("unexpected '" + Directive + "' in file, " |
| 4451 | "no current macro definition"); |
| 4452 | |
| 4453 | // Exit all conditionals that are active in the current macro. |
| 4454 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 4455 | TheCondState = TheCondStack.back(); |
| 4456 | TheCondStack.pop_back(); |
| 4457 | } |
| 4458 | |
| 4459 | handleMacroExit(); |
| 4460 | return false; |
| 4461 | } |
| 4462 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4463 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4464 | /// ::= .endm |
| 4465 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4466 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4467 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4468 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 4469 | |
| 4470 | // If we are inside a macro instantiation, terminate the current |
| 4471 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4472 | if (isInsideMacroInstantiation()) { |
| 4473 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4474 | return false; |
| 4475 | } |
| 4476 | |
| 4477 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 4478 | // .endmacro directives are handled during the macro definition parsing. |
| 4479 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4480 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4483 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4484 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4485 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4486 | StringRef Name; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4487 | SMLoc Loc; |
Nirav Dave | d8858ca | 2016-08-30 14:15:43 +0000 | [diff] [blame] | 4488 | if (parseTokenLoc(Loc) || |
| 4489 | check(parseIdentifier(Name), Loc, |
| 4490 | "expected identifier in '.purgem' directive") || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4491 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4492 | "unexpected token in '.purgem' directive")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4493 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4494 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4495 | if (!getContext().lookupMacro(Name)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4496 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 4497 | |
Rafael Espindola | 22c38a0 | 2018-02-14 16:34:27 +0000 | [diff] [blame] | 4498 | getContext().undefineMacro(Name); |
Oliver Stannard | c3e7c6d | 2018-03-06 15:32:34 +0000 | [diff] [blame] | 4499 | DEBUG_WITH_TYPE("asm-macros", dbgs() |
| 4500 | << "Un-defining macro: " << Name << "\n"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4501 | return false; |
| 4502 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4503 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4504 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4505 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4506 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4507 | // Expect a single argument: an expression that evaluates to a constant |
| 4508 | // in the inclusive range 0-30. |
| 4509 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4510 | int64_t AlignSizePow2; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4511 | if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) || |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4512 | parseToken(AsmToken::EndOfStatement, "unexpected token after expression " |
| 4513 | "in '.bundle_align_mode' " |
| 4514 | "directive") || |
| 4515 | check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc, |
| 4516 | "invalid bundle alignment size (expected between 0 and 30)")) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4517 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4518 | |
| 4519 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 4520 | // unsigned. |
| 4521 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 4522 | return false; |
| 4523 | } |
| 4524 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4525 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4526 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4527 | bool AsmParser::parseDirectiveBundleLock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4528 | if (checkForValidSection()) |
| 4529 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4530 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4531 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4532 | StringRef Option; |
| 4533 | SMLoc Loc = getTok().getLoc(); |
| 4534 | const char *kInvalidOptionError = |
| 4535 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4536 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4537 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4538 | if (check(parseIdentifier(Option), Loc, kInvalidOptionError) || |
| 4539 | check(Option != "align_to_end", Loc, kInvalidOptionError) || |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4540 | parseToken(AsmToken::EndOfStatement, |
| 4541 | "unexpected token after '.bundle_lock' directive option")) |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4542 | return true; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4543 | AlignToEnd = true; |
| 4544 | } |
| 4545 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 4546 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4547 | return false; |
| 4548 | } |
| 4549 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4550 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4551 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4552 | bool AsmParser::parseDirectiveBundleUnlock() { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4553 | if (checkForValidSection() || |
| 4554 | parseToken(AsmToken::EndOfStatement, |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4555 | "unexpected token in '.bundle_unlock' directive")) |
| 4556 | return true; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 4557 | |
| 4558 | getStreamer().EmitBundleUnlock(); |
| 4559 | return false; |
| 4560 | } |
| 4561 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4562 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4563 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4564 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame] | 4565 | SMLoc NumBytesLoc = Lexer.getLoc(); |
| 4566 | const MCExpr *NumBytes; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4567 | if (checkForValidSection() || parseExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4568 | return true; |
| 4569 | |
| 4570 | int64_t FillExpr = 0; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4571 | if (parseOptionalToken(AsmToken::Comma)) |
| 4572 | if (parseAbsoluteExpression(FillExpr)) |
| 4573 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
| 4574 | if (parseToken(AsmToken::EndOfStatement)) |
| 4575 | return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4576 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4577 | // 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] | 4578 | getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4579 | |
| 4580 | return false; |
| 4581 | } |
| 4582 | |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4583 | /// parseDirectiveDCB |
| 4584 | /// ::= .dcb.{b, l, w} expression, expression |
| 4585 | bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4586 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4587 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4588 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4589 | return true; |
| 4590 | |
| 4591 | if (NumValues < 0) { |
| 4592 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4593 | return false; |
| 4594 | } |
| 4595 | |
| 4596 | if (parseToken(AsmToken::Comma, |
| 4597 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4598 | return true; |
| 4599 | |
| 4600 | const MCExpr *Value; |
| 4601 | SMLoc ExprLoc = getLexer().getLoc(); |
| 4602 | if (parseExpression(Value)) |
| 4603 | return true; |
| 4604 | |
| 4605 | // Special case constant expressions to match code generator. |
| 4606 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 4607 | assert(Size <= 8 && "Invalid size"); |
| 4608 | uint64_t IntValue = MCE->getValue(); |
| 4609 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 4610 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4611 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4612 | getStreamer().EmitIntValue(IntValue, Size); |
| 4613 | } else { |
| 4614 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4615 | getStreamer().EmitValue(Value, Size, ExprLoc); |
| 4616 | } |
| 4617 | |
| 4618 | if (parseToken(AsmToken::EndOfStatement, |
| 4619 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4620 | return true; |
| 4621 | |
| 4622 | return false; |
| 4623 | } |
| 4624 | |
| 4625 | /// parseDirectiveRealDCB |
| 4626 | /// ::= .dcb.{d, s} expression, expression |
| 4627 | bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4628 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4629 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4630 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 4631 | return true; |
| 4632 | |
| 4633 | if (NumValues < 0) { |
| 4634 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4635 | return false; |
| 4636 | } |
| 4637 | |
| 4638 | if (parseToken(AsmToken::Comma, |
| 4639 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4640 | return true; |
| 4641 | |
| 4642 | APInt AsInt; |
| 4643 | if (parseRealValue(Semantics, AsInt)) |
| 4644 | return true; |
| 4645 | |
| 4646 | if (parseToken(AsmToken::EndOfStatement, |
| 4647 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4648 | return true; |
| 4649 | |
| 4650 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4651 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 4652 | AsInt.getBitWidth() / 8); |
| 4653 | |
| 4654 | return false; |
| 4655 | } |
| 4656 | |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4657 | /// parseDirectiveDS |
| 4658 | /// ::= .ds.{b, d, l, p, s, w, x} expression |
| 4659 | bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4660 | SMLoc NumValuesLoc = Lexer.getLoc(); |
| 4661 | int64_t NumValues; |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4662 | if (checkForValidSection() || parseAbsoluteExpression(NumValues)) |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 4663 | return true; |
| 4664 | |
| 4665 | if (NumValues < 0) { |
| 4666 | Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); |
| 4667 | return false; |
| 4668 | } |
| 4669 | |
| 4670 | if (parseToken(AsmToken::EndOfStatement, |
| 4671 | "unexpected token in '" + Twine(IDVal) + "' directive")) |
| 4672 | return true; |
| 4673 | |
| 4674 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 4675 | getStreamer().emitFill(Size, 0); |
| 4676 | |
| 4677 | return false; |
| 4678 | } |
| 4679 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4680 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4681 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4682 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4683 | if (checkForValidSection()) |
| 4684 | return true; |
| 4685 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4686 | auto parseOp = [&]() -> bool { |
| 4687 | const MCExpr *Value; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4688 | if (parseExpression(Value)) |
| 4689 | return true; |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 4690 | if (Signed) |
| 4691 | getStreamer().EmitSLEB128Value(Value); |
| 4692 | else |
| 4693 | getStreamer().EmitULEB128Value(Value); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4694 | return false; |
| 4695 | }; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4696 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4697 | if (parseMany(parseOp)) |
| 4698 | return addErrorSuffix(" in directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4699 | |
| 4700 | return false; |
| 4701 | } |
| 4702 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4703 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4704 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4705 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4706 | auto parseOp = [&]() -> bool { |
| 4707 | StringRef Name; |
| 4708 | SMLoc Loc = getTok().getLoc(); |
| 4709 | if (parseIdentifier(Name)) |
| 4710 | return Error(Loc, "expected identifier"); |
| 4711 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4712 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4713 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 4714 | if (Sym->isTemporary()) |
| 4715 | return Error(Loc, "non-local symbol required"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4716 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4717 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 4718 | return Error(Loc, "unable to emit symbol attribute"); |
| 4719 | return false; |
| 4720 | }; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4721 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4722 | if (parseMany(parseOp)) |
| 4723 | return addErrorSuffix(" in directive"); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 4724 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 4725 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4726 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4727 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4728 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4729 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Nirav Dave | f43cc9f | 2016-10-10 15:24:54 +0000 | [diff] [blame] | 4730 | if (checkForValidSection()) |
| 4731 | return true; |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 4732 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4733 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 4734 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4735 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4736 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4737 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 4738 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4739 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4740 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4741 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4742 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4743 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4744 | |
| 4745 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4746 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4747 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4748 | return true; |
| 4749 | |
| 4750 | int64_t Pow2Alignment = 0; |
| 4751 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4752 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4753 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4754 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4755 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4756 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4757 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4758 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 4759 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4760 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 4761 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4762 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 4763 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 4764 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 4765 | if (!isPowerOf2_64(Pow2Alignment)) |
| 4766 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 4767 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 4768 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4769 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4770 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4771 | if (parseToken(AsmToken::EndOfStatement, |
| 4772 | "unexpected token in '.comm' or '.lcomm' directive")) |
| 4773 | return true; |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4774 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4775 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 4776 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4777 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4778 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4779 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4780 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 4781 | // 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] | 4782 | // may internally end up wanting an alignment in bytes. |
| 4783 | // FIXME: Diagnose overflow. |
| 4784 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4785 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4786 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4787 | |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 4788 | Sym->redefineIfPossible(); |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 4789 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4790 | return Error(IDLoc, "invalid symbol redefinition"); |
| 4791 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 4792 | // 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] | 4793 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 4794 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 4795 | return false; |
| 4796 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4797 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4798 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 4799 | return false; |
| 4800 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 4801 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4802 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4803 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4804 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4805 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4806 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4807 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4808 | StringRef Str = parseStringToEndOfStatement(); |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4809 | if (parseToken(AsmToken::EndOfStatement, |
| 4810 | "unexpected token in '.abort' directive")) |
| 4811 | return true; |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4812 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4813 | if (Str.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4814 | return Error(Loc, ".abort detected. Assembly stopping."); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 4815 | else |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4816 | return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 4817 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 4818 | |
| 4819 | return false; |
| 4820 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 4821 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4822 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4823 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4824 | bool AsmParser::parseDirectiveInclude() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4825 | // Allow the strings to have escaped octal character sequence. |
| 4826 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4827 | SMLoc IncludeLoc = getTok().getLoc(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4828 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4829 | if (check(getTok().isNot(AsmToken::String), |
| 4830 | "expected string in '.include' directive") || |
| 4831 | parseEscapedString(Filename) || |
| 4832 | check(getTok().isNot(AsmToken::EndOfStatement), |
| 4833 | "unexpected token in '.include' directive") || |
| 4834 | // Attempt to switch the lexer to the included file before consuming the |
| 4835 | // end of statement to avoid losing it when we switch. |
| 4836 | check(enterIncludeFile(Filename), IncludeLoc, |
| 4837 | "Could not find include file '" + Filename + "'")) |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 4838 | return true; |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 4839 | |
| 4840 | return false; |
| 4841 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 4842 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4843 | /// parseDirectiveIncbin |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4844 | /// ::= .incbin "filename" [ , skip [ , count ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4845 | bool AsmParser::parseDirectiveIncbin() { |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4846 | // Allow the strings to have escaped octal character sequence. |
| 4847 | std::string Filename; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4848 | SMLoc IncbinLoc = getTok().getLoc(); |
| 4849 | if (check(getTok().isNot(AsmToken::String), |
| 4850 | "expected string in '.incbin' directive") || |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4851 | parseEscapedString(Filename)) |
| 4852 | return true; |
| 4853 | |
| 4854 | int64_t Skip = 0; |
| 4855 | const MCExpr *Count = nullptr; |
| 4856 | SMLoc SkipLoc, CountLoc; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4857 | if (parseOptionalToken(AsmToken::Comma)) { |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4858 | // The skip expression can be omitted while specifying the count, e.g: |
| 4859 | // .incbin "filename",,4 |
| 4860 | if (getTok().isNot(AsmToken::Comma)) { |
| 4861 | if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip)) |
| 4862 | return true; |
| 4863 | } |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 4864 | if (parseOptionalToken(AsmToken::Comma)) { |
| 4865 | CountLoc = getTok().getLoc(); |
| 4866 | if (parseExpression(Count)) |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4867 | return true; |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | if (parseToken(AsmToken::EndOfStatement, |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4872 | "unexpected token in '.incbin' directive")) |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 4873 | return true; |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4874 | |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4875 | if (check(Skip < 0, SkipLoc, "skip is negative")) |
| 4876 | return true; |
| 4877 | |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4878 | // Attempt to process the included file. |
Petr Hosek | 2f4ac44 | 2016-09-23 00:41:06 +0000 | [diff] [blame] | 4879 | if (processIncbinFile(Filename, Skip, Count, CountLoc)) |
Nirav Dave | 1ab7199 | 2016-07-18 19:35:21 +0000 | [diff] [blame] | 4880 | return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 4881 | return false; |
| 4882 | } |
| 4883 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4884 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4885 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 4886 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4887 | TheCondStack.push_back(TheCondState); |
| 4888 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4889 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4890 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4891 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4892 | int64_t ExprValue; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4893 | if (parseAbsoluteExpression(ExprValue) || |
| 4894 | parseToken(AsmToken::EndOfStatement, |
| 4895 | "unexpected token in '.if' directive")) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4896 | return true; |
| 4897 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4898 | switch (DirKind) { |
| 4899 | default: |
| 4900 | llvm_unreachable("unsupported directive"); |
| 4901 | case DK_IF: |
| 4902 | case DK_IFNE: |
| 4903 | break; |
| 4904 | case DK_IFEQ: |
| 4905 | ExprValue = ExprValue == 0; |
| 4906 | break; |
| 4907 | case DK_IFGE: |
| 4908 | ExprValue = ExprValue >= 0; |
| 4909 | break; |
| 4910 | case DK_IFGT: |
| 4911 | ExprValue = ExprValue > 0; |
| 4912 | break; |
| 4913 | case DK_IFLE: |
| 4914 | ExprValue = ExprValue <= 0; |
| 4915 | break; |
| 4916 | case DK_IFLT: |
| 4917 | ExprValue = ExprValue < 0; |
| 4918 | break; |
| 4919 | } |
| 4920 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4921 | TheCondState.CondMet = ExprValue; |
| 4922 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4923 | } |
| 4924 | |
| 4925 | return false; |
| 4926 | } |
| 4927 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4928 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4929 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4930 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4931 | TheCondStack.push_back(TheCondState); |
| 4932 | TheCondState.TheCond = AsmCond::IfCond; |
| 4933 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4934 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4935 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4936 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4937 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4938 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4939 | if (parseToken(AsmToken::EndOfStatement, |
| 4940 | "unexpected token in '.ifb' directive")) |
| 4941 | return true; |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4942 | |
| 4943 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 4944 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4945 | } |
| 4946 | |
| 4947 | return false; |
| 4948 | } |
| 4949 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4950 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4951 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4952 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4953 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4954 | TheCondStack.push_back(TheCondState); |
| 4955 | TheCondState.TheCond = AsmCond::IfCond; |
| 4956 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4957 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4958 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4959 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4960 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4961 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4962 | if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive")) |
| 4963 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4964 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4965 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4966 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 4967 | if (parseToken(AsmToken::EndOfStatement, |
| 4968 | "unexpected token in '.ifc' directive")) |
| 4969 | return true; |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4970 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4971 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4972 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4973 | } |
| 4974 | |
| 4975 | return false; |
| 4976 | } |
| 4977 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4978 | /// parseDirectiveIfeqs |
| 4979 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4980 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4981 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4982 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4983 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 4984 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | StringRef String1 = getTok().getStringContents(); |
| 4988 | Lex(); |
| 4989 | |
| 4990 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4991 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 4992 | return TokError( |
| 4993 | "expected comma after first string for '.ifeqs' directive"); |
| 4994 | return TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | Lex(); |
| 4998 | |
| 4999 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5000 | if (ExpectEqual) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5001 | return TokError("expected string parameter for '.ifeqs' directive"); |
| 5002 | return TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | StringRef String2 = getTok().getStringContents(); |
| 5006 | Lex(); |
| 5007 | |
| 5008 | TheCondStack.push_back(TheCondState); |
| 5009 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5010 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5011 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5012 | |
| 5013 | return false; |
| 5014 | } |
| 5015 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5016 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 5017 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5018 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5019 | StringRef Name; |
| 5020 | TheCondStack.push_back(TheCondState); |
| 5021 | TheCondState.TheCond = AsmCond::IfCond; |
| 5022 | |
| 5023 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5024 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5025 | } else { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5026 | if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") || |
| 5027 | parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'")) |
| 5028 | return true; |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5029 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 5030 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5031 | |
| 5032 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5033 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5034 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5035 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 5036 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5037 | } |
| 5038 | |
| 5039 | return false; |
| 5040 | } |
| 5041 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5042 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5043 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5044 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5045 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 5046 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5047 | return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an" |
| 5048 | " .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5049 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 5050 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5051 | bool LastIgnoreState = false; |
| 5052 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 5053 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5054 | if (LastIgnoreState || TheCondState.CondMet) { |
| 5055 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5056 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 5057 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5058 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5059 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5060 | return true; |
| 5061 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5062 | if (parseToken(AsmToken::EndOfStatement, |
| 5063 | "unexpected token in '.elseif' directive")) |
| 5064 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 5065 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5066 | TheCondState.CondMet = ExprValue; |
| 5067 | TheCondState.Ignore = !TheCondState.CondMet; |
| 5068 | } |
| 5069 | |
| 5070 | return false; |
| 5071 | } |
| 5072 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5073 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5074 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5075 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5076 | if (parseToken(AsmToken::EndOfStatement, |
| 5077 | "unexpected token in '.else' directive")) |
| 5078 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5079 | |
| 5080 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 5081 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5082 | return Error(DirectiveLoc, "Encountered a .else that doesn't follow " |
| 5083 | " an .if or an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5084 | TheCondState.TheCond = AsmCond::ElseCond; |
| 5085 | bool LastIgnoreState = false; |
| 5086 | if (!TheCondStack.empty()) |
| 5087 | LastIgnoreState = TheCondStack.back().Ignore; |
| 5088 | if (LastIgnoreState || TheCondState.CondMet) |
| 5089 | TheCondState.Ignore = true; |
| 5090 | else |
| 5091 | TheCondState.Ignore = false; |
| 5092 | |
| 5093 | return false; |
| 5094 | } |
| 5095 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5096 | /// parseDirectiveEnd |
| 5097 | /// ::= .end |
| 5098 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5099 | if (parseToken(AsmToken::EndOfStatement, |
| 5100 | "unexpected token in '.end' directive")) |
| 5101 | return true; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5102 | |
| 5103 | while (Lexer.isNot(AsmToken::Eof)) |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5104 | Lexer.Lex(); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5105 | |
| 5106 | return false; |
| 5107 | } |
| 5108 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5109 | /// parseDirectiveError |
| 5110 | /// ::= .err |
| 5111 | /// ::= .error [string] |
| 5112 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 5113 | if (!TheCondStack.empty()) { |
| 5114 | if (TheCondStack.back().Ignore) { |
| 5115 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5116 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5117 | } |
| 5118 | } |
| 5119 | |
| 5120 | if (!WithMessage) |
| 5121 | return Error(L, ".err encountered"); |
| 5122 | |
| 5123 | StringRef Message = ".error directive invoked in source file"; |
| 5124 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5125 | if (Lexer.isNot(AsmToken::String)) |
| 5126 | return TokError(".error argument must be a string"); |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5127 | |
| 5128 | Message = getTok().getStringContents(); |
| 5129 | Lex(); |
| 5130 | } |
| 5131 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5132 | return Error(L, Message); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5135 | /// parseDirectiveWarning |
| 5136 | /// ::= .warning [string] |
| 5137 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 5138 | if (!TheCondStack.empty()) { |
| 5139 | if (TheCondStack.back().Ignore) { |
| 5140 | eatToEndOfStatement(); |
| 5141 | return false; |
| 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | StringRef Message = ".warning directive invoked in source file"; |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5146 | |
| 5147 | if (!parseOptionalToken(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5148 | if (Lexer.isNot(AsmToken::String)) |
| 5149 | return TokError(".warning argument must be a string"); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5150 | |
| 5151 | Message = getTok().getStringContents(); |
| 5152 | Lex(); |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5153 | if (parseToken(AsmToken::EndOfStatement, |
| 5154 | "expected end of statement in '.warning' directive")) |
| 5155 | return true; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5158 | return Warning(L, Message); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5161 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5162 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5163 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5164 | if (parseToken(AsmToken::EndOfStatement, |
| 5165 | "unexpected token in '.endif' directive")) |
| 5166 | return true; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5167 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5168 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5169 | return Error(DirectiveLoc, "Encountered a .endif that doesn't follow " |
| 5170 | "an .if or .else"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 5171 | if (!TheCondStack.empty()) { |
| 5172 | TheCondState = TheCondStack.back(); |
| 5173 | TheCondStack.pop_back(); |
| 5174 | } |
| 5175 | |
| 5176 | return false; |
| 5177 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 5178 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5179 | void AsmParser::initializeDirectiveKindMap() { |
| 5180 | DirectiveKindMap[".set"] = DK_SET; |
| 5181 | DirectiveKindMap[".equ"] = DK_EQU; |
| 5182 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 5183 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 5184 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 5185 | DirectiveKindMap[".string"] = DK_STRING; |
| 5186 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 5187 | DirectiveKindMap[".short"] = DK_SHORT; |
| 5188 | DirectiveKindMap[".value"] = DK_VALUE; |
| 5189 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 5190 | DirectiveKindMap[".long"] = DK_LONG; |
| 5191 | DirectiveKindMap[".int"] = DK_INT; |
| 5192 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 5193 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 5194 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 5195 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5196 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 5197 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 5198 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 5199 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 5200 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 5201 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 5202 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 5203 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 5204 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 5205 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 5206 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 5207 | DirectiveKindMap[".org"] = DK_ORG; |
| 5208 | DirectiveKindMap[".fill"] = DK_FILL; |
| 5209 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 5210 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 5211 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 5212 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5213 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 5214 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 5215 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 5216 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 5217 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 5218 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 5219 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 5220 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 5221 | DirectiveKindMap[".comm"] = DK_COMM; |
| 5222 | DirectiveKindMap[".common"] = DK_COMMON; |
| 5223 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 5224 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 5225 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 5226 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 5227 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 5228 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 5229 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5230 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5231 | DirectiveKindMap[".irp"] = DK_IRP; |
| 5232 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 5233 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 5234 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 5235 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 5236 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 5237 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 5238 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 5239 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 5240 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 5241 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 5242 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 5243 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5244 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 5245 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 5246 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 5247 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5248 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 5249 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5250 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 5251 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 5252 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 5253 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 5254 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 5255 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5256 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 5257 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 5258 | DirectiveKindMap[".space"] = DK_SPACE; |
| 5259 | DirectiveKindMap[".file"] = DK_FILE; |
| 5260 | DirectiveKindMap[".line"] = DK_LINE; |
| 5261 | DirectiveKindMap[".loc"] = DK_LOC; |
| 5262 | DirectiveKindMap[".stabs"] = DK_STABS; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5263 | DirectiveKindMap[".cv_file"] = DK_CV_FILE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5264 | DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5265 | DirectiveKindMap[".cv_loc"] = DK_CV_LOC; |
| 5266 | DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 5267 | DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 5268 | DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 5269 | DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE; |
Reid Kleckner | 06d02d0 | 2018-09-07 21:30:52 +0000 | [diff] [blame] | 5270 | DirectiveKindMap[".cv_string"] = DK_CV_STRING; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 5271 | DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE; |
| 5272 | DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 5273 | DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET; |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 5274 | DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5275 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 5276 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 5277 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 5278 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 5279 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 5280 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 5281 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 5282 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 5283 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 5284 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 5285 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 5286 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 5287 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 5288 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 5289 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 5290 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 5291 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 5292 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
Saleem Abdulrasool | a219b3d | 2017-07-28 03:39:19 +0000 | [diff] [blame] | 5293 | DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5294 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 5295 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 5296 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 5297 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5298 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 5299 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 5300 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 5301 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 5302 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 5303 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 5304 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 5305 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 5306 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 5307 | DirectiveKindMap[".warning"] = DK_WARNING; |
Michael Zuckerman | 5670461 | 2017-05-01 13:20:12 +0000 | [diff] [blame] | 5308 | DirectiveKindMap[".altmacro"] = DK_ALTMACRO; |
| 5309 | DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 5310 | DirectiveKindMap[".reloc"] = DK_RELOC; |
Petr Hosek | 731bb9c | 2016-08-23 21:34:53 +0000 | [diff] [blame] | 5311 | DirectiveKindMap[".dc"] = DK_DC; |
| 5312 | DirectiveKindMap[".dc.a"] = DK_DC_A; |
| 5313 | DirectiveKindMap[".dc.b"] = DK_DC_B; |
| 5314 | DirectiveKindMap[".dc.d"] = DK_DC_D; |
| 5315 | DirectiveKindMap[".dc.l"] = DK_DC_L; |
| 5316 | DirectiveKindMap[".dc.s"] = DK_DC_S; |
| 5317 | DirectiveKindMap[".dc.w"] = DK_DC_W; |
| 5318 | DirectiveKindMap[".dc.x"] = DK_DC_X; |
Petr Hosek | 4cb08ce | 2016-09-23 19:25:15 +0000 | [diff] [blame] | 5319 | DirectiveKindMap[".dcb"] = DK_DCB; |
| 5320 | DirectiveKindMap[".dcb.b"] = DK_DCB_B; |
| 5321 | DirectiveKindMap[".dcb.d"] = DK_DCB_D; |
| 5322 | DirectiveKindMap[".dcb.l"] = DK_DCB_L; |
| 5323 | DirectiveKindMap[".dcb.s"] = DK_DCB_S; |
| 5324 | DirectiveKindMap[".dcb.w"] = DK_DCB_W; |
| 5325 | DirectiveKindMap[".dcb.x"] = DK_DCB_X; |
Petr Hosek | 85b2f67 | 2016-09-23 21:53:36 +0000 | [diff] [blame] | 5326 | DirectiveKindMap[".ds"] = DK_DS; |
| 5327 | DirectiveKindMap[".ds.b"] = DK_DS_B; |
| 5328 | DirectiveKindMap[".ds.d"] = DK_DS_D; |
| 5329 | DirectiveKindMap[".ds.l"] = DK_DS_L; |
| 5330 | DirectiveKindMap[".ds.p"] = DK_DS_P; |
| 5331 | DirectiveKindMap[".ds.s"] = DK_DS_S; |
| 5332 | DirectiveKindMap[".ds.w"] = DK_DS_W; |
| 5333 | DirectiveKindMap[".ds.x"] = DK_DS_X; |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5334 | DirectiveKindMap[".print"] = DK_PRINT; |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 5335 | DirectiveKindMap[".addrsig"] = DK_ADDRSIG; |
| 5336 | DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5339 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5340 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5341 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5342 | unsigned NestLevel = 0; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5343 | while (true) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5344 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5345 | if (getLexer().is(AsmToken::Eof)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5346 | printError(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5347 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5350 | if (Lexer.is(AsmToken::Identifier) && |
Nirav Dave | e24fcd5 | 2018-07-18 16:17:03 +0000 | [diff] [blame] | 5351 | (getTok().getIdentifier() == ".rep" || |
| 5352 | getTok().getIdentifier() == ".rept" || |
Nikolay Haustov | 95b4fcd | 2016-03-01 08:18:28 +0000 | [diff] [blame] | 5353 | getTok().getIdentifier() == ".irp" || |
| 5354 | getTok().getIdentifier() == ".irpc")) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5355 | ++NestLevel; |
| 5356 | } |
| 5357 | |
| 5358 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5359 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5360 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5361 | EndToken = getTok(); |
| 5362 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5363 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5364 | printError(getTok().getLoc(), |
| 5365 | "unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 5366 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5367 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5368 | break; |
| 5369 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5370 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5373 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5374 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 5378 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 5379 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 5380 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5381 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5382 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 5383 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5386 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5387 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5388 | OS << ".endr\n"; |
| 5389 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 5390 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 5391 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5392 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5393 | // Create the macro instantiation object and add to the current macro |
| 5394 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 5395 | MacroInstantiation *MI = new MacroInstantiation( |
| 5396 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5397 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5398 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5399 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 5400 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 5401 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5402 | Lex(); |
| 5403 | } |
| 5404 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 5405 | /// parseDirectiveRept |
| 5406 | /// ::= .rep | .rept count |
| 5407 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5408 | const MCExpr *CountExpr; |
| 5409 | SMLoc CountLoc = getTok().getLoc(); |
| 5410 | if (parseExpression(CountExpr)) |
| 5411 | return true; |
| 5412 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5413 | int64_t Count; |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 5414 | if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 5415 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 5416 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5417 | |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5418 | if (check(Count < 0, CountLoc, "Count is negative") || |
| 5419 | parseToken(AsmToken::EndOfStatement, |
| 5420 | "unexpected token in '" + Dir + "' directive")) |
| 5421 | return true; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5422 | |
| 5423 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5424 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5425 | if (!M) |
| 5426 | return true; |
| 5427 | |
| 5428 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5429 | // to hold the macro body with substitutions. |
| 5430 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5431 | raw_svector_ostream OS(Buf); |
| 5432 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5433 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 5434 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5435 | return true; |
| 5436 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5437 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5438 | |
| 5439 | return false; |
| 5440 | } |
| 5441 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5442 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5443 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5444 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5445 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5446 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5447 | if (check(parseIdentifier(Parameter.Name), |
| 5448 | "expected identifier in '.irp' directive") || |
| 5449 | parseToken(AsmToken::Comma, "expected comma in '.irp' directive") || |
| 5450 | parseMacroArguments(nullptr, A) || |
| 5451 | parseToken(AsmToken::EndOfStatement, "expected End of Statement")) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5452 | return true; |
| 5453 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5454 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5455 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5456 | if (!M) |
| 5457 | return true; |
| 5458 | |
| 5459 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5460 | // to hold the macro body with substitutions. |
| 5461 | SmallString<256> Buf; |
| 5462 | raw_svector_ostream OS(Buf); |
| 5463 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5464 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5465 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 5466 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 5467 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5468 | return true; |
| 5469 | } |
| 5470 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5471 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 5472 | |
| 5473 | return false; |
| 5474 | } |
| 5475 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5476 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5477 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5478 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5479 | MCAsmMacroParameter Parameter; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 5480 | MCAsmMacroArguments A; |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5481 | |
| 5482 | if (check(parseIdentifier(Parameter.Name), |
| 5483 | "expected identifier in '.irpc' directive") || |
| 5484 | parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") || |
| 5485 | parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5486 | return true; |
| 5487 | |
| 5488 | if (A.size() != 1 || A.front().size() != 1) |
| 5489 | return TokError("unexpected token in '.irpc' directive"); |
| 5490 | |
| 5491 | // Eat the end of statement. |
Nirav Dave | a645433c | 2016-07-18 15:24:03 +0000 | [diff] [blame] | 5492 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5493 | return true; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5494 | |
| 5495 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5496 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5497 | if (!M) |
| 5498 | return true; |
| 5499 | |
| 5500 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 5501 | // to hold the macro body with substitutions. |
| 5502 | SmallString<256> Buf; |
| 5503 | raw_svector_ostream OS(Buf); |
| 5504 | |
| 5505 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 5506 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 5507 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 5508 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5509 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 5510 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 5511 | // This is undocumented, but GAS seems to support it. |
| 5512 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5513 | return true; |
| 5514 | } |
| 5515 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5516 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 5517 | |
| 5518 | return false; |
| 5519 | } |
| 5520 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5521 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 5522 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 5523 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5524 | |
| 5525 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5526 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5527 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 5528 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5529 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 5530 | return false; |
| 5531 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 5532 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5533 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5534 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5535 | const MCExpr *Value; |
| 5536 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5537 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5538 | return true; |
| 5539 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5540 | if (!MCE) |
| 5541 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 5542 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 5543 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5544 | return Error(ExprLoc, "literal value out of range for directive"); |
| 5545 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5546 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5547 | return false; |
| 5548 | } |
| 5549 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5550 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5551 | const MCExpr *Value; |
| 5552 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 5553 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5554 | return true; |
| 5555 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 5556 | if (!MCE) |
| 5557 | return Error(ExprLoc, "unexpected expression in align"); |
| 5558 | uint64_t IntValue = MCE->getValue(); |
| 5559 | if (!isPowerOf2_64(IntValue)) |
| 5560 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 5561 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5562 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5563 | return false; |
| 5564 | } |
| 5565 | |
Coby Tayree | 01e5320 | 2017-10-02 14:36:31 +0000 | [diff] [blame] | 5566 | bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) { |
| 5567 | const AsmToken StrTok = getTok(); |
| 5568 | Lex(); |
| 5569 | if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"') |
| 5570 | return Error(DirectiveLoc, "expected double quoted string after .print"); |
| 5571 | if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) |
| 5572 | return true; |
| 5573 | llvm::outs() << StrTok.getStringContents() << '\n'; |
| 5574 | return false; |
| 5575 | } |
| 5576 | |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 5577 | bool AsmParser::parseDirectiveAddrsig() { |
| 5578 | getStreamer().EmitAddrsig(); |
| 5579 | return false; |
| 5580 | } |
| 5581 | |
| 5582 | bool AsmParser::parseDirectiveAddrsigSym() { |
| 5583 | StringRef Name; |
| 5584 | if (check(parseIdentifier(Name), |
| 5585 | "expected identifier in '.addrsig_sym' directive")) |
| 5586 | return true; |
| 5587 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
| 5588 | getStreamer().EmitAddrsigSym(Sym); |
| 5589 | return false; |
| 5590 | } |
| 5591 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 5592 | // We are comparing pointers, but the pointers are relative to a single string. |
| 5593 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 5594 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 5595 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 5596 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 5597 | return -1; |
| 5598 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 5599 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5600 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5601 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 5602 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 5603 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 5604 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5605 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 5606 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5607 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 5608 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5609 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 5610 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 5611 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5612 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5615 | bool AsmParser::parseMSInlineAsm( |
| 5616 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
Eugene Zelenko | 4b6ff6b | 2017-02-10 01:33:54 +0000 | [diff] [blame] | 5617 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5618 | SmallVectorImpl<std::string> &Constraints, |
| 5619 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 5620 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5621 | SmallVector<void *, 4> InputDecls; |
| 5622 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5623 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 5624 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5625 | SmallVector<std::string, 4> InputConstraints; |
| 5626 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5627 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5628 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5629 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5630 | |
| 5631 | // Prime the lexer. |
| 5632 | Lex(); |
| 5633 | |
| 5634 | // While we have input, parse each statement. |
| 5635 | unsigned InputIdx = 0; |
| 5636 | unsigned OutputIdx = 0; |
| 5637 | while (getLexer().isNot(AsmToken::Eof)) { |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5638 | // Parse curly braces marking block start/end |
| 5639 | if (parseCurlyBlockScope(AsmStrRewrites)) |
| 5640 | continue; |
| 5641 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5642 | ParseStatementInfo Info(&AsmStrRewrites); |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5643 | bool StatementErr = parseStatement(Info, &SI); |
Nirav Dave | 9fa8af2 | 2016-09-13 13:55:06 +0000 | [diff] [blame] | 5644 | |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5645 | if (StatementErr || Info.ParseError) { |
| 5646 | // Emit pending errors if any exist. |
| 5647 | printPendingErrors(); |
Nico Weber | e204c48 | 2016-09-13 18:17:00 +0000 | [diff] [blame] | 5648 | return true; |
Nirav Dave | 2364748a | 2016-09-16 18:30:20 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | // No pending error should exist here. |
| 5652 | assert(!hasPendingError() && "unexpected error from parseStatement"); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 5653 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5654 | if (Info.Opcode == ~0U) |
| 5655 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5656 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5657 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5658 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5659 | // Build the list of clobbers, outputs and inputs. |
| 5660 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5661 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5662 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5663 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5664 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5665 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5666 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5667 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 5668 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 5669 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5670 | unsigned NumDefs = Desc.getNumDefs(); |
| 5671 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5672 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 5673 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5674 | continue; |
| 5675 | } |
| 5676 | |
| 5677 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5678 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5679 | if (SymName.empty()) |
| 5680 | continue; |
| 5681 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5682 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5683 | if (!OpDecl) |
| 5684 | continue; |
| 5685 | |
| 5686 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 5687 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5688 | if (isOutput) { |
| 5689 | ++InputIdx; |
| 5690 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5691 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 5692 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5693 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5694 | } else { |
| 5695 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 5696 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 5697 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 5698 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5699 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5700 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 5701 | |
| 5702 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 5703 | ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), |
| 5704 | Desc.getNumImplicitDefs()); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5705 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5709 | NumOutputs = OutputDecls.size(); |
| 5710 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5711 | |
| 5712 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5713 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 5714 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 5715 | ClobberRegs.end()); |
| 5716 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 5717 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 5718 | raw_string_ostream OS(Clobbers[I]); |
| 5719 | IP->printRegName(OS, ClobberRegs[I]); |
| 5720 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5721 | |
| 5722 | // Merge the various outputs and inputs. Output are expected first. |
| 5723 | if (NumOutputs || NumInputs) { |
| 5724 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 5725 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5726 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5727 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5728 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5729 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5730 | } |
| 5731 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 5732 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 5733 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 5738 | std::string AsmStringIR; |
| 5739 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 5740 | StringRef ASMString = |
| 5741 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 5742 | const char *AsmStart = ASMString.begin(); |
| 5743 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5744 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5745 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 5746 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5747 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5748 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5749 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5750 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 5751 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5752 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5753 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5754 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5755 | // Skip the original expression. |
| 5756 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5757 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 5758 | continue; |
| 5759 | } |
| 5760 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 5761 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5762 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5763 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5764 | default: |
| 5765 | break; |
Coby Tayree | d891289 | 2017-08-24 08:46:25 +0000 | [diff] [blame] | 5766 | case AOK_IntelExpr: |
| 5767 | assert(AR.IntelExp.isValid() && "cannot write invalid intel expression"); |
| 5768 | if (AR.IntelExp.NeedBracs) |
| 5769 | OS << "["; |
| 5770 | if (AR.IntelExp.hasBaseReg()) |
| 5771 | OS << AR.IntelExp.BaseReg; |
| 5772 | if (AR.IntelExp.hasIndexReg()) |
| 5773 | OS << (AR.IntelExp.hasBaseReg() ? " + " : "") |
| 5774 | << AR.IntelExp.IndexReg; |
| 5775 | if (AR.IntelExp.Scale > 1) |
| 5776 | OS << " * $$" << AR.IntelExp.Scale; |
| 5777 | if (AR.IntelExp.Imm || !AR.IntelExp.hasRegs()) |
| 5778 | OS << (AR.IntelExp.hasRegs() ? " + $$" : "$$") << AR.IntelExp.Imm; |
| 5779 | if (AR.IntelExp.NeedBracs) |
| 5780 | OS << "]"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5781 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5782 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 5783 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 5784 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5785 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5786 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5787 | break; |
| 5788 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5789 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5790 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5791 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5792 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5793 | default: break; |
| 5794 | case 8: OS << "byte ptr "; break; |
| 5795 | case 16: OS << "word ptr "; break; |
| 5796 | case 32: OS << "dword ptr "; break; |
| 5797 | case 64: OS << "qword ptr "; break; |
| 5798 | case 80: OS << "xword ptr "; break; |
| 5799 | case 128: OS << "xmmword ptr "; break; |
| 5800 | case 256: OS << "ymmword ptr "; break; |
| 5801 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 5802 | break; |
| 5803 | case AOK_Emit: |
| 5804 | OS << ".byte"; |
| 5805 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5806 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5807 | // MS alignment directives are measured in bytes. If the native assembler |
| 5808 | // measures alignment in bytes, we can pass it straight through. |
| 5809 | OS << ".align"; |
| 5810 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 5811 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5812 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 5813 | // Alignment is in log2 form, so print that instead and skip the original |
| 5814 | // immediate. |
| 5815 | unsigned Val = AR.Val; |
| 5816 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 5817 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 5818 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 5819 | break; |
| 5820 | } |
Michael Zuckerman | 02ecd43 | 2015-12-13 17:07:23 +0000 | [diff] [blame] | 5821 | case AOK_EVEN: |
| 5822 | OS << ".even"; |
| 5823 | break; |
Marina Yatsina | 5f5de9f | 2016-03-07 18:11:16 +0000 | [diff] [blame] | 5824 | case AOK_EndOfStatement: |
| 5825 | OS << "\n\t"; |
| 5826 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5827 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 5828 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5829 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 5830 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 5834 | if (AsmStart != AsmEnd) |
| 5835 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 5836 | |
| 5837 | AsmString = OS.str(); |
| 5838 | return false; |
| 5839 | } |
| 5840 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5841 | namespace llvm { |
| 5842 | namespace MCParserUtils { |
| 5843 | |
| 5844 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 5845 | /// or subexpressions. |
| 5846 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 5847 | switch (Value->getKind()) { |
| 5848 | case MCExpr::Binary: { |
| 5849 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 5850 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 5851 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 5852 | } |
| 5853 | case MCExpr::Target: |
| 5854 | case MCExpr::Constant: |
| 5855 | return false; |
| 5856 | case MCExpr::SymbolRef: { |
| 5857 | const MCSymbol &S = |
| 5858 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 5859 | if (S.isVariable()) |
| 5860 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 5861 | return &S == Sym; |
| 5862 | } |
| 5863 | case MCExpr::Unary: |
| 5864 | return isSymbolUsedInExpression( |
| 5865 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 5866 | } |
| 5867 | |
| 5868 | llvm_unreachable("Unknown expr kind!"); |
| 5869 | } |
| 5870 | |
| 5871 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 5872 | MCAsmParser &Parser, MCSymbol *&Sym, |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 5873 | const MCExpr *&Value) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5874 | |
| 5875 | // FIXME: Use better location, we should use proper tokens. |
Nirav Dave | fd91041 | 2016-06-17 16:06:17 +0000 | [diff] [blame] | 5876 | SMLoc EqualLoc = Parser.getTok().getLoc(); |
Nirav Dave | 7fd992a | 2018-08-16 16:31:14 +0000 | [diff] [blame] | 5877 | if (Parser.parseExpression(Value)) |
| 5878 | return Parser.TokError("missing expression"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5879 | |
| 5880 | // Note: we don't count b as used in "a = b". This is to allow |
| 5881 | // a = b |
| 5882 | // b = c |
| 5883 | |
Nirav Dave | 1a9044b | 2016-10-24 14:35:29 +0000 | [diff] [blame] | 5884 | if (Parser.parseToken(AsmToken::EndOfStatement)) |
| 5885 | return true; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5886 | |
| 5887 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 5888 | // used as a symbol, or it is an absolute symbol). |
| 5889 | Sym = Parser.getContext().lookupSymbol(Name); |
| 5890 | if (Sym) { |
| 5891 | // Diagnose assignment to a label. |
| 5892 | // |
| 5893 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 5894 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 5895 | if (isSymbolUsedInExpression(Sym, Value)) |
| 5896 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 5897 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 5898 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5899 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 5900 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 5901 | ; // Allow redefinitions of variables that haven't yet been used. |
| 5902 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 5903 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 5904 | else if (!Sym->isVariable()) |
| 5905 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 5906 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 5907 | return Parser.Error(EqualLoc, |
| 5908 | "invalid reassignment of non-absolute variable '" + |
| 5909 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5910 | } else if (Name == ".") { |
Oliver Stannard | 268f42f | 2016-12-14 10:43:58 +0000 | [diff] [blame] | 5911 | Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5912 | return false; |
| 5913 | } else |
| 5914 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 5915 | |
| 5916 | Sym->setRedefinable(allow_redef); |
| 5917 | |
| 5918 | return false; |
| 5919 | } |
| 5920 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 5921 | } // end namespace MCParserUtils |
| 5922 | } // end namespace llvm |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 5923 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 5924 | /// Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 5925 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 5926 | MCStreamer &Out, const MCAsmInfo &MAI, |
| 5927 | unsigned CB) { |
| 5928 | return new AsmParser(SM, C, Out, MAI, CB); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 5929 | } |