blob: e20e176ea44b08d026ce68b44f61e18f788e6605 [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"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +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>
Chad Rosier8bce6642012-10-18 15:49:34 +000045#include <set>
46#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
Daniel Dunbar86033402010-07-12 17:54:38 +000052namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000053/// \brief Helper types for tracking macro definitions.
54typedef std::vector<AsmToken> MCAsmMacroArgument;
55typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000056
57struct MCAsmMacroParameter {
58 StringRef Name;
59 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000064};
65
Eli Benderskya313ae62013-01-16 18:56:50 +000066typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
67
68struct MCAsmMacro {
69 StringRef Name;
70 StringRef Body;
71 MCAsmMacroParameters Parameters;
72
73public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000074 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
75 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000076};
77
Daniel Dunbar43235712010-07-18 18:54:11 +000078/// \brief Helper class for storing information about an active macro
79/// instantiation.
80struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000081 /// The location of the instantiation.
82 SMLoc InstantiationLoc;
83
Daniel Dunbar40f1d852012-12-01 01:38:48 +000084 /// The buffer where parsing should resume upon instantiation completion.
85 int ExitBuffer;
86
Daniel Dunbar43235712010-07-18 18:54:11 +000087 /// The location where parsing should resume upon instantiation completion.
88 SMLoc ExitLoc;
89
Nico Weber155dccd12014-07-24 17:08:39 +000090 /// The depth of TheCondStack at the start of the instantiation.
91 size_t CondStackDepth;
92
Daniel Dunbar43235712010-07-18 18:54:11 +000093public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000094 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000099 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Craig Topper353eda42014-04-24 06:44:33 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112};
113
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000114/// \brief The concrete assembly parser instance.
115class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000116 AsmParser(const AsmParser &) = delete;
117 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000118private:
119 AsmLexer Lexer;
120 MCContext &Ctx;
121 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000122 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000123 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000124 SourceMgr::DiagHandlerTy SavedDiagHandler;
125 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000126 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000127
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000128 /// This is the current buffer index we're lexing from as managed by the
129 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000130 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131
132 AsmCond TheCondState;
133 std::vector<AsmCond> TheCondStack;
134
Jim Grosbach4b905842013-09-20 23:08:21 +0000135 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000136 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000137 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000138 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000139
Jim Grosbach4b905842013-09-20 23:08:21 +0000140 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000141 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000142
Jim Grosbach4b905842013-09-20 23:08:21 +0000143 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000144 std::vector<MacroInstantiation*> ActiveMacros;
145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000147 std::deque<MCAsmMacro> MacroLikeBodies;
148
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000150 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000151
Toma Tabacu217116e2015-04-27 10:50:29 +0000152 /// \brief Keeps track of how many .macro's have been instantiated.
153 unsigned NumOfMacroInstantiations;
154
Daniel Dunbar43325c42010-09-09 22:42:56 +0000155 /// Flag tracking whether any errors have been encountered.
156 unsigned HadError : 1;
157
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000158 /// The values from the last parsed cpp hash file line comment if any.
Tim Northoverc0bef992016-04-13 19:46:54 +0000159 struct CppHashInfoTy {
160 StringRef Filename;
161 int64_t LineNumber;
162 SMLoc Loc;
163 unsigned Buf;
164 };
165 CppHashInfoTy CppHashInfo;
166
167 /// \brief List of forward directional labels for diagnosis at the end.
168 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
169
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000170 /// When generating dwarf for assembly source files we need to calculate the
171 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000172 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000173 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
174 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000175 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000176 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000177
Devang Patela173ee52012-01-31 18:14:05 +0000178 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
179 unsigned AssemblerDialect;
180
Jim Grosbach4b905842013-09-20 23:08:21 +0000181 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000182 bool IsDarwin;
183
Jim Grosbach4b905842013-09-20 23:08:21 +0000184 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000185 bool ParsingInlineAsm;
186
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000188 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000190 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191
Craig Topper59be68f2014-03-08 07:14:16 +0000192 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193
Craig Topper59be68f2014-03-08 07:14:16 +0000194 void addDirectiveHandler(StringRef Directive,
195 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000196 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000197 }
198
Toma Tabacu11e14a92015-04-21 11:50:52 +0000199 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
200 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
201 }
202
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000203public:
204 /// @name MCAsmParser Interface
205 /// {
206
Craig Topper59be68f2014-03-08 07:14:16 +0000207 SourceMgr &getSourceManager() override { return SrcMgr; }
208 MCAsmLexer &getLexer() override { return Lexer; }
209 MCContext &getContext() override { return Ctx; }
210 MCStreamer &getStreamer() override { return Out; }
211 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000212 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000213 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000214 else
215 return AssemblerDialect;
216 }
Craig Topper59be68f2014-03-08 07:14:16 +0000217 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000218 AssemblerDialect = i;
219 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper59be68f2014-03-08 07:14:16 +0000221 void Note(SMLoc L, const Twine &Msg,
222 ArrayRef<SMRange> Ranges = None) override;
223 bool Warning(SMLoc L, const Twine &Msg,
224 ArrayRef<SMRange> Ranges = None) override;
225 bool Error(SMLoc L, const Twine &Msg,
226 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000227
Craig Topper59be68f2014-03-08 07:14:16 +0000228 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000229
Craig Topper59be68f2014-03-08 07:14:16 +0000230 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
231 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000234 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000235 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000236 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000237 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000238 const MCInstrInfo *MII, const MCInstPrinter *IP,
239 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000240
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000242 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
243 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
244 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000245 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
246 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000247 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000250 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000251 bool parseIdentifier(StringRef &Res) override;
252 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000253
Craig Topper59be68f2014-03-08 07:14:16 +0000254 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000255 /// }
256
257private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000258
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000259 bool parseStatement(ParseStatementInfo &Info,
260 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000261 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 void eatToEndOfLine();
Craig Topper3c76c522015-09-20 23:35:59 +0000263 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000264
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000266 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000267 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000268 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000269 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000270 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000271
Eli Benderskya313ae62013-01-16 18:56:50 +0000272 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Lookup a previously defined macro.
279 /// \param Name Macro name.
280 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
283 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000284 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000290 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000292 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000293 ///
294 /// \param M The macro.
295 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
298 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000300
David Majnemer91fc4c22014-01-29 18:57:46 +0000301 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000302 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000303
304 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000306
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 void printMacroInstantiations();
308 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000309 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000310 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000311 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000312 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000313
Jim Grosbach4b905842013-09-20 23:08:21 +0000314 /// \brief Enter the specified file. This returns true on failure.
315 bool enterIncludeFile(const std::string &Filename);
316
317 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000318 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000320
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000321 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000322 /// current token is not set; clients should ensure Lex() is called
323 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000324 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000325 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000326 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000327 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000328
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000329 /// \brief Parse up to the end of statement and a return the contents from the
330 /// current token until the end of the statement; the current token on exit
331 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000332 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000333
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000334 /// \brief Parse until the end of a statement or a comma is encountered,
335 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000337
Jim Grosbach4b905842013-09-20 23:08:21 +0000338 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000339 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000340
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000341 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
342 MCBinaryExpr::Opcode &Kind);
343
Jim Grosbach4b905842013-09-20 23:08:21 +0000344 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
345 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
346 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000347
Jim Grosbach4b905842013-09-20 23:08:21 +0000348 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000349
Eli Bendersky17233942013-01-15 22:59:42 +0000350 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000351 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000352 DK_NO_DIRECTIVE, // Placeholder
353 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000354 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000355 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
356 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000357 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000358 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000359 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000360 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000361 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000362 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
363 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000364 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000365 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
366 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000367 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000368 DK_CV_FILE, DK_CV_LOC, DK_CV_LINETABLE, DK_CV_INLINE_LINETABLE,
David Majnemer408b5e62016-02-05 01:55:49 +0000369 DK_CV_DEF_RANGE, DK_CV_STRINGTABLE, DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000370 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
371 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
372 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
373 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
374 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000375 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000376 DK_MACROS_ON, DK_MACROS_OFF,
377 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000378 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000379 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000380 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000381 };
382
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000384 /// directives parsed by this class.
385 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000386
387 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000389 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000390 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000391 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
393 bool parseDirectiveFill(); // ".fill"
394 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000395 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000396 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
397 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000398 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000399 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000400
Eli Bendersky17233942013-01-15 22:59:42 +0000401 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000402 bool parseDirectiveFile(SMLoc DirectiveLoc);
403 bool parseDirectiveLine();
404 bool parseDirectiveLoc();
405 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000406
David Majnemer408b5e62016-02-05 01:55:49 +0000407 // ".cv_file", ".cv_loc", ".cv_linetable", "cv_inline_linetable",
408 // ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000409 bool parseDirectiveCVFile();
410 bool parseDirectiveCVLoc();
411 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000412 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000413 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000414 bool parseDirectiveCVStringTable();
415 bool parseDirectiveCVFileChecksums();
416
Eli Bendersky17233942013-01-15 22:59:42 +0000417 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000419 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveCFISections();
421 bool parseDirectiveCFIStartProc();
422 bool parseDirectiveCFIEndProc();
423 bool parseDirectiveCFIDefCfaOffset();
424 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
425 bool parseDirectiveCFIAdjustCfaOffset();
426 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
427 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
428 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
429 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
430 bool parseDirectiveCFIRememberState();
431 bool parseDirectiveCFIRestoreState();
432 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
433 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
434 bool parseDirectiveCFIEscape();
435 bool parseDirectiveCFISignalFrame();
436 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000437
438 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000440 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveEndMacro(StringRef Directive);
442 bool parseDirectiveMacro(SMLoc DirectiveLoc);
443 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000444
Eli Benderskyf483ff92012-12-20 19:05:53 +0000445 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000447 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000449 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000451
Eli Bendersky17233942013-01-15 22:59:42 +0000452 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000454
455 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000457
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000459 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000461
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000463
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveAbort(); // ".abort"
465 bool parseDirectiveInclude(); // ".include"
466 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000467
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000468 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
469 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000470 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000472 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000473 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000474 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
475 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000476 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000477 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
478 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
479 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
480 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000481 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000482
Jim Grosbach4b905842013-09-20 23:08:21 +0000483 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000484 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000485
Rafael Espindola34b9c512012-06-03 23:57:14 +0000486 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000487 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
488 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000489 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000490 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000491 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
492 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
493 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000494
Chad Rosierc7f552c2013-02-12 21:33:51 +0000495 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000496 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000497 size_t Len);
498
499 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000500 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000501
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000502 // "end"
503 bool parseDirectiveEnd(SMLoc DirectiveLoc);
504
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000505 // ".err" or ".error"
506 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000507
Nico Weber404012b2014-07-24 16:26:06 +0000508 // ".warning"
509 bool parseDirectiveWarning(SMLoc DirectiveLoc);
510
Eli Bendersky17233942013-01-15 22:59:42 +0000511 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000512};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000513}
Daniel Dunbar86033402010-07-12 17:54:38 +0000514
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000515namespace llvm {
516
517extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000518extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000519extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000520
521}
522
Chris Lattnerc35681b2010-01-19 19:46:13 +0000523enum { DEFAULT_ADDRSPACE = 0 };
524
David Blaikie9f380a32015-03-16 18:06:57 +0000525AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
526 const MCAsmInfo &MAI)
527 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
528 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Tim Northoverc0bef992016-04-13 19:46:54 +0000529 MacrosEnabledFlag(true), HadError(false), CppHashInfo(),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000530 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000531 // Save the old handler.
532 SavedDiagHandler = SrcMgr.getDiagHandler();
533 SavedDiagContext = SrcMgr.getDiagContext();
534 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000535 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000536 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000537
Daniel Dunbarc5011082010-07-12 18:12:02 +0000538 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000539 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
540 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000541 PlatformParser.reset(createCOFFAsmParser());
542 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000543 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000544 PlatformParser.reset(createDarwinAsmParser());
545 IsDarwin = true;
546 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000547 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000548 PlatformParser.reset(createELFAsmParser());
549 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000550 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000551
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000552 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000553 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000554
555 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000556}
557
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000558AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000559 assert((HadError || ActiveMacros.empty()) &&
560 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000561}
562
Jim Grosbach4b905842013-09-20 23:08:21 +0000563void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000564 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000565 for (std::vector<MacroInstantiation *>::const_reverse_iterator
566 it = ActiveMacros.rbegin(),
567 ie = ActiveMacros.rend();
568 it != ie; ++it)
569 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000570 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000571}
572
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000573void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
574 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
575 printMacroInstantiations();
576}
577
Chris Lattnera3a06812011-10-16 04:47:35 +0000578bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000579 if(getTargetParser().getTargetOptions().MCNoWarn)
580 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000581 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000582 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000583 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
584 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000585 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000586}
587
Chris Lattnera3a06812011-10-16 04:47:35 +0000588bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000589 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000590 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
591 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000592 return true;
593}
594
Jim Grosbach4b905842013-09-20 23:08:21 +0000595bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000596 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000597 unsigned NewBuf =
598 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
599 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000601
Sean Callanan7a77eae2010-01-21 00:19:58 +0000602 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000603 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000604 return false;
605}
Daniel Dunbar43235712010-07-18 18:54:11 +0000606
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000607/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000608/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000609/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000610bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000611 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000612 unsigned NewBuf =
613 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
614 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000615 return true;
616
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000617 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000618 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000619 return false;
620}
621
Alp Tokera55b95b2014-07-06 10:33:31 +0000622void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
623 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000624 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
625 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000626}
627
Sean Callanan7a77eae2010-01-21 00:19:58 +0000628const AsmToken &AsmParser::Lex() {
629 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000630
Sean Callanan7a77eae2010-01-21 00:19:58 +0000631 if (tok->is(AsmToken::Eof)) {
632 // If this is the end of an included file, pop the parent file off the
633 // include stack.
634 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
635 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000636 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000637 tok = &Lexer.Lex();
638 }
639 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000640
Sean Callanan7a77eae2010-01-21 00:19:58 +0000641 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000642 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000643
Sean Callanan7a77eae2010-01-21 00:19:58 +0000644 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000645}
646
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000647bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000648 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000649 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000650 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000651
Chris Lattner36e02122009-06-21 20:54:55 +0000652 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000653 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000654
655 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000656 AsmCond StartingCondState = TheCondState;
657
Kevin Enderby6469fc22011-11-01 22:27:22 +0000658 // If we are generating dwarf for assembly source files save the initial text
659 // section and generate a .file directive.
660 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000661 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000662 if (!Sec->getBeginSymbol()) {
663 MCSymbol *SectionStartSym = getContext().createTempSymbol();
664 getStreamer().EmitLabel(SectionStartSym);
665 Sec->setBeginSymbol(SectionStartSym);
666 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000667 bool InsertResult = getContext().addGenDwarfSection(Sec);
668 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000669 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000670 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
671 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000672 }
673
Chris Lattner73f36112009-07-02 21:53:43 +0000674 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000675 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000676 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000677 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000679
Daniel Dunbar43325c42010-09-09 22:42:56 +0000680 // We had an error, validate that one was emitted and recover by skipping to
681 // the next line.
682 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000683 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000684 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000685
686 if (TheCondState.TheCond != StartingCondState.TheCond ||
687 TheCondState.Ignore != StartingCondState.Ignore)
688 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000689
690 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000691 const auto &LineTables = getContext().getMCDwarfLineTables();
692 if (!LineTables.empty()) {
693 unsigned Index = 0;
694 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
695 if (File.Name.empty() && Index != 0)
696 TokError("unassigned file number: " + Twine(Index) +
697 " for .file directives");
698 ++Index;
699 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000700 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000701
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000702 // Check to see that all assembler local symbols were actually defined.
703 // Targets that don't do subsections via symbols may not want this, though,
704 // so conservatively exclude them. Only do this if we're finalizing, though,
705 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000706 if (!NoFinalize) {
707 if (MAI.hasSubsectionsViaSymbols()) {
708 for (const auto &TableEntry : getContext().getSymbols()) {
709 MCSymbol *Sym = TableEntry.getValue();
710 // Variable symbols may not be marked as defined, so check those
711 // explicitly. If we know it's a variable, we have a definition for
712 // the purposes of this check.
713 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
714 // FIXME: We would really like to refer back to where the symbol was
715 // first referenced for a source location. We need to add something
716 // to track that. Currently, we just point to the end of the file.
717 HadError |=
718 Error(getLexer().getLoc(), "assembler local symbol '" +
719 Sym->getName() + "' not defined");
720 }
721 }
722
723 // Temporary symbols like the ones for directional jumps don't go in the
724 // symbol table. They also need to be diagnosed in all (final) cases.
Tim Northoverc0bef992016-04-13 19:46:54 +0000725 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
726 if (std::get<2>(LocSym)->isUndefined()) {
727 // Reset the state of any "# line file" directives we've seen to the
728 // context as it was at the diagnostic site.
729 CppHashInfo = std::get<1>(LocSym);
730 HadError |= Error(std::get<0>(LocSym), "directional label undefined");
731 }
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000732 }
733 }
734
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000735 // Finalize the output stream if there are no errors and if the client wants
736 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000737 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000738 Out.Finish();
739
Oliver Stannard07b43d32015-11-17 09:58:07 +0000740 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000741}
742
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000743void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000744 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000745 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000746 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000747 }
748}
749
Jim Grosbach4b905842013-09-20 23:08:21 +0000750/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000751void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000752 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000753 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000754
Chris Lattnere5074c42009-06-22 01:29:09 +0000755 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000756 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000757 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000758}
759
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000760StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000761 const char *Start = getTok().getLoc().getPointer();
762
Jim Grosbach4b905842013-09-20 23:08:21 +0000763 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000764 Lex();
765
766 const char *End = getTok().getLoc().getPointer();
767 return StringRef(Start, End - Start);
768}
Chris Lattner78db3622009-06-22 05:51:26 +0000769
Jim Grosbach4b905842013-09-20 23:08:21 +0000770StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000771 const char *Start = getTok().getLoc().getPointer();
772
773 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000774 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000775 Lex();
776
777 const char *End = getTok().getLoc().getPointer();
778 return StringRef(Start, End - Start);
779}
780
Jim Grosbach4b905842013-09-20 23:08:21 +0000781/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000782/// NOTE: This assumes the leading '(' has already been consumed.
783///
784/// parenexpr ::= expr)
785///
Jim Grosbach4b905842013-09-20 23:08:21 +0000786bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
787 if (parseExpression(Res))
788 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000789 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000790 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000791 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000792 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000793 return false;
794}
Chris Lattner78db3622009-06-22 05:51:26 +0000795
Jim Grosbach4b905842013-09-20 23:08:21 +0000796/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000797/// NOTE: This assumes the leading '[' has already been consumed.
798///
799/// bracketexpr ::= expr]
800///
Jim Grosbach4b905842013-09-20 23:08:21 +0000801bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
802 if (parseExpression(Res))
803 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000804 if (Lexer.isNot(AsmToken::RBrac))
805 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000806 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000807 Lex();
808 return false;
809}
810
Jim Grosbach4b905842013-09-20 23:08:21 +0000811/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000812/// primaryexpr ::= (parenexpr
813/// primaryexpr ::= symbol
814/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000815/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000816/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000817bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000818 SMLoc FirstTokenLoc = getLexer().getLoc();
819 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
820 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000821 default:
822 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000823 // If we have an error assume that we've already handled it.
824 case AsmToken::Error:
825 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000826 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000827 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000828 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000829 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000830 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000831 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000832 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000833 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000834 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000835 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000836 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000837 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000838 if (FirstTokenKind == AsmToken::Dollar) {
839 if (Lexer.getMAI().getDollarIsPC()) {
840 // This is a '$' reference, which references the current PC. Emit a
841 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000842 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000843 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000844 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000845 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000846 EndLoc = FirstTokenLoc;
847 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000848 }
849 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000850 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000851 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000852 // Parse symbol variant
853 std::pair<StringRef, StringRef> Split;
854 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000855 if (FirstTokenKind == AsmToken::String) {
856 if (Lexer.is(AsmToken::At)) {
857 Lexer.Lex(); // eat @
858 SMLoc AtLoc = getLexer().getLoc();
859 StringRef VName;
860 if (parseIdentifier(VName))
861 return Error(AtLoc, "expected symbol variant after '@'");
862
863 Split = std::make_pair(Identifier, VName);
864 }
865 } else {
866 Split = Identifier.split('@');
867 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000868 } else if (Lexer.is(AsmToken::LParen)) {
869 Lexer.Lex(); // eat (
870 StringRef VName;
871 parseIdentifier(VName);
872 if (Lexer.isNot(AsmToken::RParen)) {
873 return Error(Lexer.getTok().getLoc(),
874 "unexpected token in variant, expected ')'");
875 }
876 Lexer.Lex(); // eat )
877 Split = std::make_pair(Identifier, VName);
878 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000879
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000880 EndLoc = SMLoc::getFromPointer(Identifier.end());
881
Daniel Dunbard20cda02009-10-16 01:34:54 +0000882 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000883 StringRef SymbolName = Identifier;
884 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000885
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000886 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000887 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000888 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000889 if (Variant != MCSymbolRefExpr::VK_Invalid) {
890 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000891 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000892 Variant = MCSymbolRefExpr::VK_None;
893 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000894 return Error(SMLoc::getFromPointer(Split.second.begin()),
895 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000896 }
897 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000898
Jim Grosbach6f482002015-05-18 18:43:14 +0000899 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000900
Daniel Dunbard20cda02009-10-16 01:34:54 +0000901 // If this is an absolute variable reference, substitute it now to preserve
902 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000903 if (Sym->isVariable() &&
904 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000905 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000906 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000907
Vedant Kumar86dbd922015-08-31 17:44:53 +0000908 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000909 return false;
910 }
911
912 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000913 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000914 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000915 }
David Woodhousef42a6662014-02-01 16:20:54 +0000916 case AsmToken::BigNum:
917 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000918 case AsmToken::Integer: {
919 SMLoc Loc = getTok().getLoc();
920 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000921 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000922 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000923 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000924 // Look for 'b' or 'f' following an Integer as a directional label
925 if (Lexer.getKind() == AsmToken::Identifier) {
926 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000927 // Lookup the symbol variant if used.
928 std::pair<StringRef, StringRef> Split = IDVal.split('@');
929 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
930 if (Split.first.size() != IDVal.size()) {
931 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000932 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000933 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000934 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000935 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000936 if (IDVal == "f" || IDVal == "b") {
937 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000938 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000939 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000940 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +0000941 return Error(Loc, "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +0000942 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000943 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000944 Lex(); // Eat identifier.
945 }
946 }
Chris Lattner78db3622009-06-22 05:51:26 +0000947 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000948 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000949 case AsmToken::Real: {
950 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000951 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000952 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000953 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000954 Lex(); // Eat token.
955 return false;
956 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000957 case AsmToken::Dot: {
958 // This is a '.' reference, which references the current PC. Emit a
959 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000960 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000961 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000962 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000963 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000964 Lex(); // Eat identifier.
965 return false;
966 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000967 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000968 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000969 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000970 case AsmToken::LBrac:
971 if (!PlatformParser->HasBracketExpressions())
972 return TokError("brackets expression not supported on this target");
973 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000975 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000976 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000977 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000978 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000979 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000980 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000981 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000982 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000984 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000985 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000986 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000987 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000988 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000990 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000991 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000992 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000993 }
994}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000995
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000996bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000997 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000998 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000999}
1000
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +00001002AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +00001003 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +00001004 // Ask the target implementation about this expression first.
1005 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1006 if (NewE)
1007 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001008 // Recurse over the given expression, rebuilding it to apply the given variant
1009 // if there is exactly one symbol.
1010 switch (E->getKind()) {
1011 case MCExpr::Target:
1012 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001013 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001014
1015 case MCExpr::SymbolRef: {
1016 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1017
1018 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001019 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1020 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001021 return E;
1022 }
1023
Jim Grosbach13760bd2015-05-30 01:25:56 +00001024 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 }
1026
1027 case MCExpr::Unary: {
1028 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001029 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001030 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001031 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001032 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 }
1034
1035 case MCExpr::Binary: {
1036 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1038 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039
1040 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001041 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 if (!LHS)
1044 LHS = BE->getLHS();
1045 if (!RHS)
1046 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001047
Jim Grosbach13760bd2015-05-30 01:25:56 +00001048 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 }
1050 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001051
Craig Toppera2886c22012-02-07 05:05:23 +00001052 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001053}
1054
Jim Grosbach4b905842013-09-20 23:08:21 +00001055/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001056///
Jim Grosbachbd164242011-08-20 16:24:13 +00001057/// expr ::= expr &&,|| expr -> lowest.
1058/// expr ::= expr |,^,&,! expr
1059/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1060/// expr ::= expr <<,>> expr
1061/// expr ::= expr +,- expr
1062/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001063/// expr ::= primaryexpr
1064///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001065bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001066 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001067 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001068 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001069 return true;
1070
Daniel Dunbar55f16672010-09-17 02:47:07 +00001071 // As a special case, we support 'a op b @ modifier' by rewriting the
1072 // expression to include the modifier. This is inefficient, but in general we
1073 // expect users to use 'a@modifier op b'.
1074 if (Lexer.getKind() == AsmToken::At) {
1075 Lex();
1076
1077 if (Lexer.isNot(AsmToken::Identifier))
1078 return TokError("unexpected symbol modifier following '@'");
1079
1080 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001082 if (Variant == MCSymbolRefExpr::VK_Invalid)
1083 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1084
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001086 if (!ModifiedRes) {
1087 return TokError("invalid modifier '" + getTok().getIdentifier() +
1088 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001089 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001090
Daniel Dunbar55f16672010-09-17 02:47:07 +00001091 Res = ModifiedRes;
1092 Lex();
1093 }
1094
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001095 // Try to constant fold it up front, if possible.
1096 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001097 if (Res->evaluateAsAbsolute(Value))
1098 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001099
1100 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001101}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001102
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001103bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001104 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001106}
1107
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001108bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1109 SMLoc &EndLoc) {
1110 if (parseParenExpr(Res, EndLoc))
1111 return true;
1112
1113 for (; ParenDepth > 0; --ParenDepth) {
1114 if (parseBinOpRHS(1, Res, EndLoc))
1115 return true;
1116
1117 // We don't Lex() the last RParen.
1118 // This is the same behavior as parseParenExpression().
1119 if (ParenDepth - 1 > 0) {
1120 if (Lexer.isNot(AsmToken::RParen))
1121 return TokError("expected ')' in parentheses expression");
1122 EndLoc = Lexer.getTok().getEndLoc();
1123 Lex();
1124 }
1125 }
1126 return false;
1127}
1128
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001129bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001130 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001131
Daniel Dunbar75630b32009-06-30 02:10:03 +00001132 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001133 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001134 return true;
1135
Jim Grosbach13760bd2015-05-30 01:25:56 +00001136 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001137 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001138
1139 return false;
1140}
1141
David Majnemer0993e0b2015-10-26 03:15:34 +00001142static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1143 MCBinaryExpr::Opcode &Kind,
1144 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001146 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001147 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001148
Jim Grosbach4b905842013-09-20 23:08:21 +00001149 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001150 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001151 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001153 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001154 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001155 return 1;
1156
Jim Grosbach4b905842013-09-20 23:08:21 +00001157 // Low Precedence: |, &, ^
1158 //
1159 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001160 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001161 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001162 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001163 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001164 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001165 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001166 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001167 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001168 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001169
Jim Grosbach4b905842013-09-20 23:08:21 +00001170 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001171 case AsmToken::EqualEqual:
1172 Kind = MCBinaryExpr::EQ;
1173 return 3;
1174 case AsmToken::ExclaimEqual:
1175 case AsmToken::LessGreater:
1176 Kind = MCBinaryExpr::NE;
1177 return 3;
1178 case AsmToken::Less:
1179 Kind = MCBinaryExpr::LT;
1180 return 3;
1181 case AsmToken::LessEqual:
1182 Kind = MCBinaryExpr::LTE;
1183 return 3;
1184 case AsmToken::Greater:
1185 Kind = MCBinaryExpr::GT;
1186 return 3;
1187 case AsmToken::GreaterEqual:
1188 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001189 return 3;
1190
Jim Grosbach4b905842013-09-20 23:08:21 +00001191 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001192 case AsmToken::LessLess:
1193 Kind = MCBinaryExpr::Shl;
1194 return 4;
1195 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001196 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001197 return 4;
1198
Jim Grosbach4b905842013-09-20 23:08:21 +00001199 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001200 case AsmToken::Plus:
1201 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001202 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001203 case AsmToken::Minus:
1204 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001205 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001206
Jim Grosbach4b905842013-09-20 23:08:21 +00001207 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001208 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001209 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001210 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001211 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001212 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001213 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001214 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001215 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001216 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001217 }
1218}
1219
David Majnemer0993e0b2015-10-26 03:15:34 +00001220static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1221 MCBinaryExpr::Opcode &Kind,
1222 bool ShouldUseLogicalShr) {
1223 switch (K) {
1224 default:
1225 return 0; // not a binop.
1226
1227 // Lowest Precedence: &&, ||
1228 case AsmToken::AmpAmp:
1229 Kind = MCBinaryExpr::LAnd;
1230 return 2;
1231 case AsmToken::PipePipe:
1232 Kind = MCBinaryExpr::LOr;
1233 return 1;
1234
1235 // Low Precedence: ==, !=, <>, <, <=, >, >=
1236 case AsmToken::EqualEqual:
1237 Kind = MCBinaryExpr::EQ;
1238 return 3;
1239 case AsmToken::ExclaimEqual:
1240 case AsmToken::LessGreater:
1241 Kind = MCBinaryExpr::NE;
1242 return 3;
1243 case AsmToken::Less:
1244 Kind = MCBinaryExpr::LT;
1245 return 3;
1246 case AsmToken::LessEqual:
1247 Kind = MCBinaryExpr::LTE;
1248 return 3;
1249 case AsmToken::Greater:
1250 Kind = MCBinaryExpr::GT;
1251 return 3;
1252 case AsmToken::GreaterEqual:
1253 Kind = MCBinaryExpr::GTE;
1254 return 3;
1255
1256 // Low Intermediate Precedence: +, -
1257 case AsmToken::Plus:
1258 Kind = MCBinaryExpr::Add;
1259 return 4;
1260 case AsmToken::Minus:
1261 Kind = MCBinaryExpr::Sub;
1262 return 4;
1263
1264 // High Intermediate Precedence: |, &, ^
1265 //
1266 // FIXME: gas seems to support '!' as an infix operator?
1267 case AsmToken::Pipe:
1268 Kind = MCBinaryExpr::Or;
1269 return 5;
1270 case AsmToken::Caret:
1271 Kind = MCBinaryExpr::Xor;
1272 return 5;
1273 case AsmToken::Amp:
1274 Kind = MCBinaryExpr::And;
1275 return 5;
1276
1277 // Highest Precedence: *, /, %, <<, >>
1278 case AsmToken::Star:
1279 Kind = MCBinaryExpr::Mul;
1280 return 6;
1281 case AsmToken::Slash:
1282 Kind = MCBinaryExpr::Div;
1283 return 6;
1284 case AsmToken::Percent:
1285 Kind = MCBinaryExpr::Mod;
1286 return 6;
1287 case AsmToken::LessLess:
1288 Kind = MCBinaryExpr::Shl;
1289 return 6;
1290 case AsmToken::GreaterGreater:
1291 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1292 return 6;
1293 }
1294}
1295
1296unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1297 MCBinaryExpr::Opcode &Kind) {
1298 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1299 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1300 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1301}
1302
Jim Grosbach4b905842013-09-20 23:08:21 +00001303/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001304/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001305bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001306 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001307 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001308 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001309 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001311 // If the next token is lower precedence than we are allowed to eat, return
1312 // successfully with what we ate already.
1313 if (TokPrec < Precedence)
1314 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001315
Sean Callanan686ed8d2010-01-19 20:22:31 +00001316 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001317
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001318 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001319 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001320 if (parsePrimaryExpr(RHS, EndLoc))
1321 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001322
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001323 // If BinOp binds less tightly with RHS than the operator after RHS, let
1324 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001325 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001326 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001327 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1328 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001329
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001330 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001331 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001332 }
1333}
1334
Chris Lattner36e02122009-06-21 20:54:55 +00001335/// ParseStatement:
1336/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001337/// ::= Label* Directive ...Operands... EndOfStatement
1338/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001339bool AsmParser::parseStatement(ParseStatementInfo &Info,
1340 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001341 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001342 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001343 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001344 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001345 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001346
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001347 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001348 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001349 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001350 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001351 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001352 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001353 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001354 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001355
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001356 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001357 if (Lexer.is(AsmToken::Integer)) {
1358 LocalLabelVal = getTok().getIntVal();
1359 if (LocalLabelVal < 0) {
1360 if (!TheCondState.Ignore)
1361 return TokError("unexpected token at start of statement");
1362 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001363 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001364 IDVal = getTok().getString();
1365 Lex(); // Consume the integer token to be used as an identifier token.
1366 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001367 if (!TheCondState.Ignore)
1368 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001369 }
1370 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001371 } else if (Lexer.is(AsmToken::Dot)) {
1372 // Treat '.' as a valid identifier in this context.
1373 Lex();
1374 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001375 } else if (Lexer.is(AsmToken::LCurly)) {
1376 // Treat '{' as a valid identifier in this context.
1377 Lex();
1378 IDVal = "{";
1379
1380 } else if (Lexer.is(AsmToken::RCurly)) {
1381 // Treat '}' as a valid identifier in this context.
1382 Lex();
1383 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001384 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001385 if (!TheCondState.Ignore)
1386 return TokError("unexpected token at start of statement");
1387 IDVal = "";
1388 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001389
Chris Lattner926885c2010-04-17 18:14:27 +00001390 // Handle conditional assembly here before checking for skipping. We
1391 // have to do this so that .endif isn't skipped in a ".if 0" block for
1392 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001393 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001394 DirectiveKindMap.find(IDVal);
1395 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1396 ? DK_NO_DIRECTIVE
1397 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001398 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001399 default:
1400 break;
1401 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001402 case DK_IFEQ:
1403 case DK_IFGE:
1404 case DK_IFGT:
1405 case DK_IFLE:
1406 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001407 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001408 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001409 case DK_IFB:
1410 return parseDirectiveIfb(IDLoc, true);
1411 case DK_IFNB:
1412 return parseDirectiveIfb(IDLoc, false);
1413 case DK_IFC:
1414 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001415 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001416 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001417 case DK_IFNC:
1418 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001419 case DK_IFNES:
1420 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001421 case DK_IFDEF:
1422 return parseDirectiveIfdef(IDLoc, true);
1423 case DK_IFNDEF:
1424 case DK_IFNOTDEF:
1425 return parseDirectiveIfdef(IDLoc, false);
1426 case DK_ELSEIF:
1427 return parseDirectiveElseIf(IDLoc);
1428 case DK_ELSE:
1429 return parseDirectiveElse(IDLoc);
1430 case DK_ENDIF:
1431 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001432 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001433
Eli Bendersky88024712013-01-16 19:32:36 +00001434 // Ignore the statement if in the middle of inactive conditional
1435 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001436 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001437 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001438 return false;
1439 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001440
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001441 // FIXME: Recurse on local labels?
1442
1443 // See what kind of statement we have.
1444 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001445 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001446 if (!getTargetParser().isLabel(ID))
1447 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001448 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001449
Chris Lattner36e02122009-06-21 20:54:55 +00001450 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001451 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001452
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001453 // Diagnose attempt to use '.' as a label.
1454 if (IDVal == ".")
1455 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1456
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001457 // Diagnose attempt to use a variable as a label.
1458 //
1459 // FIXME: Diagnostics. Note the location of the definition as a label.
1460 // FIXME: This doesn't diagnose assignment to a symbol which has been
1461 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001462 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001463 if (LocalLabelVal == -1) {
1464 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001465 StringRef RewrittenLabel =
1466 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1467 assert(RewrittenLabel.size() &&
1468 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001469 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1470 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001471 IDVal = RewrittenLabel;
1472 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001473 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001474 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001475 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001476
1477 Sym->redefineIfPossible();
1478
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001479 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001480 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001481
Daniel Dunbare73b2672009-08-26 22:13:22 +00001482 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001483 if (!ParsingInlineAsm)
1484 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001485
Kevin Enderbye7739d42011-12-09 18:09:40 +00001486 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001487 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001488 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001489 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1490 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001491
Tim Northover1744d0a2013-10-25 12:49:50 +00001492 getTargetParser().onLabelParsed(Sym);
1493
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001494 // Consume any end of statement token, if present, to avoid spurious
1495 // AddBlankLine calls().
1496 if (Lexer.is(AsmToken::EndOfStatement)) {
1497 Lex();
1498 if (Lexer.is(AsmToken::Eof))
1499 return false;
1500 }
1501
Eli Friedman0f4871d2012-10-22 23:58:19 +00001502 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001503 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001504
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001505 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001506 if (!getTargetParser().equalIsAsmAssignment())
1507 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001508 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001509 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001510
Jim Grosbach4b905842013-09-20 23:08:21 +00001511 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001512
1513 default: // Normal instruction or directive.
1514 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001515 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001516
1517 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001518 if (areMacrosEnabled())
1519 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1520 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001521 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001522
Michael J. Spencer530ce852010-10-09 11:00:50 +00001523 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001524
Eli Bendersky17233942013-01-15 22:59:42 +00001525 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001526 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001527 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001528 //
Eli Bendersky17233942013-01-15 22:59:42 +00001529 // 1. The target-specific assembly parser. Some directives are target
1530 // specific or may potentially behave differently on certain targets.
1531 // 2. Asm parser extensions. For example, platform-specific parsers
1532 // (like the ELF parser) register themselves as extensions.
1533 // 3. The generic directive parser implemented by this class. These are
1534 // all the directives that behave in a target and platform independent
1535 // manner, or at least have a default behavior that's shared between
1536 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001537
Eli Bendersky17233942013-01-15 22:59:42 +00001538 // First query the target-specific parser. It will return 'true' if it
1539 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001540 if (!getTargetParser().ParseDirective(ID))
1541 return false;
1542
Alp Tokercb402912014-01-24 17:20:08 +00001543 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001544 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1546 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001547 if (Handler.first)
1548 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1549
1550 // Finally, if no one else is interested in this directive, it must be
1551 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001552 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001553 default:
1554 break;
1555 case DK_SET:
1556 case DK_EQU:
1557 return parseDirectiveSet(IDVal, true);
1558 case DK_EQUIV:
1559 return parseDirectiveSet(IDVal, false);
1560 case DK_ASCII:
1561 return parseDirectiveAscii(IDVal, false);
1562 case DK_ASCIZ:
1563 case DK_STRING:
1564 return parseDirectiveAscii(IDVal, true);
1565 case DK_BYTE:
1566 return parseDirectiveValue(1);
1567 case DK_SHORT:
1568 case DK_VALUE:
1569 case DK_2BYTE:
1570 return parseDirectiveValue(2);
1571 case DK_LONG:
1572 case DK_INT:
1573 case DK_4BYTE:
1574 return parseDirectiveValue(4);
1575 case DK_QUAD:
1576 case DK_8BYTE:
1577 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001578 case DK_OCTA:
1579 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001580 case DK_SINGLE:
1581 case DK_FLOAT:
1582 return parseDirectiveRealValue(APFloat::IEEEsingle);
1583 case DK_DOUBLE:
1584 return parseDirectiveRealValue(APFloat::IEEEdouble);
1585 case DK_ALIGN: {
1586 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1587 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1588 }
1589 case DK_ALIGN32: {
1590 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1591 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1592 }
1593 case DK_BALIGN:
1594 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1595 case DK_BALIGNW:
1596 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1597 case DK_BALIGNL:
1598 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1599 case DK_P2ALIGN:
1600 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1601 case DK_P2ALIGNW:
1602 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1603 case DK_P2ALIGNL:
1604 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1605 case DK_ORG:
1606 return parseDirectiveOrg();
1607 case DK_FILL:
1608 return parseDirectiveFill();
1609 case DK_ZERO:
1610 return parseDirectiveZero();
1611 case DK_EXTERN:
1612 eatToEndOfStatement(); // .extern is the default, ignore it.
1613 return false;
1614 case DK_GLOBL:
1615 case DK_GLOBAL:
1616 return parseDirectiveSymbolAttribute(MCSA_Global);
1617 case DK_LAZY_REFERENCE:
1618 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1619 case DK_NO_DEAD_STRIP:
1620 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1621 case DK_SYMBOL_RESOLVER:
1622 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1623 case DK_PRIVATE_EXTERN:
1624 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1625 case DK_REFERENCE:
1626 return parseDirectiveSymbolAttribute(MCSA_Reference);
1627 case DK_WEAK_DEFINITION:
1628 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1629 case DK_WEAK_REFERENCE:
1630 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1631 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1632 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1633 case DK_COMM:
1634 case DK_COMMON:
1635 return parseDirectiveComm(/*IsLocal=*/false);
1636 case DK_LCOMM:
1637 return parseDirectiveComm(/*IsLocal=*/true);
1638 case DK_ABORT:
1639 return parseDirectiveAbort();
1640 case DK_INCLUDE:
1641 return parseDirectiveInclude();
1642 case DK_INCBIN:
1643 return parseDirectiveIncbin();
1644 case DK_CODE16:
1645 case DK_CODE16GCC:
1646 return TokError(Twine(IDVal) + " not supported yet");
1647 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001648 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001649 case DK_IRP:
1650 return parseDirectiveIrp(IDLoc);
1651 case DK_IRPC:
1652 return parseDirectiveIrpc(IDLoc);
1653 case DK_ENDR:
1654 return parseDirectiveEndr(IDLoc);
1655 case DK_BUNDLE_ALIGN_MODE:
1656 return parseDirectiveBundleAlignMode();
1657 case DK_BUNDLE_LOCK:
1658 return parseDirectiveBundleLock();
1659 case DK_BUNDLE_UNLOCK:
1660 return parseDirectiveBundleUnlock();
1661 case DK_SLEB128:
1662 return parseDirectiveLEB128(true);
1663 case DK_ULEB128:
1664 return parseDirectiveLEB128(false);
1665 case DK_SPACE:
1666 case DK_SKIP:
1667 return parseDirectiveSpace(IDVal);
1668 case DK_FILE:
1669 return parseDirectiveFile(IDLoc);
1670 case DK_LINE:
1671 return parseDirectiveLine();
1672 case DK_LOC:
1673 return parseDirectiveLoc();
1674 case DK_STABS:
1675 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001676 case DK_CV_FILE:
1677 return parseDirectiveCVFile();
1678 case DK_CV_LOC:
1679 return parseDirectiveCVLoc();
1680 case DK_CV_LINETABLE:
1681 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001682 case DK_CV_INLINE_LINETABLE:
1683 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001684 case DK_CV_DEF_RANGE:
1685 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001686 case DK_CV_STRINGTABLE:
1687 return parseDirectiveCVStringTable();
1688 case DK_CV_FILECHECKSUMS:
1689 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001690 case DK_CFI_SECTIONS:
1691 return parseDirectiveCFISections();
1692 case DK_CFI_STARTPROC:
1693 return parseDirectiveCFIStartProc();
1694 case DK_CFI_ENDPROC:
1695 return parseDirectiveCFIEndProc();
1696 case DK_CFI_DEF_CFA:
1697 return parseDirectiveCFIDefCfa(IDLoc);
1698 case DK_CFI_DEF_CFA_OFFSET:
1699 return parseDirectiveCFIDefCfaOffset();
1700 case DK_CFI_ADJUST_CFA_OFFSET:
1701 return parseDirectiveCFIAdjustCfaOffset();
1702 case DK_CFI_DEF_CFA_REGISTER:
1703 return parseDirectiveCFIDefCfaRegister(IDLoc);
1704 case DK_CFI_OFFSET:
1705 return parseDirectiveCFIOffset(IDLoc);
1706 case DK_CFI_REL_OFFSET:
1707 return parseDirectiveCFIRelOffset(IDLoc);
1708 case DK_CFI_PERSONALITY:
1709 return parseDirectiveCFIPersonalityOrLsda(true);
1710 case DK_CFI_LSDA:
1711 return parseDirectiveCFIPersonalityOrLsda(false);
1712 case DK_CFI_REMEMBER_STATE:
1713 return parseDirectiveCFIRememberState();
1714 case DK_CFI_RESTORE_STATE:
1715 return parseDirectiveCFIRestoreState();
1716 case DK_CFI_SAME_VALUE:
1717 return parseDirectiveCFISameValue(IDLoc);
1718 case DK_CFI_RESTORE:
1719 return parseDirectiveCFIRestore(IDLoc);
1720 case DK_CFI_ESCAPE:
1721 return parseDirectiveCFIEscape();
1722 case DK_CFI_SIGNAL_FRAME:
1723 return parseDirectiveCFISignalFrame();
1724 case DK_CFI_UNDEFINED:
1725 return parseDirectiveCFIUndefined(IDLoc);
1726 case DK_CFI_REGISTER:
1727 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001728 case DK_CFI_WINDOW_SAVE:
1729 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001730 case DK_MACROS_ON:
1731 case DK_MACROS_OFF:
1732 return parseDirectiveMacrosOnOff(IDVal);
1733 case DK_MACRO:
1734 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001735 case DK_EXITM:
1736 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001737 case DK_ENDM:
1738 case DK_ENDMACRO:
1739 return parseDirectiveEndMacro(IDVal);
1740 case DK_PURGEM:
1741 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001742 case DK_END:
1743 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001744 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001745 return parseDirectiveError(IDLoc, false);
1746 case DK_ERROR:
1747 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001748 case DK_WARNING:
1749 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001750 case DK_RELOC:
1751 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001752 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001753
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001754 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001755 }
Chris Lattner36e02122009-06-21 20:54:55 +00001756
Chad Rosierc7f552c2013-02-12 21:33:51 +00001757 // __asm _emit or __asm __emit
1758 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1759 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001760 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001761
1762 // __asm align
1763 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001764 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001765
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001766 if (ParsingInlineAsm && (IDVal == "even"))
1767 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001768 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001769
Chris Lattner7cbfa442010-05-19 23:34:33 +00001770 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001771 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001772 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001773 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001774 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001775 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001776
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001777 // Dump the parsed representation, if requested.
1778 if (getShowParsedOperands()) {
1779 SmallString<256> Str;
1780 raw_svector_ostream OS(Str);
1781 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001782 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001783 if (i != 0)
1784 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001785 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001786 }
1787 OS << "]";
1788
Jim Grosbach4b905842013-09-20 23:08:21 +00001789 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001790 }
1791
Oliver Stannard8b273082014-06-19 15:52:37 +00001792 // If we are generating dwarf for the current section then generate a .loc
1793 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001794 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001795 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001796 getStreamer().getCurrentSection().first)) {
1797 unsigned Line;
1798 if (ActiveMacros.empty())
1799 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1800 else
Frederic Riss16238d92015-06-25 21:57:33 +00001801 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1802 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001803
Eli Bendersky88024712013-01-16 19:32:36 +00001804 // If we previously parsed a cpp hash file line comment then make sure the
1805 // current Dwarf File is for the CppHashFilename if not then emit the
1806 // Dwarf File table for it and adjust the line number for the .loc.
Tim Northoverc0bef992016-04-13 19:46:54 +00001807 if (CppHashInfo.Filename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001808 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
Tim Northoverc0bef992016-04-13 19:46:54 +00001809 0, StringRef(), CppHashInfo.Filename);
David Blaikiec714ef42014-03-17 01:52:11 +00001810 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001811
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1813 // cache with the different Loc from the call above we save the last
1814 // info we queried here with SrcMgr.FindLineNumber().
1815 unsigned CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001816 if (LastQueryIDLoc == CppHashInfo.Loc &&
1817 LastQueryBuffer == CppHashInfo.Buf)
Jim Grosbach4b905842013-09-20 23:08:21 +00001818 CppHashLocLineNo = LastQueryLine;
1819 else {
Tim Northoverc0bef992016-04-13 19:46:54 +00001820 CppHashLocLineNo =
1821 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001822 LastQueryLine = CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001823 LastQueryIDLoc = CppHashInfo.Loc;
1824 LastQueryBuffer = CppHashInfo.Buf;
Jim Grosbach4b905842013-09-20 23:08:21 +00001825 }
Tim Northoverc0bef992016-04-13 19:46:54 +00001826 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001827 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001828
Jim Grosbach4b905842013-09-20 23:08:21 +00001829 getStreamer().EmitDwarfLocDirective(
1830 getContext().getGenDwarfFileNumber(), Line, 0,
1831 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1832 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001833 }
1834
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001835 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001836 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001837 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001838 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1839 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001840 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001841 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001842
Chris Lattnera2a9d162010-09-11 16:18:25 +00001843 // Don't skip the rest of the line, the instruction parser is responsible for
1844 // that.
1845 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001846}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001847
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001848// Parse and erase curly braces marking block start/end
1849bool
1850AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1851 // Identify curly brace marking block start/end
1852 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1853 return false;
1854
1855 SMLoc StartLoc = Lexer.getLoc();
1856 Lex(); // Eat the brace
1857 if (Lexer.is(AsmToken::EndOfStatement))
1858 Lex(); // Eat EndOfStatement following the brace
1859
1860 // Erase the block start/end brace from the output asm string
1861 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
1862 StartLoc.getPointer());
1863 return true;
1864}
1865
Jim Grosbach4b905842013-09-20 23:08:21 +00001866/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001867/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001868void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001869 if (!Lexer.is(AsmToken::EndOfStatement))
1870 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001871 // Eat EOL.
1872 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001873}
1874
Jim Grosbach4b905842013-09-20 23:08:21 +00001875/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001876/// ::= # number "filename"
1877/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001878bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001879 Lex(); // Eat the hash token.
1880
1881 if (getLexer().isNot(AsmToken::Integer)) {
1882 // Consume the line since in cases it is not a well-formed line directive,
1883 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001884 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001885 return false;
1886 }
1887
1888 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001889 Lex();
1890
1891 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001892 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001893 return false;
1894 }
1895
1896 StringRef Filename = getTok().getString();
1897 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001898 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001899
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001900 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
Tim Northoverc0bef992016-04-13 19:46:54 +00001901 CppHashInfo.Loc = L;
1902 CppHashInfo.Filename = Filename;
1903 CppHashInfo.LineNumber = LineNumber;
1904 CppHashInfo.Buf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001905
1906 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001907 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001908 return false;
1909}
1910
Jim Grosbach4b905842013-09-20 23:08:21 +00001911/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001912/// for the Filename and LineNo if any in the diagnostic.
1913void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001914 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001915 raw_ostream &OS = errs();
1916
1917 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001918 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001919 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1920 unsigned CppHashBuf =
Tim Northoverc0bef992016-04-13 19:46:54 +00001921 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001922
Jim Grosbach4b905842013-09-20 23:08:21 +00001923 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001924 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001925 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1926 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1927 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001928 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1929 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001930 }
1931
Eric Christophera7c32732012-12-18 00:30:54 +00001932 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001933 // manager changed or buffer changed (like in a nested include) then just
1934 // print the normal diagnostic using its Filename and LineNo.
Tim Northoverc0bef992016-04-13 19:46:54 +00001935 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001936 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001937 if (Parser->SavedDiagHandler)
1938 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1939 else
Craig Topper353eda42014-04-24 06:44:33 +00001940 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001941 return;
1942 }
1943
Eric Christophera7c32732012-12-18 00:30:54 +00001944 // Use the CppHashFilename and calculate a line number based on the
Tim Northoverc0bef992016-04-13 19:46:54 +00001945 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
1946 // for the diagnostic.
1947 const std::string &Filename = Parser->CppHashInfo.Filename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001948
1949 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1950 int CppHashLocLineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001951 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001952 int LineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001953 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001954
Jim Grosbach4b905842013-09-20 23:08:21 +00001955 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1956 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001957 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001958
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001959 if (Parser->SavedDiagHandler)
1960 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1961 else
Craig Topper353eda42014-04-24 06:44:33 +00001962 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001963}
1964
Rafael Espindola2c064482012-08-21 18:29:30 +00001965// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1966// difference being that that function accepts '@' as part of identifiers and
1967// we can't do that. AsmLexer.cpp should probably be changed to handle
1968// '@' as a special case when needed.
1969static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001970 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1971 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001972}
1973
Rafael Espindola34b9c512012-06-03 23:57:14 +00001974bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001975 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001976 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001977 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001978 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001979 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001980 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001981 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001982
Preston Gurd05500642012-09-19 20:36:12 +00001983 // A macro without parameters is handled differently on Darwin:
1984 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001985 while (!Body.empty()) {
1986 // Scan for the next substitution.
1987 std::size_t End = Body.size(), Pos = 0;
1988 for (; Pos != End; ++Pos) {
1989 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001990 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001991 // This macro has no parameters, look for $0, $1, etc.
1992 if (Body[Pos] != '$' || Pos + 1 == End)
1993 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001994
Rafael Espindola1134ab232011-06-05 02:43:45 +00001995 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001996 if (Next == '$' || Next == 'n' ||
1997 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001998 break;
1999 } else {
2000 // This macro has parameters, look for \foo, \bar, etc.
2001 if (Body[Pos] == '\\' && Pos + 1 != End)
2002 break;
2003 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002004 }
2005
2006 // Add the prefix.
2007 OS << Body.slice(0, Pos);
2008
2009 // Check if we reached the end.
2010 if (Pos == End)
2011 break;
2012
Benjamin Kramer513e7442014-02-20 13:36:32 +00002013 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002014 switch (Body[Pos + 1]) {
2015 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002016 case '$':
2017 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002018 break;
2019
Jim Grosbach4b905842013-09-20 23:08:21 +00002020 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002021 case 'n':
2022 OS << A.size();
2023 break;
2024
Jim Grosbach4b905842013-09-20 23:08:21 +00002025 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002026 default: {
2027 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002028 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002029 if (Index >= A.size())
2030 break;
2031
2032 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002033 for (const AsmToken &Token : A[Index])
2034 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002035 break;
2036 }
2037 }
2038 Pos += 2;
2039 } else {
2040 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002041
2042 // Check for the \@ pseudo-variable.
2043 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002044 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002045 else
2046 while (isIdentifierChar(Body[I]) && I + 1 != End)
2047 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002048
Jim Grosbach4b905842013-09-20 23:08:21 +00002049 const char *Begin = Body.data() + Pos + 1;
2050 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002051 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002052
Toma Tabacu217116e2015-04-27 10:50:29 +00002053 if (Argument == "@") {
2054 OS << NumOfMacroInstantiations;
2055 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002056 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002057 for (; Index < NParameters; ++Index)
2058 if (Parameters[Index].Name == Argument)
2059 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002060
Toma Tabacu217116e2015-04-27 10:50:29 +00002061 if (Index == NParameters) {
2062 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2063 Pos += 3;
2064 else {
2065 OS << '\\' << Argument;
2066 Pos = I;
2067 }
2068 } else {
2069 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002070 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002071 // We expect no quotes around the string's contents when
2072 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002073 if (Token.getKind() != AsmToken::String || VarargParameter)
2074 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002075 else
Craig Topper84008482015-10-10 05:38:14 +00002076 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002077
2078 Pos += 1 + Argument.size();
2079 }
Preston Gurd05500642012-09-19 20:36:12 +00002080 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002081 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002082 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002083 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002084 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002085
Rafael Espindola1134ab232011-06-05 02:43:45 +00002086 return false;
2087}
Daniel Dunbar43235712010-07-18 18:54:11 +00002088
Nico Weber2a8f9222014-07-24 16:29:04 +00002089MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002090 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002091 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002092 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002093
Jim Grosbach4b905842013-09-20 23:08:21 +00002094static bool isOperator(AsmToken::TokenKind kind) {
2095 switch (kind) {
2096 default:
2097 return false;
2098 case AsmToken::Plus:
2099 case AsmToken::Minus:
2100 case AsmToken::Tilde:
2101 case AsmToken::Slash:
2102 case AsmToken::Star:
2103 case AsmToken::Dot:
2104 case AsmToken::Equal:
2105 case AsmToken::EqualEqual:
2106 case AsmToken::Pipe:
2107 case AsmToken::PipePipe:
2108 case AsmToken::Caret:
2109 case AsmToken::Amp:
2110 case AsmToken::AmpAmp:
2111 case AsmToken::Exclaim:
2112 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002113 case AsmToken::Less:
2114 case AsmToken::LessEqual:
2115 case AsmToken::LessLess:
2116 case AsmToken::LessGreater:
2117 case AsmToken::Greater:
2118 case AsmToken::GreaterEqual:
2119 case AsmToken::GreaterGreater:
2120 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002121 }
2122}
2123
David Majnemer16252452014-01-29 00:07:39 +00002124namespace {
2125class AsmLexerSkipSpaceRAII {
2126public:
2127 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2128 Lexer.setSkipSpace(SkipSpace);
2129 }
2130
2131 ~AsmLexerSkipSpaceRAII() {
2132 Lexer.setSkipSpace(true);
2133 }
2134
2135private:
2136 AsmLexer &Lexer;
2137};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002138}
David Majnemer16252452014-01-29 00:07:39 +00002139
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002140bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2141
2142 if (Vararg) {
2143 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2144 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002145 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002146 }
2147 return false;
2148 }
2149
Rafael Espindola768b41c2012-06-15 14:02:34 +00002150 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002151
David Majnemer16252452014-01-29 00:07:39 +00002152 // Darwin doesn't use spaces to delmit arguments.
2153 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002154
Scott Egertona1fa68a2016-02-11 13:48:49 +00002155 bool SpaceEaten;
2156
Rafael Espindola768b41c2012-06-15 14:02:34 +00002157 for (;;) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002158 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002159 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002160 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002161
Scott Egertona1fa68a2016-02-11 13:48:49 +00002162 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002163
Scott Egertona1fa68a2016-02-11 13:48:49 +00002164 if (Lexer.is(AsmToken::Comma))
2165 break;
2166
2167 if (Lexer.is(AsmToken::Space)) {
2168 SpaceEaten = true;
2169 Lex(); // Eat spaces
2170 }
Preston Gurd05500642012-09-19 20:36:12 +00002171
2172 // Spaces can delimit parameters, but could also be part an expression.
2173 // If the token after a space is an operator, add the token and the next
2174 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002175 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002176 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002177 MA.push_back(getTok());
2178 Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002179
Scott Egertona1fa68a2016-02-11 13:48:49 +00002180 // Whitespace after an operator can be ignored.
2181 if (Lexer.is(AsmToken::Space))
2182 Lex();
2183
2184 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002185 }
2186 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002187 if (SpaceEaten)
2188 break;
Preston Gurd05500642012-09-19 20:36:12 +00002189 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002190
Jim Grosbach4b905842013-09-20 23:08:21 +00002191 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002192 // to be able to fill in the remaining default parameter values
2193 if (Lexer.is(AsmToken::EndOfStatement))
2194 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002195
2196 // Adjust the current parentheses level.
2197 if (Lexer.is(AsmToken::LParen))
2198 ++ParenLevel;
2199 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2200 --ParenLevel;
2201
2202 // Append the token to the current argument list.
2203 MA.push_back(getTok());
2204 Lex();
2205 }
Preston Gurd05500642012-09-19 20:36:12 +00002206
Rafael Espindola768b41c2012-06-15 14:02:34 +00002207 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002208 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002209 return false;
2210}
2211
2212// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002213bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002214 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002215 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002216 bool NamedParametersFound = false;
2217 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002218
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002219 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002220 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002221
Rafael Espindola768b41c2012-06-15 14:02:34 +00002222 // Parse two kinds of macro invocations:
2223 // - macros defined without any parameters accept an arbitrary number of them
2224 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002225 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002226 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2227 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002228 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002229 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002230
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002231 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002232 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002233 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002234 eatToEndOfStatement();
2235 return true;
2236 }
2237
2238 if (!Lexer.is(AsmToken::Equal)) {
2239 TokError("expected '=' after formal parameter identifier");
2240 eatToEndOfStatement();
2241 return true;
2242 }
2243 Lex();
2244
2245 NamedParametersFound = true;
2246 }
2247
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002248 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002249 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002250 eatToEndOfStatement();
2251 return true;
2252 }
2253
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002254 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2255 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002256 return true;
2257
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002258 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002259 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002260 unsigned FAI = 0;
2261 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002262 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002263 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002264
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002265 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002266 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002267 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002268 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002269 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002270 return true;
2271 }
2272 PI = FAI;
2273 }
2274
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002275 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002276 if (A.size() <= PI)
2277 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002278 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002279
2280 if (FALocs.size() <= PI)
2281 FALocs.resize(PI + 1);
2282
2283 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002284 }
Jim Grosbach206661622012-07-30 22:44:17 +00002285
Preston Gurd242ed3152012-09-19 20:29:04 +00002286 // At the end of the statement, fill in remaining arguments that have
2287 // default values. If there aren't any, then the next argument is
2288 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002289 if (Lexer.is(AsmToken::EndOfStatement)) {
2290 bool Failure = false;
2291 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2292 if (A[FAI].empty()) {
2293 if (M->Parameters[FAI].Required) {
2294 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2295 "missing value for required parameter "
2296 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2297 Failure = true;
2298 }
2299
2300 if (!M->Parameters[FAI].Value.empty())
2301 A[FAI] = M->Parameters[FAI].Value;
2302 }
2303 }
2304 return Failure;
2305 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002306
2307 if (Lexer.is(AsmToken::Comma))
2308 Lex();
2309 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002310
2311 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002312}
2313
Jim Grosbach4b905842013-09-20 23:08:21 +00002314const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002315 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2316 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002317}
2318
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002319void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2320 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002321}
2322
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002323void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002324
Jim Grosbach4b905842013-09-20 23:08:21 +00002325bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002326 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2327 // this, although we should protect against infinite loops.
2328 if (ActiveMacros.size() == 20)
2329 return TokError("macros cannot be nested more than 20 levels deep");
2330
Eli Bendersky38274122013-01-14 23:22:36 +00002331 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002332 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002333 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002334
Rafael Espindola1134ab232011-06-05 02:43:45 +00002335 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2336 // to hold the macro body with substitutions.
2337 SmallString<256> Buf;
2338 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002339 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002340
Toma Tabacu217116e2015-04-27 10:50:29 +00002341 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002342 return true;
2343
Eli Bendersky38274122013-01-14 23:22:36 +00002344 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002345 // instantiation.
2346 OS << ".endmacro\n";
2347
Rafael Espindola3560ff22014-08-27 20:03:13 +00002348 std::unique_ptr<MemoryBuffer> Instantiation =
2349 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002350
Daniel Dunbar43235712010-07-18 18:54:11 +00002351 // Create the macro instantiation object and add to the current macro
2352 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002353 MacroInstantiation *MI = new MacroInstantiation(
2354 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002355 ActiveMacros.push_back(MI);
2356
Toma Tabacu217116e2015-04-27 10:50:29 +00002357 ++NumOfMacroInstantiations;
2358
Daniel Dunbar43235712010-07-18 18:54:11 +00002359 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002360 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002361 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002362 Lex();
2363
2364 return false;
2365}
2366
Jim Grosbach4b905842013-09-20 23:08:21 +00002367void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002368 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002369 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002370 Lex();
2371
2372 // Pop the instantiation entry.
2373 delete ActiveMacros.back();
2374 ActiveMacros.pop_back();
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002378 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002379 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002380 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002381 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2382 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002383 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002384
Pete Cooper80d21cb2015-06-22 19:35:57 +00002385 if (!Sym) {
2386 // In the case where we parse an expression starting with a '.', we will
2387 // not generate an error, nor will we create a symbol. In this case we
2388 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002389 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002390 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002391
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002392 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002393 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002394 if (NoDeadStrip)
2395 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2396
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002397 return false;
2398}
2399
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002400/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002401/// ::= identifier
2402/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002403bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002404 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002405 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2406 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002407 // handle this as a context dependent token, instead we detect adjacent tokens
2408 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002409 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2410 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002411
Hans Wennborgce69d772013-10-18 20:46:28 +00002412 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002413 Lex();
2414 if (Lexer.isNot(AsmToken::Identifier))
2415 return true;
2416
Hans Wennborgce69d772013-10-18 20:46:28 +00002417 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2418 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002419 return true;
2420
2421 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002422 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002423 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002424 Lex();
2425 return false;
2426 }
2427
Jim Grosbach4b905842013-09-20 23:08:21 +00002428 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002429 return true;
2430
Sean Callanan936b0d32010-01-19 21:44:56 +00002431 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002432
Sean Callanan686ed8d2010-01-19 20:22:31 +00002433 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002434
2435 return false;
2436}
2437
Jim Grosbach4b905842013-09-20 23:08:21 +00002438/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002439/// ::= .equ identifier ',' expression
2440/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002441/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002442bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002443 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002444
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002445 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002446 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002447
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002448 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002449 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002450 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002451
Jim Grosbach4b905842013-09-20 23:08:21 +00002452 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002453}
2454
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002455bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002456 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002457
2458 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002459 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002460 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2461 if (Str[i] != '\\') {
2462 Data += Str[i];
2463 continue;
2464 }
2465
2466 // Recognize escaped characters. Note that this escape semantics currently
2467 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2468 ++i;
2469 if (i == e)
2470 return TokError("unexpected backslash at end of string");
2471
2472 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002473 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002474 // Consume up to three octal characters.
2475 unsigned Value = Str[i] - '0';
2476
Jim Grosbach4b905842013-09-20 23:08:21 +00002477 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002478 ++i;
2479 Value = Value * 8 + (Str[i] - '0');
2480
Jim Grosbach4b905842013-09-20 23:08:21 +00002481 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002482 ++i;
2483 Value = Value * 8 + (Str[i] - '0');
2484 }
2485 }
2486
2487 if (Value > 255)
2488 return TokError("invalid octal escape sequence (out of range)");
2489
Jim Grosbach4b905842013-09-20 23:08:21 +00002490 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002491 continue;
2492 }
2493
2494 // Otherwise recognize individual escapes.
2495 switch (Str[i]) {
2496 default:
2497 // Just reject invalid escape sequences for now.
2498 return TokError("invalid escape sequence (unrecognized character)");
2499
2500 case 'b': Data += '\b'; break;
2501 case 'f': Data += '\f'; break;
2502 case 'n': Data += '\n'; break;
2503 case 'r': Data += '\r'; break;
2504 case 't': Data += '\t'; break;
2505 case '"': Data += '"'; break;
2506 case '\\': Data += '\\'; break;
2507 }
2508 }
2509
2510 return false;
2511}
2512
Jim Grosbach4b905842013-09-20 23:08:21 +00002513/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002514/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002515bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002516 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002517 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002518
Daniel Dunbara10e5192009-06-24 23:30:00 +00002519 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002520 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002521 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002522
Daniel Dunbaref668c12009-08-14 18:19:52 +00002523 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002525 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002526
Rafael Espindola64e1af82013-07-02 15:49:13 +00002527 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002528 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002529 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002530
Sean Callanan686ed8d2010-01-19 20:22:31 +00002531 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002532
2533 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002534 break;
2535
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002536 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002537 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002538 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539 }
2540 }
2541
Sean Callanan686ed8d2010-01-19 20:22:31 +00002542 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002543 return false;
2544}
2545
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002546/// parseDirectiveReloc
2547/// ::= .reloc expression , identifier [ , expression ]
2548bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2549 const MCExpr *Offset;
2550 const MCExpr *Expr = nullptr;
2551
2552 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2553 if (parseExpression(Offset))
2554 return true;
2555
2556 // We can only deal with constant expressions at the moment.
2557 int64_t OffsetValue;
2558 if (!Offset->evaluateAsAbsolute(OffsetValue))
2559 return Error(OffsetLoc, "expression is not a constant value");
2560
David Majnemerce108422016-01-19 23:05:27 +00002561 if (OffsetValue < 0)
2562 return Error(OffsetLoc, "expression is negative");
2563
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002564 if (Lexer.isNot(AsmToken::Comma))
2565 return TokError("expected comma");
2566 Lexer.Lex();
2567
2568 if (Lexer.isNot(AsmToken::Identifier))
2569 return TokError("expected relocation name");
2570 SMLoc NameLoc = Lexer.getTok().getLoc();
2571 StringRef Name = Lexer.getTok().getIdentifier();
2572 Lexer.Lex();
2573
2574 if (Lexer.is(AsmToken::Comma)) {
2575 Lexer.Lex();
2576 SMLoc ExprLoc = Lexer.getLoc();
2577 if (parseExpression(Expr))
2578 return true;
2579
2580 MCValue Value;
2581 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2582 return Error(ExprLoc, "expression must be relocatable");
2583 }
2584
2585 if (Lexer.isNot(AsmToken::EndOfStatement))
2586 return TokError("unexpected token in .reloc directive");
2587
2588 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2589 return Error(NameLoc, "unknown relocation name");
2590
2591 return false;
2592}
2593
Jim Grosbach4b905842013-09-20 23:08:21 +00002594/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002595/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002596bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002597 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002598 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002599
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002601 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002602 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002603 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002604 return true;
2605
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002606 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002607 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2608 assert(Size <= 8 && "Invalid size");
2609 uint64_t IntValue = MCE->getValue();
2610 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2611 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002612 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002613 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002614 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002615
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002616 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002617 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002618
Daniel Dunbara10e5192009-06-24 23:30:00 +00002619 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002620 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002621 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002622 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002623 }
2624 }
2625
Sean Callanan686ed8d2010-01-19 20:22:31 +00002626 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002627 return false;
2628}
2629
David Woodhoused6de0d92014-02-01 16:20:59 +00002630/// ParseDirectiveOctaValue
2631/// ::= .octa [ hexconstant (, hexconstant)* ]
2632bool AsmParser::parseDirectiveOctaValue() {
2633 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2634 checkForValidSection();
2635
2636 for (;;) {
2637 if (Lexer.getKind() == AsmToken::Error)
2638 return true;
2639 if (Lexer.getKind() != AsmToken::Integer &&
2640 Lexer.getKind() != AsmToken::BigNum)
2641 return TokError("unknown token in expression");
2642
2643 SMLoc ExprLoc = getLexer().getLoc();
2644 APInt IntValue = getTok().getAPIntVal();
2645 Lex();
2646
2647 uint64_t hi, lo;
2648 if (IntValue.isIntN(64)) {
2649 hi = 0;
2650 lo = IntValue.getZExtValue();
2651 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002652 // It might actually have more than 128 bits, but the top ones are zero.
2653 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002654 lo = IntValue.getLoBits(64).getZExtValue();
2655 } else
2656 return Error(ExprLoc, "literal value out of range for directive");
2657
2658 if (MAI.isLittleEndian()) {
2659 getStreamer().EmitIntValue(lo, 8);
2660 getStreamer().EmitIntValue(hi, 8);
2661 } else {
2662 getStreamer().EmitIntValue(hi, 8);
2663 getStreamer().EmitIntValue(lo, 8);
2664 }
2665
2666 if (getLexer().is(AsmToken::EndOfStatement))
2667 break;
2668
2669 // FIXME: Improve diagnostic.
2670 if (getLexer().isNot(AsmToken::Comma))
2671 return TokError("unexpected token in directive");
2672 Lex();
2673 }
2674 }
2675
2676 Lex();
2677 return false;
2678}
2679
Jim Grosbach4b905842013-09-20 23:08:21 +00002680/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002681/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002682bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002683 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002684 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002685
2686 for (;;) {
2687 // We don't truly support arithmetic on floating point expressions, so we
2688 // have to manually parse unary prefixes.
2689 bool IsNeg = false;
2690 if (getLexer().is(AsmToken::Minus)) {
2691 Lex();
2692 IsNeg = true;
2693 } else if (getLexer().is(AsmToken::Plus))
2694 Lex();
2695
Michael J. Spencer530ce852010-10-09 11:00:50 +00002696 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002697 getLexer().isNot(AsmToken::Real) &&
2698 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002699 return TokError("unexpected token in directive");
2700
2701 // Convert to an APFloat.
2702 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002703 StringRef IDVal = getTok().getString();
2704 if (getLexer().is(AsmToken::Identifier)) {
2705 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2706 Value = APFloat::getInf(Semantics);
2707 else if (!IDVal.compare_lower("nan"))
2708 Value = APFloat::getNaN(Semantics, false, ~0);
2709 else
2710 return TokError("invalid floating point literal");
2711 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002712 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002713 return TokError("invalid floating point literal");
2714 if (IsNeg)
2715 Value.changeSign();
2716
2717 // Consume the numeric token.
2718 Lex();
2719
2720 // Emit the value as an integer.
2721 APInt AsInt = Value.bitcastToAPInt();
2722 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002723 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002724
2725 if (getLexer().is(AsmToken::EndOfStatement))
2726 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002727
Daniel Dunbar2af16532010-09-24 01:59:56 +00002728 if (getLexer().isNot(AsmToken::Comma))
2729 return TokError("unexpected token in directive");
2730 Lex();
2731 }
2732 }
2733
2734 Lex();
2735 return false;
2736}
2737
Jim Grosbach4b905842013-09-20 23:08:21 +00002738/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002739/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002740bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002741 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002742
2743 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002744 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002745 return true;
2746
Rafael Espindolab91bac62010-10-05 19:42:57 +00002747 int64_t Val = 0;
2748 if (getLexer().is(AsmToken::Comma)) {
2749 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002750 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002751 return true;
2752 }
2753
Rafael Espindola922e3f42010-09-16 15:03:59 +00002754 if (getLexer().isNot(AsmToken::EndOfStatement))
2755 return TokError("unexpected token in '.zero' directive");
2756
2757 Lex();
2758
Rafael Espindola64e1af82013-07-02 15:49:13 +00002759 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002760
2761 return false;
2762}
2763
Jim Grosbach4b905842013-09-20 23:08:21 +00002764/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002765/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002766bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002767 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002768
David Majnemer522d3db2014-02-01 07:19:38 +00002769 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002770 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002771 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002772 return true;
2773
David Majnemer522d3db2014-02-01 07:19:38 +00002774 if (NumValues < 0) {
2775 Warning(RepeatLoc,
2776 "'.fill' directive with negative repeat count has no effect");
2777 NumValues = 0;
2778 }
2779
Roman Divackye33098f2013-09-24 17:44:41 +00002780 int64_t FillSize = 1;
2781 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002782
David Majnemer522d3db2014-02-01 07:19:38 +00002783 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002784 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2785 if (getLexer().isNot(AsmToken::Comma))
2786 return TokError("unexpected token in '.fill' directive");
2787 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002788
David Majnemer522d3db2014-02-01 07:19:38 +00002789 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002790 if (parseAbsoluteExpression(FillSize))
2791 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002792
Roman Divackye33098f2013-09-24 17:44:41 +00002793 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2794 if (getLexer().isNot(AsmToken::Comma))
2795 return TokError("unexpected token in '.fill' directive");
2796 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002797
David Majnemer522d3db2014-02-01 07:19:38 +00002798 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002799 if (parseAbsoluteExpression(FillExpr))
2800 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002801
Roman Divackye33098f2013-09-24 17:44:41 +00002802 if (getLexer().isNot(AsmToken::EndOfStatement))
2803 return TokError("unexpected token in '.fill' directive");
2804
2805 Lex();
2806 }
2807 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002808
David Majnemer522d3db2014-02-01 07:19:38 +00002809 if (FillSize < 0) {
2810 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2811 NumValues = 0;
2812 }
2813 if (FillSize > 8) {
2814 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2815 FillSize = 8;
2816 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002817
David Majnemer522d3db2014-02-01 07:19:38 +00002818 if (!isUInt<32>(FillExpr) && FillSize > 4)
2819 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2820
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002821 if (NumValues > 0) {
2822 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2823 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2824 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2825 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2826 if (NonZeroFillSize < FillSize)
2827 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2828 }
David Majnemer522d3db2014-02-01 07:19:38 +00002829 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002830
2831 return false;
2832}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002833
Jim Grosbach4b905842013-09-20 23:08:21 +00002834/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002835/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002836bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002837 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002838
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002839 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002840 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002841 return true;
2842
2843 // Parse optional fill expression.
2844 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002845 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2846 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002847 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002848 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002849
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002850 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002851 return true;
2852
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002853 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002854 return TokError("unexpected token in '.org' directive");
2855 }
2856
Sean Callanan686ed8d2010-01-19 20:22:31 +00002857 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002858 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002859 return false;
2860}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002861
Jim Grosbach4b905842013-09-20 23:08:21 +00002862/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002863/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002864bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002865 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002866
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002867 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002868 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002869 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002870 return true;
2871
2872 SMLoc MaxBytesLoc;
2873 bool HasFillExpr = false;
2874 int64_t FillExpr = 0;
2875 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002876 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2877 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002878 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002879 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002880
2881 // The fill expression can be omitted while specifying a maximum number of
2882 // alignment bytes, e.g:
2883 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002884 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002885 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002886 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002887 return true;
2888 }
2889
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002890 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2891 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002892 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002893 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002894
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002895 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002896 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002897 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002898
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002899 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002900 return TokError("unexpected token in directive");
2901 }
2902 }
2903
Sean Callanan686ed8d2010-01-19 20:22:31 +00002904 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002905
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002906 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002907 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002908
2909 // Compute alignment in bytes.
2910 if (IsPow2) {
2911 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002912 if (Alignment >= 32) {
2913 Error(AlignmentLoc, "invalid alignment value");
2914 Alignment = 31;
2915 }
2916
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002917 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002918 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002919 // Reject alignments that aren't either a power of two or zero,
2920 // for gas compatibility. Alignment of zero is silently rounded
2921 // up to one.
2922 if (Alignment == 0)
2923 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002924 if (!isPowerOf2_64(Alignment))
2925 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002926 }
2927
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002928 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002929 if (MaxBytesLoc.isValid()) {
2930 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002931 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002932 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002933 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002934 }
2935
2936 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002937 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002938 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002939 MaxBytesToFill = 0;
2940 }
2941 }
2942
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002943 // Check whether we should use optimal code alignment for this .align
2944 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002945 const MCSection *Section = getStreamer().getCurrentSection().first;
2946 assert(Section && "must have section to emit alignment");
2947 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002948 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2949 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002950 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002951 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002952 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002953 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2954 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002955 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002956
2957 return false;
2958}
2959
Jim Grosbach4b905842013-09-20 23:08:21 +00002960/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002961/// ::= .file [number] filename
2962/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002964 // FIXME: I'm not sure what this is.
2965 int64_t FileNumber = -1;
2966 SMLoc FileNumberLoc = getLexer().getLoc();
2967 if (getLexer().is(AsmToken::Integer)) {
2968 FileNumber = getTok().getIntVal();
2969 Lex();
2970
2971 if (FileNumber < 1)
2972 return TokError("file number less than one");
2973 }
2974
2975 if (getLexer().isNot(AsmToken::String))
2976 return TokError("unexpected token in '.file' directive");
2977
2978 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002979 // Allow the strings to have escaped octal character sequence.
2980 std::string Path = getTok().getString();
2981 if (parseEscapedString(Path))
2982 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002983 Lex();
2984
2985 StringRef Directory;
2986 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002987 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002988 if (getLexer().is(AsmToken::String)) {
2989 if (FileNumber == -1)
2990 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002991 if (parseEscapedString(FilenameData))
2992 return true;
2993 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002994 Directory = Path;
2995 Lex();
2996 } else {
2997 Filename = Path;
2998 }
2999
3000 if (getLexer().isNot(AsmToken::EndOfStatement))
3001 return TokError("unexpected token in '.file' directive");
3002
3003 if (FileNumber == -1)
3004 getStreamer().EmitFileDirective(Filename);
3005 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00003006 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00003007 Error(DirectiveLoc,
3008 "input can't have .file dwarf directives when -g is "
3009 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00003010
David Blaikiec714ef42014-03-17 01:52:11 +00003011 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
3012 0)
Eli Bendersky17233942013-01-15 22:59:42 +00003013 Error(FileNumberLoc, "file number already allocated");
3014 }
3015
3016 return false;
3017}
3018
Jim Grosbach4b905842013-09-20 23:08:21 +00003019/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003020/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003021bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00003022 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3023 if (getLexer().isNot(AsmToken::Integer))
3024 return TokError("unexpected token in '.line' directive");
3025
3026 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00003027 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003028 Lex();
3029
3030 // FIXME: Do something with the .line.
3031 }
3032
3033 if (getLexer().isNot(AsmToken::EndOfStatement))
3034 return TokError("unexpected token in '.line' directive");
3035
3036 return false;
3037}
3038
Jim Grosbach4b905842013-09-20 23:08:21 +00003039/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003040/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3041/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3042/// The first number is a file number, must have been previously assigned with
3043/// a .file directive, the second number is the line number and optionally the
3044/// third number is a column position (zero if not specified). The remaining
3045/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003046bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003047 if (getLexer().isNot(AsmToken::Integer))
3048 return TokError("unexpected token in '.loc' directive");
3049 int64_t FileNumber = getTok().getIntVal();
3050 if (FileNumber < 1)
3051 return TokError("file number less than one in '.loc' directive");
3052 if (!getContext().isValidDwarfFileNumber(FileNumber))
3053 return TokError("unassigned file number in '.loc' directive");
3054 Lex();
3055
3056 int64_t LineNumber = 0;
3057 if (getLexer().is(AsmToken::Integer)) {
3058 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003059 if (LineNumber < 0)
3060 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003061 Lex();
3062 }
3063
3064 int64_t ColumnPos = 0;
3065 if (getLexer().is(AsmToken::Integer)) {
3066 ColumnPos = getTok().getIntVal();
3067 if (ColumnPos < 0)
3068 return TokError("column position less than zero in '.loc' directive");
3069 Lex();
3070 }
3071
3072 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3073 unsigned Isa = 0;
3074 int64_t Discriminator = 0;
3075 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3076 for (;;) {
3077 if (getLexer().is(AsmToken::EndOfStatement))
3078 break;
3079
3080 StringRef Name;
3081 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003082 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003083 return TokError("unexpected token in '.loc' directive");
3084
3085 if (Name == "basic_block")
3086 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3087 else if (Name == "prologue_end")
3088 Flags |= DWARF2_FLAG_PROLOGUE_END;
3089 else if (Name == "epilogue_begin")
3090 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3091 else if (Name == "is_stmt") {
3092 Loc = getTok().getLoc();
3093 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003094 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003095 return true;
3096 // The expression must be the constant 0 or 1.
3097 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3098 int Value = MCE->getValue();
3099 if (Value == 0)
3100 Flags &= ~DWARF2_FLAG_IS_STMT;
3101 else if (Value == 1)
3102 Flags |= DWARF2_FLAG_IS_STMT;
3103 else
3104 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003105 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003106 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3107 }
Craig Topperf15655b2013-04-22 04:22:40 +00003108 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003109 Loc = getTok().getLoc();
3110 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003111 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003112 return true;
3113 // The expression must be a constant greater or equal to 0.
3114 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3115 int Value = MCE->getValue();
3116 if (Value < 0)
3117 return Error(Loc, "isa number less than zero");
3118 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003119 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003120 return Error(Loc, "isa number not a constant value");
3121 }
Craig Topperf15655b2013-04-22 04:22:40 +00003122 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003123 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003124 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003125 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003126 return Error(Loc, "unknown sub-directive in '.loc' directive");
3127 }
3128
3129 if (getLexer().is(AsmToken::EndOfStatement))
3130 break;
3131 }
3132 }
3133
3134 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3135 Isa, Discriminator, StringRef());
3136
3137 return false;
3138}
3139
Jim Grosbach4b905842013-09-20 23:08:21 +00003140/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003141/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003142bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003143 return TokError("unsupported directive '.stabs'");
3144}
3145
Reid Kleckner2214ed82016-01-29 00:49:42 +00003146/// parseDirectiveCVFile
3147/// ::= .cv_file number filename
3148bool AsmParser::parseDirectiveCVFile() {
3149 SMLoc FileNumberLoc = getLexer().getLoc();
3150 if (getLexer().isNot(AsmToken::Integer))
3151 return TokError("expected file number in '.cv_file' directive");
3152
3153 int64_t FileNumber = getTok().getIntVal();
3154 Lex();
3155
3156 if (FileNumber < 1)
3157 return TokError("file number less than one");
3158
3159 if (getLexer().isNot(AsmToken::String))
3160 return TokError("unexpected token in '.cv_file' directive");
3161
3162 // Usually the directory and filename together, otherwise just the directory.
3163 // Allow the strings to have escaped octal character sequence.
3164 std::string Filename;
3165 if (parseEscapedString(Filename))
3166 return true;
3167 Lex();
3168
3169 if (getLexer().isNot(AsmToken::EndOfStatement))
3170 return TokError("unexpected token in '.cv_file' directive");
3171
3172 if (getStreamer().EmitCVFileDirective(FileNumber, Filename) == 0)
3173 Error(FileNumberLoc, "file number already allocated");
3174
3175 return false;
3176}
3177
3178/// parseDirectiveCVLoc
3179/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3180/// [is_stmt VALUE]
3181/// The first number is a file number, must have been previously assigned with
3182/// a .file directive, the second number is the line number and optionally the
3183/// third number is a column position (zero if not specified). The remaining
3184/// optional items are .loc sub-directives.
3185bool AsmParser::parseDirectiveCVLoc() {
3186 if (getLexer().isNot(AsmToken::Integer))
3187 return TokError("unexpected token in '.cv_loc' directive");
3188
3189 int64_t FunctionId = getTok().getIntVal();
3190 if (FunctionId < 0)
3191 return TokError("function id less than zero in '.cv_loc' directive");
3192 Lex();
3193
3194 int64_t FileNumber = getTok().getIntVal();
3195 if (FileNumber < 1)
3196 return TokError("file number less than one in '.cv_loc' directive");
3197 if (!getContext().isValidCVFileNumber(FileNumber))
3198 return TokError("unassigned file number in '.cv_loc' directive");
3199 Lex();
3200
3201 int64_t LineNumber = 0;
3202 if (getLexer().is(AsmToken::Integer)) {
3203 LineNumber = getTok().getIntVal();
3204 if (LineNumber < 0)
3205 return TokError("line number less than zero in '.cv_loc' directive");
3206 Lex();
3207 }
3208
3209 int64_t ColumnPos = 0;
3210 if (getLexer().is(AsmToken::Integer)) {
3211 ColumnPos = getTok().getIntVal();
3212 if (ColumnPos < 0)
3213 return TokError("column position less than zero in '.cv_loc' directive");
3214 Lex();
3215 }
3216
3217 bool PrologueEnd = false;
3218 uint64_t IsStmt = 0;
3219 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3220 StringRef Name;
3221 SMLoc Loc = getTok().getLoc();
3222 if (parseIdentifier(Name))
3223 return TokError("unexpected token in '.cv_loc' directive");
3224
3225 if (Name == "prologue_end")
3226 PrologueEnd = true;
3227 else if (Name == "is_stmt") {
3228 Loc = getTok().getLoc();
3229 const MCExpr *Value;
3230 if (parseExpression(Value))
3231 return true;
3232 // The expression must be the constant 0 or 1.
3233 IsStmt = ~0ULL;
3234 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3235 IsStmt = MCE->getValue();
3236
3237 if (IsStmt > 1)
3238 return Error(Loc, "is_stmt value not 0 or 1");
3239 } else {
3240 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3241 }
3242 }
3243
3244 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
3245 ColumnPos, PrologueEnd, IsStmt, StringRef());
3246 return false;
3247}
3248
3249/// parseDirectiveCVLinetable
3250/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3251bool AsmParser::parseDirectiveCVLinetable() {
3252 int64_t FunctionId = getTok().getIntVal();
3253 if (FunctionId < 0)
3254 return TokError("function id less than zero in '.cv_linetable' directive");
3255 Lex();
3256
3257 if (Lexer.isNot(AsmToken::Comma))
3258 return TokError("unexpected token in '.cv_linetable' directive");
3259 Lex();
3260
3261 SMLoc Loc = getLexer().getLoc();
3262 StringRef FnStartName;
3263 if (parseIdentifier(FnStartName))
3264 return Error(Loc, "expected identifier in directive");
3265
3266 if (Lexer.isNot(AsmToken::Comma))
3267 return TokError("unexpected token in '.cv_linetable' directive");
3268 Lex();
3269
3270 Loc = getLexer().getLoc();
3271 StringRef FnEndName;
3272 if (parseIdentifier(FnEndName))
3273 return Error(Loc, "expected identifier in directive");
3274
3275 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3276 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3277
3278 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3279 return false;
3280}
3281
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003282/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003283/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003284/// ("contains" SecondaryFunctionId+)?
3285bool AsmParser::parseDirectiveCVInlineLinetable() {
3286 int64_t PrimaryFunctionId = getTok().getIntVal();
3287 if (PrimaryFunctionId < 0)
3288 return TokError(
3289 "function id less than zero in '.cv_inline_linetable' directive");
3290 Lex();
3291
3292 int64_t SourceFileId = getTok().getIntVal();
3293 if (SourceFileId <= 0)
3294 return TokError(
3295 "File id less than zero in '.cv_inline_linetable' directive");
3296 Lex();
3297
3298 int64_t SourceLineNum = getTok().getIntVal();
3299 if (SourceLineNum < 0)
3300 return TokError(
3301 "Line number less than zero in '.cv_inline_linetable' directive");
3302 Lex();
3303
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003304 SMLoc Loc = getLexer().getLoc();
3305 StringRef FnStartName;
3306 if (parseIdentifier(FnStartName))
3307 return Error(Loc, "expected identifier in directive");
3308 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3309
David Majnemerc9911f22016-02-02 19:22:34 +00003310 Loc = getLexer().getLoc();
3311 StringRef FnEndName;
3312 if (parseIdentifier(FnEndName))
3313 return Error(Loc, "expected identifier in directive");
3314 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3315
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003316 SmallVector<unsigned, 8> SecondaryFunctionIds;
3317 if (getLexer().is(AsmToken::Identifier)) {
3318 if (getTok().getIdentifier() != "contains")
3319 return TokError(
3320 "unexpected identifier in '.cv_inline_linetable' directive");
3321 Lex();
3322
3323 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3324 int64_t SecondaryFunctionId = getTok().getIntVal();
3325 if (SecondaryFunctionId < 0)
3326 return TokError(
3327 "function id less than zero in '.cv_inline_linetable' directive");
3328 Lex();
3329
3330 SecondaryFunctionIds.push_back(SecondaryFunctionId);
3331 }
3332 }
3333
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003334 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3335 SourceLineNum, FnStartSym,
David Majnemerc9911f22016-02-02 19:22:34 +00003336 FnEndSym, SecondaryFunctionIds);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003337 return false;
3338}
3339
David Majnemer408b5e62016-02-05 01:55:49 +00003340/// parseDirectiveCVDefRange
3341/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3342bool AsmParser::parseDirectiveCVDefRange() {
3343 SMLoc Loc;
3344 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3345 while (getLexer().is(AsmToken::Identifier)) {
3346 Loc = getLexer().getLoc();
3347 StringRef GapStartName;
3348 if (parseIdentifier(GapStartName))
3349 return Error(Loc, "expected identifier in directive");
3350 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3351
3352 Loc = getLexer().getLoc();
3353 StringRef GapEndName;
3354 if (parseIdentifier(GapEndName))
3355 return Error(Loc, "expected identifier in directive");
3356 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3357
3358 Ranges.push_back({GapStartSym, GapEndSym});
3359 }
3360
3361 if (getLexer().isNot(AsmToken::Comma))
3362 return TokError("unexpected token in directive");
3363 Lex();
3364
3365 std::string FixedSizePortion;
3366 if (parseEscapedString(FixedSizePortion))
3367 return true;
3368 Lex();
3369
3370 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3371 return false;
3372}
3373
Reid Kleckner2214ed82016-01-29 00:49:42 +00003374/// parseDirectiveCVStringTable
3375/// ::= .cv_stringtable
3376bool AsmParser::parseDirectiveCVStringTable() {
3377 getStreamer().EmitCVStringTableDirective();
3378 return false;
3379}
3380
3381/// parseDirectiveCVFileChecksums
3382/// ::= .cv_filechecksums
3383bool AsmParser::parseDirectiveCVFileChecksums() {
3384 getStreamer().EmitCVFileChecksumsDirective();
3385 return false;
3386}
3387
Jim Grosbach4b905842013-09-20 23:08:21 +00003388/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003389/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003390bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003391 StringRef Name;
3392 bool EH = false;
3393 bool Debug = false;
3394
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003395 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003396 return TokError("Expected an identifier");
3397
3398 if (Name == ".eh_frame")
3399 EH = true;
3400 else if (Name == ".debug_frame")
3401 Debug = true;
3402
3403 if (getLexer().is(AsmToken::Comma)) {
3404 Lex();
3405
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003406 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003407 return TokError("Expected an identifier");
3408
3409 if (Name == ".eh_frame")
3410 EH = true;
3411 else if (Name == ".debug_frame")
3412 Debug = true;
3413 }
3414
3415 getStreamer().EmitCFISections(EH, Debug);
3416 return false;
3417}
3418
Jim Grosbach4b905842013-09-20 23:08:21 +00003419/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003420/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003421bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003422 StringRef Simple;
3423 if (getLexer().isNot(AsmToken::EndOfStatement))
3424 if (parseIdentifier(Simple) || Simple != "simple")
3425 return TokError("unexpected token in .cfi_startproc directive");
3426
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003427 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003428 return false;
3429}
3430
Jim Grosbach4b905842013-09-20 23:08:21 +00003431/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003432/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003433bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003434 getStreamer().EmitCFIEndProc();
3435 return false;
3436}
3437
Jim Grosbach4b905842013-09-20 23:08:21 +00003438/// \brief parse register name or number.
3439bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003440 SMLoc DirectiveLoc) {
3441 unsigned RegNo;
3442
3443 if (getLexer().isNot(AsmToken::Integer)) {
3444 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3445 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003446 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003447 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003448 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003449
3450 return false;
3451}
3452
Jim Grosbach4b905842013-09-20 23:08:21 +00003453/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003454/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003455bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003456 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003458 return true;
3459
3460 if (getLexer().isNot(AsmToken::Comma))
3461 return TokError("unexpected token in directive");
3462 Lex();
3463
3464 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003465 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003466 return true;
3467
3468 getStreamer().EmitCFIDefCfa(Register, Offset);
3469 return false;
3470}
3471
Jim Grosbach4b905842013-09-20 23:08:21 +00003472/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003473/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003474bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003475 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003476 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003477 return true;
3478
3479 getStreamer().EmitCFIDefCfaOffset(Offset);
3480 return false;
3481}
3482
Jim Grosbach4b905842013-09-20 23:08:21 +00003483/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003484/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003485bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003486 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003487 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003488 return true;
3489
3490 if (getLexer().isNot(AsmToken::Comma))
3491 return TokError("unexpected token in directive");
3492 Lex();
3493
3494 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003495 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003496 return true;
3497
3498 getStreamer().EmitCFIRegister(Register1, Register2);
3499 return false;
3500}
3501
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003502/// parseDirectiveCFIWindowSave
3503/// ::= .cfi_window_save
3504bool AsmParser::parseDirectiveCFIWindowSave() {
3505 getStreamer().EmitCFIWindowSave();
3506 return false;
3507}
3508
Jim Grosbach4b905842013-09-20 23:08:21 +00003509/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003510/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003511bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003512 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003513 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003514 return true;
3515
3516 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3517 return false;
3518}
3519
Jim Grosbach4b905842013-09-20 23:08:21 +00003520/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003521/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003522bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003523 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003524 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003525 return true;
3526
3527 getStreamer().EmitCFIDefCfaRegister(Register);
3528 return false;
3529}
3530
Jim Grosbach4b905842013-09-20 23:08:21 +00003531/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003532/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003533bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003534 int64_t Register = 0;
3535 int64_t Offset = 0;
3536
Jim Grosbach4b905842013-09-20 23:08:21 +00003537 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003538 return true;
3539
3540 if (getLexer().isNot(AsmToken::Comma))
3541 return TokError("unexpected token in directive");
3542 Lex();
3543
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003545 return true;
3546
3547 getStreamer().EmitCFIOffset(Register, Offset);
3548 return false;
3549}
3550
Jim Grosbach4b905842013-09-20 23:08:21 +00003551/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003552/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003553bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003554 int64_t Register = 0;
3555
Jim Grosbach4b905842013-09-20 23:08:21 +00003556 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003557 return true;
3558
3559 if (getLexer().isNot(AsmToken::Comma))
3560 return TokError("unexpected token in directive");
3561 Lex();
3562
3563 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003564 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003565 return true;
3566
3567 getStreamer().EmitCFIRelOffset(Register, Offset);
3568 return false;
3569}
3570
3571static bool isValidEncoding(int64_t Encoding) {
3572 if (Encoding & ~0xff)
3573 return false;
3574
3575 if (Encoding == dwarf::DW_EH_PE_omit)
3576 return true;
3577
3578 const unsigned Format = Encoding & 0xf;
3579 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3580 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3581 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3582 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3583 return false;
3584
3585 const unsigned Application = Encoding & 0x70;
3586 if (Application != dwarf::DW_EH_PE_absptr &&
3587 Application != dwarf::DW_EH_PE_pcrel)
3588 return false;
3589
3590 return true;
3591}
3592
Jim Grosbach4b905842013-09-20 23:08:21 +00003593/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003594/// IsPersonality true for cfi_personality, false for cfi_lsda
3595/// ::= .cfi_personality encoding, [symbol_name]
3596/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003597bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003598 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003599 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003600 return true;
3601 if (Encoding == dwarf::DW_EH_PE_omit)
3602 return false;
3603
3604 if (!isValidEncoding(Encoding))
3605 return TokError("unsupported encoding.");
3606
3607 if (getLexer().isNot(AsmToken::Comma))
3608 return TokError("unexpected token in directive");
3609 Lex();
3610
3611 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003612 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003613 return TokError("expected identifier in directive");
3614
Jim Grosbach6f482002015-05-18 18:43:14 +00003615 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003616
3617 if (IsPersonality)
3618 getStreamer().EmitCFIPersonality(Sym, Encoding);
3619 else
3620 getStreamer().EmitCFILsda(Sym, Encoding);
3621 return false;
3622}
3623
Jim Grosbach4b905842013-09-20 23:08:21 +00003624/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003625/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003626bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003627 getStreamer().EmitCFIRememberState();
3628 return false;
3629}
3630
Jim Grosbach4b905842013-09-20 23:08:21 +00003631/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003632/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003633bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003634 getStreamer().EmitCFIRestoreState();
3635 return false;
3636}
3637
Jim Grosbach4b905842013-09-20 23:08:21 +00003638/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003639/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003640bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003641 int64_t Register = 0;
3642
Jim Grosbach4b905842013-09-20 23:08:21 +00003643 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003644 return true;
3645
3646 getStreamer().EmitCFISameValue(Register);
3647 return false;
3648}
3649
Jim Grosbach4b905842013-09-20 23:08:21 +00003650/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003651/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003652bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003653 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003654 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003655 return true;
3656
3657 getStreamer().EmitCFIRestore(Register);
3658 return false;
3659}
3660
Jim Grosbach4b905842013-09-20 23:08:21 +00003661/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003662/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003663bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003664 std::string Values;
3665 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003666 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003667 return true;
3668
3669 Values.push_back((uint8_t)CurrValue);
3670
3671 while (getLexer().is(AsmToken::Comma)) {
3672 Lex();
3673
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003675 return true;
3676
3677 Values.push_back((uint8_t)CurrValue);
3678 }
3679
3680 getStreamer().EmitCFIEscape(Values);
3681 return false;
3682}
3683
Jim Grosbach4b905842013-09-20 23:08:21 +00003684/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003685/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003686bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003687 if (getLexer().isNot(AsmToken::EndOfStatement))
3688 return Error(getLexer().getLoc(),
3689 "unexpected token in '.cfi_signal_frame'");
3690
3691 getStreamer().EmitCFISignalFrame();
3692 return false;
3693}
3694
Jim Grosbach4b905842013-09-20 23:08:21 +00003695/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003696/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003697bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003698 int64_t Register = 0;
3699
Jim Grosbach4b905842013-09-20 23:08:21 +00003700 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003701 return true;
3702
3703 getStreamer().EmitCFIUndefined(Register);
3704 return false;
3705}
3706
Jim Grosbach4b905842013-09-20 23:08:21 +00003707/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003708/// ::= .macros_on
3709/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003710bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003711 if (getLexer().isNot(AsmToken::EndOfStatement))
3712 return Error(getLexer().getLoc(),
3713 "unexpected token in '" + Directive + "' directive");
3714
Jim Grosbach4b905842013-09-20 23:08:21 +00003715 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003716 return false;
3717}
3718
Jim Grosbach4b905842013-09-20 23:08:21 +00003719/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003720/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003721bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003722 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003723 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003724 return TokError("expected identifier in '.macro' directive");
3725
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003726 if (getLexer().is(AsmToken::Comma))
3727 Lex();
3728
Eli Bendersky17233942013-01-15 22:59:42 +00003729 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003730 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003731
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003732 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003733 return Error(Lexer.getLoc(),
3734 "Vararg parameter '" + Parameters.back().Name +
3735 "' should be last one in the list of parameters.");
3736
David Majnemer91fc4c22014-01-29 18:57:46 +00003737 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003738 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003739 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003740
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003741 if (Lexer.is(AsmToken::Colon)) {
3742 Lex(); // consume ':'
3743
3744 SMLoc QualLoc;
3745 StringRef Qualifier;
3746
3747 QualLoc = Lexer.getLoc();
3748 if (parseIdentifier(Qualifier))
3749 return Error(QualLoc, "missing parameter qualifier for "
3750 "'" + Parameter.Name + "' in macro '" + Name + "'");
3751
3752 if (Qualifier == "req")
3753 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003754 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003755 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003756 else
3757 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3758 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3759 }
3760
David Majnemer91fc4c22014-01-29 18:57:46 +00003761 if (getLexer().is(AsmToken::Equal)) {
3762 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003763
3764 SMLoc ParamLoc;
3765
3766 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003767 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003768 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003769
3770 if (Parameter.Required)
3771 Warning(ParamLoc, "pointless default value for required parameter "
3772 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003773 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003774
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003775 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003776
3777 if (getLexer().is(AsmToken::Comma))
3778 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003779 }
3780
3781 // Eat the end of statement.
3782 Lex();
3783
3784 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003785 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003786
3787 // Lex the macro definition.
3788 for (;;) {
3789 // Check whether we have reached the end of the file.
3790 if (getLexer().is(AsmToken::Eof))
3791 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3792
3793 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003794 if (getLexer().is(AsmToken::Identifier)) {
3795 if (getTok().getIdentifier() == ".endm" ||
3796 getTok().getIdentifier() == ".endmacro") {
3797 if (MacroDepth == 0) { // Outermost macro.
3798 EndToken = getTok();
3799 Lex();
3800 if (getLexer().isNot(AsmToken::EndOfStatement))
3801 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3802 "' directive");
3803 break;
3804 } else {
3805 // Otherwise we just found the end of an inner macro.
3806 --MacroDepth;
3807 }
3808 } else if (getTok().getIdentifier() == ".macro") {
3809 // We allow nested macros. Those aren't instantiated until the outermost
3810 // macro is expanded so just ignore them for now.
3811 ++MacroDepth;
3812 }
Eli Bendersky17233942013-01-15 22:59:42 +00003813 }
3814
3815 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003816 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003817 }
3818
Jim Grosbach4b905842013-09-20 23:08:21 +00003819 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003820 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3821 }
3822
3823 const char *BodyStart = StartToken.getLoc().getPointer();
3824 const char *BodyEnd = EndToken.getLoc().getPointer();
3825 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003826 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003827 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003828 return false;
3829}
3830
Jim Grosbach4b905842013-09-20 23:08:21 +00003831/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003832///
3833/// With the support added for named parameters there may be code out there that
3834/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003835/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003836/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003837/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003838/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3839/// warning that the positional parameter found in body which have no effect.
3840/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003841/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003842/// intended or change the macro to use the named parameters. It is possible
3843/// this warning will trigger when the none of the named parameters are used
3844/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003845void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003846 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003847 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003848 // If this macro is not defined with named parameters the warning we are
3849 // checking for here doesn't apply.
3850 unsigned NParameters = Parameters.size();
3851 if (NParameters == 0)
3852 return;
3853
3854 bool NamedParametersFound = false;
3855 bool PositionalParametersFound = false;
3856
3857 // Look at the body of the macro for use of both the named parameters and what
3858 // are likely to be positional parameters. This is what expandMacro() is
3859 // doing when it finds the parameters in the body.
3860 while (!Body.empty()) {
3861 // Scan for the next possible parameter.
3862 std::size_t End = Body.size(), Pos = 0;
3863 for (; Pos != End; ++Pos) {
3864 // Check for a substitution or escape.
3865 // This macro is defined with parameters, look for \foo, \bar, etc.
3866 if (Body[Pos] == '\\' && Pos + 1 != End)
3867 break;
3868
3869 // This macro should have parameters, but look for $0, $1, ..., $n too.
3870 if (Body[Pos] != '$' || Pos + 1 == End)
3871 continue;
3872 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003873 if (Next == '$' || Next == 'n' ||
3874 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003875 break;
3876 }
3877
3878 // Check if we reached the end.
3879 if (Pos == End)
3880 break;
3881
3882 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003883 switch (Body[Pos + 1]) {
3884 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003885 case '$':
3886 break;
3887
Jim Grosbach4b905842013-09-20 23:08:21 +00003888 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003889 case 'n':
3890 PositionalParametersFound = true;
3891 break;
3892
Jim Grosbach4b905842013-09-20 23:08:21 +00003893 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003894 default: {
3895 PositionalParametersFound = true;
3896 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003897 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003898 }
3899 Pos += 2;
3900 } else {
3901 unsigned I = Pos + 1;
3902 while (isIdentifierChar(Body[I]) && I + 1 != End)
3903 ++I;
3904
Jim Grosbach4b905842013-09-20 23:08:21 +00003905 const char *Begin = Body.data() + Pos + 1;
3906 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003907 unsigned Index = 0;
3908 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003909 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003910 break;
3911
3912 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003913 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3914 Pos += 3;
3915 else {
3916 Pos = I;
3917 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003918 } else {
3919 NamedParametersFound = true;
3920 Pos += 1 + Argument.size();
3921 }
3922 }
3923 // Update the scan point.
3924 Body = Body.substr(Pos);
3925 }
3926
3927 if (!NamedParametersFound && PositionalParametersFound)
3928 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3929 "used in macro body, possible positional parameter "
3930 "found in body which will have no effect");
3931}
3932
Nico Weber155dccd12014-07-24 17:08:39 +00003933/// parseDirectiveExitMacro
3934/// ::= .exitm
3935bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3936 if (getLexer().isNot(AsmToken::EndOfStatement))
3937 return TokError("unexpected token in '" + Directive + "' directive");
3938
3939 if (!isInsideMacroInstantiation())
3940 return TokError("unexpected '" + Directive + "' in file, "
3941 "no current macro definition");
3942
3943 // Exit all conditionals that are active in the current macro.
3944 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3945 TheCondState = TheCondStack.back();
3946 TheCondStack.pop_back();
3947 }
3948
3949 handleMacroExit();
3950 return false;
3951}
3952
Jim Grosbach4b905842013-09-20 23:08:21 +00003953/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003954/// ::= .endm
3955/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003956bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003957 if (getLexer().isNot(AsmToken::EndOfStatement))
3958 return TokError("unexpected token in '" + Directive + "' directive");
3959
3960 // If we are inside a macro instantiation, terminate the current
3961 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003962 if (isInsideMacroInstantiation()) {
3963 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003964 return false;
3965 }
3966
3967 // Otherwise, this .endmacro is a stray entry in the file; well formed
3968 // .endmacro directives are handled during the macro definition parsing.
3969 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003970 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003971}
3972
Jim Grosbach4b905842013-09-20 23:08:21 +00003973/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003974/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003975bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003976 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003977 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003978 return TokError("expected identifier in '.purgem' directive");
3979
3980 if (getLexer().isNot(AsmToken::EndOfStatement))
3981 return TokError("unexpected token in '.purgem' directive");
3982
Jim Grosbach4b905842013-09-20 23:08:21 +00003983 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003984 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3985
Jim Grosbach4b905842013-09-20 23:08:21 +00003986 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003987 return false;
3988}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003989
Jim Grosbach4b905842013-09-20 23:08:21 +00003990/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003991/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003992bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003993 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003994
3995 // Expect a single argument: an expression that evaluates to a constant
3996 // in the inclusive range 0-30.
3997 SMLoc ExprLoc = getLexer().getLoc();
3998 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003999 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00004000 return true;
4001 else if (getLexer().isNot(AsmToken::EndOfStatement))
4002 return TokError("unexpected token after expression in"
4003 " '.bundle_align_mode' directive");
4004 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
4005 return Error(ExprLoc,
4006 "invalid bundle alignment size (expected between 0 and 30)");
4007
4008 Lex();
4009
4010 // Because of AlignSizePow2's verified range we can safely truncate it to
4011 // unsigned.
4012 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4013 return false;
4014}
4015
Jim Grosbach4b905842013-09-20 23:08:21 +00004016/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00004017/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00004018bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004019 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00004020 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004021
Eli Bendersky802b6282013-01-07 21:51:08 +00004022 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4023 StringRef Option;
4024 SMLoc Loc = getTok().getLoc();
4025 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004026 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004027
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004028 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00004029 return Error(Loc, kInvalidOptionError);
4030
4031 if (Option != "align_to_end")
4032 return Error(Loc, kInvalidOptionError);
4033 else if (getLexer().isNot(AsmToken::EndOfStatement))
4034 return Error(Loc,
4035 "unexpected token after '.bundle_lock' directive option");
4036 AlignToEnd = true;
4037 }
4038
Eli Benderskyf483ff92012-12-20 19:05:53 +00004039 Lex();
4040
Eli Bendersky802b6282013-01-07 21:51:08 +00004041 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004042 return false;
4043}
4044
Jim Grosbach4b905842013-09-20 23:08:21 +00004045/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004046/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004047bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004048 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004049
4050 if (getLexer().isNot(AsmToken::EndOfStatement))
4051 return TokError("unexpected token in '.bundle_unlock' directive");
4052 Lex();
4053
4054 getStreamer().EmitBundleUnlock();
4055 return false;
4056}
4057
Jim Grosbach4b905842013-09-20 23:08:21 +00004058/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004059/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004060bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004061 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004062
4063 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004064 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004065 return true;
4066
4067 int64_t FillExpr = 0;
4068 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4069 if (getLexer().isNot(AsmToken::Comma))
4070 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4071 Lex();
4072
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004073 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004074 return true;
4075
4076 if (getLexer().isNot(AsmToken::EndOfStatement))
4077 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4078 }
4079
4080 Lex();
4081
4082 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00004083 return TokError("invalid number of bytes in '" + Twine(IDVal) +
4084 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00004085
4086 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00004087 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00004088
4089 return false;
4090}
4091
Jim Grosbach4b905842013-09-20 23:08:21 +00004092/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004093/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004094bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004095 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004096 const MCExpr *Value;
4097
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004098 for (;;) {
4099 if (parseExpression(Value))
4100 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004101
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004102 if (Signed)
4103 getStreamer().EmitSLEB128Value(Value);
4104 else
4105 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004106
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004107 if (getLexer().is(AsmToken::EndOfStatement))
4108 break;
4109
4110 if (getLexer().isNot(AsmToken::Comma))
4111 return TokError("unexpected token in directive");
4112 Lex();
4113 }
Eli Bendersky17233942013-01-15 22:59:42 +00004114
4115 return false;
4116}
4117
Jim Grosbach4b905842013-09-20 23:08:21 +00004118/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004119/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004120bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004121 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00004122 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004123 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004124 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004125
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004126 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004127 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004128
Jim Grosbach6f482002015-05-18 18:43:14 +00004129 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004130
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004131 // Assembler local symbols don't make any sense here. Complain loudly.
4132 if (Sym->isTemporary())
4133 return Error(Loc, "non-local symbol required in directive");
4134
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004135 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4136 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004137
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004138 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004139 break;
4140
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004141 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004142 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004143 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00004144 }
4145 }
4146
Sean Callanan686ed8d2010-01-19 20:22:31 +00004147 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004148 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004149}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004150
Jim Grosbach4b905842013-09-20 23:08:21 +00004151/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004152/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004153bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004154 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004155
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004156 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004157 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004158 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004159 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004160
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004161 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004162 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004163
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004164 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004165 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004166 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004167
4168 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004169 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004170 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004171 return true;
4172
4173 int64_t Pow2Alignment = 0;
4174 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004175 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004176 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004177 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004178 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004179 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004180
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004181 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4182 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004183 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4184
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004185 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004186 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4187 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004188 if (!isPowerOf2_64(Pow2Alignment))
4189 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4190 Pow2Alignment = Log2_64(Pow2Alignment);
4191 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004192 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004193
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004194 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004195 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004196
Sean Callanan686ed8d2010-01-19 20:22:31 +00004197 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004198
Chris Lattner28ad7542009-07-09 17:25:12 +00004199 // NOTE: a size of zero for a .comm should create a undefined symbol
4200 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004201 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004202 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004203 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004204
Eric Christopherbc818852010-05-14 01:38:54 +00004205 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004206 // may internally end up wanting an alignment in bytes.
4207 // FIXME: Diagnose overflow.
4208 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004209 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004210 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004211
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004212 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004213 return Error(IDLoc, "invalid symbol redefinition");
4214
Chris Lattner28ad7542009-07-09 17:25:12 +00004215 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004216 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004217 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004218 return false;
4219 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004220
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004221 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004222 return false;
4223}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004224
Jim Grosbach4b905842013-09-20 23:08:21 +00004225/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004226/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004227bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004228 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004229 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004230
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004231 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004232 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00004233 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004234
Sean Callanan686ed8d2010-01-19 20:22:31 +00004235 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00004236
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004237 if (Str.empty())
4238 Error(Loc, ".abort detected. Assembly stopping.");
4239 else
4240 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004241 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004242
4243 return false;
4244}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004245
Jim Grosbach4b905842013-09-20 23:08:21 +00004246/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004247/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004248bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004249 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004250 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004251
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004252 // Allow the strings to have escaped octal character sequence.
4253 std::string Filename;
4254 if (parseEscapedString(Filename))
4255 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004256 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00004257 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004258
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004259 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004260 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004261
Chris Lattner693fbb82009-07-16 06:14:39 +00004262 // Attempt to switch the lexer to the included file before consuming the end
4263 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00004264 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00004265 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00004266 return true;
4267 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004268
4269 return false;
4270}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004271
Jim Grosbach4b905842013-09-20 23:08:21 +00004272/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004273/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004274bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004275 if (getLexer().isNot(AsmToken::String))
4276 return TokError("expected string in '.incbin' directive");
4277
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004278 // Allow the strings to have escaped octal character sequence.
4279 std::string Filename;
4280 if (parseEscapedString(Filename))
4281 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00004282 SMLoc IncbinLoc = getLexer().getLoc();
4283 Lex();
4284
4285 if (getLexer().isNot(AsmToken::EndOfStatement))
4286 return TokError("unexpected token in '.incbin' directive");
4287
Kevin Enderby109f25c2011-12-14 21:47:48 +00004288 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00004289 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004290 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
4291 return true;
4292 }
4293
4294 return false;
4295}
4296
Jim Grosbach4b905842013-09-20 23:08:21 +00004297/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004298/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4299bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004300 TheCondStack.push_back(TheCondState);
4301 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004302 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004303 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004304 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004305 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004306 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004307 return true;
4308
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004309 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004310 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004311
Sean Callanan686ed8d2010-01-19 20:22:31 +00004312 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004313
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004314 switch (DirKind) {
4315 default:
4316 llvm_unreachable("unsupported directive");
4317 case DK_IF:
4318 case DK_IFNE:
4319 break;
4320 case DK_IFEQ:
4321 ExprValue = ExprValue == 0;
4322 break;
4323 case DK_IFGE:
4324 ExprValue = ExprValue >= 0;
4325 break;
4326 case DK_IFGT:
4327 ExprValue = ExprValue > 0;
4328 break;
4329 case DK_IFLE:
4330 ExprValue = ExprValue <= 0;
4331 break;
4332 case DK_IFLT:
4333 ExprValue = ExprValue < 0;
4334 break;
4335 }
4336
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004337 TheCondState.CondMet = ExprValue;
4338 TheCondState.Ignore = !TheCondState.CondMet;
4339 }
4340
4341 return false;
4342}
4343
Jim Grosbach4b905842013-09-20 23:08:21 +00004344/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004345/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004346bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004347 TheCondStack.push_back(TheCondState);
4348 TheCondState.TheCond = AsmCond::IfCond;
4349
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004350 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004351 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004352 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004353 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004354
4355 if (getLexer().isNot(AsmToken::EndOfStatement))
4356 return TokError("unexpected token in '.ifb' directive");
4357
4358 Lex();
4359
4360 TheCondState.CondMet = ExpectBlank == Str.empty();
4361 TheCondState.Ignore = !TheCondState.CondMet;
4362 }
4363
4364 return false;
4365}
4366
Jim Grosbach4b905842013-09-20 23:08:21 +00004367/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004368/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004369/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004370bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004371 TheCondStack.push_back(TheCondState);
4372 TheCondState.TheCond = AsmCond::IfCond;
4373
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004374 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004375 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004376 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004377 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004378
4379 if (getLexer().isNot(AsmToken::Comma))
4380 return TokError("unexpected token in '.ifc' directive");
4381
4382 Lex();
4383
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004384 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004385
4386 if (getLexer().isNot(AsmToken::EndOfStatement))
4387 return TokError("unexpected token in '.ifc' directive");
4388
4389 Lex();
4390
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004391 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004392 TheCondState.Ignore = !TheCondState.CondMet;
4393 }
4394
4395 return false;
4396}
4397
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004398/// parseDirectiveIfeqs
4399/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004400bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004401 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004402 if (ExpectEqual)
4403 TokError("expected string parameter for '.ifeqs' directive");
4404 else
4405 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004406 eatToEndOfStatement();
4407 return true;
4408 }
4409
4410 StringRef String1 = getTok().getStringContents();
4411 Lex();
4412
4413 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004414 if (ExpectEqual)
4415 TokError("expected comma after first string for '.ifeqs' directive");
4416 else
4417 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004418 eatToEndOfStatement();
4419 return true;
4420 }
4421
4422 Lex();
4423
4424 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004425 if (ExpectEqual)
4426 TokError("expected string parameter for '.ifeqs' directive");
4427 else
4428 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004429 eatToEndOfStatement();
4430 return true;
4431 }
4432
4433 StringRef String2 = getTok().getStringContents();
4434 Lex();
4435
4436 TheCondStack.push_back(TheCondState);
4437 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004438 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004439 TheCondState.Ignore = !TheCondState.CondMet;
4440
4441 return false;
4442}
4443
Jim Grosbach4b905842013-09-20 23:08:21 +00004444/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004445/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004446bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004447 StringRef Name;
4448 TheCondStack.push_back(TheCondState);
4449 TheCondState.TheCond = AsmCond::IfCond;
4450
4451 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004452 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004453 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004454 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004455 return TokError("expected identifier after '.ifdef'");
4456
4457 Lex();
4458
Jim Grosbach6f482002015-05-18 18:43:14 +00004459 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004460
4461 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004462 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004463 else
Craig Topper353eda42014-04-24 06:44:33 +00004464 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004465 TheCondState.Ignore = !TheCondState.CondMet;
4466 }
4467
4468 return false;
4469}
4470
Jim Grosbach4b905842013-09-20 23:08:21 +00004471/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004472/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004473bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004474 if (TheCondState.TheCond != AsmCond::IfCond &&
4475 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004476 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4477 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004478 TheCondState.TheCond = AsmCond::ElseIfCond;
4479
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004480 bool LastIgnoreState = false;
4481 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004482 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004483 if (LastIgnoreState || TheCondState.CondMet) {
4484 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004485 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004486 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004487 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004488 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004489 return true;
4490
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004491 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004492 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004493
Sean Callanan686ed8d2010-01-19 20:22:31 +00004494 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004495 TheCondState.CondMet = ExprValue;
4496 TheCondState.Ignore = !TheCondState.CondMet;
4497 }
4498
4499 return false;
4500}
4501
Jim Grosbach4b905842013-09-20 23:08:21 +00004502/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004503/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004504bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004505 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004506 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004507
Sean Callanan686ed8d2010-01-19 20:22:31 +00004508 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004509
4510 if (TheCondState.TheCond != AsmCond::IfCond &&
4511 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004512 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4513 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004514 TheCondState.TheCond = AsmCond::ElseCond;
4515 bool LastIgnoreState = false;
4516 if (!TheCondStack.empty())
4517 LastIgnoreState = TheCondStack.back().Ignore;
4518 if (LastIgnoreState || TheCondState.CondMet)
4519 TheCondState.Ignore = true;
4520 else
4521 TheCondState.Ignore = false;
4522
4523 return false;
4524}
4525
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004526/// parseDirectiveEnd
4527/// ::= .end
4528bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4529 if (getLexer().isNot(AsmToken::EndOfStatement))
4530 return TokError("unexpected token in '.end' directive");
4531
4532 Lex();
4533
4534 while (Lexer.isNot(AsmToken::Eof))
4535 Lex();
4536
4537 return false;
4538}
4539
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004540/// parseDirectiveError
4541/// ::= .err
4542/// ::= .error [string]
4543bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4544 if (!TheCondStack.empty()) {
4545 if (TheCondStack.back().Ignore) {
4546 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004547 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004548 }
4549 }
4550
4551 if (!WithMessage)
4552 return Error(L, ".err encountered");
4553
4554 StringRef Message = ".error directive invoked in source file";
4555 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4556 if (Lexer.isNot(AsmToken::String)) {
4557 TokError(".error argument must be a string");
4558 eatToEndOfStatement();
4559 return true;
4560 }
4561
4562 Message = getTok().getStringContents();
4563 Lex();
4564 }
4565
4566 Error(L, Message);
4567 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004568}
4569
Nico Weber404012b2014-07-24 16:26:06 +00004570/// parseDirectiveWarning
4571/// ::= .warning [string]
4572bool AsmParser::parseDirectiveWarning(SMLoc L) {
4573 if (!TheCondStack.empty()) {
4574 if (TheCondStack.back().Ignore) {
4575 eatToEndOfStatement();
4576 return false;
4577 }
4578 }
4579
4580 StringRef Message = ".warning directive invoked in source file";
4581 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4582 if (Lexer.isNot(AsmToken::String)) {
4583 TokError(".warning argument must be a string");
4584 eatToEndOfStatement();
4585 return true;
4586 }
4587
4588 Message = getTok().getStringContents();
4589 Lex();
4590 }
4591
4592 Warning(L, Message);
4593 return false;
4594}
4595
Jim Grosbach4b905842013-09-20 23:08:21 +00004596/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004597/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004598bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004599 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004600 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004601
Sean Callanan686ed8d2010-01-19 20:22:31 +00004602 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004603
Jim Grosbach4b905842013-09-20 23:08:21 +00004604 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004605 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4606 ".else");
4607 if (!TheCondStack.empty()) {
4608 TheCondState = TheCondStack.back();
4609 TheCondStack.pop_back();
4610 }
4611
4612 return false;
4613}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004614
Eli Bendersky17233942013-01-15 22:59:42 +00004615void AsmParser::initializeDirectiveKindMap() {
4616 DirectiveKindMap[".set"] = DK_SET;
4617 DirectiveKindMap[".equ"] = DK_EQU;
4618 DirectiveKindMap[".equiv"] = DK_EQUIV;
4619 DirectiveKindMap[".ascii"] = DK_ASCII;
4620 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4621 DirectiveKindMap[".string"] = DK_STRING;
4622 DirectiveKindMap[".byte"] = DK_BYTE;
4623 DirectiveKindMap[".short"] = DK_SHORT;
4624 DirectiveKindMap[".value"] = DK_VALUE;
4625 DirectiveKindMap[".2byte"] = DK_2BYTE;
4626 DirectiveKindMap[".long"] = DK_LONG;
4627 DirectiveKindMap[".int"] = DK_INT;
4628 DirectiveKindMap[".4byte"] = DK_4BYTE;
4629 DirectiveKindMap[".quad"] = DK_QUAD;
4630 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004631 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004632 DirectiveKindMap[".single"] = DK_SINGLE;
4633 DirectiveKindMap[".float"] = DK_FLOAT;
4634 DirectiveKindMap[".double"] = DK_DOUBLE;
4635 DirectiveKindMap[".align"] = DK_ALIGN;
4636 DirectiveKindMap[".align32"] = DK_ALIGN32;
4637 DirectiveKindMap[".balign"] = DK_BALIGN;
4638 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4639 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4640 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4641 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4642 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4643 DirectiveKindMap[".org"] = DK_ORG;
4644 DirectiveKindMap[".fill"] = DK_FILL;
4645 DirectiveKindMap[".zero"] = DK_ZERO;
4646 DirectiveKindMap[".extern"] = DK_EXTERN;
4647 DirectiveKindMap[".globl"] = DK_GLOBL;
4648 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004649 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4650 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4651 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4652 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4653 DirectiveKindMap[".reference"] = DK_REFERENCE;
4654 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4655 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4656 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4657 DirectiveKindMap[".comm"] = DK_COMM;
4658 DirectiveKindMap[".common"] = DK_COMMON;
4659 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4660 DirectiveKindMap[".abort"] = DK_ABORT;
4661 DirectiveKindMap[".include"] = DK_INCLUDE;
4662 DirectiveKindMap[".incbin"] = DK_INCBIN;
4663 DirectiveKindMap[".code16"] = DK_CODE16;
4664 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4665 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004666 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004667 DirectiveKindMap[".irp"] = DK_IRP;
4668 DirectiveKindMap[".irpc"] = DK_IRPC;
4669 DirectiveKindMap[".endr"] = DK_ENDR;
4670 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4671 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4672 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4673 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004674 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4675 DirectiveKindMap[".ifge"] = DK_IFGE;
4676 DirectiveKindMap[".ifgt"] = DK_IFGT;
4677 DirectiveKindMap[".ifle"] = DK_IFLE;
4678 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004679 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004680 DirectiveKindMap[".ifb"] = DK_IFB;
4681 DirectiveKindMap[".ifnb"] = DK_IFNB;
4682 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004683 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004684 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004685 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004686 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4687 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4688 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4689 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4690 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004691 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004692 DirectiveKindMap[".endif"] = DK_ENDIF;
4693 DirectiveKindMap[".skip"] = DK_SKIP;
4694 DirectiveKindMap[".space"] = DK_SPACE;
4695 DirectiveKindMap[".file"] = DK_FILE;
4696 DirectiveKindMap[".line"] = DK_LINE;
4697 DirectiveKindMap[".loc"] = DK_LOC;
4698 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004699 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
4700 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4701 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004702 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
David Majnemer408b5e62016-02-05 01:55:49 +00004703 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004704 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4705 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004706 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4707 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4708 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4709 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4710 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4711 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4712 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4713 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4714 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4715 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4716 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4717 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4718 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4719 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4720 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4721 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4722 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4723 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4724 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4725 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4726 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004727 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004728 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4729 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4730 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004731 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004732 DirectiveKindMap[".endm"] = DK_ENDM;
4733 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4734 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004735 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004736 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004737 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004738 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004739}
4740
Jim Grosbach4b905842013-09-20 23:08:21 +00004741MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004742 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004743
Rafael Espindola34b9c512012-06-03 23:57:14 +00004744 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004745 for (;;) {
4746 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004747 if (getLexer().is(AsmToken::Eof)) {
4748 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004749 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004750 }
4751
Rafael Espindola34b9c512012-06-03 23:57:14 +00004752 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004753 (getTok().getIdentifier() == ".rept" ||
4754 getTok().getIdentifier() == ".irp" ||
4755 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004756 ++NestLevel;
4757 }
4758
4759 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004760 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004761 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004762 EndToken = getTok();
4763 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004764 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4765 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004766 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004767 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004768 break;
4769 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004770 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004771 }
4772
Rafael Espindola34b9c512012-06-03 23:57:14 +00004773 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004774 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004775 }
4776
4777 const char *BodyStart = StartToken.getLoc().getPointer();
4778 const char *BodyEnd = EndToken.getLoc().getPointer();
4779 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4780
Rafael Espindola34b9c512012-06-03 23:57:14 +00004781 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004782 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004783 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004784}
4785
Jim Grosbach4b905842013-09-20 23:08:21 +00004786void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004787 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004788 OS << ".endr\n";
4789
Rafael Espindola3560ff22014-08-27 20:03:13 +00004790 std::unique_ptr<MemoryBuffer> Instantiation =
4791 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004792
Rafael Espindola34b9c512012-06-03 23:57:14 +00004793 // Create the macro instantiation object and add to the current macro
4794 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004795 MacroInstantiation *MI = new MacroInstantiation(
4796 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004797 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004798
Rafael Espindola34b9c512012-06-03 23:57:14 +00004799 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004800 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004801 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004802 Lex();
4803}
4804
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004805/// parseDirectiveRept
4806/// ::= .rep | .rept count
4807bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004808 const MCExpr *CountExpr;
4809 SMLoc CountLoc = getTok().getLoc();
4810 if (parseExpression(CountExpr))
4811 return true;
4812
Rafael Espindola34b9c512012-06-03 23:57:14 +00004813 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004814 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004815 eatToEndOfStatement();
4816 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4817 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004818
4819 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004820 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004821
4822 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004823 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004824
4825 // Eat the end of statement.
4826 Lex();
4827
4828 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004829 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004830 if (!M)
4831 return true;
4832
4833 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4834 // to hold the macro body with substitutions.
4835 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004836 raw_svector_ostream OS(Buf);
4837 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004838 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4839 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004840 return true;
4841 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004842 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004843
4844 return false;
4845}
4846
Jim Grosbach4b905842013-09-20 23:08:21 +00004847/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004848/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004849bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004850 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004851
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004852 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004853 return TokError("expected identifier in '.irp' directive");
4854
Rafael Espindola768b41c2012-06-15 14:02:34 +00004855 if (Lexer.isNot(AsmToken::Comma))
4856 return TokError("expected comma in '.irp' directive");
4857
4858 Lex();
4859
Eli Bendersky38274122013-01-14 23:22:36 +00004860 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004861 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004862 return true;
4863
4864 // Eat the end of statement.
4865 Lex();
4866
4867 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004868 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004869 if (!M)
4870 return true;
4871
4872 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4873 // to hold the macro body with substitutions.
4874 SmallString<256> Buf;
4875 raw_svector_ostream OS(Buf);
4876
Craig Topper84008482015-10-10 05:38:14 +00004877 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004878 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4879 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004880 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004881 return true;
4882 }
4883
Jim Grosbach4b905842013-09-20 23:08:21 +00004884 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004885
4886 return false;
4887}
4888
Jim Grosbach4b905842013-09-20 23:08:21 +00004889/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004890/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004891bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004892 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004893
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004894 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004895 return TokError("expected identifier in '.irpc' directive");
4896
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004897 if (Lexer.isNot(AsmToken::Comma))
4898 return TokError("expected comma in '.irpc' directive");
4899
4900 Lex();
4901
Eli Bendersky38274122013-01-14 23:22:36 +00004902 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004903 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004904 return true;
4905
4906 if (A.size() != 1 || A.front().size() != 1)
4907 return TokError("unexpected token in '.irpc' directive");
4908
4909 // Eat the end of statement.
4910 Lex();
4911
4912 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004913 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004914 if (!M)
4915 return true;
4916
4917 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4918 // to hold the macro body with substitutions.
4919 SmallString<256> Buf;
4920 raw_svector_ostream OS(Buf);
4921
4922 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004923 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004924 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004925 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004926
Toma Tabacu217116e2015-04-27 10:50:29 +00004927 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4928 // This is undocumented, but GAS seems to support it.
4929 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004930 return true;
4931 }
4932
Jim Grosbach4b905842013-09-20 23:08:21 +00004933 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004934
4935 return false;
4936}
4937
Jim Grosbach4b905842013-09-20 23:08:21 +00004938bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004939 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004940 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004941
4942 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004943 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004944 assert(getLexer().is(AsmToken::EndOfStatement));
4945
Jim Grosbach4b905842013-09-20 23:08:21 +00004946 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004947 return false;
4948}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004949
Jim Grosbach4b905842013-09-20 23:08:21 +00004950bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004951 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004952 const MCExpr *Value;
4953 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004954 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004955 return true;
4956 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4957 if (!MCE)
4958 return Error(ExprLoc, "unexpected expression in _emit");
4959 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004960 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004961 return Error(ExprLoc, "literal value out of range for directive");
4962
Craig Topper7d5b2312015-10-10 05:25:02 +00004963 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004964 return false;
4965}
4966
Jim Grosbach4b905842013-09-20 23:08:21 +00004967bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004968 const MCExpr *Value;
4969 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004970 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004971 return true;
4972 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4973 if (!MCE)
4974 return Error(ExprLoc, "unexpected expression in align");
4975 uint64_t IntValue = MCE->getValue();
4976 if (!isPowerOf2_64(IntValue))
4977 return Error(ExprLoc, "literal value not a power of two greater then zero");
4978
Craig Topper7d5b2312015-10-10 05:25:02 +00004979 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004980 return false;
4981}
4982
Chad Rosierf43fcf52013-02-13 21:27:17 +00004983// We are comparing pointers, but the pointers are relative to a single string.
4984// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004985static int rewritesSort(const AsmRewrite *AsmRewriteA,
4986 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004987 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4988 return -1;
4989 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4990 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004991
Chad Rosierfce4fab2013-04-08 17:43:47 +00004992 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4993 // rewrite to the same location. Make sure the SizeDirective rewrite is
4994 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4995 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004996 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4997 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004998 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004999
Jim Grosbach4b905842013-09-20 23:08:21 +00005000 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5001 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005002 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00005003 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00005004}
5005
Jim Grosbach4b905842013-09-20 23:08:21 +00005006bool AsmParser::parseMSInlineAsm(
5007 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
5008 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
5009 SmallVectorImpl<std::string> &Constraints,
5010 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5011 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00005012 SmallVector<void *, 4> InputDecls;
5013 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00005014 SmallVector<bool, 4> InputDeclsAddressOf;
5015 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00005016 SmallVector<std::string, 4> InputConstraints;
5017 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005018 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00005019
Benjamin Kramer1a136112013-02-15 20:37:21 +00005020 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005021
5022 // Prime the lexer.
5023 Lex();
5024
5025 // While we have input, parse each statement.
5026 unsigned InputIdx = 0;
5027 unsigned OutputIdx = 0;
5028 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005029 // Parse curly braces marking block start/end
5030 if (parseCurlyBlockScope(AsmStrRewrites))
5031 continue;
5032
Eli Friedman0f4871d2012-10-22 23:58:19 +00005033 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005034 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00005035 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00005036
Chad Rosier149e8e02012-12-12 22:45:52 +00005037 if (Info.ParseError)
5038 return true;
5039
Benjamin Kramer1a136112013-02-15 20:37:21 +00005040 if (Info.Opcode == ~0U)
5041 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005042
Benjamin Kramer1a136112013-02-15 20:37:21 +00005043 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005044
Benjamin Kramer1a136112013-02-15 20:37:21 +00005045 // Build the list of clobbers, outputs and inputs.
5046 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005047 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005048
Benjamin Kramer1a136112013-02-15 20:37:21 +00005049 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005050 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005051 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005052
Benjamin Kramer1a136112013-02-15 20:37:21 +00005053 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005054 if (Operand.isReg() && !Operand.needAddressOf() &&
5055 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005056 unsigned NumDefs = Desc.getNumDefs();
5057 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005058 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5059 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005060 continue;
5061 }
5062
5063 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005064 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005065 if (SymName.empty())
5066 continue;
5067
David Blaikie960ea3f2014-06-08 16:18:35 +00005068 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005069 if (!OpDecl)
5070 continue;
5071
5072 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005073 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005074 if (isOutput) {
5075 ++InputIdx;
5076 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005077 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005078 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005079 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005080 } else {
5081 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005082 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5083 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005084 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005085 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005086 }
Reid Kleckneree088972013-12-10 18:27:32 +00005087
5088 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005089 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5090 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005091 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005092 }
5093
5094 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005095 NumOutputs = OutputDecls.size();
5096 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005097
5098 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005099 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5100 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5101 ClobberRegs.end());
5102 Clobbers.assign(ClobberRegs.size(), std::string());
5103 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5104 raw_string_ostream OS(Clobbers[I]);
5105 IP->printRegName(OS, ClobberRegs[I]);
5106 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005107
5108 // Merge the various outputs and inputs. Output are expected first.
5109 if (NumOutputs || NumInputs) {
5110 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005111 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005112 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005113 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005114 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005115 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005116 }
5117 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005118 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005119 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005120 }
5121 }
5122
5123 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005124 std::string AsmStringIR;
5125 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005126 StringRef ASMString =
5127 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5128 const char *AsmStart = ASMString.begin();
5129 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005130 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005131 for (const AsmRewrite &AR : AsmStrRewrites) {
5132 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005133 if (Kind == AOK_Delete)
5134 continue;
5135
David Majnemer8114c1a2014-06-23 02:17:16 +00005136 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005137 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005138
Chad Rosier120eefd2013-03-19 17:32:17 +00005139 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005140 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005141 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005142
Chad Rosier37e755c2012-10-23 17:43:43 +00005143 // Skip the original expression.
5144 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005145 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005146 continue;
5147 }
5148
Chad Rosierff10ed12013-04-12 16:26:42 +00005149 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005150 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005151 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005152 default:
5153 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005154 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005155 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005156 break;
5157 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005158 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005159 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005160 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005161 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005162 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005163 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005164 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005165 break;
5166 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005167 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005168 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005169 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005170 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005171 default: break;
5172 case 8: OS << "byte ptr "; break;
5173 case 16: OS << "word ptr "; break;
5174 case 32: OS << "dword ptr "; break;
5175 case 64: OS << "qword ptr "; break;
5176 case 80: OS << "xword ptr "; break;
5177 case 128: OS << "xmmword ptr "; break;
5178 case 256: OS << "ymmword ptr "; break;
5179 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005180 break;
5181 case AOK_Emit:
5182 OS << ".byte";
5183 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005184 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005185 // MS alignment directives are measured in bytes. If the native assembler
5186 // measures alignment in bytes, we can pass it straight through.
5187 OS << ".align";
5188 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5189 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005190
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005191 // Alignment is in log2 form, so print that instead and skip the original
5192 // immediate.
5193 unsigned Val = AR.Val;
5194 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005195 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005196 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5197 break;
5198 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005199 case AOK_EVEN:
5200 OS << ".even";
5201 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005202 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005203 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005204 OS.flush();
5205 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005206 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005207 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005208 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005209 case AOK_EndOfStatement:
5210 OS << "\n\t";
5211 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005212 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005213
Chad Rosier8bce6642012-10-18 15:49:34 +00005214 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005215 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005216 }
5217
5218 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005219 if (AsmStart != AsmEnd)
5220 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005221
5222 AsmString = OS.str();
5223 return false;
5224}
5225
Pete Cooper80d21cb2015-06-22 19:35:57 +00005226namespace llvm {
5227namespace MCParserUtils {
5228
5229/// Returns whether the given symbol is used anywhere in the given expression,
5230/// or subexpressions.
5231static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5232 switch (Value->getKind()) {
5233 case MCExpr::Binary: {
5234 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5235 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5236 isSymbolUsedInExpression(Sym, BE->getRHS());
5237 }
5238 case MCExpr::Target:
5239 case MCExpr::Constant:
5240 return false;
5241 case MCExpr::SymbolRef: {
5242 const MCSymbol &S =
5243 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5244 if (S.isVariable())
5245 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5246 return &S == Sym;
5247 }
5248 case MCExpr::Unary:
5249 return isSymbolUsedInExpression(
5250 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5251 }
5252
5253 llvm_unreachable("Unknown expr kind!");
5254}
5255
5256bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5257 MCAsmParser &Parser, MCSymbol *&Sym,
5258 const MCExpr *&Value) {
5259 MCAsmLexer &Lexer = Parser.getLexer();
5260
5261 // FIXME: Use better location, we should use proper tokens.
5262 SMLoc EqualLoc = Lexer.getLoc();
5263
5264 if (Parser.parseExpression(Value)) {
5265 Parser.TokError("missing expression");
5266 Parser.eatToEndOfStatement();
5267 return true;
5268 }
5269
5270 // Note: we don't count b as used in "a = b". This is to allow
5271 // a = b
5272 // b = c
5273
5274 if (Lexer.isNot(AsmToken::EndOfStatement))
5275 return Parser.TokError("unexpected token in assignment");
5276
5277 // Eat the end of statement marker.
5278 Parser.Lex();
5279
5280 // Validate that the LHS is allowed to be a variable (either it has not been
5281 // used as a symbol, or it is an absolute symbol).
5282 Sym = Parser.getContext().lookupSymbol(Name);
5283 if (Sym) {
5284 // Diagnose assignment to a label.
5285 //
5286 // FIXME: Diagnostics. Note the location of the definition as a label.
5287 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5288 if (isSymbolUsedInExpression(Sym, Value))
5289 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005290 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5291 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005292 ; // Allow redefinitions of undefined symbols only used in directives.
5293 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5294 ; // Allow redefinitions of variables that haven't yet been used.
5295 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5296 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5297 else if (!Sym->isVariable())
5298 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5299 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5300 return Parser.Error(EqualLoc,
5301 "invalid reassignment of non-absolute variable '" +
5302 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005303 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005304 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005305 return false;
5306 } else
5307 Sym = Parser.getContext().getOrCreateSymbol(Name);
5308
5309 Sym->setRedefinable(allow_redef);
5310
5311 return false;
5312}
5313
5314} // namespace MCParserUtils
5315} // namespace llvm
5316
Daniel Dunbar01e36072010-07-17 02:26:10 +00005317/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005318MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5319 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005320 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005321}