blob: 61f7d749b9688463222c3133fe72400eb0608ccd [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
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001717 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001718
Chris Lattner7cbfa442010-05-19 23:34:33 +00001719 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001720 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001721 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001722 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001723 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001724 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001725
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001726 // Dump the parsed representation, if requested.
1727 if (getShowParsedOperands()) {
1728 SmallString<256> Str;
1729 raw_svector_ostream OS(Str);
1730 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001731 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001732 if (i != 0)
1733 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001734 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001735 }
1736 OS << "]";
1737
Jim Grosbach4b905842013-09-20 23:08:21 +00001738 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001739 }
1740
Oliver Stannard8b273082014-06-19 15:52:37 +00001741 // If we are generating dwarf for the current section then generate a .loc
1742 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001743 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001744 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001745 getStreamer().getCurrentSection().first)) {
1746 unsigned Line;
1747 if (ActiveMacros.empty())
1748 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1749 else
Frederic Riss16238d92015-06-25 21:57:33 +00001750 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1751 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001752
Eli Bendersky88024712013-01-16 19:32:36 +00001753 // If we previously parsed a cpp hash file line comment then make sure the
1754 // current Dwarf File is for the CppHashFilename if not then emit the
1755 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001756 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001757 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1758 0, StringRef(), CppHashFilename);
1759 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001760
Jim Grosbach4b905842013-09-20 23:08:21 +00001761 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1762 // cache with the different Loc from the call above we save the last
1763 // info we queried here with SrcMgr.FindLineNumber().
1764 unsigned CppHashLocLineNo;
1765 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1766 CppHashLocLineNo = LastQueryLine;
1767 else {
1768 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1769 LastQueryLine = CppHashLocLineNo;
1770 LastQueryIDLoc = CppHashLoc;
1771 LastQueryBuffer = CppHashBuf;
1772 }
1773 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001774 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001775
Jim Grosbach4b905842013-09-20 23:08:21 +00001776 getStreamer().EmitDwarfLocDirective(
1777 getContext().getGenDwarfFileNumber(), Line, 0,
1778 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1779 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001780 }
1781
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001782 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001783 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001784 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001785 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1786 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001787 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001788 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001789
Chris Lattnera2a9d162010-09-11 16:18:25 +00001790 // Don't skip the rest of the line, the instruction parser is responsible for
1791 // that.
1792 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001793}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001794
Jim Grosbach4b905842013-09-20 23:08:21 +00001795/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001796/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001797void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001798 if (!Lexer.is(AsmToken::EndOfStatement))
1799 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 // Eat EOL.
1801 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001802}
1803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001805/// ::= # number "filename"
1806/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001807bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001808 Lex(); // Eat the hash token.
1809
1810 if (getLexer().isNot(AsmToken::Integer)) {
1811 // Consume the line since in cases it is not a well-formed line directive,
1812 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001813 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001814 return false;
1815 }
1816
1817 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001818 Lex();
1819
1820 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001821 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001822 return false;
1823 }
1824
1825 StringRef Filename = getTok().getString();
1826 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001827 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001828
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001829 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1830 CppHashLoc = L;
1831 CppHashFilename = Filename;
1832 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001833 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001834
1835 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001836 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001837 return false;
1838}
1839
Jim Grosbach4b905842013-09-20 23:08:21 +00001840/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001841/// for the Filename and LineNo if any in the diagnostic.
1842void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001843 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001844 raw_ostream &OS = errs();
1845
1846 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001847 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001848 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1849 unsigned CppHashBuf =
1850 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001851
Jim Grosbach4b905842013-09-20 23:08:21 +00001852 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001853 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001854 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1855 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1856 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001857 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1858 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001859 }
1860
Eric Christophera7c32732012-12-18 00:30:54 +00001861 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001862 // manager changed or buffer changed (like in a nested include) then just
1863 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001864 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001865 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001866 if (Parser->SavedDiagHandler)
1867 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1868 else
Craig Topper353eda42014-04-24 06:44:33 +00001869 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001870 return;
1871 }
1872
Eric Christophera7c32732012-12-18 00:30:54 +00001873 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001874 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1875 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001876 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001877
1878 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1879 int CppHashLocLineNo =
1880 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001881 int LineNo =
1882 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001883
Jim Grosbach4b905842013-09-20 23:08:21 +00001884 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1885 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001886 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001887
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001888 if (Parser->SavedDiagHandler)
1889 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1890 else
Craig Topper353eda42014-04-24 06:44:33 +00001891 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001892}
1893
Rafael Espindola2c064482012-08-21 18:29:30 +00001894// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1895// difference being that that function accepts '@' as part of identifiers and
1896// we can't do that. AsmLexer.cpp should probably be changed to handle
1897// '@' as a special case when needed.
1898static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001899 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1900 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001901}
1902
Rafael Espindola34b9c512012-06-03 23:57:14 +00001903bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001904 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001905 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001906 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001907 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001908 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001909 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001910 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001911
Preston Gurd05500642012-09-19 20:36:12 +00001912 // A macro without parameters is handled differently on Darwin:
1913 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001914 while (!Body.empty()) {
1915 // Scan for the next substitution.
1916 std::size_t End = Body.size(), Pos = 0;
1917 for (; Pos != End; ++Pos) {
1918 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001919 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001920 // This macro has no parameters, look for $0, $1, etc.
1921 if (Body[Pos] != '$' || Pos + 1 == End)
1922 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001923
Rafael Espindola1134ab232011-06-05 02:43:45 +00001924 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001925 if (Next == '$' || Next == 'n' ||
1926 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001927 break;
1928 } else {
1929 // This macro has parameters, look for \foo, \bar, etc.
1930 if (Body[Pos] == '\\' && Pos + 1 != End)
1931 break;
1932 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001933 }
1934
1935 // Add the prefix.
1936 OS << Body.slice(0, Pos);
1937
1938 // Check if we reached the end.
1939 if (Pos == End)
1940 break;
1941
Benjamin Kramer513e7442014-02-20 13:36:32 +00001942 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001943 switch (Body[Pos + 1]) {
1944 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001945 case '$':
1946 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001947 break;
1948
Jim Grosbach4b905842013-09-20 23:08:21 +00001949 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001950 case 'n':
1951 OS << A.size();
1952 break;
1953
Jim Grosbach4b905842013-09-20 23:08:21 +00001954 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001955 default: {
1956 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001957 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001958 if (Index >= A.size())
1959 break;
1960
1961 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00001962 for (const AsmToken &Token : A[Index])
1963 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001964 break;
1965 }
1966 }
1967 Pos += 2;
1968 } else {
1969 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001970
1971 // Check for the \@ pseudo-variable.
1972 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001973 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001974 else
1975 while (isIdentifierChar(Body[I]) && I + 1 != End)
1976 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001977
Jim Grosbach4b905842013-09-20 23:08:21 +00001978 const char *Begin = Body.data() + Pos + 1;
1979 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001980 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001981
Toma Tabacu217116e2015-04-27 10:50:29 +00001982 if (Argument == "@") {
1983 OS << NumOfMacroInstantiations;
1984 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001985 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001986 for (; Index < NParameters; ++Index)
1987 if (Parameters[Index].Name == Argument)
1988 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001989
Toma Tabacu217116e2015-04-27 10:50:29 +00001990 if (Index == NParameters) {
1991 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1992 Pos += 3;
1993 else {
1994 OS << '\\' << Argument;
1995 Pos = I;
1996 }
1997 } else {
1998 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00001999 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002000 // We expect no quotes around the string's contents when
2001 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002002 if (Token.getKind() != AsmToken::String || VarargParameter)
2003 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002004 else
Craig Topper84008482015-10-10 05:38:14 +00002005 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002006
2007 Pos += 1 + Argument.size();
2008 }
Preston Gurd05500642012-09-19 20:36:12 +00002009 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002010 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002011 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002012 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002013 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002014
Rafael Espindola1134ab232011-06-05 02:43:45 +00002015 return false;
2016}
Daniel Dunbar43235712010-07-18 18:54:11 +00002017
Nico Weber2a8f9222014-07-24 16:29:04 +00002018MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002019 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002020 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002021 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002022
Jim Grosbach4b905842013-09-20 23:08:21 +00002023static bool isOperator(AsmToken::TokenKind kind) {
2024 switch (kind) {
2025 default:
2026 return false;
2027 case AsmToken::Plus:
2028 case AsmToken::Minus:
2029 case AsmToken::Tilde:
2030 case AsmToken::Slash:
2031 case AsmToken::Star:
2032 case AsmToken::Dot:
2033 case AsmToken::Equal:
2034 case AsmToken::EqualEqual:
2035 case AsmToken::Pipe:
2036 case AsmToken::PipePipe:
2037 case AsmToken::Caret:
2038 case AsmToken::Amp:
2039 case AsmToken::AmpAmp:
2040 case AsmToken::Exclaim:
2041 case AsmToken::ExclaimEqual:
2042 case AsmToken::Percent:
2043 case AsmToken::Less:
2044 case AsmToken::LessEqual:
2045 case AsmToken::LessLess:
2046 case AsmToken::LessGreater:
2047 case AsmToken::Greater:
2048 case AsmToken::GreaterEqual:
2049 case AsmToken::GreaterGreater:
2050 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002051 }
2052}
2053
David Majnemer16252452014-01-29 00:07:39 +00002054namespace {
2055class AsmLexerSkipSpaceRAII {
2056public:
2057 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2058 Lexer.setSkipSpace(SkipSpace);
2059 }
2060
2061 ~AsmLexerSkipSpaceRAII() {
2062 Lexer.setSkipSpace(true);
2063 }
2064
2065private:
2066 AsmLexer &Lexer;
2067};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002068}
David Majnemer16252452014-01-29 00:07:39 +00002069
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002070bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2071
2072 if (Vararg) {
2073 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2074 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002075 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002076 }
2077 return false;
2078 }
2079
Rafael Espindola768b41c2012-06-15 14:02:34 +00002080 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002081 unsigned AddTokens = 0;
2082
David Majnemer16252452014-01-29 00:07:39 +00002083 // Darwin doesn't use spaces to delmit arguments.
2084 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002085
2086 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00002087 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002088 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002089
David Majnemer91fc4c22014-01-29 18:57:46 +00002090 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00002091 break;
Preston Gurd05500642012-09-19 20:36:12 +00002092
2093 if (Lexer.is(AsmToken::Space)) {
2094 Lex(); // Eat spaces
2095
2096 // Spaces can delimit parameters, but could also be part an expression.
2097 // If the token after a space is an operator, add the token and the next
2098 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002099 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002100 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00002101 // Check to see whether the token is used as an operator,
2102 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002103 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00002104 if (*NextChar == ' ')
2105 AddTokens = 2;
2106 }
2107
2108 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002109 break;
2110 }
2111 }
2112 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002113
Jim Grosbach4b905842013-09-20 23:08:21 +00002114 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002115 // to be able to fill in the remaining default parameter values
2116 if (Lexer.is(AsmToken::EndOfStatement))
2117 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002118
2119 // Adjust the current parentheses level.
2120 if (Lexer.is(AsmToken::LParen))
2121 ++ParenLevel;
2122 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2123 --ParenLevel;
2124
2125 // Append the token to the current argument list.
2126 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002127 if (AddTokens)
2128 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002129 Lex();
2130 }
Preston Gurd05500642012-09-19 20:36:12 +00002131
Rafael Espindola768b41c2012-06-15 14:02:34 +00002132 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002133 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002134 return false;
2135}
2136
2137// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002138bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002139 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002140 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002141 bool NamedParametersFound = false;
2142 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002143
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002144 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002145 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002146
Rafael Espindola768b41c2012-06-15 14:02:34 +00002147 // Parse two kinds of macro invocations:
2148 // - macros defined without any parameters accept an arbitrary number of them
2149 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002150 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002151 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2152 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002153 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002154 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002155
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002156 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002157 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002158 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002159 eatToEndOfStatement();
2160 return true;
2161 }
2162
2163 if (!Lexer.is(AsmToken::Equal)) {
2164 TokError("expected '=' after formal parameter identifier");
2165 eatToEndOfStatement();
2166 return true;
2167 }
2168 Lex();
2169
2170 NamedParametersFound = true;
2171 }
2172
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002173 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002174 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002175 eatToEndOfStatement();
2176 return true;
2177 }
2178
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002179 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2180 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002181 return true;
2182
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002183 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002184 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002185 unsigned FAI = 0;
2186 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002187 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002188 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002189
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002190 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002191 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002192 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002193 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002194 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002195 return true;
2196 }
2197 PI = FAI;
2198 }
2199
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002200 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002201 if (A.size() <= PI)
2202 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002203 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002204
2205 if (FALocs.size() <= PI)
2206 FALocs.resize(PI + 1);
2207
2208 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002209 }
Jim Grosbach206661622012-07-30 22:44:17 +00002210
Preston Gurd242ed3152012-09-19 20:29:04 +00002211 // At the end of the statement, fill in remaining arguments that have
2212 // default values. If there aren't any, then the next argument is
2213 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002214 if (Lexer.is(AsmToken::EndOfStatement)) {
2215 bool Failure = false;
2216 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2217 if (A[FAI].empty()) {
2218 if (M->Parameters[FAI].Required) {
2219 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2220 "missing value for required parameter "
2221 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2222 Failure = true;
2223 }
2224
2225 if (!M->Parameters[FAI].Value.empty())
2226 A[FAI] = M->Parameters[FAI].Value;
2227 }
2228 }
2229 return Failure;
2230 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002231
2232 if (Lexer.is(AsmToken::Comma))
2233 Lex();
2234 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002235
2236 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002237}
2238
Jim Grosbach4b905842013-09-20 23:08:21 +00002239const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002240 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2241 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002242}
2243
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002244void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2245 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002246}
2247
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002248void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002249
Jim Grosbach4b905842013-09-20 23:08:21 +00002250bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002251 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2252 // this, although we should protect against infinite loops.
2253 if (ActiveMacros.size() == 20)
2254 return TokError("macros cannot be nested more than 20 levels deep");
2255
Eli Bendersky38274122013-01-14 23:22:36 +00002256 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002257 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002258 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002259
Rafael Espindola1134ab232011-06-05 02:43:45 +00002260 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2261 // to hold the macro body with substitutions.
2262 SmallString<256> Buf;
2263 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002264 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002265
Toma Tabacu217116e2015-04-27 10:50:29 +00002266 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002267 return true;
2268
Eli Bendersky38274122013-01-14 23:22:36 +00002269 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002270 // instantiation.
2271 OS << ".endmacro\n";
2272
Rafael Espindola3560ff22014-08-27 20:03:13 +00002273 std::unique_ptr<MemoryBuffer> Instantiation =
2274 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002275
Daniel Dunbar43235712010-07-18 18:54:11 +00002276 // Create the macro instantiation object and add to the current macro
2277 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002278 MacroInstantiation *MI = new MacroInstantiation(
2279 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002280 ActiveMacros.push_back(MI);
2281
Toma Tabacu217116e2015-04-27 10:50:29 +00002282 ++NumOfMacroInstantiations;
2283
Daniel Dunbar43235712010-07-18 18:54:11 +00002284 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002285 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002286 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002287 Lex();
2288
2289 return false;
2290}
2291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002293 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002294 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002295 Lex();
2296
2297 // Pop the instantiation entry.
2298 delete ActiveMacros.back();
2299 ActiveMacros.pop_back();
2300}
2301
Jim Grosbach4b905842013-09-20 23:08:21 +00002302bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002303 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002304 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002305 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002306 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2307 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002308 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002309
Pete Cooper80d21cb2015-06-22 19:35:57 +00002310 if (!Sym) {
2311 // In the case where we parse an expression starting with a '.', we will
2312 // not generate an error, nor will we create a symbol. In this case we
2313 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002314 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002315 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002316
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002317 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002318 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002319 if (NoDeadStrip)
2320 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2321
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002322 return false;
2323}
2324
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002325/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002326/// ::= identifier
2327/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002328bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002329 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002330 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2331 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002332 // handle this as a context dependent token, instead we detect adjacent tokens
2333 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002334 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2335 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002336
Hans Wennborgce69d772013-10-18 20:46:28 +00002337 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002338 Lex();
2339 if (Lexer.isNot(AsmToken::Identifier))
2340 return true;
2341
Hans Wennborgce69d772013-10-18 20:46:28 +00002342 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2343 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002344 return true;
2345
2346 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002347 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002348 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002349 Lex();
2350 return false;
2351 }
2352
Jim Grosbach4b905842013-09-20 23:08:21 +00002353 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002354 return true;
2355
Sean Callanan936b0d32010-01-19 21:44:56 +00002356 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002357
Sean Callanan686ed8d2010-01-19 20:22:31 +00002358 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002359
2360 return false;
2361}
2362
Jim Grosbach4b905842013-09-20 23:08:21 +00002363/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002364/// ::= .equ identifier ',' expression
2365/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002366/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002367bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002368 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002369
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002370 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002371 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002372
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002373 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002374 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002378}
2379
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002380bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002381 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002382
2383 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002384 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002385 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2386 if (Str[i] != '\\') {
2387 Data += Str[i];
2388 continue;
2389 }
2390
2391 // Recognize escaped characters. Note that this escape semantics currently
2392 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2393 ++i;
2394 if (i == e)
2395 return TokError("unexpected backslash at end of string");
2396
2397 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002398 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002399 // Consume up to three octal characters.
2400 unsigned Value = Str[i] - '0';
2401
Jim Grosbach4b905842013-09-20 23:08:21 +00002402 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002403 ++i;
2404 Value = Value * 8 + (Str[i] - '0');
2405
Jim Grosbach4b905842013-09-20 23:08:21 +00002406 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002407 ++i;
2408 Value = Value * 8 + (Str[i] - '0');
2409 }
2410 }
2411
2412 if (Value > 255)
2413 return TokError("invalid octal escape sequence (out of range)");
2414
Jim Grosbach4b905842013-09-20 23:08:21 +00002415 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002416 continue;
2417 }
2418
2419 // Otherwise recognize individual escapes.
2420 switch (Str[i]) {
2421 default:
2422 // Just reject invalid escape sequences for now.
2423 return TokError("invalid escape sequence (unrecognized character)");
2424
2425 case 'b': Data += '\b'; break;
2426 case 'f': Data += '\f'; break;
2427 case 'n': Data += '\n'; break;
2428 case 'r': Data += '\r'; break;
2429 case 't': Data += '\t'; break;
2430 case '"': Data += '"'; break;
2431 case '\\': Data += '\\'; break;
2432 }
2433 }
2434
2435 return false;
2436}
2437
Jim Grosbach4b905842013-09-20 23:08:21 +00002438/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002439/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002440bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002441 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002442 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002443
Daniel Dunbara10e5192009-06-24 23:30:00 +00002444 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002445 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002446 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002447
Daniel Dunbaref668c12009-08-14 18:19:52 +00002448 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002449 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002450 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002451
Rafael Espindola64e1af82013-07-02 15:49:13 +00002452 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002453 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002454 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002455
Sean Callanan686ed8d2010-01-19 20:22:31 +00002456 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002457
2458 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002459 break;
2460
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002461 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002462 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002463 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002464 }
2465 }
2466
Sean Callanan686ed8d2010-01-19 20:22:31 +00002467 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002468 return false;
2469}
2470
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002471/// parseDirectiveReloc
2472/// ::= .reloc expression , identifier [ , expression ]
2473bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2474 const MCExpr *Offset;
2475 const MCExpr *Expr = nullptr;
2476
2477 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2478 if (parseExpression(Offset))
2479 return true;
2480
2481 // We can only deal with constant expressions at the moment.
2482 int64_t OffsetValue;
2483 if (!Offset->evaluateAsAbsolute(OffsetValue))
2484 return Error(OffsetLoc, "expression is not a constant value");
2485
2486 if (Lexer.isNot(AsmToken::Comma))
2487 return TokError("expected comma");
2488 Lexer.Lex();
2489
2490 if (Lexer.isNot(AsmToken::Identifier))
2491 return TokError("expected relocation name");
2492 SMLoc NameLoc = Lexer.getTok().getLoc();
2493 StringRef Name = Lexer.getTok().getIdentifier();
2494 Lexer.Lex();
2495
2496 if (Lexer.is(AsmToken::Comma)) {
2497 Lexer.Lex();
2498 SMLoc ExprLoc = Lexer.getLoc();
2499 if (parseExpression(Expr))
2500 return true;
2501
2502 MCValue Value;
2503 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2504 return Error(ExprLoc, "expression must be relocatable");
2505 }
2506
2507 if (Lexer.isNot(AsmToken::EndOfStatement))
2508 return TokError("unexpected token in .reloc directive");
2509
2510 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2511 return Error(NameLoc, "unknown relocation name");
2512
2513 return false;
2514}
2515
Jim Grosbach4b905842013-09-20 23:08:21 +00002516/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002517/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002518bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002519 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002520 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002521
Daniel Dunbara10e5192009-06-24 23:30:00 +00002522 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002523 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002524 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002525 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002526 return true;
2527
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002528 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002529 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2530 assert(Size <= 8 && "Invalid size");
2531 uint64_t IntValue = MCE->getValue();
2532 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2533 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002534 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002535 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002536 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002537
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002538 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002540
Daniel Dunbara10e5192009-06-24 23:30:00 +00002541 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002542 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002543 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002544 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002545 }
2546 }
2547
Sean Callanan686ed8d2010-01-19 20:22:31 +00002548 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002549 return false;
2550}
2551
David Woodhoused6de0d92014-02-01 16:20:59 +00002552/// ParseDirectiveOctaValue
2553/// ::= .octa [ hexconstant (, hexconstant)* ]
2554bool AsmParser::parseDirectiveOctaValue() {
2555 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2556 checkForValidSection();
2557
2558 for (;;) {
2559 if (Lexer.getKind() == AsmToken::Error)
2560 return true;
2561 if (Lexer.getKind() != AsmToken::Integer &&
2562 Lexer.getKind() != AsmToken::BigNum)
2563 return TokError("unknown token in expression");
2564
2565 SMLoc ExprLoc = getLexer().getLoc();
2566 APInt IntValue = getTok().getAPIntVal();
2567 Lex();
2568
2569 uint64_t hi, lo;
2570 if (IntValue.isIntN(64)) {
2571 hi = 0;
2572 lo = IntValue.getZExtValue();
2573 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002574 // It might actually have more than 128 bits, but the top ones are zero.
2575 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002576 lo = IntValue.getLoBits(64).getZExtValue();
2577 } else
2578 return Error(ExprLoc, "literal value out of range for directive");
2579
2580 if (MAI.isLittleEndian()) {
2581 getStreamer().EmitIntValue(lo, 8);
2582 getStreamer().EmitIntValue(hi, 8);
2583 } else {
2584 getStreamer().EmitIntValue(hi, 8);
2585 getStreamer().EmitIntValue(lo, 8);
2586 }
2587
2588 if (getLexer().is(AsmToken::EndOfStatement))
2589 break;
2590
2591 // FIXME: Improve diagnostic.
2592 if (getLexer().isNot(AsmToken::Comma))
2593 return TokError("unexpected token in directive");
2594 Lex();
2595 }
2596 }
2597
2598 Lex();
2599 return false;
2600}
2601
Jim Grosbach4b905842013-09-20 23:08:21 +00002602/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002603/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002604bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002605 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002606 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002607
2608 for (;;) {
2609 // We don't truly support arithmetic on floating point expressions, so we
2610 // have to manually parse unary prefixes.
2611 bool IsNeg = false;
2612 if (getLexer().is(AsmToken::Minus)) {
2613 Lex();
2614 IsNeg = true;
2615 } else if (getLexer().is(AsmToken::Plus))
2616 Lex();
2617
Michael J. Spencer530ce852010-10-09 11:00:50 +00002618 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002619 getLexer().isNot(AsmToken::Real) &&
2620 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002621 return TokError("unexpected token in directive");
2622
2623 // Convert to an APFloat.
2624 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002625 StringRef IDVal = getTok().getString();
2626 if (getLexer().is(AsmToken::Identifier)) {
2627 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2628 Value = APFloat::getInf(Semantics);
2629 else if (!IDVal.compare_lower("nan"))
2630 Value = APFloat::getNaN(Semantics, false, ~0);
2631 else
2632 return TokError("invalid floating point literal");
2633 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002634 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002635 return TokError("invalid floating point literal");
2636 if (IsNeg)
2637 Value.changeSign();
2638
2639 // Consume the numeric token.
2640 Lex();
2641
2642 // Emit the value as an integer.
2643 APInt AsInt = Value.bitcastToAPInt();
2644 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002645 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002646
2647 if (getLexer().is(AsmToken::EndOfStatement))
2648 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002649
Daniel Dunbar2af16532010-09-24 01:59:56 +00002650 if (getLexer().isNot(AsmToken::Comma))
2651 return TokError("unexpected token in directive");
2652 Lex();
2653 }
2654 }
2655
2656 Lex();
2657 return false;
2658}
2659
Jim Grosbach4b905842013-09-20 23:08:21 +00002660/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002661/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002662bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002663 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002664
2665 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002666 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002667 return true;
2668
Rafael Espindolab91bac62010-10-05 19:42:57 +00002669 int64_t Val = 0;
2670 if (getLexer().is(AsmToken::Comma)) {
2671 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002672 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002673 return true;
2674 }
2675
Rafael Espindola922e3f42010-09-16 15:03:59 +00002676 if (getLexer().isNot(AsmToken::EndOfStatement))
2677 return TokError("unexpected token in '.zero' directive");
2678
2679 Lex();
2680
Rafael Espindola64e1af82013-07-02 15:49:13 +00002681 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002682
2683 return false;
2684}
2685
Jim Grosbach4b905842013-09-20 23:08:21 +00002686/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002687/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002688bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002689 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002690
David Majnemer522d3db2014-02-01 07:19:38 +00002691 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002692 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002693 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002694 return true;
2695
David Majnemer522d3db2014-02-01 07:19:38 +00002696 if (NumValues < 0) {
2697 Warning(RepeatLoc,
2698 "'.fill' directive with negative repeat count has no effect");
2699 NumValues = 0;
2700 }
2701
Roman Divackye33098f2013-09-24 17:44:41 +00002702 int64_t FillSize = 1;
2703 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002704
David Majnemer522d3db2014-02-01 07:19:38 +00002705 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2707 if (getLexer().isNot(AsmToken::Comma))
2708 return TokError("unexpected token in '.fill' directive");
2709 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002710
David Majnemer522d3db2014-02-01 07:19:38 +00002711 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002712 if (parseAbsoluteExpression(FillSize))
2713 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002714
Roman Divackye33098f2013-09-24 17:44:41 +00002715 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2716 if (getLexer().isNot(AsmToken::Comma))
2717 return TokError("unexpected token in '.fill' directive");
2718 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002719
David Majnemer522d3db2014-02-01 07:19:38 +00002720 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002721 if (parseAbsoluteExpression(FillExpr))
2722 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002723
Roman Divackye33098f2013-09-24 17:44:41 +00002724 if (getLexer().isNot(AsmToken::EndOfStatement))
2725 return TokError("unexpected token in '.fill' directive");
2726
2727 Lex();
2728 }
2729 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002730
David Majnemer522d3db2014-02-01 07:19:38 +00002731 if (FillSize < 0) {
2732 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2733 NumValues = 0;
2734 }
2735 if (FillSize > 8) {
2736 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2737 FillSize = 8;
2738 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002739
David Majnemer522d3db2014-02-01 07:19:38 +00002740 if (!isUInt<32>(FillExpr) && FillSize > 4)
2741 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2742
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002743 if (NumValues > 0) {
2744 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2745 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2746 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2747 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2748 if (NonZeroFillSize < FillSize)
2749 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2750 }
David Majnemer522d3db2014-02-01 07:19:38 +00002751 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002752
2753 return false;
2754}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002755
Jim Grosbach4b905842013-09-20 23:08:21 +00002756/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002757/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002758bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002759 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002760
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002761 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002762 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002763 return true;
2764
2765 // Parse optional fill expression.
2766 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002767 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2768 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002769 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002770 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002771
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002772 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002773 return true;
2774
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002775 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002776 return TokError("unexpected token in '.org' directive");
2777 }
2778
Sean Callanan686ed8d2010-01-19 20:22:31 +00002779 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002780 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002781 return false;
2782}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002783
Jim Grosbach4b905842013-09-20 23:08:21 +00002784/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002785/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002786bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002787 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002788
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002789 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002790 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002791 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002792 return true;
2793
2794 SMLoc MaxBytesLoc;
2795 bool HasFillExpr = false;
2796 int64_t FillExpr = 0;
2797 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002798 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2799 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002800 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002801 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002802
2803 // The fill expression can be omitted while specifying a maximum number of
2804 // alignment bytes, e.g:
2805 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002806 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002807 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002808 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002809 return true;
2810 }
2811
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002812 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2813 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002814 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002815 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002816
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002817 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002818 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002819 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002820
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002821 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002822 return TokError("unexpected token in directive");
2823 }
2824 }
2825
Sean Callanan686ed8d2010-01-19 20:22:31 +00002826 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002827
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002828 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002829 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002830
2831 // Compute alignment in bytes.
2832 if (IsPow2) {
2833 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002834 if (Alignment >= 32) {
2835 Error(AlignmentLoc, "invalid alignment value");
2836 Alignment = 31;
2837 }
2838
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002839 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002840 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002841 // Reject alignments that aren't either a power of two or zero,
2842 // for gas compatibility. Alignment of zero is silently rounded
2843 // up to one.
2844 if (Alignment == 0)
2845 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002846 if (!isPowerOf2_64(Alignment))
2847 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002848 }
2849
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002850 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002851 if (MaxBytesLoc.isValid()) {
2852 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002853 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002854 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002855 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002856 }
2857
2858 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002859 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002860 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002861 MaxBytesToFill = 0;
2862 }
2863 }
2864
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002865 // Check whether we should use optimal code alignment for this .align
2866 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002867 const MCSection *Section = getStreamer().getCurrentSection().first;
2868 assert(Section && "must have section to emit alignment");
2869 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002870 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2871 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002872 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002873 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002874 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002875 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2876 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002877 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002878
2879 return false;
2880}
2881
Jim Grosbach4b905842013-09-20 23:08:21 +00002882/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002883/// ::= .file [number] filename
2884/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002885bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002886 // FIXME: I'm not sure what this is.
2887 int64_t FileNumber = -1;
2888 SMLoc FileNumberLoc = getLexer().getLoc();
2889 if (getLexer().is(AsmToken::Integer)) {
2890 FileNumber = getTok().getIntVal();
2891 Lex();
2892
2893 if (FileNumber < 1)
2894 return TokError("file number less than one");
2895 }
2896
2897 if (getLexer().isNot(AsmToken::String))
2898 return TokError("unexpected token in '.file' directive");
2899
2900 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002901 // Allow the strings to have escaped octal character sequence.
2902 std::string Path = getTok().getString();
2903 if (parseEscapedString(Path))
2904 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002905 Lex();
2906
2907 StringRef Directory;
2908 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002909 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002910 if (getLexer().is(AsmToken::String)) {
2911 if (FileNumber == -1)
2912 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002913 if (parseEscapedString(FilenameData))
2914 return true;
2915 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002916 Directory = Path;
2917 Lex();
2918 } else {
2919 Filename = Path;
2920 }
2921
2922 if (getLexer().isNot(AsmToken::EndOfStatement))
2923 return TokError("unexpected token in '.file' directive");
2924
2925 if (FileNumber == -1)
2926 getStreamer().EmitFileDirective(Filename);
2927 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002928 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002929 Error(DirectiveLoc,
2930 "input can't have .file dwarf directives when -g is "
2931 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002932
David Blaikiec714ef42014-03-17 01:52:11 +00002933 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2934 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002935 Error(FileNumberLoc, "file number already allocated");
2936 }
2937
2938 return false;
2939}
2940
Jim Grosbach4b905842013-09-20 23:08:21 +00002941/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002942/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002943bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002944 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2945 if (getLexer().isNot(AsmToken::Integer))
2946 return TokError("unexpected token in '.line' directive");
2947
2948 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002949 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002950 Lex();
2951
2952 // FIXME: Do something with the .line.
2953 }
2954
2955 if (getLexer().isNot(AsmToken::EndOfStatement))
2956 return TokError("unexpected token in '.line' directive");
2957
2958 return false;
2959}
2960
Jim Grosbach4b905842013-09-20 23:08:21 +00002961/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002962/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2963/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2964/// The first number is a file number, must have been previously assigned with
2965/// a .file directive, the second number is the line number and optionally the
2966/// third number is a column position (zero if not specified). The remaining
2967/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002968bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002969 if (getLexer().isNot(AsmToken::Integer))
2970 return TokError("unexpected token in '.loc' directive");
2971 int64_t FileNumber = getTok().getIntVal();
2972 if (FileNumber < 1)
2973 return TokError("file number less than one in '.loc' directive");
2974 if (!getContext().isValidDwarfFileNumber(FileNumber))
2975 return TokError("unassigned file number in '.loc' directive");
2976 Lex();
2977
2978 int64_t LineNumber = 0;
2979 if (getLexer().is(AsmToken::Integer)) {
2980 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002981 if (LineNumber < 0)
2982 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002983 Lex();
2984 }
2985
2986 int64_t ColumnPos = 0;
2987 if (getLexer().is(AsmToken::Integer)) {
2988 ColumnPos = getTok().getIntVal();
2989 if (ColumnPos < 0)
2990 return TokError("column position less than zero in '.loc' directive");
2991 Lex();
2992 }
2993
2994 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2995 unsigned Isa = 0;
2996 int64_t Discriminator = 0;
2997 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2998 for (;;) {
2999 if (getLexer().is(AsmToken::EndOfStatement))
3000 break;
3001
3002 StringRef Name;
3003 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003004 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003005 return TokError("unexpected token in '.loc' directive");
3006
3007 if (Name == "basic_block")
3008 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3009 else if (Name == "prologue_end")
3010 Flags |= DWARF2_FLAG_PROLOGUE_END;
3011 else if (Name == "epilogue_begin")
3012 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3013 else if (Name == "is_stmt") {
3014 Loc = getTok().getLoc();
3015 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003016 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return true;
3018 // The expression must be the constant 0 or 1.
3019 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3020 int Value = MCE->getValue();
3021 if (Value == 0)
3022 Flags &= ~DWARF2_FLAG_IS_STMT;
3023 else if (Value == 1)
3024 Flags |= DWARF2_FLAG_IS_STMT;
3025 else
3026 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003027 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003028 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3029 }
Craig Topperf15655b2013-04-22 04:22:40 +00003030 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003031 Loc = getTok().getLoc();
3032 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003033 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003034 return true;
3035 // The expression must be a constant greater or equal to 0.
3036 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3037 int Value = MCE->getValue();
3038 if (Value < 0)
3039 return Error(Loc, "isa number less than zero");
3040 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003041 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return Error(Loc, "isa number not a constant value");
3043 }
Craig Topperf15655b2013-04-22 04:22:40 +00003044 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003045 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003046 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003047 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return Error(Loc, "unknown sub-directive in '.loc' directive");
3049 }
3050
3051 if (getLexer().is(AsmToken::EndOfStatement))
3052 break;
3053 }
3054 }
3055
3056 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3057 Isa, Discriminator, StringRef());
3058
3059 return false;
3060}
3061
Jim Grosbach4b905842013-09-20 23:08:21 +00003062/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003063/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003064bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003065 return TokError("unsupported directive '.stabs'");
3066}
3067
Jim Grosbach4b905842013-09-20 23:08:21 +00003068/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003069/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003070bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003071 StringRef Name;
3072 bool EH = false;
3073 bool Debug = false;
3074
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003075 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003076 return TokError("Expected an identifier");
3077
3078 if (Name == ".eh_frame")
3079 EH = true;
3080 else if (Name == ".debug_frame")
3081 Debug = true;
3082
3083 if (getLexer().is(AsmToken::Comma)) {
3084 Lex();
3085
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003086 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003087 return TokError("Expected an identifier");
3088
3089 if (Name == ".eh_frame")
3090 EH = true;
3091 else if (Name == ".debug_frame")
3092 Debug = true;
3093 }
3094
3095 getStreamer().EmitCFISections(EH, Debug);
3096 return false;
3097}
3098
Jim Grosbach4b905842013-09-20 23:08:21 +00003099/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003100/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003101bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003102 StringRef Simple;
3103 if (getLexer().isNot(AsmToken::EndOfStatement))
3104 if (parseIdentifier(Simple) || Simple != "simple")
3105 return TokError("unexpected token in .cfi_startproc directive");
3106
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003107 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003108 return false;
3109}
3110
Jim Grosbach4b905842013-09-20 23:08:21 +00003111/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003112/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003113bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003114 getStreamer().EmitCFIEndProc();
3115 return false;
3116}
3117
Jim Grosbach4b905842013-09-20 23:08:21 +00003118/// \brief parse register name or number.
3119bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003120 SMLoc DirectiveLoc) {
3121 unsigned RegNo;
3122
3123 if (getLexer().isNot(AsmToken::Integer)) {
3124 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3125 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003126 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003127 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003128 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003129
3130 return false;
3131}
3132
Jim Grosbach4b905842013-09-20 23:08:21 +00003133/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003134/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003135bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003136 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003137 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003138 return true;
3139
3140 if (getLexer().isNot(AsmToken::Comma))
3141 return TokError("unexpected token in directive");
3142 Lex();
3143
3144 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003145 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003146 return true;
3147
3148 getStreamer().EmitCFIDefCfa(Register, Offset);
3149 return false;
3150}
3151
Jim Grosbach4b905842013-09-20 23:08:21 +00003152/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003153/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003154bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003155 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003156 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003157 return true;
3158
3159 getStreamer().EmitCFIDefCfaOffset(Offset);
3160 return false;
3161}
3162
Jim Grosbach4b905842013-09-20 23:08:21 +00003163/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003164/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003165bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003166 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003167 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003168 return true;
3169
3170 if (getLexer().isNot(AsmToken::Comma))
3171 return TokError("unexpected token in directive");
3172 Lex();
3173
3174 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003175 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003176 return true;
3177
3178 getStreamer().EmitCFIRegister(Register1, Register2);
3179 return false;
3180}
3181
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003182/// parseDirectiveCFIWindowSave
3183/// ::= .cfi_window_save
3184bool AsmParser::parseDirectiveCFIWindowSave() {
3185 getStreamer().EmitCFIWindowSave();
3186 return false;
3187}
3188
Jim Grosbach4b905842013-09-20 23:08:21 +00003189/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003190/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003191bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003192 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003193 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003194 return true;
3195
3196 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3197 return false;
3198}
3199
Jim Grosbach4b905842013-09-20 23:08:21 +00003200/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003201/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003202bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003203 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003204 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003205 return true;
3206
3207 getStreamer().EmitCFIDefCfaRegister(Register);
3208 return false;
3209}
3210
Jim Grosbach4b905842013-09-20 23:08:21 +00003211/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003212/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003213bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003214 int64_t Register = 0;
3215 int64_t Offset = 0;
3216
Jim Grosbach4b905842013-09-20 23:08:21 +00003217 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003218 return true;
3219
3220 if (getLexer().isNot(AsmToken::Comma))
3221 return TokError("unexpected token in directive");
3222 Lex();
3223
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003224 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003225 return true;
3226
3227 getStreamer().EmitCFIOffset(Register, Offset);
3228 return false;
3229}
3230
Jim Grosbach4b905842013-09-20 23:08:21 +00003231/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003232/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003233bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003234 int64_t Register = 0;
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003237 return true;
3238
3239 if (getLexer().isNot(AsmToken::Comma))
3240 return TokError("unexpected token in directive");
3241 Lex();
3242
3243 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003244 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003245 return true;
3246
3247 getStreamer().EmitCFIRelOffset(Register, Offset);
3248 return false;
3249}
3250
3251static bool isValidEncoding(int64_t Encoding) {
3252 if (Encoding & ~0xff)
3253 return false;
3254
3255 if (Encoding == dwarf::DW_EH_PE_omit)
3256 return true;
3257
3258 const unsigned Format = Encoding & 0xf;
3259 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3260 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3261 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3262 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3263 return false;
3264
3265 const unsigned Application = Encoding & 0x70;
3266 if (Application != dwarf::DW_EH_PE_absptr &&
3267 Application != dwarf::DW_EH_PE_pcrel)
3268 return false;
3269
3270 return true;
3271}
3272
Jim Grosbach4b905842013-09-20 23:08:21 +00003273/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003274/// IsPersonality true for cfi_personality, false for cfi_lsda
3275/// ::= .cfi_personality encoding, [symbol_name]
3276/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003277bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003278 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003279 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003280 return true;
3281 if (Encoding == dwarf::DW_EH_PE_omit)
3282 return false;
3283
3284 if (!isValidEncoding(Encoding))
3285 return TokError("unsupported encoding.");
3286
3287 if (getLexer().isNot(AsmToken::Comma))
3288 return TokError("unexpected token in directive");
3289 Lex();
3290
3291 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003292 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003293 return TokError("expected identifier in directive");
3294
Jim Grosbach6f482002015-05-18 18:43:14 +00003295 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003296
3297 if (IsPersonality)
3298 getStreamer().EmitCFIPersonality(Sym, Encoding);
3299 else
3300 getStreamer().EmitCFILsda(Sym, Encoding);
3301 return false;
3302}
3303
Jim Grosbach4b905842013-09-20 23:08:21 +00003304/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003305/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003306bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003307 getStreamer().EmitCFIRememberState();
3308 return false;
3309}
3310
Jim Grosbach4b905842013-09-20 23:08:21 +00003311/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003312/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003313bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003314 getStreamer().EmitCFIRestoreState();
3315 return false;
3316}
3317
Jim Grosbach4b905842013-09-20 23:08:21 +00003318/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003319/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003320bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003321 int64_t Register = 0;
3322
Jim Grosbach4b905842013-09-20 23:08:21 +00003323 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003324 return true;
3325
3326 getStreamer().EmitCFISameValue(Register);
3327 return false;
3328}
3329
Jim Grosbach4b905842013-09-20 23:08:21 +00003330/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003331/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003332bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003333 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003334 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003335 return true;
3336
3337 getStreamer().EmitCFIRestore(Register);
3338 return false;
3339}
3340
Jim Grosbach4b905842013-09-20 23:08:21 +00003341/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003342/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003343bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003344 std::string Values;
3345 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003346 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003347 return true;
3348
3349 Values.push_back((uint8_t)CurrValue);
3350
3351 while (getLexer().is(AsmToken::Comma)) {
3352 Lex();
3353
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003354 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003355 return true;
3356
3357 Values.push_back((uint8_t)CurrValue);
3358 }
3359
3360 getStreamer().EmitCFIEscape(Values);
3361 return false;
3362}
3363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003365/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003366bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003367 if (getLexer().isNot(AsmToken::EndOfStatement))
3368 return Error(getLexer().getLoc(),
3369 "unexpected token in '.cfi_signal_frame'");
3370
3371 getStreamer().EmitCFISignalFrame();
3372 return false;
3373}
3374
Jim Grosbach4b905842013-09-20 23:08:21 +00003375/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003376/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003377bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003378 int64_t Register = 0;
3379
Jim Grosbach4b905842013-09-20 23:08:21 +00003380 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003381 return true;
3382
3383 getStreamer().EmitCFIUndefined(Register);
3384 return false;
3385}
3386
Jim Grosbach4b905842013-09-20 23:08:21 +00003387/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003388/// ::= .macros_on
3389/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003390bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003391 if (getLexer().isNot(AsmToken::EndOfStatement))
3392 return Error(getLexer().getLoc(),
3393 "unexpected token in '" + Directive + "' directive");
3394
Jim Grosbach4b905842013-09-20 23:08:21 +00003395 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003396 return false;
3397}
3398
Jim Grosbach4b905842013-09-20 23:08:21 +00003399/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003400/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003401bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003402 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003403 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003404 return TokError("expected identifier in '.macro' directive");
3405
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003406 if (getLexer().is(AsmToken::Comma))
3407 Lex();
3408
Eli Bendersky17233942013-01-15 22:59:42 +00003409 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003410 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003411
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003412 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003413 return Error(Lexer.getLoc(),
3414 "Vararg parameter '" + Parameters.back().Name +
3415 "' should be last one in the list of parameters.");
3416
David Majnemer91fc4c22014-01-29 18:57:46 +00003417 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003418 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003419 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003420
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003421 if (Lexer.is(AsmToken::Colon)) {
3422 Lex(); // consume ':'
3423
3424 SMLoc QualLoc;
3425 StringRef Qualifier;
3426
3427 QualLoc = Lexer.getLoc();
3428 if (parseIdentifier(Qualifier))
3429 return Error(QualLoc, "missing parameter qualifier for "
3430 "'" + Parameter.Name + "' in macro '" + Name + "'");
3431
3432 if (Qualifier == "req")
3433 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003434 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003435 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003436 else
3437 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3438 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3439 }
3440
David Majnemer91fc4c22014-01-29 18:57:46 +00003441 if (getLexer().is(AsmToken::Equal)) {
3442 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003443
3444 SMLoc ParamLoc;
3445
3446 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003447 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003448 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003449
3450 if (Parameter.Required)
3451 Warning(ParamLoc, "pointless default value for required parameter "
3452 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003453 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003454
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003455 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003456
3457 if (getLexer().is(AsmToken::Comma))
3458 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003459 }
3460
3461 // Eat the end of statement.
3462 Lex();
3463
3464 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003465 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003466
3467 // Lex the macro definition.
3468 for (;;) {
3469 // Check whether we have reached the end of the file.
3470 if (getLexer().is(AsmToken::Eof))
3471 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3472
3473 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003474 if (getLexer().is(AsmToken::Identifier)) {
3475 if (getTok().getIdentifier() == ".endm" ||
3476 getTok().getIdentifier() == ".endmacro") {
3477 if (MacroDepth == 0) { // Outermost macro.
3478 EndToken = getTok();
3479 Lex();
3480 if (getLexer().isNot(AsmToken::EndOfStatement))
3481 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3482 "' directive");
3483 break;
3484 } else {
3485 // Otherwise we just found the end of an inner macro.
3486 --MacroDepth;
3487 }
3488 } else if (getTok().getIdentifier() == ".macro") {
3489 // We allow nested macros. Those aren't instantiated until the outermost
3490 // macro is expanded so just ignore them for now.
3491 ++MacroDepth;
3492 }
Eli Bendersky17233942013-01-15 22:59:42 +00003493 }
3494
3495 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003496 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003497 }
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003500 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3501 }
3502
3503 const char *BodyStart = StartToken.getLoc().getPointer();
3504 const char *BodyEnd = EndToken.getLoc().getPointer();
3505 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003506 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003507 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003508 return false;
3509}
3510
Jim Grosbach4b905842013-09-20 23:08:21 +00003511/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003512///
3513/// With the support added for named parameters there may be code out there that
3514/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003515/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003516/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003517/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003518/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3519/// warning that the positional parameter found in body which have no effect.
3520/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003521/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003522/// intended or change the macro to use the named parameters. It is possible
3523/// this warning will trigger when the none of the named parameters are used
3524/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003525void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003526 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003527 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003528 // If this macro is not defined with named parameters the warning we are
3529 // checking for here doesn't apply.
3530 unsigned NParameters = Parameters.size();
3531 if (NParameters == 0)
3532 return;
3533
3534 bool NamedParametersFound = false;
3535 bool PositionalParametersFound = false;
3536
3537 // Look at the body of the macro for use of both the named parameters and what
3538 // are likely to be positional parameters. This is what expandMacro() is
3539 // doing when it finds the parameters in the body.
3540 while (!Body.empty()) {
3541 // Scan for the next possible parameter.
3542 std::size_t End = Body.size(), Pos = 0;
3543 for (; Pos != End; ++Pos) {
3544 // Check for a substitution or escape.
3545 // This macro is defined with parameters, look for \foo, \bar, etc.
3546 if (Body[Pos] == '\\' && Pos + 1 != End)
3547 break;
3548
3549 // This macro should have parameters, but look for $0, $1, ..., $n too.
3550 if (Body[Pos] != '$' || Pos + 1 == End)
3551 continue;
3552 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003553 if (Next == '$' || Next == 'n' ||
3554 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003555 break;
3556 }
3557
3558 // Check if we reached the end.
3559 if (Pos == End)
3560 break;
3561
3562 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003563 switch (Body[Pos + 1]) {
3564 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003565 case '$':
3566 break;
3567
Jim Grosbach4b905842013-09-20 23:08:21 +00003568 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003569 case 'n':
3570 PositionalParametersFound = true;
3571 break;
3572
Jim Grosbach4b905842013-09-20 23:08:21 +00003573 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003574 default: {
3575 PositionalParametersFound = true;
3576 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003577 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003578 }
3579 Pos += 2;
3580 } else {
3581 unsigned I = Pos + 1;
3582 while (isIdentifierChar(Body[I]) && I + 1 != End)
3583 ++I;
3584
Jim Grosbach4b905842013-09-20 23:08:21 +00003585 const char *Begin = Body.data() + Pos + 1;
3586 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003587 unsigned Index = 0;
3588 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003589 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003590 break;
3591
3592 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003593 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3594 Pos += 3;
3595 else {
3596 Pos = I;
3597 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003598 } else {
3599 NamedParametersFound = true;
3600 Pos += 1 + Argument.size();
3601 }
3602 }
3603 // Update the scan point.
3604 Body = Body.substr(Pos);
3605 }
3606
3607 if (!NamedParametersFound && PositionalParametersFound)
3608 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3609 "used in macro body, possible positional parameter "
3610 "found in body which will have no effect");
3611}
3612
Nico Weber155dccd12014-07-24 17:08:39 +00003613/// parseDirectiveExitMacro
3614/// ::= .exitm
3615bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3616 if (getLexer().isNot(AsmToken::EndOfStatement))
3617 return TokError("unexpected token in '" + Directive + "' directive");
3618
3619 if (!isInsideMacroInstantiation())
3620 return TokError("unexpected '" + Directive + "' in file, "
3621 "no current macro definition");
3622
3623 // Exit all conditionals that are active in the current macro.
3624 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3625 TheCondState = TheCondStack.back();
3626 TheCondStack.pop_back();
3627 }
3628
3629 handleMacroExit();
3630 return false;
3631}
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003634/// ::= .endm
3635/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003636bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003637 if (getLexer().isNot(AsmToken::EndOfStatement))
3638 return TokError("unexpected token in '" + Directive + "' directive");
3639
3640 // If we are inside a macro instantiation, terminate the current
3641 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003642 if (isInsideMacroInstantiation()) {
3643 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003644 return false;
3645 }
3646
3647 // Otherwise, this .endmacro is a stray entry in the file; well formed
3648 // .endmacro directives are handled during the macro definition parsing.
3649 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003650 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003651}
3652
Jim Grosbach4b905842013-09-20 23:08:21 +00003653/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003654/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003655bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003656 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003657 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003658 return TokError("expected identifier in '.purgem' directive");
3659
3660 if (getLexer().isNot(AsmToken::EndOfStatement))
3661 return TokError("unexpected token in '.purgem' directive");
3662
Jim Grosbach4b905842013-09-20 23:08:21 +00003663 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003664 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003667 return false;
3668}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003669
Jim Grosbach4b905842013-09-20 23:08:21 +00003670/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003671/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003672bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003674
3675 // Expect a single argument: an expression that evaluates to a constant
3676 // in the inclusive range 0-30.
3677 SMLoc ExprLoc = getLexer().getLoc();
3678 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003679 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003680 return true;
3681 else if (getLexer().isNot(AsmToken::EndOfStatement))
3682 return TokError("unexpected token after expression in"
3683 " '.bundle_align_mode' directive");
3684 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3685 return Error(ExprLoc,
3686 "invalid bundle alignment size (expected between 0 and 30)");
3687
3688 Lex();
3689
3690 // Because of AlignSizePow2's verified range we can safely truncate it to
3691 // unsigned.
3692 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3693 return false;
3694}
3695
Jim Grosbach4b905842013-09-20 23:08:21 +00003696/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003697/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003698bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003699 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003700 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003701
Eli Bendersky802b6282013-01-07 21:51:08 +00003702 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3703 StringRef Option;
3704 SMLoc Loc = getTok().getLoc();
3705 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003706 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003707
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003708 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003709 return Error(Loc, kInvalidOptionError);
3710
3711 if (Option != "align_to_end")
3712 return Error(Loc, kInvalidOptionError);
3713 else if (getLexer().isNot(AsmToken::EndOfStatement))
3714 return Error(Loc,
3715 "unexpected token after '.bundle_lock' directive option");
3716 AlignToEnd = true;
3717 }
3718
Eli Benderskyf483ff92012-12-20 19:05:53 +00003719 Lex();
3720
Eli Bendersky802b6282013-01-07 21:51:08 +00003721 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003722 return false;
3723}
3724
Jim Grosbach4b905842013-09-20 23:08:21 +00003725/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003726/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003727bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003728 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003729
3730 if (getLexer().isNot(AsmToken::EndOfStatement))
3731 return TokError("unexpected token in '.bundle_unlock' directive");
3732 Lex();
3733
3734 getStreamer().EmitBundleUnlock();
3735 return false;
3736}
3737
Jim Grosbach4b905842013-09-20 23:08:21 +00003738/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003739/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003740bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003741 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003742
3743 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003744 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003745 return true;
3746
3747 int64_t FillExpr = 0;
3748 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3749 if (getLexer().isNot(AsmToken::Comma))
3750 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3751 Lex();
3752
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003753 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003754 return true;
3755
3756 if (getLexer().isNot(AsmToken::EndOfStatement))
3757 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3758 }
3759
3760 Lex();
3761
3762 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003763 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3764 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003765
3766 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003767 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003768
3769 return false;
3770}
3771
Jim Grosbach4b905842013-09-20 23:08:21 +00003772/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003773/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003774bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003775 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003776 const MCExpr *Value;
3777
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003778 for (;;) {
3779 if (parseExpression(Value))
3780 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003781
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003782 if (Signed)
3783 getStreamer().EmitSLEB128Value(Value);
3784 else
3785 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003786
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003787 if (getLexer().is(AsmToken::EndOfStatement))
3788 break;
3789
3790 if (getLexer().isNot(AsmToken::Comma))
3791 return TokError("unexpected token in directive");
3792 Lex();
3793 }
Eli Bendersky17233942013-01-15 22:59:42 +00003794
3795 return false;
3796}
3797
Jim Grosbach4b905842013-09-20 23:08:21 +00003798/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003799/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003800bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003801 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003802 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003803 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003804 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003805
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003807 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003808
Jim Grosbach6f482002015-05-18 18:43:14 +00003809 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003810
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003811 // Assembler local symbols don't make any sense here. Complain loudly.
3812 if (Sym->isTemporary())
3813 return Error(Loc, "non-local symbol required in directive");
3814
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003815 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3816 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003817
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003818 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003819 break;
3820
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003821 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003822 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003823 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003824 }
3825 }
3826
Sean Callanan686ed8d2010-01-19 20:22:31 +00003827 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003828 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003829}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003830
Jim Grosbach4b905842013-09-20 23:08:21 +00003831/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003832/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003833bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003834 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003835
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003836 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003837 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003838 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003839 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003840
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003841 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003842 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003843
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003844 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003845 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003846 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003847
3848 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003849 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003850 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003851 return true;
3852
3853 int64_t Pow2Alignment = 0;
3854 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003855 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003856 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003857 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003858 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003859 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003860
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003861 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3862 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003863 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3864
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003865 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003866 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3867 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003868 if (!isPowerOf2_64(Pow2Alignment))
3869 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3870 Pow2Alignment = Log2_64(Pow2Alignment);
3871 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003872 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003873
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003874 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003875 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003876
Sean Callanan686ed8d2010-01-19 20:22:31 +00003877 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003878
Chris Lattner28ad7542009-07-09 17:25:12 +00003879 // NOTE: a size of zero for a .comm should create a undefined symbol
3880 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003881 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003882 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003883 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003884
Eric Christopherbc818852010-05-14 01:38:54 +00003885 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003886 // may internally end up wanting an alignment in bytes.
3887 // FIXME: Diagnose overflow.
3888 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003889 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003890 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003891
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003892 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003893 return Error(IDLoc, "invalid symbol redefinition");
3894
Chris Lattner28ad7542009-07-09 17:25:12 +00003895 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003896 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003897 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003898 return false;
3899 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003900
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003901 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003902 return false;
3903}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003904
Jim Grosbach4b905842013-09-20 23:08:21 +00003905/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003906/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003907bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003908 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003909 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003910
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003911 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003912 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003913 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003914
Sean Callanan686ed8d2010-01-19 20:22:31 +00003915 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003916
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003917 if (Str.empty())
3918 Error(Loc, ".abort detected. Assembly stopping.");
3919 else
3920 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003921 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003922
3923 return false;
3924}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003925
Jim Grosbach4b905842013-09-20 23:08:21 +00003926/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003927/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003928bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003929 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003930 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003931
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003932 // Allow the strings to have escaped octal character sequence.
3933 std::string Filename;
3934 if (parseEscapedString(Filename))
3935 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003936 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003937 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003938
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003939 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003940 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003941
Chris Lattner693fbb82009-07-16 06:14:39 +00003942 // Attempt to switch the lexer to the included file before consuming the end
3943 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003944 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003945 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003946 return true;
3947 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003948
3949 return false;
3950}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003951
Jim Grosbach4b905842013-09-20 23:08:21 +00003952/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003953/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003954bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003955 if (getLexer().isNot(AsmToken::String))
3956 return TokError("expected string in '.incbin' directive");
3957
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003958 // Allow the strings to have escaped octal character sequence.
3959 std::string Filename;
3960 if (parseEscapedString(Filename))
3961 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003962 SMLoc IncbinLoc = getLexer().getLoc();
3963 Lex();
3964
3965 if (getLexer().isNot(AsmToken::EndOfStatement))
3966 return TokError("unexpected token in '.incbin' directive");
3967
Kevin Enderby109f25c2011-12-14 21:47:48 +00003968 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003969 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003970 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3971 return true;
3972 }
3973
3974 return false;
3975}
3976
Jim Grosbach4b905842013-09-20 23:08:21 +00003977/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003978/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3979bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003980 TheCondStack.push_back(TheCondState);
3981 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003982 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003983 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003984 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003986 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003987 return true;
3988
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003989 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003990 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003991
Sean Callanan686ed8d2010-01-19 20:22:31 +00003992 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003993
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003994 switch (DirKind) {
3995 default:
3996 llvm_unreachable("unsupported directive");
3997 case DK_IF:
3998 case DK_IFNE:
3999 break;
4000 case DK_IFEQ:
4001 ExprValue = ExprValue == 0;
4002 break;
4003 case DK_IFGE:
4004 ExprValue = ExprValue >= 0;
4005 break;
4006 case DK_IFGT:
4007 ExprValue = ExprValue > 0;
4008 break;
4009 case DK_IFLE:
4010 ExprValue = ExprValue <= 0;
4011 break;
4012 case DK_IFLT:
4013 ExprValue = ExprValue < 0;
4014 break;
4015 }
4016
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 TheCondState.CondMet = ExprValue;
4018 TheCondState.Ignore = !TheCondState.CondMet;
4019 }
4020
4021 return false;
4022}
4023
Jim Grosbach4b905842013-09-20 23:08:21 +00004024/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004025/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004026bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004027 TheCondStack.push_back(TheCondState);
4028 TheCondState.TheCond = AsmCond::IfCond;
4029
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004030 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004031 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004032 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004033 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004034
4035 if (getLexer().isNot(AsmToken::EndOfStatement))
4036 return TokError("unexpected token in '.ifb' directive");
4037
4038 Lex();
4039
4040 TheCondState.CondMet = ExpectBlank == Str.empty();
4041 TheCondState.Ignore = !TheCondState.CondMet;
4042 }
4043
4044 return false;
4045}
4046
Jim Grosbach4b905842013-09-20 23:08:21 +00004047/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004048/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004049/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004050bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004051 TheCondStack.push_back(TheCondState);
4052 TheCondState.TheCond = AsmCond::IfCond;
4053
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004054 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004055 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004056 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004057 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004058
4059 if (getLexer().isNot(AsmToken::Comma))
4060 return TokError("unexpected token in '.ifc' directive");
4061
4062 Lex();
4063
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004064 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004065
4066 if (getLexer().isNot(AsmToken::EndOfStatement))
4067 return TokError("unexpected token in '.ifc' directive");
4068
4069 Lex();
4070
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004071 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004072 TheCondState.Ignore = !TheCondState.CondMet;
4073 }
4074
4075 return false;
4076}
4077
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004078/// parseDirectiveIfeqs
4079/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004080bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004081 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004082 if (ExpectEqual)
4083 TokError("expected string parameter for '.ifeqs' directive");
4084 else
4085 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004086 eatToEndOfStatement();
4087 return true;
4088 }
4089
4090 StringRef String1 = getTok().getStringContents();
4091 Lex();
4092
4093 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004094 if (ExpectEqual)
4095 TokError("expected comma after first string for '.ifeqs' directive");
4096 else
4097 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004098 eatToEndOfStatement();
4099 return true;
4100 }
4101
4102 Lex();
4103
4104 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004105 if (ExpectEqual)
4106 TokError("expected string parameter for '.ifeqs' directive");
4107 else
4108 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004109 eatToEndOfStatement();
4110 return true;
4111 }
4112
4113 StringRef String2 = getTok().getStringContents();
4114 Lex();
4115
4116 TheCondStack.push_back(TheCondState);
4117 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004118 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004119 TheCondState.Ignore = !TheCondState.CondMet;
4120
4121 return false;
4122}
4123
Jim Grosbach4b905842013-09-20 23:08:21 +00004124/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004125/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004126bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004127 StringRef Name;
4128 TheCondStack.push_back(TheCondState);
4129 TheCondState.TheCond = AsmCond::IfCond;
4130
4131 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004132 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004133 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004134 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004135 return TokError("expected identifier after '.ifdef'");
4136
4137 Lex();
4138
Jim Grosbach6f482002015-05-18 18:43:14 +00004139 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004140
4141 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004142 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004143 else
Craig Topper353eda42014-04-24 06:44:33 +00004144 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004145 TheCondState.Ignore = !TheCondState.CondMet;
4146 }
4147
4148 return false;
4149}
4150
Jim Grosbach4b905842013-09-20 23:08:21 +00004151/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004152/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004153bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004154 if (TheCondState.TheCond != AsmCond::IfCond &&
4155 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004156 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4157 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004158 TheCondState.TheCond = AsmCond::ElseIfCond;
4159
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004160 bool LastIgnoreState = false;
4161 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004162 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004163 if (LastIgnoreState || TheCondState.CondMet) {
4164 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004165 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004166 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004167 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004168 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004169 return true;
4170
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004171 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004172 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004173
Sean Callanan686ed8d2010-01-19 20:22:31 +00004174 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004175 TheCondState.CondMet = ExprValue;
4176 TheCondState.Ignore = !TheCondState.CondMet;
4177 }
4178
4179 return false;
4180}
4181
Jim Grosbach4b905842013-09-20 23:08:21 +00004182/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004183/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004184bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004185 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004186 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004187
Sean Callanan686ed8d2010-01-19 20:22:31 +00004188 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004189
4190 if (TheCondState.TheCond != AsmCond::IfCond &&
4191 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004192 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4193 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004194 TheCondState.TheCond = AsmCond::ElseCond;
4195 bool LastIgnoreState = false;
4196 if (!TheCondStack.empty())
4197 LastIgnoreState = TheCondStack.back().Ignore;
4198 if (LastIgnoreState || TheCondState.CondMet)
4199 TheCondState.Ignore = true;
4200 else
4201 TheCondState.Ignore = false;
4202
4203 return false;
4204}
4205
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004206/// parseDirectiveEnd
4207/// ::= .end
4208bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4209 if (getLexer().isNot(AsmToken::EndOfStatement))
4210 return TokError("unexpected token in '.end' directive");
4211
4212 Lex();
4213
4214 while (Lexer.isNot(AsmToken::Eof))
4215 Lex();
4216
4217 return false;
4218}
4219
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004220/// parseDirectiveError
4221/// ::= .err
4222/// ::= .error [string]
4223bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4224 if (!TheCondStack.empty()) {
4225 if (TheCondStack.back().Ignore) {
4226 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004227 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004228 }
4229 }
4230
4231 if (!WithMessage)
4232 return Error(L, ".err encountered");
4233
4234 StringRef Message = ".error directive invoked in source file";
4235 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4236 if (Lexer.isNot(AsmToken::String)) {
4237 TokError(".error argument must be a string");
4238 eatToEndOfStatement();
4239 return true;
4240 }
4241
4242 Message = getTok().getStringContents();
4243 Lex();
4244 }
4245
4246 Error(L, Message);
4247 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004248}
4249
Nico Weber404012b2014-07-24 16:26:06 +00004250/// parseDirectiveWarning
4251/// ::= .warning [string]
4252bool AsmParser::parseDirectiveWarning(SMLoc L) {
4253 if (!TheCondStack.empty()) {
4254 if (TheCondStack.back().Ignore) {
4255 eatToEndOfStatement();
4256 return false;
4257 }
4258 }
4259
4260 StringRef Message = ".warning directive invoked in source file";
4261 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4262 if (Lexer.isNot(AsmToken::String)) {
4263 TokError(".warning argument must be a string");
4264 eatToEndOfStatement();
4265 return true;
4266 }
4267
4268 Message = getTok().getStringContents();
4269 Lex();
4270 }
4271
4272 Warning(L, Message);
4273 return false;
4274}
4275
Jim Grosbach4b905842013-09-20 23:08:21 +00004276/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004277/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004278bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004279 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004280 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004281
Sean Callanan686ed8d2010-01-19 20:22:31 +00004282 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004283
Jim Grosbach4b905842013-09-20 23:08:21 +00004284 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004285 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4286 ".else");
4287 if (!TheCondStack.empty()) {
4288 TheCondState = TheCondStack.back();
4289 TheCondStack.pop_back();
4290 }
4291
4292 return false;
4293}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004294
Eli Bendersky17233942013-01-15 22:59:42 +00004295void AsmParser::initializeDirectiveKindMap() {
4296 DirectiveKindMap[".set"] = DK_SET;
4297 DirectiveKindMap[".equ"] = DK_EQU;
4298 DirectiveKindMap[".equiv"] = DK_EQUIV;
4299 DirectiveKindMap[".ascii"] = DK_ASCII;
4300 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4301 DirectiveKindMap[".string"] = DK_STRING;
4302 DirectiveKindMap[".byte"] = DK_BYTE;
4303 DirectiveKindMap[".short"] = DK_SHORT;
4304 DirectiveKindMap[".value"] = DK_VALUE;
4305 DirectiveKindMap[".2byte"] = DK_2BYTE;
4306 DirectiveKindMap[".long"] = DK_LONG;
4307 DirectiveKindMap[".int"] = DK_INT;
4308 DirectiveKindMap[".4byte"] = DK_4BYTE;
4309 DirectiveKindMap[".quad"] = DK_QUAD;
4310 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004311 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004312 DirectiveKindMap[".single"] = DK_SINGLE;
4313 DirectiveKindMap[".float"] = DK_FLOAT;
4314 DirectiveKindMap[".double"] = DK_DOUBLE;
4315 DirectiveKindMap[".align"] = DK_ALIGN;
4316 DirectiveKindMap[".align32"] = DK_ALIGN32;
4317 DirectiveKindMap[".balign"] = DK_BALIGN;
4318 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4319 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4320 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4321 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4322 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4323 DirectiveKindMap[".org"] = DK_ORG;
4324 DirectiveKindMap[".fill"] = DK_FILL;
4325 DirectiveKindMap[".zero"] = DK_ZERO;
4326 DirectiveKindMap[".extern"] = DK_EXTERN;
4327 DirectiveKindMap[".globl"] = DK_GLOBL;
4328 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004329 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4330 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4331 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4332 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4333 DirectiveKindMap[".reference"] = DK_REFERENCE;
4334 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4335 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4336 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4337 DirectiveKindMap[".comm"] = DK_COMM;
4338 DirectiveKindMap[".common"] = DK_COMMON;
4339 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4340 DirectiveKindMap[".abort"] = DK_ABORT;
4341 DirectiveKindMap[".include"] = DK_INCLUDE;
4342 DirectiveKindMap[".incbin"] = DK_INCBIN;
4343 DirectiveKindMap[".code16"] = DK_CODE16;
4344 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4345 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004346 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004347 DirectiveKindMap[".irp"] = DK_IRP;
4348 DirectiveKindMap[".irpc"] = DK_IRPC;
4349 DirectiveKindMap[".endr"] = DK_ENDR;
4350 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4351 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4352 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4353 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004354 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4355 DirectiveKindMap[".ifge"] = DK_IFGE;
4356 DirectiveKindMap[".ifgt"] = DK_IFGT;
4357 DirectiveKindMap[".ifle"] = DK_IFLE;
4358 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004359 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004360 DirectiveKindMap[".ifb"] = DK_IFB;
4361 DirectiveKindMap[".ifnb"] = DK_IFNB;
4362 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004363 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004364 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004365 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004366 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4367 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4368 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4369 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4370 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004371 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004372 DirectiveKindMap[".endif"] = DK_ENDIF;
4373 DirectiveKindMap[".skip"] = DK_SKIP;
4374 DirectiveKindMap[".space"] = DK_SPACE;
4375 DirectiveKindMap[".file"] = DK_FILE;
4376 DirectiveKindMap[".line"] = DK_LINE;
4377 DirectiveKindMap[".loc"] = DK_LOC;
4378 DirectiveKindMap[".stabs"] = DK_STABS;
4379 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4380 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4381 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4382 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4383 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4384 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4385 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4386 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4387 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4388 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4389 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4390 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4391 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4392 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4393 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4394 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4395 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4396 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4397 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4398 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4399 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004400 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004401 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4402 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4403 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004404 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004405 DirectiveKindMap[".endm"] = DK_ENDM;
4406 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4407 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004408 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004409 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004410 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004411 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004412}
4413
Jim Grosbach4b905842013-09-20 23:08:21 +00004414MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004415 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004416
Rafael Espindola34b9c512012-06-03 23:57:14 +00004417 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004418 for (;;) {
4419 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004420 if (getLexer().is(AsmToken::Eof)) {
4421 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004422 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004423 }
4424
Rafael Espindola34b9c512012-06-03 23:57:14 +00004425 if (Lexer.is(AsmToken::Identifier) &&
4426 (getTok().getIdentifier() == ".rept")) {
4427 ++NestLevel;
4428 }
4429
4430 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004431 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004432 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004433 EndToken = getTok();
4434 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004435 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4436 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004437 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004438 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004439 break;
4440 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004441 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004442 }
4443
Rafael Espindola34b9c512012-06-03 23:57:14 +00004444 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004445 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004446 }
4447
4448 const char *BodyStart = StartToken.getLoc().getPointer();
4449 const char *BodyEnd = EndToken.getLoc().getPointer();
4450 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4451
Rafael Espindola34b9c512012-06-03 23:57:14 +00004452 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004453 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004454 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004455}
4456
Jim Grosbach4b905842013-09-20 23:08:21 +00004457void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004458 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004459 OS << ".endr\n";
4460
Rafael Espindola3560ff22014-08-27 20:03:13 +00004461 std::unique_ptr<MemoryBuffer> Instantiation =
4462 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004463
Rafael Espindola34b9c512012-06-03 23:57:14 +00004464 // Create the macro instantiation object and add to the current macro
4465 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004466 MacroInstantiation *MI = new MacroInstantiation(
4467 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004468 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004469
Rafael Espindola34b9c512012-06-03 23:57:14 +00004470 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004471 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004472 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004473 Lex();
4474}
4475
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004476/// parseDirectiveRept
4477/// ::= .rep | .rept count
4478bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004479 const MCExpr *CountExpr;
4480 SMLoc CountLoc = getTok().getLoc();
4481 if (parseExpression(CountExpr))
4482 return true;
4483
Rafael Espindola34b9c512012-06-03 23:57:14 +00004484 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004485 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004486 eatToEndOfStatement();
4487 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4488 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004489
4490 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004491 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004492
4493 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004494 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004495
4496 // Eat the end of statement.
4497 Lex();
4498
4499 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004500 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004501 if (!M)
4502 return true;
4503
4504 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4505 // to hold the macro body with substitutions.
4506 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004507 raw_svector_ostream OS(Buf);
4508 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004509 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4510 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004511 return true;
4512 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004514
4515 return false;
4516}
4517
Jim Grosbach4b905842013-09-20 23:08:21 +00004518/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004519/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004520bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004521 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004522
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004523 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004524 return TokError("expected identifier in '.irp' directive");
4525
Rafael Espindola768b41c2012-06-15 14:02:34 +00004526 if (Lexer.isNot(AsmToken::Comma))
4527 return TokError("expected comma in '.irp' directive");
4528
4529 Lex();
4530
Eli Bendersky38274122013-01-14 23:22:36 +00004531 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004532 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004533 return true;
4534
4535 // Eat the end of statement.
4536 Lex();
4537
4538 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004539 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004540 if (!M)
4541 return true;
4542
4543 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4544 // to hold the macro body with substitutions.
4545 SmallString<256> Buf;
4546 raw_svector_ostream OS(Buf);
4547
Craig Topper84008482015-10-10 05:38:14 +00004548 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004549 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4550 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004551 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004552 return true;
4553 }
4554
Jim Grosbach4b905842013-09-20 23:08:21 +00004555 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004556
4557 return false;
4558}
4559
Jim Grosbach4b905842013-09-20 23:08:21 +00004560/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004561/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004562bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004563 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004564
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004565 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004566 return TokError("expected identifier in '.irpc' directive");
4567
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004568 if (Lexer.isNot(AsmToken::Comma))
4569 return TokError("expected comma in '.irpc' directive");
4570
4571 Lex();
4572
Eli Bendersky38274122013-01-14 23:22:36 +00004573 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004574 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004575 return true;
4576
4577 if (A.size() != 1 || A.front().size() != 1)
4578 return TokError("unexpected token in '.irpc' directive");
4579
4580 // Eat the end of statement.
4581 Lex();
4582
4583 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004584 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004585 if (!M)
4586 return true;
4587
4588 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4589 // to hold the macro body with substitutions.
4590 SmallString<256> Buf;
4591 raw_svector_ostream OS(Buf);
4592
4593 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004594 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004595 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004596 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004597
Toma Tabacu217116e2015-04-27 10:50:29 +00004598 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4599 // This is undocumented, but GAS seems to support it.
4600 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004601 return true;
4602 }
4603
Jim Grosbach4b905842013-09-20 23:08:21 +00004604 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004605
4606 return false;
4607}
4608
Jim Grosbach4b905842013-09-20 23:08:21 +00004609bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004610 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004611 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004612
4613 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004614 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004615 assert(getLexer().is(AsmToken::EndOfStatement));
4616
Jim Grosbach4b905842013-09-20 23:08:21 +00004617 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004618 return false;
4619}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004620
Jim Grosbach4b905842013-09-20 23:08:21 +00004621bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004622 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004623 const MCExpr *Value;
4624 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004625 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004626 return true;
4627 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4628 if (!MCE)
4629 return Error(ExprLoc, "unexpected expression in _emit");
4630 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004631 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004632 return Error(ExprLoc, "literal value out of range for directive");
4633
Craig Topper7d5b2312015-10-10 05:25:02 +00004634 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004635 return false;
4636}
4637
Jim Grosbach4b905842013-09-20 23:08:21 +00004638bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004639 const MCExpr *Value;
4640 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004641 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004642 return true;
4643 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4644 if (!MCE)
4645 return Error(ExprLoc, "unexpected expression in align");
4646 uint64_t IntValue = MCE->getValue();
4647 if (!isPowerOf2_64(IntValue))
4648 return Error(ExprLoc, "literal value not a power of two greater then zero");
4649
Craig Topper7d5b2312015-10-10 05:25:02 +00004650 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004651 return false;
4652}
4653
Chad Rosierf43fcf52013-02-13 21:27:17 +00004654// We are comparing pointers, but the pointers are relative to a single string.
4655// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004656static int rewritesSort(const AsmRewrite *AsmRewriteA,
4657 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004658 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4659 return -1;
4660 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4661 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004662
Chad Rosierfce4fab2013-04-08 17:43:47 +00004663 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4664 // rewrite to the same location. Make sure the SizeDirective rewrite is
4665 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4666 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004667 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4668 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004669 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004670
Jim Grosbach4b905842013-09-20 23:08:21 +00004671 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4672 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004673 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004674 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004675}
4676
Jim Grosbach4b905842013-09-20 23:08:21 +00004677bool AsmParser::parseMSInlineAsm(
4678 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4679 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4680 SmallVectorImpl<std::string> &Constraints,
4681 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4682 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004683 SmallVector<void *, 4> InputDecls;
4684 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004685 SmallVector<bool, 4> InputDeclsAddressOf;
4686 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004687 SmallVector<std::string, 4> InputConstraints;
4688 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004689 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004690
Benjamin Kramer1a136112013-02-15 20:37:21 +00004691 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004692
4693 // Prime the lexer.
4694 Lex();
4695
4696 // While we have input, parse each statement.
4697 unsigned InputIdx = 0;
4698 unsigned OutputIdx = 0;
4699 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004700 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004701 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004702 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004703
Chad Rosier149e8e02012-12-12 22:45:52 +00004704 if (Info.ParseError)
4705 return true;
4706
Benjamin Kramer1a136112013-02-15 20:37:21 +00004707 if (Info.Opcode == ~0U)
4708 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004709
Benjamin Kramer1a136112013-02-15 20:37:21 +00004710 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004711
Benjamin Kramer1a136112013-02-15 20:37:21 +00004712 // Build the list of clobbers, outputs and inputs.
4713 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004714 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004715
Benjamin Kramer1a136112013-02-15 20:37:21 +00004716 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004717 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004718 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004719
Benjamin Kramer1a136112013-02-15 20:37:21 +00004720 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004721 if (Operand.isReg() && !Operand.needAddressOf() &&
4722 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004723 unsigned NumDefs = Desc.getNumDefs();
4724 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004725 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4726 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004727 continue;
4728 }
4729
4730 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004731 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004732 if (SymName.empty())
4733 continue;
4734
David Blaikie960ea3f2014-06-08 16:18:35 +00004735 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004736 if (!OpDecl)
4737 continue;
4738
4739 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004740 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004741 if (isOutput) {
4742 ++InputIdx;
4743 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004744 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004745 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004746 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004747 } else {
4748 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004749 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4750 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004751 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00004752 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004753 }
Reid Kleckneree088972013-12-10 18:27:32 +00004754
4755 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00004756 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
4757 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00004758 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004759 }
4760
4761 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004762 NumOutputs = OutputDecls.size();
4763 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004764
4765 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004766 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4767 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4768 ClobberRegs.end());
4769 Clobbers.assign(ClobberRegs.size(), std::string());
4770 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4771 raw_string_ostream OS(Clobbers[I]);
4772 IP->printRegName(OS, ClobberRegs[I]);
4773 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004774
4775 // Merge the various outputs and inputs. Output are expected first.
4776 if (NumOutputs || NumInputs) {
4777 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004778 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004779 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004780 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004781 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004782 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004783 }
4784 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004785 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004786 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004787 }
4788 }
4789
4790 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004791 std::string AsmStringIR;
4792 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004793 StringRef ASMString =
4794 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4795 const char *AsmStart = ASMString.begin();
4796 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004797 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004798 for (const AsmRewrite &AR : AsmStrRewrites) {
4799 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004800 if (Kind == AOK_Delete)
4801 continue;
4802
David Majnemer8114c1a2014-06-23 02:17:16 +00004803 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004804 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004805
Chad Rosier120eefd2013-03-19 17:32:17 +00004806 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004807 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004808 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004809
Chad Rosier37e755c2012-10-23 17:43:43 +00004810 // Skip the original expression.
4811 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004812 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004813 continue;
4814 }
4815
Chad Rosierff10ed12013-04-12 16:26:42 +00004816 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004817 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004818 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004819 default:
4820 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004821 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004822 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004823 break;
4824 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004825 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004826 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004827 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004828 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004829 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004830 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004831 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004832 break;
4833 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004834 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004835 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004836 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004837 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004838 default: break;
4839 case 8: OS << "byte ptr "; break;
4840 case 16: OS << "word ptr "; break;
4841 case 32: OS << "dword ptr "; break;
4842 case 64: OS << "qword ptr "; break;
4843 case 80: OS << "xword ptr "; break;
4844 case 128: OS << "xmmword ptr "; break;
4845 case 256: OS << "ymmword ptr "; break;
4846 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004847 break;
4848 case AOK_Emit:
4849 OS << ".byte";
4850 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004851 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004852 // MS alignment directives are measured in bytes. If the native assembler
4853 // measures alignment in bytes, we can pass it straight through.
4854 OS << ".align";
4855 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
4856 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004857
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004858 // Alignment is in log2 form, so print that instead and skip the original
4859 // immediate.
4860 unsigned Val = AR.Val;
4861 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004862 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004863 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4864 break;
4865 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004866 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004867 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004868 OS.flush();
4869 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004870 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004871 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004872 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004873 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004874
Chad Rosier8bce6642012-10-18 15:49:34 +00004875 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004876 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004877 }
4878
4879 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004880 if (AsmStart != AsmEnd)
4881 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004882
4883 AsmString = OS.str();
4884 return false;
4885}
4886
Pete Cooper80d21cb2015-06-22 19:35:57 +00004887namespace llvm {
4888namespace MCParserUtils {
4889
4890/// Returns whether the given symbol is used anywhere in the given expression,
4891/// or subexpressions.
4892static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
4893 switch (Value->getKind()) {
4894 case MCExpr::Binary: {
4895 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
4896 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
4897 isSymbolUsedInExpression(Sym, BE->getRHS());
4898 }
4899 case MCExpr::Target:
4900 case MCExpr::Constant:
4901 return false;
4902 case MCExpr::SymbolRef: {
4903 const MCSymbol &S =
4904 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
4905 if (S.isVariable())
4906 return isSymbolUsedInExpression(Sym, S.getVariableValue());
4907 return &S == Sym;
4908 }
4909 case MCExpr::Unary:
4910 return isSymbolUsedInExpression(
4911 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
4912 }
4913
4914 llvm_unreachable("Unknown expr kind!");
4915}
4916
4917bool parseAssignmentExpression(StringRef Name, bool allow_redef,
4918 MCAsmParser &Parser, MCSymbol *&Sym,
4919 const MCExpr *&Value) {
4920 MCAsmLexer &Lexer = Parser.getLexer();
4921
4922 // FIXME: Use better location, we should use proper tokens.
4923 SMLoc EqualLoc = Lexer.getLoc();
4924
4925 if (Parser.parseExpression(Value)) {
4926 Parser.TokError("missing expression");
4927 Parser.eatToEndOfStatement();
4928 return true;
4929 }
4930
4931 // Note: we don't count b as used in "a = b". This is to allow
4932 // a = b
4933 // b = c
4934
4935 if (Lexer.isNot(AsmToken::EndOfStatement))
4936 return Parser.TokError("unexpected token in assignment");
4937
4938 // Eat the end of statement marker.
4939 Parser.Lex();
4940
4941 // Validate that the LHS is allowed to be a variable (either it has not been
4942 // used as a symbol, or it is an absolute symbol).
4943 Sym = Parser.getContext().lookupSymbol(Name);
4944 if (Sym) {
4945 // Diagnose assignment to a label.
4946 //
4947 // FIXME: Diagnostics. Note the location of the definition as a label.
4948 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
4949 if (isSymbolUsedInExpression(Sym, Value))
4950 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00004951 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
4952 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00004953 ; // Allow redefinitions of undefined symbols only used in directives.
4954 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
4955 ; // Allow redefinitions of variables that haven't yet been used.
4956 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
4957 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
4958 else if (!Sym->isVariable())
4959 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
4960 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
4961 return Parser.Error(EqualLoc,
4962 "invalid reassignment of non-absolute variable '" +
4963 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00004964 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00004965 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00004966 return false;
4967 } else
4968 Sym = Parser.getContext().getOrCreateSymbol(Name);
4969
4970 Sym->setRedefinable(allow_redef);
4971
4972 return false;
4973}
4974
4975} // namespace MCParserUtils
4976} // namespace llvm
4977
Daniel Dunbar01e36072010-07-17 02:26:10 +00004978/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004979MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4980 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004981 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004982}