blob: 939b54dd3c81337b68ce88fdf8134e0d50b71503 [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"
Evan Cheng76792992011-07-20 05:58:47 +000031#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000032#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000033#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000034#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000035#include "llvm/MC/MCTargetAsmParser.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.
159 StringRef CppHashFilename;
160 int64_t CppHashLineNumber;
161 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000162 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000163 /// When generating dwarf for assembly source files we need to calculate the
164 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000165 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
167 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000168 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000170
Devang Patela173ee52012-01-31 18:14:05 +0000171 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
172 unsigned AssemblerDialect;
173
Jim Grosbach4b905842013-09-20 23:08:21 +0000174 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000175 bool IsDarwin;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000178 bool ParsingInlineAsm;
179
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000181 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000183 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186
Craig Topper59be68f2014-03-08 07:14:16 +0000187 void addDirectiveHandler(StringRef Directive,
188 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000189 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 }
191
Toma Tabacu11e14a92015-04-21 11:50:52 +0000192 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
193 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
194 }
195
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000196public:
197 /// @name MCAsmParser Interface
198 /// {
199
Craig Topper59be68f2014-03-08 07:14:16 +0000200 SourceMgr &getSourceManager() override { return SrcMgr; }
201 MCAsmLexer &getLexer() override { return Lexer; }
202 MCContext &getContext() override { return Ctx; }
203 MCStreamer &getStreamer() override { return Out; }
204 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
Craig Topper59be68f2014-03-08 07:14:16 +0000210 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void Note(SMLoc L, const Twine &Msg,
215 ArrayRef<SMRange> Ranges = None) override;
216 bool Warning(SMLoc L, const Twine &Msg,
217 ArrayRef<SMRange> Ranges = None) override;
218 bool Error(SMLoc L, const Twine &Msg,
219 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper59be68f2014-03-08 07:14:16 +0000221 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000222
Craig Topper59be68f2014-03-08 07:14:16 +0000223 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
224 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000225
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000226 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000228 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000231 const MCInstrInfo *MII, const MCInstPrinter *IP,
232 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000233
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000234 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000235 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000238 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
239 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000241
Jim Grosbach4b905842013-09-20 23:08:21 +0000242 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000244 bool parseIdentifier(StringRef &Res) override;
245 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000246
Craig Topper59be68f2014-03-08 07:14:16 +0000247 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000248 /// }
249
250private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000251
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000252 bool parseStatement(ParseStatementInfo &Info,
253 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 void eatToEndOfLine();
Craig Topper3c76c522015-09-20 23:35:59 +0000255 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000256
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000258 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000259 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000260 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000261 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000262 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000263
Eli Benderskya313ae62013-01-16 18:56:50 +0000264 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Lookup a previously defined macro.
271 /// \param Name Macro name.
272 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000276 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
281 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000282 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000283
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000284 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000285 ///
286 /// \param M The macro.
287 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000289
290 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000292
David Majnemer91fc4c22014-01-29 18:57:46 +0000293 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000294 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000295
296 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000297 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000298
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 void printMacroInstantiations();
300 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000301 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000302 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000303 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000304 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000305
Jim Grosbach4b905842013-09-20 23:08:21 +0000306 /// \brief Enter the specified file. This returns true on failure.
307 bool enterIncludeFile(const std::string &Filename);
308
309 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000310 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000311 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000312
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000313 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000314 /// current token is not set; clients should ensure Lex() is called
315 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000316 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000317 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000318 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000319 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000320
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321 /// \brief Parse up to the end of statement and a return the contents from the
322 /// current token until the end of the statement; the current token on exit
323 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000324 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000325
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000326 /// \brief Parse until the end of a statement or a comma is encountered,
327 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000328 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000329
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000331 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000333 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
334 MCBinaryExpr::Opcode &Kind);
335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
337 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
338 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000339
Jim Grosbach4b905842013-09-20 23:08:21 +0000340 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000341
Eli Bendersky17233942013-01-15 22:59:42 +0000342 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000343 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_NO_DIRECTIVE, // Placeholder
345 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000346 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000347 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
348 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000349 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000350 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000351 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000352 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
353 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
354 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
355 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000356 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000357 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
358 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000359 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
360 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
361 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
362 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
363 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
364 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000365 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000366 DK_MACROS_ON, DK_MACROS_OFF,
367 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000368 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000369 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000370 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371 };
372
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000374 /// directives parsed by this class.
375 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000376
377 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000379 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000381 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
383 bool parseDirectiveFill(); // ".fill"
384 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000385 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
387 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000388 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000390
Eli Bendersky17233942013-01-15 22:59:42 +0000391 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveFile(SMLoc DirectiveLoc);
393 bool parseDirectiveLine();
394 bool parseDirectiveLoc();
395 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000396
397 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000398 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000399 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000400 bool parseDirectiveCFISections();
401 bool parseDirectiveCFIStartProc();
402 bool parseDirectiveCFIEndProc();
403 bool parseDirectiveCFIDefCfaOffset();
404 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIAdjustCfaOffset();
406 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
409 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
410 bool parseDirectiveCFIRememberState();
411 bool parseDirectiveCFIRestoreState();
412 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
413 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
414 bool parseDirectiveCFIEscape();
415 bool parseDirectiveCFISignalFrame();
416 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000417
418 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000420 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveEndMacro(StringRef Directive);
422 bool parseDirectiveMacro(SMLoc DirectiveLoc);
423 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000424
Eli Benderskyf483ff92012-12-20 19:05:53 +0000425 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000427 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000429 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000431
Eli Bendersky17233942013-01-15 22:59:42 +0000432 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000434
435 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000437
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000439 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000441
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000443
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveAbort(); // ".abort"
445 bool parseDirectiveInclude(); // ".include"
446 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000447
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000448 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
449 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000450 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000452 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000454 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
455 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000456 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
458 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
459 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
460 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000461 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000462
Jim Grosbach4b905842013-09-20 23:08:21 +0000463 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000464 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000465
Rafael Espindola34b9c512012-06-03 23:57:14 +0000466 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
468 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000469 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000470 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
472 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
473 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000474
Chad Rosierc7f552c2013-02-12 21:33:51 +0000475 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000476 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000477 size_t Len);
478
479 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000480 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000481
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000482 // "end"
483 bool parseDirectiveEnd(SMLoc DirectiveLoc);
484
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000485 // ".err" or ".error"
486 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000487
Nico Weber404012b2014-07-24 16:26:06 +0000488 // ".warning"
489 bool parseDirectiveWarning(SMLoc DirectiveLoc);
490
Eli Bendersky17233942013-01-15 22:59:42 +0000491 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000492};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000493}
Daniel Dunbar86033402010-07-12 17:54:38 +0000494
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000495namespace llvm {
496
497extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000498extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000499extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000500
501}
502
Chris Lattnerc35681b2010-01-19 19:46:13 +0000503enum { DEFAULT_ADDRSPACE = 0 };
504
David Blaikie9f380a32015-03-16 18:06:57 +0000505AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
506 const MCAsmInfo &MAI)
507 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
508 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000509 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000510 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000511 // Save the old handler.
512 SavedDiagHandler = SrcMgr.getDiagHandler();
513 SavedDiagContext = SrcMgr.getDiagContext();
514 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000515 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000516 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000517
Daniel Dunbarc5011082010-07-12 18:12:02 +0000518 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000519 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
520 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000521 PlatformParser.reset(createCOFFAsmParser());
522 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000523 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000524 PlatformParser.reset(createDarwinAsmParser());
525 IsDarwin = true;
526 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000527 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000528 PlatformParser.reset(createELFAsmParser());
529 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000530 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000531
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000532 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000533 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000534
535 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000536}
537
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000538AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000539 assert((HadError || ActiveMacros.empty()) &&
540 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000541}
542
Jim Grosbach4b905842013-09-20 23:08:21 +0000543void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000544 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000545 for (std::vector<MacroInstantiation *>::const_reverse_iterator
546 it = ActiveMacros.rbegin(),
547 ie = ActiveMacros.rend();
548 it != ie; ++it)
549 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000550 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000551}
552
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000553void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
554 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
555 printMacroInstantiations();
556}
557
Chris Lattnera3a06812011-10-16 04:47:35 +0000558bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000559 if(getTargetParser().getTargetOptions().MCNoWarn)
560 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000561 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000562 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000563 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
564 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000565 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000566}
567
Chris Lattnera3a06812011-10-16 04:47:35 +0000568bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000569 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000570 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
571 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000572 return true;
573}
574
Jim Grosbach4b905842013-09-20 23:08:21 +0000575bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000576 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000577 unsigned NewBuf =
578 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
579 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000580 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000581
Sean Callanan7a77eae2010-01-21 00:19:58 +0000582 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000583 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000584 return false;
585}
Daniel Dunbar43235712010-07-18 18:54:11 +0000586
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000587/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000588/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000589/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000590bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000592 unsigned NewBuf =
593 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
594 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000595 return true;
596
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000597 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000598 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000599 return false;
600}
601
Alp Tokera55b95b2014-07-06 10:33:31 +0000602void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
603 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000604 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
605 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000606}
607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608const AsmToken &AsmParser::Lex() {
609 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000610
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Eof)) {
612 // If this is the end of an included file, pop the parent file off the
613 // include stack.
614 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
615 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000616 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000617 tok = &Lexer.Lex();
618 }
619 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000620
Sean Callanan7a77eae2010-01-21 00:19:58 +0000621 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000622 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000623
Sean Callanan7a77eae2010-01-21 00:19:58 +0000624 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000625}
626
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000627bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000628 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000629 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000630 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000631
Chris Lattner36e02122009-06-21 20:54:55 +0000632 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000633 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000634
635 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000636 AsmCond StartingCondState = TheCondState;
637
Kevin Enderby6469fc22011-11-01 22:27:22 +0000638 // If we are generating dwarf for assembly source files save the initial text
639 // section and generate a .file directive.
640 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000641 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000642 if (!Sec->getBeginSymbol()) {
643 MCSymbol *SectionStartSym = getContext().createTempSymbol();
644 getStreamer().EmitLabel(SectionStartSym);
645 Sec->setBeginSymbol(SectionStartSym);
646 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000647 bool InsertResult = getContext().addGenDwarfSection(Sec);
648 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000649 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000650 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
651 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000652 }
653
Chris Lattner73f36112009-07-02 21:53:43 +0000654 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000655 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000656 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000657 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000658 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000659
Daniel Dunbar43325c42010-09-09 22:42:56 +0000660 // We had an error, validate that one was emitted and recover by skipping to
661 // the next line.
662 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000663 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000664 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000665
666 if (TheCondState.TheCond != StartingCondState.TheCond ||
667 TheCondState.Ignore != StartingCondState.Ignore)
668 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669
670 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000671 const auto &LineTables = getContext().getMCDwarfLineTables();
672 if (!LineTables.empty()) {
673 unsigned Index = 0;
674 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
675 if (File.Name.empty() && Index != 0)
676 TokError("unassigned file number: " + Twine(Index) +
677 " for .file directives");
678 ++Index;
679 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000680 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000681
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000682 // Check to see that all assembler local symbols were actually defined.
683 // Targets that don't do subsections via symbols may not want this, though,
684 // so conservatively exclude them. Only do this if we're finalizing, though,
685 // as otherwise we won't necessarilly have seen everything yet.
686 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
Craig Topper84008482015-10-10 05:38:14 +0000687 for (const auto &TableEntry : getContext().getSymbols()) {
688 MCSymbol *Sym = TableEntry.getValue();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000689 // Variable symbols may not be marked as defined, so check those
690 // explicitly. If we know it's a variable, we have a definition for
691 // the purposes of this check.
692 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
693 // FIXME: We would really like to refer back to where the symbol was
694 // first referenced for a source location. We need to add something
695 // to track that. Currently, we just point to the end of the file.
Jim Grosbach0fdd5722015-10-16 22:07:59 +0000696 return Error(getLexer().getLoc(), "assembler local symbol '" +
697 Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000698 }
699 }
700
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000701 // Finalize the output stream if there are no errors and if the client wants
702 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000703 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000704 Out.Finish();
705
Oliver Stannard07b43d32015-11-17 09:58:07 +0000706 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000707}
708
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000709void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000710 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000711 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000712 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000713 }
714}
715
Jim Grosbach4b905842013-09-20 23:08:21 +0000716/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000717void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000718 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000719 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000720
Chris Lattnere5074c42009-06-22 01:29:09 +0000721 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000722 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000723 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000724}
725
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000726StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000727 const char *Start = getTok().getLoc().getPointer();
728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000730 Lex();
731
732 const char *End = getTok().getLoc().getPointer();
733 return StringRef(Start, End - Start);
734}
Chris Lattner78db3622009-06-22 05:51:26 +0000735
Jim Grosbach4b905842013-09-20 23:08:21 +0000736StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000737 const char *Start = getTok().getLoc().getPointer();
738
739 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000740 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000741 Lex();
742
743 const char *End = getTok().getLoc().getPointer();
744 return StringRef(Start, End - Start);
745}
746
Jim Grosbach4b905842013-09-20 23:08:21 +0000747/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000748/// NOTE: This assumes the leading '(' has already been consumed.
749///
750/// parenexpr ::= expr)
751///
Jim Grosbach4b905842013-09-20 23:08:21 +0000752bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
753 if (parseExpression(Res))
754 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000755 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000756 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000757 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000758 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000759 return false;
760}
Chris Lattner78db3622009-06-22 05:51:26 +0000761
Jim Grosbach4b905842013-09-20 23:08:21 +0000762/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763/// NOTE: This assumes the leading '[' has already been consumed.
764///
765/// bracketexpr ::= expr]
766///
Jim Grosbach4b905842013-09-20 23:08:21 +0000767bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
768 if (parseExpression(Res))
769 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000770 if (Lexer.isNot(AsmToken::RBrac))
771 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000772 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000773 Lex();
774 return false;
775}
776
Jim Grosbach4b905842013-09-20 23:08:21 +0000777/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000778/// primaryexpr ::= (parenexpr
779/// primaryexpr ::= symbol
780/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000781/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000782/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000783bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000784 SMLoc FirstTokenLoc = getLexer().getLoc();
785 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
786 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000787 default:
788 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000789 // If we have an error assume that we've already handled it.
790 case AsmToken::Error:
791 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000792 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000793 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000794 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000795 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000796 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000797 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000798 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000799 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000800 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000801 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000802 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000803 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000804 if (FirstTokenKind == AsmToken::Dollar) {
805 if (Lexer.getMAI().getDollarIsPC()) {
806 // This is a '$' reference, which references the current PC. Emit a
807 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000808 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000810 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000811 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000812 EndLoc = FirstTokenLoc;
813 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000814 }
815 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000816 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000817 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000818 // Parse symbol variant
819 std::pair<StringRef, StringRef> Split;
820 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000821 if (FirstTokenKind == AsmToken::String) {
822 if (Lexer.is(AsmToken::At)) {
823 Lexer.Lex(); // eat @
824 SMLoc AtLoc = getLexer().getLoc();
825 StringRef VName;
826 if (parseIdentifier(VName))
827 return Error(AtLoc, "expected symbol variant after '@'");
828
829 Split = std::make_pair(Identifier, VName);
830 }
831 } else {
832 Split = Identifier.split('@');
833 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000834 } else if (Lexer.is(AsmToken::LParen)) {
835 Lexer.Lex(); // eat (
836 StringRef VName;
837 parseIdentifier(VName);
838 if (Lexer.isNot(AsmToken::RParen)) {
839 return Error(Lexer.getTok().getLoc(),
840 "unexpected token in variant, expected ')'");
841 }
842 Lexer.Lex(); // eat )
843 Split = std::make_pair(Identifier, VName);
844 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000845
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000846 EndLoc = SMLoc::getFromPointer(Identifier.end());
847
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000849 StringRef SymbolName = Identifier;
850 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000851
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000852 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000853 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000854 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000855 if (Variant != MCSymbolRefExpr::VK_Invalid) {
856 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000857 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000858 Variant = MCSymbolRefExpr::VK_None;
859 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000860 return Error(SMLoc::getFromPointer(Split.second.begin()),
861 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000862 }
863 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000864
Jim Grosbach6f482002015-05-18 18:43:14 +0000865 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000866
Daniel Dunbard20cda02009-10-16 01:34:54 +0000867 // If this is an absolute variable reference, substitute it now to preserve
868 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000869 if (Sym->isVariable() &&
870 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000871 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000872 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000873
Vedant Kumar86dbd922015-08-31 17:44:53 +0000874 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000875 return false;
876 }
877
878 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000879 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000880 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000881 }
David Woodhousef42a6662014-02-01 16:20:54 +0000882 case AsmToken::BigNum:
883 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 case AsmToken::Integer: {
885 SMLoc Loc = getTok().getLoc();
886 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000887 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000888 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000889 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 // Look for 'b' or 'f' following an Integer as a directional label
891 if (Lexer.getKind() == AsmToken::Identifier) {
892 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000893 // Lookup the symbol variant if used.
894 std::pair<StringRef, StringRef> Split = IDVal.split('@');
895 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
896 if (Split.first.size() != IDVal.size()) {
897 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000898 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000899 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000900 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000901 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000902 if (IDVal == "f" || IDVal == "b") {
903 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000904 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000905 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000906 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000907 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000909 Lex(); // Eat identifier.
910 }
911 }
Chris Lattner78db3622009-06-22 05:51:26 +0000912 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000913 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000914 case AsmToken::Real: {
915 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000916 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000917 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000918 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000919 Lex(); // Eat token.
920 return false;
921 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000922 case AsmToken::Dot: {
923 // This is a '.' reference, which references the current PC. Emit a
924 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000925 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000926 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000927 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000928 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000929 Lex(); // Eat identifier.
930 return false;
931 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000935 case AsmToken::LBrac:
936 if (!PlatformParser->HasBracketExpressions())
937 return TokError("brackets expression not supported on this target");
938 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000939 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000940 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000941 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000942 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000943 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000944 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000945 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000946 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000947 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000948 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000949 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000950 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000951 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000952 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000953 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000954 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000955 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000956 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000957 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000958 }
959}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000960
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000961bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000962 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000963 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000964}
965
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000967AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000969 // Ask the target implementation about this expression first.
970 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
971 if (NewE)
972 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000973 // Recurse over the given expression, rebuilding it to apply the given variant
974 // if there is exactly one symbol.
975 switch (E->getKind()) {
976 case MCExpr::Target:
977 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000978 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979
980 case MCExpr::SymbolRef: {
981 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
982
983 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000984 TokError("invalid variant on expression '" + getTok().getIdentifier() +
985 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 return E;
987 }
988
Jim Grosbach13760bd2015-05-30 01:25:56 +0000989 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990 }
991
992 case MCExpr::Unary: {
993 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000994 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000996 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000997 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998 }
999
1000 case MCExpr::Binary: {
1001 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001002 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1003 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004
1005 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001006 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007
Jim Grosbach4b905842013-09-20 23:08:21 +00001008 if (!LHS)
1009 LHS = BE->getLHS();
1010 if (!RHS)
1011 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001012
Jim Grosbach13760bd2015-05-30 01:25:56 +00001013 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001014 }
1015 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001016
Craig Toppera2886c22012-02-07 05:05:23 +00001017 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001018}
1019
Jim Grosbach4b905842013-09-20 23:08:21 +00001020/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001021///
Jim Grosbachbd164242011-08-20 16:24:13 +00001022/// expr ::= expr &&,|| expr -> lowest.
1023/// expr ::= expr |,^,&,! expr
1024/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1025/// expr ::= expr <<,>> expr
1026/// expr ::= expr +,- expr
1027/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001028/// expr ::= primaryexpr
1029///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001030bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001031 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001032 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001033 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001034 return true;
1035
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 // As a special case, we support 'a op b @ modifier' by rewriting the
1037 // expression to include the modifier. This is inefficient, but in general we
1038 // expect users to use 'a@modifier op b'.
1039 if (Lexer.getKind() == AsmToken::At) {
1040 Lex();
1041
1042 if (Lexer.isNot(AsmToken::Identifier))
1043 return TokError("unexpected symbol modifier following '@'");
1044
1045 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001046 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001047 if (Variant == MCSymbolRefExpr::VK_Invalid)
1048 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1049
Jim Grosbach4b905842013-09-20 23:08:21 +00001050 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 if (!ModifiedRes) {
1052 return TokError("invalid modifier '" + getTok().getIdentifier() +
1053 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001054 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001055
Daniel Dunbar55f16672010-09-17 02:47:07 +00001056 Res = ModifiedRes;
1057 Lex();
1058 }
1059
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001060 // Try to constant fold it up front, if possible.
1061 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001062 if (Res->evaluateAsAbsolute(Value))
1063 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001064
1065 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001066}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001067
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001068bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001069 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001071}
1072
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001073bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1074 SMLoc &EndLoc) {
1075 if (parseParenExpr(Res, EndLoc))
1076 return true;
1077
1078 for (; ParenDepth > 0; --ParenDepth) {
1079 if (parseBinOpRHS(1, Res, EndLoc))
1080 return true;
1081
1082 // We don't Lex() the last RParen.
1083 // This is the same behavior as parseParenExpression().
1084 if (ParenDepth - 1 > 0) {
1085 if (Lexer.isNot(AsmToken::RParen))
1086 return TokError("expected ')' in parentheses expression");
1087 EndLoc = Lexer.getTok().getEndLoc();
1088 Lex();
1089 }
1090 }
1091 return false;
1092}
1093
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001094bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001095 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001096
Daniel Dunbar75630b32009-06-30 02:10:03 +00001097 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001098 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001099 return true;
1100
Jim Grosbach13760bd2015-05-30 01:25:56 +00001101 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001102 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001103
1104 return false;
1105}
1106
David Majnemer0993e0b2015-10-26 03:15:34 +00001107static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1108 MCBinaryExpr::Opcode &Kind,
1109 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001110 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001111 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001112 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001113
Jim Grosbach4b905842013-09-20 23:08:21 +00001114 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001115 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001116 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001117 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001118 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001119 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001120 return 1;
1121
Jim Grosbach4b905842013-09-20 23:08:21 +00001122 // Low Precedence: |, &, ^
1123 //
1124 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001125 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001126 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001127 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001130 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001133 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001134
Jim Grosbach4b905842013-09-20 23:08:21 +00001135 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001136 case AsmToken::EqualEqual:
1137 Kind = MCBinaryExpr::EQ;
1138 return 3;
1139 case AsmToken::ExclaimEqual:
1140 case AsmToken::LessGreater:
1141 Kind = MCBinaryExpr::NE;
1142 return 3;
1143 case AsmToken::Less:
1144 Kind = MCBinaryExpr::LT;
1145 return 3;
1146 case AsmToken::LessEqual:
1147 Kind = MCBinaryExpr::LTE;
1148 return 3;
1149 case AsmToken::Greater:
1150 Kind = MCBinaryExpr::GT;
1151 return 3;
1152 case AsmToken::GreaterEqual:
1153 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001154 return 3;
1155
Jim Grosbach4b905842013-09-20 23:08:21 +00001156 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001157 case AsmToken::LessLess:
1158 Kind = MCBinaryExpr::Shl;
1159 return 4;
1160 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001161 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001162 return 4;
1163
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001165 case AsmToken::Plus:
1166 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001167 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001168 case AsmToken::Minus:
1169 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001170 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001171
Jim Grosbach4b905842013-09-20 23:08:21 +00001172 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001173 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001174 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001175 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001176 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001177 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001178 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001179 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001181 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001182 }
1183}
1184
David Majnemer0993e0b2015-10-26 03:15:34 +00001185static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1186 MCBinaryExpr::Opcode &Kind,
1187 bool ShouldUseLogicalShr) {
1188 switch (K) {
1189 default:
1190 return 0; // not a binop.
1191
1192 // Lowest Precedence: &&, ||
1193 case AsmToken::AmpAmp:
1194 Kind = MCBinaryExpr::LAnd;
1195 return 2;
1196 case AsmToken::PipePipe:
1197 Kind = MCBinaryExpr::LOr;
1198 return 1;
1199
1200 // Low Precedence: ==, !=, <>, <, <=, >, >=
1201 case AsmToken::EqualEqual:
1202 Kind = MCBinaryExpr::EQ;
1203 return 3;
1204 case AsmToken::ExclaimEqual:
1205 case AsmToken::LessGreater:
1206 Kind = MCBinaryExpr::NE;
1207 return 3;
1208 case AsmToken::Less:
1209 Kind = MCBinaryExpr::LT;
1210 return 3;
1211 case AsmToken::LessEqual:
1212 Kind = MCBinaryExpr::LTE;
1213 return 3;
1214 case AsmToken::Greater:
1215 Kind = MCBinaryExpr::GT;
1216 return 3;
1217 case AsmToken::GreaterEqual:
1218 Kind = MCBinaryExpr::GTE;
1219 return 3;
1220
1221 // Low Intermediate Precedence: +, -
1222 case AsmToken::Plus:
1223 Kind = MCBinaryExpr::Add;
1224 return 4;
1225 case AsmToken::Minus:
1226 Kind = MCBinaryExpr::Sub;
1227 return 4;
1228
1229 // High Intermediate Precedence: |, &, ^
1230 //
1231 // FIXME: gas seems to support '!' as an infix operator?
1232 case AsmToken::Pipe:
1233 Kind = MCBinaryExpr::Or;
1234 return 5;
1235 case AsmToken::Caret:
1236 Kind = MCBinaryExpr::Xor;
1237 return 5;
1238 case AsmToken::Amp:
1239 Kind = MCBinaryExpr::And;
1240 return 5;
1241
1242 // Highest Precedence: *, /, %, <<, >>
1243 case AsmToken::Star:
1244 Kind = MCBinaryExpr::Mul;
1245 return 6;
1246 case AsmToken::Slash:
1247 Kind = MCBinaryExpr::Div;
1248 return 6;
1249 case AsmToken::Percent:
1250 Kind = MCBinaryExpr::Mod;
1251 return 6;
1252 case AsmToken::LessLess:
1253 Kind = MCBinaryExpr::Shl;
1254 return 6;
1255 case AsmToken::GreaterGreater:
1256 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1257 return 6;
1258 }
1259}
1260
1261unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1262 MCBinaryExpr::Opcode &Kind) {
1263 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1264 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1265 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1266}
1267
Jim Grosbach4b905842013-09-20 23:08:21 +00001268/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001269/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001270bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001271 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001272 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001273 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001274 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001275
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001276 // If the next token is lower precedence than we are allowed to eat, return
1277 // successfully with what we ate already.
1278 if (TokPrec < Precedence)
1279 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001280
Sean Callanan686ed8d2010-01-19 20:22:31 +00001281 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001283 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001284 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001285 if (parsePrimaryExpr(RHS, EndLoc))
1286 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001287
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001288 // If BinOp binds less tightly with RHS than the operator after RHS, let
1289 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001290 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001291 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001292 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1293 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001294
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001295 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001296 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001297 }
1298}
1299
Chris Lattner36e02122009-06-21 20:54:55 +00001300/// ParseStatement:
1301/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001302/// ::= Label* Directive ...Operands... EndOfStatement
1303/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001304bool AsmParser::parseStatement(ParseStatementInfo &Info,
1305 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001306 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001307 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001308 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001309 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001310 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001311
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001312 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001313 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001314 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001315 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001316 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001317 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001318 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001319 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001320
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001321 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001322 if (Lexer.is(AsmToken::Integer)) {
1323 LocalLabelVal = getTok().getIntVal();
1324 if (LocalLabelVal < 0) {
1325 if (!TheCondState.Ignore)
1326 return TokError("unexpected token at start of statement");
1327 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001328 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001329 IDVal = getTok().getString();
1330 Lex(); // Consume the integer token to be used as an identifier token.
1331 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001332 if (!TheCondState.Ignore)
1333 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001334 }
1335 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001336 } else if (Lexer.is(AsmToken::Dot)) {
1337 // Treat '.' as a valid identifier in this context.
1338 Lex();
1339 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001340 } else if (Lexer.is(AsmToken::LCurly)) {
1341 // Treat '{' as a valid identifier in this context.
1342 Lex();
1343 IDVal = "{";
1344
1345 } else if (Lexer.is(AsmToken::RCurly)) {
1346 // Treat '}' as a valid identifier in this context.
1347 Lex();
1348 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001349 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001350 if (!TheCondState.Ignore)
1351 return TokError("unexpected token at start of statement");
1352 IDVal = "";
1353 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001354
Chris Lattner926885c2010-04-17 18:14:27 +00001355 // Handle conditional assembly here before checking for skipping. We
1356 // have to do this so that .endif isn't skipped in a ".if 0" block for
1357 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001358 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001359 DirectiveKindMap.find(IDVal);
1360 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1361 ? DK_NO_DIRECTIVE
1362 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001363 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001364 default:
1365 break;
1366 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001367 case DK_IFEQ:
1368 case DK_IFGE:
1369 case DK_IFGT:
1370 case DK_IFLE:
1371 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001372 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001373 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001374 case DK_IFB:
1375 return parseDirectiveIfb(IDLoc, true);
1376 case DK_IFNB:
1377 return parseDirectiveIfb(IDLoc, false);
1378 case DK_IFC:
1379 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001380 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001381 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001382 case DK_IFNC:
1383 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001384 case DK_IFNES:
1385 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001386 case DK_IFDEF:
1387 return parseDirectiveIfdef(IDLoc, true);
1388 case DK_IFNDEF:
1389 case DK_IFNOTDEF:
1390 return parseDirectiveIfdef(IDLoc, false);
1391 case DK_ELSEIF:
1392 return parseDirectiveElseIf(IDLoc);
1393 case DK_ELSE:
1394 return parseDirectiveElse(IDLoc);
1395 case DK_ENDIF:
1396 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001397 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001398
Eli Bendersky88024712013-01-16 19:32:36 +00001399 // Ignore the statement if in the middle of inactive conditional
1400 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001401 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001402 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001403 return false;
1404 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001405
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001406 // FIXME: Recurse on local labels?
1407
1408 // See what kind of statement we have.
1409 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001410 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001411 if (!getTargetParser().isLabel(ID))
1412 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001413 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001414
Chris Lattner36e02122009-06-21 20:54:55 +00001415 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001416 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001417
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001418 // Diagnose attempt to use '.' as a label.
1419 if (IDVal == ".")
1420 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1421
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001422 // Diagnose attempt to use a variable as a label.
1423 //
1424 // FIXME: Diagnostics. Note the location of the definition as a label.
1425 // FIXME: This doesn't diagnose assignment to a symbol which has been
1426 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001427 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001428 if (LocalLabelVal == -1) {
1429 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001430 StringRef RewrittenLabel =
1431 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1432 assert(RewrittenLabel.size() &&
1433 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001434 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1435 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001436 IDVal = RewrittenLabel;
1437 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001438 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001439 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001440 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001441
1442 Sym->redefineIfPossible();
1443
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001444 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001445 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001446
Daniel Dunbare73b2672009-08-26 22:13:22 +00001447 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001448 if (!ParsingInlineAsm)
1449 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001450
Kevin Enderbye7739d42011-12-09 18:09:40 +00001451 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001452 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001453 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001454 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1455 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001456
Tim Northover1744d0a2013-10-25 12:49:50 +00001457 getTargetParser().onLabelParsed(Sym);
1458
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001459 // Consume any end of statement token, if present, to avoid spurious
1460 // AddBlankLine calls().
1461 if (Lexer.is(AsmToken::EndOfStatement)) {
1462 Lex();
1463 if (Lexer.is(AsmToken::Eof))
1464 return false;
1465 }
1466
Eli Friedman0f4871d2012-10-22 23:58:19 +00001467 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001468 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001469
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001470 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001471 if (!getTargetParser().equalIsAsmAssignment())
1472 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001473 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001474 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001475
Jim Grosbach4b905842013-09-20 23:08:21 +00001476 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001477
1478 default: // Normal instruction or directive.
1479 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001480 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001481
1482 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001483 if (areMacrosEnabled())
1484 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1485 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001486 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001487
Michael J. Spencer530ce852010-10-09 11:00:50 +00001488 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001489
Eli Bendersky17233942013-01-15 22:59:42 +00001490 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001491 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001492 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001493 //
Eli Bendersky17233942013-01-15 22:59:42 +00001494 // 1. The target-specific assembly parser. Some directives are target
1495 // specific or may potentially behave differently on certain targets.
1496 // 2. Asm parser extensions. For example, platform-specific parsers
1497 // (like the ELF parser) register themselves as extensions.
1498 // 3. The generic directive parser implemented by this class. These are
1499 // all the directives that behave in a target and platform independent
1500 // manner, or at least have a default behavior that's shared between
1501 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001502
Eli Bendersky17233942013-01-15 22:59:42 +00001503 // First query the target-specific parser. It will return 'true' if it
1504 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001505 if (!getTargetParser().ParseDirective(ID))
1506 return false;
1507
Alp Tokercb402912014-01-24 17:20:08 +00001508 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001509 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001510 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1511 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001512 if (Handler.first)
1513 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1514
1515 // Finally, if no one else is interested in this directive, it must be
1516 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001517 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001518 default:
1519 break;
1520 case DK_SET:
1521 case DK_EQU:
1522 return parseDirectiveSet(IDVal, true);
1523 case DK_EQUIV:
1524 return parseDirectiveSet(IDVal, false);
1525 case DK_ASCII:
1526 return parseDirectiveAscii(IDVal, false);
1527 case DK_ASCIZ:
1528 case DK_STRING:
1529 return parseDirectiveAscii(IDVal, true);
1530 case DK_BYTE:
1531 return parseDirectiveValue(1);
1532 case DK_SHORT:
1533 case DK_VALUE:
1534 case DK_2BYTE:
1535 return parseDirectiveValue(2);
1536 case DK_LONG:
1537 case DK_INT:
1538 case DK_4BYTE:
1539 return parseDirectiveValue(4);
1540 case DK_QUAD:
1541 case DK_8BYTE:
1542 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001543 case DK_OCTA:
1544 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 case DK_SINGLE:
1546 case DK_FLOAT:
1547 return parseDirectiveRealValue(APFloat::IEEEsingle);
1548 case DK_DOUBLE:
1549 return parseDirectiveRealValue(APFloat::IEEEdouble);
1550 case DK_ALIGN: {
1551 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1552 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1553 }
1554 case DK_ALIGN32: {
1555 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1556 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1557 }
1558 case DK_BALIGN:
1559 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1560 case DK_BALIGNW:
1561 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1562 case DK_BALIGNL:
1563 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1564 case DK_P2ALIGN:
1565 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1566 case DK_P2ALIGNW:
1567 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1568 case DK_P2ALIGNL:
1569 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1570 case DK_ORG:
1571 return parseDirectiveOrg();
1572 case DK_FILL:
1573 return parseDirectiveFill();
1574 case DK_ZERO:
1575 return parseDirectiveZero();
1576 case DK_EXTERN:
1577 eatToEndOfStatement(); // .extern is the default, ignore it.
1578 return false;
1579 case DK_GLOBL:
1580 case DK_GLOBAL:
1581 return parseDirectiveSymbolAttribute(MCSA_Global);
1582 case DK_LAZY_REFERENCE:
1583 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1584 case DK_NO_DEAD_STRIP:
1585 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1586 case DK_SYMBOL_RESOLVER:
1587 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1588 case DK_PRIVATE_EXTERN:
1589 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1590 case DK_REFERENCE:
1591 return parseDirectiveSymbolAttribute(MCSA_Reference);
1592 case DK_WEAK_DEFINITION:
1593 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1594 case DK_WEAK_REFERENCE:
1595 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1596 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1597 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1598 case DK_COMM:
1599 case DK_COMMON:
1600 return parseDirectiveComm(/*IsLocal=*/false);
1601 case DK_LCOMM:
1602 return parseDirectiveComm(/*IsLocal=*/true);
1603 case DK_ABORT:
1604 return parseDirectiveAbort();
1605 case DK_INCLUDE:
1606 return parseDirectiveInclude();
1607 case DK_INCBIN:
1608 return parseDirectiveIncbin();
1609 case DK_CODE16:
1610 case DK_CODE16GCC:
1611 return TokError(Twine(IDVal) + " not supported yet");
1612 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001613 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001614 case DK_IRP:
1615 return parseDirectiveIrp(IDLoc);
1616 case DK_IRPC:
1617 return parseDirectiveIrpc(IDLoc);
1618 case DK_ENDR:
1619 return parseDirectiveEndr(IDLoc);
1620 case DK_BUNDLE_ALIGN_MODE:
1621 return parseDirectiveBundleAlignMode();
1622 case DK_BUNDLE_LOCK:
1623 return parseDirectiveBundleLock();
1624 case DK_BUNDLE_UNLOCK:
1625 return parseDirectiveBundleUnlock();
1626 case DK_SLEB128:
1627 return parseDirectiveLEB128(true);
1628 case DK_ULEB128:
1629 return parseDirectiveLEB128(false);
1630 case DK_SPACE:
1631 case DK_SKIP:
1632 return parseDirectiveSpace(IDVal);
1633 case DK_FILE:
1634 return parseDirectiveFile(IDLoc);
1635 case DK_LINE:
1636 return parseDirectiveLine();
1637 case DK_LOC:
1638 return parseDirectiveLoc();
1639 case DK_STABS:
1640 return parseDirectiveStabs();
1641 case DK_CFI_SECTIONS:
1642 return parseDirectiveCFISections();
1643 case DK_CFI_STARTPROC:
1644 return parseDirectiveCFIStartProc();
1645 case DK_CFI_ENDPROC:
1646 return parseDirectiveCFIEndProc();
1647 case DK_CFI_DEF_CFA:
1648 return parseDirectiveCFIDefCfa(IDLoc);
1649 case DK_CFI_DEF_CFA_OFFSET:
1650 return parseDirectiveCFIDefCfaOffset();
1651 case DK_CFI_ADJUST_CFA_OFFSET:
1652 return parseDirectiveCFIAdjustCfaOffset();
1653 case DK_CFI_DEF_CFA_REGISTER:
1654 return parseDirectiveCFIDefCfaRegister(IDLoc);
1655 case DK_CFI_OFFSET:
1656 return parseDirectiveCFIOffset(IDLoc);
1657 case DK_CFI_REL_OFFSET:
1658 return parseDirectiveCFIRelOffset(IDLoc);
1659 case DK_CFI_PERSONALITY:
1660 return parseDirectiveCFIPersonalityOrLsda(true);
1661 case DK_CFI_LSDA:
1662 return parseDirectiveCFIPersonalityOrLsda(false);
1663 case DK_CFI_REMEMBER_STATE:
1664 return parseDirectiveCFIRememberState();
1665 case DK_CFI_RESTORE_STATE:
1666 return parseDirectiveCFIRestoreState();
1667 case DK_CFI_SAME_VALUE:
1668 return parseDirectiveCFISameValue(IDLoc);
1669 case DK_CFI_RESTORE:
1670 return parseDirectiveCFIRestore(IDLoc);
1671 case DK_CFI_ESCAPE:
1672 return parseDirectiveCFIEscape();
1673 case DK_CFI_SIGNAL_FRAME:
1674 return parseDirectiveCFISignalFrame();
1675 case DK_CFI_UNDEFINED:
1676 return parseDirectiveCFIUndefined(IDLoc);
1677 case DK_CFI_REGISTER:
1678 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001679 case DK_CFI_WINDOW_SAVE:
1680 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001681 case DK_MACROS_ON:
1682 case DK_MACROS_OFF:
1683 return parseDirectiveMacrosOnOff(IDVal);
1684 case DK_MACRO:
1685 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001686 case DK_EXITM:
1687 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 case DK_ENDM:
1689 case DK_ENDMACRO:
1690 return parseDirectiveEndMacro(IDVal);
1691 case DK_PURGEM:
1692 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001693 case DK_END:
1694 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001695 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001696 return parseDirectiveError(IDLoc, false);
1697 case DK_ERROR:
1698 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001699 case DK_WARNING:
1700 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001701 case DK_RELOC:
1702 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001703 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001704
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001705 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001706 }
Chris Lattner36e02122009-06-21 20:54:55 +00001707
Chad Rosierc7f552c2013-02-12 21:33:51 +00001708 // __asm _emit or __asm __emit
1709 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1710 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001711 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001712
1713 // __asm align
1714 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001716
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001717 if (ParsingInlineAsm && (IDVal == "even"))
1718 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001719 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001720
Chris Lattner7cbfa442010-05-19 23:34:33 +00001721 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001722 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001723 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001724 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001725 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001726 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001727
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001728 // Dump the parsed representation, if requested.
1729 if (getShowParsedOperands()) {
1730 SmallString<256> Str;
1731 raw_svector_ostream OS(Str);
1732 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001733 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001734 if (i != 0)
1735 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001736 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001737 }
1738 OS << "]";
1739
Jim Grosbach4b905842013-09-20 23:08:21 +00001740 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001741 }
1742
Oliver Stannard8b273082014-06-19 15:52:37 +00001743 // If we are generating dwarf for the current section then generate a .loc
1744 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001745 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001746 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001747 getStreamer().getCurrentSection().first)) {
1748 unsigned Line;
1749 if (ActiveMacros.empty())
1750 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1751 else
Frederic Riss16238d92015-06-25 21:57:33 +00001752 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1753 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001754
Eli Bendersky88024712013-01-16 19:32:36 +00001755 // If we previously parsed a cpp hash file line comment then make sure the
1756 // current Dwarf File is for the CppHashFilename if not then emit the
1757 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001758 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001759 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1760 0, StringRef(), CppHashFilename);
1761 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001762
Jim Grosbach4b905842013-09-20 23:08:21 +00001763 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1764 // cache with the different Loc from the call above we save the last
1765 // info we queried here with SrcMgr.FindLineNumber().
1766 unsigned CppHashLocLineNo;
1767 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1768 CppHashLocLineNo = LastQueryLine;
1769 else {
1770 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1771 LastQueryLine = CppHashLocLineNo;
1772 LastQueryIDLoc = CppHashLoc;
1773 LastQueryBuffer = CppHashBuf;
1774 }
1775 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001776 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001777
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 getStreamer().EmitDwarfLocDirective(
1779 getContext().getGenDwarfFileNumber(), Line, 0,
1780 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1781 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001782 }
1783
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001784 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001785 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001786 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001787 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1788 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001789 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001790 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001791
Chris Lattnera2a9d162010-09-11 16:18:25 +00001792 // Don't skip the rest of the line, the instruction parser is responsible for
1793 // that.
1794 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001795}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001796
Jim Grosbach4b905842013-09-20 23:08:21 +00001797/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001798/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001799void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001800 if (!Lexer.is(AsmToken::EndOfStatement))
1801 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001802 // Eat EOL.
1803 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001804}
1805
Jim Grosbach4b905842013-09-20 23:08:21 +00001806/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001807/// ::= # number "filename"
1808/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001809bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001810 Lex(); // Eat the hash token.
1811
1812 if (getLexer().isNot(AsmToken::Integer)) {
1813 // Consume the line since in cases it is not a well-formed line directive,
1814 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001815 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001816 return false;
1817 }
1818
1819 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001820 Lex();
1821
1822 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001823 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001824 return false;
1825 }
1826
1827 StringRef Filename = getTok().getString();
1828 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001829 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001830
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001831 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1832 CppHashLoc = L;
1833 CppHashFilename = Filename;
1834 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001835 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001836
1837 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001838 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001839 return false;
1840}
1841
Jim Grosbach4b905842013-09-20 23:08:21 +00001842/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001843/// for the Filename and LineNo if any in the diagnostic.
1844void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001845 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001846 raw_ostream &OS = errs();
1847
1848 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001849 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001850 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1851 unsigned CppHashBuf =
1852 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001853
Jim Grosbach4b905842013-09-20 23:08:21 +00001854 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001855 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001856 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1857 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1858 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001859 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1860 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001861 }
1862
Eric Christophera7c32732012-12-18 00:30:54 +00001863 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001864 // manager changed or buffer changed (like in a nested include) then just
1865 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001866 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001867 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001868 if (Parser->SavedDiagHandler)
1869 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1870 else
Craig Topper353eda42014-04-24 06:44:33 +00001871 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001872 return;
1873 }
1874
Eric Christophera7c32732012-12-18 00:30:54 +00001875 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001876 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1877 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001878 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001879
1880 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1881 int CppHashLocLineNo =
1882 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001883 int LineNo =
1884 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001885
Jim Grosbach4b905842013-09-20 23:08:21 +00001886 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1887 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001888 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001889
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001890 if (Parser->SavedDiagHandler)
1891 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1892 else
Craig Topper353eda42014-04-24 06:44:33 +00001893 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001894}
1895
Rafael Espindola2c064482012-08-21 18:29:30 +00001896// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1897// difference being that that function accepts '@' as part of identifiers and
1898// we can't do that. AsmLexer.cpp should probably be changed to handle
1899// '@' as a special case when needed.
1900static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001901 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1902 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001903}
1904
Rafael Espindola34b9c512012-06-03 23:57:14 +00001905bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001906 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001907 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001908 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001909 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001910 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001911 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001912 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001913
Preston Gurd05500642012-09-19 20:36:12 +00001914 // A macro without parameters is handled differently on Darwin:
1915 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001916 while (!Body.empty()) {
1917 // Scan for the next substitution.
1918 std::size_t End = Body.size(), Pos = 0;
1919 for (; Pos != End; ++Pos) {
1920 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001921 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001922 // This macro has no parameters, look for $0, $1, etc.
1923 if (Body[Pos] != '$' || Pos + 1 == End)
1924 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001925
Rafael Espindola1134ab232011-06-05 02:43:45 +00001926 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001927 if (Next == '$' || Next == 'n' ||
1928 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001929 break;
1930 } else {
1931 // This macro has parameters, look for \foo, \bar, etc.
1932 if (Body[Pos] == '\\' && Pos + 1 != End)
1933 break;
1934 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001935 }
1936
1937 // Add the prefix.
1938 OS << Body.slice(0, Pos);
1939
1940 // Check if we reached the end.
1941 if (Pos == End)
1942 break;
1943
Benjamin Kramer513e7442014-02-20 13:36:32 +00001944 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001945 switch (Body[Pos + 1]) {
1946 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001947 case '$':
1948 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001949 break;
1950
Jim Grosbach4b905842013-09-20 23:08:21 +00001951 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001952 case 'n':
1953 OS << A.size();
1954 break;
1955
Jim Grosbach4b905842013-09-20 23:08:21 +00001956 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001957 default: {
1958 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001959 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001960 if (Index >= A.size())
1961 break;
1962
1963 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00001964 for (const AsmToken &Token : A[Index])
1965 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001966 break;
1967 }
1968 }
1969 Pos += 2;
1970 } else {
1971 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001972
1973 // Check for the \@ pseudo-variable.
1974 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001975 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001976 else
1977 while (isIdentifierChar(Body[I]) && I + 1 != End)
1978 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001979
Jim Grosbach4b905842013-09-20 23:08:21 +00001980 const char *Begin = Body.data() + Pos + 1;
1981 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001982 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001983
Toma Tabacu217116e2015-04-27 10:50:29 +00001984 if (Argument == "@") {
1985 OS << NumOfMacroInstantiations;
1986 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001987 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001988 for (; Index < NParameters; ++Index)
1989 if (Parameters[Index].Name == Argument)
1990 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001991
Toma Tabacu217116e2015-04-27 10:50:29 +00001992 if (Index == NParameters) {
1993 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1994 Pos += 3;
1995 else {
1996 OS << '\\' << Argument;
1997 Pos = I;
1998 }
1999 } else {
2000 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002001 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002002 // We expect no quotes around the string's contents when
2003 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002004 if (Token.getKind() != AsmToken::String || VarargParameter)
2005 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002006 else
Craig Topper84008482015-10-10 05:38:14 +00002007 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002008
2009 Pos += 1 + Argument.size();
2010 }
Preston Gurd05500642012-09-19 20:36:12 +00002011 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002012 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002013 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002014 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002015 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002016
Rafael Espindola1134ab232011-06-05 02:43:45 +00002017 return false;
2018}
Daniel Dunbar43235712010-07-18 18:54:11 +00002019
Nico Weber2a8f9222014-07-24 16:29:04 +00002020MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002021 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002022 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002023 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002024
Jim Grosbach4b905842013-09-20 23:08:21 +00002025static bool isOperator(AsmToken::TokenKind kind) {
2026 switch (kind) {
2027 default:
2028 return false;
2029 case AsmToken::Plus:
2030 case AsmToken::Minus:
2031 case AsmToken::Tilde:
2032 case AsmToken::Slash:
2033 case AsmToken::Star:
2034 case AsmToken::Dot:
2035 case AsmToken::Equal:
2036 case AsmToken::EqualEqual:
2037 case AsmToken::Pipe:
2038 case AsmToken::PipePipe:
2039 case AsmToken::Caret:
2040 case AsmToken::Amp:
2041 case AsmToken::AmpAmp:
2042 case AsmToken::Exclaim:
2043 case AsmToken::ExclaimEqual:
2044 case AsmToken::Percent:
2045 case AsmToken::Less:
2046 case AsmToken::LessEqual:
2047 case AsmToken::LessLess:
2048 case AsmToken::LessGreater:
2049 case AsmToken::Greater:
2050 case AsmToken::GreaterEqual:
2051 case AsmToken::GreaterGreater:
2052 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002053 }
2054}
2055
David Majnemer16252452014-01-29 00:07:39 +00002056namespace {
2057class AsmLexerSkipSpaceRAII {
2058public:
2059 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2060 Lexer.setSkipSpace(SkipSpace);
2061 }
2062
2063 ~AsmLexerSkipSpaceRAII() {
2064 Lexer.setSkipSpace(true);
2065 }
2066
2067private:
2068 AsmLexer &Lexer;
2069};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002070}
David Majnemer16252452014-01-29 00:07:39 +00002071
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002072bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2073
2074 if (Vararg) {
2075 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2076 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002077 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002078 }
2079 return false;
2080 }
2081
Rafael Espindola768b41c2012-06-15 14:02:34 +00002082 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002083 unsigned AddTokens = 0;
2084
David Majnemer16252452014-01-29 00:07:39 +00002085 // Darwin doesn't use spaces to delmit arguments.
2086 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002087
2088 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00002089 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002090 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002091
David Majnemer91fc4c22014-01-29 18:57:46 +00002092 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00002093 break;
Preston Gurd05500642012-09-19 20:36:12 +00002094
2095 if (Lexer.is(AsmToken::Space)) {
2096 Lex(); // Eat spaces
2097
2098 // Spaces can delimit parameters, but could also be part an expression.
2099 // If the token after a space is an operator, add the token and the next
2100 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002101 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002102 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00002103 // Check to see whether the token is used as an operator,
2104 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002105 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00002106 if (*NextChar == ' ')
2107 AddTokens = 2;
2108 }
2109
2110 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002111 break;
2112 }
2113 }
2114 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002115
Jim Grosbach4b905842013-09-20 23:08:21 +00002116 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002117 // to be able to fill in the remaining default parameter values
2118 if (Lexer.is(AsmToken::EndOfStatement))
2119 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002120
2121 // Adjust the current parentheses level.
2122 if (Lexer.is(AsmToken::LParen))
2123 ++ParenLevel;
2124 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2125 --ParenLevel;
2126
2127 // Append the token to the current argument list.
2128 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002129 if (AddTokens)
2130 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002131 Lex();
2132 }
Preston Gurd05500642012-09-19 20:36:12 +00002133
Rafael Espindola768b41c2012-06-15 14:02:34 +00002134 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002135 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002136 return false;
2137}
2138
2139// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002140bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002141 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002142 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002143 bool NamedParametersFound = false;
2144 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002145
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002146 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002147 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002148
Rafael Espindola768b41c2012-06-15 14:02:34 +00002149 // Parse two kinds of macro invocations:
2150 // - macros defined without any parameters accept an arbitrary number of them
2151 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002152 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002153 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2154 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002155 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002156 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002157
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002158 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002159 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002160 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002161 eatToEndOfStatement();
2162 return true;
2163 }
2164
2165 if (!Lexer.is(AsmToken::Equal)) {
2166 TokError("expected '=' after formal parameter identifier");
2167 eatToEndOfStatement();
2168 return true;
2169 }
2170 Lex();
2171
2172 NamedParametersFound = true;
2173 }
2174
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002175 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002176 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002177 eatToEndOfStatement();
2178 return true;
2179 }
2180
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002181 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2182 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002183 return true;
2184
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002185 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002186 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002187 unsigned FAI = 0;
2188 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002189 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002190 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002191
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002192 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002193 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002194 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002195 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002196 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002197 return true;
2198 }
2199 PI = FAI;
2200 }
2201
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002202 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002203 if (A.size() <= PI)
2204 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002205 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002206
2207 if (FALocs.size() <= PI)
2208 FALocs.resize(PI + 1);
2209
2210 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002211 }
Jim Grosbach206661622012-07-30 22:44:17 +00002212
Preston Gurd242ed3152012-09-19 20:29:04 +00002213 // At the end of the statement, fill in remaining arguments that have
2214 // default values. If there aren't any, then the next argument is
2215 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002216 if (Lexer.is(AsmToken::EndOfStatement)) {
2217 bool Failure = false;
2218 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2219 if (A[FAI].empty()) {
2220 if (M->Parameters[FAI].Required) {
2221 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2222 "missing value for required parameter "
2223 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2224 Failure = true;
2225 }
2226
2227 if (!M->Parameters[FAI].Value.empty())
2228 A[FAI] = M->Parameters[FAI].Value;
2229 }
2230 }
2231 return Failure;
2232 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002233
2234 if (Lexer.is(AsmToken::Comma))
2235 Lex();
2236 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002237
2238 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002239}
2240
Jim Grosbach4b905842013-09-20 23:08:21 +00002241const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002242 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2243 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002244}
2245
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002246void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2247 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002248}
2249
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002250void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002251
Jim Grosbach4b905842013-09-20 23:08:21 +00002252bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002253 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2254 // this, although we should protect against infinite loops.
2255 if (ActiveMacros.size() == 20)
2256 return TokError("macros cannot be nested more than 20 levels deep");
2257
Eli Bendersky38274122013-01-14 23:22:36 +00002258 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002260 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002261
Rafael Espindola1134ab232011-06-05 02:43:45 +00002262 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2263 // to hold the macro body with substitutions.
2264 SmallString<256> Buf;
2265 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002266 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002267
Toma Tabacu217116e2015-04-27 10:50:29 +00002268 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002269 return true;
2270
Eli Bendersky38274122013-01-14 23:22:36 +00002271 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002272 // instantiation.
2273 OS << ".endmacro\n";
2274
Rafael Espindola3560ff22014-08-27 20:03:13 +00002275 std::unique_ptr<MemoryBuffer> Instantiation =
2276 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002277
Daniel Dunbar43235712010-07-18 18:54:11 +00002278 // Create the macro instantiation object and add to the current macro
2279 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002280 MacroInstantiation *MI = new MacroInstantiation(
2281 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002282 ActiveMacros.push_back(MI);
2283
Toma Tabacu217116e2015-04-27 10:50:29 +00002284 ++NumOfMacroInstantiations;
2285
Daniel Dunbar43235712010-07-18 18:54:11 +00002286 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002287 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002288 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002289 Lex();
2290
2291 return false;
2292}
2293
Jim Grosbach4b905842013-09-20 23:08:21 +00002294void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002295 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002296 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002297 Lex();
2298
2299 // Pop the instantiation entry.
2300 delete ActiveMacros.back();
2301 ActiveMacros.pop_back();
2302}
2303
Jim Grosbach4b905842013-09-20 23:08:21 +00002304bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002305 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002306 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002307 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002308 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2309 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002310 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002311
Pete Cooper80d21cb2015-06-22 19:35:57 +00002312 if (!Sym) {
2313 // In the case where we parse an expression starting with a '.', we will
2314 // not generate an error, nor will we create a symbol. In this case we
2315 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002316 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002317 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002318
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002319 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002320 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002321 if (NoDeadStrip)
2322 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2323
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002324 return false;
2325}
2326
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002327/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002328/// ::= identifier
2329/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002330bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002331 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002332 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2333 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002334 // handle this as a context dependent token, instead we detect adjacent tokens
2335 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002336 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2337 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002338
Hans Wennborgce69d772013-10-18 20:46:28 +00002339 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002340 Lex();
2341 if (Lexer.isNot(AsmToken::Identifier))
2342 return true;
2343
Hans Wennborgce69d772013-10-18 20:46:28 +00002344 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2345 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002346 return true;
2347
2348 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002349 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002350 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002351 Lex();
2352 return false;
2353 }
2354
Jim Grosbach4b905842013-09-20 23:08:21 +00002355 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002356 return true;
2357
Sean Callanan936b0d32010-01-19 21:44:56 +00002358 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002359
Sean Callanan686ed8d2010-01-19 20:22:31 +00002360 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002361
2362 return false;
2363}
2364
Jim Grosbach4b905842013-09-20 23:08:21 +00002365/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002366/// ::= .equ identifier ',' expression
2367/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002368/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002369bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002370 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002371
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002372 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002373 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002374
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002375 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002376 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002377 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002378
Jim Grosbach4b905842013-09-20 23:08:21 +00002379 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002380}
2381
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002382bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002383 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002384
2385 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002386 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002387 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2388 if (Str[i] != '\\') {
2389 Data += Str[i];
2390 continue;
2391 }
2392
2393 // Recognize escaped characters. Note that this escape semantics currently
2394 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2395 ++i;
2396 if (i == e)
2397 return TokError("unexpected backslash at end of string");
2398
2399 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002400 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002401 // Consume up to three octal characters.
2402 unsigned Value = Str[i] - '0';
2403
Jim Grosbach4b905842013-09-20 23:08:21 +00002404 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002405 ++i;
2406 Value = Value * 8 + (Str[i] - '0');
2407
Jim Grosbach4b905842013-09-20 23:08:21 +00002408 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002409 ++i;
2410 Value = Value * 8 + (Str[i] - '0');
2411 }
2412 }
2413
2414 if (Value > 255)
2415 return TokError("invalid octal escape sequence (out of range)");
2416
Jim Grosbach4b905842013-09-20 23:08:21 +00002417 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002418 continue;
2419 }
2420
2421 // Otherwise recognize individual escapes.
2422 switch (Str[i]) {
2423 default:
2424 // Just reject invalid escape sequences for now.
2425 return TokError("invalid escape sequence (unrecognized character)");
2426
2427 case 'b': Data += '\b'; break;
2428 case 'f': Data += '\f'; break;
2429 case 'n': Data += '\n'; break;
2430 case 'r': Data += '\r'; break;
2431 case 't': Data += '\t'; break;
2432 case '"': Data += '"'; break;
2433 case '\\': Data += '\\'; break;
2434 }
2435 }
2436
2437 return false;
2438}
2439
Jim Grosbach4b905842013-09-20 23:08:21 +00002440/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002441/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002442bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002444 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002445
Daniel Dunbara10e5192009-06-24 23:30:00 +00002446 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002447 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002448 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002449
Daniel Dunbaref668c12009-08-14 18:19:52 +00002450 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002451 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002452 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002453
Rafael Espindola64e1af82013-07-02 15:49:13 +00002454 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002455 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002456 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002457
Sean Callanan686ed8d2010-01-19 20:22:31 +00002458 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002459
2460 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002461 break;
2462
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002463 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002464 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002465 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002466 }
2467 }
2468
Sean Callanan686ed8d2010-01-19 20:22:31 +00002469 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002470 return false;
2471}
2472
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002473/// parseDirectiveReloc
2474/// ::= .reloc expression , identifier [ , expression ]
2475bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2476 const MCExpr *Offset;
2477 const MCExpr *Expr = nullptr;
2478
2479 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2480 if (parseExpression(Offset))
2481 return true;
2482
2483 // We can only deal with constant expressions at the moment.
2484 int64_t OffsetValue;
2485 if (!Offset->evaluateAsAbsolute(OffsetValue))
2486 return Error(OffsetLoc, "expression is not a constant value");
2487
David Majnemerce108422016-01-19 23:05:27 +00002488 if (OffsetValue < 0)
2489 return Error(OffsetLoc, "expression is negative");
2490
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002491 if (Lexer.isNot(AsmToken::Comma))
2492 return TokError("expected comma");
2493 Lexer.Lex();
2494
2495 if (Lexer.isNot(AsmToken::Identifier))
2496 return TokError("expected relocation name");
2497 SMLoc NameLoc = Lexer.getTok().getLoc();
2498 StringRef Name = Lexer.getTok().getIdentifier();
2499 Lexer.Lex();
2500
2501 if (Lexer.is(AsmToken::Comma)) {
2502 Lexer.Lex();
2503 SMLoc ExprLoc = Lexer.getLoc();
2504 if (parseExpression(Expr))
2505 return true;
2506
2507 MCValue Value;
2508 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2509 return Error(ExprLoc, "expression must be relocatable");
2510 }
2511
2512 if (Lexer.isNot(AsmToken::EndOfStatement))
2513 return TokError("unexpected token in .reloc directive");
2514
2515 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2516 return Error(NameLoc, "unknown relocation name");
2517
2518 return false;
2519}
2520
Jim Grosbach4b905842013-09-20 23:08:21 +00002521/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002522/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002523bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002524 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002525 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002526
Daniel Dunbara10e5192009-06-24 23:30:00 +00002527 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002528 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002529 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002530 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002531 return true;
2532
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002533 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002534 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2535 assert(Size <= 8 && "Invalid size");
2536 uint64_t IntValue = MCE->getValue();
2537 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2538 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002539 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002540 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002541 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002542
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002543 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002544 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002545
Daniel Dunbara10e5192009-06-24 23:30:00 +00002546 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002547 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002548 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002549 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002550 }
2551 }
2552
Sean Callanan686ed8d2010-01-19 20:22:31 +00002553 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002554 return false;
2555}
2556
David Woodhoused6de0d92014-02-01 16:20:59 +00002557/// ParseDirectiveOctaValue
2558/// ::= .octa [ hexconstant (, hexconstant)* ]
2559bool AsmParser::parseDirectiveOctaValue() {
2560 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2561 checkForValidSection();
2562
2563 for (;;) {
2564 if (Lexer.getKind() == AsmToken::Error)
2565 return true;
2566 if (Lexer.getKind() != AsmToken::Integer &&
2567 Lexer.getKind() != AsmToken::BigNum)
2568 return TokError("unknown token in expression");
2569
2570 SMLoc ExprLoc = getLexer().getLoc();
2571 APInt IntValue = getTok().getAPIntVal();
2572 Lex();
2573
2574 uint64_t hi, lo;
2575 if (IntValue.isIntN(64)) {
2576 hi = 0;
2577 lo = IntValue.getZExtValue();
2578 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002579 // It might actually have more than 128 bits, but the top ones are zero.
2580 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002581 lo = IntValue.getLoBits(64).getZExtValue();
2582 } else
2583 return Error(ExprLoc, "literal value out of range for directive");
2584
2585 if (MAI.isLittleEndian()) {
2586 getStreamer().EmitIntValue(lo, 8);
2587 getStreamer().EmitIntValue(hi, 8);
2588 } else {
2589 getStreamer().EmitIntValue(hi, 8);
2590 getStreamer().EmitIntValue(lo, 8);
2591 }
2592
2593 if (getLexer().is(AsmToken::EndOfStatement))
2594 break;
2595
2596 // FIXME: Improve diagnostic.
2597 if (getLexer().isNot(AsmToken::Comma))
2598 return TokError("unexpected token in directive");
2599 Lex();
2600 }
2601 }
2602
2603 Lex();
2604 return false;
2605}
2606
Jim Grosbach4b905842013-09-20 23:08:21 +00002607/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002608/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002609bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002610 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002611 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002612
2613 for (;;) {
2614 // We don't truly support arithmetic on floating point expressions, so we
2615 // have to manually parse unary prefixes.
2616 bool IsNeg = false;
2617 if (getLexer().is(AsmToken::Minus)) {
2618 Lex();
2619 IsNeg = true;
2620 } else if (getLexer().is(AsmToken::Plus))
2621 Lex();
2622
Michael J. Spencer530ce852010-10-09 11:00:50 +00002623 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002624 getLexer().isNot(AsmToken::Real) &&
2625 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002626 return TokError("unexpected token in directive");
2627
2628 // Convert to an APFloat.
2629 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002630 StringRef IDVal = getTok().getString();
2631 if (getLexer().is(AsmToken::Identifier)) {
2632 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2633 Value = APFloat::getInf(Semantics);
2634 else if (!IDVal.compare_lower("nan"))
2635 Value = APFloat::getNaN(Semantics, false, ~0);
2636 else
2637 return TokError("invalid floating point literal");
2638 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002639 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002640 return TokError("invalid floating point literal");
2641 if (IsNeg)
2642 Value.changeSign();
2643
2644 // Consume the numeric token.
2645 Lex();
2646
2647 // Emit the value as an integer.
2648 APInt AsInt = Value.bitcastToAPInt();
2649 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002650 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002651
2652 if (getLexer().is(AsmToken::EndOfStatement))
2653 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002654
Daniel Dunbar2af16532010-09-24 01:59:56 +00002655 if (getLexer().isNot(AsmToken::Comma))
2656 return TokError("unexpected token in directive");
2657 Lex();
2658 }
2659 }
2660
2661 Lex();
2662 return false;
2663}
2664
Jim Grosbach4b905842013-09-20 23:08:21 +00002665/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002666/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002667bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002668 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002669
2670 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002671 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002672 return true;
2673
Rafael Espindolab91bac62010-10-05 19:42:57 +00002674 int64_t Val = 0;
2675 if (getLexer().is(AsmToken::Comma)) {
2676 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002677 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002678 return true;
2679 }
2680
Rafael Espindola922e3f42010-09-16 15:03:59 +00002681 if (getLexer().isNot(AsmToken::EndOfStatement))
2682 return TokError("unexpected token in '.zero' directive");
2683
2684 Lex();
2685
Rafael Espindola64e1af82013-07-02 15:49:13 +00002686 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002687
2688 return false;
2689}
2690
Jim Grosbach4b905842013-09-20 23:08:21 +00002691/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002692/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002693bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002694 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002695
David Majnemer522d3db2014-02-01 07:19:38 +00002696 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002697 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002698 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002699 return true;
2700
David Majnemer522d3db2014-02-01 07:19:38 +00002701 if (NumValues < 0) {
2702 Warning(RepeatLoc,
2703 "'.fill' directive with negative repeat count has no effect");
2704 NumValues = 0;
2705 }
2706
Roman Divackye33098f2013-09-24 17:44:41 +00002707 int64_t FillSize = 1;
2708 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002709
David Majnemer522d3db2014-02-01 07:19:38 +00002710 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002711 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2712 if (getLexer().isNot(AsmToken::Comma))
2713 return TokError("unexpected token in '.fill' directive");
2714 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002715
David Majnemer522d3db2014-02-01 07:19:38 +00002716 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002717 if (parseAbsoluteExpression(FillSize))
2718 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002719
Roman Divackye33098f2013-09-24 17:44:41 +00002720 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2721 if (getLexer().isNot(AsmToken::Comma))
2722 return TokError("unexpected token in '.fill' directive");
2723 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002724
David Majnemer522d3db2014-02-01 07:19:38 +00002725 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002726 if (parseAbsoluteExpression(FillExpr))
2727 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002728
Roman Divackye33098f2013-09-24 17:44:41 +00002729 if (getLexer().isNot(AsmToken::EndOfStatement))
2730 return TokError("unexpected token in '.fill' directive");
2731
2732 Lex();
2733 }
2734 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002735
David Majnemer522d3db2014-02-01 07:19:38 +00002736 if (FillSize < 0) {
2737 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2738 NumValues = 0;
2739 }
2740 if (FillSize > 8) {
2741 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2742 FillSize = 8;
2743 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002744
David Majnemer522d3db2014-02-01 07:19:38 +00002745 if (!isUInt<32>(FillExpr) && FillSize > 4)
2746 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2747
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002748 if (NumValues > 0) {
2749 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2750 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2751 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2752 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2753 if (NonZeroFillSize < FillSize)
2754 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2755 }
David Majnemer522d3db2014-02-01 07:19:38 +00002756 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002757
2758 return false;
2759}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002760
Jim Grosbach4b905842013-09-20 23:08:21 +00002761/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002762/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002763bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002764 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002765
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002766 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002767 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002768 return true;
2769
2770 // Parse optional fill expression.
2771 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002772 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2773 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002774 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002775 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002776
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002777 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002778 return true;
2779
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002780 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002781 return TokError("unexpected token in '.org' directive");
2782 }
2783
Sean Callanan686ed8d2010-01-19 20:22:31 +00002784 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002785 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002786 return false;
2787}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002788
Jim Grosbach4b905842013-09-20 23:08:21 +00002789/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002790/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002791bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002792 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002793
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002794 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002795 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002796 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002797 return true;
2798
2799 SMLoc MaxBytesLoc;
2800 bool HasFillExpr = false;
2801 int64_t FillExpr = 0;
2802 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002803 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2804 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002805 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002806 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002807
2808 // The fill expression can be omitted while specifying a maximum number of
2809 // alignment bytes, e.g:
2810 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002811 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002812 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002813 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002814 return true;
2815 }
2816
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002817 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2818 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002819 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002820 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002821
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002822 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002823 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002824 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002825
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002826 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002827 return TokError("unexpected token in directive");
2828 }
2829 }
2830
Sean Callanan686ed8d2010-01-19 20:22:31 +00002831 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002832
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002833 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002834 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002835
2836 // Compute alignment in bytes.
2837 if (IsPow2) {
2838 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002839 if (Alignment >= 32) {
2840 Error(AlignmentLoc, "invalid alignment value");
2841 Alignment = 31;
2842 }
2843
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002844 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002845 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002846 // Reject alignments that aren't either a power of two or zero,
2847 // for gas compatibility. Alignment of zero is silently rounded
2848 // up to one.
2849 if (Alignment == 0)
2850 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002851 if (!isPowerOf2_64(Alignment))
2852 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002853 }
2854
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002855 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002856 if (MaxBytesLoc.isValid()) {
2857 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002858 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002859 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002860 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002861 }
2862
2863 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002864 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002865 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002866 MaxBytesToFill = 0;
2867 }
2868 }
2869
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002870 // Check whether we should use optimal code alignment for this .align
2871 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002872 const MCSection *Section = getStreamer().getCurrentSection().first;
2873 assert(Section && "must have section to emit alignment");
2874 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002875 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2876 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002877 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002878 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002879 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002880 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2881 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002882 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002883
2884 return false;
2885}
2886
Jim Grosbach4b905842013-09-20 23:08:21 +00002887/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002888/// ::= .file [number] filename
2889/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002890bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 // FIXME: I'm not sure what this is.
2892 int64_t FileNumber = -1;
2893 SMLoc FileNumberLoc = getLexer().getLoc();
2894 if (getLexer().is(AsmToken::Integer)) {
2895 FileNumber = getTok().getIntVal();
2896 Lex();
2897
2898 if (FileNumber < 1)
2899 return TokError("file number less than one");
2900 }
2901
2902 if (getLexer().isNot(AsmToken::String))
2903 return TokError("unexpected token in '.file' directive");
2904
2905 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002906 // Allow the strings to have escaped octal character sequence.
2907 std::string Path = getTok().getString();
2908 if (parseEscapedString(Path))
2909 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002910 Lex();
2911
2912 StringRef Directory;
2913 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002914 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002915 if (getLexer().is(AsmToken::String)) {
2916 if (FileNumber == -1)
2917 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002918 if (parseEscapedString(FilenameData))
2919 return true;
2920 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002921 Directory = Path;
2922 Lex();
2923 } else {
2924 Filename = Path;
2925 }
2926
2927 if (getLexer().isNot(AsmToken::EndOfStatement))
2928 return TokError("unexpected token in '.file' directive");
2929
2930 if (FileNumber == -1)
2931 getStreamer().EmitFileDirective(Filename);
2932 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002933 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002934 Error(DirectiveLoc,
2935 "input can't have .file dwarf directives when -g is "
2936 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002937
David Blaikiec714ef42014-03-17 01:52:11 +00002938 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2939 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002940 Error(FileNumberLoc, "file number already allocated");
2941 }
2942
2943 return false;
2944}
2945
Jim Grosbach4b905842013-09-20 23:08:21 +00002946/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002947/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002948bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002949 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2950 if (getLexer().isNot(AsmToken::Integer))
2951 return TokError("unexpected token in '.line' directive");
2952
2953 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002954 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002955 Lex();
2956
2957 // FIXME: Do something with the .line.
2958 }
2959
2960 if (getLexer().isNot(AsmToken::EndOfStatement))
2961 return TokError("unexpected token in '.line' directive");
2962
2963 return false;
2964}
2965
Jim Grosbach4b905842013-09-20 23:08:21 +00002966/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002967/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2968/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2969/// The first number is a file number, must have been previously assigned with
2970/// a .file directive, the second number is the line number and optionally the
2971/// third number is a column position (zero if not specified). The remaining
2972/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002973bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002974 if (getLexer().isNot(AsmToken::Integer))
2975 return TokError("unexpected token in '.loc' directive");
2976 int64_t FileNumber = getTok().getIntVal();
2977 if (FileNumber < 1)
2978 return TokError("file number less than one in '.loc' directive");
2979 if (!getContext().isValidDwarfFileNumber(FileNumber))
2980 return TokError("unassigned file number in '.loc' directive");
2981 Lex();
2982
2983 int64_t LineNumber = 0;
2984 if (getLexer().is(AsmToken::Integer)) {
2985 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002986 if (LineNumber < 0)
2987 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002988 Lex();
2989 }
2990
2991 int64_t ColumnPos = 0;
2992 if (getLexer().is(AsmToken::Integer)) {
2993 ColumnPos = getTok().getIntVal();
2994 if (ColumnPos < 0)
2995 return TokError("column position less than zero in '.loc' directive");
2996 Lex();
2997 }
2998
2999 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3000 unsigned Isa = 0;
3001 int64_t Discriminator = 0;
3002 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3003 for (;;) {
3004 if (getLexer().is(AsmToken::EndOfStatement))
3005 break;
3006
3007 StringRef Name;
3008 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003009 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003010 return TokError("unexpected token in '.loc' directive");
3011
3012 if (Name == "basic_block")
3013 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3014 else if (Name == "prologue_end")
3015 Flags |= DWARF2_FLAG_PROLOGUE_END;
3016 else if (Name == "epilogue_begin")
3017 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3018 else if (Name == "is_stmt") {
3019 Loc = getTok().getLoc();
3020 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003021 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003022 return true;
3023 // The expression must be the constant 0 or 1.
3024 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3025 int Value = MCE->getValue();
3026 if (Value == 0)
3027 Flags &= ~DWARF2_FLAG_IS_STMT;
3028 else if (Value == 1)
3029 Flags |= DWARF2_FLAG_IS_STMT;
3030 else
3031 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003032 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003033 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3034 }
Craig Topperf15655b2013-04-22 04:22:40 +00003035 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003036 Loc = getTok().getLoc();
3037 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003038 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003039 return true;
3040 // The expression must be a constant greater or equal to 0.
3041 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3042 int Value = MCE->getValue();
3043 if (Value < 0)
3044 return Error(Loc, "isa number less than zero");
3045 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003046 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003047 return Error(Loc, "isa number not a constant value");
3048 }
Craig Topperf15655b2013-04-22 04:22:40 +00003049 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003050 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003051 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003052 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003053 return Error(Loc, "unknown sub-directive in '.loc' directive");
3054 }
3055
3056 if (getLexer().is(AsmToken::EndOfStatement))
3057 break;
3058 }
3059 }
3060
3061 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3062 Isa, Discriminator, StringRef());
3063
3064 return false;
3065}
3066
Jim Grosbach4b905842013-09-20 23:08:21 +00003067/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003068/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003069bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003070 return TokError("unsupported directive '.stabs'");
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 StringRef Name;
3077 bool EH = false;
3078 bool Debug = false;
3079
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003080 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003081 return TokError("Expected an identifier");
3082
3083 if (Name == ".eh_frame")
3084 EH = true;
3085 else if (Name == ".debug_frame")
3086 Debug = true;
3087
3088 if (getLexer().is(AsmToken::Comma)) {
3089 Lex();
3090
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003091 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003092 return TokError("Expected an identifier");
3093
3094 if (Name == ".eh_frame")
3095 EH = true;
3096 else if (Name == ".debug_frame")
3097 Debug = true;
3098 }
3099
3100 getStreamer().EmitCFISections(EH, Debug);
3101 return false;
3102}
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003105/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003106bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003107 StringRef Simple;
3108 if (getLexer().isNot(AsmToken::EndOfStatement))
3109 if (parseIdentifier(Simple) || Simple != "simple")
3110 return TokError("unexpected token in .cfi_startproc directive");
3111
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003112 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003113 return false;
3114}
3115
Jim Grosbach4b905842013-09-20 23:08:21 +00003116/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003117/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003118bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003119 getStreamer().EmitCFIEndProc();
3120 return false;
3121}
3122
Jim Grosbach4b905842013-09-20 23:08:21 +00003123/// \brief parse register name or number.
3124bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003125 SMLoc DirectiveLoc) {
3126 unsigned RegNo;
3127
3128 if (getLexer().isNot(AsmToken::Integer)) {
3129 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3130 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003131 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003132 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003133 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003134
3135 return false;
3136}
3137
Jim Grosbach4b905842013-09-20 23:08:21 +00003138/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003139/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003140bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003141 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003142 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003143 return true;
3144
3145 if (getLexer().isNot(AsmToken::Comma))
3146 return TokError("unexpected token in directive");
3147 Lex();
3148
3149 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003150 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003151 return true;
3152
3153 getStreamer().EmitCFIDefCfa(Register, Offset);
3154 return false;
3155}
3156
Jim Grosbach4b905842013-09-20 23:08:21 +00003157/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003158/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003159bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003160 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003161 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003162 return true;
3163
3164 getStreamer().EmitCFIDefCfaOffset(Offset);
3165 return false;
3166}
3167
Jim Grosbach4b905842013-09-20 23:08:21 +00003168/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003169/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003170bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003171 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003172 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003173 return true;
3174
3175 if (getLexer().isNot(AsmToken::Comma))
3176 return TokError("unexpected token in directive");
3177 Lex();
3178
3179 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003180 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003181 return true;
3182
3183 getStreamer().EmitCFIRegister(Register1, Register2);
3184 return false;
3185}
3186
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003187/// parseDirectiveCFIWindowSave
3188/// ::= .cfi_window_save
3189bool AsmParser::parseDirectiveCFIWindowSave() {
3190 getStreamer().EmitCFIWindowSave();
3191 return false;
3192}
3193
Jim Grosbach4b905842013-09-20 23:08:21 +00003194/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003195/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003196bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003197 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003198 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003199 return true;
3200
3201 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003207bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003208 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003209 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003210 return true;
3211
3212 getStreamer().EmitCFIDefCfaRegister(Register);
3213 return false;
3214}
3215
Jim Grosbach4b905842013-09-20 23:08:21 +00003216/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003217/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003218bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003219 int64_t Register = 0;
3220 int64_t Offset = 0;
3221
Jim Grosbach4b905842013-09-20 23:08:21 +00003222 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003223 return true;
3224
3225 if (getLexer().isNot(AsmToken::Comma))
3226 return TokError("unexpected token in directive");
3227 Lex();
3228
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003229 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003230 return true;
3231
3232 getStreamer().EmitCFIOffset(Register, Offset);
3233 return false;
3234}
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003237/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003238bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 int64_t Register = 0;
3240
Jim Grosbach4b905842013-09-20 23:08:21 +00003241 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003242 return true;
3243
3244 if (getLexer().isNot(AsmToken::Comma))
3245 return TokError("unexpected token in directive");
3246 Lex();
3247
3248 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003249 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003250 return true;
3251
3252 getStreamer().EmitCFIRelOffset(Register, Offset);
3253 return false;
3254}
3255
3256static bool isValidEncoding(int64_t Encoding) {
3257 if (Encoding & ~0xff)
3258 return false;
3259
3260 if (Encoding == dwarf::DW_EH_PE_omit)
3261 return true;
3262
3263 const unsigned Format = Encoding & 0xf;
3264 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3265 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3266 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3267 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3268 return false;
3269
3270 const unsigned Application = Encoding & 0x70;
3271 if (Application != dwarf::DW_EH_PE_absptr &&
3272 Application != dwarf::DW_EH_PE_pcrel)
3273 return false;
3274
3275 return true;
3276}
3277
Jim Grosbach4b905842013-09-20 23:08:21 +00003278/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003279/// IsPersonality true for cfi_personality, false for cfi_lsda
3280/// ::= .cfi_personality encoding, [symbol_name]
3281/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003282bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003283 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003284 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003285 return true;
3286 if (Encoding == dwarf::DW_EH_PE_omit)
3287 return false;
3288
3289 if (!isValidEncoding(Encoding))
3290 return TokError("unsupported encoding.");
3291
3292 if (getLexer().isNot(AsmToken::Comma))
3293 return TokError("unexpected token in directive");
3294 Lex();
3295
3296 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003297 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003298 return TokError("expected identifier in directive");
3299
Jim Grosbach6f482002015-05-18 18:43:14 +00003300 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003301
3302 if (IsPersonality)
3303 getStreamer().EmitCFIPersonality(Sym, Encoding);
3304 else
3305 getStreamer().EmitCFILsda(Sym, Encoding);
3306 return false;
3307}
3308
Jim Grosbach4b905842013-09-20 23:08:21 +00003309/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003310/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003311bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003312 getStreamer().EmitCFIRememberState();
3313 return false;
3314}
3315
Jim Grosbach4b905842013-09-20 23:08:21 +00003316/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003317/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003318bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003319 getStreamer().EmitCFIRestoreState();
3320 return false;
3321}
3322
Jim Grosbach4b905842013-09-20 23:08:21 +00003323/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003324/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003325bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003326 int64_t Register = 0;
3327
Jim Grosbach4b905842013-09-20 23:08:21 +00003328 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003329 return true;
3330
3331 getStreamer().EmitCFISameValue(Register);
3332 return false;
3333}
3334
Jim Grosbach4b905842013-09-20 23:08:21 +00003335/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003336/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003337bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003338 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003339 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003340 return true;
3341
3342 getStreamer().EmitCFIRestore(Register);
3343 return false;
3344}
3345
Jim Grosbach4b905842013-09-20 23:08:21 +00003346/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003347/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003348bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003349 std::string Values;
3350 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003351 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003352 return true;
3353
3354 Values.push_back((uint8_t)CurrValue);
3355
3356 while (getLexer().is(AsmToken::Comma)) {
3357 Lex();
3358
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003359 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003360 return true;
3361
3362 Values.push_back((uint8_t)CurrValue);
3363 }
3364
3365 getStreamer().EmitCFIEscape(Values);
3366 return false;
3367}
3368
Jim Grosbach4b905842013-09-20 23:08:21 +00003369/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003370/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003371bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003372 if (getLexer().isNot(AsmToken::EndOfStatement))
3373 return Error(getLexer().getLoc(),
3374 "unexpected token in '.cfi_signal_frame'");
3375
3376 getStreamer().EmitCFISignalFrame();
3377 return false;
3378}
3379
Jim Grosbach4b905842013-09-20 23:08:21 +00003380/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003381/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003382bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003383 int64_t Register = 0;
3384
Jim Grosbach4b905842013-09-20 23:08:21 +00003385 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003386 return true;
3387
3388 getStreamer().EmitCFIUndefined(Register);
3389 return false;
3390}
3391
Jim Grosbach4b905842013-09-20 23:08:21 +00003392/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003393/// ::= .macros_on
3394/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003395bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003396 if (getLexer().isNot(AsmToken::EndOfStatement))
3397 return Error(getLexer().getLoc(),
3398 "unexpected token in '" + Directive + "' directive");
3399
Jim Grosbach4b905842013-09-20 23:08:21 +00003400 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003401 return false;
3402}
3403
Jim Grosbach4b905842013-09-20 23:08:21 +00003404/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003405/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003406bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003407 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003408 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003409 return TokError("expected identifier in '.macro' directive");
3410
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003411 if (getLexer().is(AsmToken::Comma))
3412 Lex();
3413
Eli Bendersky17233942013-01-15 22:59:42 +00003414 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003415 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003416
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003417 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003418 return Error(Lexer.getLoc(),
3419 "Vararg parameter '" + Parameters.back().Name +
3420 "' should be last one in the list of parameters.");
3421
David Majnemer91fc4c22014-01-29 18:57:46 +00003422 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003423 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003424 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003425
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003426 if (Lexer.is(AsmToken::Colon)) {
3427 Lex(); // consume ':'
3428
3429 SMLoc QualLoc;
3430 StringRef Qualifier;
3431
3432 QualLoc = Lexer.getLoc();
3433 if (parseIdentifier(Qualifier))
3434 return Error(QualLoc, "missing parameter qualifier for "
3435 "'" + Parameter.Name + "' in macro '" + Name + "'");
3436
3437 if (Qualifier == "req")
3438 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003439 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003440 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003441 else
3442 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3443 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3444 }
3445
David Majnemer91fc4c22014-01-29 18:57:46 +00003446 if (getLexer().is(AsmToken::Equal)) {
3447 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003448
3449 SMLoc ParamLoc;
3450
3451 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003452 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003453 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003454
3455 if (Parameter.Required)
3456 Warning(ParamLoc, "pointless default value for required parameter "
3457 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003458 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003459
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003460 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003461
3462 if (getLexer().is(AsmToken::Comma))
3463 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003464 }
3465
3466 // Eat the end of statement.
3467 Lex();
3468
3469 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003470 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003471
3472 // Lex the macro definition.
3473 for (;;) {
3474 // Check whether we have reached the end of the file.
3475 if (getLexer().is(AsmToken::Eof))
3476 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3477
3478 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003479 if (getLexer().is(AsmToken::Identifier)) {
3480 if (getTok().getIdentifier() == ".endm" ||
3481 getTok().getIdentifier() == ".endmacro") {
3482 if (MacroDepth == 0) { // Outermost macro.
3483 EndToken = getTok();
3484 Lex();
3485 if (getLexer().isNot(AsmToken::EndOfStatement))
3486 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3487 "' directive");
3488 break;
3489 } else {
3490 // Otherwise we just found the end of an inner macro.
3491 --MacroDepth;
3492 }
3493 } else if (getTok().getIdentifier() == ".macro") {
3494 // We allow nested macros. Those aren't instantiated until the outermost
3495 // macro is expanded so just ignore them for now.
3496 ++MacroDepth;
3497 }
Eli Bendersky17233942013-01-15 22:59:42 +00003498 }
3499
3500 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003501 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003502 }
3503
Jim Grosbach4b905842013-09-20 23:08:21 +00003504 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003505 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3506 }
3507
3508 const char *BodyStart = StartToken.getLoc().getPointer();
3509 const char *BodyEnd = EndToken.getLoc().getPointer();
3510 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003511 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003512 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003513 return false;
3514}
3515
Jim Grosbach4b905842013-09-20 23:08:21 +00003516/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003517///
3518/// With the support added for named parameters there may be code out there that
3519/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003520/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003521/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003522/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003523/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3524/// warning that the positional parameter found in body which have no effect.
3525/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003526/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003527/// intended or change the macro to use the named parameters. It is possible
3528/// this warning will trigger when the none of the named parameters are used
3529/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003530void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003531 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003532 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003533 // If this macro is not defined with named parameters the warning we are
3534 // checking for here doesn't apply.
3535 unsigned NParameters = Parameters.size();
3536 if (NParameters == 0)
3537 return;
3538
3539 bool NamedParametersFound = false;
3540 bool PositionalParametersFound = false;
3541
3542 // Look at the body of the macro for use of both the named parameters and what
3543 // are likely to be positional parameters. This is what expandMacro() is
3544 // doing when it finds the parameters in the body.
3545 while (!Body.empty()) {
3546 // Scan for the next possible parameter.
3547 std::size_t End = Body.size(), Pos = 0;
3548 for (; Pos != End; ++Pos) {
3549 // Check for a substitution or escape.
3550 // This macro is defined with parameters, look for \foo, \bar, etc.
3551 if (Body[Pos] == '\\' && Pos + 1 != End)
3552 break;
3553
3554 // This macro should have parameters, but look for $0, $1, ..., $n too.
3555 if (Body[Pos] != '$' || Pos + 1 == End)
3556 continue;
3557 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003558 if (Next == '$' || Next == 'n' ||
3559 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003560 break;
3561 }
3562
3563 // Check if we reached the end.
3564 if (Pos == End)
3565 break;
3566
3567 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003568 switch (Body[Pos + 1]) {
3569 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003570 case '$':
3571 break;
3572
Jim Grosbach4b905842013-09-20 23:08:21 +00003573 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003574 case 'n':
3575 PositionalParametersFound = true;
3576 break;
3577
Jim Grosbach4b905842013-09-20 23:08:21 +00003578 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003579 default: {
3580 PositionalParametersFound = true;
3581 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003582 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003583 }
3584 Pos += 2;
3585 } else {
3586 unsigned I = Pos + 1;
3587 while (isIdentifierChar(Body[I]) && I + 1 != End)
3588 ++I;
3589
Jim Grosbach4b905842013-09-20 23:08:21 +00003590 const char *Begin = Body.data() + Pos + 1;
3591 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003592 unsigned Index = 0;
3593 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003594 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003595 break;
3596
3597 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003598 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3599 Pos += 3;
3600 else {
3601 Pos = I;
3602 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003603 } else {
3604 NamedParametersFound = true;
3605 Pos += 1 + Argument.size();
3606 }
3607 }
3608 // Update the scan point.
3609 Body = Body.substr(Pos);
3610 }
3611
3612 if (!NamedParametersFound && PositionalParametersFound)
3613 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3614 "used in macro body, possible positional parameter "
3615 "found in body which will have no effect");
3616}
3617
Nico Weber155dccd12014-07-24 17:08:39 +00003618/// parseDirectiveExitMacro
3619/// ::= .exitm
3620bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3621 if (getLexer().isNot(AsmToken::EndOfStatement))
3622 return TokError("unexpected token in '" + Directive + "' directive");
3623
3624 if (!isInsideMacroInstantiation())
3625 return TokError("unexpected '" + Directive + "' in file, "
3626 "no current macro definition");
3627
3628 // Exit all conditionals that are active in the current macro.
3629 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3630 TheCondState = TheCondStack.back();
3631 TheCondStack.pop_back();
3632 }
3633
3634 handleMacroExit();
3635 return false;
3636}
3637
Jim Grosbach4b905842013-09-20 23:08:21 +00003638/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003639/// ::= .endm
3640/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003641bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003642 if (getLexer().isNot(AsmToken::EndOfStatement))
3643 return TokError("unexpected token in '" + Directive + "' directive");
3644
3645 // If we are inside a macro instantiation, terminate the current
3646 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003647 if (isInsideMacroInstantiation()) {
3648 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003649 return false;
3650 }
3651
3652 // Otherwise, this .endmacro is a stray entry in the file; well formed
3653 // .endmacro directives are handled during the macro definition parsing.
3654 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003655 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003656}
3657
Jim Grosbach4b905842013-09-20 23:08:21 +00003658/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003659/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003660bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003661 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003662 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003663 return TokError("expected identifier in '.purgem' directive");
3664
3665 if (getLexer().isNot(AsmToken::EndOfStatement))
3666 return TokError("unexpected token in '.purgem' directive");
3667
Jim Grosbach4b905842013-09-20 23:08:21 +00003668 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003669 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3670
Jim Grosbach4b905842013-09-20 23:08:21 +00003671 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003672 return false;
3673}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003674
Jim Grosbach4b905842013-09-20 23:08:21 +00003675/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003676/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003677bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003678 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003679
3680 // Expect a single argument: an expression that evaluates to a constant
3681 // in the inclusive range 0-30.
3682 SMLoc ExprLoc = getLexer().getLoc();
3683 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003684 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003685 return true;
3686 else if (getLexer().isNot(AsmToken::EndOfStatement))
3687 return TokError("unexpected token after expression in"
3688 " '.bundle_align_mode' directive");
3689 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3690 return Error(ExprLoc,
3691 "invalid bundle alignment size (expected between 0 and 30)");
3692
3693 Lex();
3694
3695 // Because of AlignSizePow2's verified range we can safely truncate it to
3696 // unsigned.
3697 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3698 return false;
3699}
3700
Jim Grosbach4b905842013-09-20 23:08:21 +00003701/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003702/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003703bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003704 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003705 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003706
Eli Bendersky802b6282013-01-07 21:51:08 +00003707 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3708 StringRef Option;
3709 SMLoc Loc = getTok().getLoc();
3710 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003711 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003712
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003713 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003714 return Error(Loc, kInvalidOptionError);
3715
3716 if (Option != "align_to_end")
3717 return Error(Loc, kInvalidOptionError);
3718 else if (getLexer().isNot(AsmToken::EndOfStatement))
3719 return Error(Loc,
3720 "unexpected token after '.bundle_lock' directive option");
3721 AlignToEnd = true;
3722 }
3723
Eli Benderskyf483ff92012-12-20 19:05:53 +00003724 Lex();
3725
Eli Bendersky802b6282013-01-07 21:51:08 +00003726 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003727 return false;
3728}
3729
Jim Grosbach4b905842013-09-20 23:08:21 +00003730/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003731/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003732bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003733 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003734
3735 if (getLexer().isNot(AsmToken::EndOfStatement))
3736 return TokError("unexpected token in '.bundle_unlock' directive");
3737 Lex();
3738
3739 getStreamer().EmitBundleUnlock();
3740 return false;
3741}
3742
Jim Grosbach4b905842013-09-20 23:08:21 +00003743/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003744/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003745bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003746 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003747
3748 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003749 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003750 return true;
3751
3752 int64_t FillExpr = 0;
3753 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3754 if (getLexer().isNot(AsmToken::Comma))
3755 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3756 Lex();
3757
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003758 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003759 return true;
3760
3761 if (getLexer().isNot(AsmToken::EndOfStatement))
3762 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3763 }
3764
3765 Lex();
3766
3767 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003768 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3769 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003770
3771 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003772 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003773
3774 return false;
3775}
3776
Jim Grosbach4b905842013-09-20 23:08:21 +00003777/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003778/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003779bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003780 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003781 const MCExpr *Value;
3782
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003783 for (;;) {
3784 if (parseExpression(Value))
3785 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003786
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003787 if (Signed)
3788 getStreamer().EmitSLEB128Value(Value);
3789 else
3790 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003791
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003792 if (getLexer().is(AsmToken::EndOfStatement))
3793 break;
3794
3795 if (getLexer().isNot(AsmToken::Comma))
3796 return TokError("unexpected token in directive");
3797 Lex();
3798 }
Eli Bendersky17233942013-01-15 22:59:42 +00003799
3800 return false;
3801}
3802
Jim Grosbach4b905842013-09-20 23:08:21 +00003803/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003804/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003805bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003806 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003807 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003808 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003809 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003810
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003811 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003812 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003813
Jim Grosbach6f482002015-05-18 18:43:14 +00003814 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003815
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003816 // Assembler local symbols don't make any sense here. Complain loudly.
3817 if (Sym->isTemporary())
3818 return Error(Loc, "non-local symbol required in directive");
3819
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003820 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3821 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003822
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003823 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003824 break;
3825
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003826 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003827 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003828 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003829 }
3830 }
3831
Sean Callanan686ed8d2010-01-19 20:22:31 +00003832 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003833 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003834}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003835
Jim Grosbach4b905842013-09-20 23:08:21 +00003836/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003837/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003838bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003839 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003840
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003841 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003842 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003843 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003844 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003845
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003846 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003847 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003848
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003849 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003850 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003851 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003852
3853 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003854 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003855 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003856 return true;
3857
3858 int64_t Pow2Alignment = 0;
3859 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003860 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003861 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003862 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003863 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003864 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003865
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003866 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3867 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003868 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3869
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003870 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003871 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3872 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003873 if (!isPowerOf2_64(Pow2Alignment))
3874 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3875 Pow2Alignment = Log2_64(Pow2Alignment);
3876 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003877 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003878
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003879 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003880 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003881
Sean Callanan686ed8d2010-01-19 20:22:31 +00003882 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003883
Chris Lattner28ad7542009-07-09 17:25:12 +00003884 // NOTE: a size of zero for a .comm should create a undefined symbol
3885 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003886 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003887 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003888 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003889
Eric Christopherbc818852010-05-14 01:38:54 +00003890 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003891 // may internally end up wanting an alignment in bytes.
3892 // FIXME: Diagnose overflow.
3893 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003894 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003895 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003896
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003897 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003898 return Error(IDLoc, "invalid symbol redefinition");
3899
Chris Lattner28ad7542009-07-09 17:25:12 +00003900 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003901 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003902 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003903 return false;
3904 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003905
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003906 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003907 return false;
3908}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003909
Jim Grosbach4b905842013-09-20 23:08:21 +00003910/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003911/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003912bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003913 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003914 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003915
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003916 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003917 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003918 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003919
Sean Callanan686ed8d2010-01-19 20:22:31 +00003920 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003921
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003922 if (Str.empty())
3923 Error(Loc, ".abort detected. Assembly stopping.");
3924 else
3925 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003926 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003927
3928 return false;
3929}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003930
Jim Grosbach4b905842013-09-20 23:08:21 +00003931/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003932/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003933bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003934 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003935 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003936
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003937 // Allow the strings to have escaped octal character sequence.
3938 std::string Filename;
3939 if (parseEscapedString(Filename))
3940 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003941 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003942 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003943
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003944 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003945 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003946
Chris Lattner693fbb82009-07-16 06:14:39 +00003947 // Attempt to switch the lexer to the included file before consuming the end
3948 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003949 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003950 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003951 return true;
3952 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003953
3954 return false;
3955}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003956
Jim Grosbach4b905842013-09-20 23:08:21 +00003957/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003958/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003959bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003960 if (getLexer().isNot(AsmToken::String))
3961 return TokError("expected string in '.incbin' directive");
3962
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003963 // Allow the strings to have escaped octal character sequence.
3964 std::string Filename;
3965 if (parseEscapedString(Filename))
3966 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003967 SMLoc IncbinLoc = getLexer().getLoc();
3968 Lex();
3969
3970 if (getLexer().isNot(AsmToken::EndOfStatement))
3971 return TokError("unexpected token in '.incbin' directive");
3972
Kevin Enderby109f25c2011-12-14 21:47:48 +00003973 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003974 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003975 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3976 return true;
3977 }
3978
3979 return false;
3980}
3981
Jim Grosbach4b905842013-09-20 23:08:21 +00003982/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003983/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3984bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 TheCondStack.push_back(TheCondState);
3986 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003987 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003988 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003989 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003990 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003991 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003992 return true;
3993
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003994 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003995 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003996
Sean Callanan686ed8d2010-01-19 20:22:31 +00003997 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003998
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003999 switch (DirKind) {
4000 default:
4001 llvm_unreachable("unsupported directive");
4002 case DK_IF:
4003 case DK_IFNE:
4004 break;
4005 case DK_IFEQ:
4006 ExprValue = ExprValue == 0;
4007 break;
4008 case DK_IFGE:
4009 ExprValue = ExprValue >= 0;
4010 break;
4011 case DK_IFGT:
4012 ExprValue = ExprValue > 0;
4013 break;
4014 case DK_IFLE:
4015 ExprValue = ExprValue <= 0;
4016 break;
4017 case DK_IFLT:
4018 ExprValue = ExprValue < 0;
4019 break;
4020 }
4021
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022 TheCondState.CondMet = ExprValue;
4023 TheCondState.Ignore = !TheCondState.CondMet;
4024 }
4025
4026 return false;
4027}
4028
Jim Grosbach4b905842013-09-20 23:08:21 +00004029/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004030/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004031bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004032 TheCondStack.push_back(TheCondState);
4033 TheCondState.TheCond = AsmCond::IfCond;
4034
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004035 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004036 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004037 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004038 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004039
4040 if (getLexer().isNot(AsmToken::EndOfStatement))
4041 return TokError("unexpected token in '.ifb' directive");
4042
4043 Lex();
4044
4045 TheCondState.CondMet = ExpectBlank == Str.empty();
4046 TheCondState.Ignore = !TheCondState.CondMet;
4047 }
4048
4049 return false;
4050}
4051
Jim Grosbach4b905842013-09-20 23:08:21 +00004052/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004053/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004054/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004055bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004056 TheCondStack.push_back(TheCondState);
4057 TheCondState.TheCond = AsmCond::IfCond;
4058
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004059 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004060 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004061 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004062 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004063
4064 if (getLexer().isNot(AsmToken::Comma))
4065 return TokError("unexpected token in '.ifc' directive");
4066
4067 Lex();
4068
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004069 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004070
4071 if (getLexer().isNot(AsmToken::EndOfStatement))
4072 return TokError("unexpected token in '.ifc' directive");
4073
4074 Lex();
4075
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004076 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004077 TheCondState.Ignore = !TheCondState.CondMet;
4078 }
4079
4080 return false;
4081}
4082
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004083/// parseDirectiveIfeqs
4084/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004085bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004086 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004087 if (ExpectEqual)
4088 TokError("expected string parameter for '.ifeqs' directive");
4089 else
4090 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004091 eatToEndOfStatement();
4092 return true;
4093 }
4094
4095 StringRef String1 = getTok().getStringContents();
4096 Lex();
4097
4098 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004099 if (ExpectEqual)
4100 TokError("expected comma after first string for '.ifeqs' directive");
4101 else
4102 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004103 eatToEndOfStatement();
4104 return true;
4105 }
4106
4107 Lex();
4108
4109 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004110 if (ExpectEqual)
4111 TokError("expected string parameter for '.ifeqs' directive");
4112 else
4113 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004114 eatToEndOfStatement();
4115 return true;
4116 }
4117
4118 StringRef String2 = getTok().getStringContents();
4119 Lex();
4120
4121 TheCondStack.push_back(TheCondState);
4122 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004123 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004124 TheCondState.Ignore = !TheCondState.CondMet;
4125
4126 return false;
4127}
4128
Jim Grosbach4b905842013-09-20 23:08:21 +00004129/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004130/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004131bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004132 StringRef Name;
4133 TheCondStack.push_back(TheCondState);
4134 TheCondState.TheCond = AsmCond::IfCond;
4135
4136 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004137 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004138 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004139 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004140 return TokError("expected identifier after '.ifdef'");
4141
4142 Lex();
4143
Jim Grosbach6f482002015-05-18 18:43:14 +00004144 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004145
4146 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004147 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004148 else
Craig Topper353eda42014-04-24 06:44:33 +00004149 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004150 TheCondState.Ignore = !TheCondState.CondMet;
4151 }
4152
4153 return false;
4154}
4155
Jim Grosbach4b905842013-09-20 23:08:21 +00004156/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004157/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004158bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004159 if (TheCondState.TheCond != AsmCond::IfCond &&
4160 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004161 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4162 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004163 TheCondState.TheCond = AsmCond::ElseIfCond;
4164
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004165 bool LastIgnoreState = false;
4166 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004167 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004168 if (LastIgnoreState || TheCondState.CondMet) {
4169 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004170 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004171 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004172 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004173 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004174 return true;
4175
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004176 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004177 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004178
Sean Callanan686ed8d2010-01-19 20:22:31 +00004179 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004180 TheCondState.CondMet = ExprValue;
4181 TheCondState.Ignore = !TheCondState.CondMet;
4182 }
4183
4184 return false;
4185}
4186
Jim Grosbach4b905842013-09-20 23:08:21 +00004187/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004188/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004189bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004190 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004191 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004192
Sean Callanan686ed8d2010-01-19 20:22:31 +00004193 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004194
4195 if (TheCondState.TheCond != AsmCond::IfCond &&
4196 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004197 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4198 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004199 TheCondState.TheCond = AsmCond::ElseCond;
4200 bool LastIgnoreState = false;
4201 if (!TheCondStack.empty())
4202 LastIgnoreState = TheCondStack.back().Ignore;
4203 if (LastIgnoreState || TheCondState.CondMet)
4204 TheCondState.Ignore = true;
4205 else
4206 TheCondState.Ignore = false;
4207
4208 return false;
4209}
4210
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004211/// parseDirectiveEnd
4212/// ::= .end
4213bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4214 if (getLexer().isNot(AsmToken::EndOfStatement))
4215 return TokError("unexpected token in '.end' directive");
4216
4217 Lex();
4218
4219 while (Lexer.isNot(AsmToken::Eof))
4220 Lex();
4221
4222 return false;
4223}
4224
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004225/// parseDirectiveError
4226/// ::= .err
4227/// ::= .error [string]
4228bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4229 if (!TheCondStack.empty()) {
4230 if (TheCondStack.back().Ignore) {
4231 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004232 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004233 }
4234 }
4235
4236 if (!WithMessage)
4237 return Error(L, ".err encountered");
4238
4239 StringRef Message = ".error directive invoked in source file";
4240 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4241 if (Lexer.isNot(AsmToken::String)) {
4242 TokError(".error argument must be a string");
4243 eatToEndOfStatement();
4244 return true;
4245 }
4246
4247 Message = getTok().getStringContents();
4248 Lex();
4249 }
4250
4251 Error(L, Message);
4252 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004253}
4254
Nico Weber404012b2014-07-24 16:26:06 +00004255/// parseDirectiveWarning
4256/// ::= .warning [string]
4257bool AsmParser::parseDirectiveWarning(SMLoc L) {
4258 if (!TheCondStack.empty()) {
4259 if (TheCondStack.back().Ignore) {
4260 eatToEndOfStatement();
4261 return false;
4262 }
4263 }
4264
4265 StringRef Message = ".warning directive invoked in source file";
4266 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4267 if (Lexer.isNot(AsmToken::String)) {
4268 TokError(".warning argument must be a string");
4269 eatToEndOfStatement();
4270 return true;
4271 }
4272
4273 Message = getTok().getStringContents();
4274 Lex();
4275 }
4276
4277 Warning(L, Message);
4278 return false;
4279}
4280
Jim Grosbach4b905842013-09-20 23:08:21 +00004281/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004282/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004283bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004284 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004285 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004286
Sean Callanan686ed8d2010-01-19 20:22:31 +00004287 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004288
Jim Grosbach4b905842013-09-20 23:08:21 +00004289 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004290 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4291 ".else");
4292 if (!TheCondStack.empty()) {
4293 TheCondState = TheCondStack.back();
4294 TheCondStack.pop_back();
4295 }
4296
4297 return false;
4298}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004299
Eli Bendersky17233942013-01-15 22:59:42 +00004300void AsmParser::initializeDirectiveKindMap() {
4301 DirectiveKindMap[".set"] = DK_SET;
4302 DirectiveKindMap[".equ"] = DK_EQU;
4303 DirectiveKindMap[".equiv"] = DK_EQUIV;
4304 DirectiveKindMap[".ascii"] = DK_ASCII;
4305 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4306 DirectiveKindMap[".string"] = DK_STRING;
4307 DirectiveKindMap[".byte"] = DK_BYTE;
4308 DirectiveKindMap[".short"] = DK_SHORT;
4309 DirectiveKindMap[".value"] = DK_VALUE;
4310 DirectiveKindMap[".2byte"] = DK_2BYTE;
4311 DirectiveKindMap[".long"] = DK_LONG;
4312 DirectiveKindMap[".int"] = DK_INT;
4313 DirectiveKindMap[".4byte"] = DK_4BYTE;
4314 DirectiveKindMap[".quad"] = DK_QUAD;
4315 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004316 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004317 DirectiveKindMap[".single"] = DK_SINGLE;
4318 DirectiveKindMap[".float"] = DK_FLOAT;
4319 DirectiveKindMap[".double"] = DK_DOUBLE;
4320 DirectiveKindMap[".align"] = DK_ALIGN;
4321 DirectiveKindMap[".align32"] = DK_ALIGN32;
4322 DirectiveKindMap[".balign"] = DK_BALIGN;
4323 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4324 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4325 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4326 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4327 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4328 DirectiveKindMap[".org"] = DK_ORG;
4329 DirectiveKindMap[".fill"] = DK_FILL;
4330 DirectiveKindMap[".zero"] = DK_ZERO;
4331 DirectiveKindMap[".extern"] = DK_EXTERN;
4332 DirectiveKindMap[".globl"] = DK_GLOBL;
4333 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004334 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4335 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4336 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4337 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4338 DirectiveKindMap[".reference"] = DK_REFERENCE;
4339 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4340 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4341 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4342 DirectiveKindMap[".comm"] = DK_COMM;
4343 DirectiveKindMap[".common"] = DK_COMMON;
4344 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4345 DirectiveKindMap[".abort"] = DK_ABORT;
4346 DirectiveKindMap[".include"] = DK_INCLUDE;
4347 DirectiveKindMap[".incbin"] = DK_INCBIN;
4348 DirectiveKindMap[".code16"] = DK_CODE16;
4349 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4350 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004351 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004352 DirectiveKindMap[".irp"] = DK_IRP;
4353 DirectiveKindMap[".irpc"] = DK_IRPC;
4354 DirectiveKindMap[".endr"] = DK_ENDR;
4355 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4356 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4357 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4358 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004359 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4360 DirectiveKindMap[".ifge"] = DK_IFGE;
4361 DirectiveKindMap[".ifgt"] = DK_IFGT;
4362 DirectiveKindMap[".ifle"] = DK_IFLE;
4363 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004364 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004365 DirectiveKindMap[".ifb"] = DK_IFB;
4366 DirectiveKindMap[".ifnb"] = DK_IFNB;
4367 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004368 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004369 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004370 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004371 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4372 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4373 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4374 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4375 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004376 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004377 DirectiveKindMap[".endif"] = DK_ENDIF;
4378 DirectiveKindMap[".skip"] = DK_SKIP;
4379 DirectiveKindMap[".space"] = DK_SPACE;
4380 DirectiveKindMap[".file"] = DK_FILE;
4381 DirectiveKindMap[".line"] = DK_LINE;
4382 DirectiveKindMap[".loc"] = DK_LOC;
4383 DirectiveKindMap[".stabs"] = DK_STABS;
4384 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4385 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4386 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4387 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4388 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4389 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4390 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4391 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4392 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4393 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4394 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4395 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4396 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4397 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4398 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4399 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4400 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4401 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4402 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4403 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4404 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004405 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004406 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4407 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4408 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004409 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004410 DirectiveKindMap[".endm"] = DK_ENDM;
4411 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4412 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004413 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004414 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004415 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004416 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004417}
4418
Jim Grosbach4b905842013-09-20 23:08:21 +00004419MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004420 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004421
Rafael Espindola34b9c512012-06-03 23:57:14 +00004422 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004423 for (;;) {
4424 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004425 if (getLexer().is(AsmToken::Eof)) {
4426 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004427 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004428 }
4429
Rafael Espindola34b9c512012-06-03 23:57:14 +00004430 if (Lexer.is(AsmToken::Identifier) &&
4431 (getTok().getIdentifier() == ".rept")) {
4432 ++NestLevel;
4433 }
4434
4435 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004436 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004437 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004438 EndToken = getTok();
4439 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004440 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4441 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004442 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004443 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004444 break;
4445 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004446 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004447 }
4448
Rafael Espindola34b9c512012-06-03 23:57:14 +00004449 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004450 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004451 }
4452
4453 const char *BodyStart = StartToken.getLoc().getPointer();
4454 const char *BodyEnd = EndToken.getLoc().getPointer();
4455 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4456
Rafael Espindola34b9c512012-06-03 23:57:14 +00004457 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004458 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004459 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004460}
4461
Jim Grosbach4b905842013-09-20 23:08:21 +00004462void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004463 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004464 OS << ".endr\n";
4465
Rafael Espindola3560ff22014-08-27 20:03:13 +00004466 std::unique_ptr<MemoryBuffer> Instantiation =
4467 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004468
Rafael Espindola34b9c512012-06-03 23:57:14 +00004469 // Create the macro instantiation object and add to the current macro
4470 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004471 MacroInstantiation *MI = new MacroInstantiation(
4472 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004473 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004474
Rafael Espindola34b9c512012-06-03 23:57:14 +00004475 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004476 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004477 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004478 Lex();
4479}
4480
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004481/// parseDirectiveRept
4482/// ::= .rep | .rept count
4483bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004484 const MCExpr *CountExpr;
4485 SMLoc CountLoc = getTok().getLoc();
4486 if (parseExpression(CountExpr))
4487 return true;
4488
Rafael Espindola34b9c512012-06-03 23:57:14 +00004489 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004490 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004491 eatToEndOfStatement();
4492 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4493 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004494
4495 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004496 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004497
4498 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004499 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004500
4501 // Eat the end of statement.
4502 Lex();
4503
4504 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004505 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004506 if (!M)
4507 return true;
4508
4509 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4510 // to hold the macro body with substitutions.
4511 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004512 raw_svector_ostream OS(Buf);
4513 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004514 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4515 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004516 return true;
4517 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004518 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004519
4520 return false;
4521}
4522
Jim Grosbach4b905842013-09-20 23:08:21 +00004523/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004524/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004525bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004526 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004527
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004528 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004529 return TokError("expected identifier in '.irp' directive");
4530
Rafael Espindola768b41c2012-06-15 14:02:34 +00004531 if (Lexer.isNot(AsmToken::Comma))
4532 return TokError("expected comma in '.irp' directive");
4533
4534 Lex();
4535
Eli Bendersky38274122013-01-14 23:22:36 +00004536 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004537 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004538 return true;
4539
4540 // Eat the end of statement.
4541 Lex();
4542
4543 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004544 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004545 if (!M)
4546 return true;
4547
4548 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4549 // to hold the macro body with substitutions.
4550 SmallString<256> Buf;
4551 raw_svector_ostream OS(Buf);
4552
Craig Topper84008482015-10-10 05:38:14 +00004553 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004554 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4555 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004556 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004557 return true;
4558 }
4559
Jim Grosbach4b905842013-09-20 23:08:21 +00004560 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004561
4562 return false;
4563}
4564
Jim Grosbach4b905842013-09-20 23:08:21 +00004565/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004566/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004567bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004568 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004569
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004570 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004571 return TokError("expected identifier in '.irpc' directive");
4572
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004573 if (Lexer.isNot(AsmToken::Comma))
4574 return TokError("expected comma in '.irpc' directive");
4575
4576 Lex();
4577
Eli Bendersky38274122013-01-14 23:22:36 +00004578 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004579 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004580 return true;
4581
4582 if (A.size() != 1 || A.front().size() != 1)
4583 return TokError("unexpected token in '.irpc' directive");
4584
4585 // Eat the end of statement.
4586 Lex();
4587
4588 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004589 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004590 if (!M)
4591 return true;
4592
4593 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4594 // to hold the macro body with substitutions.
4595 SmallString<256> Buf;
4596 raw_svector_ostream OS(Buf);
4597
4598 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004599 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004600 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004601 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004602
Toma Tabacu217116e2015-04-27 10:50:29 +00004603 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4604 // This is undocumented, but GAS seems to support it.
4605 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004606 return true;
4607 }
4608
Jim Grosbach4b905842013-09-20 23:08:21 +00004609 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004610
4611 return false;
4612}
4613
Jim Grosbach4b905842013-09-20 23:08:21 +00004614bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004615 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004616 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004617
4618 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004619 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004620 assert(getLexer().is(AsmToken::EndOfStatement));
4621
Jim Grosbach4b905842013-09-20 23:08:21 +00004622 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004623 return false;
4624}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004625
Jim Grosbach4b905842013-09-20 23:08:21 +00004626bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004627 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004628 const MCExpr *Value;
4629 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004630 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004631 return true;
4632 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4633 if (!MCE)
4634 return Error(ExprLoc, "unexpected expression in _emit");
4635 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004636 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004637 return Error(ExprLoc, "literal value out of range for directive");
4638
Craig Topper7d5b2312015-10-10 05:25:02 +00004639 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004640 return false;
4641}
4642
Jim Grosbach4b905842013-09-20 23:08:21 +00004643bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004644 const MCExpr *Value;
4645 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004646 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004647 return true;
4648 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4649 if (!MCE)
4650 return Error(ExprLoc, "unexpected expression in align");
4651 uint64_t IntValue = MCE->getValue();
4652 if (!isPowerOf2_64(IntValue))
4653 return Error(ExprLoc, "literal value not a power of two greater then zero");
4654
Craig Topper7d5b2312015-10-10 05:25:02 +00004655 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004656 return false;
4657}
4658
Chad Rosierf43fcf52013-02-13 21:27:17 +00004659// We are comparing pointers, but the pointers are relative to a single string.
4660// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004661static int rewritesSort(const AsmRewrite *AsmRewriteA,
4662 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004663 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4664 return -1;
4665 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4666 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004667
Chad Rosierfce4fab2013-04-08 17:43:47 +00004668 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4669 // rewrite to the same location. Make sure the SizeDirective rewrite is
4670 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4671 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004672 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4673 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004674 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004675
Jim Grosbach4b905842013-09-20 23:08:21 +00004676 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4677 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004678 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004679 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004680}
4681
Jim Grosbach4b905842013-09-20 23:08:21 +00004682bool AsmParser::parseMSInlineAsm(
4683 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4684 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4685 SmallVectorImpl<std::string> &Constraints,
4686 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4687 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004688 SmallVector<void *, 4> InputDecls;
4689 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004690 SmallVector<bool, 4> InputDeclsAddressOf;
4691 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004692 SmallVector<std::string, 4> InputConstraints;
4693 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004694 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004695
Benjamin Kramer1a136112013-02-15 20:37:21 +00004696 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004697
4698 // Prime the lexer.
4699 Lex();
4700
4701 // While we have input, parse each statement.
4702 unsigned InputIdx = 0;
4703 unsigned OutputIdx = 0;
4704 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004705 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004706 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004707 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004708
Chad Rosier149e8e02012-12-12 22:45:52 +00004709 if (Info.ParseError)
4710 return true;
4711
Benjamin Kramer1a136112013-02-15 20:37:21 +00004712 if (Info.Opcode == ~0U)
4713 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004714
Benjamin Kramer1a136112013-02-15 20:37:21 +00004715 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004716
Benjamin Kramer1a136112013-02-15 20:37:21 +00004717 // Build the list of clobbers, outputs and inputs.
4718 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004719 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004720
Benjamin Kramer1a136112013-02-15 20:37:21 +00004721 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004722 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004723 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004724
Benjamin Kramer1a136112013-02-15 20:37:21 +00004725 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004726 if (Operand.isReg() && !Operand.needAddressOf() &&
4727 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004728 unsigned NumDefs = Desc.getNumDefs();
4729 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004730 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4731 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004732 continue;
4733 }
4734
4735 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004736 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004737 if (SymName.empty())
4738 continue;
4739
David Blaikie960ea3f2014-06-08 16:18:35 +00004740 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004741 if (!OpDecl)
4742 continue;
4743
4744 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004745 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004746 if (isOutput) {
4747 ++InputIdx;
4748 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004749 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004750 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004751 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004752 } else {
4753 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004754 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4755 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004756 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00004757 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004758 }
Reid Kleckneree088972013-12-10 18:27:32 +00004759
4760 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00004761 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
4762 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00004763 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004764 }
4765
4766 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004767 NumOutputs = OutputDecls.size();
4768 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004769
4770 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004771 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4772 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4773 ClobberRegs.end());
4774 Clobbers.assign(ClobberRegs.size(), std::string());
4775 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4776 raw_string_ostream OS(Clobbers[I]);
4777 IP->printRegName(OS, ClobberRegs[I]);
4778 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004779
4780 // Merge the various outputs and inputs. Output are expected first.
4781 if (NumOutputs || NumInputs) {
4782 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004783 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004784 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004785 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004786 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004787 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004788 }
4789 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004790 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004791 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004792 }
4793 }
4794
4795 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004796 std::string AsmStringIR;
4797 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004798 StringRef ASMString =
4799 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4800 const char *AsmStart = ASMString.begin();
4801 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004802 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004803 for (const AsmRewrite &AR : AsmStrRewrites) {
4804 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004805 if (Kind == AOK_Delete)
4806 continue;
4807
David Majnemer8114c1a2014-06-23 02:17:16 +00004808 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004809 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004810
Chad Rosier120eefd2013-03-19 17:32:17 +00004811 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004812 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004813 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004814
Chad Rosier37e755c2012-10-23 17:43:43 +00004815 // Skip the original expression.
4816 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004817 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004818 continue;
4819 }
4820
Chad Rosierff10ed12013-04-12 16:26:42 +00004821 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004822 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004823 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004824 default:
4825 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004826 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004827 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004828 break;
4829 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004830 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004831 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004832 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004833 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004834 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004835 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004836 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004837 break;
4838 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004839 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004840 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004841 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004842 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004843 default: break;
4844 case 8: OS << "byte ptr "; break;
4845 case 16: OS << "word ptr "; break;
4846 case 32: OS << "dword ptr "; break;
4847 case 64: OS << "qword ptr "; break;
4848 case 80: OS << "xword ptr "; break;
4849 case 128: OS << "xmmword ptr "; break;
4850 case 256: OS << "ymmword ptr "; break;
4851 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004852 break;
4853 case AOK_Emit:
4854 OS << ".byte";
4855 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004856 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004857 // MS alignment directives are measured in bytes. If the native assembler
4858 // measures alignment in bytes, we can pass it straight through.
4859 OS << ".align";
4860 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
4861 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004862
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004863 // Alignment is in log2 form, so print that instead and skip the original
4864 // immediate.
4865 unsigned Val = AR.Val;
4866 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004867 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004868 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4869 break;
4870 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00004871 case AOK_EVEN:
4872 OS << ".even";
4873 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00004874 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004875 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004876 OS.flush();
4877 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004878 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004879 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004880 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004881 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004882
Chad Rosier8bce6642012-10-18 15:49:34 +00004883 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004884 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004885 }
4886
4887 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004888 if (AsmStart != AsmEnd)
4889 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004890
4891 AsmString = OS.str();
4892 return false;
4893}
4894
Pete Cooper80d21cb2015-06-22 19:35:57 +00004895namespace llvm {
4896namespace MCParserUtils {
4897
4898/// Returns whether the given symbol is used anywhere in the given expression,
4899/// or subexpressions.
4900static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
4901 switch (Value->getKind()) {
4902 case MCExpr::Binary: {
4903 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
4904 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
4905 isSymbolUsedInExpression(Sym, BE->getRHS());
4906 }
4907 case MCExpr::Target:
4908 case MCExpr::Constant:
4909 return false;
4910 case MCExpr::SymbolRef: {
4911 const MCSymbol &S =
4912 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
4913 if (S.isVariable())
4914 return isSymbolUsedInExpression(Sym, S.getVariableValue());
4915 return &S == Sym;
4916 }
4917 case MCExpr::Unary:
4918 return isSymbolUsedInExpression(
4919 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
4920 }
4921
4922 llvm_unreachable("Unknown expr kind!");
4923}
4924
4925bool parseAssignmentExpression(StringRef Name, bool allow_redef,
4926 MCAsmParser &Parser, MCSymbol *&Sym,
4927 const MCExpr *&Value) {
4928 MCAsmLexer &Lexer = Parser.getLexer();
4929
4930 // FIXME: Use better location, we should use proper tokens.
4931 SMLoc EqualLoc = Lexer.getLoc();
4932
4933 if (Parser.parseExpression(Value)) {
4934 Parser.TokError("missing expression");
4935 Parser.eatToEndOfStatement();
4936 return true;
4937 }
4938
4939 // Note: we don't count b as used in "a = b". This is to allow
4940 // a = b
4941 // b = c
4942
4943 if (Lexer.isNot(AsmToken::EndOfStatement))
4944 return Parser.TokError("unexpected token in assignment");
4945
4946 // Eat the end of statement marker.
4947 Parser.Lex();
4948
4949 // Validate that the LHS is allowed to be a variable (either it has not been
4950 // used as a symbol, or it is an absolute symbol).
4951 Sym = Parser.getContext().lookupSymbol(Name);
4952 if (Sym) {
4953 // Diagnose assignment to a label.
4954 //
4955 // FIXME: Diagnostics. Note the location of the definition as a label.
4956 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
4957 if (isSymbolUsedInExpression(Sym, Value))
4958 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00004959 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
4960 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00004961 ; // Allow redefinitions of undefined symbols only used in directives.
4962 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
4963 ; // Allow redefinitions of variables that haven't yet been used.
4964 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
4965 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
4966 else if (!Sym->isVariable())
4967 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
4968 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
4969 return Parser.Error(EqualLoc,
4970 "invalid reassignment of non-absolute variable '" +
4971 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00004972 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00004973 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00004974 return false;
4975 } else
4976 Sym = Parser.getContext().getOrCreateSymbol(Name);
4977
4978 Sym->setRedefinable(allow_redef);
4979
4980 return false;
4981}
4982
4983} // namespace MCParserUtils
4984} // namespace llvm
4985
Daniel Dunbar01e36072010-07-17 02:26:10 +00004986/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004987MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4988 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004989 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004990}