blob: 4865807045d426090f89fd33eb7dc0b922764eb6 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
Pete Cooper80d21cb2015-06-22 19:35:57 +000029#include "llvm/MC/MCParser/MCAsmParserUtils.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000030#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000031#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Evan Cheng76792992011-07-20 05:58:47 +000032#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000033#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000034#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000035#include "llvm/MC/MCSymbol.h"
Daniel Sanders9f6ad492015-11-12 13:33:00 +000036#include "llvm/MC/MCValue.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000037#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000038#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000039#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000040#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000041#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000042#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000043#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000044#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Eric Christophera7c32732012-12-18 00:30:54 +000048MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000049
Daniel Dunbar86033402010-07-12 17:54:38 +000050namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000051/// \brief Helper types for tracking macro definitions.
52typedef std::vector<AsmToken> MCAsmMacroArgument;
53typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000054
55struct MCAsmMacroParameter {
56 StringRef Name;
57 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000058 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000059 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000062};
63
Eli Benderskya313ae62013-01-16 18:56:50 +000064typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
65
66struct MCAsmMacro {
67 StringRef Name;
68 StringRef Body;
69 MCAsmMacroParameters Parameters;
70
71public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000072 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
73 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000074};
75
Daniel Dunbar43235712010-07-18 18:54:11 +000076/// \brief Helper class for storing information about an active macro
77/// instantiation.
78struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000079 /// The location of the instantiation.
80 SMLoc InstantiationLoc;
81
Daniel Dunbar40f1d852012-12-01 01:38:48 +000082 /// The buffer where parsing should resume upon instantiation completion.
83 int ExitBuffer;
84
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location where parsing should resume upon instantiation completion.
86 SMLoc ExitLoc;
87
Nico Weber155dccd12014-07-24 17:08:39 +000088 /// The depth of TheCondStack at the start of the instantiation.
89 size_t CondStackDepth;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000092 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000093};
94
Eli Friedman0f4871d2012-10-22 23:58:19 +000095struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000096 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000097 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000098
Jim Grosbach4b905842013-09-20 23:08:21 +000099 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100 unsigned Opcode;
101
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000103 bool ParseError;
104
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVectorImpl<AsmRewrite> *AsmRewrites;
106
Craig Topper353eda42014-04-24 06:44:33 +0000107 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110};
111
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000112/// \brief The concrete assembly parser instance.
113class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000114 AsmParser(const AsmParser &) = delete;
115 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000116private:
117 AsmLexer Lexer;
118 MCContext &Ctx;
119 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000120 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000122 SourceMgr::DiagHandlerTy SavedDiagHandler;
123 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000124 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000125
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126 /// This is the current buffer index we're lexing from as managed by the
127 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000128 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129
130 AsmCond TheCondState;
131 std::vector<AsmCond> TheCondStack;
132
Jim Grosbach4b905842013-09-20 23:08:21 +0000133 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000134 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000135 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000136 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000137
Jim Grosbach4b905842013-09-20 23:08:21 +0000138 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000139 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000142 std::vector<MacroInstantiation*> ActiveMacros;
143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000145 std::deque<MCAsmMacro> MacroLikeBodies;
146
Daniel Dunbar828984f2010-07-18 18:38:02 +0000147 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000148 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149
Toma Tabacu217116e2015-04-27 10:50:29 +0000150 /// \brief Keeps track of how many .macro's have been instantiated.
151 unsigned NumOfMacroInstantiations;
152
Daniel Dunbar43325c42010-09-09 22:42:56 +0000153 /// Flag tracking whether any errors have been encountered.
154 unsigned HadError : 1;
155
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000156 /// The values from the last parsed cpp hash file line comment if any.
Tim Northoverc0bef992016-04-13 19:46:54 +0000157 struct CppHashInfoTy {
158 StringRef Filename;
Andrew Kaylorca196472016-04-21 20:09:35 +0000159 int64_t LineNumber = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000160 SMLoc Loc;
Andrew Kaylorca196472016-04-21 20:09:35 +0000161 unsigned Buf = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000162 };
163 CppHashInfoTy CppHashInfo;
164
165 /// \brief List of forward directional labels for diagnosis at the end.
166 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
167
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000168 /// When generating dwarf for assembly source files we need to calculate the
169 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000170 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000171 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
172 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000173 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000174 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000175
Devang Patela173ee52012-01-31 18:14:05 +0000176 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
177 unsigned AssemblerDialect;
178
Jim Grosbach4b905842013-09-20 23:08:21 +0000179 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000180 bool IsDarwin;
181
Jim Grosbach4b905842013-09-20 23:08:21 +0000182 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000183 bool ParsingInlineAsm;
184
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000186 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000188 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191
Craig Topper59be68f2014-03-08 07:14:16 +0000192 void addDirectiveHandler(StringRef Directive,
193 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000194 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000195 }
196
Toma Tabacu11e14a92015-04-21 11:50:52 +0000197 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
198 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
199 }
200
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000201public:
202 /// @name MCAsmParser Interface
203 /// {
204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 SourceMgr &getSourceManager() override { return SrcMgr; }
206 MCAsmLexer &getLexer() override { return Lexer; }
207 MCContext &getContext() override { return Ctx; }
208 MCStreamer &getStreamer() override { return Out; }
209 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000211 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000212 else
213 return AssemblerDialect;
214 }
Craig Topper59be68f2014-03-08 07:14:16 +0000215 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000216 AssemblerDialect = i;
217 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper59be68f2014-03-08 07:14:16 +0000219 void Note(SMLoc L, const Twine &Msg,
220 ArrayRef<SMRange> Ranges = None) override;
221 bool Warning(SMLoc L, const Twine &Msg,
222 ArrayRef<SMRange> Ranges = None) override;
223 bool Error(SMLoc L, const Twine &Msg,
224 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000225
Craig Topper59be68f2014-03-08 07:14:16 +0000226 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000227
Craig Topper59be68f2014-03-08 07:14:16 +0000228 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
229 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000230
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000231 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000232 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000233 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000234 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000235 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000236 const MCInstrInfo *MII, const MCInstPrinter *IP,
237 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000238
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000239 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
241 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000243 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
244 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000245 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000246
Jim Grosbach4b905842013-09-20 23:08:21 +0000247 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000248 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000249 bool parseIdentifier(StringRef &Res) override;
250 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000251
Craig Topper59be68f2014-03-08 07:14:16 +0000252 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000253 /// }
254
255private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000256
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000257 bool parseStatement(ParseStatementInfo &Info,
258 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000259 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Craig Topper3c76c522015-09-20 23:35:59 +0000260 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000261
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000263 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000264 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000265 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000266 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000267 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000268
Eli Benderskya313ae62013-01-16 18:56:50 +0000269 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
272 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Lookup a previously defined macro.
276 /// \param Name Macro name.
277 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
280 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000281 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
283 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000289 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000290 ///
291 /// \param M The macro.
292 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
295 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
David Majnemer91fc4c22014-01-29 18:57:46 +0000298 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000299 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000300
301 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000303
Jim Grosbach4b905842013-09-20 23:08:21 +0000304 void printMacroInstantiations();
305 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000306 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000307 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000308 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000309 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000310
Jim Grosbach4b905842013-09-20 23:08:21 +0000311 /// \brief Enter the specified file. This returns true on failure.
312 bool enterIncludeFile(const std::string &Filename);
313
314 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000315 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000316 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000317
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000318 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000319 /// current token is not set; clients should ensure Lex() is called
320 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000321 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000322 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000323 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000324 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000325
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000326 /// \brief Parse up to the end of statement and a return the contents from the
327 /// current token until the end of the statement; the current token on exit
328 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000329 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000330
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000331 /// \brief Parse until the end of a statement or a comma is encountered,
332 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000334
Jim Grosbach4b905842013-09-20 23:08:21 +0000335 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000336 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000337
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000338 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
339 MCBinaryExpr::Opcode &Kind);
340
Jim Grosbach4b905842013-09-20 23:08:21 +0000341 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
342 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
343 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000344
Jim Grosbach4b905842013-09-20 23:08:21 +0000345 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000346
Eli Bendersky17233942013-01-15 22:59:42 +0000347 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000348 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_NO_DIRECTIVE, // Placeholder
350 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000351 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000352 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
353 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000354 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000355 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000356 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000357 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000358 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000359 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
360 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000361 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000362 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
363 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000364 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000365 DK_CV_FILE, DK_CV_LOC, DK_CV_LINETABLE, DK_CV_INLINE_LINETABLE,
David Majnemer408b5e62016-02-05 01:55:49 +0000366 DK_CV_DEF_RANGE, DK_CV_STRINGTABLE, DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000367 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
368 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
369 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
370 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
371 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000372 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000373 DK_MACROS_ON, DK_MACROS_OFF,
374 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000375 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000376 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000377 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000378 };
379
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000381 /// directives parsed by this class.
382 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000383
384 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000386 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000387 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000388 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
390 bool parseDirectiveFill(); // ".fill"
391 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000392 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000393 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
394 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000395 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000396 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000397
Eli Bendersky17233942013-01-15 22:59:42 +0000398 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000399 bool parseDirectiveFile(SMLoc DirectiveLoc);
400 bool parseDirectiveLine();
401 bool parseDirectiveLoc();
402 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000403
David Majnemer408b5e62016-02-05 01:55:49 +0000404 // ".cv_file", ".cv_loc", ".cv_linetable", "cv_inline_linetable",
405 // ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000406 bool parseDirectiveCVFile();
407 bool parseDirectiveCVLoc();
408 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000409 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000410 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000411 bool parseDirectiveCVStringTable();
412 bool parseDirectiveCVFileChecksums();
413
Eli Bendersky17233942013-01-15 22:59:42 +0000414 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000416 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveCFISections();
418 bool parseDirectiveCFIStartProc();
419 bool parseDirectiveCFIEndProc();
420 bool parseDirectiveCFIDefCfaOffset();
421 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
422 bool parseDirectiveCFIAdjustCfaOffset();
423 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
424 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
425 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
426 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
427 bool parseDirectiveCFIRememberState();
428 bool parseDirectiveCFIRestoreState();
429 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
430 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
431 bool parseDirectiveCFIEscape();
432 bool parseDirectiveCFISignalFrame();
433 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000434
435 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000437 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveEndMacro(StringRef Directive);
439 bool parseDirectiveMacro(SMLoc DirectiveLoc);
440 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000441
Eli Benderskyf483ff92012-12-20 19:05:53 +0000442 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000443 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000444 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000446 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000448
Eli Bendersky17233942013-01-15 22:59:42 +0000449 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000451
452 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000454
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000456 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000458
Jim Grosbach4b905842013-09-20 23:08:21 +0000459 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000460
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 bool parseDirectiveAbort(); // ".abort"
462 bool parseDirectiveInclude(); // ".include"
463 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000464
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000465 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
466 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000467 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000468 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000469 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000471 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
472 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000473 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000474 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
475 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
476 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
477 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000478 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000479
Jim Grosbach4b905842013-09-20 23:08:21 +0000480 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000481 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000482
Rafael Espindola34b9c512012-06-03 23:57:14 +0000483 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000484 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
485 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000486 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000487 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000488 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
489 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
490 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000491
Chad Rosierc7f552c2013-02-12 21:33:51 +0000492 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000493 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000494 size_t Len);
495
496 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000497 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000498
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000499 // "end"
500 bool parseDirectiveEnd(SMLoc DirectiveLoc);
501
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000502 // ".err" or ".error"
503 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000504
Nico Weber404012b2014-07-24 16:26:06 +0000505 // ".warning"
506 bool parseDirectiveWarning(SMLoc DirectiveLoc);
507
Eli Bendersky17233942013-01-15 22:59:42 +0000508 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000509};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000510}
Daniel Dunbar86033402010-07-12 17:54:38 +0000511
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000512namespace llvm {
513
514extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000515extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000516extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000517
518}
519
Chris Lattnerc35681b2010-01-19 19:46:13 +0000520enum { DEFAULT_ADDRSPACE = 0 };
521
David Blaikie9f380a32015-03-16 18:06:57 +0000522AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
523 const MCAsmInfo &MAI)
524 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
525 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Tim Northoverc0bef992016-04-13 19:46:54 +0000526 MacrosEnabledFlag(true), HadError(false), CppHashInfo(),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000527 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000528 // Save the old handler.
529 SavedDiagHandler = SrcMgr.getDiagHandler();
530 SavedDiagContext = SrcMgr.getDiagContext();
531 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000532 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000533 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000534
Daniel Dunbarc5011082010-07-12 18:12:02 +0000535 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000536 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
537 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000538 PlatformParser.reset(createCOFFAsmParser());
539 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000540 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000541 PlatformParser.reset(createDarwinAsmParser());
542 IsDarwin = true;
543 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000544 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000545 PlatformParser.reset(createELFAsmParser());
546 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000547 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000548
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000549 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000550 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000551
552 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000553}
554
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000555AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000556 assert((HadError || ActiveMacros.empty()) &&
557 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000558}
559
Jim Grosbach4b905842013-09-20 23:08:21 +0000560void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000561 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 for (std::vector<MacroInstantiation *>::const_reverse_iterator
563 it = ActiveMacros.rbegin(),
564 ie = ActiveMacros.rend();
565 it != ie; ++it)
566 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000567 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000568}
569
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000570void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
571 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
572 printMacroInstantiations();
573}
574
Chris Lattnera3a06812011-10-16 04:47:35 +0000575bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000576 if(getTargetParser().getTargetOptions().MCNoWarn)
577 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000578 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000579 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000580 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
581 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000582 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000583}
584
Chris Lattnera3a06812011-10-16 04:47:35 +0000585bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000586 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000587 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
588 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000589 return true;
590}
591
Jim Grosbach4b905842013-09-20 23:08:21 +0000592bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000593 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000594 unsigned NewBuf =
595 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
596 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000598
Sean Callanan7a77eae2010-01-21 00:19:58 +0000599 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000600 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 return false;
602}
Daniel Dunbar43235712010-07-18 18:54:11 +0000603
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000604/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000605/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000606/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000607bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000608 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000609 unsigned NewBuf =
610 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
611 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000612 return true;
613
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000614 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000615 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000616 return false;
617}
618
Alp Tokera55b95b2014-07-06 10:33:31 +0000619void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
620 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000621 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
622 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000623}
624
Sean Callanan7a77eae2010-01-21 00:19:58 +0000625const AsmToken &AsmParser::Lex() {
Nirav Dave1180e6892016-06-02 17:15:05 +0000626 if (Lexer.getTok().is(AsmToken::Error))
627 Error(Lexer.getErrLoc(), Lexer.getErr());
628
Nirav Dave53a72f42016-07-11 12:42:14 +0000629 // if it's a end of statement with a comment in it
630 if (getTok().is(AsmToken::EndOfStatement)) {
631 // if this is a line comment output it.
632 if (getTok().getString().front() != '\n' &&
633 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
634 Out.addExplicitComment(Twine(getTok().getString()));
635 }
636
Sean Callanan7a77eae2010-01-21 00:19:58 +0000637 const AsmToken *tok = &Lexer.Lex();
Nirav Dave53a72f42016-07-11 12:42:14 +0000638
639 // Parse comments here to be deferred until end of next statement.
Nirav Davefd910412016-06-17 16:06:17 +0000640 while (tok->is(AsmToken::Comment)) {
Nirav Dave53a72f42016-07-11 12:42:14 +0000641 if (MAI.preserveAsmComments())
642 Out.addExplicitComment(Twine(tok->getString()));
Nirav Davefd910412016-06-17 16:06:17 +0000643 tok = &Lexer.Lex();
644 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000645
Sean Callanan7a77eae2010-01-21 00:19:58 +0000646 if (tok->is(AsmToken::Eof)) {
647 // If this is the end of an included file, pop the parent file off the
648 // include stack.
649 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
650 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000651 jumpToLoc(ParentIncludeLoc);
Nirav Davefd910412016-06-17 16:06:17 +0000652 return Lex();
Sean Callanan7a77eae2010-01-21 00:19:58 +0000653 }
654 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000655
Michael J. Spencer530ce852010-10-09 11:00:50 +0000656
Sean Callanan7a77eae2010-01-21 00:19:58 +0000657 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000658}
659
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000660bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000661 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000662 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000663 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000664
Chris Lattner36e02122009-06-21 20:54:55 +0000665 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000666 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000667
668 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000669 AsmCond StartingCondState = TheCondState;
670
Kevin Enderby6469fc22011-11-01 22:27:22 +0000671 // If we are generating dwarf for assembly source files save the initial text
672 // section and generate a .file directive.
673 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000674 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000675 if (!Sec->getBeginSymbol()) {
676 MCSymbol *SectionStartSym = getContext().createTempSymbol();
677 getStreamer().EmitLabel(SectionStartSym);
678 Sec->setBeginSymbol(SectionStartSym);
679 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000680 bool InsertResult = getContext().addGenDwarfSection(Sec);
681 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000682 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000683 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
684 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000685 }
686
Chris Lattner73f36112009-07-02 21:53:43 +0000687 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000688 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000689 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000690 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000691 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000692
Nirav Dave1180e6892016-06-02 17:15:05 +0000693 // If we've failed, but on a Error Token, but did not consume it in
694 // favor of a better message, emit it now.
695 if (Lexer.getTok().is(AsmToken::Error)) {
696 Lex();
697 }
698
Daniel Dunbar43325c42010-09-09 22:42:56 +0000699 // We had an error, validate that one was emitted and recover by skipping to
700 // the next line.
701 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000703 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000704
705 if (TheCondState.TheCond != StartingCondState.TheCond ||
706 TheCondState.Ignore != StartingCondState.Ignore)
707 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000708
709 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000710 const auto &LineTables = getContext().getMCDwarfLineTables();
711 if (!LineTables.empty()) {
712 unsigned Index = 0;
713 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
714 if (File.Name.empty() && Index != 0)
715 TokError("unassigned file number: " + Twine(Index) +
716 " for .file directives");
717 ++Index;
718 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000719 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000720
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000721 // Check to see that all assembler local symbols were actually defined.
722 // Targets that don't do subsections via symbols may not want this, though,
723 // so conservatively exclude them. Only do this if we're finalizing, though,
724 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000725 if (!NoFinalize) {
726 if (MAI.hasSubsectionsViaSymbols()) {
727 for (const auto &TableEntry : getContext().getSymbols()) {
728 MCSymbol *Sym = TableEntry.getValue();
729 // Variable symbols may not be marked as defined, so check those
730 // explicitly. If we know it's a variable, we have a definition for
731 // the purposes of this check.
732 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
733 // FIXME: We would really like to refer back to where the symbol was
734 // first referenced for a source location. We need to add something
735 // to track that. Currently, we just point to the end of the file.
736 HadError |=
Nirav Davefd910412016-06-17 16:06:17 +0000737 Error(getTok().getLoc(), "assembler local symbol '" +
738 Sym->getName() + "' not defined");
Tim Northover6b3169b2016-04-11 19:50:46 +0000739 }
740 }
741
742 // Temporary symbols like the ones for directional jumps don't go in the
743 // symbol table. They also need to be diagnosed in all (final) cases.
Tim Northoverc0bef992016-04-13 19:46:54 +0000744 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
745 if (std::get<2>(LocSym)->isUndefined()) {
746 // Reset the state of any "# line file" directives we've seen to the
747 // context as it was at the diagnostic site.
748 CppHashInfo = std::get<1>(LocSym);
749 HadError |= Error(std::get<0>(LocSym), "directional label undefined");
750 }
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000751 }
752 }
753
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000754 // Finalize the output stream if there are no errors and if the client wants
755 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000756 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000757 Out.Finish();
758
Oliver Stannard07b43d32015-11-17 09:58:07 +0000759 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000760}
761
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000762void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000763 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000764 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000765 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000766 }
767}
768
Jim Grosbach4b905842013-09-20 23:08:21 +0000769/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000770void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000771 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Dave1180e6892016-06-02 17:15:05 +0000772 Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000773
Chris Lattnere5074c42009-06-22 01:29:09 +0000774 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000775 if (Lexer.is(AsmToken::EndOfStatement))
Nirav Dave1180e6892016-06-02 17:15:05 +0000776 Lexer.Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000777}
778
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000779StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000780 const char *Start = getTok().getLoc().getPointer();
781
Jim Grosbach4b905842013-09-20 23:08:21 +0000782 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000783 Lexer.Lex();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000784
785 const char *End = getTok().getLoc().getPointer();
786 return StringRef(Start, End - Start);
787}
Chris Lattner78db3622009-06-22 05:51:26 +0000788
Jim Grosbach4b905842013-09-20 23:08:21 +0000789StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000790 const char *Start = getTok().getLoc().getPointer();
791
792 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000793 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000794 Lexer.Lex();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000795
796 const char *End = getTok().getLoc().getPointer();
797 return StringRef(Start, End - Start);
798}
799
Jim Grosbach4b905842013-09-20 23:08:21 +0000800/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000801/// NOTE: This assumes the leading '(' has already been consumed.
802///
803/// parenexpr ::= expr)
804///
Jim Grosbach4b905842013-09-20 23:08:21 +0000805bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
806 if (parseExpression(Res))
807 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000808 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000809 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000810 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000811 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000812 return false;
813}
Chris Lattner78db3622009-06-22 05:51:26 +0000814
Jim Grosbach4b905842013-09-20 23:08:21 +0000815/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000816/// NOTE: This assumes the leading '[' has already been consumed.
817///
818/// bracketexpr ::= expr]
819///
Jim Grosbach4b905842013-09-20 23:08:21 +0000820bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
821 if (parseExpression(Res))
822 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000823 if (Lexer.isNot(AsmToken::RBrac))
824 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000825 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000826 Lex();
827 return false;
828}
829
Jim Grosbach4b905842013-09-20 23:08:21 +0000830/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000831/// primaryexpr ::= (parenexpr
832/// primaryexpr ::= symbol
833/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000834/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000835/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000836bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000837 SMLoc FirstTokenLoc = getLexer().getLoc();
838 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
839 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000840 default:
841 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000842 // If we have an error assume that we've already handled it.
843 case AsmToken::Error:
844 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000845 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000846 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000847 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000848 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000849 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000850 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000851 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000852 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000853 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000854 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000855 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000856 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000857 if (FirstTokenKind == AsmToken::Dollar) {
858 if (Lexer.getMAI().getDollarIsPC()) {
859 // This is a '$' reference, which references the current PC. Emit a
860 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000861 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000862 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000863 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000864 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000865 EndLoc = FirstTokenLoc;
866 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000867 }
868 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000869 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000870 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000871 // Parse symbol variant
872 std::pair<StringRef, StringRef> Split;
873 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000874 if (FirstTokenKind == AsmToken::String) {
875 if (Lexer.is(AsmToken::At)) {
Nirav Davefd910412016-06-17 16:06:17 +0000876 Lex(); // eat @
David Majnemer6a5b8122014-06-19 01:25:43 +0000877 SMLoc AtLoc = getLexer().getLoc();
878 StringRef VName;
879 if (parseIdentifier(VName))
880 return Error(AtLoc, "expected symbol variant after '@'");
881
882 Split = std::make_pair(Identifier, VName);
883 }
884 } else {
885 Split = Identifier.split('@');
886 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000887 } else if (Lexer.is(AsmToken::LParen)) {
Nirav Davefd910412016-06-17 16:06:17 +0000888 Lex(); // eat '('.
David Peixotto8ad70b32013-12-04 22:43:20 +0000889 StringRef VName;
890 parseIdentifier(VName);
891 if (Lexer.isNot(AsmToken::RParen)) {
892 return Error(Lexer.getTok().getLoc(),
893 "unexpected token in variant, expected ')'");
894 }
Nirav Davefd910412016-06-17 16:06:17 +0000895 Lex(); // eat ')'.
David Peixotto8ad70b32013-12-04 22:43:20 +0000896 Split = std::make_pair(Identifier, VName);
897 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000898
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = SMLoc::getFromPointer(Identifier.end());
900
Daniel Dunbard20cda02009-10-16 01:34:54 +0000901 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000902 StringRef SymbolName = Identifier;
903 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000904
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000905 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000906 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000907 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000908 if (Variant != MCSymbolRefExpr::VK_Invalid) {
909 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000910 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000911 Variant = MCSymbolRefExpr::VK_None;
912 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000913 return Error(SMLoc::getFromPointer(Split.second.begin()),
914 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000915 }
916 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000917
Jim Grosbach6f482002015-05-18 18:43:14 +0000918 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000919
Daniel Dunbard20cda02009-10-16 01:34:54 +0000920 // If this is an absolute variable reference, substitute it now to preserve
921 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000922 if (Sym->isVariable() &&
923 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000924 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000925 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000926
Vedant Kumar86dbd922015-08-31 17:44:53 +0000927 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000928 return false;
929 }
930
931 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000932 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000933 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000934 }
David Woodhousef42a6662014-02-01 16:20:54 +0000935 case AsmToken::BigNum:
936 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000937 case AsmToken::Integer: {
938 SMLoc Loc = getTok().getLoc();
939 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000940 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000941 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000943 // Look for 'b' or 'f' following an Integer as a directional label
944 if (Lexer.getKind() == AsmToken::Identifier) {
945 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000946 // Lookup the symbol variant if used.
947 std::pair<StringRef, StringRef> Split = IDVal.split('@');
948 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
949 if (Split.first.size() != IDVal.size()) {
950 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000951 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000952 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000953 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000954 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 if (IDVal == "f" || IDVal == "b") {
956 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000957 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000958 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000959 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +0000960 return Error(Loc, "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +0000961 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000962 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000963 Lex(); // Eat identifier.
964 }
965 }
Chris Lattner78db3622009-06-22 05:51:26 +0000966 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000967 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000968 case AsmToken::Real: {
969 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000970 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000971 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000972 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000973 Lex(); // Eat token.
974 return false;
975 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000976 case AsmToken::Dot: {
977 // This is a '.' reference, which references the current PC. Emit a
978 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000979 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000980 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000981 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000982 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000983 Lex(); // Eat identifier.
984 return false;
985 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000986 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000987 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000989 case AsmToken::LBrac:
990 if (!PlatformParser->HasBracketExpressions())
991 return TokError("brackets expression not supported on this target");
992 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000993 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000994 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000995 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000996 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000997 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000998 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000999 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001000 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001001 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001002 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001003 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001004 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001005 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001006 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001007 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001008 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001009 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001010 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001011 return false;
Chris Lattner78db3622009-06-22 05:51:26 +00001012 }
1013}
Chris Lattner7fdbce72009-06-22 06:32:03 +00001014
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001015bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001016 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001017 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +00001018}
1019
Daniel Dunbar55f16672010-09-17 02:47:07 +00001020const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +00001021AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +00001023 // Ask the target implementation about this expression first.
1024 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1025 if (NewE)
1026 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 // Recurse over the given expression, rebuilding it to apply the given variant
1028 // if there is exactly one symbol.
1029 switch (E->getKind()) {
1030 case MCExpr::Target:
1031 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001032 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033
1034 case MCExpr::SymbolRef: {
1035 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1036
1037 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001038 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1039 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 return E;
1041 }
1042
Jim Grosbach13760bd2015-05-30 01:25:56 +00001043 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 }
1045
1046 case MCExpr::Unary: {
1047 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001048 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001050 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001051 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001052 }
1053
1054 case MCExpr::Binary: {
1055 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001056 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1057 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001058
1059 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001060 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001061
Jim Grosbach4b905842013-09-20 23:08:21 +00001062 if (!LHS)
1063 LHS = BE->getLHS();
1064 if (!RHS)
1065 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001066
Jim Grosbach13760bd2015-05-30 01:25:56 +00001067 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001068 }
1069 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001070
Craig Toppera2886c22012-02-07 05:05:23 +00001071 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001072}
1073
Jim Grosbach4b905842013-09-20 23:08:21 +00001074/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001075///
Jim Grosbachbd164242011-08-20 16:24:13 +00001076/// expr ::= expr &&,|| expr -> lowest.
1077/// expr ::= expr |,^,&,! expr
1078/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1079/// expr ::= expr <<,>> expr
1080/// expr ::= expr +,- expr
1081/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001082/// expr ::= primaryexpr
1083///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001084bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001085 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001086 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001088 return true;
1089
Daniel Dunbar55f16672010-09-17 02:47:07 +00001090 // As a special case, we support 'a op b @ modifier' by rewriting the
1091 // expression to include the modifier. This is inefficient, but in general we
1092 // expect users to use 'a@modifier op b'.
1093 if (Lexer.getKind() == AsmToken::At) {
1094 Lex();
1095
1096 if (Lexer.isNot(AsmToken::Identifier))
1097 return TokError("unexpected symbol modifier following '@'");
1098
1099 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001100 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001101 if (Variant == MCSymbolRefExpr::VK_Invalid)
1102 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1103
Jim Grosbach4b905842013-09-20 23:08:21 +00001104 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001105 if (!ModifiedRes) {
1106 return TokError("invalid modifier '" + getTok().getIdentifier() +
1107 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001108 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001109
Daniel Dunbar55f16672010-09-17 02:47:07 +00001110 Res = ModifiedRes;
1111 Lex();
1112 }
1113
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001114 // Try to constant fold it up front, if possible.
1115 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001116 if (Res->evaluateAsAbsolute(Value))
1117 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001118
1119 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001120}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001121
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001122bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001123 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001124 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001125}
1126
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001127bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1128 SMLoc &EndLoc) {
1129 if (parseParenExpr(Res, EndLoc))
1130 return true;
1131
1132 for (; ParenDepth > 0; --ParenDepth) {
1133 if (parseBinOpRHS(1, Res, EndLoc))
1134 return true;
1135
1136 // We don't Lex() the last RParen.
1137 // This is the same behavior as parseParenExpression().
1138 if (ParenDepth - 1 > 0) {
1139 if (Lexer.isNot(AsmToken::RParen))
1140 return TokError("expected ')' in parentheses expression");
1141 EndLoc = Lexer.getTok().getEndLoc();
1142 Lex();
1143 }
1144 }
1145 return false;
1146}
1147
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001148bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001149 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Daniel Dunbar75630b32009-06-30 02:10:03 +00001151 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001152 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001153 return true;
1154
Jim Grosbach13760bd2015-05-30 01:25:56 +00001155 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001156 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001157
1158 return false;
1159}
1160
David Majnemer0993e0b2015-10-26 03:15:34 +00001161static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1162 MCBinaryExpr::Opcode &Kind,
1163 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001165 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001166 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167
Jim Grosbach4b905842013-09-20 23:08:21 +00001168 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001169 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001170 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001171 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001172 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001173 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001174 return 1;
1175
Jim Grosbach4b905842013-09-20 23:08:21 +00001176 // Low Precedence: |, &, ^
1177 //
1178 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001179 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001181 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001182 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001183 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001184 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001185 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001186 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001187 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001188
Jim Grosbach4b905842013-09-20 23:08:21 +00001189 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001190 case AsmToken::EqualEqual:
1191 Kind = MCBinaryExpr::EQ;
1192 return 3;
1193 case AsmToken::ExclaimEqual:
1194 case AsmToken::LessGreater:
1195 Kind = MCBinaryExpr::NE;
1196 return 3;
1197 case AsmToken::Less:
1198 Kind = MCBinaryExpr::LT;
1199 return 3;
1200 case AsmToken::LessEqual:
1201 Kind = MCBinaryExpr::LTE;
1202 return 3;
1203 case AsmToken::Greater:
1204 Kind = MCBinaryExpr::GT;
1205 return 3;
1206 case AsmToken::GreaterEqual:
1207 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001208 return 3;
1209
Jim Grosbach4b905842013-09-20 23:08:21 +00001210 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001211 case AsmToken::LessLess:
1212 Kind = MCBinaryExpr::Shl;
1213 return 4;
1214 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001215 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001216 return 4;
1217
Jim Grosbach4b905842013-09-20 23:08:21 +00001218 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001219 case AsmToken::Plus:
1220 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001221 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001222 case AsmToken::Minus:
1223 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001224 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001225
Jim Grosbach4b905842013-09-20 23:08:21 +00001226 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001227 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001228 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001229 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001230 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001231 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001232 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001233 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001234 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001235 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001236 }
1237}
1238
David Majnemer0993e0b2015-10-26 03:15:34 +00001239static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1240 MCBinaryExpr::Opcode &Kind,
1241 bool ShouldUseLogicalShr) {
1242 switch (K) {
1243 default:
1244 return 0; // not a binop.
1245
1246 // Lowest Precedence: &&, ||
1247 case AsmToken::AmpAmp:
1248 Kind = MCBinaryExpr::LAnd;
1249 return 2;
1250 case AsmToken::PipePipe:
1251 Kind = MCBinaryExpr::LOr;
1252 return 1;
1253
1254 // Low Precedence: ==, !=, <>, <, <=, >, >=
1255 case AsmToken::EqualEqual:
1256 Kind = MCBinaryExpr::EQ;
1257 return 3;
1258 case AsmToken::ExclaimEqual:
1259 case AsmToken::LessGreater:
1260 Kind = MCBinaryExpr::NE;
1261 return 3;
1262 case AsmToken::Less:
1263 Kind = MCBinaryExpr::LT;
1264 return 3;
1265 case AsmToken::LessEqual:
1266 Kind = MCBinaryExpr::LTE;
1267 return 3;
1268 case AsmToken::Greater:
1269 Kind = MCBinaryExpr::GT;
1270 return 3;
1271 case AsmToken::GreaterEqual:
1272 Kind = MCBinaryExpr::GTE;
1273 return 3;
1274
1275 // Low Intermediate Precedence: +, -
1276 case AsmToken::Plus:
1277 Kind = MCBinaryExpr::Add;
1278 return 4;
1279 case AsmToken::Minus:
1280 Kind = MCBinaryExpr::Sub;
1281 return 4;
1282
1283 // High Intermediate Precedence: |, &, ^
1284 //
1285 // FIXME: gas seems to support '!' as an infix operator?
1286 case AsmToken::Pipe:
1287 Kind = MCBinaryExpr::Or;
1288 return 5;
1289 case AsmToken::Caret:
1290 Kind = MCBinaryExpr::Xor;
1291 return 5;
1292 case AsmToken::Amp:
1293 Kind = MCBinaryExpr::And;
1294 return 5;
1295
1296 // Highest Precedence: *, /, %, <<, >>
1297 case AsmToken::Star:
1298 Kind = MCBinaryExpr::Mul;
1299 return 6;
1300 case AsmToken::Slash:
1301 Kind = MCBinaryExpr::Div;
1302 return 6;
1303 case AsmToken::Percent:
1304 Kind = MCBinaryExpr::Mod;
1305 return 6;
1306 case AsmToken::LessLess:
1307 Kind = MCBinaryExpr::Shl;
1308 return 6;
1309 case AsmToken::GreaterGreater:
1310 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1311 return 6;
1312 }
1313}
1314
1315unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1316 MCBinaryExpr::Opcode &Kind) {
1317 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1318 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1319 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1320}
1321
Jim Grosbach4b905842013-09-20 23:08:21 +00001322/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001323/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001324bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001325 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001326 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001327 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001328 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001329
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001330 // If the next token is lower precedence than we are allowed to eat, return
1331 // successfully with what we ate already.
1332 if (TokPrec < Precedence)
1333 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001334
Sean Callanan686ed8d2010-01-19 20:22:31 +00001335 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001336
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001337 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001338 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001339 if (parsePrimaryExpr(RHS, EndLoc))
1340 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001341
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001342 // If BinOp binds less tightly with RHS than the operator after RHS, let
1343 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001344 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001345 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001346 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1347 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001348
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001349 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001350 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001351 }
1352}
1353
Chris Lattner36e02122009-06-21 20:54:55 +00001354/// ParseStatement:
1355/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001356/// ::= Label* Directive ...Operands... EndOfStatement
1357/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001358bool AsmParser::parseStatement(ParseStatementInfo &Info,
1359 MCAsmParserSemaCallback *SI) {
Nirav Davefd910412016-06-17 16:06:17 +00001360 // Eat initial spaces and comments
1361 while (Lexer.is(AsmToken::Space))
1362 Lex();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001363 if (Lexer.is(AsmToken::EndOfStatement)) {
Nirav Davefd910412016-06-17 16:06:17 +00001364 // if this is a line comment we can drop it safely
1365 if (getTok().getString().front() == '\r' ||
1366 getTok().getString().front() == '\n')
1367 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001368 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001369 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001370 }
Nirav Davefd910412016-06-17 16:06:17 +00001371 // Statements always start with an identifier.
Sean Callanan936b0d32010-01-19 21:44:56 +00001372 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001373 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001374 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001375 int64_t LocalLabelVal = -1;
Nirav Davefd910412016-06-17 16:06:17 +00001376 if (Lexer.is(AsmToken::HashDirective))
Jim Grosbach4b905842013-09-20 23:08:21 +00001377 return parseCppHashLineFilenameComment(IDLoc);
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001378 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001379 if (Lexer.is(AsmToken::Integer)) {
1380 LocalLabelVal = getTok().getIntVal();
1381 if (LocalLabelVal < 0) {
1382 if (!TheCondState.Ignore)
1383 return TokError("unexpected token at start of statement");
1384 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001385 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001386 IDVal = getTok().getString();
1387 Lex(); // Consume the integer token to be used as an identifier token.
1388 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001389 if (!TheCondState.Ignore)
1390 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001391 }
1392 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001393 } else if (Lexer.is(AsmToken::Dot)) {
1394 // Treat '.' as a valid identifier in this context.
1395 Lex();
1396 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001397 } else if (Lexer.is(AsmToken::LCurly)) {
1398 // Treat '{' as a valid identifier in this context.
1399 Lex();
1400 IDVal = "{";
1401
1402 } else if (Lexer.is(AsmToken::RCurly)) {
1403 // Treat '}' as a valid identifier in this context.
1404 Lex();
1405 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001406 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001407 if (!TheCondState.Ignore)
1408 return TokError("unexpected token at start of statement");
1409 IDVal = "";
1410 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001411
Chris Lattner926885c2010-04-17 18:14:27 +00001412 // Handle conditional assembly here before checking for skipping. We
1413 // have to do this so that .endif isn't skipped in a ".if 0" block for
1414 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001415 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001416 DirectiveKindMap.find(IDVal);
1417 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1418 ? DK_NO_DIRECTIVE
1419 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001420 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001421 default:
1422 break;
1423 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001424 case DK_IFEQ:
1425 case DK_IFGE:
1426 case DK_IFGT:
1427 case DK_IFLE:
1428 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001429 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001430 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001431 case DK_IFB:
1432 return parseDirectiveIfb(IDLoc, true);
1433 case DK_IFNB:
1434 return parseDirectiveIfb(IDLoc, false);
1435 case DK_IFC:
1436 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001437 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001438 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001439 case DK_IFNC:
1440 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001441 case DK_IFNES:
1442 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001443 case DK_IFDEF:
1444 return parseDirectiveIfdef(IDLoc, true);
1445 case DK_IFNDEF:
1446 case DK_IFNOTDEF:
1447 return parseDirectiveIfdef(IDLoc, false);
1448 case DK_ELSEIF:
1449 return parseDirectiveElseIf(IDLoc);
1450 case DK_ELSE:
1451 return parseDirectiveElse(IDLoc);
1452 case DK_ENDIF:
1453 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001454 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001455
Eli Bendersky88024712013-01-16 19:32:36 +00001456 // Ignore the statement if in the middle of inactive conditional
1457 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001458 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001459 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001460 return false;
1461 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001462
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001463 // FIXME: Recurse on local labels?
1464
1465 // See what kind of statement we have.
1466 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001467 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001468 if (!getTargetParser().isLabel(ID))
1469 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001470 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001471
Chris Lattner36e02122009-06-21 20:54:55 +00001472 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001473 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001474
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001475 // Diagnose attempt to use '.' as a label.
1476 if (IDVal == ".")
1477 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1478
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001479 // Diagnose attempt to use a variable as a label.
1480 //
1481 // FIXME: Diagnostics. Note the location of the definition as a label.
1482 // FIXME: This doesn't diagnose assignment to a symbol which has been
1483 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001484 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001485 if (LocalLabelVal == -1) {
1486 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001487 StringRef RewrittenLabel =
1488 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1489 assert(RewrittenLabel.size() &&
1490 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001491 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1492 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001493 IDVal = RewrittenLabel;
1494 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001495 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001496 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001497 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001498
1499 Sym->redefineIfPossible();
1500
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001501 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001502 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001503
Nirav Dave8ea792d2016-07-13 14:03:12 +00001504 // Consume any end of statement token, if present, to avoid spurious
1505 // AddBlankLine calls().
1506 if (getTok().is(AsmToken::EndOfStatement)) {
1507 Lex();
1508 }
1509
Daniel Dunbare73b2672009-08-26 22:13:22 +00001510 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001511 if (!ParsingInlineAsm)
1512 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001513
Kevin Enderbye7739d42011-12-09 18:09:40 +00001514 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001515 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001516 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001517 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1518 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001519
Tim Northover1744d0a2013-10-25 12:49:50 +00001520 getTargetParser().onLabelParsed(Sym);
1521
Nirav Dave8ea792d2016-07-13 14:03:12 +00001522
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001523
Eli Friedman0f4871d2012-10-22 23:58:19 +00001524 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001525 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001526
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001527 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001528 if (!getTargetParser().equalIsAsmAssignment())
1529 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001530 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001531 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001532
Jim Grosbach4b905842013-09-20 23:08:21 +00001533 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001534
1535 default: // Normal instruction or directive.
1536 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001537 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001538
1539 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001540 if (areMacrosEnabled())
1541 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1542 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001543 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001544
Michael J. Spencer530ce852010-10-09 11:00:50 +00001545 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001546
Eli Bendersky17233942013-01-15 22:59:42 +00001547 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001548 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001549 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001550 //
Eli Bendersky17233942013-01-15 22:59:42 +00001551 // 1. The target-specific assembly parser. Some directives are target
1552 // specific or may potentially behave differently on certain targets.
1553 // 2. Asm parser extensions. For example, platform-specific parsers
1554 // (like the ELF parser) register themselves as extensions.
1555 // 3. The generic directive parser implemented by this class. These are
1556 // all the directives that behave in a target and platform independent
1557 // manner, or at least have a default behavior that's shared between
1558 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001559
Eli Bendersky17233942013-01-15 22:59:42 +00001560 // First query the target-specific parser. It will return 'true' if it
1561 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001562 if (!getTargetParser().ParseDirective(ID))
1563 return false;
1564
Alp Tokercb402912014-01-24 17:20:08 +00001565 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001566 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1568 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001569 if (Handler.first)
1570 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1571
1572 // Finally, if no one else is interested in this directive, it must be
1573 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001574 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001575 default:
1576 break;
1577 case DK_SET:
1578 case DK_EQU:
1579 return parseDirectiveSet(IDVal, true);
1580 case DK_EQUIV:
1581 return parseDirectiveSet(IDVal, false);
1582 case DK_ASCII:
1583 return parseDirectiveAscii(IDVal, false);
1584 case DK_ASCIZ:
1585 case DK_STRING:
1586 return parseDirectiveAscii(IDVal, true);
1587 case DK_BYTE:
1588 return parseDirectiveValue(1);
1589 case DK_SHORT:
1590 case DK_VALUE:
1591 case DK_2BYTE:
1592 return parseDirectiveValue(2);
1593 case DK_LONG:
1594 case DK_INT:
1595 case DK_4BYTE:
1596 return parseDirectiveValue(4);
1597 case DK_QUAD:
1598 case DK_8BYTE:
1599 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001600 case DK_OCTA:
1601 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001602 case DK_SINGLE:
1603 case DK_FLOAT:
1604 return parseDirectiveRealValue(APFloat::IEEEsingle);
1605 case DK_DOUBLE:
1606 return parseDirectiveRealValue(APFloat::IEEEdouble);
1607 case DK_ALIGN: {
1608 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1609 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1610 }
1611 case DK_ALIGN32: {
1612 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1613 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1614 }
1615 case DK_BALIGN:
1616 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1617 case DK_BALIGNW:
1618 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1619 case DK_BALIGNL:
1620 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1621 case DK_P2ALIGN:
1622 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1623 case DK_P2ALIGNW:
1624 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1625 case DK_P2ALIGNL:
1626 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1627 case DK_ORG:
1628 return parseDirectiveOrg();
1629 case DK_FILL:
1630 return parseDirectiveFill();
1631 case DK_ZERO:
1632 return parseDirectiveZero();
1633 case DK_EXTERN:
1634 eatToEndOfStatement(); // .extern is the default, ignore it.
1635 return false;
1636 case DK_GLOBL:
1637 case DK_GLOBAL:
1638 return parseDirectiveSymbolAttribute(MCSA_Global);
1639 case DK_LAZY_REFERENCE:
1640 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1641 case DK_NO_DEAD_STRIP:
1642 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1643 case DK_SYMBOL_RESOLVER:
1644 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1645 case DK_PRIVATE_EXTERN:
1646 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1647 case DK_REFERENCE:
1648 return parseDirectiveSymbolAttribute(MCSA_Reference);
1649 case DK_WEAK_DEFINITION:
1650 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1651 case DK_WEAK_REFERENCE:
1652 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1653 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1654 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1655 case DK_COMM:
1656 case DK_COMMON:
1657 return parseDirectiveComm(/*IsLocal=*/false);
1658 case DK_LCOMM:
1659 return parseDirectiveComm(/*IsLocal=*/true);
1660 case DK_ABORT:
1661 return parseDirectiveAbort();
1662 case DK_INCLUDE:
1663 return parseDirectiveInclude();
1664 case DK_INCBIN:
1665 return parseDirectiveIncbin();
1666 case DK_CODE16:
1667 case DK_CODE16GCC:
Nirav Davefd910412016-06-17 16:06:17 +00001668 return TokError(Twine(IDVal) +
1669 " not currently supported for this target");
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001671 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001672 case DK_IRP:
1673 return parseDirectiveIrp(IDLoc);
1674 case DK_IRPC:
1675 return parseDirectiveIrpc(IDLoc);
1676 case DK_ENDR:
1677 return parseDirectiveEndr(IDLoc);
1678 case DK_BUNDLE_ALIGN_MODE:
1679 return parseDirectiveBundleAlignMode();
1680 case DK_BUNDLE_LOCK:
1681 return parseDirectiveBundleLock();
1682 case DK_BUNDLE_UNLOCK:
1683 return parseDirectiveBundleUnlock();
1684 case DK_SLEB128:
1685 return parseDirectiveLEB128(true);
1686 case DK_ULEB128:
1687 return parseDirectiveLEB128(false);
1688 case DK_SPACE:
1689 case DK_SKIP:
1690 return parseDirectiveSpace(IDVal);
1691 case DK_FILE:
1692 return parseDirectiveFile(IDLoc);
1693 case DK_LINE:
1694 return parseDirectiveLine();
1695 case DK_LOC:
1696 return parseDirectiveLoc();
1697 case DK_STABS:
1698 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001699 case DK_CV_FILE:
1700 return parseDirectiveCVFile();
1701 case DK_CV_LOC:
1702 return parseDirectiveCVLoc();
1703 case DK_CV_LINETABLE:
1704 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001705 case DK_CV_INLINE_LINETABLE:
1706 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001707 case DK_CV_DEF_RANGE:
1708 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001709 case DK_CV_STRINGTABLE:
1710 return parseDirectiveCVStringTable();
1711 case DK_CV_FILECHECKSUMS:
1712 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001713 case DK_CFI_SECTIONS:
1714 return parseDirectiveCFISections();
1715 case DK_CFI_STARTPROC:
1716 return parseDirectiveCFIStartProc();
1717 case DK_CFI_ENDPROC:
1718 return parseDirectiveCFIEndProc();
1719 case DK_CFI_DEF_CFA:
1720 return parseDirectiveCFIDefCfa(IDLoc);
1721 case DK_CFI_DEF_CFA_OFFSET:
1722 return parseDirectiveCFIDefCfaOffset();
1723 case DK_CFI_ADJUST_CFA_OFFSET:
1724 return parseDirectiveCFIAdjustCfaOffset();
1725 case DK_CFI_DEF_CFA_REGISTER:
1726 return parseDirectiveCFIDefCfaRegister(IDLoc);
1727 case DK_CFI_OFFSET:
1728 return parseDirectiveCFIOffset(IDLoc);
1729 case DK_CFI_REL_OFFSET:
1730 return parseDirectiveCFIRelOffset(IDLoc);
1731 case DK_CFI_PERSONALITY:
1732 return parseDirectiveCFIPersonalityOrLsda(true);
1733 case DK_CFI_LSDA:
1734 return parseDirectiveCFIPersonalityOrLsda(false);
1735 case DK_CFI_REMEMBER_STATE:
1736 return parseDirectiveCFIRememberState();
1737 case DK_CFI_RESTORE_STATE:
1738 return parseDirectiveCFIRestoreState();
1739 case DK_CFI_SAME_VALUE:
1740 return parseDirectiveCFISameValue(IDLoc);
1741 case DK_CFI_RESTORE:
1742 return parseDirectiveCFIRestore(IDLoc);
1743 case DK_CFI_ESCAPE:
1744 return parseDirectiveCFIEscape();
1745 case DK_CFI_SIGNAL_FRAME:
1746 return parseDirectiveCFISignalFrame();
1747 case DK_CFI_UNDEFINED:
1748 return parseDirectiveCFIUndefined(IDLoc);
1749 case DK_CFI_REGISTER:
1750 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001751 case DK_CFI_WINDOW_SAVE:
1752 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001753 case DK_MACROS_ON:
1754 case DK_MACROS_OFF:
1755 return parseDirectiveMacrosOnOff(IDVal);
1756 case DK_MACRO:
1757 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001758 case DK_EXITM:
1759 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001760 case DK_ENDM:
1761 case DK_ENDMACRO:
1762 return parseDirectiveEndMacro(IDVal);
1763 case DK_PURGEM:
1764 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001765 case DK_END:
1766 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001767 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001768 return parseDirectiveError(IDLoc, false);
1769 case DK_ERROR:
1770 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001771 case DK_WARNING:
1772 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001773 case DK_RELOC:
1774 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001775 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001776
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001777 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001778 }
Chris Lattner36e02122009-06-21 20:54:55 +00001779
Chad Rosierc7f552c2013-02-12 21:33:51 +00001780 // __asm _emit or __asm __emit
1781 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1782 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001783 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001784
1785 // __asm align
1786 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001787 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001788
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001789 if (ParsingInlineAsm && (IDVal == "even"))
1790 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001791 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001792
Chris Lattner7cbfa442010-05-19 23:34:33 +00001793 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001794 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001795 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001796 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001797 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001798 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001799
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001800 // Dump the parsed representation, if requested.
1801 if (getShowParsedOperands()) {
1802 SmallString<256> Str;
1803 raw_svector_ostream OS(Str);
1804 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001805 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001806 if (i != 0)
1807 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001808 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001809 }
1810 OS << "]";
1811
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001813 }
1814
Oliver Stannard8b273082014-06-19 15:52:37 +00001815 // If we are generating dwarf for the current section then generate a .loc
1816 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001817 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001818 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001819 getStreamer().getCurrentSection().first)) {
1820 unsigned Line;
1821 if (ActiveMacros.empty())
1822 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1823 else
Frederic Riss16238d92015-06-25 21:57:33 +00001824 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1825 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001826
Eli Bendersky88024712013-01-16 19:32:36 +00001827 // If we previously parsed a cpp hash file line comment then make sure the
1828 // current Dwarf File is for the CppHashFilename if not then emit the
1829 // Dwarf File table for it and adjust the line number for the .loc.
Tim Northoverc0bef992016-04-13 19:46:54 +00001830 if (CppHashInfo.Filename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001831 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
Tim Northoverc0bef992016-04-13 19:46:54 +00001832 0, StringRef(), CppHashInfo.Filename);
David Blaikiec714ef42014-03-17 01:52:11 +00001833 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001834
Jim Grosbach4b905842013-09-20 23:08:21 +00001835 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1836 // cache with the different Loc from the call above we save the last
1837 // info we queried here with SrcMgr.FindLineNumber().
1838 unsigned CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001839 if (LastQueryIDLoc == CppHashInfo.Loc &&
1840 LastQueryBuffer == CppHashInfo.Buf)
Jim Grosbach4b905842013-09-20 23:08:21 +00001841 CppHashLocLineNo = LastQueryLine;
1842 else {
Tim Northoverc0bef992016-04-13 19:46:54 +00001843 CppHashLocLineNo =
1844 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001845 LastQueryLine = CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001846 LastQueryIDLoc = CppHashInfo.Loc;
1847 LastQueryBuffer = CppHashInfo.Buf;
Jim Grosbach4b905842013-09-20 23:08:21 +00001848 }
Tim Northoverc0bef992016-04-13 19:46:54 +00001849 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001850 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001851
Jim Grosbach4b905842013-09-20 23:08:21 +00001852 getStreamer().EmitDwarfLocDirective(
1853 getContext().getGenDwarfFileNumber(), Line, 0,
1854 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1855 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001856 }
1857
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001858 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001859 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001860 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001861 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1862 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001863 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001864 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001865
Chris Lattnera2a9d162010-09-11 16:18:25 +00001866 // Don't skip the rest of the line, the instruction parser is responsible for
1867 // that.
1868 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001869}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001870
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001871// Parse and erase curly braces marking block start/end
1872bool
1873AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1874 // Identify curly brace marking block start/end
1875 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1876 return false;
1877
1878 SMLoc StartLoc = Lexer.getLoc();
1879 Lex(); // Eat the brace
1880 if (Lexer.is(AsmToken::EndOfStatement))
1881 Lex(); // Eat EndOfStatement following the brace
1882
1883 // Erase the block start/end brace from the output asm string
1884 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
1885 StartLoc.getPointer());
1886 return true;
1887}
1888
Jim Grosbach4b905842013-09-20 23:08:21 +00001889/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001890/// ::= # number "filename"
Craig Topper3c76c522015-09-20 23:35:59 +00001891bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001892 Lex(); // Eat the hash token.
Nirav Davefd910412016-06-17 16:06:17 +00001893 // Lexer only ever emits HashDirective if it fully formed if it's
1894 // done the checking already so this is an internal error.
1895 assert(getTok().is(AsmToken::Integer) &&
1896 "Lexing Cpp line comment: Expected Integer");
Kevin Enderby72553612011-09-13 23:45:18 +00001897 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001898 Lex();
Nirav Davefd910412016-06-17 16:06:17 +00001899 assert(getTok().is(AsmToken::String) &&
1900 "Lexing Cpp line comment: Expected String");
Kevin Enderby72553612011-09-13 23:45:18 +00001901 StringRef Filename = getTok().getString();
Nirav Davefd910412016-06-17 16:06:17 +00001902 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001903 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001904 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001905
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001906 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
Tim Northoverc0bef992016-04-13 19:46:54 +00001907 CppHashInfo.Loc = L;
1908 CppHashInfo.Filename = Filename;
1909 CppHashInfo.LineNumber = LineNumber;
1910 CppHashInfo.Buf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001911 return false;
1912}
1913
Jim Grosbach4b905842013-09-20 23:08:21 +00001914/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001915/// for the Filename and LineNo if any in the diagnostic.
1916void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001917 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001918 raw_ostream &OS = errs();
1919
1920 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001921 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001922 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1923 unsigned CppHashBuf =
Tim Northoverc0bef992016-04-13 19:46:54 +00001924 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001925
Jim Grosbach4b905842013-09-20 23:08:21 +00001926 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001927 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001928 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1929 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1930 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001931 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1932 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001933 }
1934
Eric Christophera7c32732012-12-18 00:30:54 +00001935 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001936 // manager changed or buffer changed (like in a nested include) then just
1937 // print the normal diagnostic using its Filename and LineNo.
Tim Northoverc0bef992016-04-13 19:46:54 +00001938 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001939 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001940 if (Parser->SavedDiagHandler)
1941 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1942 else
Craig Topper353eda42014-04-24 06:44:33 +00001943 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001944 return;
1945 }
1946
Eric Christophera7c32732012-12-18 00:30:54 +00001947 // Use the CppHashFilename and calculate a line number based on the
Tim Northoverc0bef992016-04-13 19:46:54 +00001948 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
1949 // for the diagnostic.
1950 const std::string &Filename = Parser->CppHashInfo.Filename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001951
1952 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1953 int CppHashLocLineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001954 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001955 int LineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00001956 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001957
Jim Grosbach4b905842013-09-20 23:08:21 +00001958 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1959 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001960 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001961
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001962 if (Parser->SavedDiagHandler)
1963 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1964 else
Craig Topper353eda42014-04-24 06:44:33 +00001965 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001966}
1967
Rafael Espindola2c064482012-08-21 18:29:30 +00001968// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1969// difference being that that function accepts '@' as part of identifiers and
1970// we can't do that. AsmLexer.cpp should probably be changed to handle
1971// '@' as a special case when needed.
1972static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001973 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1974 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001975}
1976
Rafael Espindola34b9c512012-06-03 23:57:14 +00001977bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001978 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001979 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001980 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001981 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001982 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001983 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001984 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001985
Preston Gurd05500642012-09-19 20:36:12 +00001986 // A macro without parameters is handled differently on Darwin:
1987 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001988 while (!Body.empty()) {
1989 // Scan for the next substitution.
1990 std::size_t End = Body.size(), Pos = 0;
1991 for (; Pos != End; ++Pos) {
1992 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001993 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001994 // This macro has no parameters, look for $0, $1, etc.
1995 if (Body[Pos] != '$' || Pos + 1 == End)
1996 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001997
Rafael Espindola1134ab232011-06-05 02:43:45 +00001998 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001999 if (Next == '$' || Next == 'n' ||
2000 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002001 break;
2002 } else {
2003 // This macro has parameters, look for \foo, \bar, etc.
2004 if (Body[Pos] == '\\' && Pos + 1 != End)
2005 break;
2006 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002007 }
2008
2009 // Add the prefix.
2010 OS << Body.slice(0, Pos);
2011
2012 // Check if we reached the end.
2013 if (Pos == End)
2014 break;
2015
Benjamin Kramer513e7442014-02-20 13:36:32 +00002016 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002017 switch (Body[Pos + 1]) {
2018 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002019 case '$':
2020 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002021 break;
2022
Jim Grosbach4b905842013-09-20 23:08:21 +00002023 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002024 case 'n':
2025 OS << A.size();
2026 break;
2027
Jim Grosbach4b905842013-09-20 23:08:21 +00002028 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002029 default: {
2030 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002031 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002032 if (Index >= A.size())
2033 break;
2034
2035 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002036 for (const AsmToken &Token : A[Index])
2037 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002038 break;
2039 }
2040 }
2041 Pos += 2;
2042 } else {
2043 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002044
2045 // Check for the \@ pseudo-variable.
2046 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002047 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002048 else
2049 while (isIdentifierChar(Body[I]) && I + 1 != End)
2050 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002051
Jim Grosbach4b905842013-09-20 23:08:21 +00002052 const char *Begin = Body.data() + Pos + 1;
2053 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002054 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002055
Toma Tabacu217116e2015-04-27 10:50:29 +00002056 if (Argument == "@") {
2057 OS << NumOfMacroInstantiations;
2058 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002059 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002060 for (; Index < NParameters; ++Index)
2061 if (Parameters[Index].Name == Argument)
2062 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002063
Toma Tabacu217116e2015-04-27 10:50:29 +00002064 if (Index == NParameters) {
2065 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2066 Pos += 3;
2067 else {
2068 OS << '\\' << Argument;
2069 Pos = I;
2070 }
2071 } else {
2072 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002073 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002074 // We expect no quotes around the string's contents when
2075 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002076 if (Token.getKind() != AsmToken::String || VarargParameter)
2077 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002078 else
Craig Topper84008482015-10-10 05:38:14 +00002079 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002080
2081 Pos += 1 + Argument.size();
2082 }
Preston Gurd05500642012-09-19 20:36:12 +00002083 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002084 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002085 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002086 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002087 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002088
Rafael Espindola1134ab232011-06-05 02:43:45 +00002089 return false;
2090}
Daniel Dunbar43235712010-07-18 18:54:11 +00002091
Nico Weber2a8f9222014-07-24 16:29:04 +00002092MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002093 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002094 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002095 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002096
Jim Grosbach4b905842013-09-20 23:08:21 +00002097static bool isOperator(AsmToken::TokenKind kind) {
2098 switch (kind) {
2099 default:
2100 return false;
2101 case AsmToken::Plus:
2102 case AsmToken::Minus:
2103 case AsmToken::Tilde:
2104 case AsmToken::Slash:
2105 case AsmToken::Star:
2106 case AsmToken::Dot:
2107 case AsmToken::Equal:
2108 case AsmToken::EqualEqual:
2109 case AsmToken::Pipe:
2110 case AsmToken::PipePipe:
2111 case AsmToken::Caret:
2112 case AsmToken::Amp:
2113 case AsmToken::AmpAmp:
2114 case AsmToken::Exclaim:
2115 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002116 case AsmToken::Less:
2117 case AsmToken::LessEqual:
2118 case AsmToken::LessLess:
2119 case AsmToken::LessGreater:
2120 case AsmToken::Greater:
2121 case AsmToken::GreaterEqual:
2122 case AsmToken::GreaterGreater:
2123 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002124 }
2125}
2126
David Majnemer16252452014-01-29 00:07:39 +00002127namespace {
2128class AsmLexerSkipSpaceRAII {
2129public:
2130 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2131 Lexer.setSkipSpace(SkipSpace);
2132 }
2133
2134 ~AsmLexerSkipSpaceRAII() {
2135 Lexer.setSkipSpace(true);
2136 }
2137
2138private:
2139 AsmLexer &Lexer;
2140};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002141}
David Majnemer16252452014-01-29 00:07:39 +00002142
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002143bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2144
2145 if (Vararg) {
2146 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2147 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002148 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002149 }
2150 return false;
2151 }
2152
Rafael Espindola768b41c2012-06-15 14:02:34 +00002153 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002154
David Majnemer16252452014-01-29 00:07:39 +00002155 // Darwin doesn't use spaces to delmit arguments.
2156 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002157
Scott Egertona1fa68a2016-02-11 13:48:49 +00002158 bool SpaceEaten;
2159
Rafael Espindola768b41c2012-06-15 14:02:34 +00002160 for (;;) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002161 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002162 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002163 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002164
Scott Egertona1fa68a2016-02-11 13:48:49 +00002165 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002166
Scott Egertona1fa68a2016-02-11 13:48:49 +00002167 if (Lexer.is(AsmToken::Comma))
2168 break;
2169
2170 if (Lexer.is(AsmToken::Space)) {
2171 SpaceEaten = true;
Nirav Dave1180e6892016-06-02 17:15:05 +00002172 Lexer.Lex(); // Eat spaces
Scott Egertona1fa68a2016-02-11 13:48:49 +00002173 }
Preston Gurd05500642012-09-19 20:36:12 +00002174
2175 // Spaces can delimit parameters, but could also be part an expression.
2176 // If the token after a space is an operator, add the token and the next
2177 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002178 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002179 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002180 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002181 Lexer.Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002182
Scott Egertona1fa68a2016-02-11 13:48:49 +00002183 // Whitespace after an operator can be ignored.
2184 if (Lexer.is(AsmToken::Space))
Nirav Dave1180e6892016-06-02 17:15:05 +00002185 Lexer.Lex();
Scott Egertona1fa68a2016-02-11 13:48:49 +00002186
2187 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002188 }
2189 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002190 if (SpaceEaten)
2191 break;
Preston Gurd05500642012-09-19 20:36:12 +00002192 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002193
Jim Grosbach4b905842013-09-20 23:08:21 +00002194 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002195 // to be able to fill in the remaining default parameter values
2196 if (Lexer.is(AsmToken::EndOfStatement))
2197 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002198
2199 // Adjust the current parentheses level.
2200 if (Lexer.is(AsmToken::LParen))
2201 ++ParenLevel;
2202 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2203 --ParenLevel;
2204
2205 // Append the token to the current argument list.
2206 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002207 Lexer.Lex();
Rafael Espindola768b41c2012-06-15 14:02:34 +00002208 }
Preston Gurd05500642012-09-19 20:36:12 +00002209
Rafael Espindola768b41c2012-06-15 14:02:34 +00002210 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002211 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002212 return false;
2213}
2214
2215// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002216bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002217 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002218 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002219 bool NamedParametersFound = false;
2220 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002221
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002222 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002223 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002224
Rafael Espindola768b41c2012-06-15 14:02:34 +00002225 // Parse two kinds of macro invocations:
2226 // - macros defined without any parameters accept an arbitrary number of them
2227 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002228 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002229 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2230 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002231 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002232 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002233
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002234 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002235 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002236 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002237 eatToEndOfStatement();
2238 return true;
2239 }
2240
2241 if (!Lexer.is(AsmToken::Equal)) {
2242 TokError("expected '=' after formal parameter identifier");
2243 eatToEndOfStatement();
2244 return true;
2245 }
2246 Lex();
2247
2248 NamedParametersFound = true;
2249 }
2250
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002251 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002252 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002253 eatToEndOfStatement();
2254 return true;
2255 }
2256
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002257 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2258 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002259 return true;
2260
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002261 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002262 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002263 unsigned FAI = 0;
2264 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002265 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002266 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002267
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002268 if (FAI >= NParameters) {
Nirav Davefd910412016-06-17 16:06:17 +00002269 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002270 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002271 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002272 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002273 return true;
2274 }
2275 PI = FAI;
2276 }
2277
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002278 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002279 if (A.size() <= PI)
2280 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002281 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002282
2283 if (FALocs.size() <= PI)
2284 FALocs.resize(PI + 1);
2285
2286 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002287 }
Jim Grosbach206661622012-07-30 22:44:17 +00002288
Preston Gurd242ed3152012-09-19 20:29:04 +00002289 // At the end of the statement, fill in remaining arguments that have
2290 // default values. If there aren't any, then the next argument is
2291 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002292 if (Lexer.is(AsmToken::EndOfStatement)) {
2293 bool Failure = false;
2294 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2295 if (A[FAI].empty()) {
2296 if (M->Parameters[FAI].Required) {
2297 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2298 "missing value for required parameter "
2299 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2300 Failure = true;
2301 }
2302
2303 if (!M->Parameters[FAI].Value.empty())
2304 A[FAI] = M->Parameters[FAI].Value;
2305 }
2306 }
2307 return Failure;
2308 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002309
2310 if (Lexer.is(AsmToken::Comma))
2311 Lex();
2312 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002313
2314 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002315}
2316
Jim Grosbach4b905842013-09-20 23:08:21 +00002317const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002318 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2319 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002320}
2321
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002322void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2323 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002324}
2325
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002326void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002327
Jim Grosbach4b905842013-09-20 23:08:21 +00002328bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002329 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2330 // this, although we should protect against infinite loops.
2331 if (ActiveMacros.size() == 20)
2332 return TokError("macros cannot be nested more than 20 levels deep");
2333
Eli Bendersky38274122013-01-14 23:22:36 +00002334 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002335 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002336 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002337
Rafael Espindola1134ab232011-06-05 02:43:45 +00002338 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2339 // to hold the macro body with substitutions.
2340 SmallString<256> Buf;
2341 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002342 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002343
Toma Tabacu217116e2015-04-27 10:50:29 +00002344 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002345 return true;
2346
Eli Bendersky38274122013-01-14 23:22:36 +00002347 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002348 // instantiation.
2349 OS << ".endmacro\n";
2350
Rafael Espindola3560ff22014-08-27 20:03:13 +00002351 std::unique_ptr<MemoryBuffer> Instantiation =
2352 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002353
Daniel Dunbar43235712010-07-18 18:54:11 +00002354 // Create the macro instantiation object and add to the current macro
2355 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002356 MacroInstantiation *MI = new MacroInstantiation(
2357 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002358 ActiveMacros.push_back(MI);
2359
Toma Tabacu217116e2015-04-27 10:50:29 +00002360 ++NumOfMacroInstantiations;
2361
Daniel Dunbar43235712010-07-18 18:54:11 +00002362 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002363 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002364 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002365 Lex();
2366
2367 return false;
2368}
2369
Jim Grosbach4b905842013-09-20 23:08:21 +00002370void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002371 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002372 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002373 Lex();
2374
2375 // Pop the instantiation entry.
2376 delete ActiveMacros.back();
2377 ActiveMacros.pop_back();
2378}
2379
Jim Grosbach4b905842013-09-20 23:08:21 +00002380bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002381 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002382 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002383 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002384 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2385 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002386 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002387
Pete Cooper80d21cb2015-06-22 19:35:57 +00002388 if (!Sym) {
2389 // In the case where we parse an expression starting with a '.', we will
2390 // not generate an error, nor will we create a symbol. In this case we
2391 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002392 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002393 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002394
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002395 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002396 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002397 if (NoDeadStrip)
2398 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2399
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002400 return false;
2401}
2402
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002403/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002404/// ::= identifier
2405/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002406bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002407 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002408 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2409 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002410 // handle this as a context dependent token, instead we detect adjacent tokens
2411 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002412 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2413 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002414
Hans Wennborgce69d772013-10-18 20:46:28 +00002415 // Consume the prefix character, and check for a following identifier.
Nirav Dave1180e6892016-06-02 17:15:05 +00002416 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002417 if (Lexer.isNot(AsmToken::Identifier))
2418 return true;
2419
Hans Wennborgce69d772013-10-18 20:46:28 +00002420 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2421 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002422 return true;
2423
2424 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002425 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002426 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Nirav Davefd910412016-06-17 16:06:17 +00002427 Lex(); // Parser Lex to maintain invariants.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002428 return false;
2429 }
2430
Jim Grosbach4b905842013-09-20 23:08:21 +00002431 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002432 return true;
2433
Sean Callanan936b0d32010-01-19 21:44:56 +00002434 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002435
Sean Callanan686ed8d2010-01-19 20:22:31 +00002436 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002437
2438 return false;
2439}
2440
Jim Grosbach4b905842013-09-20 23:08:21 +00002441/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002442/// ::= .equ identifier ',' expression
2443/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002444/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002445bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002446 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002447
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002448 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002449 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002450
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002451 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002452 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002453 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002454
Jim Grosbach4b905842013-09-20 23:08:21 +00002455 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002456}
2457
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002458bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002459 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002460
2461 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002462 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002463 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2464 if (Str[i] != '\\') {
2465 Data += Str[i];
2466 continue;
2467 }
2468
2469 // Recognize escaped characters. Note that this escape semantics currently
2470 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2471 ++i;
2472 if (i == e)
2473 return TokError("unexpected backslash at end of string");
2474
2475 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002476 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002477 // Consume up to three octal characters.
2478 unsigned Value = Str[i] - '0';
2479
Jim Grosbach4b905842013-09-20 23:08:21 +00002480 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002481 ++i;
2482 Value = Value * 8 + (Str[i] - '0');
2483
Jim Grosbach4b905842013-09-20 23:08:21 +00002484 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002485 ++i;
2486 Value = Value * 8 + (Str[i] - '0');
2487 }
2488 }
2489
2490 if (Value > 255)
2491 return TokError("invalid octal escape sequence (out of range)");
2492
Jim Grosbach4b905842013-09-20 23:08:21 +00002493 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002494 continue;
2495 }
2496
2497 // Otherwise recognize individual escapes.
2498 switch (Str[i]) {
2499 default:
2500 // Just reject invalid escape sequences for now.
2501 return TokError("invalid escape sequence (unrecognized character)");
2502
2503 case 'b': Data += '\b'; break;
2504 case 'f': Data += '\f'; break;
2505 case 'n': Data += '\n'; break;
2506 case 'r': Data += '\r'; break;
2507 case 't': Data += '\t'; break;
2508 case '"': Data += '"'; break;
2509 case '\\': Data += '\\'; break;
2510 }
2511 }
2512
2513 return false;
2514}
2515
Jim Grosbach4b905842013-09-20 23:08:21 +00002516/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002517/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002518bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
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 Dunbardd41dcf2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002524 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002525
Daniel Dunbaref668c12009-08-14 18:19:52 +00002526 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002528 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002529
Rafael Espindola64e1af82013-07-02 15:49:13 +00002530 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002531 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002532 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002533
Sean Callanan686ed8d2010-01-19 20:22:31 +00002534 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002535
2536 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002537 break;
2538
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002539 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002540 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002541 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002542 }
2543 }
2544
Sean Callanan686ed8d2010-01-19 20:22:31 +00002545 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002546 return false;
2547}
2548
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002549/// parseDirectiveReloc
2550/// ::= .reloc expression , identifier [ , expression ]
2551bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2552 const MCExpr *Offset;
2553 const MCExpr *Expr = nullptr;
2554
2555 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2556 if (parseExpression(Offset))
2557 return true;
2558
2559 // We can only deal with constant expressions at the moment.
2560 int64_t OffsetValue;
2561 if (!Offset->evaluateAsAbsolute(OffsetValue))
2562 return Error(OffsetLoc, "expression is not a constant value");
2563
David Majnemerce108422016-01-19 23:05:27 +00002564 if (OffsetValue < 0)
2565 return Error(OffsetLoc, "expression is negative");
2566
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002567 if (Lexer.isNot(AsmToken::Comma))
2568 return TokError("expected comma");
Nirav Davefd910412016-06-17 16:06:17 +00002569 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002570
2571 if (Lexer.isNot(AsmToken::Identifier))
2572 return TokError("expected relocation name");
2573 SMLoc NameLoc = Lexer.getTok().getLoc();
2574 StringRef Name = Lexer.getTok().getIdentifier();
Nirav Davefd910412016-06-17 16:06:17 +00002575 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002576
2577 if (Lexer.is(AsmToken::Comma)) {
Nirav Davefd910412016-06-17 16:06:17 +00002578 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002579 SMLoc ExprLoc = Lexer.getLoc();
2580 if (parseExpression(Expr))
2581 return true;
2582
2583 MCValue Value;
2584 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2585 return Error(ExprLoc, "expression must be relocatable");
2586 }
2587
2588 if (Lexer.isNot(AsmToken::EndOfStatement))
2589 return TokError("unexpected token in .reloc directive");
2590
2591 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2592 return Error(NameLoc, "unknown relocation name");
2593
2594 return false;
2595}
2596
Jim Grosbach4b905842013-09-20 23:08:21 +00002597/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002598/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002599bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002600 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002601 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002602
Daniel Dunbara10e5192009-06-24 23:30:00 +00002603 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002604 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002605 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002606 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002607 return true;
2608
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002609 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002610 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2611 assert(Size <= 8 && "Invalid size");
2612 uint64_t IntValue = MCE->getValue();
2613 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2614 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002615 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002616 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002617 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002618
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002619 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002620 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002621
Daniel Dunbara10e5192009-06-24 23:30:00 +00002622 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002623 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002624 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002625 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002626 }
2627 }
2628
Sean Callanan686ed8d2010-01-19 20:22:31 +00002629 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002630 return false;
2631}
2632
David Woodhoused6de0d92014-02-01 16:20:59 +00002633/// ParseDirectiveOctaValue
2634/// ::= .octa [ hexconstant (, hexconstant)* ]
2635bool AsmParser::parseDirectiveOctaValue() {
2636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2637 checkForValidSection();
2638
2639 for (;;) {
2640 if (Lexer.getKind() == AsmToken::Error)
2641 return true;
2642 if (Lexer.getKind() != AsmToken::Integer &&
2643 Lexer.getKind() != AsmToken::BigNum)
2644 return TokError("unknown token in expression");
2645
2646 SMLoc ExprLoc = getLexer().getLoc();
2647 APInt IntValue = getTok().getAPIntVal();
2648 Lex();
2649
2650 uint64_t hi, lo;
2651 if (IntValue.isIntN(64)) {
2652 hi = 0;
2653 lo = IntValue.getZExtValue();
2654 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002655 // It might actually have more than 128 bits, but the top ones are zero.
2656 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002657 lo = IntValue.getLoBits(64).getZExtValue();
2658 } else
2659 return Error(ExprLoc, "literal value out of range for directive");
2660
2661 if (MAI.isLittleEndian()) {
2662 getStreamer().EmitIntValue(lo, 8);
2663 getStreamer().EmitIntValue(hi, 8);
2664 } else {
2665 getStreamer().EmitIntValue(hi, 8);
2666 getStreamer().EmitIntValue(lo, 8);
2667 }
2668
2669 if (getLexer().is(AsmToken::EndOfStatement))
2670 break;
2671
2672 // FIXME: Improve diagnostic.
2673 if (getLexer().isNot(AsmToken::Comma))
2674 return TokError("unexpected token in directive");
2675 Lex();
2676 }
2677 }
2678
2679 Lex();
2680 return false;
2681}
2682
Jim Grosbach4b905842013-09-20 23:08:21 +00002683/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002684/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002685bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002686 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002687 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002688
2689 for (;;) {
2690 // We don't truly support arithmetic on floating point expressions, so we
2691 // have to manually parse unary prefixes.
2692 bool IsNeg = false;
2693 if (getLexer().is(AsmToken::Minus)) {
Nirav Dave1180e6892016-06-02 17:15:05 +00002694 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002695 IsNeg = true;
2696 } else if (getLexer().is(AsmToken::Plus))
Nirav Dave1180e6892016-06-02 17:15:05 +00002697 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002698
Nirav Dave1180e6892016-06-02 17:15:05 +00002699 if (Lexer.is(AsmToken::Error))
2700 return TokError(Lexer.getErr());
2701 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
2702 Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002703 return TokError("unexpected token in directive");
2704
2705 // Convert to an APFloat.
2706 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002707 StringRef IDVal = getTok().getString();
2708 if (getLexer().is(AsmToken::Identifier)) {
2709 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2710 Value = APFloat::getInf(Semantics);
2711 else if (!IDVal.compare_lower("nan"))
2712 Value = APFloat::getNaN(Semantics, false, ~0);
2713 else
2714 return TokError("invalid floating point literal");
2715 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002716 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002717 return TokError("invalid floating point literal");
2718 if (IsNeg)
2719 Value.changeSign();
2720
2721 // Consume the numeric token.
2722 Lex();
2723
2724 // Emit the value as an integer.
2725 APInt AsInt = Value.bitcastToAPInt();
2726 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002727 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002728
Nirav Dave1180e6892016-06-02 17:15:05 +00002729 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002730 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002731
Nirav Dave1180e6892016-06-02 17:15:05 +00002732 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002733 return TokError("unexpected token in directive");
2734 Lex();
2735 }
2736 }
2737
2738 Lex();
2739 return false;
2740}
2741
Jim Grosbach4b905842013-09-20 23:08:21 +00002742/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002743/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002744bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002745 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002746
Petr Hosek67a94a72016-05-28 05:57:48 +00002747 SMLoc NumBytesLoc = Lexer.getLoc();
2748 const MCExpr *NumBytes;
2749 if (parseExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002750 return true;
2751
Rafael Espindolab91bac62010-10-05 19:42:57 +00002752 int64_t Val = 0;
2753 if (getLexer().is(AsmToken::Comma)) {
2754 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002755 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002756 return true;
2757 }
2758
Rafael Espindola922e3f42010-09-16 15:03:59 +00002759 if (getLexer().isNot(AsmToken::EndOfStatement))
2760 return TokError("unexpected token in '.zero' directive");
2761
2762 Lex();
2763
Petr Hosek67a94a72016-05-28 05:57:48 +00002764 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002765
2766 return false;
2767}
2768
Jim Grosbach4b905842013-09-20 23:08:21 +00002769/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002770/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002771bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002772 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002773
Petr Hosek67a94a72016-05-28 05:57:48 +00002774 SMLoc NumValuesLoc = Lexer.getLoc();
2775 const MCExpr *NumValues;
2776 if (parseExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002777 return true;
2778
Roman Divackye33098f2013-09-24 17:44:41 +00002779 int64_t FillSize = 1;
2780 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002781
David Majnemer522d3db2014-02-01 07:19:38 +00002782 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002783 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2784 if (getLexer().isNot(AsmToken::Comma))
2785 return TokError("unexpected token in '.fill' directive");
2786 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002787
David Majnemer522d3db2014-02-01 07:19:38 +00002788 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002789 if (parseAbsoluteExpression(FillSize))
2790 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002791
Roman Divackye33098f2013-09-24 17:44:41 +00002792 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2793 if (getLexer().isNot(AsmToken::Comma))
2794 return TokError("unexpected token in '.fill' directive");
2795 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002796
David Majnemer522d3db2014-02-01 07:19:38 +00002797 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002798 if (parseAbsoluteExpression(FillExpr))
2799 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002800
Roman Divackye33098f2013-09-24 17:44:41 +00002801 if (getLexer().isNot(AsmToken::EndOfStatement))
2802 return TokError("unexpected token in '.fill' directive");
2803
2804 Lex();
2805 }
2806 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002807
David Majnemer522d3db2014-02-01 07:19:38 +00002808 if (FillSize < 0) {
2809 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
Petr Hosek6abd38b2016-05-28 08:20:08 +00002810 return false;
David Majnemer522d3db2014-02-01 07:19:38 +00002811 }
2812 if (FillSize > 8) {
2813 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2814 FillSize = 8;
2815 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002816
David Majnemer522d3db2014-02-01 07:19:38 +00002817 if (!isUInt<32>(FillExpr) && FillSize > 4)
2818 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2819
Petr Hosek67a94a72016-05-28 05:57:48 +00002820 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002821
2822 return false;
2823}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002824
Jim Grosbach4b905842013-09-20 23:08:21 +00002825/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002826/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002827bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002828 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002829
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002830 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002831 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002832 return true;
2833
2834 // Parse optional fill expression.
2835 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002836 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2837 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002838 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002839 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002840
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002841 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002842 return true;
2843
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002844 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002845 return TokError("unexpected token in '.org' directive");
2846 }
2847
Sean Callanan686ed8d2010-01-19 20:22:31 +00002848 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002849 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002850 return false;
2851}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002852
Jim Grosbach4b905842013-09-20 23:08:21 +00002853/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002854/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002855bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002856 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002857
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002858 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002859 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002860 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002861 return true;
2862
2863 SMLoc MaxBytesLoc;
2864 bool HasFillExpr = false;
2865 int64_t FillExpr = 0;
2866 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002867 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2868 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002869 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002870 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002871
2872 // The fill expression can be omitted while specifying a maximum number of
2873 // alignment bytes, e.g:
2874 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002875 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002876 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002877 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002878 return true;
2879 }
2880
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002881 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2882 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002883 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002884 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002885
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002886 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002887 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002888 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002889
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002890 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002891 return TokError("unexpected token in directive");
2892 }
2893 }
2894
Sean Callanan686ed8d2010-01-19 20:22:31 +00002895 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002896
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002897 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002898 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002899
2900 // Compute alignment in bytes.
2901 if (IsPow2) {
2902 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002903 if (Alignment >= 32) {
2904 Error(AlignmentLoc, "invalid alignment value");
2905 Alignment = 31;
2906 }
2907
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002908 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002909 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002910 // Reject alignments that aren't either a power of two or zero,
2911 // for gas compatibility. Alignment of zero is silently rounded
2912 // up to one.
2913 if (Alignment == 0)
2914 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002915 if (!isPowerOf2_64(Alignment))
2916 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002917 }
2918
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002919 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002920 if (MaxBytesLoc.isValid()) {
2921 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002922 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002923 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002924 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002925 }
2926
2927 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002928 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002929 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002930 MaxBytesToFill = 0;
2931 }
2932 }
2933
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002934 // Check whether we should use optimal code alignment for this .align
2935 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002936 const MCSection *Section = getStreamer().getCurrentSection().first;
2937 assert(Section && "must have section to emit alignment");
2938 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002939 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2940 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002941 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002942 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002943 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002944 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2945 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002946 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002947
2948 return false;
2949}
2950
Jim Grosbach4b905842013-09-20 23:08:21 +00002951/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002952/// ::= .file [number] filename
2953/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002954bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002955 // FIXME: I'm not sure what this is.
2956 int64_t FileNumber = -1;
2957 SMLoc FileNumberLoc = getLexer().getLoc();
2958 if (getLexer().is(AsmToken::Integer)) {
2959 FileNumber = getTok().getIntVal();
2960 Lex();
2961
2962 if (FileNumber < 1)
2963 return TokError("file number less than one");
2964 }
2965
2966 if (getLexer().isNot(AsmToken::String))
2967 return TokError("unexpected token in '.file' directive");
2968
2969 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002970 // Allow the strings to have escaped octal character sequence.
2971 std::string Path = getTok().getString();
2972 if (parseEscapedString(Path))
2973 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002974 Lex();
2975
2976 StringRef Directory;
2977 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002978 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002979 if (getLexer().is(AsmToken::String)) {
2980 if (FileNumber == -1)
2981 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002982 if (parseEscapedString(FilenameData))
2983 return true;
2984 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002985 Directory = Path;
2986 Lex();
2987 } else {
2988 Filename = Path;
2989 }
2990
2991 if (getLexer().isNot(AsmToken::EndOfStatement))
2992 return TokError("unexpected token in '.file' directive");
2993
2994 if (FileNumber == -1)
2995 getStreamer().EmitFileDirective(Filename);
2996 else {
David Blaikie22748082016-05-26 00:22:26 +00002997 // If there is -g option as well as debug info from directive file,
2998 // we turn off -g option, directly use the existing debug info instead.
David Blaikiedc3f01e2015-03-09 01:57:13 +00002999 if (getContext().getGenDwarfForAssembly())
David Blaikie22748082016-05-26 00:22:26 +00003000 getContext().setGenDwarfForAssembly(false);
3001 else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
David Blaikiec714ef42014-03-17 01:52:11 +00003002 0)
Eli Bendersky17233942013-01-15 22:59:42 +00003003 Error(FileNumberLoc, "file number already allocated");
3004 }
3005
3006 return false;
3007}
3008
Jim Grosbach4b905842013-09-20 23:08:21 +00003009/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003010/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003011bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00003012 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3013 if (getLexer().isNot(AsmToken::Integer))
3014 return TokError("unexpected token in '.line' directive");
3015
3016 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00003017 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003018 Lex();
3019
3020 // FIXME: Do something with the .line.
3021 }
3022
3023 if (getLexer().isNot(AsmToken::EndOfStatement))
3024 return TokError("unexpected token in '.line' directive");
3025
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3031/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3032/// The first number is a file number, must have been previously assigned with
3033/// a .file directive, the second number is the line number and optionally the
3034/// third number is a column position (zero if not specified). The remaining
3035/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003037 if (getLexer().isNot(AsmToken::Integer))
3038 return TokError("unexpected token in '.loc' directive");
3039 int64_t FileNumber = getTok().getIntVal();
3040 if (FileNumber < 1)
3041 return TokError("file number less than one in '.loc' directive");
3042 if (!getContext().isValidDwarfFileNumber(FileNumber))
3043 return TokError("unassigned file number in '.loc' directive");
3044 Lex();
3045
3046 int64_t LineNumber = 0;
3047 if (getLexer().is(AsmToken::Integer)) {
3048 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003049 if (LineNumber < 0)
3050 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003051 Lex();
3052 }
3053
3054 int64_t ColumnPos = 0;
3055 if (getLexer().is(AsmToken::Integer)) {
3056 ColumnPos = getTok().getIntVal();
3057 if (ColumnPos < 0)
3058 return TokError("column position less than zero in '.loc' directive");
3059 Lex();
3060 }
3061
3062 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3063 unsigned Isa = 0;
3064 int64_t Discriminator = 0;
3065 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3066 for (;;) {
3067 if (getLexer().is(AsmToken::EndOfStatement))
3068 break;
3069
3070 StringRef Name;
3071 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return TokError("unexpected token in '.loc' directive");
3074
3075 if (Name == "basic_block")
3076 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3077 else if (Name == "prologue_end")
3078 Flags |= DWARF2_FLAG_PROLOGUE_END;
3079 else if (Name == "epilogue_begin")
3080 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3081 else if (Name == "is_stmt") {
3082 Loc = getTok().getLoc();
3083 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003084 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003085 return true;
3086 // The expression must be the constant 0 or 1.
3087 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3088 int Value = MCE->getValue();
3089 if (Value == 0)
3090 Flags &= ~DWARF2_FLAG_IS_STMT;
3091 else if (Value == 1)
3092 Flags |= DWARF2_FLAG_IS_STMT;
3093 else
3094 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003095 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003096 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3097 }
Craig Topperf15655b2013-04-22 04:22:40 +00003098 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003099 Loc = getTok().getLoc();
3100 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003101 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
3103 // The expression must be a constant greater or equal to 0.
3104 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3105 int Value = MCE->getValue();
3106 if (Value < 0)
3107 return Error(Loc, "isa number less than zero");
3108 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003109 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return Error(Loc, "isa number not a constant value");
3111 }
Craig Topperf15655b2013-04-22 04:22:40 +00003112 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003113 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003114 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003115 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003116 return Error(Loc, "unknown sub-directive in '.loc' directive");
3117 }
3118
3119 if (getLexer().is(AsmToken::EndOfStatement))
3120 break;
3121 }
3122 }
3123
3124 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3125 Isa, Discriminator, StringRef());
3126
3127 return false;
3128}
3129
Jim Grosbach4b905842013-09-20 23:08:21 +00003130/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003131/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003132bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003133 return TokError("unsupported directive '.stabs'");
3134}
3135
Reid Kleckner2214ed82016-01-29 00:49:42 +00003136/// parseDirectiveCVFile
3137/// ::= .cv_file number filename
3138bool AsmParser::parseDirectiveCVFile() {
3139 SMLoc FileNumberLoc = getLexer().getLoc();
3140 if (getLexer().isNot(AsmToken::Integer))
3141 return TokError("expected file number in '.cv_file' directive");
3142
3143 int64_t FileNumber = getTok().getIntVal();
3144 Lex();
3145
3146 if (FileNumber < 1)
3147 return TokError("file number less than one");
3148
3149 if (getLexer().isNot(AsmToken::String))
3150 return TokError("unexpected token in '.cv_file' directive");
3151
3152 // Usually the directory and filename together, otherwise just the directory.
3153 // Allow the strings to have escaped octal character sequence.
3154 std::string Filename;
3155 if (parseEscapedString(Filename))
3156 return true;
3157 Lex();
3158
3159 if (getLexer().isNot(AsmToken::EndOfStatement))
3160 return TokError("unexpected token in '.cv_file' directive");
3161
3162 if (getStreamer().EmitCVFileDirective(FileNumber, Filename) == 0)
3163 Error(FileNumberLoc, "file number already allocated");
3164
3165 return false;
3166}
3167
3168/// parseDirectiveCVLoc
3169/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3170/// [is_stmt VALUE]
3171/// The first number is a file number, must have been previously assigned with
3172/// a .file directive, the second number is the line number and optionally the
3173/// third number is a column position (zero if not specified). The remaining
3174/// optional items are .loc sub-directives.
3175bool AsmParser::parseDirectiveCVLoc() {
3176 if (getLexer().isNot(AsmToken::Integer))
3177 return TokError("unexpected token in '.cv_loc' directive");
3178
3179 int64_t FunctionId = getTok().getIntVal();
3180 if (FunctionId < 0)
3181 return TokError("function id less than zero in '.cv_loc' directive");
3182 Lex();
3183
3184 int64_t FileNumber = getTok().getIntVal();
3185 if (FileNumber < 1)
3186 return TokError("file number less than one in '.cv_loc' directive");
3187 if (!getContext().isValidCVFileNumber(FileNumber))
3188 return TokError("unassigned file number in '.cv_loc' directive");
3189 Lex();
3190
3191 int64_t LineNumber = 0;
3192 if (getLexer().is(AsmToken::Integer)) {
3193 LineNumber = getTok().getIntVal();
3194 if (LineNumber < 0)
3195 return TokError("line number less than zero in '.cv_loc' directive");
3196 Lex();
3197 }
3198
3199 int64_t ColumnPos = 0;
3200 if (getLexer().is(AsmToken::Integer)) {
3201 ColumnPos = getTok().getIntVal();
3202 if (ColumnPos < 0)
3203 return TokError("column position less than zero in '.cv_loc' directive");
3204 Lex();
3205 }
3206
3207 bool PrologueEnd = false;
3208 uint64_t IsStmt = 0;
3209 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3210 StringRef Name;
3211 SMLoc Loc = getTok().getLoc();
3212 if (parseIdentifier(Name))
3213 return TokError("unexpected token in '.cv_loc' directive");
3214
3215 if (Name == "prologue_end")
3216 PrologueEnd = true;
3217 else if (Name == "is_stmt") {
3218 Loc = getTok().getLoc();
3219 const MCExpr *Value;
3220 if (parseExpression(Value))
3221 return true;
3222 // The expression must be the constant 0 or 1.
3223 IsStmt = ~0ULL;
3224 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3225 IsStmt = MCE->getValue();
3226
3227 if (IsStmt > 1)
3228 return Error(Loc, "is_stmt value not 0 or 1");
3229 } else {
3230 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3231 }
3232 }
3233
3234 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
3235 ColumnPos, PrologueEnd, IsStmt, StringRef());
3236 return false;
3237}
3238
3239/// parseDirectiveCVLinetable
3240/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3241bool AsmParser::parseDirectiveCVLinetable() {
3242 int64_t FunctionId = getTok().getIntVal();
3243 if (FunctionId < 0)
3244 return TokError("function id less than zero in '.cv_linetable' directive");
3245 Lex();
3246
3247 if (Lexer.isNot(AsmToken::Comma))
3248 return TokError("unexpected token in '.cv_linetable' directive");
3249 Lex();
3250
3251 SMLoc Loc = getLexer().getLoc();
3252 StringRef FnStartName;
3253 if (parseIdentifier(FnStartName))
3254 return Error(Loc, "expected identifier in directive");
3255
3256 if (Lexer.isNot(AsmToken::Comma))
3257 return TokError("unexpected token in '.cv_linetable' directive");
3258 Lex();
3259
3260 Loc = getLexer().getLoc();
3261 StringRef FnEndName;
3262 if (parseIdentifier(FnEndName))
3263 return Error(Loc, "expected identifier in directive");
3264
3265 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3266 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3267
3268 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3269 return false;
3270}
3271
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003272/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003273/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003274/// ("contains" SecondaryFunctionId+)?
3275bool AsmParser::parseDirectiveCVInlineLinetable() {
3276 int64_t PrimaryFunctionId = getTok().getIntVal();
3277 if (PrimaryFunctionId < 0)
3278 return TokError(
3279 "function id less than zero in '.cv_inline_linetable' directive");
3280 Lex();
3281
3282 int64_t SourceFileId = getTok().getIntVal();
3283 if (SourceFileId <= 0)
3284 return TokError(
3285 "File id less than zero in '.cv_inline_linetable' directive");
3286 Lex();
3287
3288 int64_t SourceLineNum = getTok().getIntVal();
3289 if (SourceLineNum < 0)
3290 return TokError(
3291 "Line number less than zero in '.cv_inline_linetable' directive");
3292 Lex();
3293
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003294 SMLoc Loc = getLexer().getLoc();
3295 StringRef FnStartName;
3296 if (parseIdentifier(FnStartName))
3297 return Error(Loc, "expected identifier in directive");
3298 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3299
David Majnemerc9911f22016-02-02 19:22:34 +00003300 Loc = getLexer().getLoc();
3301 StringRef FnEndName;
3302 if (parseIdentifier(FnEndName))
3303 return Error(Loc, "expected identifier in directive");
3304 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3305
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003306 SmallVector<unsigned, 8> SecondaryFunctionIds;
3307 if (getLexer().is(AsmToken::Identifier)) {
3308 if (getTok().getIdentifier() != "contains")
3309 return TokError(
3310 "unexpected identifier in '.cv_inline_linetable' directive");
3311 Lex();
3312
3313 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3314 int64_t SecondaryFunctionId = getTok().getIntVal();
3315 if (SecondaryFunctionId < 0)
3316 return TokError(
3317 "function id less than zero in '.cv_inline_linetable' directive");
3318 Lex();
3319
3320 SecondaryFunctionIds.push_back(SecondaryFunctionId);
3321 }
3322 }
3323
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003324 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3325 SourceLineNum, FnStartSym,
David Majnemerc9911f22016-02-02 19:22:34 +00003326 FnEndSym, SecondaryFunctionIds);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003327 return false;
3328}
3329
David Majnemer408b5e62016-02-05 01:55:49 +00003330/// parseDirectiveCVDefRange
3331/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3332bool AsmParser::parseDirectiveCVDefRange() {
3333 SMLoc Loc;
3334 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3335 while (getLexer().is(AsmToken::Identifier)) {
3336 Loc = getLexer().getLoc();
3337 StringRef GapStartName;
3338 if (parseIdentifier(GapStartName))
3339 return Error(Loc, "expected identifier in directive");
3340 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3341
3342 Loc = getLexer().getLoc();
3343 StringRef GapEndName;
3344 if (parseIdentifier(GapEndName))
3345 return Error(Loc, "expected identifier in directive");
3346 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3347
3348 Ranges.push_back({GapStartSym, GapEndSym});
3349 }
3350
3351 if (getLexer().isNot(AsmToken::Comma))
3352 return TokError("unexpected token in directive");
3353 Lex();
3354
3355 std::string FixedSizePortion;
3356 if (parseEscapedString(FixedSizePortion))
3357 return true;
3358 Lex();
3359
3360 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3361 return false;
3362}
3363
Reid Kleckner2214ed82016-01-29 00:49:42 +00003364/// parseDirectiveCVStringTable
3365/// ::= .cv_stringtable
3366bool AsmParser::parseDirectiveCVStringTable() {
3367 getStreamer().EmitCVStringTableDirective();
3368 return false;
3369}
3370
3371/// parseDirectiveCVFileChecksums
3372/// ::= .cv_filechecksums
3373bool AsmParser::parseDirectiveCVFileChecksums() {
3374 getStreamer().EmitCVFileChecksumsDirective();
3375 return false;
3376}
3377
Jim Grosbach4b905842013-09-20 23:08:21 +00003378/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003379/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003380bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003381 StringRef Name;
3382 bool EH = false;
3383 bool Debug = false;
3384
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003385 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003386 return TokError("Expected an identifier");
3387
3388 if (Name == ".eh_frame")
3389 EH = true;
3390 else if (Name == ".debug_frame")
3391 Debug = true;
3392
3393 if (getLexer().is(AsmToken::Comma)) {
3394 Lex();
3395
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003396 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003397 return TokError("Expected an identifier");
3398
3399 if (Name == ".eh_frame")
3400 EH = true;
3401 else if (Name == ".debug_frame")
3402 Debug = true;
3403 }
3404
3405 getStreamer().EmitCFISections(EH, Debug);
3406 return false;
3407}
3408
Jim Grosbach4b905842013-09-20 23:08:21 +00003409/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003410/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003411bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003412 StringRef Simple;
3413 if (getLexer().isNot(AsmToken::EndOfStatement))
3414 if (parseIdentifier(Simple) || Simple != "simple")
3415 return TokError("unexpected token in .cfi_startproc directive");
3416
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003417 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003418 return false;
3419}
3420
Jim Grosbach4b905842013-09-20 23:08:21 +00003421/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003422/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003423bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003424 getStreamer().EmitCFIEndProc();
3425 return false;
3426}
3427
Jim Grosbach4b905842013-09-20 23:08:21 +00003428/// \brief parse register name or number.
3429bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003430 SMLoc DirectiveLoc) {
3431 unsigned RegNo;
3432
3433 if (getLexer().isNot(AsmToken::Integer)) {
3434 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3435 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003436 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003437 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003438 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003439
3440 return false;
3441}
3442
Jim Grosbach4b905842013-09-20 23:08:21 +00003443/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003444/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003445bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003446 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003447 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003448 return true;
3449
3450 if (getLexer().isNot(AsmToken::Comma))
3451 return TokError("unexpected token in directive");
3452 Lex();
3453
3454 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003455 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003456 return true;
3457
3458 getStreamer().EmitCFIDefCfa(Register, Offset);
3459 return false;
3460}
3461
Jim Grosbach4b905842013-09-20 23:08:21 +00003462/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003463/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003464bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003465 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003466 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003467 return true;
3468
3469 getStreamer().EmitCFIDefCfaOffset(Offset);
3470 return false;
3471}
3472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003474/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003475bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003476 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003477 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003478 return true;
3479
3480 if (getLexer().isNot(AsmToken::Comma))
3481 return TokError("unexpected token in directive");
3482 Lex();
3483
3484 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003485 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003486 return true;
3487
3488 getStreamer().EmitCFIRegister(Register1, Register2);
3489 return false;
3490}
3491
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003492/// parseDirectiveCFIWindowSave
3493/// ::= .cfi_window_save
3494bool AsmParser::parseDirectiveCFIWindowSave() {
3495 getStreamer().EmitCFIWindowSave();
3496 return false;
3497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003500/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003502 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003504 return true;
3505
3506 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3507 return false;
3508}
3509
Jim Grosbach4b905842013-09-20 23:08:21 +00003510/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003511/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003512bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003513 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003515 return true;
3516
3517 getStreamer().EmitCFIDefCfaRegister(Register);
3518 return false;
3519}
3520
Jim Grosbach4b905842013-09-20 23:08:21 +00003521/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003522/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003523bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003524 int64_t Register = 0;
3525 int64_t Offset = 0;
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003528 return true;
3529
3530 if (getLexer().isNot(AsmToken::Comma))
3531 return TokError("unexpected token in directive");
3532 Lex();
3533
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003534 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return true;
3536
3537 getStreamer().EmitCFIOffset(Register, Offset);
3538 return false;
3539}
3540
Jim Grosbach4b905842013-09-20 23:08:21 +00003541/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003542/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003543bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003544 int64_t Register = 0;
3545
Jim Grosbach4b905842013-09-20 23:08:21 +00003546 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003547 return true;
3548
3549 if (getLexer().isNot(AsmToken::Comma))
3550 return TokError("unexpected token in directive");
3551 Lex();
3552
3553 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003554 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003555 return true;
3556
3557 getStreamer().EmitCFIRelOffset(Register, Offset);
3558 return false;
3559}
3560
3561static bool isValidEncoding(int64_t Encoding) {
3562 if (Encoding & ~0xff)
3563 return false;
3564
3565 if (Encoding == dwarf::DW_EH_PE_omit)
3566 return true;
3567
3568 const unsigned Format = Encoding & 0xf;
3569 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3570 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3571 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3572 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3573 return false;
3574
3575 const unsigned Application = Encoding & 0x70;
3576 if (Application != dwarf::DW_EH_PE_absptr &&
3577 Application != dwarf::DW_EH_PE_pcrel)
3578 return false;
3579
3580 return true;
3581}
3582
Jim Grosbach4b905842013-09-20 23:08:21 +00003583/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003584/// IsPersonality true for cfi_personality, false for cfi_lsda
3585/// ::= .cfi_personality encoding, [symbol_name]
3586/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003587bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003588 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003589 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003590 return true;
3591 if (Encoding == dwarf::DW_EH_PE_omit)
3592 return false;
3593
3594 if (!isValidEncoding(Encoding))
3595 return TokError("unsupported encoding.");
3596
3597 if (getLexer().isNot(AsmToken::Comma))
3598 return TokError("unexpected token in directive");
3599 Lex();
3600
3601 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003602 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003603 return TokError("expected identifier in directive");
3604
Jim Grosbach6f482002015-05-18 18:43:14 +00003605 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003606
3607 if (IsPersonality)
3608 getStreamer().EmitCFIPersonality(Sym, Encoding);
3609 else
3610 getStreamer().EmitCFILsda(Sym, Encoding);
3611 return false;
3612}
3613
Jim Grosbach4b905842013-09-20 23:08:21 +00003614/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003615/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003616bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003617 getStreamer().EmitCFIRememberState();
3618 return false;
3619}
3620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003622/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003624 getStreamer().EmitCFIRestoreState();
3625 return false;
3626}
3627
Jim Grosbach4b905842013-09-20 23:08:21 +00003628/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003629/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003630bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003631 int64_t Register = 0;
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003634 return true;
3635
3636 getStreamer().EmitCFISameValue(Register);
3637 return false;
3638}
3639
Jim Grosbach4b905842013-09-20 23:08:21 +00003640/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003641/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003642bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003643 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003644 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003645 return true;
3646
3647 getStreamer().EmitCFIRestore(Register);
3648 return false;
3649}
3650
Jim Grosbach4b905842013-09-20 23:08:21 +00003651/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003652/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003653bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003654 std::string Values;
3655 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003656 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003657 return true;
3658
3659 Values.push_back((uint8_t)CurrValue);
3660
3661 while (getLexer().is(AsmToken::Comma)) {
3662 Lex();
3663
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003664 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003665 return true;
3666
3667 Values.push_back((uint8_t)CurrValue);
3668 }
3669
3670 getStreamer().EmitCFIEscape(Values);
3671 return false;
3672}
3673
Jim Grosbach4b905842013-09-20 23:08:21 +00003674/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003675/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003676bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003677 if (getLexer().isNot(AsmToken::EndOfStatement))
3678 return Error(getLexer().getLoc(),
3679 "unexpected token in '.cfi_signal_frame'");
3680
3681 getStreamer().EmitCFISignalFrame();
3682 return false;
3683}
3684
Jim Grosbach4b905842013-09-20 23:08:21 +00003685/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003686/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003687bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003688 int64_t Register = 0;
3689
Jim Grosbach4b905842013-09-20 23:08:21 +00003690 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003691 return true;
3692
3693 getStreamer().EmitCFIUndefined(Register);
3694 return false;
3695}
3696
Jim Grosbach4b905842013-09-20 23:08:21 +00003697/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003698/// ::= .macros_on
3699/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003700bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003701 if (getLexer().isNot(AsmToken::EndOfStatement))
3702 return Error(getLexer().getLoc(),
3703 "unexpected token in '" + Directive + "' directive");
3704
Jim Grosbach4b905842013-09-20 23:08:21 +00003705 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003706 return false;
3707}
3708
Jim Grosbach4b905842013-09-20 23:08:21 +00003709/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003710/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003711bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003712 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003713 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003714 return TokError("expected identifier in '.macro' directive");
3715
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003716 if (getLexer().is(AsmToken::Comma))
3717 Lex();
3718
Eli Bendersky17233942013-01-15 22:59:42 +00003719 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003720 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003721
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003722 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003723 return Error(Lexer.getLoc(),
3724 "Vararg parameter '" + Parameters.back().Name +
3725 "' should be last one in the list of parameters.");
3726
David Majnemer91fc4c22014-01-29 18:57:46 +00003727 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003728 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003729 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003730
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003731 if (Lexer.is(AsmToken::Colon)) {
3732 Lex(); // consume ':'
3733
3734 SMLoc QualLoc;
3735 StringRef Qualifier;
3736
3737 QualLoc = Lexer.getLoc();
3738 if (parseIdentifier(Qualifier))
3739 return Error(QualLoc, "missing parameter qualifier for "
3740 "'" + Parameter.Name + "' in macro '" + Name + "'");
3741
3742 if (Qualifier == "req")
3743 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003744 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003745 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003746 else
3747 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3748 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3749 }
3750
David Majnemer91fc4c22014-01-29 18:57:46 +00003751 if (getLexer().is(AsmToken::Equal)) {
3752 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003753
3754 SMLoc ParamLoc;
3755
3756 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003757 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003758 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003759
3760 if (Parameter.Required)
3761 Warning(ParamLoc, "pointless default value for required parameter "
3762 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003763 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003764
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003765 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003766
3767 if (getLexer().is(AsmToken::Comma))
3768 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003769 }
3770
Nirav Dave1180e6892016-06-02 17:15:05 +00003771 // Eat just the end of statement.
3772 Lexer.Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003773
Nirav Dave1180e6892016-06-02 17:15:05 +00003774 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
Eli Bendersky17233942013-01-15 22:59:42 +00003775 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003776 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003777 // Lex the macro definition.
3778 for (;;) {
Nirav Dave1180e6892016-06-02 17:15:05 +00003779 // Ignore Lexing errors in macros.
3780 while (Lexer.is(AsmToken::Error)) {
3781 Lexer.Lex();
3782 }
3783
Eli Bendersky17233942013-01-15 22:59:42 +00003784 // Check whether we have reached the end of the file.
3785 if (getLexer().is(AsmToken::Eof))
3786 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3787
3788 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003789 if (getLexer().is(AsmToken::Identifier)) {
3790 if (getTok().getIdentifier() == ".endm" ||
3791 getTok().getIdentifier() == ".endmacro") {
3792 if (MacroDepth == 0) { // Outermost macro.
3793 EndToken = getTok();
Nirav Dave1180e6892016-06-02 17:15:05 +00003794 Lexer.Lex();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003795 if (getLexer().isNot(AsmToken::EndOfStatement))
3796 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3797 "' directive");
3798 break;
3799 } else {
3800 // Otherwise we just found the end of an inner macro.
3801 --MacroDepth;
3802 }
3803 } else if (getTok().getIdentifier() == ".macro") {
3804 // We allow nested macros. Those aren't instantiated until the outermost
3805 // macro is expanded so just ignore them for now.
3806 ++MacroDepth;
3807 }
Eli Bendersky17233942013-01-15 22:59:42 +00003808 }
3809
3810 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003811 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003812 }
3813
Jim Grosbach4b905842013-09-20 23:08:21 +00003814 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003815 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3816 }
3817
3818 const char *BodyStart = StartToken.getLoc().getPointer();
3819 const char *BodyEnd = EndToken.getLoc().getPointer();
3820 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003821 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003822 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003823 return false;
3824}
3825
Jim Grosbach4b905842013-09-20 23:08:21 +00003826/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003827///
3828/// With the support added for named parameters there may be code out there that
3829/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003830/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003831/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003832/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003833/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3834/// warning that the positional parameter found in body which have no effect.
3835/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003836/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003837/// intended or change the macro to use the named parameters. It is possible
3838/// this warning will trigger when the none of the named parameters are used
3839/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003840void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003841 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003842 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003843 // If this macro is not defined with named parameters the warning we are
3844 // checking for here doesn't apply.
3845 unsigned NParameters = Parameters.size();
3846 if (NParameters == 0)
3847 return;
3848
3849 bool NamedParametersFound = false;
3850 bool PositionalParametersFound = false;
3851
3852 // Look at the body of the macro for use of both the named parameters and what
3853 // are likely to be positional parameters. This is what expandMacro() is
3854 // doing when it finds the parameters in the body.
3855 while (!Body.empty()) {
3856 // Scan for the next possible parameter.
3857 std::size_t End = Body.size(), Pos = 0;
3858 for (; Pos != End; ++Pos) {
3859 // Check for a substitution or escape.
3860 // This macro is defined with parameters, look for \foo, \bar, etc.
3861 if (Body[Pos] == '\\' && Pos + 1 != End)
3862 break;
3863
3864 // This macro should have parameters, but look for $0, $1, ..., $n too.
3865 if (Body[Pos] != '$' || Pos + 1 == End)
3866 continue;
3867 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003868 if (Next == '$' || Next == 'n' ||
3869 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003870 break;
3871 }
3872
3873 // Check if we reached the end.
3874 if (Pos == End)
3875 break;
3876
3877 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003878 switch (Body[Pos + 1]) {
3879 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003880 case '$':
3881 break;
3882
Jim Grosbach4b905842013-09-20 23:08:21 +00003883 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003884 case 'n':
3885 PositionalParametersFound = true;
3886 break;
3887
Jim Grosbach4b905842013-09-20 23:08:21 +00003888 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003889 default: {
3890 PositionalParametersFound = true;
3891 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003892 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003893 }
3894 Pos += 2;
3895 } else {
3896 unsigned I = Pos + 1;
3897 while (isIdentifierChar(Body[I]) && I + 1 != End)
3898 ++I;
3899
Jim Grosbach4b905842013-09-20 23:08:21 +00003900 const char *Begin = Body.data() + Pos + 1;
3901 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003902 unsigned Index = 0;
3903 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003904 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003905 break;
3906
3907 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003908 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3909 Pos += 3;
3910 else {
3911 Pos = I;
3912 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003913 } else {
3914 NamedParametersFound = true;
3915 Pos += 1 + Argument.size();
3916 }
3917 }
3918 // Update the scan point.
3919 Body = Body.substr(Pos);
3920 }
3921
3922 if (!NamedParametersFound && PositionalParametersFound)
3923 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3924 "used in macro body, possible positional parameter "
3925 "found in body which will have no effect");
3926}
3927
Nico Weber155dccd12014-07-24 17:08:39 +00003928/// parseDirectiveExitMacro
3929/// ::= .exitm
3930bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3931 if (getLexer().isNot(AsmToken::EndOfStatement))
3932 return TokError("unexpected token in '" + Directive + "' directive");
3933
3934 if (!isInsideMacroInstantiation())
3935 return TokError("unexpected '" + Directive + "' in file, "
3936 "no current macro definition");
3937
3938 // Exit all conditionals that are active in the current macro.
3939 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3940 TheCondState = TheCondStack.back();
3941 TheCondStack.pop_back();
3942 }
3943
3944 handleMacroExit();
3945 return false;
3946}
3947
Jim Grosbach4b905842013-09-20 23:08:21 +00003948/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003949/// ::= .endm
3950/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003951bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003952 if (getLexer().isNot(AsmToken::EndOfStatement))
3953 return TokError("unexpected token in '" + Directive + "' directive");
3954
3955 // If we are inside a macro instantiation, terminate the current
3956 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003957 if (isInsideMacroInstantiation()) {
3958 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003959 return false;
3960 }
3961
3962 // Otherwise, this .endmacro is a stray entry in the file; well formed
3963 // .endmacro directives are handled during the macro definition parsing.
3964 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003965 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003966}
3967
Jim Grosbach4b905842013-09-20 23:08:21 +00003968/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003969/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003970bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003971 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003972 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003973 return TokError("expected identifier in '.purgem' directive");
3974
3975 if (getLexer().isNot(AsmToken::EndOfStatement))
3976 return TokError("unexpected token in '.purgem' directive");
3977
Jim Grosbach4b905842013-09-20 23:08:21 +00003978 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003979 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3980
Jim Grosbach4b905842013-09-20 23:08:21 +00003981 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003982 return false;
3983}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003984
Jim Grosbach4b905842013-09-20 23:08:21 +00003985/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003986/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003987bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003988 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003989
3990 // Expect a single argument: an expression that evaluates to a constant
3991 // in the inclusive range 0-30.
3992 SMLoc ExprLoc = getLexer().getLoc();
3993 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003994 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003995 return true;
3996 else if (getLexer().isNot(AsmToken::EndOfStatement))
3997 return TokError("unexpected token after expression in"
3998 " '.bundle_align_mode' directive");
3999 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
4000 return Error(ExprLoc,
4001 "invalid bundle alignment size (expected between 0 and 30)");
4002
4003 Lex();
4004
4005 // Because of AlignSizePow2's verified range we can safely truncate it to
4006 // unsigned.
4007 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4008 return false;
4009}
4010
Jim Grosbach4b905842013-09-20 23:08:21 +00004011/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00004012/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00004013bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004014 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00004015 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004016
Eli Bendersky802b6282013-01-07 21:51:08 +00004017 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4018 StringRef Option;
4019 SMLoc Loc = getTok().getLoc();
4020 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004021 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004022
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004023 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00004024 return Error(Loc, kInvalidOptionError);
4025
4026 if (Option != "align_to_end")
4027 return Error(Loc, kInvalidOptionError);
4028 else if (getLexer().isNot(AsmToken::EndOfStatement))
4029 return Error(Loc,
4030 "unexpected token after '.bundle_lock' directive option");
4031 AlignToEnd = true;
4032 }
4033
Eli Benderskyf483ff92012-12-20 19:05:53 +00004034 Lex();
4035
Eli Bendersky802b6282013-01-07 21:51:08 +00004036 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004037 return false;
4038}
4039
Jim Grosbach4b905842013-09-20 23:08:21 +00004040/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004041/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004042bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004043 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004044
4045 if (getLexer().isNot(AsmToken::EndOfStatement))
4046 return TokError("unexpected token in '.bundle_unlock' directive");
4047 Lex();
4048
4049 getStreamer().EmitBundleUnlock();
4050 return false;
4051}
4052
Jim Grosbach4b905842013-09-20 23:08:21 +00004053/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004054/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004055bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004056 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004057
Petr Hosek67a94a72016-05-28 05:57:48 +00004058 SMLoc NumBytesLoc = Lexer.getLoc();
4059 const MCExpr *NumBytes;
4060 if (parseExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004061 return true;
4062
4063 int64_t FillExpr = 0;
4064 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4065 if (getLexer().isNot(AsmToken::Comma))
4066 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4067 Lex();
4068
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004069 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004070 return true;
4071
4072 if (getLexer().isNot(AsmToken::EndOfStatement))
4073 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4074 }
4075
4076 Lex();
4077
Eli Bendersky17233942013-01-15 22:59:42 +00004078 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Petr Hosek67a94a72016-05-28 05:57:48 +00004079 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
Eli Bendersky17233942013-01-15 22:59:42 +00004080
4081 return false;
4082}
4083
Jim Grosbach4b905842013-09-20 23:08:21 +00004084/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004085/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004086bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004087 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004088 const MCExpr *Value;
4089
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004090 for (;;) {
4091 if (parseExpression(Value))
4092 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004093
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004094 if (Signed)
4095 getStreamer().EmitSLEB128Value(Value);
4096 else
4097 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004098
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004099 if (getLexer().is(AsmToken::EndOfStatement))
4100 break;
4101
4102 if (getLexer().isNot(AsmToken::Comma))
4103 return TokError("unexpected token in directive");
4104 Lex();
4105 }
Eli Bendersky17233942013-01-15 22:59:42 +00004106
4107 return false;
4108}
4109
Jim Grosbach4b905842013-09-20 23:08:21 +00004110/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004111/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004112bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004113 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00004114 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004115 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004116 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004117
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004118 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004119 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004120
Jim Grosbach6f482002015-05-18 18:43:14 +00004121 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004122
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004123 // Assembler local symbols don't make any sense here. Complain loudly.
4124 if (Sym->isTemporary())
4125 return Error(Loc, "non-local symbol required in directive");
4126
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004127 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4128 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004129
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004130 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004131 break;
4132
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004133 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004134 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004135 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00004136 }
4137 }
4138
Sean Callanan686ed8d2010-01-19 20:22:31 +00004139 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004140 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004141}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004142
Jim Grosbach4b905842013-09-20 23:08:21 +00004143/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004144/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004145bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004146 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004147
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004148 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004149 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004150 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004151 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004152
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004153 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004154 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004155
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004156 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004157 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004158 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004159
4160 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004161 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004162 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004163 return true;
4164
4165 int64_t Pow2Alignment = 0;
4166 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004167 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004168 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004169 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004170 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004171 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004172
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004173 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4174 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004175 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4176
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004177 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004178 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4179 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004180 if (!isPowerOf2_64(Pow2Alignment))
4181 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4182 Pow2Alignment = Log2_64(Pow2Alignment);
4183 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004184 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004185
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004186 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004187 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004188
Sean Callanan686ed8d2010-01-19 20:22:31 +00004189 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004190
Chris Lattner28ad7542009-07-09 17:25:12 +00004191 // NOTE: a size of zero for a .comm should create a undefined symbol
4192 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004193 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004194 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004195 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004196
Eric Christopherbc818852010-05-14 01:38:54 +00004197 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004198 // may internally end up wanting an alignment in bytes.
4199 // FIXME: Diagnose overflow.
4200 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004201 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004202 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004203
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004204 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004205 return Error(IDLoc, "invalid symbol redefinition");
4206
Chris Lattner28ad7542009-07-09 17:25:12 +00004207 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004208 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004209 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004210 return false;
4211 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004212
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004213 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004214 return false;
4215}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004216
Jim Grosbach4b905842013-09-20 23:08:21 +00004217/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004218/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004219bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004220 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004221 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004222
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004223 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004224 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00004225 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004226
Sean Callanan686ed8d2010-01-19 20:22:31 +00004227 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00004228
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004229 if (Str.empty())
4230 Error(Loc, ".abort detected. Assembly stopping.");
4231 else
4232 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004233 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004234
4235 return false;
4236}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004237
Jim Grosbach4b905842013-09-20 23:08:21 +00004238/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004239/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004240bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004241 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004242 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004243
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004244 // Allow the strings to have escaped octal character sequence.
4245 std::string Filename;
4246 if (parseEscapedString(Filename))
4247 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004248 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00004249 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004250
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004251 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004252 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004253
Chris Lattner693fbb82009-07-16 06:14:39 +00004254 // Attempt to switch the lexer to the included file before consuming the end
4255 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00004256 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00004257 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00004258 return true;
4259 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004260
4261 return false;
4262}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004263
Jim Grosbach4b905842013-09-20 23:08:21 +00004264/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004265/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004266bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004267 if (getLexer().isNot(AsmToken::String))
4268 return TokError("expected string in '.incbin' directive");
4269
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004270 // Allow the strings to have escaped octal character sequence.
4271 std::string Filename;
4272 if (parseEscapedString(Filename))
4273 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00004274 SMLoc IncbinLoc = getLexer().getLoc();
4275 Lex();
4276
4277 if (getLexer().isNot(AsmToken::EndOfStatement))
4278 return TokError("unexpected token in '.incbin' directive");
4279
Kevin Enderby109f25c2011-12-14 21:47:48 +00004280 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00004281 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004282 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
4283 return true;
4284 }
4285
4286 return false;
4287}
4288
Jim Grosbach4b905842013-09-20 23:08:21 +00004289/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004290/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4291bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004292 TheCondStack.push_back(TheCondState);
4293 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004294 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004295 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004296 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004297 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004298 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004299 return true;
4300
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004301 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004302 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004303
Sean Callanan686ed8d2010-01-19 20:22:31 +00004304 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004305
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004306 switch (DirKind) {
4307 default:
4308 llvm_unreachable("unsupported directive");
4309 case DK_IF:
4310 case DK_IFNE:
4311 break;
4312 case DK_IFEQ:
4313 ExprValue = ExprValue == 0;
4314 break;
4315 case DK_IFGE:
4316 ExprValue = ExprValue >= 0;
4317 break;
4318 case DK_IFGT:
4319 ExprValue = ExprValue > 0;
4320 break;
4321 case DK_IFLE:
4322 ExprValue = ExprValue <= 0;
4323 break;
4324 case DK_IFLT:
4325 ExprValue = ExprValue < 0;
4326 break;
4327 }
4328
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004329 TheCondState.CondMet = ExprValue;
4330 TheCondState.Ignore = !TheCondState.CondMet;
4331 }
4332
4333 return false;
4334}
4335
Jim Grosbach4b905842013-09-20 23:08:21 +00004336/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004337/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004338bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004339 TheCondStack.push_back(TheCondState);
4340 TheCondState.TheCond = AsmCond::IfCond;
4341
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004342 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004343 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004344 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004345 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004346
4347 if (getLexer().isNot(AsmToken::EndOfStatement))
4348 return TokError("unexpected token in '.ifb' directive");
4349
4350 Lex();
4351
4352 TheCondState.CondMet = ExpectBlank == Str.empty();
4353 TheCondState.Ignore = !TheCondState.CondMet;
4354 }
4355
4356 return false;
4357}
4358
Jim Grosbach4b905842013-09-20 23:08:21 +00004359/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004360/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004361/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004362bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004363 TheCondStack.push_back(TheCondState);
4364 TheCondState.TheCond = AsmCond::IfCond;
4365
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004366 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004367 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004368 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004369 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004370
4371 if (getLexer().isNot(AsmToken::Comma))
4372 return TokError("unexpected token in '.ifc' directive");
4373
4374 Lex();
4375
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004376 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004377
4378 if (getLexer().isNot(AsmToken::EndOfStatement))
4379 return TokError("unexpected token in '.ifc' directive");
4380
4381 Lex();
4382
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004383 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004384 TheCondState.Ignore = !TheCondState.CondMet;
4385 }
4386
4387 return false;
4388}
4389
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004390/// parseDirectiveIfeqs
4391/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004392bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004393 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004394 if (ExpectEqual)
4395 TokError("expected string parameter for '.ifeqs' directive");
4396 else
4397 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004398 eatToEndOfStatement();
4399 return true;
4400 }
4401
4402 StringRef String1 = getTok().getStringContents();
4403 Lex();
4404
4405 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004406 if (ExpectEqual)
4407 TokError("expected comma after first string for '.ifeqs' directive");
4408 else
4409 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004410 eatToEndOfStatement();
4411 return true;
4412 }
4413
4414 Lex();
4415
4416 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004417 if (ExpectEqual)
4418 TokError("expected string parameter for '.ifeqs' directive");
4419 else
4420 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004421 eatToEndOfStatement();
4422 return true;
4423 }
4424
4425 StringRef String2 = getTok().getStringContents();
4426 Lex();
4427
4428 TheCondStack.push_back(TheCondState);
4429 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004430 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004431 TheCondState.Ignore = !TheCondState.CondMet;
4432
4433 return false;
4434}
4435
Jim Grosbach4b905842013-09-20 23:08:21 +00004436/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004437/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004438bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004439 StringRef Name;
4440 TheCondStack.push_back(TheCondState);
4441 TheCondState.TheCond = AsmCond::IfCond;
4442
4443 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004444 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004445 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004446 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004447 return TokError("expected identifier after '.ifdef'");
4448
4449 Lex();
4450
Jim Grosbach6f482002015-05-18 18:43:14 +00004451 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004452
4453 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004454 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004455 else
Craig Topper353eda42014-04-24 06:44:33 +00004456 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004457 TheCondState.Ignore = !TheCondState.CondMet;
4458 }
4459
4460 return false;
4461}
4462
Jim Grosbach4b905842013-09-20 23:08:21 +00004463/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004464/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004465bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004466 if (TheCondState.TheCond != AsmCond::IfCond &&
4467 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004468 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4469 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004470 TheCondState.TheCond = AsmCond::ElseIfCond;
4471
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004472 bool LastIgnoreState = false;
4473 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004474 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004475 if (LastIgnoreState || TheCondState.CondMet) {
4476 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004477 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004478 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004479 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004480 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004481 return true;
4482
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004483 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004484 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004485
Sean Callanan686ed8d2010-01-19 20:22:31 +00004486 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004487 TheCondState.CondMet = ExprValue;
4488 TheCondState.Ignore = !TheCondState.CondMet;
4489 }
4490
4491 return false;
4492}
4493
Jim Grosbach4b905842013-09-20 23:08:21 +00004494/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004495/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004496bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004497 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004498 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004499
Sean Callanan686ed8d2010-01-19 20:22:31 +00004500 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004501
4502 if (TheCondState.TheCond != AsmCond::IfCond &&
4503 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004504 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4505 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004506 TheCondState.TheCond = AsmCond::ElseCond;
4507 bool LastIgnoreState = false;
4508 if (!TheCondStack.empty())
4509 LastIgnoreState = TheCondStack.back().Ignore;
4510 if (LastIgnoreState || TheCondState.CondMet)
4511 TheCondState.Ignore = true;
4512 else
4513 TheCondState.Ignore = false;
4514
4515 return false;
4516}
4517
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004518/// parseDirectiveEnd
4519/// ::= .end
4520bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4521 if (getLexer().isNot(AsmToken::EndOfStatement))
4522 return TokError("unexpected token in '.end' directive");
4523
4524 Lex();
4525
4526 while (Lexer.isNot(AsmToken::Eof))
4527 Lex();
4528
4529 return false;
4530}
4531
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004532/// parseDirectiveError
4533/// ::= .err
4534/// ::= .error [string]
4535bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4536 if (!TheCondStack.empty()) {
4537 if (TheCondStack.back().Ignore) {
4538 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004539 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004540 }
4541 }
4542
4543 if (!WithMessage)
4544 return Error(L, ".err encountered");
4545
4546 StringRef Message = ".error directive invoked in source file";
4547 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4548 if (Lexer.isNot(AsmToken::String)) {
4549 TokError(".error argument must be a string");
4550 eatToEndOfStatement();
4551 return true;
4552 }
4553
4554 Message = getTok().getStringContents();
4555 Lex();
4556 }
4557
4558 Error(L, Message);
4559 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004560}
4561
Nico Weber404012b2014-07-24 16:26:06 +00004562/// parseDirectiveWarning
4563/// ::= .warning [string]
4564bool AsmParser::parseDirectiveWarning(SMLoc L) {
4565 if (!TheCondStack.empty()) {
4566 if (TheCondStack.back().Ignore) {
4567 eatToEndOfStatement();
4568 return false;
4569 }
4570 }
4571
4572 StringRef Message = ".warning directive invoked in source file";
4573 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4574 if (Lexer.isNot(AsmToken::String)) {
4575 TokError(".warning argument must be a string");
4576 eatToEndOfStatement();
4577 return true;
4578 }
4579
4580 Message = getTok().getStringContents();
4581 Lex();
4582 }
4583
4584 Warning(L, Message);
4585 return false;
4586}
4587
Jim Grosbach4b905842013-09-20 23:08:21 +00004588/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004589/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004590bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004591 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004592 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004593
Sean Callanan686ed8d2010-01-19 20:22:31 +00004594 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004595
Jim Grosbach4b905842013-09-20 23:08:21 +00004596 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004597 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4598 ".else");
4599 if (!TheCondStack.empty()) {
4600 TheCondState = TheCondStack.back();
4601 TheCondStack.pop_back();
4602 }
4603
4604 return false;
4605}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004606
Eli Bendersky17233942013-01-15 22:59:42 +00004607void AsmParser::initializeDirectiveKindMap() {
4608 DirectiveKindMap[".set"] = DK_SET;
4609 DirectiveKindMap[".equ"] = DK_EQU;
4610 DirectiveKindMap[".equiv"] = DK_EQUIV;
4611 DirectiveKindMap[".ascii"] = DK_ASCII;
4612 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4613 DirectiveKindMap[".string"] = DK_STRING;
4614 DirectiveKindMap[".byte"] = DK_BYTE;
4615 DirectiveKindMap[".short"] = DK_SHORT;
4616 DirectiveKindMap[".value"] = DK_VALUE;
4617 DirectiveKindMap[".2byte"] = DK_2BYTE;
4618 DirectiveKindMap[".long"] = DK_LONG;
4619 DirectiveKindMap[".int"] = DK_INT;
4620 DirectiveKindMap[".4byte"] = DK_4BYTE;
4621 DirectiveKindMap[".quad"] = DK_QUAD;
4622 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004623 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004624 DirectiveKindMap[".single"] = DK_SINGLE;
4625 DirectiveKindMap[".float"] = DK_FLOAT;
4626 DirectiveKindMap[".double"] = DK_DOUBLE;
4627 DirectiveKindMap[".align"] = DK_ALIGN;
4628 DirectiveKindMap[".align32"] = DK_ALIGN32;
4629 DirectiveKindMap[".balign"] = DK_BALIGN;
4630 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4631 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4632 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4633 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4634 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4635 DirectiveKindMap[".org"] = DK_ORG;
4636 DirectiveKindMap[".fill"] = DK_FILL;
4637 DirectiveKindMap[".zero"] = DK_ZERO;
4638 DirectiveKindMap[".extern"] = DK_EXTERN;
4639 DirectiveKindMap[".globl"] = DK_GLOBL;
4640 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004641 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4642 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4643 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4644 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4645 DirectiveKindMap[".reference"] = DK_REFERENCE;
4646 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4647 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4648 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4649 DirectiveKindMap[".comm"] = DK_COMM;
4650 DirectiveKindMap[".common"] = DK_COMMON;
4651 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4652 DirectiveKindMap[".abort"] = DK_ABORT;
4653 DirectiveKindMap[".include"] = DK_INCLUDE;
4654 DirectiveKindMap[".incbin"] = DK_INCBIN;
4655 DirectiveKindMap[".code16"] = DK_CODE16;
4656 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4657 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004658 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004659 DirectiveKindMap[".irp"] = DK_IRP;
4660 DirectiveKindMap[".irpc"] = DK_IRPC;
4661 DirectiveKindMap[".endr"] = DK_ENDR;
4662 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4663 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4664 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4665 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004666 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4667 DirectiveKindMap[".ifge"] = DK_IFGE;
4668 DirectiveKindMap[".ifgt"] = DK_IFGT;
4669 DirectiveKindMap[".ifle"] = DK_IFLE;
4670 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004671 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004672 DirectiveKindMap[".ifb"] = DK_IFB;
4673 DirectiveKindMap[".ifnb"] = DK_IFNB;
4674 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004675 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004676 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004677 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004678 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4679 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4680 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4681 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4682 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004683 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004684 DirectiveKindMap[".endif"] = DK_ENDIF;
4685 DirectiveKindMap[".skip"] = DK_SKIP;
4686 DirectiveKindMap[".space"] = DK_SPACE;
4687 DirectiveKindMap[".file"] = DK_FILE;
4688 DirectiveKindMap[".line"] = DK_LINE;
4689 DirectiveKindMap[".loc"] = DK_LOC;
4690 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004691 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
4692 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4693 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004694 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
David Majnemer408b5e62016-02-05 01:55:49 +00004695 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004696 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4697 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004698 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4699 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4700 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4701 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4702 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4703 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4704 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4705 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4706 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4707 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4708 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4709 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4710 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4711 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4712 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4713 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4714 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4715 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4716 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4717 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4718 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004719 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004720 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4721 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4722 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004723 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004724 DirectiveKindMap[".endm"] = DK_ENDM;
4725 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4726 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004727 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004728 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004729 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004730 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004731}
4732
Jim Grosbach4b905842013-09-20 23:08:21 +00004733MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004734 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004735
Rafael Espindola34b9c512012-06-03 23:57:14 +00004736 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004737 for (;;) {
4738 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004739 if (getLexer().is(AsmToken::Eof)) {
4740 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004741 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004742 }
4743
Rafael Espindola34b9c512012-06-03 23:57:14 +00004744 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004745 (getTok().getIdentifier() == ".rept" ||
4746 getTok().getIdentifier() == ".irp" ||
4747 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004748 ++NestLevel;
4749 }
4750
4751 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004752 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004753 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004754 EndToken = getTok();
4755 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004756 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4757 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004758 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004759 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004760 break;
4761 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004762 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004763 }
4764
Rafael Espindola34b9c512012-06-03 23:57:14 +00004765 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004766 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004767 }
4768
4769 const char *BodyStart = StartToken.getLoc().getPointer();
4770 const char *BodyEnd = EndToken.getLoc().getPointer();
4771 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4772
Rafael Espindola34b9c512012-06-03 23:57:14 +00004773 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004774 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004775 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004776}
4777
Jim Grosbach4b905842013-09-20 23:08:21 +00004778void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004779 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004780 OS << ".endr\n";
4781
Rafael Espindola3560ff22014-08-27 20:03:13 +00004782 std::unique_ptr<MemoryBuffer> Instantiation =
4783 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004784
Rafael Espindola34b9c512012-06-03 23:57:14 +00004785 // Create the macro instantiation object and add to the current macro
4786 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004787 MacroInstantiation *MI = new MacroInstantiation(
4788 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004789 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004790
Rafael Espindola34b9c512012-06-03 23:57:14 +00004791 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004792 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004793 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004794 Lex();
4795}
4796
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004797/// parseDirectiveRept
4798/// ::= .rep | .rept count
4799bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004800 const MCExpr *CountExpr;
4801 SMLoc CountLoc = getTok().getLoc();
4802 if (parseExpression(CountExpr))
4803 return true;
4804
Rafael Espindola34b9c512012-06-03 23:57:14 +00004805 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004806 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004807 eatToEndOfStatement();
4808 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4809 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004810
4811 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004812 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004813
4814 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004815 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004816
4817 // Eat the end of statement.
4818 Lex();
4819
4820 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004821 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004822 if (!M)
4823 return true;
4824
4825 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4826 // to hold the macro body with substitutions.
4827 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004828 raw_svector_ostream OS(Buf);
4829 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004830 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4831 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004832 return true;
4833 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004834 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004835
4836 return false;
4837}
4838
Jim Grosbach4b905842013-09-20 23:08:21 +00004839/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004840/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004841bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004842 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004843
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004844 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004845 return TokError("expected identifier in '.irp' directive");
4846
Rafael Espindola768b41c2012-06-15 14:02:34 +00004847 if (Lexer.isNot(AsmToken::Comma))
4848 return TokError("expected comma in '.irp' directive");
4849
4850 Lex();
4851
Eli Bendersky38274122013-01-14 23:22:36 +00004852 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004853 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004854 return true;
4855
4856 // Eat the end of statement.
4857 Lex();
4858
4859 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004860 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004861 if (!M)
4862 return true;
4863
4864 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4865 // to hold the macro body with substitutions.
4866 SmallString<256> Buf;
4867 raw_svector_ostream OS(Buf);
4868
Craig Topper84008482015-10-10 05:38:14 +00004869 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004870 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4871 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004872 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004873 return true;
4874 }
4875
Jim Grosbach4b905842013-09-20 23:08:21 +00004876 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004877
4878 return false;
4879}
4880
Jim Grosbach4b905842013-09-20 23:08:21 +00004881/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004882/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004883bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004884 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004885
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004886 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004887 return TokError("expected identifier in '.irpc' directive");
4888
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004889 if (Lexer.isNot(AsmToken::Comma))
4890 return TokError("expected comma in '.irpc' directive");
4891
4892 Lex();
4893
Eli Bendersky38274122013-01-14 23:22:36 +00004894 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004895 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004896 return true;
4897
4898 if (A.size() != 1 || A.front().size() != 1)
4899 return TokError("unexpected token in '.irpc' directive");
4900
4901 // Eat the end of statement.
4902 Lex();
4903
4904 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004905 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004906 if (!M)
4907 return true;
4908
4909 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4910 // to hold the macro body with substitutions.
4911 SmallString<256> Buf;
4912 raw_svector_ostream OS(Buf);
4913
4914 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004915 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004916 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004917 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004918
Toma Tabacu217116e2015-04-27 10:50:29 +00004919 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4920 // This is undocumented, but GAS seems to support it.
4921 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004922 return true;
4923 }
4924
Jim Grosbach4b905842013-09-20 23:08:21 +00004925 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004926
4927 return false;
4928}
4929
Jim Grosbach4b905842013-09-20 23:08:21 +00004930bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004931 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004932 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004933
4934 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004935 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004936 assert(getLexer().is(AsmToken::EndOfStatement));
4937
Jim Grosbach4b905842013-09-20 23:08:21 +00004938 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004939 return false;
4940}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004941
Jim Grosbach4b905842013-09-20 23:08:21 +00004942bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004943 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004944 const MCExpr *Value;
4945 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004946 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004947 return true;
4948 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4949 if (!MCE)
4950 return Error(ExprLoc, "unexpected expression in _emit");
4951 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004952 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004953 return Error(ExprLoc, "literal value out of range for directive");
4954
Craig Topper7d5b2312015-10-10 05:25:02 +00004955 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004956 return false;
4957}
4958
Jim Grosbach4b905842013-09-20 23:08:21 +00004959bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004960 const MCExpr *Value;
4961 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004962 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004963 return true;
4964 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4965 if (!MCE)
4966 return Error(ExprLoc, "unexpected expression in align");
4967 uint64_t IntValue = MCE->getValue();
4968 if (!isPowerOf2_64(IntValue))
4969 return Error(ExprLoc, "literal value not a power of two greater then zero");
4970
Craig Topper7d5b2312015-10-10 05:25:02 +00004971 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004972 return false;
4973}
4974
Chad Rosierf43fcf52013-02-13 21:27:17 +00004975// We are comparing pointers, but the pointers are relative to a single string.
4976// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004977static int rewritesSort(const AsmRewrite *AsmRewriteA,
4978 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004979 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4980 return -1;
4981 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4982 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004983
Chad Rosierfce4fab2013-04-08 17:43:47 +00004984 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4985 // rewrite to the same location. Make sure the SizeDirective rewrite is
4986 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4987 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004988 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4989 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004990 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004991
Jim Grosbach4b905842013-09-20 23:08:21 +00004992 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4993 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004994 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004995 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004996}
4997
Jim Grosbach4b905842013-09-20 23:08:21 +00004998bool AsmParser::parseMSInlineAsm(
4999 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
5000 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
5001 SmallVectorImpl<std::string> &Constraints,
5002 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5003 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00005004 SmallVector<void *, 4> InputDecls;
5005 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00005006 SmallVector<bool, 4> InputDeclsAddressOf;
5007 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00005008 SmallVector<std::string, 4> InputConstraints;
5009 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005010 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00005011
Benjamin Kramer1a136112013-02-15 20:37:21 +00005012 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005013
5014 // Prime the lexer.
5015 Lex();
5016
5017 // While we have input, parse each statement.
5018 unsigned InputIdx = 0;
5019 unsigned OutputIdx = 0;
5020 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005021 // Parse curly braces marking block start/end
5022 if (parseCurlyBlockScope(AsmStrRewrites))
5023 continue;
5024
Eli Friedman0f4871d2012-10-22 23:58:19 +00005025 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005026 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00005027 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00005028
Chad Rosier149e8e02012-12-12 22:45:52 +00005029 if (Info.ParseError)
5030 return true;
5031
Benjamin Kramer1a136112013-02-15 20:37:21 +00005032 if (Info.Opcode == ~0U)
5033 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005034
Benjamin Kramer1a136112013-02-15 20:37:21 +00005035 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005036
Benjamin Kramer1a136112013-02-15 20:37:21 +00005037 // Build the list of clobbers, outputs and inputs.
5038 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005039 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005040
Benjamin Kramer1a136112013-02-15 20:37:21 +00005041 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005042 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005043 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005044
Benjamin Kramer1a136112013-02-15 20:37:21 +00005045 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005046 if (Operand.isReg() && !Operand.needAddressOf() &&
5047 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005048 unsigned NumDefs = Desc.getNumDefs();
5049 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005050 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5051 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005052 continue;
5053 }
5054
5055 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005056 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005057 if (SymName.empty())
5058 continue;
5059
David Blaikie960ea3f2014-06-08 16:18:35 +00005060 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005061 if (!OpDecl)
5062 continue;
5063
5064 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005065 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005066 if (isOutput) {
5067 ++InputIdx;
5068 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005069 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005070 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005071 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005072 } else {
5073 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005074 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5075 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005076 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005077 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005078 }
Reid Kleckneree088972013-12-10 18:27:32 +00005079
5080 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005081 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5082 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005083 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005084 }
5085
5086 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005087 NumOutputs = OutputDecls.size();
5088 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005089
5090 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005091 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5092 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5093 ClobberRegs.end());
5094 Clobbers.assign(ClobberRegs.size(), std::string());
5095 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5096 raw_string_ostream OS(Clobbers[I]);
5097 IP->printRegName(OS, ClobberRegs[I]);
5098 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005099
5100 // Merge the various outputs and inputs. Output are expected first.
5101 if (NumOutputs || NumInputs) {
5102 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005103 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005104 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005105 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005106 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005107 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005108 }
5109 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005110 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005111 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005112 }
5113 }
5114
5115 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005116 std::string AsmStringIR;
5117 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005118 StringRef ASMString =
5119 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5120 const char *AsmStart = ASMString.begin();
5121 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005122 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005123 for (const AsmRewrite &AR : AsmStrRewrites) {
5124 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005125 if (Kind == AOK_Delete)
5126 continue;
5127
David Majnemer8114c1a2014-06-23 02:17:16 +00005128 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005129 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005130
Chad Rosier120eefd2013-03-19 17:32:17 +00005131 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005132 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005133 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005134
Chad Rosier37e755c2012-10-23 17:43:43 +00005135 // Skip the original expression.
5136 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005137 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005138 continue;
5139 }
5140
Chad Rosierff10ed12013-04-12 16:26:42 +00005141 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005142 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005143 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005144 default:
5145 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005146 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005147 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005148 break;
5149 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005150 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005151 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005152 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005153 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005154 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005155 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005156 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005157 break;
5158 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005159 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005160 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005161 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005162 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005163 default: break;
5164 case 8: OS << "byte ptr "; break;
5165 case 16: OS << "word ptr "; break;
5166 case 32: OS << "dword ptr "; break;
5167 case 64: OS << "qword ptr "; break;
5168 case 80: OS << "xword ptr "; break;
5169 case 128: OS << "xmmword ptr "; break;
5170 case 256: OS << "ymmword ptr "; break;
5171 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005172 break;
5173 case AOK_Emit:
5174 OS << ".byte";
5175 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005176 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005177 // MS alignment directives are measured in bytes. If the native assembler
5178 // measures alignment in bytes, we can pass it straight through.
5179 OS << ".align";
5180 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5181 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005182
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005183 // Alignment is in log2 form, so print that instead and skip the original
5184 // immediate.
5185 unsigned Val = AR.Val;
5186 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005187 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005188 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5189 break;
5190 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005191 case AOK_EVEN:
5192 OS << ".even";
5193 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005194 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005195 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005196 OS.flush();
5197 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005198 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005199 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005200 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005201 case AOK_EndOfStatement:
5202 OS << "\n\t";
5203 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005204 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005205
Chad Rosier8bce6642012-10-18 15:49:34 +00005206 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005207 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005208 }
5209
5210 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005211 if (AsmStart != AsmEnd)
5212 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005213
5214 AsmString = OS.str();
5215 return false;
5216}
5217
Pete Cooper80d21cb2015-06-22 19:35:57 +00005218namespace llvm {
5219namespace MCParserUtils {
5220
5221/// Returns whether the given symbol is used anywhere in the given expression,
5222/// or subexpressions.
5223static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5224 switch (Value->getKind()) {
5225 case MCExpr::Binary: {
5226 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5227 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5228 isSymbolUsedInExpression(Sym, BE->getRHS());
5229 }
5230 case MCExpr::Target:
5231 case MCExpr::Constant:
5232 return false;
5233 case MCExpr::SymbolRef: {
5234 const MCSymbol &S =
5235 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5236 if (S.isVariable())
5237 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5238 return &S == Sym;
5239 }
5240 case MCExpr::Unary:
5241 return isSymbolUsedInExpression(
5242 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5243 }
5244
5245 llvm_unreachable("Unknown expr kind!");
5246}
5247
5248bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5249 MCAsmParser &Parser, MCSymbol *&Sym,
5250 const MCExpr *&Value) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00005251
5252 // FIXME: Use better location, we should use proper tokens.
Nirav Davefd910412016-06-17 16:06:17 +00005253 SMLoc EqualLoc = Parser.getTok().getLoc();
Pete Cooper80d21cb2015-06-22 19:35:57 +00005254
5255 if (Parser.parseExpression(Value)) {
5256 Parser.TokError("missing expression");
5257 Parser.eatToEndOfStatement();
5258 return true;
5259 }
5260
5261 // Note: we don't count b as used in "a = b". This is to allow
5262 // a = b
5263 // b = c
5264
Nirav Davefd910412016-06-17 16:06:17 +00005265 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
Pete Cooper80d21cb2015-06-22 19:35:57 +00005266 return Parser.TokError("unexpected token in assignment");
5267
5268 // Eat the end of statement marker.
5269 Parser.Lex();
5270
5271 // Validate that the LHS is allowed to be a variable (either it has not been
5272 // used as a symbol, or it is an absolute symbol).
5273 Sym = Parser.getContext().lookupSymbol(Name);
5274 if (Sym) {
5275 // Diagnose assignment to a label.
5276 //
5277 // FIXME: Diagnostics. Note the location of the definition as a label.
5278 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5279 if (isSymbolUsedInExpression(Sym, Value))
5280 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005281 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5282 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005283 ; // Allow redefinitions of undefined symbols only used in directives.
5284 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5285 ; // Allow redefinitions of variables that haven't yet been used.
5286 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5287 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5288 else if (!Sym->isVariable())
5289 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5290 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5291 return Parser.Error(EqualLoc,
5292 "invalid reassignment of non-absolute variable '" +
5293 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005294 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005295 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005296 return false;
5297 } else
5298 Sym = Parser.getContext().getOrCreateSymbol(Name);
5299
5300 Sym->setRedefinable(allow_redef);
5301
5302 return false;
5303}
5304
5305} // namespace MCParserUtils
5306} // namespace llvm
5307
Daniel Dunbar01e36072010-07-17 02:26:10 +00005308/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005309MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5310 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005311 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005312}