blob: d85f1c613c719fc18aa94697675de78c6d024945 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- 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 Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
Pete Cooper80d21cb2015-06-22 19:35:57 +000029#include "llvm/MC/MCParser/MCAsmParserUtils.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000030#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000031#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Evan Cheng76792992011-07-20 05:58:47 +000032#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000033#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000034#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000035#include "llvm/MC/MCSymbol.h"
Daniel Sanders9f6ad492015-11-12 13:33:00 +000036#include "llvm/MC/MCValue.h"
Davide Italiano7c9fc732016-07-27 05:51:56 +000037#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000038#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000039#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000040#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000041#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000042#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000043#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000044#include <deque>
Davide Italiano7c9fc732016-07-27 05:51:56 +000045#include <sstream>
Chad Rosier8bce6642012-10-18 15:49:34 +000046#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000047#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000048using namespace llvm;
49
Eric Christophera7c32732012-12-18 00:30:54 +000050MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000051
Davide Italiano7c9fc732016-07-27 05:51:56 +000052static cl::opt<unsigned> AsmMacroMaxNestingDepth(
53 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
54 cl::desc("The maximum nesting depth allowed for assembly macros."));
55
Daniel Dunbar86033402010-07-12 17:54:38 +000056namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000057/// \brief Helper types for tracking macro definitions.
58typedef std::vector<AsmToken> MCAsmMacroArgument;
59typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000060
61struct MCAsmMacroParameter {
62 StringRef Name;
63 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000066
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000067 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000068};
69
Eli Benderskya313ae62013-01-16 18:56:50 +000070typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
71
72struct MCAsmMacro {
73 StringRef Name;
74 StringRef Body;
75 MCAsmMacroParameters Parameters;
76
77public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000078 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
79 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000080};
81
Daniel Dunbar43235712010-07-18 18:54:11 +000082/// \brief Helper class for storing information about an active macro
83/// instantiation.
84struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location of the instantiation.
86 SMLoc InstantiationLoc;
87
Daniel Dunbar40f1d852012-12-01 01:38:48 +000088 /// The buffer where parsing should resume upon instantiation completion.
89 int ExitBuffer;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091 /// The location where parsing should resume upon instantiation completion.
92 SMLoc ExitLoc;
93
Nico Weber155dccd12014-07-24 17:08:39 +000094 /// The depth of TheCondStack at the start of the instantiation.
95 size_t CondStackDepth;
96
Daniel Dunbar43235712010-07-18 18:54:11 +000097public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000098 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000099};
100
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000103 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000104
Jim Grosbach4b905842013-09-20 23:08:21 +0000105 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 unsigned Opcode;
107
Jim Grosbach4b905842013-09-20 23:08:21 +0000108 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 bool ParseError;
110
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111 SmallVectorImpl<AsmRewrite> *AsmRewrites;
112
Craig Topper353eda42014-04-24 06:44:33 +0000113 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000114 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000115 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116};
117
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000118/// \brief The concrete assembly parser instance.
119class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000120 AsmParser(const AsmParser &) = delete;
121 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000122private:
123 AsmLexer Lexer;
124 MCContext &Ctx;
125 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000126 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000128 SourceMgr::DiagHandlerTy SavedDiagHandler;
129 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000130 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000131
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 /// This is the current buffer index we're lexing from as managed by the
133 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000134 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135
136 AsmCond TheCondState;
137 std::vector<AsmCond> TheCondStack;
138
Jim Grosbach4b905842013-09-20 23:08:21 +0000139 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000140 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000141 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000142 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000145 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000148 std::vector<MacroInstantiation*> ActiveMacros;
149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000151 std::deque<MCAsmMacro> MacroLikeBodies;
152
Daniel Dunbar828984f2010-07-18 18:38:02 +0000153 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000154 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155
Toma Tabacu217116e2015-04-27 10:50:29 +0000156 /// \brief Keeps track of how many .macro's have been instantiated.
157 unsigned NumOfMacroInstantiations;
158
Daniel Dunbar43325c42010-09-09 22:42:56 +0000159 /// Flag tracking whether any errors have been encountered.
160 unsigned HadError : 1;
161
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000162 /// The values from the last parsed cpp hash file line comment if any.
Tim Northoverc0bef992016-04-13 19:46:54 +0000163 struct CppHashInfoTy {
164 StringRef Filename;
Andrew Kaylorca196472016-04-21 20:09:35 +0000165 int64_t LineNumber = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000166 SMLoc Loc;
Andrew Kaylorca196472016-04-21 20:09:35 +0000167 unsigned Buf = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000168 };
169 CppHashInfoTy CppHashInfo;
170
171 /// \brief List of forward directional labels for diagnosis at the end.
172 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
173
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000174 /// When generating dwarf for assembly source files we need to calculate the
175 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000176 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000177 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
178 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000179 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000180 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000181
Devang Patela173ee52012-01-31 18:14:05 +0000182 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
183 unsigned AssemblerDialect;
184
Jim Grosbach4b905842013-09-20 23:08:21 +0000185 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000186 bool IsDarwin;
187
Jim Grosbach4b905842013-09-20 23:08:21 +0000188 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000189 bool ParsingInlineAsm;
190
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000192 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000194 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000195
Craig Topper59be68f2014-03-08 07:14:16 +0000196 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000197
Craig Topper59be68f2014-03-08 07:14:16 +0000198 void addDirectiveHandler(StringRef Directive,
199 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000200 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000201 }
202
Toma Tabacu11e14a92015-04-21 11:50:52 +0000203 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
204 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
205 }
206
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000207public:
208 /// @name MCAsmParser Interface
209 /// {
210
Craig Topper59be68f2014-03-08 07:14:16 +0000211 SourceMgr &getSourceManager() override { return SrcMgr; }
212 MCAsmLexer &getLexer() override { return Lexer; }
213 MCContext &getContext() override { return Ctx; }
214 MCStreamer &getStreamer() override { return Out; }
215 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000216 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000217 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000218 else
219 return AssemblerDialect;
220 }
Craig Topper59be68f2014-03-08 07:14:16 +0000221 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000222 AssemblerDialect = i;
223 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000224
Craig Topper59be68f2014-03-08 07:14:16 +0000225 void Note(SMLoc L, const Twine &Msg,
226 ArrayRef<SMRange> Ranges = None) override;
227 bool Warning(SMLoc L, const Twine &Msg,
228 ArrayRef<SMRange> Ranges = None) override;
229 bool Error(SMLoc L, const Twine &Msg,
230 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000231
Craig Topper59be68f2014-03-08 07:14:16 +0000232 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000233
Craig Topper59be68f2014-03-08 07:14:16 +0000234 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
235 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000236
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000237 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000238 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000239 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000240 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000241 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000242 const MCInstrInfo *MII, const MCInstPrinter *IP,
243 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000244
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000245 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000246 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
247 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
248 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000249 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
250 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000251 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000254 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000255 bool parseIdentifier(StringRef &Res) override;
256 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000257
Craig Topper59be68f2014-03-08 07:14:16 +0000258 void checkForValidSection() override;
Nirav Davea645433c2016-07-18 15:24:03 +0000259
260 bool getTokenLoc(SMLoc &Loc) {
261 Loc = getTok().getLoc();
262 return false;
263 }
264
265 /// parseToken - If current token has the specified kind, eat it and
266 /// return success. Otherwise, emit the specified error and return failure.
267 bool parseToken(AsmToken::TokenKind T, const Twine &ErrMsg) {
268 if (getTok().getKind() != T)
269 return TokError(ErrMsg);
270 Lex();
271 return false;
272 }
273
274 bool parseIntToken(int64_t &V, const Twine &ErrMsg) {
275 if (getTok().getKind() != AsmToken::Integer)
276 return TokError(ErrMsg);
277 V = getTok().getIntVal();
278 Lex();
279 return false;
280 }
281
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000282 /// }
283
284private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000285
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000286 bool parseStatement(ParseStatementInfo &Info,
287 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000288 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Craig Topper3c76c522015-09-20 23:35:59 +0000289 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000290
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000292 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000293 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000294 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000295 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000296 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000297
Eli Benderskya313ae62013-01-16 18:56:50 +0000298 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000300
301 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000303
304 /// \brief Lookup a previously defined macro.
305 /// \param Name Macro name.
306 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000308
309 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000310 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000311
312 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000313 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000314
315 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000316 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000317
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000318 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000319 ///
320 /// \param M The macro.
321 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000323
324 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000325 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000326
David Majnemer91fc4c22014-01-29 18:57:46 +0000327 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000328 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000329
330 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 void printMacroInstantiations();
334 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000335 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000336 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000337 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000338 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000339
Nirav Davea645433c2016-07-18 15:24:03 +0000340 bool check(bool P, SMLoc Loc, const Twine &Msg) {
341 if (P)
342 return Error(Loc, Msg);
343 return false;
344 }
345
346 bool check(bool P, const Twine &Msg) {
347 if (P)
348 return TokError(Msg);
349 return false;
350 }
351
Jim Grosbach4b905842013-09-20 23:08:21 +0000352 /// \brief Enter the specified file. This returns true on failure.
353 bool enterIncludeFile(const std::string &Filename);
354
355 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000356 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000357 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000358
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000359 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000360 /// current token is not set; clients should ensure Lex() is called
361 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000362 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000363 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000364 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000365 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000366
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000367 /// \brief Parse up to the end of statement and a return the contents from the
368 /// current token until the end of the statement; the current token on exit
369 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000370 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000371
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000372 /// \brief Parse until the end of a statement or a comma is encountered,
373 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000375
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000377 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000379 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
380 MCBinaryExpr::Opcode &Kind);
381
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
383 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
384 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000385
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000387
Eli Bendersky17233942013-01-15 22:59:42 +0000388 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000389 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000390 DK_NO_DIRECTIVE, // Placeholder
391 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000392 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000393 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
394 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000395 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000396 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000397 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000398 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000399 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000400 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
401 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000402 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000403 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
404 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000405 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000406 DK_CV_FILE, DK_CV_LOC, DK_CV_LINETABLE, DK_CV_INLINE_LINETABLE,
David Majnemer408b5e62016-02-05 01:55:49 +0000407 DK_CV_DEF_RANGE, DK_CV_STRINGTABLE, DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000408 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
409 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
410 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
411 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
412 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000413 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000414 DK_MACROS_ON, DK_MACROS_OFF,
415 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000416 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000417 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000418 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000419 };
420
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000422 /// directives parsed by this class.
423 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000424
425 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000427 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000429 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
431 bool parseDirectiveFill(); // ".fill"
432 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000433 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
435 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Eli Bendersky17233942013-01-15 22:59:42 +0000439 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveFile(SMLoc DirectiveLoc);
441 bool parseDirectiveLine();
442 bool parseDirectiveLoc();
443 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000444
David Majnemer408b5e62016-02-05 01:55:49 +0000445 // ".cv_file", ".cv_loc", ".cv_linetable", "cv_inline_linetable",
446 // ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000447 bool parseDirectiveCVFile();
448 bool parseDirectiveCVLoc();
449 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000450 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000451 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000452 bool parseDirectiveCVStringTable();
453 bool parseDirectiveCVFileChecksums();
454
Eli Bendersky17233942013-01-15 22:59:42 +0000455 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000457 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveCFISections();
459 bool parseDirectiveCFIStartProc();
460 bool parseDirectiveCFIEndProc();
461 bool parseDirectiveCFIDefCfaOffset();
462 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
463 bool parseDirectiveCFIAdjustCfaOffset();
464 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
465 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
466 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
467 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
468 bool parseDirectiveCFIRememberState();
469 bool parseDirectiveCFIRestoreState();
470 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
471 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
472 bool parseDirectiveCFIEscape();
473 bool parseDirectiveCFISignalFrame();
474 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000475
476 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000477 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000478 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000479 bool parseDirectiveEndMacro(StringRef Directive);
480 bool parseDirectiveMacro(SMLoc DirectiveLoc);
481 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000482
Eli Benderskyf483ff92012-12-20 19:05:53 +0000483 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000484 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000485 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000486 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000487 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000488 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000489
Eli Bendersky17233942013-01-15 22:59:42 +0000490 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000491 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000492
493 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000494 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000495
Jim Grosbach4b905842013-09-20 23:08:21 +0000496 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000497 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000498 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000499
Jim Grosbach4b905842013-09-20 23:08:21 +0000500 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000501
Jim Grosbach4b905842013-09-20 23:08:21 +0000502 bool parseDirectiveAbort(); // ".abort"
503 bool parseDirectiveInclude(); // ".include"
504 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000505
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000506 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
507 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000508 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000509 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000510 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000511 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000512 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
513 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000514 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000515 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
516 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
517 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
518 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000519 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000520
Jim Grosbach4b905842013-09-20 23:08:21 +0000521 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000522 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000523
Rafael Espindola34b9c512012-06-03 23:57:14 +0000524 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000525 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
526 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000527 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000528 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
530 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
531 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000532
Chad Rosierc7f552c2013-02-12 21:33:51 +0000533 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000534 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000535 size_t Len);
536
537 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000538 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000539
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000540 // "end"
541 bool parseDirectiveEnd(SMLoc DirectiveLoc);
542
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000543 // ".err" or ".error"
544 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000545
Nico Weber404012b2014-07-24 16:26:06 +0000546 // ".warning"
547 bool parseDirectiveWarning(SMLoc DirectiveLoc);
548
Eli Bendersky17233942013-01-15 22:59:42 +0000549 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000550};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000551}
Daniel Dunbar86033402010-07-12 17:54:38 +0000552
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000553namespace llvm {
554
555extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000556extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000557extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000558
559}
560
Chris Lattnerc35681b2010-01-19 19:46:13 +0000561enum { DEFAULT_ADDRSPACE = 0 };
562
David Blaikie9f380a32015-03-16 18:06:57 +0000563AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
564 const MCAsmInfo &MAI)
565 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
566 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Tim Northoverc0bef992016-04-13 19:46:54 +0000567 MacrosEnabledFlag(true), HadError(false), CppHashInfo(),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000568 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000569 // Save the old handler.
570 SavedDiagHandler = SrcMgr.getDiagHandler();
571 SavedDiagContext = SrcMgr.getDiagContext();
572 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000573 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000574 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000575
Daniel Dunbarc5011082010-07-12 18:12:02 +0000576 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000577 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
578 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000579 PlatformParser.reset(createCOFFAsmParser());
580 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000581 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000582 PlatformParser.reset(createDarwinAsmParser());
583 IsDarwin = true;
584 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000585 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000586 PlatformParser.reset(createELFAsmParser());
587 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000588 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000589
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000590 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000591 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000592
593 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000594}
595
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000596AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000597 assert((HadError || ActiveMacros.empty()) &&
598 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000599}
600
Jim Grosbach4b905842013-09-20 23:08:21 +0000601void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000602 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000603 for (std::vector<MacroInstantiation *>::const_reverse_iterator
604 it = ActiveMacros.rbegin(),
605 ie = ActiveMacros.rend();
606 it != ie; ++it)
607 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000608 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000609}
610
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000611void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
612 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
613 printMacroInstantiations();
614}
615
Chris Lattnera3a06812011-10-16 04:47:35 +0000616bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000617 if(getTargetParser().getTargetOptions().MCNoWarn)
618 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000619 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000620 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000621 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
622 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000623 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000624}
625
Chris Lattnera3a06812011-10-16 04:47:35 +0000626bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000627 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000628 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
629 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000630 return true;
631}
632
Jim Grosbach4b905842013-09-20 23:08:21 +0000633bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000634 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000635 unsigned NewBuf =
636 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
637 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000638 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000639
Sean Callanan7a77eae2010-01-21 00:19:58 +0000640 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000641 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000642 return false;
643}
Daniel Dunbar43235712010-07-18 18:54:11 +0000644
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000645/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000646/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000647/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000648bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000649 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000650 unsigned NewBuf =
651 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
652 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000653 return true;
654
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000655 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000656 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000657 return false;
658}
659
Alp Tokera55b95b2014-07-06 10:33:31 +0000660void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
661 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000662 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
663 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000664}
665
Sean Callanan7a77eae2010-01-21 00:19:58 +0000666const AsmToken &AsmParser::Lex() {
Nirav Dave1180e6892016-06-02 17:15:05 +0000667 if (Lexer.getTok().is(AsmToken::Error))
668 Error(Lexer.getErrLoc(), Lexer.getErr());
669
Nirav Dave53a72f42016-07-11 12:42:14 +0000670 // if it's a end of statement with a comment in it
671 if (getTok().is(AsmToken::EndOfStatement)) {
672 // if this is a line comment output it.
673 if (getTok().getString().front() != '\n' &&
674 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
675 Out.addExplicitComment(Twine(getTok().getString()));
676 }
677
Sean Callanan7a77eae2010-01-21 00:19:58 +0000678 const AsmToken *tok = &Lexer.Lex();
Nirav Dave53a72f42016-07-11 12:42:14 +0000679
680 // Parse comments here to be deferred until end of next statement.
Nirav Davefd910412016-06-17 16:06:17 +0000681 while (tok->is(AsmToken::Comment)) {
Nirav Dave53a72f42016-07-11 12:42:14 +0000682 if (MAI.preserveAsmComments())
683 Out.addExplicitComment(Twine(tok->getString()));
Nirav Davefd910412016-06-17 16:06:17 +0000684 tok = &Lexer.Lex();
685 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000686
Sean Callanan7a77eae2010-01-21 00:19:58 +0000687 if (tok->is(AsmToken::Eof)) {
688 // If this is the end of an included file, pop the parent file off the
689 // include stack.
690 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
691 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000692 jumpToLoc(ParentIncludeLoc);
Nirav Davefd910412016-06-17 16:06:17 +0000693 return Lex();
Sean Callanan7a77eae2010-01-21 00:19:58 +0000694 }
695 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000696
Michael J. Spencer530ce852010-10-09 11:00:50 +0000697
Sean Callanan7a77eae2010-01-21 00:19:58 +0000698 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000699}
700
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000701bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000702 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000703 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000704 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000705
Chris Lattner36e02122009-06-21 20:54:55 +0000706 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000707 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000708
709 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000710 AsmCond StartingCondState = TheCondState;
711
Kevin Enderby6469fc22011-11-01 22:27:22 +0000712 // If we are generating dwarf for assembly source files save the initial text
713 // section and generate a .file directive.
714 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000715 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000716 if (!Sec->getBeginSymbol()) {
717 MCSymbol *SectionStartSym = getContext().createTempSymbol();
718 getStreamer().EmitLabel(SectionStartSym);
719 Sec->setBeginSymbol(SectionStartSym);
720 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000721 bool InsertResult = getContext().addGenDwarfSection(Sec);
722 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000723 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000724 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
725 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000726 }
727
Chris Lattner73f36112009-07-02 21:53:43 +0000728 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000729 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000730 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000731 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000732 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000733
Nirav Dave1180e6892016-06-02 17:15:05 +0000734 // If we've failed, but on a Error Token, but did not consume it in
735 // favor of a better message, emit it now.
736 if (Lexer.getTok().is(AsmToken::Error)) {
737 Lex();
738 }
739
Daniel Dunbar43325c42010-09-09 22:42:56 +0000740 // We had an error, validate that one was emitted and recover by skipping to
741 // the next line.
742 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000743 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000744 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000745
Oliver Stannard21718282016-07-26 14:19:47 +0000746 getTargetParser().flushPendingInstructions(getStreamer());
747
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000748 if (TheCondState.TheCond != StartingCondState.TheCond ||
749 TheCondState.Ignore != StartingCondState.Ignore)
750 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000751
752 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000753 const auto &LineTables = getContext().getMCDwarfLineTables();
754 if (!LineTables.empty()) {
755 unsigned Index = 0;
756 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
757 if (File.Name.empty() && Index != 0)
758 TokError("unassigned file number: " + Twine(Index) +
759 " for .file directives");
760 ++Index;
761 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000762 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000763
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000764 // Check to see that all assembler local symbols were actually defined.
765 // Targets that don't do subsections via symbols may not want this, though,
766 // so conservatively exclude them. Only do this if we're finalizing, though,
767 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000768 if (!NoFinalize) {
769 if (MAI.hasSubsectionsViaSymbols()) {
770 for (const auto &TableEntry : getContext().getSymbols()) {
771 MCSymbol *Sym = TableEntry.getValue();
772 // Variable symbols may not be marked as defined, so check those
773 // explicitly. If we know it's a variable, we have a definition for
774 // the purposes of this check.
775 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
776 // FIXME: We would really like to refer back to where the symbol was
777 // first referenced for a source location. We need to add something
778 // to track that. Currently, we just point to the end of the file.
779 HadError |=
Nirav Davefd910412016-06-17 16:06:17 +0000780 Error(getTok().getLoc(), "assembler local symbol '" +
781 Sym->getName() + "' not defined");
Tim Northover6b3169b2016-04-11 19:50:46 +0000782 }
783 }
784
785 // Temporary symbols like the ones for directional jumps don't go in the
786 // symbol table. They also need to be diagnosed in all (final) cases.
Tim Northoverc0bef992016-04-13 19:46:54 +0000787 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
788 if (std::get<2>(LocSym)->isUndefined()) {
789 // Reset the state of any "# line file" directives we've seen to the
790 // context as it was at the diagnostic site.
791 CppHashInfo = std::get<1>(LocSym);
792 HadError |= Error(std::get<0>(LocSym), "directional label undefined");
793 }
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000794 }
795 }
796
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000797 // Finalize the output stream if there are no errors and if the client wants
798 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000799 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000800 Out.Finish();
801
Oliver Stannard07b43d32015-11-17 09:58:07 +0000802 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000803}
804
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000805void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000806 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000807 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000808 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000809 }
810}
811
Jim Grosbach4b905842013-09-20 23:08:21 +0000812/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000813void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000814 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Dave1180e6892016-06-02 17:15:05 +0000815 Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000816
Chris Lattnere5074c42009-06-22 01:29:09 +0000817 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000818 if (Lexer.is(AsmToken::EndOfStatement))
Nirav Dave1180e6892016-06-02 17:15:05 +0000819 Lexer.Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000820}
821
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000822StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000823 const char *Start = getTok().getLoc().getPointer();
824
Jim Grosbach4b905842013-09-20 23:08:21 +0000825 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000826 Lexer.Lex();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000827
828 const char *End = getTok().getLoc().getPointer();
829 return StringRef(Start, End - Start);
830}
Chris Lattner78db3622009-06-22 05:51:26 +0000831
Jim Grosbach4b905842013-09-20 23:08:21 +0000832StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000833 const char *Start = getTok().getLoc().getPointer();
834
835 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000836 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000837 Lexer.Lex();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000838
839 const char *End = getTok().getLoc().getPointer();
840 return StringRef(Start, End - Start);
841}
842
Jim Grosbach4b905842013-09-20 23:08:21 +0000843/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000844/// NOTE: This assumes the leading '(' has already been consumed.
845///
846/// parenexpr ::= expr)
847///
Jim Grosbach4b905842013-09-20 23:08:21 +0000848bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
849 if (parseExpression(Res))
850 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000851 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000852 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000853 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000854 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000855 return false;
856}
Chris Lattner78db3622009-06-22 05:51:26 +0000857
Jim Grosbach4b905842013-09-20 23:08:21 +0000858/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000859/// NOTE: This assumes the leading '[' has already been consumed.
860///
861/// bracketexpr ::= expr]
862///
Jim Grosbach4b905842013-09-20 23:08:21 +0000863bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
864 if (parseExpression(Res))
865 return true;
Nirav Davea645433c2016-07-18 15:24:03 +0000866 EndLoc = getTok().getEndLoc();
867 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
868 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000869 return false;
870}
871
Jim Grosbach4b905842013-09-20 23:08:21 +0000872/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000873/// primaryexpr ::= (parenexpr
874/// primaryexpr ::= symbol
875/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000876/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000877/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000878bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000879 SMLoc FirstTokenLoc = getLexer().getLoc();
880 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
881 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000882 default:
883 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000884 // If we have an error assume that we've already handled it.
885 case AsmToken::Error:
886 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000887 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000888 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000889 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000890 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000891 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000892 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000893 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000894 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000895 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000896 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000897 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000898 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000899 if (FirstTokenKind == AsmToken::Dollar) {
900 if (Lexer.getMAI().getDollarIsPC()) {
901 // This is a '$' reference, which references the current PC. Emit a
902 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000903 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000904 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000905 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000906 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000907 EndLoc = FirstTokenLoc;
908 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000909 }
910 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000911 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000912 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000913 // Parse symbol variant
914 std::pair<StringRef, StringRef> Split;
915 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000916 if (FirstTokenKind == AsmToken::String) {
917 if (Lexer.is(AsmToken::At)) {
Nirav Davefd910412016-06-17 16:06:17 +0000918 Lex(); // eat @
David Majnemer6a5b8122014-06-19 01:25:43 +0000919 SMLoc AtLoc = getLexer().getLoc();
920 StringRef VName;
921 if (parseIdentifier(VName))
922 return Error(AtLoc, "expected symbol variant after '@'");
923
924 Split = std::make_pair(Identifier, VName);
925 }
926 } else {
927 Split = Identifier.split('@');
928 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000929 } else if (Lexer.is(AsmToken::LParen)) {
Nirav Davefd910412016-06-17 16:06:17 +0000930 Lex(); // eat '('.
David Peixotto8ad70b32013-12-04 22:43:20 +0000931 StringRef VName;
932 parseIdentifier(VName);
Nirav Davea645433c2016-07-18 15:24:03 +0000933 // eat ')'.
934 if (parseToken(AsmToken::RParen,
935 "unexpected token in variant, expected ')'"))
936 return true;
David Peixotto8ad70b32013-12-04 22:43:20 +0000937 Split = std::make_pair(Identifier, VName);
938 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000939
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000940 EndLoc = SMLoc::getFromPointer(Identifier.end());
941
Daniel Dunbard20cda02009-10-16 01:34:54 +0000942 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000943 StringRef SymbolName = Identifier;
944 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000945
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000946 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000947 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000948 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000949 if (Variant != MCSymbolRefExpr::VK_Invalid) {
950 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000951 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000952 Variant = MCSymbolRefExpr::VK_None;
953 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000954 return Error(SMLoc::getFromPointer(Split.second.begin()),
955 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000956 }
957 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000958
Jim Grosbach6f482002015-05-18 18:43:14 +0000959 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000960
Daniel Dunbard20cda02009-10-16 01:34:54 +0000961 // If this is an absolute variable reference, substitute it now to preserve
962 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000963 if (Sym->isVariable() &&
964 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000965 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000966 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000967
Vedant Kumar86dbd922015-08-31 17:44:53 +0000968 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000969 return false;
970 }
971
972 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000973 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000974 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000975 }
David Woodhousef42a6662014-02-01 16:20:54 +0000976 case AsmToken::BigNum:
977 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000978 case AsmToken::Integer: {
979 SMLoc Loc = getTok().getLoc();
980 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000981 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000982 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000983 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000984 // Look for 'b' or 'f' following an Integer as a directional label
985 if (Lexer.getKind() == AsmToken::Identifier) {
986 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000987 // Lookup the symbol variant if used.
988 std::pair<StringRef, StringRef> Split = IDVal.split('@');
989 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
990 if (Split.first.size() != IDVal.size()) {
991 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000992 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000993 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000994 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000995 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000996 if (IDVal == "f" || IDVal == "b") {
997 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000998 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000999 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00001000 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +00001001 return Error(Loc, "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +00001002 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001003 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +00001004 Lex(); // Eat identifier.
1005 }
1006 }
Chris Lattner78db3622009-06-22 05:51:26 +00001007 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +00001008 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001009 case AsmToken::Real: {
1010 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +00001011 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001012 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001013 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001014 Lex(); // Eat token.
1015 return false;
1016 }
Chris Lattner6b55cb92010-04-14 04:40:28 +00001017 case AsmToken::Dot: {
1018 // This is a '.' reference, which references the current PC. Emit a
1019 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +00001020 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001021 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001022 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001023 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001024 Lex(); // Eat identifier.
1025 return false;
1026 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001027 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001028 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +00001029 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +00001030 case AsmToken::LBrac:
1031 if (!PlatformParser->HasBracketExpressions())
1032 return TokError("brackets expression not supported on this target");
1033 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001035 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001036 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001038 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001039 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001040 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001041 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001042 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001044 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001045 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001046 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001047 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001048 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001049 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001050 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001051 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001052 return false;
Chris Lattner78db3622009-06-22 05:51:26 +00001053 }
1054}
Chris Lattner7fdbce72009-06-22 06:32:03 +00001055
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001056bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001057 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +00001059}
1060
Daniel Dunbar55f16672010-09-17 02:47:07 +00001061const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +00001062AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +00001063 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +00001064 // Ask the target implementation about this expression first.
1065 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1066 if (NewE)
1067 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001068 // Recurse over the given expression, rebuilding it to apply the given variant
1069 // if there is exactly one symbol.
1070 switch (E->getKind()) {
1071 case MCExpr::Target:
1072 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001073 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001074
1075 case MCExpr::SymbolRef: {
1076 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1077
1078 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001079 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1080 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001081 return E;
1082 }
1083
Jim Grosbach13760bd2015-05-30 01:25:56 +00001084 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001085 }
1086
1087 case MCExpr::Unary: {
1088 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001089 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001090 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001091 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001092 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001093 }
1094
1095 case MCExpr::Binary: {
1096 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1098 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001099
1100 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001101 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001102
Jim Grosbach4b905842013-09-20 23:08:21 +00001103 if (!LHS)
1104 LHS = BE->getLHS();
1105 if (!RHS)
1106 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001107
Jim Grosbach13760bd2015-05-30 01:25:56 +00001108 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001109 }
1110 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001111
Craig Toppera2886c22012-02-07 05:05:23 +00001112 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001113}
1114
Jim Grosbach4b905842013-09-20 23:08:21 +00001115/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001116///
Jim Grosbachbd164242011-08-20 16:24:13 +00001117/// expr ::= expr &&,|| expr -> lowest.
1118/// expr ::= expr |,^,&,! expr
1119/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1120/// expr ::= expr <<,>> expr
1121/// expr ::= expr +,- expr
1122/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001123/// expr ::= primaryexpr
1124///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001125bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001126 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001127 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001128 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001129 return true;
1130
Daniel Dunbar55f16672010-09-17 02:47:07 +00001131 // As a special case, we support 'a op b @ modifier' by rewriting the
1132 // expression to include the modifier. This is inefficient, but in general we
1133 // expect users to use 'a@modifier op b'.
1134 if (Lexer.getKind() == AsmToken::At) {
1135 Lex();
1136
1137 if (Lexer.isNot(AsmToken::Identifier))
1138 return TokError("unexpected symbol modifier following '@'");
1139
1140 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001141 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001142 if (Variant == MCSymbolRefExpr::VK_Invalid)
1143 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1144
Jim Grosbach4b905842013-09-20 23:08:21 +00001145 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001146 if (!ModifiedRes) {
1147 return TokError("invalid modifier '" + getTok().getIdentifier() +
1148 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001149 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Daniel Dunbar55f16672010-09-17 02:47:07 +00001151 Res = ModifiedRes;
1152 Lex();
1153 }
1154
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001155 // Try to constant fold it up front, if possible.
1156 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001157 if (Res->evaluateAsAbsolute(Value))
1158 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001159
1160 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001161}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001163bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001164 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001165 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001166}
1167
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001168bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1169 SMLoc &EndLoc) {
1170 if (parseParenExpr(Res, EndLoc))
1171 return true;
1172
1173 for (; ParenDepth > 0; --ParenDepth) {
1174 if (parseBinOpRHS(1, Res, EndLoc))
1175 return true;
1176
1177 // We don't Lex() the last RParen.
1178 // This is the same behavior as parseParenExpression().
1179 if (ParenDepth - 1 > 0) {
Nirav Davea645433c2016-07-18 15:24:03 +00001180 EndLoc = getTok().getEndLoc();
1181 if (parseToken(AsmToken::RParen,
1182 "expected ')' in parentheses expression"))
1183 return true;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001184 }
1185 }
1186 return false;
1187}
1188
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001189bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001190 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001191
Daniel Dunbar75630b32009-06-30 02:10:03 +00001192 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001193 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001194 return true;
1195
Jim Grosbach13760bd2015-05-30 01:25:56 +00001196 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001197 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001198
1199 return false;
1200}
1201
David Majnemer0993e0b2015-10-26 03:15:34 +00001202static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1203 MCBinaryExpr::Opcode &Kind,
1204 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001205 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001206 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001207 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001208
Jim Grosbach4b905842013-09-20 23:08:21 +00001209 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001210 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001211 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001212 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001213 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001214 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001215 return 1;
1216
Jim Grosbach4b905842013-09-20 23:08:21 +00001217 // Low Precedence: |, &, ^
1218 //
1219 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001220 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001221 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001222 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001223 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001224 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001225 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001226 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001227 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001228 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001229
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001231 case AsmToken::EqualEqual:
1232 Kind = MCBinaryExpr::EQ;
1233 return 3;
1234 case AsmToken::ExclaimEqual:
1235 case AsmToken::LessGreater:
1236 Kind = MCBinaryExpr::NE;
1237 return 3;
1238 case AsmToken::Less:
1239 Kind = MCBinaryExpr::LT;
1240 return 3;
1241 case AsmToken::LessEqual:
1242 Kind = MCBinaryExpr::LTE;
1243 return 3;
1244 case AsmToken::Greater:
1245 Kind = MCBinaryExpr::GT;
1246 return 3;
1247 case AsmToken::GreaterEqual:
1248 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001249 return 3;
1250
Jim Grosbach4b905842013-09-20 23:08:21 +00001251 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001252 case AsmToken::LessLess:
1253 Kind = MCBinaryExpr::Shl;
1254 return 4;
1255 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001256 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001257 return 4;
1258
Jim Grosbach4b905842013-09-20 23:08:21 +00001259 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001260 case AsmToken::Plus:
1261 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001262 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001263 case AsmToken::Minus:
1264 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001265 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001266
Jim Grosbach4b905842013-09-20 23:08:21 +00001267 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001268 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001269 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001270 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001271 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001272 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001273 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001274 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001275 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001276 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001277 }
1278}
1279
David Majnemer0993e0b2015-10-26 03:15:34 +00001280static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1281 MCBinaryExpr::Opcode &Kind,
1282 bool ShouldUseLogicalShr) {
1283 switch (K) {
1284 default:
1285 return 0; // not a binop.
1286
1287 // Lowest Precedence: &&, ||
1288 case AsmToken::AmpAmp:
1289 Kind = MCBinaryExpr::LAnd;
1290 return 2;
1291 case AsmToken::PipePipe:
1292 Kind = MCBinaryExpr::LOr;
1293 return 1;
1294
1295 // Low Precedence: ==, !=, <>, <, <=, >, >=
1296 case AsmToken::EqualEqual:
1297 Kind = MCBinaryExpr::EQ;
1298 return 3;
1299 case AsmToken::ExclaimEqual:
1300 case AsmToken::LessGreater:
1301 Kind = MCBinaryExpr::NE;
1302 return 3;
1303 case AsmToken::Less:
1304 Kind = MCBinaryExpr::LT;
1305 return 3;
1306 case AsmToken::LessEqual:
1307 Kind = MCBinaryExpr::LTE;
1308 return 3;
1309 case AsmToken::Greater:
1310 Kind = MCBinaryExpr::GT;
1311 return 3;
1312 case AsmToken::GreaterEqual:
1313 Kind = MCBinaryExpr::GTE;
1314 return 3;
1315
1316 // Low Intermediate Precedence: +, -
1317 case AsmToken::Plus:
1318 Kind = MCBinaryExpr::Add;
1319 return 4;
1320 case AsmToken::Minus:
1321 Kind = MCBinaryExpr::Sub;
1322 return 4;
1323
1324 // High Intermediate Precedence: |, &, ^
1325 //
1326 // FIXME: gas seems to support '!' as an infix operator?
1327 case AsmToken::Pipe:
1328 Kind = MCBinaryExpr::Or;
1329 return 5;
1330 case AsmToken::Caret:
1331 Kind = MCBinaryExpr::Xor;
1332 return 5;
1333 case AsmToken::Amp:
1334 Kind = MCBinaryExpr::And;
1335 return 5;
1336
1337 // Highest Precedence: *, /, %, <<, >>
1338 case AsmToken::Star:
1339 Kind = MCBinaryExpr::Mul;
1340 return 6;
1341 case AsmToken::Slash:
1342 Kind = MCBinaryExpr::Div;
1343 return 6;
1344 case AsmToken::Percent:
1345 Kind = MCBinaryExpr::Mod;
1346 return 6;
1347 case AsmToken::LessLess:
1348 Kind = MCBinaryExpr::Shl;
1349 return 6;
1350 case AsmToken::GreaterGreater:
1351 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1352 return 6;
1353 }
1354}
1355
1356unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1357 MCBinaryExpr::Opcode &Kind) {
1358 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1359 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1360 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1361}
1362
Jim Grosbach4b905842013-09-20 23:08:21 +00001363/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001364/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001365bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001366 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001367 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001368 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001369 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001370
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001371 // If the next token is lower precedence than we are allowed to eat, return
1372 // successfully with what we ate already.
1373 if (TokPrec < Precedence)
1374 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001375
Sean Callanan686ed8d2010-01-19 20:22:31 +00001376 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001377
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001378 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001379 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001380 if (parsePrimaryExpr(RHS, EndLoc))
1381 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001382
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001383 // If BinOp binds less tightly with RHS than the operator after RHS, let
1384 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001385 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001386 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001387 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1388 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001389
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001390 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001391 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001392 }
1393}
1394
Chris Lattner36e02122009-06-21 20:54:55 +00001395/// ParseStatement:
1396/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001397/// ::= Label* Directive ...Operands... EndOfStatement
1398/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001399bool AsmParser::parseStatement(ParseStatementInfo &Info,
1400 MCAsmParserSemaCallback *SI) {
Nirav Davefd910412016-06-17 16:06:17 +00001401 // Eat initial spaces and comments
1402 while (Lexer.is(AsmToken::Space))
1403 Lex();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001404 if (Lexer.is(AsmToken::EndOfStatement)) {
Nirav Davefd910412016-06-17 16:06:17 +00001405 // if this is a line comment we can drop it safely
1406 if (getTok().getString().front() == '\r' ||
1407 getTok().getString().front() == '\n')
1408 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001409 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001410 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001411 }
Nirav Davefd910412016-06-17 16:06:17 +00001412 // Statements always start with an identifier.
Sean Callanan936b0d32010-01-19 21:44:56 +00001413 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001414 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001415 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001416 int64_t LocalLabelVal = -1;
Nirav Davefd910412016-06-17 16:06:17 +00001417 if (Lexer.is(AsmToken::HashDirective))
Jim Grosbach4b905842013-09-20 23:08:21 +00001418 return parseCppHashLineFilenameComment(IDLoc);
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001419 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001420 if (Lexer.is(AsmToken::Integer)) {
1421 LocalLabelVal = getTok().getIntVal();
1422 if (LocalLabelVal < 0) {
1423 if (!TheCondState.Ignore)
1424 return TokError("unexpected token at start of statement");
1425 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001426 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001427 IDVal = getTok().getString();
1428 Lex(); // Consume the integer token to be used as an identifier token.
1429 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001430 if (!TheCondState.Ignore)
1431 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001432 }
1433 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001434 } else if (Lexer.is(AsmToken::Dot)) {
1435 // Treat '.' as a valid identifier in this context.
1436 Lex();
1437 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001438 } else if (Lexer.is(AsmToken::LCurly)) {
1439 // Treat '{' as a valid identifier in this context.
1440 Lex();
1441 IDVal = "{";
1442
1443 } else if (Lexer.is(AsmToken::RCurly)) {
1444 // Treat '}' as a valid identifier in this context.
1445 Lex();
1446 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001447 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001448 if (!TheCondState.Ignore)
1449 return TokError("unexpected token at start of statement");
1450 IDVal = "";
1451 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001452
Chris Lattner926885c2010-04-17 18:14:27 +00001453 // Handle conditional assembly here before checking for skipping. We
1454 // have to do this so that .endif isn't skipped in a ".if 0" block for
1455 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001456 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001457 DirectiveKindMap.find(IDVal);
1458 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1459 ? DK_NO_DIRECTIVE
1460 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001461 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001462 default:
1463 break;
1464 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001465 case DK_IFEQ:
1466 case DK_IFGE:
1467 case DK_IFGT:
1468 case DK_IFLE:
1469 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001470 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001471 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001472 case DK_IFB:
1473 return parseDirectiveIfb(IDLoc, true);
1474 case DK_IFNB:
1475 return parseDirectiveIfb(IDLoc, false);
1476 case DK_IFC:
1477 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001478 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001479 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001480 case DK_IFNC:
1481 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001482 case DK_IFNES:
1483 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001484 case DK_IFDEF:
1485 return parseDirectiveIfdef(IDLoc, true);
1486 case DK_IFNDEF:
1487 case DK_IFNOTDEF:
1488 return parseDirectiveIfdef(IDLoc, false);
1489 case DK_ELSEIF:
1490 return parseDirectiveElseIf(IDLoc);
1491 case DK_ELSE:
1492 return parseDirectiveElse(IDLoc);
1493 case DK_ENDIF:
1494 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001495 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001496
Eli Bendersky88024712013-01-16 19:32:36 +00001497 // Ignore the statement if in the middle of inactive conditional
1498 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001499 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001500 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001501 return false;
1502 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001503
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001504 // FIXME: Recurse on local labels?
1505
1506 // See what kind of statement we have.
1507 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001508 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001509 if (!getTargetParser().isLabel(ID))
1510 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001511 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001512
Chris Lattner36e02122009-06-21 20:54:55 +00001513 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001514 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001515
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001516 // Diagnose attempt to use '.' as a label.
1517 if (IDVal == ".")
1518 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1519
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001520 // Diagnose attempt to use a variable as a label.
1521 //
1522 // FIXME: Diagnostics. Note the location of the definition as a label.
1523 // FIXME: This doesn't diagnose assignment to a symbol which has been
1524 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001525 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001526 if (LocalLabelVal == -1) {
1527 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001528 StringRef RewrittenLabel =
1529 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1530 assert(RewrittenLabel.size() &&
1531 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001532 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1533 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001534 IDVal = RewrittenLabel;
1535 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001536 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001537 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001538 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001539
1540 Sym->redefineIfPossible();
1541
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001542 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001543 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001544
Nirav Dave8ea792d2016-07-13 14:03:12 +00001545 // Consume any end of statement token, if present, to avoid spurious
1546 // AddBlankLine calls().
1547 if (getTok().is(AsmToken::EndOfStatement)) {
1548 Lex();
1549 }
1550
Daniel Dunbare73b2672009-08-26 22:13:22 +00001551 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001552 if (!ParsingInlineAsm)
1553 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001554
Kevin Enderbye7739d42011-12-09 18:09:40 +00001555 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001556 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001557 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001558 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1559 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001560
Tim Northover1744d0a2013-10-25 12:49:50 +00001561 getTargetParser().onLabelParsed(Sym);
1562
Nirav Dave8ea792d2016-07-13 14:03:12 +00001563
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001564
Eli Friedman0f4871d2012-10-22 23:58:19 +00001565 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001566 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001567
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001568 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001569 if (!getTargetParser().equalIsAsmAssignment())
1570 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001571 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001572 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001573
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001575
1576 default: // Normal instruction or directive.
1577 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001578 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001579
1580 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 if (areMacrosEnabled())
1582 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1583 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001584 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001585
Michael J. Spencer530ce852010-10-09 11:00:50 +00001586 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001587
Eli Bendersky17233942013-01-15 22:59:42 +00001588 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001589 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001590 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001591 //
Eli Bendersky17233942013-01-15 22:59:42 +00001592 // 1. The target-specific assembly parser. Some directives are target
1593 // specific or may potentially behave differently on certain targets.
1594 // 2. Asm parser extensions. For example, platform-specific parsers
1595 // (like the ELF parser) register themselves as extensions.
1596 // 3. The generic directive parser implemented by this class. These are
1597 // all the directives that behave in a target and platform independent
1598 // manner, or at least have a default behavior that's shared between
1599 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001600
Oliver Stannard21718282016-07-26 14:19:47 +00001601 getTargetParser().flushPendingInstructions(getStreamer());
1602
Eli Bendersky17233942013-01-15 22:59:42 +00001603 // First query the target-specific parser. It will return 'true' if it
1604 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001605 if (!getTargetParser().ParseDirective(ID))
1606 return false;
1607
Alp Tokercb402912014-01-24 17:20:08 +00001608 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001609 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001610 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1611 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001612 if (Handler.first)
1613 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1614
1615 // Finally, if no one else is interested in this directive, it must be
1616 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001617 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001618 default:
1619 break;
1620 case DK_SET:
1621 case DK_EQU:
1622 return parseDirectiveSet(IDVal, true);
1623 case DK_EQUIV:
1624 return parseDirectiveSet(IDVal, false);
1625 case DK_ASCII:
1626 return parseDirectiveAscii(IDVal, false);
1627 case DK_ASCIZ:
1628 case DK_STRING:
1629 return parseDirectiveAscii(IDVal, true);
1630 case DK_BYTE:
1631 return parseDirectiveValue(1);
1632 case DK_SHORT:
1633 case DK_VALUE:
1634 case DK_2BYTE:
1635 return parseDirectiveValue(2);
1636 case DK_LONG:
1637 case DK_INT:
1638 case DK_4BYTE:
1639 return parseDirectiveValue(4);
1640 case DK_QUAD:
1641 case DK_8BYTE:
1642 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001643 case DK_OCTA:
1644 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001645 case DK_SINGLE:
1646 case DK_FLOAT:
1647 return parseDirectiveRealValue(APFloat::IEEEsingle);
1648 case DK_DOUBLE:
1649 return parseDirectiveRealValue(APFloat::IEEEdouble);
1650 case DK_ALIGN: {
1651 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1652 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1653 }
1654 case DK_ALIGN32: {
1655 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1656 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1657 }
1658 case DK_BALIGN:
1659 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1660 case DK_BALIGNW:
1661 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1662 case DK_BALIGNL:
1663 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1664 case DK_P2ALIGN:
1665 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1666 case DK_P2ALIGNW:
1667 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1668 case DK_P2ALIGNL:
1669 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1670 case DK_ORG:
1671 return parseDirectiveOrg();
1672 case DK_FILL:
1673 return parseDirectiveFill();
1674 case DK_ZERO:
1675 return parseDirectiveZero();
1676 case DK_EXTERN:
1677 eatToEndOfStatement(); // .extern is the default, ignore it.
1678 return false;
1679 case DK_GLOBL:
1680 case DK_GLOBAL:
1681 return parseDirectiveSymbolAttribute(MCSA_Global);
1682 case DK_LAZY_REFERENCE:
1683 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1684 case DK_NO_DEAD_STRIP:
1685 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1686 case DK_SYMBOL_RESOLVER:
1687 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1688 case DK_PRIVATE_EXTERN:
1689 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1690 case DK_REFERENCE:
1691 return parseDirectiveSymbolAttribute(MCSA_Reference);
1692 case DK_WEAK_DEFINITION:
1693 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1694 case DK_WEAK_REFERENCE:
1695 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1696 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1697 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1698 case DK_COMM:
1699 case DK_COMMON:
1700 return parseDirectiveComm(/*IsLocal=*/false);
1701 case DK_LCOMM:
1702 return parseDirectiveComm(/*IsLocal=*/true);
1703 case DK_ABORT:
1704 return parseDirectiveAbort();
1705 case DK_INCLUDE:
1706 return parseDirectiveInclude();
1707 case DK_INCBIN:
1708 return parseDirectiveIncbin();
1709 case DK_CODE16:
1710 case DK_CODE16GCC:
Nirav Davefd910412016-06-17 16:06:17 +00001711 return TokError(Twine(IDVal) +
1712 " not currently supported for this target");
Jim Grosbach4b905842013-09-20 23:08:21 +00001713 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001714 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 case DK_IRP:
1716 return parseDirectiveIrp(IDLoc);
1717 case DK_IRPC:
1718 return parseDirectiveIrpc(IDLoc);
1719 case DK_ENDR:
1720 return parseDirectiveEndr(IDLoc);
1721 case DK_BUNDLE_ALIGN_MODE:
1722 return parseDirectiveBundleAlignMode();
1723 case DK_BUNDLE_LOCK:
1724 return parseDirectiveBundleLock();
1725 case DK_BUNDLE_UNLOCK:
1726 return parseDirectiveBundleUnlock();
1727 case DK_SLEB128:
1728 return parseDirectiveLEB128(true);
1729 case DK_ULEB128:
1730 return parseDirectiveLEB128(false);
1731 case DK_SPACE:
1732 case DK_SKIP:
1733 return parseDirectiveSpace(IDVal);
1734 case DK_FILE:
1735 return parseDirectiveFile(IDLoc);
1736 case DK_LINE:
1737 return parseDirectiveLine();
1738 case DK_LOC:
1739 return parseDirectiveLoc();
1740 case DK_STABS:
1741 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001742 case DK_CV_FILE:
1743 return parseDirectiveCVFile();
1744 case DK_CV_LOC:
1745 return parseDirectiveCVLoc();
1746 case DK_CV_LINETABLE:
1747 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001748 case DK_CV_INLINE_LINETABLE:
1749 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001750 case DK_CV_DEF_RANGE:
1751 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001752 case DK_CV_STRINGTABLE:
1753 return parseDirectiveCVStringTable();
1754 case DK_CV_FILECHECKSUMS:
1755 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001756 case DK_CFI_SECTIONS:
1757 return parseDirectiveCFISections();
1758 case DK_CFI_STARTPROC:
1759 return parseDirectiveCFIStartProc();
1760 case DK_CFI_ENDPROC:
1761 return parseDirectiveCFIEndProc();
1762 case DK_CFI_DEF_CFA:
1763 return parseDirectiveCFIDefCfa(IDLoc);
1764 case DK_CFI_DEF_CFA_OFFSET:
1765 return parseDirectiveCFIDefCfaOffset();
1766 case DK_CFI_ADJUST_CFA_OFFSET:
1767 return parseDirectiveCFIAdjustCfaOffset();
1768 case DK_CFI_DEF_CFA_REGISTER:
1769 return parseDirectiveCFIDefCfaRegister(IDLoc);
1770 case DK_CFI_OFFSET:
1771 return parseDirectiveCFIOffset(IDLoc);
1772 case DK_CFI_REL_OFFSET:
1773 return parseDirectiveCFIRelOffset(IDLoc);
1774 case DK_CFI_PERSONALITY:
1775 return parseDirectiveCFIPersonalityOrLsda(true);
1776 case DK_CFI_LSDA:
1777 return parseDirectiveCFIPersonalityOrLsda(false);
1778 case DK_CFI_REMEMBER_STATE:
1779 return parseDirectiveCFIRememberState();
1780 case DK_CFI_RESTORE_STATE:
1781 return parseDirectiveCFIRestoreState();
1782 case DK_CFI_SAME_VALUE:
1783 return parseDirectiveCFISameValue(IDLoc);
1784 case DK_CFI_RESTORE:
1785 return parseDirectiveCFIRestore(IDLoc);
1786 case DK_CFI_ESCAPE:
1787 return parseDirectiveCFIEscape();
1788 case DK_CFI_SIGNAL_FRAME:
1789 return parseDirectiveCFISignalFrame();
1790 case DK_CFI_UNDEFINED:
1791 return parseDirectiveCFIUndefined(IDLoc);
1792 case DK_CFI_REGISTER:
1793 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001794 case DK_CFI_WINDOW_SAVE:
1795 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 case DK_MACROS_ON:
1797 case DK_MACROS_OFF:
1798 return parseDirectiveMacrosOnOff(IDVal);
1799 case DK_MACRO:
1800 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001801 case DK_EXITM:
1802 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001803 case DK_ENDM:
1804 case DK_ENDMACRO:
1805 return parseDirectiveEndMacro(IDVal);
1806 case DK_PURGEM:
1807 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001808 case DK_END:
1809 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001810 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001811 return parseDirectiveError(IDLoc, false);
1812 case DK_ERROR:
1813 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001814 case DK_WARNING:
1815 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001816 case DK_RELOC:
1817 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001818 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001819
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001820 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001821 }
Chris Lattner36e02122009-06-21 20:54:55 +00001822
Chad Rosierc7f552c2013-02-12 21:33:51 +00001823 // __asm _emit or __asm __emit
1824 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1825 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001826 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001827
1828 // __asm align
1829 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001830 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001831
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001832 if (ParsingInlineAsm && (IDVal == "even"))
1833 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001834 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001835
Chris Lattner7cbfa442010-05-19 23:34:33 +00001836 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001837 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001838 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001839 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001840 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001841 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001842
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001843 // Dump the parsed representation, if requested.
1844 if (getShowParsedOperands()) {
1845 SmallString<256> Str;
1846 raw_svector_ostream OS(Str);
1847 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001848 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001849 if (i != 0)
1850 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001851 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001852 }
1853 OS << "]";
1854
Jim Grosbach4b905842013-09-20 23:08:21 +00001855 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001856 }
1857
Oliver Stannard8b273082014-06-19 15:52:37 +00001858 // If we are generating dwarf for the current section then generate a .loc
1859 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001860 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001861 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001862 getStreamer().getCurrentSection().first)) {
1863 unsigned Line;
1864 if (ActiveMacros.empty())
1865 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1866 else
Frederic Riss16238d92015-06-25 21:57:33 +00001867 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1868 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001869
Eli Bendersky88024712013-01-16 19:32:36 +00001870 // If we previously parsed a cpp hash file line comment then make sure the
1871 // current Dwarf File is for the CppHashFilename if not then emit the
1872 // Dwarf File table for it and adjust the line number for the .loc.
Tim Northoverc0bef992016-04-13 19:46:54 +00001873 if (CppHashInfo.Filename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001874 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
Tim Northoverc0bef992016-04-13 19:46:54 +00001875 0, StringRef(), CppHashInfo.Filename);
David Blaikiec714ef42014-03-17 01:52:11 +00001876 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001877
Jim Grosbach4b905842013-09-20 23:08:21 +00001878 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1879 // cache with the different Loc from the call above we save the last
1880 // info we queried here with SrcMgr.FindLineNumber().
1881 unsigned CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001882 if (LastQueryIDLoc == CppHashInfo.Loc &&
1883 LastQueryBuffer == CppHashInfo.Buf)
Jim Grosbach4b905842013-09-20 23:08:21 +00001884 CppHashLocLineNo = LastQueryLine;
1885 else {
Tim Northoverc0bef992016-04-13 19:46:54 +00001886 CppHashLocLineNo =
1887 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001888 LastQueryLine = CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001889 LastQueryIDLoc = CppHashInfo.Loc;
1890 LastQueryBuffer = CppHashInfo.Buf;
Jim Grosbach4b905842013-09-20 23:08:21 +00001891 }
Tim Northoverc0bef992016-04-13 19:46:54 +00001892 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001893 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001894
Jim Grosbach4b905842013-09-20 23:08:21 +00001895 getStreamer().EmitDwarfLocDirective(
1896 getContext().getGenDwarfFileNumber(), Line, 0,
1897 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1898 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001899 }
1900
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001901 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001902 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001903 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001904 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1905 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001906 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001907 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001908
Chris Lattnera2a9d162010-09-11 16:18:25 +00001909 // Don't skip the rest of the line, the instruction parser is responsible for
1910 // that.
1911 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001912}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001913
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001914// Parse and erase curly braces marking block start/end
1915bool
1916AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1917 // Identify curly brace marking block start/end
1918 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1919 return false;
1920
1921 SMLoc StartLoc = Lexer.getLoc();
1922 Lex(); // Eat the brace
1923 if (Lexer.is(AsmToken::EndOfStatement))
1924 Lex(); // Eat EndOfStatement following the brace
1925
1926 // Erase the block start/end brace from the output asm string
1927 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
1928 StartLoc.getPointer());
1929 return true;
1930}
1931
Jim Grosbach4b905842013-09-20 23:08:21 +00001932/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001933/// ::= # number "filename"
Craig Topper3c76c522015-09-20 23:35:59 +00001934bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001935 Lex(); // Eat the hash token.
Nirav Davefd910412016-06-17 16:06:17 +00001936 // Lexer only ever emits HashDirective if it fully formed if it's
1937 // done the checking already so this is an internal error.
1938 assert(getTok().is(AsmToken::Integer) &&
1939 "Lexing Cpp line comment: Expected Integer");
Kevin Enderby72553612011-09-13 23:45:18 +00001940 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001941 Lex();
Nirav Davefd910412016-06-17 16:06:17 +00001942 assert(getTok().is(AsmToken::String) &&
1943 "Lexing Cpp line comment: Expected String");
Kevin Enderby72553612011-09-13 23:45:18 +00001944 StringRef Filename = getTok().getString();
Nirav Davefd910412016-06-17 16:06:17 +00001945 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001946 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001947 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001948
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001949 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
Tim Northoverc0bef992016-04-13 19:46:54 +00001950 CppHashInfo.Loc = L;
1951 CppHashInfo.Filename = Filename;
1952 CppHashInfo.LineNumber = LineNumber;
1953 CppHashInfo.Buf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001954 return false;
1955}
1956
Jim Grosbach4b905842013-09-20 23:08:21 +00001957/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001958/// for the Filename and LineNo if any in the diagnostic.
1959void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001960 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001961 raw_ostream &OS = errs();
1962
1963 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001964 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001965 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1966 unsigned CppHashBuf =
Tim Northoverc0bef992016-04-13 19:46:54 +00001967 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001968
Jim Grosbach4b905842013-09-20 23:08:21 +00001969 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001970 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001971 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1972 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1973 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001974 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1975 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001976 }
1977
Eric Christophera7c32732012-12-18 00:30:54 +00001978 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001979 // manager changed or buffer changed (like in a nested include) then just
1980 // print the normal diagnostic using its Filename and LineNo.
Tim Northoverc0bef992016-04-13 19:46:54 +00001981 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001982 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001983 if (Parser->SavedDiagHandler)
1984 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1985 else
Craig Topper353eda42014-04-24 06:44:33 +00001986 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001987 return;
1988 }
1989
Eric Christophera7c32732012-12-18 00:30:54 +00001990 // Use the CppHashFilename and calculate a line number based on the
Tim Northoverc0bef992016-04-13 19:46:54 +00001991 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
1992 // for the diagnostic.
1993 const std::string &Filename = Parser->CppHashInfo.Filename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001994
1995 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1996 int CppHashLocLineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001997 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001998 int LineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001999 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002000
Jim Grosbach4b905842013-09-20 23:08:21 +00002001 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2002 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00002003 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002004
Benjamin Kramer47f5e302011-10-16 10:48:29 +00002005 if (Parser->SavedDiagHandler)
2006 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
2007 else
Craig Topper353eda42014-04-24 06:44:33 +00002008 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002009}
2010
Rafael Espindola2c064482012-08-21 18:29:30 +00002011// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
2012// difference being that that function accepts '@' as part of identifiers and
2013// we can't do that. AsmLexer.cpp should probably be changed to handle
2014// '@' as a special case when needed.
2015static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00002016 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
2017 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00002018}
2019
Rafael Espindola34b9c512012-06-03 23:57:14 +00002020bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00002021 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00002022 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00002023 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002024 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002025 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00002026 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00002027 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002028
Preston Gurd05500642012-09-19 20:36:12 +00002029 // A macro without parameters is handled differently on Darwin:
2030 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002031 while (!Body.empty()) {
2032 // Scan for the next substitution.
2033 std::size_t End = Body.size(), Pos = 0;
2034 for (; Pos != End; ++Pos) {
2035 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00002036 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002037 // This macro has no parameters, look for $0, $1, etc.
2038 if (Body[Pos] != '$' || Pos + 1 == End)
2039 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002040
Rafael Espindola1134ab232011-06-05 02:43:45 +00002041 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00002042 if (Next == '$' || Next == 'n' ||
2043 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002044 break;
2045 } else {
2046 // This macro has parameters, look for \foo, \bar, etc.
2047 if (Body[Pos] == '\\' && Pos + 1 != End)
2048 break;
2049 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002050 }
2051
2052 // Add the prefix.
2053 OS << Body.slice(0, Pos);
2054
2055 // Check if we reached the end.
2056 if (Pos == End)
2057 break;
2058
Benjamin Kramer513e7442014-02-20 13:36:32 +00002059 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002060 switch (Body[Pos + 1]) {
2061 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002062 case '$':
2063 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002064 break;
2065
Jim Grosbach4b905842013-09-20 23:08:21 +00002066 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002067 case 'n':
2068 OS << A.size();
2069 break;
2070
Jim Grosbach4b905842013-09-20 23:08:21 +00002071 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002072 default: {
2073 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002074 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002075 if (Index >= A.size())
2076 break;
2077
2078 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002079 for (const AsmToken &Token : A[Index])
2080 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002081 break;
2082 }
2083 }
2084 Pos += 2;
2085 } else {
2086 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002087
2088 // Check for the \@ pseudo-variable.
2089 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002090 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002091 else
2092 while (isIdentifierChar(Body[I]) && I + 1 != End)
2093 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002094
Jim Grosbach4b905842013-09-20 23:08:21 +00002095 const char *Begin = Body.data() + Pos + 1;
2096 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002097 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002098
Toma Tabacu217116e2015-04-27 10:50:29 +00002099 if (Argument == "@") {
2100 OS << NumOfMacroInstantiations;
2101 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002102 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002103 for (; Index < NParameters; ++Index)
2104 if (Parameters[Index].Name == Argument)
2105 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002106
Toma Tabacu217116e2015-04-27 10:50:29 +00002107 if (Index == NParameters) {
2108 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2109 Pos += 3;
2110 else {
2111 OS << '\\' << Argument;
2112 Pos = I;
2113 }
2114 } else {
2115 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002116 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002117 // We expect no quotes around the string's contents when
2118 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002119 if (Token.getKind() != AsmToken::String || VarargParameter)
2120 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002121 else
Craig Topper84008482015-10-10 05:38:14 +00002122 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002123
2124 Pos += 1 + Argument.size();
2125 }
Preston Gurd05500642012-09-19 20:36:12 +00002126 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002127 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002128 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002129 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002130 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002131
Rafael Espindola1134ab232011-06-05 02:43:45 +00002132 return false;
2133}
Daniel Dunbar43235712010-07-18 18:54:11 +00002134
Nico Weber2a8f9222014-07-24 16:29:04 +00002135MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002136 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002137 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002138 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002139
Jim Grosbach4b905842013-09-20 23:08:21 +00002140static bool isOperator(AsmToken::TokenKind kind) {
2141 switch (kind) {
2142 default:
2143 return false;
2144 case AsmToken::Plus:
2145 case AsmToken::Minus:
2146 case AsmToken::Tilde:
2147 case AsmToken::Slash:
2148 case AsmToken::Star:
2149 case AsmToken::Dot:
2150 case AsmToken::Equal:
2151 case AsmToken::EqualEqual:
2152 case AsmToken::Pipe:
2153 case AsmToken::PipePipe:
2154 case AsmToken::Caret:
2155 case AsmToken::Amp:
2156 case AsmToken::AmpAmp:
2157 case AsmToken::Exclaim:
2158 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002159 case AsmToken::Less:
2160 case AsmToken::LessEqual:
2161 case AsmToken::LessLess:
2162 case AsmToken::LessGreater:
2163 case AsmToken::Greater:
2164 case AsmToken::GreaterEqual:
2165 case AsmToken::GreaterGreater:
2166 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002167 }
2168}
2169
David Majnemer16252452014-01-29 00:07:39 +00002170namespace {
2171class AsmLexerSkipSpaceRAII {
2172public:
2173 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2174 Lexer.setSkipSpace(SkipSpace);
2175 }
2176
2177 ~AsmLexerSkipSpaceRAII() {
2178 Lexer.setSkipSpace(true);
2179 }
2180
2181private:
2182 AsmLexer &Lexer;
2183};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002184}
David Majnemer16252452014-01-29 00:07:39 +00002185
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002186bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2187
2188 if (Vararg) {
2189 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2190 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002191 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002192 }
2193 return false;
2194 }
2195
Rafael Espindola768b41c2012-06-15 14:02:34 +00002196 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002197
David Majnemer16252452014-01-29 00:07:39 +00002198 // Darwin doesn't use spaces to delmit arguments.
2199 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002200
Scott Egertona1fa68a2016-02-11 13:48:49 +00002201 bool SpaceEaten;
2202
Rafael Espindola768b41c2012-06-15 14:02:34 +00002203 for (;;) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002204 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002205 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002206 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002207
Scott Egertona1fa68a2016-02-11 13:48:49 +00002208 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002209
Scott Egertona1fa68a2016-02-11 13:48:49 +00002210 if (Lexer.is(AsmToken::Comma))
2211 break;
2212
2213 if (Lexer.is(AsmToken::Space)) {
2214 SpaceEaten = true;
Nirav Dave1180e6892016-06-02 17:15:05 +00002215 Lexer.Lex(); // Eat spaces
Scott Egertona1fa68a2016-02-11 13:48:49 +00002216 }
Preston Gurd05500642012-09-19 20:36:12 +00002217
2218 // Spaces can delimit parameters, but could also be part an expression.
2219 // If the token after a space is an operator, add the token and the next
2220 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002221 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002222 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002223 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002224 Lexer.Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002225
Scott Egertona1fa68a2016-02-11 13:48:49 +00002226 // Whitespace after an operator can be ignored.
2227 if (Lexer.is(AsmToken::Space))
Nirav Dave1180e6892016-06-02 17:15:05 +00002228 Lexer.Lex();
Scott Egertona1fa68a2016-02-11 13:48:49 +00002229
2230 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002231 }
2232 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002233 if (SpaceEaten)
2234 break;
Preston Gurd05500642012-09-19 20:36:12 +00002235 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002236
Jim Grosbach4b905842013-09-20 23:08:21 +00002237 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002238 // to be able to fill in the remaining default parameter values
2239 if (Lexer.is(AsmToken::EndOfStatement))
2240 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002241
2242 // Adjust the current parentheses level.
2243 if (Lexer.is(AsmToken::LParen))
2244 ++ParenLevel;
2245 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2246 --ParenLevel;
2247
2248 // Append the token to the current argument list.
2249 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002250 Lexer.Lex();
Rafael Espindola768b41c2012-06-15 14:02:34 +00002251 }
Preston Gurd05500642012-09-19 20:36:12 +00002252
Rafael Espindola768b41c2012-06-15 14:02:34 +00002253 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002254 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002255 return false;
2256}
2257
2258// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002259bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002260 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002261 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002262 bool NamedParametersFound = false;
2263 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002264
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002265 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002266 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002267
Rafael Espindola768b41c2012-06-15 14:02:34 +00002268 // Parse two kinds of macro invocations:
2269 // - macros defined without any parameters accept an arbitrary number of them
2270 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002271 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002272 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2273 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002274 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002275 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002276
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002277 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002278 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002279 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002280 eatToEndOfStatement();
2281 return true;
2282 }
2283
Nirav Davea645433c2016-07-18 15:24:03 +00002284 if (Lexer.isNot(AsmToken::Equal)) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002285 TokError("expected '=' after formal parameter identifier");
2286 eatToEndOfStatement();
2287 return true;
2288 }
2289 Lex();
2290
2291 NamedParametersFound = true;
2292 }
2293
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002294 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002295 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002296 eatToEndOfStatement();
2297 return true;
2298 }
2299
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002300 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2301 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002302 return true;
2303
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002304 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002305 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002306 unsigned FAI = 0;
2307 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002308 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002309 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002310
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002311 if (FAI >= NParameters) {
Nirav Davefd910412016-06-17 16:06:17 +00002312 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002313 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002314 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002315 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002316 return true;
2317 }
2318 PI = FAI;
2319 }
2320
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002321 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002322 if (A.size() <= PI)
2323 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002324 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002325
2326 if (FALocs.size() <= PI)
2327 FALocs.resize(PI + 1);
2328
2329 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002330 }
Jim Grosbach206661622012-07-30 22:44:17 +00002331
Preston Gurd242ed3152012-09-19 20:29:04 +00002332 // At the end of the statement, fill in remaining arguments that have
2333 // default values. If there aren't any, then the next argument is
2334 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002335 if (Lexer.is(AsmToken::EndOfStatement)) {
2336 bool Failure = false;
2337 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2338 if (A[FAI].empty()) {
2339 if (M->Parameters[FAI].Required) {
2340 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2341 "missing value for required parameter "
2342 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2343 Failure = true;
2344 }
2345
2346 if (!M->Parameters[FAI].Value.empty())
2347 A[FAI] = M->Parameters[FAI].Value;
2348 }
2349 }
2350 return Failure;
2351 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002352
2353 if (Lexer.is(AsmToken::Comma))
2354 Lex();
2355 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002356
2357 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002358}
2359
Jim Grosbach4b905842013-09-20 23:08:21 +00002360const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002361 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2362 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002363}
2364
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002365void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2366 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002367}
2368
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002369void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002370
Jim Grosbach4b905842013-09-20 23:08:21 +00002371bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Davide Italiano7c9fc732016-07-27 05:51:56 +00002372 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2373 // eliminate this, although we should protect against infinite loops.
2374 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2375 if (ActiveMacros.size() == MaxNestingDepth) {
2376 std::ostringstream MaxNestingDepthError;
2377 MaxNestingDepthError << "macros cannot be nested more than "
2378 << MaxNestingDepth << " levels deep."
2379 << " Use -asm-macro-max-nesting-depth to increase "
2380 "this limit.";
2381 return TokError(MaxNestingDepthError.str());
2382 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002383
Eli Bendersky38274122013-01-14 23:22:36 +00002384 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002385 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002386 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002387
Rafael Espindola1134ab232011-06-05 02:43:45 +00002388 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2389 // to hold the macro body with substitutions.
2390 SmallString<256> Buf;
2391 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002392 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002393
Toma Tabacu217116e2015-04-27 10:50:29 +00002394 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002395 return true;
2396
Eli Bendersky38274122013-01-14 23:22:36 +00002397 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002398 // instantiation.
2399 OS << ".endmacro\n";
2400
Rafael Espindola3560ff22014-08-27 20:03:13 +00002401 std::unique_ptr<MemoryBuffer> Instantiation =
2402 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002403
Daniel Dunbar43235712010-07-18 18:54:11 +00002404 // Create the macro instantiation object and add to the current macro
2405 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002406 MacroInstantiation *MI = new MacroInstantiation(
2407 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002408 ActiveMacros.push_back(MI);
2409
Toma Tabacu217116e2015-04-27 10:50:29 +00002410 ++NumOfMacroInstantiations;
2411
Daniel Dunbar43235712010-07-18 18:54:11 +00002412 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002413 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002414 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002415 Lex();
2416
2417 return false;
2418}
2419
Jim Grosbach4b905842013-09-20 23:08:21 +00002420void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002421 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002422 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002423 Lex();
2424
2425 // Pop the instantiation entry.
2426 delete ActiveMacros.back();
2427 ActiveMacros.pop_back();
2428}
2429
Jim Grosbach4b905842013-09-20 23:08:21 +00002430bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002431 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002432 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002433 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002434 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2435 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002436 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002437
Pete Cooper80d21cb2015-06-22 19:35:57 +00002438 if (!Sym) {
2439 // In the case where we parse an expression starting with a '.', we will
2440 // not generate an error, nor will we create a symbol. In this case we
2441 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002442 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002443 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002444
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002445 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002446 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002447 if (NoDeadStrip)
2448 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2449
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002450 return false;
2451}
2452
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002453/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002454/// ::= identifier
2455/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002456bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002457 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002458 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2459 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002460 // handle this as a context dependent token, instead we detect adjacent tokens
2461 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002462 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2463 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002464
Hans Wennborgce69d772013-10-18 20:46:28 +00002465 // Consume the prefix character, and check for a following identifier.
Nirav Dave1180e6892016-06-02 17:15:05 +00002466 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002467 if (Lexer.isNot(AsmToken::Identifier))
2468 return true;
2469
Hans Wennborgce69d772013-10-18 20:46:28 +00002470 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2471 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002472 return true;
2473
2474 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002475 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002476 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Nirav Davefd910412016-06-17 16:06:17 +00002477 Lex(); // Parser Lex to maintain invariants.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002478 return false;
2479 }
2480
Jim Grosbach4b905842013-09-20 23:08:21 +00002481 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002482 return true;
2483
Sean Callanan936b0d32010-01-19 21:44:56 +00002484 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002485
Sean Callanan686ed8d2010-01-19 20:22:31 +00002486 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002487
2488 return false;
2489}
2490
Jim Grosbach4b905842013-09-20 23:08:21 +00002491/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002492/// ::= .equ identifier ',' expression
2493/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002494/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002495bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002496 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002497
Nirav Davea645433c2016-07-18 15:24:03 +00002498 if (check(parseIdentifier(Name),
2499 "expected identifier after '" + Twine(IDVal) + "'") ||
2500 parseToken(AsmToken::Comma, "unexpected token in '" + Twine(IDVal) + "'"))
2501 return true;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002502
Jim Grosbach4b905842013-09-20 23:08:21 +00002503 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002504}
2505
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002506bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002507 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002508
2509 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002510 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002511 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2512 if (Str[i] != '\\') {
2513 Data += Str[i];
2514 continue;
2515 }
2516
2517 // Recognize escaped characters. Note that this escape semantics currently
2518 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2519 ++i;
2520 if (i == e)
2521 return TokError("unexpected backslash at end of string");
2522
2523 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002524 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002525 // Consume up to three octal characters.
2526 unsigned Value = Str[i] - '0';
2527
Jim Grosbach4b905842013-09-20 23:08:21 +00002528 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002529 ++i;
2530 Value = Value * 8 + (Str[i] - '0');
2531
Jim Grosbach4b905842013-09-20 23:08:21 +00002532 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002533 ++i;
2534 Value = Value * 8 + (Str[i] - '0');
2535 }
2536 }
2537
2538 if (Value > 255)
2539 return TokError("invalid octal escape sequence (out of range)");
2540
Jim Grosbach4b905842013-09-20 23:08:21 +00002541 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002542 continue;
2543 }
2544
2545 // Otherwise recognize individual escapes.
2546 switch (Str[i]) {
2547 default:
2548 // Just reject invalid escape sequences for now.
2549 return TokError("invalid escape sequence (unrecognized character)");
2550
2551 case 'b': Data += '\b'; break;
2552 case 'f': Data += '\f'; break;
2553 case 'n': Data += '\n'; break;
2554 case 'r': Data += '\r'; break;
2555 case 't': Data += '\t'; break;
2556 case '"': Data += '"'; break;
2557 case '\\': Data += '\\'; break;
2558 }
2559 }
2560
Nirav Davea645433c2016-07-18 15:24:03 +00002561 Lex();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002562 return false;
2563}
2564
Jim Grosbach4b905842013-09-20 23:08:21 +00002565/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002566/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002567bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002568 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002569 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002570
Daniel Dunbara10e5192009-06-24 23:30:00 +00002571 for (;;) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002572 std::string Data;
Nirav Davea645433c2016-07-18 15:24:03 +00002573 if (check(getTok().isNot(AsmToken::String),
2574 "expected string in '" + Twine(IDVal) + "' directive") ||
2575 parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002576 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002577
Rafael Espindola64e1af82013-07-02 15:49:13 +00002578 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002579 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002580 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002581
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002582 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002583 break;
2584
Nirav Davea645433c2016-07-18 15:24:03 +00002585 if (parseToken(AsmToken::Comma,
2586 "unexpected token in '" + Twine(IDVal) + "' directive"))
2587 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002588 }
2589 }
2590
Sean Callanan686ed8d2010-01-19 20:22:31 +00002591 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002592 return false;
2593}
2594
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002595/// parseDirectiveReloc
2596/// ::= .reloc expression , identifier [ , expression ]
2597bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2598 const MCExpr *Offset;
2599 const MCExpr *Expr = nullptr;
2600
2601 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2602 if (parseExpression(Offset))
2603 return true;
2604
2605 // We can only deal with constant expressions at the moment.
2606 int64_t OffsetValue;
Nirav Davea645433c2016-07-18 15:24:03 +00002607 if (check(!Offset->evaluateAsAbsolute(OffsetValue), OffsetLoc,
2608 "expression is not a constant value") ||
2609 check(OffsetValue < 0, OffsetLoc, "expression is negative") ||
2610 parseToken(AsmToken::Comma, "expected comma") ||
2611 check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
2612 return true;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002613
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002614 SMLoc NameLoc = Lexer.getTok().getLoc();
2615 StringRef Name = Lexer.getTok().getIdentifier();
Nirav Davefd910412016-06-17 16:06:17 +00002616 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002617
2618 if (Lexer.is(AsmToken::Comma)) {
Nirav Davefd910412016-06-17 16:06:17 +00002619 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002620 SMLoc ExprLoc = Lexer.getLoc();
2621 if (parseExpression(Expr))
2622 return true;
2623
2624 MCValue Value;
2625 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2626 return Error(ExprLoc, "expression must be relocatable");
2627 }
2628
Nirav Davea645433c2016-07-18 15:24:03 +00002629 if (parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00002630 "unexpected token in .reloc directive"))
2631 return true;
2632
2633 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2634 return Error(NameLoc, "unknown relocation name");
2635
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002636 return false;
2637}
2638
Jim Grosbach4b905842013-09-20 23:08:21 +00002639/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002640/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002641bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002642 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002643 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002644
Daniel Dunbara10e5192009-06-24 23:30:00 +00002645 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002646 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002647 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002648 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002649 return true;
2650
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002651 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002652 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2653 assert(Size <= 8 && "Invalid size");
2654 uint64_t IntValue = MCE->getValue();
2655 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2656 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002657 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002658 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002659 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002660
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002661 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002662 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002663
Daniel Dunbara10e5192009-06-24 23:30:00 +00002664 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002665 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2666 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002667 }
2668 }
2669
Sean Callanan686ed8d2010-01-19 20:22:31 +00002670 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002671 return false;
2672}
2673
David Woodhoused6de0d92014-02-01 16:20:59 +00002674/// ParseDirectiveOctaValue
2675/// ::= .octa [ hexconstant (, hexconstant)* ]
2676bool AsmParser::parseDirectiveOctaValue() {
2677 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2678 checkForValidSection();
2679
2680 for (;;) {
Nirav Davea645433c2016-07-18 15:24:03 +00002681 if (getTok().is(AsmToken::Error))
David Woodhoused6de0d92014-02-01 16:20:59 +00002682 return true;
Nirav Davea645433c2016-07-18 15:24:03 +00002683 if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::BigNum))
David Woodhoused6de0d92014-02-01 16:20:59 +00002684 return TokError("unknown token in expression");
2685
2686 SMLoc ExprLoc = getLexer().getLoc();
2687 APInt IntValue = getTok().getAPIntVal();
2688 Lex();
2689
2690 uint64_t hi, lo;
2691 if (IntValue.isIntN(64)) {
2692 hi = 0;
2693 lo = IntValue.getZExtValue();
2694 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002695 // It might actually have more than 128 bits, but the top ones are zero.
2696 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002697 lo = IntValue.getLoBits(64).getZExtValue();
2698 } else
2699 return Error(ExprLoc, "literal value out of range for directive");
2700
2701 if (MAI.isLittleEndian()) {
2702 getStreamer().EmitIntValue(lo, 8);
2703 getStreamer().EmitIntValue(hi, 8);
2704 } else {
2705 getStreamer().EmitIntValue(hi, 8);
2706 getStreamer().EmitIntValue(lo, 8);
2707 }
2708
2709 if (getLexer().is(AsmToken::EndOfStatement))
2710 break;
2711
2712 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002713 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2714 return true;
David Woodhoused6de0d92014-02-01 16:20:59 +00002715 }
2716 }
2717
2718 Lex();
2719 return false;
2720}
2721
Jim Grosbach4b905842013-09-20 23:08:21 +00002722/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002723/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002724bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002725 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002726 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002727
2728 for (;;) {
2729 // We don't truly support arithmetic on floating point expressions, so we
2730 // have to manually parse unary prefixes.
2731 bool IsNeg = false;
2732 if (getLexer().is(AsmToken::Minus)) {
Nirav Dave1180e6892016-06-02 17:15:05 +00002733 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002734 IsNeg = true;
2735 } else if (getLexer().is(AsmToken::Plus))
Nirav Dave1180e6892016-06-02 17:15:05 +00002736 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002737
Nirav Dave1180e6892016-06-02 17:15:05 +00002738 if (Lexer.is(AsmToken::Error))
2739 return TokError(Lexer.getErr());
2740 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
2741 Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002742 return TokError("unexpected token in directive");
2743
2744 // Convert to an APFloat.
2745 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002746 StringRef IDVal = getTok().getString();
2747 if (getLexer().is(AsmToken::Identifier)) {
2748 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2749 Value = APFloat::getInf(Semantics);
2750 else if (!IDVal.compare_lower("nan"))
2751 Value = APFloat::getNaN(Semantics, false, ~0);
2752 else
2753 return TokError("invalid floating point literal");
2754 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002755 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002756 return TokError("invalid floating point literal");
2757 if (IsNeg)
2758 Value.changeSign();
2759
2760 // Consume the numeric token.
2761 Lex();
2762
2763 // Emit the value as an integer.
2764 APInt AsInt = Value.bitcastToAPInt();
2765 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002766 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002767
Nirav Dave1180e6892016-06-02 17:15:05 +00002768 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002769 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002770
Nirav Davea645433c2016-07-18 15:24:03 +00002771 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2772 return true;
Daniel Dunbar2af16532010-09-24 01:59:56 +00002773 }
2774 }
2775
2776 Lex();
2777 return false;
2778}
2779
Jim Grosbach4b905842013-09-20 23:08:21 +00002780/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002781/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002782bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002783 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002784
Petr Hosek67a94a72016-05-28 05:57:48 +00002785 SMLoc NumBytesLoc = Lexer.getLoc();
2786 const MCExpr *NumBytes;
2787 if (parseExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002788 return true;
2789
Rafael Espindolab91bac62010-10-05 19:42:57 +00002790 int64_t Val = 0;
2791 if (getLexer().is(AsmToken::Comma)) {
2792 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002793 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002794 return true;
2795 }
2796
Nirav Davea645433c2016-07-18 15:24:03 +00002797 if (parseToken(AsmToken::EndOfStatement,
2798 "unexpected token in '.zero' directive"))
2799 return true;
Petr Hosek67a94a72016-05-28 05:57:48 +00002800 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002801
2802 return false;
2803}
2804
Jim Grosbach4b905842013-09-20 23:08:21 +00002805/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002806/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002807bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002808 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002809
Petr Hosek67a94a72016-05-28 05:57:48 +00002810 SMLoc NumValuesLoc = Lexer.getLoc();
2811 const MCExpr *NumValues;
2812 if (parseExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002813 return true;
2814
Roman Divackye33098f2013-09-24 17:44:41 +00002815 int64_t FillSize = 1;
2816 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002817
David Majnemer522d3db2014-02-01 07:19:38 +00002818 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara10e5192009-06-24 23:30:00 +00002820
Nirav Davea645433c2016-07-18 15:24:03 +00002821 if (parseToken(AsmToken::Comma, "unexpected token in '.fill' directive") ||
2822 getTokenLoc(SizeLoc) || parseAbsoluteExpression(FillSize))
Roman Divackye33098f2013-09-24 17:44:41 +00002823 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002824
Roman Divackye33098f2013-09-24 17:44:41 +00002825 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002826 if (parseToken(AsmToken::Comma,
2827 "unexpected token in '.fill' directive") ||
2828 getTokenLoc(ExprLoc) || parseAbsoluteExpression(FillExpr) ||
2829 parseToken(AsmToken::EndOfStatement,
2830 "unexpected token in '.fill' directive"))
Roman Divackye33098f2013-09-24 17:44:41 +00002831 return true;
Roman Divackye33098f2013-09-24 17:44:41 +00002832 }
2833 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002834
David Majnemer522d3db2014-02-01 07:19:38 +00002835 if (FillSize < 0) {
2836 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
Petr Hosek6abd38b2016-05-28 08:20:08 +00002837 return false;
David Majnemer522d3db2014-02-01 07:19:38 +00002838 }
2839 if (FillSize > 8) {
2840 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2841 FillSize = 8;
2842 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002843
David Majnemer522d3db2014-02-01 07:19:38 +00002844 if (!isUInt<32>(FillExpr) && FillSize > 4)
2845 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2846
Petr Hosek67a94a72016-05-28 05:57:48 +00002847 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002848
2849 return false;
2850}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002851
Jim Grosbach4b905842013-09-20 23:08:21 +00002852/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002853/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002854bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002855 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002856
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002857 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002858 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002859 return true;
2860
2861 // Parse optional fill expression.
2862 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002863 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002864 if (parseToken(AsmToken::Comma, "unexpected token in '.org' directive") ||
2865 parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002866 return true;
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002867 }
2868
Nirav Davea645433c2016-07-18 15:24:03 +00002869 if (parseToken(AsmToken::EndOfStatement,
2870 "unexpected token in '.org' directive"))
2871 return true;
2872
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002873 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002874 return false;
2875}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002876
Jim Grosbach4b905842013-09-20 23:08:21 +00002877/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002878/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002879bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002880 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002881
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002882 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002883 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002884 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002885 return true;
2886
2887 SMLoc MaxBytesLoc;
2888 bool HasFillExpr = false;
2889 int64_t FillExpr = 0;
2890 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002891 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002892 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2893 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002894
2895 // The fill expression can be omitted while specifying a maximum number of
2896 // alignment bytes, e.g:
2897 // .align 3,,4
Nirav Davea645433c2016-07-18 15:24:03 +00002898 if (getTok().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002899 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002900 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002901 return true;
2902 }
2903
Nirav Davea645433c2016-07-18 15:24:03 +00002904 if (getTok().isNot(AsmToken::EndOfStatement)) {
2905 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
2906 getTokenLoc(MaxBytesLoc) || parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002907 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002908 }
2909 }
2910
Nirav Davea645433c2016-07-18 15:24:03 +00002911 if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
2912 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002913
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002914 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002915 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002916
2917 // Compute alignment in bytes.
2918 if (IsPow2) {
2919 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002920 if (Alignment >= 32) {
2921 Error(AlignmentLoc, "invalid alignment value");
2922 Alignment = 31;
2923 }
2924
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002925 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002926 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002927 // Reject alignments that aren't either a power of two or zero,
2928 // for gas compatibility. Alignment of zero is silently rounded
2929 // up to one.
2930 if (Alignment == 0)
2931 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002932 if (!isPowerOf2_64(Alignment))
2933 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002934 }
2935
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002936 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002937 if (MaxBytesLoc.isValid()) {
2938 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002939 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002940 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002941 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002942 }
2943
2944 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002945 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002946 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002947 MaxBytesToFill = 0;
2948 }
2949 }
2950
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002951 // Check whether we should use optimal code alignment for this .align
2952 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002953 const MCSection *Section = getStreamer().getCurrentSection().first;
2954 assert(Section && "must have section to emit alignment");
2955 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002956 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2957 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002958 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002959 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002960 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002961 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2962 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002963 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002964
2965 return false;
2966}
2967
Jim Grosbach4b905842013-09-20 23:08:21 +00002968/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002969/// ::= .file [number] filename
2970/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002971bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002972 // FIXME: I'm not sure what this is.
2973 int64_t FileNumber = -1;
2974 SMLoc FileNumberLoc = getLexer().getLoc();
2975 if (getLexer().is(AsmToken::Integer)) {
2976 FileNumber = getTok().getIntVal();
2977 Lex();
2978
2979 if (FileNumber < 1)
2980 return TokError("file number less than one");
2981 }
2982
Nirav Davea645433c2016-07-18 15:24:03 +00002983 std::string Path = getTok().getString();
Eli Bendersky17233942013-01-15 22:59:42 +00002984
2985 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002986 // Allow the strings to have escaped octal character sequence.
Nirav Davea645433c2016-07-18 15:24:03 +00002987 if (check(getTok().isNot(AsmToken::String),
2988 "unexpected token in '.file' directive") ||
2989 parseEscapedString(Path))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002990 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002991
2992 StringRef Directory;
2993 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002994 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002995 if (getLexer().is(AsmToken::String)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002996 if (check(FileNumber == -1,
2997 "explicit path specified, but no file number") ||
2998 parseEscapedString(FilenameData))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002999 return true;
3000 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00003001 Directory = Path;
Eli Bendersky17233942013-01-15 22:59:42 +00003002 } else {
3003 Filename = Path;
3004 }
3005
Nirav Davea645433c2016-07-18 15:24:03 +00003006 if (parseToken(AsmToken::EndOfStatement,
3007 "unexpected token in '.file' directive"))
3008 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003009
3010 if (FileNumber == -1)
3011 getStreamer().EmitFileDirective(Filename);
3012 else {
David Blaikie22748082016-05-26 00:22:26 +00003013 // If there is -g option as well as debug info from directive file,
3014 // we turn off -g option, directly use the existing debug info instead.
David Blaikiedc3f01e2015-03-09 01:57:13 +00003015 if (getContext().getGenDwarfForAssembly())
David Blaikie22748082016-05-26 00:22:26 +00003016 getContext().setGenDwarfForAssembly(false);
3017 else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
David Blaikiec714ef42014-03-17 01:52:11 +00003018 0)
Eli Bendersky17233942013-01-15 22:59:42 +00003019 Error(FileNumberLoc, "file number already allocated");
3020 }
3021
3022 return false;
3023}
3024
Jim Grosbach4b905842013-09-20 23:08:21 +00003025/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003026/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003027bool AsmParser::parseDirectiveLine() {
Nirav Davea645433c2016-07-18 15:24:03 +00003028 int64_t LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003029 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003030 if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
3031 return true;
Jim Grosbach4b905842013-09-20 23:08:21 +00003032 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003033 // FIXME: Do something with the .line.
3034 }
Nirav Davea645433c2016-07-18 15:24:03 +00003035 if (parseToken(AsmToken::EndOfStatement,
3036 "unexpected token in '.line' directive"))
3037 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003038
3039 return false;
3040}
3041
Jim Grosbach4b905842013-09-20 23:08:21 +00003042/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003043/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3044/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3045/// The first number is a file number, must have been previously assigned with
3046/// a .file directive, the second number is the line number and optionally the
3047/// third number is a column position (zero if not specified). The remaining
3048/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003049bool AsmParser::parseDirectiveLoc() {
Nirav Davea645433c2016-07-18 15:24:03 +00003050 int64_t FileNumber = 0, LineNumber = 0;
3051 SMLoc Loc = getTok().getLoc();
3052 if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
3053 check(FileNumber < 1, Loc,
3054 "file number less than one in '.loc' directive") ||
3055 check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
3056 "unassigned file number in '.loc' directive"))
3057 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003058
Nirav Davea645433c2016-07-18 15:24:03 +00003059 // optional
Eli Bendersky17233942013-01-15 22:59:42 +00003060 if (getLexer().is(AsmToken::Integer)) {
3061 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003062 if (LineNumber < 0)
3063 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003064 Lex();
3065 }
3066
3067 int64_t ColumnPos = 0;
3068 if (getLexer().is(AsmToken::Integer)) {
3069 ColumnPos = getTok().getIntVal();
3070 if (ColumnPos < 0)
3071 return TokError("column position less than zero in '.loc' directive");
3072 Lex();
3073 }
3074
3075 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3076 unsigned Isa = 0;
3077 int64_t Discriminator = 0;
3078 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3079 for (;;) {
3080 if (getLexer().is(AsmToken::EndOfStatement))
3081 break;
3082
3083 StringRef Name;
3084 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003085 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003086 return TokError("unexpected token in '.loc' directive");
3087
3088 if (Name == "basic_block")
3089 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3090 else if (Name == "prologue_end")
3091 Flags |= DWARF2_FLAG_PROLOGUE_END;
3092 else if (Name == "epilogue_begin")
3093 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3094 else if (Name == "is_stmt") {
3095 Loc = getTok().getLoc();
3096 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003097 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return true;
3099 // The expression must be the constant 0 or 1.
3100 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3101 int Value = MCE->getValue();
3102 if (Value == 0)
3103 Flags &= ~DWARF2_FLAG_IS_STMT;
3104 else if (Value == 1)
3105 Flags |= DWARF2_FLAG_IS_STMT;
3106 else
3107 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003108 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3110 }
Craig Topperf15655b2013-04-22 04:22:40 +00003111 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003112 Loc = getTok().getLoc();
3113 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003114 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003115 return true;
3116 // The expression must be a constant greater or equal to 0.
3117 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3118 int Value = MCE->getValue();
3119 if (Value < 0)
3120 return Error(Loc, "isa number less than zero");
3121 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003122 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003123 return Error(Loc, "isa number not a constant value");
3124 }
Craig Topperf15655b2013-04-22 04:22:40 +00003125 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003126 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003127 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003128 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003129 return Error(Loc, "unknown sub-directive in '.loc' directive");
3130 }
3131
3132 if (getLexer().is(AsmToken::EndOfStatement))
3133 break;
3134 }
3135 }
Nirav Davea645433c2016-07-18 15:24:03 +00003136 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003137
3138 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3139 Isa, Discriminator, StringRef());
3140
3141 return false;
3142}
3143
Jim Grosbach4b905842013-09-20 23:08:21 +00003144/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003145/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003146bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003147 return TokError("unsupported directive '.stabs'");
3148}
3149
Reid Kleckner2214ed82016-01-29 00:49:42 +00003150/// parseDirectiveCVFile
3151/// ::= .cv_file number filename
3152bool AsmParser::parseDirectiveCVFile() {
Nirav Davea645433c2016-07-18 15:24:03 +00003153 SMLoc FileNumberLoc = getTok().getLoc();
3154 int64_t FileNumber;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003155 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00003156
3157 if (parseIntToken(FileNumber,
3158 "expected file number in '.cv_file' directive") ||
3159 check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
3160 check(getTok().isNot(AsmToken::String),
3161 "unexpected token in '.cv_file' directive") ||
3162 // Usually directory and filename are together, otherwise just
3163 // directory. Allow the strings to have escaped octal character sequence.
3164 parseEscapedString(Filename) ||
3165 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00003166 "unexpected token in '.cv_file' directive"))
3167 return true;
3168
3169 if (getStreamer().EmitCVFileDirective(FileNumber, Filename) == 0)
3170 Error(FileNumberLoc, "file number already allocated");
Reid Kleckner2214ed82016-01-29 00:49:42 +00003171
3172 return false;
3173}
3174
3175/// parseDirectiveCVLoc
3176/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3177/// [is_stmt VALUE]
3178/// The first number is a file number, must have been previously assigned with
3179/// a .file directive, the second number is the line number and optionally the
3180/// third number is a column position (zero if not specified). The remaining
3181/// optional items are .loc sub-directives.
3182bool AsmParser::parseDirectiveCVLoc() {
Nirav Davea645433c2016-07-18 15:24:03 +00003183 SMLoc Loc;
3184 int64_t FunctionId, FileNumber;
3185 if (getTokenLoc(Loc) ||
3186 parseIntToken(FunctionId, "unexpected token in '.cv_loc' directive") ||
3187 check(FunctionId < 0, Loc,
3188 "function id less than zero in '.cv_loc' directive") ||
3189 getTokenLoc(Loc) ||
3190 parseIntToken(FileNumber, "expected integer in '.cv_loc' directive") ||
3191 check(FileNumber < 1, Loc,
3192 "file number less than one in '.cv_loc' directive") ||
3193 check(!getContext().isValidCVFileNumber(FileNumber), Loc,
3194 "unassigned file number in '.cv_loc' directive"))
3195 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003196
3197 int64_t LineNumber = 0;
3198 if (getLexer().is(AsmToken::Integer)) {
3199 LineNumber = getTok().getIntVal();
3200 if (LineNumber < 0)
3201 return TokError("line number less than zero in '.cv_loc' directive");
3202 Lex();
3203 }
3204
3205 int64_t ColumnPos = 0;
3206 if (getLexer().is(AsmToken::Integer)) {
3207 ColumnPos = getTok().getIntVal();
3208 if (ColumnPos < 0)
3209 return TokError("column position less than zero in '.cv_loc' directive");
3210 Lex();
3211 }
3212
3213 bool PrologueEnd = false;
3214 uint64_t IsStmt = 0;
3215 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3216 StringRef Name;
3217 SMLoc Loc = getTok().getLoc();
3218 if (parseIdentifier(Name))
3219 return TokError("unexpected token in '.cv_loc' directive");
3220
3221 if (Name == "prologue_end")
3222 PrologueEnd = true;
3223 else if (Name == "is_stmt") {
3224 Loc = getTok().getLoc();
3225 const MCExpr *Value;
3226 if (parseExpression(Value))
3227 return true;
3228 // The expression must be the constant 0 or 1.
3229 IsStmt = ~0ULL;
3230 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3231 IsStmt = MCE->getValue();
3232
3233 if (IsStmt > 1)
3234 return Error(Loc, "is_stmt value not 0 or 1");
3235 } else {
3236 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3237 }
3238 }
Nirav Davea645433c2016-07-18 15:24:03 +00003239 Lex();
Reid Kleckner2214ed82016-01-29 00:49:42 +00003240
3241 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
3242 ColumnPos, PrologueEnd, IsStmt, StringRef());
3243 return false;
3244}
3245
3246/// parseDirectiveCVLinetable
3247/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3248bool AsmParser::parseDirectiveCVLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003249 int64_t FunctionId;
3250 StringRef FnStartName, FnEndName;
3251 SMLoc Loc = getTok().getLoc();
3252 if (parseIntToken(FunctionId,
3253 "expected Integer in '.cv_linetable' directive") ||
3254 check(FunctionId < 0, Loc,
3255 "function id less than zero in '.cv_linetable' directive") ||
3256 parseToken(AsmToken::Comma,
3257 "unexpected token in '.cv_linetable' directive") ||
3258 getTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3259 "expected identifier in directive") ||
3260 parseToken(AsmToken::Comma,
3261 "unexpected token in '.cv_linetable' directive") ||
3262 getTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3263 "expected identifier in directive"))
3264 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003265
3266 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3267 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3268
3269 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3270 return false;
3271}
3272
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003273/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003274/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003275/// ("contains" SecondaryFunctionId+)?
3276bool AsmParser::parseDirectiveCVInlineLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003277 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3278 StringRef FnStartName, FnEndName;
3279 SMLoc Loc = getTok().getLoc();
3280 if (parseIntToken(
3281 PrimaryFunctionId,
3282 "expected PrimaryFunctionId in '.cv_inline_linetable' directive") ||
3283 check(PrimaryFunctionId < 0, Loc,
3284 "function id less than zero in '.cv_inline_linetable' directive") ||
3285 getTokenLoc(Loc) ||
3286 parseIntToken(
3287 SourceFileId,
3288 "expected SourceField in '.cv_inline_linetable' directive") ||
3289 check(SourceFileId <= 0, Loc,
3290 "File id less than zero in '.cv_inline_linetable' directive") ||
3291 getTokenLoc(Loc) ||
3292 parseIntToken(
3293 SourceLineNum,
3294 "expected SourceLineNum in '.cv_inline_linetable' directive") ||
3295 check(SourceLineNum < 0, Loc,
3296 "Line number less than zero in '.cv_inline_linetable' directive") ||
3297 getTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3298 "expected identifier in directive") ||
3299 getTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3300 "expected identifier in directive"))
3301 return true;
David Majnemerc9911f22016-02-02 19:22:34 +00003302
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003303 SmallVector<unsigned, 8> SecondaryFunctionIds;
3304 if (getLexer().is(AsmToken::Identifier)) {
3305 if (getTok().getIdentifier() != "contains")
3306 return TokError(
3307 "unexpected identifier in '.cv_inline_linetable' directive");
3308 Lex();
3309
3310 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3311 int64_t SecondaryFunctionId = getTok().getIntVal();
3312 if (SecondaryFunctionId < 0)
3313 return TokError(
3314 "function id less than zero in '.cv_inline_linetable' directive");
3315 Lex();
3316
3317 SecondaryFunctionIds.push_back(SecondaryFunctionId);
3318 }
3319 }
3320
Nirav Davea645433c2016-07-18 15:24:03 +00003321 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
3322 return true;
3323
3324 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3325 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003326 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3327 SourceLineNum, FnStartSym,
David Majnemerc9911f22016-02-02 19:22:34 +00003328 FnEndSym, SecondaryFunctionIds);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003329 return false;
3330}
3331
David Majnemer408b5e62016-02-05 01:55:49 +00003332/// parseDirectiveCVDefRange
3333/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3334bool AsmParser::parseDirectiveCVDefRange() {
3335 SMLoc Loc;
3336 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3337 while (getLexer().is(AsmToken::Identifier)) {
3338 Loc = getLexer().getLoc();
3339 StringRef GapStartName;
3340 if (parseIdentifier(GapStartName))
3341 return Error(Loc, "expected identifier in directive");
3342 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3343
3344 Loc = getLexer().getLoc();
3345 StringRef GapEndName;
3346 if (parseIdentifier(GapEndName))
3347 return Error(Loc, "expected identifier in directive");
3348 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3349
3350 Ranges.push_back({GapStartSym, GapEndSym});
3351 }
3352
David Majnemer408b5e62016-02-05 01:55:49 +00003353 std::string FixedSizePortion;
Nirav Davea645433c2016-07-18 15:24:03 +00003354 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
3355 parseEscapedString(FixedSizePortion))
David Majnemer408b5e62016-02-05 01:55:49 +00003356 return true;
David Majnemer408b5e62016-02-05 01:55:49 +00003357
3358 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3359 return false;
3360}
3361
Reid Kleckner2214ed82016-01-29 00:49:42 +00003362/// parseDirectiveCVStringTable
3363/// ::= .cv_stringtable
3364bool AsmParser::parseDirectiveCVStringTable() {
3365 getStreamer().EmitCVStringTableDirective();
3366 return false;
3367}
3368
3369/// parseDirectiveCVFileChecksums
3370/// ::= .cv_filechecksums
3371bool AsmParser::parseDirectiveCVFileChecksums() {
3372 getStreamer().EmitCVFileChecksumsDirective();
3373 return false;
3374}
3375
Jim Grosbach4b905842013-09-20 23:08:21 +00003376/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003377/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003378bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003379 StringRef Name;
3380 bool EH = false;
3381 bool Debug = false;
3382
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003383 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003384 return TokError("Expected an identifier");
3385
3386 if (Name == ".eh_frame")
3387 EH = true;
3388 else if (Name == ".debug_frame")
3389 Debug = true;
3390
3391 if (getLexer().is(AsmToken::Comma)) {
3392 Lex();
3393
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003394 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003395 return TokError("Expected an identifier");
3396
3397 if (Name == ".eh_frame")
3398 EH = true;
3399 else if (Name == ".debug_frame")
3400 Debug = true;
3401 }
3402
3403 getStreamer().EmitCFISections(EH, Debug);
3404 return false;
3405}
3406
Jim Grosbach4b905842013-09-20 23:08:21 +00003407/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003408/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003409bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003410 StringRef Simple;
3411 if (getLexer().isNot(AsmToken::EndOfStatement))
3412 if (parseIdentifier(Simple) || Simple != "simple")
3413 return TokError("unexpected token in .cfi_startproc directive");
3414
Nirav Davea645433c2016-07-18 15:24:03 +00003415 if (parseToken(AsmToken::EndOfStatement, "Expected end of statement"))
3416 return true;
3417
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003418 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003419 return false;
3420}
3421
Jim Grosbach4b905842013-09-20 23:08:21 +00003422/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003423/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003424bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003425 getStreamer().EmitCFIEndProc();
3426 return false;
3427}
3428
Jim Grosbach4b905842013-09-20 23:08:21 +00003429/// \brief parse register name or number.
3430bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003431 SMLoc DirectiveLoc) {
3432 unsigned RegNo;
3433
3434 if (getLexer().isNot(AsmToken::Integer)) {
3435 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3436 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003437 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003438 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003439 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003440
3441 return false;
3442}
3443
Jim Grosbach4b905842013-09-20 23:08:21 +00003444/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003445/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003446bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003447 int64_t Register = 0, Offset = 0;
3448 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3449 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3450 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003451 return true;
3452
3453 getStreamer().EmitCFIDefCfa(Register, Offset);
3454 return false;
3455}
3456
Jim Grosbach4b905842013-09-20 23:08:21 +00003457/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003458/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003459bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003460 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003461 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003462 return true;
3463
3464 getStreamer().EmitCFIDefCfaOffset(Offset);
3465 return false;
3466}
3467
Jim Grosbach4b905842013-09-20 23:08:21 +00003468/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003469/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003470bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003471 int64_t Register1 = 0, Register2 = 0;
3472 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
3473 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3474 parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003475 return true;
3476
3477 getStreamer().EmitCFIRegister(Register1, Register2);
3478 return false;
3479}
3480
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003481/// parseDirectiveCFIWindowSave
3482/// ::= .cfi_window_save
3483bool AsmParser::parseDirectiveCFIWindowSave() {
3484 getStreamer().EmitCFIWindowSave();
3485 return false;
3486}
3487
Jim Grosbach4b905842013-09-20 23:08:21 +00003488/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003489/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003490bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003491 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003492 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003493 return true;
3494
3495 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3496 return false;
3497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003500/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003502 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003503 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003504 return true;
3505
3506 getStreamer().EmitCFIDefCfaRegister(Register);
3507 return false;
3508}
3509
Jim Grosbach4b905842013-09-20 23:08:21 +00003510/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003511/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003512bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003513 int64_t Register = 0;
3514 int64_t Offset = 0;
3515
Nirav Davea645433c2016-07-18 15:24:03 +00003516 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3517 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3518 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003519 return true;
3520
3521 getStreamer().EmitCFIOffset(Register, Offset);
3522 return false;
3523}
3524
Jim Grosbach4b905842013-09-20 23:08:21 +00003525/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003526/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003528 int64_t Register = 0, Offset = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003529
Nirav Davea645433c2016-07-18 15:24:03 +00003530 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3531 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3532 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003533 return true;
3534
3535 getStreamer().EmitCFIRelOffset(Register, Offset);
3536 return false;
3537}
3538
3539static bool isValidEncoding(int64_t Encoding) {
3540 if (Encoding & ~0xff)
3541 return false;
3542
3543 if (Encoding == dwarf::DW_EH_PE_omit)
3544 return true;
3545
3546 const unsigned Format = Encoding & 0xf;
3547 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3548 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3549 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3550 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3551 return false;
3552
3553 const unsigned Application = Encoding & 0x70;
3554 if (Application != dwarf::DW_EH_PE_absptr &&
3555 Application != dwarf::DW_EH_PE_pcrel)
3556 return false;
3557
3558 return true;
3559}
3560
Jim Grosbach4b905842013-09-20 23:08:21 +00003561/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003562/// IsPersonality true for cfi_personality, false for cfi_lsda
3563/// ::= .cfi_personality encoding, [symbol_name]
3564/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003565bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003566 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003567 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003568 return true;
3569 if (Encoding == dwarf::DW_EH_PE_omit)
3570 return false;
3571
Eli Bendersky17233942013-01-15 22:59:42 +00003572 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00003573 if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
3574 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3575 check(parseIdentifier(Name), "expected identifier in directive"))
3576 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003577
Jim Grosbach6f482002015-05-18 18:43:14 +00003578 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003579
3580 if (IsPersonality)
3581 getStreamer().EmitCFIPersonality(Sym, Encoding);
3582 else
3583 getStreamer().EmitCFILsda(Sym, Encoding);
3584 return false;
3585}
3586
Jim Grosbach4b905842013-09-20 23:08:21 +00003587/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003588/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003589bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003590 getStreamer().EmitCFIRememberState();
3591 return false;
3592}
3593
Jim Grosbach4b905842013-09-20 23:08:21 +00003594/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003595/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003596bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003597 getStreamer().EmitCFIRestoreState();
3598 return false;
3599}
3600
Jim Grosbach4b905842013-09-20 23:08:21 +00003601/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003602/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003603bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003604 int64_t Register = 0;
3605
Jim Grosbach4b905842013-09-20 23:08:21 +00003606 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003607 return true;
3608
3609 getStreamer().EmitCFISameValue(Register);
3610 return false;
3611}
3612
Jim Grosbach4b905842013-09-20 23:08:21 +00003613/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003614/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003615bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003616 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003617 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003618 return true;
3619
3620 getStreamer().EmitCFIRestore(Register);
3621 return false;
3622}
3623
Jim Grosbach4b905842013-09-20 23:08:21 +00003624/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003625/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003626bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003627 std::string Values;
3628 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003629 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003630 return true;
3631
3632 Values.push_back((uint8_t)CurrValue);
3633
3634 while (getLexer().is(AsmToken::Comma)) {
3635 Lex();
3636
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003637 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003638 return true;
3639
3640 Values.push_back((uint8_t)CurrValue);
3641 }
3642
3643 getStreamer().EmitCFIEscape(Values);
3644 return false;
3645}
3646
Jim Grosbach4b905842013-09-20 23:08:21 +00003647/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003648/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003649bool AsmParser::parseDirectiveCFISignalFrame() {
Nirav Davea645433c2016-07-18 15:24:03 +00003650 if (parseToken(AsmToken::EndOfStatement,
3651 "unexpected token in '.cfi_signal_frame'"))
3652 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003653
3654 getStreamer().EmitCFISignalFrame();
3655 return false;
3656}
3657
Jim Grosbach4b905842013-09-20 23:08:21 +00003658/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003659/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003660bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003661 int64_t Register = 0;
3662
Jim Grosbach4b905842013-09-20 23:08:21 +00003663 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003664 return true;
3665
3666 getStreamer().EmitCFIUndefined(Register);
3667 return false;
3668}
3669
Jim Grosbach4b905842013-09-20 23:08:21 +00003670/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003671/// ::= .macros_on
3672/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003673bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00003674 if (parseToken(AsmToken::EndOfStatement,
3675 "unexpected token in '" + Directive + "' directive"))
3676 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003677
Jim Grosbach4b905842013-09-20 23:08:21 +00003678 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003679 return false;
3680}
3681
Jim Grosbach4b905842013-09-20 23:08:21 +00003682/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003683/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003684bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003685 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003686 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003687 return TokError("expected identifier in '.macro' directive");
3688
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003689 if (getLexer().is(AsmToken::Comma))
3690 Lex();
3691
Eli Bendersky17233942013-01-15 22:59:42 +00003692 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003693 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003694
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003695 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003696 return Error(Lexer.getLoc(),
3697 "Vararg parameter '" + Parameters.back().Name +
3698 "' should be last one in the list of parameters.");
3699
David Majnemer91fc4c22014-01-29 18:57:46 +00003700 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003701 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003702 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003703
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003704 if (Lexer.is(AsmToken::Colon)) {
3705 Lex(); // consume ':'
3706
3707 SMLoc QualLoc;
3708 StringRef Qualifier;
3709
3710 QualLoc = Lexer.getLoc();
3711 if (parseIdentifier(Qualifier))
3712 return Error(QualLoc, "missing parameter qualifier for "
3713 "'" + Parameter.Name + "' in macro '" + Name + "'");
3714
3715 if (Qualifier == "req")
3716 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003717 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003718 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003719 else
3720 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3721 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3722 }
3723
David Majnemer91fc4c22014-01-29 18:57:46 +00003724 if (getLexer().is(AsmToken::Equal)) {
3725 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003726
3727 SMLoc ParamLoc;
3728
3729 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003730 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003731 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003732
3733 if (Parameter.Required)
3734 Warning(ParamLoc, "pointless default value for required parameter "
3735 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003736 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003737
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003738 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003739
3740 if (getLexer().is(AsmToken::Comma))
3741 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003742 }
3743
Nirav Dave1180e6892016-06-02 17:15:05 +00003744 // Eat just the end of statement.
3745 Lexer.Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003746
Nirav Dave1180e6892016-06-02 17:15:05 +00003747 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
Eli Bendersky17233942013-01-15 22:59:42 +00003748 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003749 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003750 // Lex the macro definition.
3751 for (;;) {
Nirav Dave1180e6892016-06-02 17:15:05 +00003752 // Ignore Lexing errors in macros.
3753 while (Lexer.is(AsmToken::Error)) {
3754 Lexer.Lex();
3755 }
3756
Eli Bendersky17233942013-01-15 22:59:42 +00003757 // Check whether we have reached the end of the file.
3758 if (getLexer().is(AsmToken::Eof))
3759 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3760
3761 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003762 if (getLexer().is(AsmToken::Identifier)) {
3763 if (getTok().getIdentifier() == ".endm" ||
3764 getTok().getIdentifier() == ".endmacro") {
3765 if (MacroDepth == 0) { // Outermost macro.
3766 EndToken = getTok();
Nirav Dave1180e6892016-06-02 17:15:05 +00003767 Lexer.Lex();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003768 if (getLexer().isNot(AsmToken::EndOfStatement))
3769 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3770 "' directive");
3771 break;
3772 } else {
3773 // Otherwise we just found the end of an inner macro.
3774 --MacroDepth;
3775 }
3776 } else if (getTok().getIdentifier() == ".macro") {
3777 // We allow nested macros. Those aren't instantiated until the outermost
3778 // macro is expanded so just ignore them for now.
3779 ++MacroDepth;
3780 }
Eli Bendersky17233942013-01-15 22:59:42 +00003781 }
3782
3783 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003784 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003785 }
3786
Jim Grosbach4b905842013-09-20 23:08:21 +00003787 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003788 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3789 }
3790
3791 const char *BodyStart = StartToken.getLoc().getPointer();
3792 const char *BodyEnd = EndToken.getLoc().getPointer();
3793 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003794 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003795 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003796 return false;
3797}
3798
Jim Grosbach4b905842013-09-20 23:08:21 +00003799/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003800///
3801/// With the support added for named parameters there may be code out there that
3802/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003803/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003804/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003805/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003806/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3807/// warning that the positional parameter found in body which have no effect.
3808/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003809/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003810/// intended or change the macro to use the named parameters. It is possible
3811/// this warning will trigger when the none of the named parameters are used
3812/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003813void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003814 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003815 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003816 // If this macro is not defined with named parameters the warning we are
3817 // checking for here doesn't apply.
3818 unsigned NParameters = Parameters.size();
3819 if (NParameters == 0)
3820 return;
3821
3822 bool NamedParametersFound = false;
3823 bool PositionalParametersFound = false;
3824
3825 // Look at the body of the macro for use of both the named parameters and what
3826 // are likely to be positional parameters. This is what expandMacro() is
3827 // doing when it finds the parameters in the body.
3828 while (!Body.empty()) {
3829 // Scan for the next possible parameter.
3830 std::size_t End = Body.size(), Pos = 0;
3831 for (; Pos != End; ++Pos) {
3832 // Check for a substitution or escape.
3833 // This macro is defined with parameters, look for \foo, \bar, etc.
3834 if (Body[Pos] == '\\' && Pos + 1 != End)
3835 break;
3836
3837 // This macro should have parameters, but look for $0, $1, ..., $n too.
3838 if (Body[Pos] != '$' || Pos + 1 == End)
3839 continue;
3840 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003841 if (Next == '$' || Next == 'n' ||
3842 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003843 break;
3844 }
3845
3846 // Check if we reached the end.
3847 if (Pos == End)
3848 break;
3849
3850 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003851 switch (Body[Pos + 1]) {
3852 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003853 case '$':
3854 break;
3855
Jim Grosbach4b905842013-09-20 23:08:21 +00003856 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003857 case 'n':
3858 PositionalParametersFound = true;
3859 break;
3860
Jim Grosbach4b905842013-09-20 23:08:21 +00003861 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003862 default: {
3863 PositionalParametersFound = true;
3864 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003865 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003866 }
3867 Pos += 2;
3868 } else {
3869 unsigned I = Pos + 1;
3870 while (isIdentifierChar(Body[I]) && I + 1 != End)
3871 ++I;
3872
Jim Grosbach4b905842013-09-20 23:08:21 +00003873 const char *Begin = Body.data() + Pos + 1;
3874 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003875 unsigned Index = 0;
3876 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003877 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003878 break;
3879
3880 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003881 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3882 Pos += 3;
3883 else {
3884 Pos = I;
3885 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003886 } else {
3887 NamedParametersFound = true;
3888 Pos += 1 + Argument.size();
3889 }
3890 }
3891 // Update the scan point.
3892 Body = Body.substr(Pos);
3893 }
3894
3895 if (!NamedParametersFound && PositionalParametersFound)
3896 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3897 "used in macro body, possible positional parameter "
3898 "found in body which will have no effect");
3899}
3900
Nico Weber155dccd12014-07-24 17:08:39 +00003901/// parseDirectiveExitMacro
3902/// ::= .exitm
3903bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00003904 if (parseToken(AsmToken::EndOfStatement,
3905 "unexpected token in '" + Directive + "' directive"))
3906 return true;
Nico Weber155dccd12014-07-24 17:08:39 +00003907
3908 if (!isInsideMacroInstantiation())
3909 return TokError("unexpected '" + Directive + "' in file, "
3910 "no current macro definition");
3911
3912 // Exit all conditionals that are active in the current macro.
3913 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3914 TheCondState = TheCondStack.back();
3915 TheCondStack.pop_back();
3916 }
3917
3918 handleMacroExit();
3919 return false;
3920}
3921
Jim Grosbach4b905842013-09-20 23:08:21 +00003922/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003923/// ::= .endm
3924/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003925bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003926 if (getLexer().isNot(AsmToken::EndOfStatement))
3927 return TokError("unexpected token in '" + Directive + "' directive");
3928
3929 // If we are inside a macro instantiation, terminate the current
3930 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003931 if (isInsideMacroInstantiation()) {
3932 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003933 return false;
3934 }
3935
3936 // Otherwise, this .endmacro is a stray entry in the file; well formed
3937 // .endmacro directives are handled during the macro definition parsing.
3938 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003939 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003940}
3941
Jim Grosbach4b905842013-09-20 23:08:21 +00003942/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003943/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003944bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003945 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00003946 SMLoc Loc;
3947 if (getTokenLoc(Loc) || check(parseIdentifier(Name), Loc,
3948 "expected identifier in '.purgem' directive") ||
3949 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00003950 "unexpected token in '.purgem' directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00003951 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003952
Nirav Dave1ab71992016-07-18 19:35:21 +00003953 if (!lookupMacro(Name))
3954 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3955
Jim Grosbach4b905842013-09-20 23:08:21 +00003956 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003957 return false;
3958}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003959
Jim Grosbach4b905842013-09-20 23:08:21 +00003960/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003961/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003962bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003963 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003964
3965 // Expect a single argument: an expression that evaluates to a constant
3966 // in the inclusive range 0-30.
3967 SMLoc ExprLoc = getLexer().getLoc();
3968 int64_t AlignSizePow2;
Nirav Davea645433c2016-07-18 15:24:03 +00003969 if (parseAbsoluteExpression(AlignSizePow2) ||
3970 parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
3971 "in '.bundle_align_mode' "
3972 "directive") ||
3973 check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
3974 "invalid bundle alignment size (expected between 0 and 30)"))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003975 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003976
3977 // Because of AlignSizePow2's verified range we can safely truncate it to
3978 // unsigned.
3979 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3980 return false;
3981}
3982
Jim Grosbach4b905842013-09-20 23:08:21 +00003983/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003984/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003985bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003986 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003987 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003988
Eli Bendersky802b6282013-01-07 21:51:08 +00003989 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3990 StringRef Option;
3991 SMLoc Loc = getTok().getLoc();
3992 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003993 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003994
Nirav Davea645433c2016-07-18 15:24:03 +00003995 if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
3996 check(Option != "align_to_end", Loc, kInvalidOptionError) ||
3997 check(getTok().isNot(AsmToken::EndOfStatement), Loc,
3998 "unexpected token after '.bundle_lock' directive option"))
3999 return true;
Eli Bendersky802b6282013-01-07 21:51:08 +00004000 AlignToEnd = true;
4001 }
4002
Eli Benderskyf483ff92012-12-20 19:05:53 +00004003 Lex();
4004
Eli Bendersky802b6282013-01-07 21:51:08 +00004005 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004006 return false;
4007}
4008
Jim Grosbach4b905842013-09-20 23:08:21 +00004009/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004010/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004011bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004012 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004013
Nirav Davea645433c2016-07-18 15:24:03 +00004014 if (parseToken(AsmToken::EndOfStatement,
4015 "unexpected token in '.bundle_unlock' directive"))
4016 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004017
4018 getStreamer().EmitBundleUnlock();
4019 return false;
4020}
4021
Jim Grosbach4b905842013-09-20 23:08:21 +00004022/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004023/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004024bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004025 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004026
Petr Hosek67a94a72016-05-28 05:57:48 +00004027 SMLoc NumBytesLoc = Lexer.getLoc();
4028 const MCExpr *NumBytes;
4029 if (parseExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004030 return true;
4031
4032 int64_t FillExpr = 0;
4033 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eli Bendersky17233942013-01-15 22:59:42 +00004034
Nirav Davea645433c2016-07-18 15:24:03 +00004035 if (parseToken(AsmToken::Comma,
4036 "unexpected token in '" + Twine(IDVal) + "' directive") ||
4037 parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004038 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004039 }
4040
Nirav Davea645433c2016-07-18 15:24:03 +00004041 if (parseToken(AsmToken::EndOfStatement,
4042 "unexpected token in '" + Twine(IDVal) + "' directive"))
4043 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004044
Eli Bendersky17233942013-01-15 22:59:42 +00004045 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Petr Hosek67a94a72016-05-28 05:57:48 +00004046 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
Eli Bendersky17233942013-01-15 22:59:42 +00004047
4048 return false;
4049}
4050
Jim Grosbach4b905842013-09-20 23:08:21 +00004051/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004052/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004053bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004054 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004055 const MCExpr *Value;
4056
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004057 for (;;) {
4058 if (parseExpression(Value))
4059 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004060
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004061 if (Signed)
4062 getStreamer().EmitSLEB128Value(Value);
4063 else
4064 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004065
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004066 if (getLexer().is(AsmToken::EndOfStatement))
4067 break;
4068
Nirav Davea645433c2016-07-18 15:24:03 +00004069 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4070 return true;
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004071 }
Nirav Davea645433c2016-07-18 15:24:03 +00004072 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00004073
4074 return false;
4075}
4076
Jim Grosbach4b905842013-09-20 23:08:21 +00004077/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004078/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004079bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004080 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00004081 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004082 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004083 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004084
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004085 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004086 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004087
Jim Grosbach6f482002015-05-18 18:43:14 +00004088 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004089
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004090 // Assembler local symbols don't make any sense here. Complain loudly.
4091 if (Sym->isTemporary())
4092 return Error(Loc, "non-local symbol required in directive");
4093
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004094 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4095 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004096
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004097 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004098 break;
4099
Nirav Davea645433c2016-07-18 15:24:03 +00004100 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4101 return true;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004102 }
4103 }
4104
Sean Callanan686ed8d2010-01-19 20:22:31 +00004105 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004106 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004107}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004108
Jim Grosbach4b905842013-09-20 23:08:21 +00004109/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004110/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004111bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004112 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004113
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004114 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004115 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004116 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004117 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004118
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004119 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004120 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004121
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004122 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004123 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004124 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004125
4126 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004127 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004128 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004129 return true;
4130
4131 int64_t Pow2Alignment = 0;
4132 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004133 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004134 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004135 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004136 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004137 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004138
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004139 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4140 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004141 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4142
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004143 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004144 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4145 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004146 if (!isPowerOf2_64(Pow2Alignment))
4147 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4148 Pow2Alignment = Log2_64(Pow2Alignment);
4149 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004150 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004151
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004152 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004153 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004154
Sean Callanan686ed8d2010-01-19 20:22:31 +00004155 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004156
Chris Lattner28ad7542009-07-09 17:25:12 +00004157 // NOTE: a size of zero for a .comm should create a undefined symbol
4158 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004159 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004160 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004161 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004162
Eric Christopherbc818852010-05-14 01:38:54 +00004163 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004164 // may internally end up wanting an alignment in bytes.
4165 // FIXME: Diagnose overflow.
4166 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004167 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004168 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004169
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004170 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004171 return Error(IDLoc, "invalid symbol redefinition");
4172
Chris Lattner28ad7542009-07-09 17:25:12 +00004173 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004174 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004175 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004176 return false;
4177 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004178
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004179 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004180 return false;
4181}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004182
Jim Grosbach4b905842013-09-20 23:08:21 +00004183/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004184/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004185bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004186 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004187 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004188
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004189 StringRef Str = parseStringToEndOfStatement();
Nirav Davea645433c2016-07-18 15:24:03 +00004190 if (parseToken(AsmToken::EndOfStatement,
4191 "unexpected token in '.abort' directive"))
4192 return true;
Kevin Enderby56523ce2009-07-13 23:15:14 +00004193
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004194 if (Str.empty())
4195 Error(Loc, ".abort detected. Assembly stopping.");
4196 else
4197 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004198 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004199
4200 return false;
4201}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004202
Jim Grosbach4b905842013-09-20 23:08:21 +00004203/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004204/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004205bool AsmParser::parseDirectiveInclude() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004206 // Allow the strings to have escaped octal character sequence.
4207 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004208 SMLoc IncludeLoc = getTok().getLoc();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004209
Nirav Davea645433c2016-07-18 15:24:03 +00004210 if (check(getTok().isNot(AsmToken::String),
4211 "expected string in '.include' directive") ||
4212 parseEscapedString(Filename) ||
4213 check(getTok().isNot(AsmToken::EndOfStatement),
4214 "unexpected token in '.include' directive") ||
4215 // Attempt to switch the lexer to the included file before consuming the
4216 // end of statement to avoid losing it when we switch.
4217 check(enterIncludeFile(Filename), IncludeLoc,
4218 "Could not find include file '" + Filename + "'"))
Chris Lattner693fbb82009-07-16 06:14:39 +00004219 return true;
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004220
4221 return false;
4222}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004223
Jim Grosbach4b905842013-09-20 23:08:21 +00004224/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004225/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004226bool AsmParser::parseDirectiveIncbin() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004227 // Allow the strings to have escaped octal character sequence.
4228 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004229 SMLoc IncbinLoc = getTok().getLoc();
4230 if (check(getTok().isNot(AsmToken::String),
4231 "expected string in '.incbin' directive") ||
4232 parseEscapedString(Filename) ||
4233 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00004234 "unexpected token in '.incbin' directive"))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004235 return true;
Nirav Dave1ab71992016-07-18 19:35:21 +00004236
4237 // Attempt to process the included file.
4238 if (processIncbinFile(Filename))
4239 return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
Kevin Enderby109f25c2011-12-14 21:47:48 +00004240 return false;
4241}
4242
Jim Grosbach4b905842013-09-20 23:08:21 +00004243/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004244/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4245bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004246 TheCondStack.push_back(TheCondState);
4247 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004248 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004249 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004250 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004251 int64_t ExprValue;
Nirav Davea645433c2016-07-18 15:24:03 +00004252 if (parseAbsoluteExpression(ExprValue) ||
4253 parseToken(AsmToken::EndOfStatement,
4254 "unexpected token in '.if' directive"))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004255 return true;
4256
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004257 switch (DirKind) {
4258 default:
4259 llvm_unreachable("unsupported directive");
4260 case DK_IF:
4261 case DK_IFNE:
4262 break;
4263 case DK_IFEQ:
4264 ExprValue = ExprValue == 0;
4265 break;
4266 case DK_IFGE:
4267 ExprValue = ExprValue >= 0;
4268 break;
4269 case DK_IFGT:
4270 ExprValue = ExprValue > 0;
4271 break;
4272 case DK_IFLE:
4273 ExprValue = ExprValue <= 0;
4274 break;
4275 case DK_IFLT:
4276 ExprValue = ExprValue < 0;
4277 break;
4278 }
4279
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004280 TheCondState.CondMet = ExprValue;
4281 TheCondState.Ignore = !TheCondState.CondMet;
4282 }
4283
4284 return false;
4285}
4286
Jim Grosbach4b905842013-09-20 23:08:21 +00004287/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004288/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004289bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004290 TheCondStack.push_back(TheCondState);
4291 TheCondState.TheCond = AsmCond::IfCond;
4292
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004293 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004294 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004295 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004296 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004297
Nirav Davea645433c2016-07-18 15:24:03 +00004298 if (parseToken(AsmToken::EndOfStatement,
4299 "unexpected token in '.ifb' directive"))
4300 return true;
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004301
4302 TheCondState.CondMet = ExpectBlank == Str.empty();
4303 TheCondState.Ignore = !TheCondState.CondMet;
4304 }
4305
4306 return false;
4307}
4308
Jim Grosbach4b905842013-09-20 23:08:21 +00004309/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004310/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004311/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004312bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004313 TheCondStack.push_back(TheCondState);
4314 TheCondState.TheCond = AsmCond::IfCond;
4315
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004316 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004317 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004318 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004319 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004320
Nirav Davea645433c2016-07-18 15:24:03 +00004321 if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
4322 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004323
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004324 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004325
Nirav Davea645433c2016-07-18 15:24:03 +00004326 if (parseToken(AsmToken::EndOfStatement,
4327 "unexpected token in '.ifc' directive"))
4328 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004329
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004330 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004331 TheCondState.Ignore = !TheCondState.CondMet;
4332 }
4333
4334 return false;
4335}
4336
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004337/// parseDirectiveIfeqs
4338/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004339bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004340 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004341 if (ExpectEqual)
4342 TokError("expected string parameter for '.ifeqs' directive");
4343 else
4344 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004345 eatToEndOfStatement();
4346 return true;
4347 }
4348
4349 StringRef String1 = getTok().getStringContents();
4350 Lex();
4351
4352 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004353 if (ExpectEqual)
4354 TokError("expected comma after first string for '.ifeqs' directive");
4355 else
4356 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004357 eatToEndOfStatement();
4358 return true;
4359 }
4360
4361 Lex();
4362
4363 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004364 if (ExpectEqual)
4365 TokError("expected string parameter for '.ifeqs' directive");
4366 else
4367 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004368 eatToEndOfStatement();
4369 return true;
4370 }
4371
4372 StringRef String2 = getTok().getStringContents();
4373 Lex();
4374
4375 TheCondStack.push_back(TheCondState);
4376 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004377 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004378 TheCondState.Ignore = !TheCondState.CondMet;
4379
4380 return false;
4381}
4382
Jim Grosbach4b905842013-09-20 23:08:21 +00004383/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004384/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004385bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004386 StringRef Name;
4387 TheCondStack.push_back(TheCondState);
4388 TheCondState.TheCond = AsmCond::IfCond;
4389
4390 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004391 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004392 } else {
Nirav Davea645433c2016-07-18 15:24:03 +00004393 if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
4394 parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
4395 return true;
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004396
Jim Grosbach6f482002015-05-18 18:43:14 +00004397 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004398
4399 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004400 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004401 else
Craig Topper353eda42014-04-24 06:44:33 +00004402 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004403 TheCondState.Ignore = !TheCondState.CondMet;
4404 }
4405
4406 return false;
4407}
4408
Jim Grosbach4b905842013-09-20 23:08:21 +00004409/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004410/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004411bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004412 if (TheCondState.TheCond != AsmCond::IfCond &&
4413 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004414 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4415 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004416 TheCondState.TheCond = AsmCond::ElseIfCond;
4417
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004418 bool LastIgnoreState = false;
4419 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004420 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004421 if (LastIgnoreState || TheCondState.CondMet) {
4422 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004423 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004424 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004425 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004426 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004427 return true;
4428
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004429 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004430 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004431
Sean Callanan686ed8d2010-01-19 20:22:31 +00004432 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004433 TheCondState.CondMet = ExprValue;
4434 TheCondState.Ignore = !TheCondState.CondMet;
4435 }
4436
4437 return false;
4438}
4439
Jim Grosbach4b905842013-09-20 23:08:21 +00004440/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004441/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004442bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004443 if (parseToken(AsmToken::EndOfStatement,
4444 "unexpected token in '.else' directive"))
4445 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004446
4447 if (TheCondState.TheCond != AsmCond::IfCond &&
4448 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004449 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4450 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004451 TheCondState.TheCond = AsmCond::ElseCond;
4452 bool LastIgnoreState = false;
4453 if (!TheCondStack.empty())
4454 LastIgnoreState = TheCondStack.back().Ignore;
4455 if (LastIgnoreState || TheCondState.CondMet)
4456 TheCondState.Ignore = true;
4457 else
4458 TheCondState.Ignore = false;
4459
4460 return false;
4461}
4462
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004463/// parseDirectiveEnd
4464/// ::= .end
4465bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004466 if (parseToken(AsmToken::EndOfStatement,
4467 "unexpected token in '.end' directive"))
4468 return true;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004469
4470 while (Lexer.isNot(AsmToken::Eof))
4471 Lex();
4472
4473 return false;
4474}
4475
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004476/// parseDirectiveError
4477/// ::= .err
4478/// ::= .error [string]
4479bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4480 if (!TheCondStack.empty()) {
4481 if (TheCondStack.back().Ignore) {
4482 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004483 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004484 }
4485 }
4486
4487 if (!WithMessage)
4488 return Error(L, ".err encountered");
4489
4490 StringRef Message = ".error directive invoked in source file";
4491 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4492 if (Lexer.isNot(AsmToken::String)) {
4493 TokError(".error argument must be a string");
4494 eatToEndOfStatement();
4495 return true;
4496 }
4497
4498 Message = getTok().getStringContents();
4499 Lex();
4500 }
4501
4502 Error(L, Message);
4503 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004504}
4505
Nico Weber404012b2014-07-24 16:26:06 +00004506/// parseDirectiveWarning
4507/// ::= .warning [string]
4508bool AsmParser::parseDirectiveWarning(SMLoc L) {
4509 if (!TheCondStack.empty()) {
4510 if (TheCondStack.back().Ignore) {
4511 eatToEndOfStatement();
4512 return false;
4513 }
4514 }
4515
4516 StringRef Message = ".warning directive invoked in source file";
4517 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4518 if (Lexer.isNot(AsmToken::String)) {
4519 TokError(".warning argument must be a string");
4520 eatToEndOfStatement();
4521 return true;
4522 }
4523
4524 Message = getTok().getStringContents();
4525 Lex();
4526 }
4527
4528 Warning(L, Message);
4529 return false;
4530}
4531
Jim Grosbach4b905842013-09-20 23:08:21 +00004532/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004533/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004534bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004535 if (parseToken(AsmToken::EndOfStatement,
4536 "unexpected token in '.endif' directive"))
4537 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004538
Jim Grosbach4b905842013-09-20 23:08:21 +00004539 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004540 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4541 ".else");
4542 if (!TheCondStack.empty()) {
4543 TheCondState = TheCondStack.back();
4544 TheCondStack.pop_back();
4545 }
4546
4547 return false;
4548}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004549
Eli Bendersky17233942013-01-15 22:59:42 +00004550void AsmParser::initializeDirectiveKindMap() {
4551 DirectiveKindMap[".set"] = DK_SET;
4552 DirectiveKindMap[".equ"] = DK_EQU;
4553 DirectiveKindMap[".equiv"] = DK_EQUIV;
4554 DirectiveKindMap[".ascii"] = DK_ASCII;
4555 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4556 DirectiveKindMap[".string"] = DK_STRING;
4557 DirectiveKindMap[".byte"] = DK_BYTE;
4558 DirectiveKindMap[".short"] = DK_SHORT;
4559 DirectiveKindMap[".value"] = DK_VALUE;
4560 DirectiveKindMap[".2byte"] = DK_2BYTE;
4561 DirectiveKindMap[".long"] = DK_LONG;
4562 DirectiveKindMap[".int"] = DK_INT;
4563 DirectiveKindMap[".4byte"] = DK_4BYTE;
4564 DirectiveKindMap[".quad"] = DK_QUAD;
4565 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004566 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004567 DirectiveKindMap[".single"] = DK_SINGLE;
4568 DirectiveKindMap[".float"] = DK_FLOAT;
4569 DirectiveKindMap[".double"] = DK_DOUBLE;
4570 DirectiveKindMap[".align"] = DK_ALIGN;
4571 DirectiveKindMap[".align32"] = DK_ALIGN32;
4572 DirectiveKindMap[".balign"] = DK_BALIGN;
4573 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4574 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4575 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4576 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4577 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4578 DirectiveKindMap[".org"] = DK_ORG;
4579 DirectiveKindMap[".fill"] = DK_FILL;
4580 DirectiveKindMap[".zero"] = DK_ZERO;
4581 DirectiveKindMap[".extern"] = DK_EXTERN;
4582 DirectiveKindMap[".globl"] = DK_GLOBL;
4583 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004584 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4585 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4586 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4587 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4588 DirectiveKindMap[".reference"] = DK_REFERENCE;
4589 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4590 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4591 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4592 DirectiveKindMap[".comm"] = DK_COMM;
4593 DirectiveKindMap[".common"] = DK_COMMON;
4594 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4595 DirectiveKindMap[".abort"] = DK_ABORT;
4596 DirectiveKindMap[".include"] = DK_INCLUDE;
4597 DirectiveKindMap[".incbin"] = DK_INCBIN;
4598 DirectiveKindMap[".code16"] = DK_CODE16;
4599 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4600 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004601 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004602 DirectiveKindMap[".irp"] = DK_IRP;
4603 DirectiveKindMap[".irpc"] = DK_IRPC;
4604 DirectiveKindMap[".endr"] = DK_ENDR;
4605 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4606 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4607 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4608 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004609 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4610 DirectiveKindMap[".ifge"] = DK_IFGE;
4611 DirectiveKindMap[".ifgt"] = DK_IFGT;
4612 DirectiveKindMap[".ifle"] = DK_IFLE;
4613 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004614 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004615 DirectiveKindMap[".ifb"] = DK_IFB;
4616 DirectiveKindMap[".ifnb"] = DK_IFNB;
4617 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004618 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004619 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004620 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004621 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4622 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4623 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4624 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4625 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004626 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004627 DirectiveKindMap[".endif"] = DK_ENDIF;
4628 DirectiveKindMap[".skip"] = DK_SKIP;
4629 DirectiveKindMap[".space"] = DK_SPACE;
4630 DirectiveKindMap[".file"] = DK_FILE;
4631 DirectiveKindMap[".line"] = DK_LINE;
4632 DirectiveKindMap[".loc"] = DK_LOC;
4633 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004634 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
4635 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4636 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004637 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
David Majnemer408b5e62016-02-05 01:55:49 +00004638 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004639 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4640 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004641 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4642 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4643 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4644 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4645 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4646 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4647 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4648 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4649 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4650 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4651 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4652 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4653 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4654 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4655 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4656 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4657 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4658 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4659 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4660 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4661 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004662 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004663 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4664 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4665 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004666 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004667 DirectiveKindMap[".endm"] = DK_ENDM;
4668 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4669 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004670 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004671 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004672 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004673 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004674}
4675
Jim Grosbach4b905842013-09-20 23:08:21 +00004676MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004677 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004678
Rafael Espindola34b9c512012-06-03 23:57:14 +00004679 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004680 for (;;) {
4681 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004682 if (getLexer().is(AsmToken::Eof)) {
4683 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004684 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004685 }
4686
Rafael Espindola34b9c512012-06-03 23:57:14 +00004687 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004688 (getTok().getIdentifier() == ".rept" ||
4689 getTok().getIdentifier() == ".irp" ||
4690 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004691 ++NestLevel;
4692 }
4693
4694 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004695 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004696 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004697 EndToken = getTok();
4698 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004699 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4700 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004701 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004702 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004703 break;
4704 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004705 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004706 }
4707
Rafael Espindola34b9c512012-06-03 23:57:14 +00004708 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004709 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004710 }
4711
4712 const char *BodyStart = StartToken.getLoc().getPointer();
4713 const char *BodyEnd = EndToken.getLoc().getPointer();
4714 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4715
Rafael Espindola34b9c512012-06-03 23:57:14 +00004716 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004717 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004718 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004719}
4720
Jim Grosbach4b905842013-09-20 23:08:21 +00004721void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004722 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004723 OS << ".endr\n";
4724
Rafael Espindola3560ff22014-08-27 20:03:13 +00004725 std::unique_ptr<MemoryBuffer> Instantiation =
4726 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004727
Rafael Espindola34b9c512012-06-03 23:57:14 +00004728 // Create the macro instantiation object and add to the current macro
4729 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004730 MacroInstantiation *MI = new MacroInstantiation(
4731 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004732 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004733
Rafael Espindola34b9c512012-06-03 23:57:14 +00004734 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004735 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004736 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004737 Lex();
4738}
4739
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004740/// parseDirectiveRept
4741/// ::= .rep | .rept count
4742bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004743 const MCExpr *CountExpr;
4744 SMLoc CountLoc = getTok().getLoc();
4745 if (parseExpression(CountExpr))
4746 return true;
4747
Rafael Espindola34b9c512012-06-03 23:57:14 +00004748 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004749 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004750 eatToEndOfStatement();
4751 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4752 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004753
Nirav Davea645433c2016-07-18 15:24:03 +00004754 if (check(Count < 0, CountLoc, "Count is negative") ||
4755 parseToken(AsmToken::EndOfStatement,
4756 "unexpected token in '" + Dir + "' directive"))
4757 return true;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004758
4759 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004760 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004761 if (!M)
4762 return true;
4763
4764 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4765 // to hold the macro body with substitutions.
4766 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004767 raw_svector_ostream OS(Buf);
4768 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004769 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4770 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004771 return true;
4772 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004773 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004774
4775 return false;
4776}
4777
Jim Grosbach4b905842013-09-20 23:08:21 +00004778/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004779/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004780bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004781 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00004782 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00004783 if (check(parseIdentifier(Parameter.Name),
4784 "expected identifier in '.irp' directive") ||
4785 parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
4786 parseMacroArguments(nullptr, A) ||
4787 parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004788 return true;
4789
Rafael Espindola768b41c2012-06-15 14:02:34 +00004790 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004791 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004792 if (!M)
4793 return true;
4794
4795 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4796 // to hold the macro body with substitutions.
4797 SmallString<256> Buf;
4798 raw_svector_ostream OS(Buf);
4799
Craig Topper84008482015-10-10 05:38:14 +00004800 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004801 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4802 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004803 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004804 return true;
4805 }
4806
Jim Grosbach4b905842013-09-20 23:08:21 +00004807 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004808
4809 return false;
4810}
4811
Jim Grosbach4b905842013-09-20 23:08:21 +00004812/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004813/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004814bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004815 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00004816 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00004817
4818 if (check(parseIdentifier(Parameter.Name),
4819 "expected identifier in '.irpc' directive") ||
4820 parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
4821 parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004822 return true;
4823
4824 if (A.size() != 1 || A.front().size() != 1)
4825 return TokError("unexpected token in '.irpc' directive");
4826
4827 // Eat the end of statement.
Nirav Davea645433c2016-07-18 15:24:03 +00004828 if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
4829 return true;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004830
4831 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004832 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004833 if (!M)
4834 return true;
4835
4836 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4837 // to hold the macro body with substitutions.
4838 SmallString<256> Buf;
4839 raw_svector_ostream OS(Buf);
4840
4841 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004842 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004843 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004844 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004845
Toma Tabacu217116e2015-04-27 10:50:29 +00004846 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4847 // This is undocumented, but GAS seems to support it.
4848 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004849 return true;
4850 }
4851
Jim Grosbach4b905842013-09-20 23:08:21 +00004852 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004853
4854 return false;
4855}
4856
Jim Grosbach4b905842013-09-20 23:08:21 +00004857bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004858 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004859 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004860
4861 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004862 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004863 assert(getLexer().is(AsmToken::EndOfStatement));
4864
Jim Grosbach4b905842013-09-20 23:08:21 +00004865 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004866 return false;
4867}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004868
Jim Grosbach4b905842013-09-20 23:08:21 +00004869bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004870 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004871 const MCExpr *Value;
4872 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004873 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004874 return true;
4875 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4876 if (!MCE)
4877 return Error(ExprLoc, "unexpected expression in _emit");
4878 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004879 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004880 return Error(ExprLoc, "literal value out of range for directive");
4881
Craig Topper7d5b2312015-10-10 05:25:02 +00004882 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004883 return false;
4884}
4885
Jim Grosbach4b905842013-09-20 23:08:21 +00004886bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004887 const MCExpr *Value;
4888 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004889 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004890 return true;
4891 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4892 if (!MCE)
4893 return Error(ExprLoc, "unexpected expression in align");
4894 uint64_t IntValue = MCE->getValue();
4895 if (!isPowerOf2_64(IntValue))
4896 return Error(ExprLoc, "literal value not a power of two greater then zero");
4897
Craig Topper7d5b2312015-10-10 05:25:02 +00004898 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004899 return false;
4900}
4901
Chad Rosierf43fcf52013-02-13 21:27:17 +00004902// We are comparing pointers, but the pointers are relative to a single string.
4903// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004904static int rewritesSort(const AsmRewrite *AsmRewriteA,
4905 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004906 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4907 return -1;
4908 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4909 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004910
Chad Rosierfce4fab2013-04-08 17:43:47 +00004911 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4912 // rewrite to the same location. Make sure the SizeDirective rewrite is
4913 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4914 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004915 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4916 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004917 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004918
Jim Grosbach4b905842013-09-20 23:08:21 +00004919 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4920 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004921 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004922 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004923}
4924
Jim Grosbach4b905842013-09-20 23:08:21 +00004925bool AsmParser::parseMSInlineAsm(
4926 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4927 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4928 SmallVectorImpl<std::string> &Constraints,
4929 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4930 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004931 SmallVector<void *, 4> InputDecls;
4932 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004933 SmallVector<bool, 4> InputDeclsAddressOf;
4934 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004935 SmallVector<std::string, 4> InputConstraints;
4936 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004937 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004938
Benjamin Kramer1a136112013-02-15 20:37:21 +00004939 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004940
4941 // Prime the lexer.
4942 Lex();
4943
4944 // While we have input, parse each statement.
4945 unsigned InputIdx = 0;
4946 unsigned OutputIdx = 0;
4947 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00004948 // Parse curly braces marking block start/end
4949 if (parseCurlyBlockScope(AsmStrRewrites))
4950 continue;
4951
Eli Friedman0f4871d2012-10-22 23:58:19 +00004952 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004953 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004954 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004955
Chad Rosier149e8e02012-12-12 22:45:52 +00004956 if (Info.ParseError)
4957 return true;
4958
Benjamin Kramer1a136112013-02-15 20:37:21 +00004959 if (Info.Opcode == ~0U)
4960 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004961
Benjamin Kramer1a136112013-02-15 20:37:21 +00004962 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004963
Benjamin Kramer1a136112013-02-15 20:37:21 +00004964 // Build the list of clobbers, outputs and inputs.
4965 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004966 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004967
Benjamin Kramer1a136112013-02-15 20:37:21 +00004968 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004969 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004970 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004971
Benjamin Kramer1a136112013-02-15 20:37:21 +00004972 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004973 if (Operand.isReg() && !Operand.needAddressOf() &&
4974 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004975 unsigned NumDefs = Desc.getNumDefs();
4976 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004977 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4978 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004979 continue;
4980 }
4981
4982 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004983 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004984 if (SymName.empty())
4985 continue;
4986
David Blaikie960ea3f2014-06-08 16:18:35 +00004987 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004988 if (!OpDecl)
4989 continue;
4990
4991 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004992 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004993 if (isOutput) {
4994 ++InputIdx;
4995 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004996 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004997 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004998 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004999 } else {
5000 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005001 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5002 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005003 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005004 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005005 }
Reid Kleckneree088972013-12-10 18:27:32 +00005006
5007 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005008 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5009 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005010 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005011 }
5012
5013 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005014 NumOutputs = OutputDecls.size();
5015 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005016
5017 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005018 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5019 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5020 ClobberRegs.end());
5021 Clobbers.assign(ClobberRegs.size(), std::string());
5022 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5023 raw_string_ostream OS(Clobbers[I]);
5024 IP->printRegName(OS, ClobberRegs[I]);
5025 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005026
5027 // Merge the various outputs and inputs. Output are expected first.
5028 if (NumOutputs || NumInputs) {
5029 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005030 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005031 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005032 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005033 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005034 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005035 }
5036 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005037 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005038 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005039 }
5040 }
5041
5042 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005043 std::string AsmStringIR;
5044 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005045 StringRef ASMString =
5046 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5047 const char *AsmStart = ASMString.begin();
5048 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005049 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005050 for (const AsmRewrite &AR : AsmStrRewrites) {
5051 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005052 if (Kind == AOK_Delete)
5053 continue;
5054
David Majnemer8114c1a2014-06-23 02:17:16 +00005055 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005056 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005057
Chad Rosier120eefd2013-03-19 17:32:17 +00005058 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005059 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005060 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005061
Chad Rosier37e755c2012-10-23 17:43:43 +00005062 // Skip the original expression.
5063 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005064 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005065 continue;
5066 }
5067
Chad Rosierff10ed12013-04-12 16:26:42 +00005068 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005069 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005070 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005071 default:
5072 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005073 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005074 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005075 break;
5076 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005077 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005078 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005079 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005080 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005081 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005082 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005083 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005084 break;
5085 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005086 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005087 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005088 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005089 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005090 default: break;
5091 case 8: OS << "byte ptr "; break;
5092 case 16: OS << "word ptr "; break;
5093 case 32: OS << "dword ptr "; break;
5094 case 64: OS << "qword ptr "; break;
5095 case 80: OS << "xword ptr "; break;
5096 case 128: OS << "xmmword ptr "; break;
5097 case 256: OS << "ymmword ptr "; break;
5098 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005099 break;
5100 case AOK_Emit:
5101 OS << ".byte";
5102 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005103 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005104 // MS alignment directives are measured in bytes. If the native assembler
5105 // measures alignment in bytes, we can pass it straight through.
5106 OS << ".align";
5107 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5108 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005109
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005110 // Alignment is in log2 form, so print that instead and skip the original
5111 // immediate.
5112 unsigned Val = AR.Val;
5113 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005114 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005115 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5116 break;
5117 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005118 case AOK_EVEN:
5119 OS << ".even";
5120 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005121 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005122 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005123 OS.flush();
5124 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005125 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005126 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005127 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005128 case AOK_EndOfStatement:
5129 OS << "\n\t";
5130 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005131 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005132
Chad Rosier8bce6642012-10-18 15:49:34 +00005133 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005134 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005135 }
5136
5137 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005138 if (AsmStart != AsmEnd)
5139 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005140
5141 AsmString = OS.str();
5142 return false;
5143}
5144
Pete Cooper80d21cb2015-06-22 19:35:57 +00005145namespace llvm {
5146namespace MCParserUtils {
5147
5148/// Returns whether the given symbol is used anywhere in the given expression,
5149/// or subexpressions.
5150static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5151 switch (Value->getKind()) {
5152 case MCExpr::Binary: {
5153 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5154 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5155 isSymbolUsedInExpression(Sym, BE->getRHS());
5156 }
5157 case MCExpr::Target:
5158 case MCExpr::Constant:
5159 return false;
5160 case MCExpr::SymbolRef: {
5161 const MCSymbol &S =
5162 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5163 if (S.isVariable())
5164 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5165 return &S == Sym;
5166 }
5167 case MCExpr::Unary:
5168 return isSymbolUsedInExpression(
5169 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5170 }
5171
5172 llvm_unreachable("Unknown expr kind!");
5173}
5174
5175bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5176 MCAsmParser &Parser, MCSymbol *&Sym,
5177 const MCExpr *&Value) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00005178
5179 // FIXME: Use better location, we should use proper tokens.
Nirav Davefd910412016-06-17 16:06:17 +00005180 SMLoc EqualLoc = Parser.getTok().getLoc();
Pete Cooper80d21cb2015-06-22 19:35:57 +00005181
5182 if (Parser.parseExpression(Value)) {
5183 Parser.TokError("missing expression");
5184 Parser.eatToEndOfStatement();
5185 return true;
5186 }
5187
5188 // Note: we don't count b as used in "a = b". This is to allow
5189 // a = b
5190 // b = c
5191
Nirav Davefd910412016-06-17 16:06:17 +00005192 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
Pete Cooper80d21cb2015-06-22 19:35:57 +00005193 return Parser.TokError("unexpected token in assignment");
5194
5195 // Eat the end of statement marker.
5196 Parser.Lex();
5197
5198 // Validate that the LHS is allowed to be a variable (either it has not been
5199 // used as a symbol, or it is an absolute symbol).
5200 Sym = Parser.getContext().lookupSymbol(Name);
5201 if (Sym) {
5202 // Diagnose assignment to a label.
5203 //
5204 // FIXME: Diagnostics. Note the location of the definition as a label.
5205 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5206 if (isSymbolUsedInExpression(Sym, Value))
5207 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005208 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5209 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005210 ; // Allow redefinitions of undefined symbols only used in directives.
5211 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5212 ; // Allow redefinitions of variables that haven't yet been used.
5213 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5214 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5215 else if (!Sym->isVariable())
5216 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5217 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5218 return Parser.Error(EqualLoc,
5219 "invalid reassignment of non-absolute variable '" +
5220 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005221 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005222 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005223 return false;
5224 } else
5225 Sym = Parser.getContext().getOrCreateSymbol(Name);
5226
5227 Sym->setRedefinable(allow_redef);
5228
5229 return false;
5230}
5231
5232} // namespace MCParserUtils
5233} // namespace llvm
5234
Daniel Dunbar01e36072010-07-17 02:26:10 +00005235/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005236MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5237 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005238 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005239}