blob: 12b9cf561a858b6becd2fdf80795ed6b93b3be78 [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"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#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
Daniel Dunbar43325c42010-09-09 22:42:56 +0000150 /// Flag tracking whether any errors have been encountered.
151 unsigned HadError : 1;
152
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000153 /// The values from the last parsed cpp hash file line comment if any.
154 StringRef CppHashFilename;
155 int64_t CppHashLineNumber;
156 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000157 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000158 /// When generating dwarf for assembly source files we need to calculate the
159 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000160 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
162 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000163 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000165
Devang Patela173ee52012-01-31 18:14:05 +0000166 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
167 unsigned AssemblerDialect;
168
Jim Grosbach4b905842013-09-20 23:08:21 +0000169 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000170 bool IsDarwin;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000173 bool ParsingInlineAsm;
174
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000175public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000176 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000177 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000178 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179
Craig Topper59be68f2014-03-08 07:14:16 +0000180 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181
Craig Topper59be68f2014-03-08 07:14:16 +0000182 void addDirectiveHandler(StringRef Directive,
183 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000184 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 }
186
Toma Tabacu11e14a92015-04-21 11:50:52 +0000187 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
188 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
189 }
190
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191public:
192 /// @name MCAsmParser Interface
193 /// {
194
Craig Topper59be68f2014-03-08 07:14:16 +0000195 SourceMgr &getSourceManager() override { return SrcMgr; }
196 MCAsmLexer &getLexer() override { return Lexer; }
197 MCContext &getContext() override { return Ctx; }
198 MCStreamer &getStreamer() override { return Out; }
199 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000200 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000201 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000202 else
203 return AssemblerDialect;
204 }
Craig Topper59be68f2014-03-08 07:14:16 +0000205 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000206 AssemblerDialect = i;
207 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000208
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void Note(SMLoc L, const Twine &Msg,
210 ArrayRef<SMRange> Ranges = None) override;
211 bool Warning(SMLoc L, const Twine &Msg,
212 ArrayRef<SMRange> Ranges = None) override;
213 bool Error(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000215
Craig Topper59be68f2014-03-08 07:14:16 +0000216 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Craig Topper59be68f2014-03-08 07:14:16 +0000218 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
219 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000220
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000221 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000222 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000223 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000224 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000226 const MCInstrInfo *MII, const MCInstPrinter *IP,
227 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000228
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000229 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000230 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
231 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
232 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
233 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000234
Jim Grosbach4b905842013-09-20 23:08:21 +0000235 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000236 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000237 bool parseIdentifier(StringRef &Res) override;
238 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000239
Craig Topper59be68f2014-03-08 07:14:16 +0000240 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000241 /// }
242
243private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000244
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000245 bool parseStatement(ParseStatementInfo &Info,
246 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000247 void eatToEndOfLine();
248 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000249
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000251 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000252 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000253 ArrayRef<MCAsmMacroParameter> Parameters,
254 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000255 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000256
Eli Benderskya313ae62013-01-16 18:56:50 +0000257 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000259
260 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Lookup a previously defined macro.
264 /// \param Name Macro name.
265 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000269 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000277 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000278 ///
279 /// \param M The macro.
280 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
283 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
David Majnemer91fc4c22014-01-29 18:57:46 +0000286 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000287 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000290 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void printMacroInstantiations();
293 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000294 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000295 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000296 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000297 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000298
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 /// \brief Enter the specified file. This returns true on failure.
300 bool enterIncludeFile(const std::string &Filename);
301
302 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000303 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000304 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000305
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000306 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000307 /// current token is not set; clients should ensure Lex() is called
308 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000309 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000310 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000311 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000312 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314 /// \brief Parse up to the end of statement and a return the contents from the
315 /// current token until the end of the statement; the current token on exit
316 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000317 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000318
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000319 /// \brief Parse until the end of a statement or a comma is encountered,
320 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000321 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322
Jim Grosbach4b905842013-09-20 23:08:21 +0000323 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000324 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
327 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
328 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000329
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000331
Eli Bendersky17233942013-01-15 22:59:42 +0000332 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000333 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000334 DK_NO_DIRECTIVE, // Placeholder
335 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000336 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
337 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000338 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000339 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000340 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000341 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
342 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
343 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
344 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000345 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000346 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
347 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000348 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
349 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
350 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
351 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
352 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
353 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000354 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000355 DK_MACROS_ON, DK_MACROS_OFF,
356 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000357 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000358 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000360 };
361
Jim Grosbach4b905842013-09-20 23:08:21 +0000362 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000363 /// directives parsed by this class.
364 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000365
366 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000367 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
368 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000369 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
371 bool parseDirectiveFill(); // ".fill"
372 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000373 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
375 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000376 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Eli Bendersky17233942013-01-15 22:59:42 +0000379 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveFile(SMLoc DirectiveLoc);
381 bool parseDirectiveLine();
382 bool parseDirectiveLoc();
383 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000384
385 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000387 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFISections();
389 bool parseDirectiveCFIStartProc();
390 bool parseDirectiveCFIEndProc();
391 bool parseDirectiveCFIDefCfaOffset();
392 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIAdjustCfaOffset();
394 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
398 bool parseDirectiveCFIRememberState();
399 bool parseDirectiveCFIRestoreState();
400 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIEscape();
403 bool parseDirectiveCFISignalFrame();
404 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000405
406 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000407 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000408 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectiveEndMacro(StringRef Directive);
410 bool parseDirectiveMacro(SMLoc DirectiveLoc);
411 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000412
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419
Eli Bendersky17233942013-01-15 22:59:42 +0000420 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000422
423 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000429
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveAbort(); // ".abort"
433 bool parseDirectiveInclude(); // ".include"
434 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000436 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
437 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000438 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000440 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000442 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
443 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000444 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
446 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
447 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
448 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000449 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000450
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000452 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000453
Rafael Espindola34b9c512012-06-03 23:57:14 +0000454 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
456 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000457 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000458 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000459 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
460 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
461 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000462
Chad Rosierc7f552c2013-02-12 21:33:51 +0000463 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000465 size_t Len);
466
467 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000468 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000469
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000470 // "end"
471 bool parseDirectiveEnd(SMLoc DirectiveLoc);
472
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000473 // ".err" or ".error"
474 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000475
Nico Weber404012b2014-07-24 16:26:06 +0000476 // ".warning"
477 bool parseDirectiveWarning(SMLoc DirectiveLoc);
478
Eli Bendersky17233942013-01-15 22:59:42 +0000479 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000480};
Daniel Dunbar86033402010-07-12 17:54:38 +0000481}
482
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000483namespace llvm {
484
485extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000486extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000487extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000488
489}
490
Chris Lattnerc35681b2010-01-19 19:46:13 +0000491enum { DEFAULT_ADDRSPACE = 0 };
492
David Blaikie9f380a32015-03-16 18:06:57 +0000493AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
494 const MCAsmInfo &MAI)
495 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
496 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000497 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000498 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000499 // Save the old handler.
500 SavedDiagHandler = SrcMgr.getDiagHandler();
501 SavedDiagContext = SrcMgr.getDiagContext();
502 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000503 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000504 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000505
Daniel Dunbarc5011082010-07-12 18:12:02 +0000506 // Initialize the platform / file format parser.
David Blaikie9f380a32015-03-16 18:06:57 +0000507 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +0000508 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000509 PlatformParser.reset(createCOFFAsmParser());
510 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000512 PlatformParser.reset(createDarwinAsmParser());
513 IsDarwin = true;
514 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000515 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000516 PlatformParser.reset(createELFAsmParser());
517 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000518 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000519
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000520 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000521 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000522}
523
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000527}
528
Jim Grosbach4b905842013-09-20 23:08:21 +0000529void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000530 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000531 for (std::vector<MacroInstantiation *>::const_reverse_iterator
532 it = ActiveMacros.rbegin(),
533 ie = ActiveMacros.rend();
534 it != ie; ++it)
535 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000536 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000537}
538
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000539void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
540 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
541 printMacroInstantiations();
542}
543
Chris Lattnera3a06812011-10-16 04:47:35 +0000544bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000545 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000546 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000547 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
548 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000549 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000553 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000554 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
555 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000556 return true;
557}
558
Jim Grosbach4b905842013-09-20 23:08:21 +0000559bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000560 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000561 unsigned NewBuf =
562 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
563 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000565
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000567 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000568 return false;
569}
Daniel Dunbar43235712010-07-18 18:54:11 +0000570
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000571/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000572/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000573/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000574bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000576 unsigned NewBuf =
577 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
578 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579 return true;
580
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000581 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000582 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 return false;
584}
585
Alp Tokera55b95b2014-07-06 10:33:31 +0000586void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
587 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000588 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
589 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000590}
591
Sean Callanan7a77eae2010-01-21 00:19:58 +0000592const AsmToken &AsmParser::Lex() {
593 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000594
Sean Callanan7a77eae2010-01-21 00:19:58 +0000595 if (tok->is(AsmToken::Eof)) {
596 // If this is the end of an included file, pop the parent file off the
597 // include stack.
598 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
599 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000600 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 tok = &Lexer.Lex();
602 }
603 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000606 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000609}
610
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000611bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000612 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000613 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000614 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000615
Chris Lattner36e02122009-06-21 20:54:55 +0000616 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000618
619 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000620 AsmCond StartingCondState = TheCondState;
621
Kevin Enderby6469fc22011-11-01 22:27:22 +0000622 // If we are generating dwarf for assembly source files save the initial text
623 // section and generate a .file directive.
624 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000625 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
626 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000627 auto InsertResult = getContext().addGenDwarfSection(
628 getStreamer().getCurrentSection().first);
629 assert(InsertResult.second && ".text section should not have debug info yet");
630 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000631 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
632 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 }
634
Chris Lattner73f36112009-07-02 21:53:43 +0000635 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000636 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000637 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000638 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000639 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000640
Daniel Dunbar43325c42010-09-09 22:42:56 +0000641 // We had an error, validate that one was emitted and recover by skipping to
642 // the next line.
643 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000644 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000645 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000646
647 if (TheCondState.TheCond != StartingCondState.TheCond ||
648 TheCondState.Ignore != StartingCondState.Ignore)
649 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000650
651 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000652 const auto &LineTables = getContext().getMCDwarfLineTables();
653 if (!LineTables.empty()) {
654 unsigned Index = 0;
655 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
656 if (File.Name.empty() && Index != 0)
657 TokError("unassigned file number: " + Twine(Index) +
658 " for .file directives");
659 ++Index;
660 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000662
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000663 // Check to see that all assembler local symbols were actually defined.
664 // Targets that don't do subsections via symbols may not want this, though,
665 // so conservatively exclude them. Only do this if we're finalizing, though,
666 // as otherwise we won't necessarilly have seen everything yet.
667 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
668 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
669 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000670 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 i != e; ++i) {
672 MCSymbol *Sym = i->getValue();
673 // Variable symbols may not be marked as defined, so check those
674 // explicitly. If we know it's a variable, we have a definition for
675 // the purposes of this check.
676 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
677 // FIXME: We would really like to refer back to where the symbol was
678 // first referenced for a source location. We need to add something
679 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000680 printMessage(
681 getLexer().getLoc(), SourceMgr::DK_Error,
682 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000683 }
684 }
685
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000686 // Finalize the output stream if there are no errors and if the client wants
687 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000688 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000689 Out.Finish();
690
Chris Lattner73f36112009-07-02 21:53:43 +0000691 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000692}
693
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000694void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000695 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000696 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000697 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000698 }
699}
700
Jim Grosbach4b905842013-09-20 23:08:21 +0000701/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000703 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000704 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000705
Chris Lattnere5074c42009-06-22 01:29:09 +0000706 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000707 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000708 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000709}
710
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000712 const char *Start = getTok().getLoc().getPointer();
713
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000715 Lex();
716
717 const char *End = getTok().getLoc().getPointer();
718 return StringRef(Start, End - Start);
719}
Chris Lattner78db3622009-06-22 05:51:26 +0000720
Jim Grosbach4b905842013-09-20 23:08:21 +0000721StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000722 const char *Start = getTok().getLoc().getPointer();
723
724 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000725 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000726 Lex();
727
728 const char *End = getTok().getLoc().getPointer();
729 return StringRef(Start, End - Start);
730}
731
Jim Grosbach4b905842013-09-20 23:08:21 +0000732/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000733/// NOTE: This assumes the leading '(' has already been consumed.
734///
735/// parenexpr ::= expr)
736///
Jim Grosbach4b905842013-09-20 23:08:21 +0000737bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
738 if (parseExpression(Res))
739 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000740 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000742 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000743 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000744 return false;
745}
Chris Lattner78db3622009-06-22 05:51:26 +0000746
Jim Grosbach4b905842013-09-20 23:08:21 +0000747/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000748/// NOTE: This assumes the leading '[' has already been consumed.
749///
750/// bracketexpr ::= expr]
751///
Jim Grosbach4b905842013-09-20 23:08:21 +0000752bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
753 if (parseExpression(Res))
754 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000755 if (Lexer.isNot(AsmToken::RBrac))
756 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000757 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000758 Lex();
759 return false;
760}
761
Jim Grosbach4b905842013-09-20 23:08:21 +0000762/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000763/// primaryexpr ::= (parenexpr
764/// primaryexpr ::= symbol
765/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000766/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000767/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000768bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000769 SMLoc FirstTokenLoc = getLexer().getLoc();
770 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
771 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000772 default:
773 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000774 // If we have an error assume that we've already handled it.
775 case AsmToken::Error:
776 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000777 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000778 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000779 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000780 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000781 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000782 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000783 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000784 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000785 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000786 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000787 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000788 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000789 if (FirstTokenKind == AsmToken::Dollar) {
790 if (Lexer.getMAI().getDollarIsPC()) {
791 // This is a '$' reference, which references the current PC. Emit a
792 // temporary label to the streamer and refer to it.
793 MCSymbol *Sym = Ctx.CreateTempSymbol();
794 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000795 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
796 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 EndLoc = FirstTokenLoc;
798 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000799 }
800 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000801 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000802 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000803 // Parse symbol variant
804 std::pair<StringRef, StringRef> Split;
805 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000806 if (FirstTokenKind == AsmToken::String) {
807 if (Lexer.is(AsmToken::At)) {
808 Lexer.Lex(); // eat @
809 SMLoc AtLoc = getLexer().getLoc();
810 StringRef VName;
811 if (parseIdentifier(VName))
812 return Error(AtLoc, "expected symbol variant after '@'");
813
814 Split = std::make_pair(Identifier, VName);
815 }
816 } else {
817 Split = Identifier.split('@');
818 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000819 } else if (Lexer.is(AsmToken::LParen)) {
820 Lexer.Lex(); // eat (
821 StringRef VName;
822 parseIdentifier(VName);
823 if (Lexer.isNot(AsmToken::RParen)) {
824 return Error(Lexer.getTok().getLoc(),
825 "unexpected token in variant, expected ')'");
826 }
827 Lexer.Lex(); // eat )
828 Split = std::make_pair(Identifier, VName);
829 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000830
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000831 EndLoc = SMLoc::getFromPointer(Identifier.end());
832
Daniel Dunbard20cda02009-10-16 01:34:54 +0000833 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000834 StringRef SymbolName = Identifier;
835 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000836
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000837 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000839 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 if (Variant != MCSymbolRefExpr::VK_Invalid) {
841 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000842 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000843 Variant = MCSymbolRefExpr::VK_None;
844 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000845 return Error(SMLoc::getFromPointer(Split.second.begin()),
846 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000847 }
848 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000849
Hans Wennborgce69d772013-10-18 20:46:28 +0000850 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
851
Daniel Dunbard20cda02009-10-16 01:34:54 +0000852 // If this is an absolute variable reference, substitute it now to preserve
853 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000855 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000856 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000857
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000858 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000859 return false;
860 }
861
862 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000863 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000864 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000865 }
David Woodhousef42a6662014-02-01 16:20:54 +0000866 case AsmToken::BigNum:
867 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000868 case AsmToken::Integer: {
869 SMLoc Loc = getTok().getLoc();
870 int64_t IntVal = getTok().getIntVal();
871 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000872 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000873 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000874 // Look for 'b' or 'f' following an Integer as a directional label
875 if (Lexer.getKind() == AsmToken::Identifier) {
876 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000877 // Lookup the symbol variant if used.
878 std::pair<StringRef, StringRef> Split = IDVal.split('@');
879 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
880 if (Split.first.size() != IDVal.size()) {
881 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000882 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000884 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000886 if (IDVal == "f" || IDVal == "b") {
887 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000888 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000889 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000890 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000892 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 Lex(); // Eat identifier.
894 }
895 }
Chris Lattner78db3622009-06-22 05:51:26 +0000896 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000898 case AsmToken::Real: {
899 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000900 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000901 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000902 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 Lex(); // Eat token.
904 return false;
905 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000906 case AsmToken::Dot: {
907 // This is a '.' reference, which references the current PC. Emit a
908 // temporary label to the streamer and refer to it.
909 MCSymbol *Sym = Ctx.CreateTempSymbol();
910 Out.EmitLabel(Sym);
911 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000912 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000913 Lex(); // Eat identifier.
914 return false;
915 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000916 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000917 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000918 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000919 case AsmToken::LBrac:
920 if (!PlatformParser->HasBracketExpressions())
921 return TokError("brackets expression not supported on this target");
922 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000928 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000936 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000937 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000938 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000940 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000942 }
943}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000944
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000945bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000946 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000947 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000948}
949
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000951AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000952 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000953 // Ask the target implementation about this expression first.
954 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
955 if (NewE)
956 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 // Recurse over the given expression, rebuilding it to apply the given variant
958 // if there is exactly one symbol.
959 switch (E->getKind()) {
960 case MCExpr::Target:
961 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000962 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963
964 case MCExpr::SymbolRef: {
965 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
966
967 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000968 TokError("invalid variant on expression '" + getTok().getIdentifier() +
969 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 return E;
971 }
972
973 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
974 }
975
976 case MCExpr::Unary: {
977 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000978 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000980 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
982 }
983
984 case MCExpr::Binary: {
985 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
987 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988
989 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000990 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000991
Jim Grosbach4b905842013-09-20 23:08:21 +0000992 if (!LHS)
993 LHS = BE->getLHS();
994 if (!RHS)
995 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
997 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
998 }
999 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001000
Craig Toppera2886c22012-02-07 05:05:23 +00001001 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002}
1003
Jim Grosbach4b905842013-09-20 23:08:21 +00001004/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001005///
Jim Grosbachbd164242011-08-20 16:24:13 +00001006/// expr ::= expr &&,|| expr -> lowest.
1007/// expr ::= expr |,^,&,! expr
1008/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1009/// expr ::= expr <<,>> expr
1010/// expr ::= expr +,- expr
1011/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001012/// expr ::= primaryexpr
1013///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001014bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001015 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001016 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001018 return true;
1019
Daniel Dunbar55f16672010-09-17 02:47:07 +00001020 // As a special case, we support 'a op b @ modifier' by rewriting the
1021 // expression to include the modifier. This is inefficient, but in general we
1022 // expect users to use 'a@modifier op b'.
1023 if (Lexer.getKind() == AsmToken::At) {
1024 Lex();
1025
1026 if (Lexer.isNot(AsmToken::Identifier))
1027 return TokError("unexpected symbol modifier following '@'");
1028
1029 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (Variant == MCSymbolRefExpr::VK_Invalid)
1032 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1033
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001035 if (!ModifiedRes) {
1036 return TokError("invalid modifier '" + getTok().getIdentifier() +
1037 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001039
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 Res = ModifiedRes;
1041 Lex();
1042 }
1043
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001044 // Try to constant fold it up front, if possible.
1045 int64_t Value;
1046 if (Res->EvaluateAsAbsolute(Value))
1047 Res = MCConstantExpr::Create(Value, getContext());
1048
1049 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001050}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001051
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001052bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001053 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001054 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001055}
1056
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001058 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001059
Daniel Dunbar75630b32009-06-30 02:10:03 +00001060 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001061 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062 return true;
1063
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001064 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001065 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001066
1067 return false;
1068}
1069
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001071 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001072 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001073 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001074 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075
Jim Grosbach4b905842013-09-20 23:08:21 +00001076 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001079 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001082 return 1;
1083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Low Precedence: |, &, ^
1085 //
1086 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 case AsmToken::EqualEqual:
1099 Kind = MCBinaryExpr::EQ;
1100 return 3;
1101 case AsmToken::ExclaimEqual:
1102 case AsmToken::LessGreater:
1103 Kind = MCBinaryExpr::NE;
1104 return 3;
1105 case AsmToken::Less:
1106 Kind = MCBinaryExpr::LT;
1107 return 3;
1108 case AsmToken::LessEqual:
1109 Kind = MCBinaryExpr::LTE;
1110 return 3;
1111 case AsmToken::Greater:
1112 Kind = MCBinaryExpr::GT;
1113 return 3;
1114 case AsmToken::GreaterEqual:
1115 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001116 return 3;
1117
Jim Grosbach4b905842013-09-20 23:08:21 +00001118 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 case AsmToken::LessLess:
1120 Kind = MCBinaryExpr::Shl;
1121 return 4;
1122 case AsmToken::GreaterGreater:
1123 Kind = MCBinaryExpr::Shr;
1124 return 4;
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127 case AsmToken::Plus:
1128 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130 case AsmToken::Minus:
1131 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001132 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 }
1145}
1146
Jim Grosbach4b905842013-09-20 23:08:21 +00001147/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001149bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001150 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001152 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001153 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001154
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 // If the next token is lower precedence than we are allowed to eat, return
1156 // successfully with what we ate already.
1157 if (TokPrec < Precedence)
1158 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Sean Callanan686ed8d2010-01-19 20:22:31 +00001160 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001163 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 if (parsePrimaryExpr(RHS, EndLoc))
1165 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 // If BinOp binds less tightly with RHS than the operator after RHS, let
1168 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001169 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001171 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1172 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001174 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001175 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001176 }
1177}
1178
Chris Lattner36e02122009-06-21 20:54:55 +00001179/// ParseStatement:
1180/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001181/// ::= Label* Directive ...Operands... EndOfStatement
1182/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001183bool AsmParser::parseStatement(ParseStatementInfo &Info,
1184 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001185 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001186 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001187 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001188 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001189 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001191 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001192 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001193 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001195 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001197 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001198 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001199
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 if (Lexer.is(AsmToken::Integer)) {
1202 LocalLabelVal = getTok().getIntVal();
1203 if (LocalLabelVal < 0) {
1204 if (!TheCondState.Ignore)
1205 return TokError("unexpected token at start of statement");
1206 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001207 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001208 IDVal = getTok().getString();
1209 Lex(); // Consume the integer token to be used as an identifier token.
1210 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001211 if (!TheCondState.Ignore)
1212 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 }
1214 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001215 } else if (Lexer.is(AsmToken::Dot)) {
1216 // Treat '.' as a valid identifier in this context.
1217 Lex();
1218 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001219 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001220 if (!TheCondState.Ignore)
1221 return TokError("unexpected token at start of statement");
1222 IDVal = "";
1223 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001224
Chris Lattner926885c2010-04-17 18:14:27 +00001225 // Handle conditional assembly here before checking for skipping. We
1226 // have to do this so that .endif isn't skipped in a ".if 0" block for
1227 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001228 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001229 DirectiveKindMap.find(IDVal);
1230 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1231 ? DK_NO_DIRECTIVE
1232 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001233 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001234 default:
1235 break;
1236 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001237 case DK_IFEQ:
1238 case DK_IFGE:
1239 case DK_IFGT:
1240 case DK_IFLE:
1241 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001242 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001243 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 case DK_IFB:
1245 return parseDirectiveIfb(IDLoc, true);
1246 case DK_IFNB:
1247 return parseDirectiveIfb(IDLoc, false);
1248 case DK_IFC:
1249 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001250 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001251 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001252 case DK_IFNC:
1253 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001254 case DK_IFNES:
1255 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001256 case DK_IFDEF:
1257 return parseDirectiveIfdef(IDLoc, true);
1258 case DK_IFNDEF:
1259 case DK_IFNOTDEF:
1260 return parseDirectiveIfdef(IDLoc, false);
1261 case DK_ELSEIF:
1262 return parseDirectiveElseIf(IDLoc);
1263 case DK_ELSE:
1264 return parseDirectiveElse(IDLoc);
1265 case DK_ENDIF:
1266 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001267 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001268
Eli Bendersky88024712013-01-16 19:32:36 +00001269 // Ignore the statement if in the middle of inactive conditional
1270 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001271 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001272 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001273 return false;
1274 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001275
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001276 // FIXME: Recurse on local labels?
1277
1278 // See what kind of statement we have.
1279 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001280 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001281 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001282
Chris Lattner36e02122009-06-21 20:54:55 +00001283 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001284 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001285
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001286 // Diagnose attempt to use '.' as a label.
1287 if (IDVal == ".")
1288 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1289
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001290 // Diagnose attempt to use a variable as a label.
1291 //
1292 // FIXME: Diagnostics. Note the location of the definition as a label.
1293 // FIXME: This doesn't diagnose assignment to a symbol which has been
1294 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001295 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001296 if (LocalLabelVal == -1) {
1297 if (ParsingInlineAsm && SI) {
1298 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1299 assert(RewrittenLabel.size() && "We should have an internal name here.");
1300 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1301 IDVal.size(), RewrittenLabel));
1302 IDVal = RewrittenLabel;
1303 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001304 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001305 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001306 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001307
1308 Sym->redefineIfPossible();
1309
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001310 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001311 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001312
Daniel Dunbare73b2672009-08-26 22:13:22 +00001313 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001314 if (!ParsingInlineAsm)
1315 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001316
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001318 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001319 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001320 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1321 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001322
Tim Northover1744d0a2013-10-25 12:49:50 +00001323 getTargetParser().onLabelParsed(Sym);
1324
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001325 // Consume any end of statement token, if present, to avoid spurious
1326 // AddBlankLine calls().
1327 if (Lexer.is(AsmToken::EndOfStatement)) {
1328 Lex();
1329 if (Lexer.is(AsmToken::Eof))
1330 return false;
1331 }
1332
Eli Friedman0f4871d2012-10-22 23:58:19 +00001333 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001334 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001336 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001338 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001339
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001341
1342 default: // Normal instruction or directive.
1343 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001344 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001345
1346 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001347 if (areMacrosEnabled())
1348 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1349 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001350 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001351
Michael J. Spencer530ce852010-10-09 11:00:50 +00001352 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001353
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001355 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001357 //
Eli Bendersky17233942013-01-15 22:59:42 +00001358 // 1. The target-specific assembly parser. Some directives are target
1359 // specific or may potentially behave differently on certain targets.
1360 // 2. Asm parser extensions. For example, platform-specific parsers
1361 // (like the ELF parser) register themselves as extensions.
1362 // 3. The generic directive parser implemented by this class. These are
1363 // all the directives that behave in a target and platform independent
1364 // manner, or at least have a default behavior that's shared between
1365 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001366
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // First query the target-specific parser. It will return 'true' if it
1368 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001369 if (!getTargetParser().ParseDirective(ID))
1370 return false;
1371
Alp Tokercb402912014-01-24 17:20:08 +00001372 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001373 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001374 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1375 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001376 if (Handler.first)
1377 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1378
1379 // Finally, if no one else is interested in this directive, it must be
1380 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001381 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001382 default:
1383 break;
1384 case DK_SET:
1385 case DK_EQU:
1386 return parseDirectiveSet(IDVal, true);
1387 case DK_EQUIV:
1388 return parseDirectiveSet(IDVal, false);
1389 case DK_ASCII:
1390 return parseDirectiveAscii(IDVal, false);
1391 case DK_ASCIZ:
1392 case DK_STRING:
1393 return parseDirectiveAscii(IDVal, true);
1394 case DK_BYTE:
1395 return parseDirectiveValue(1);
1396 case DK_SHORT:
1397 case DK_VALUE:
1398 case DK_2BYTE:
1399 return parseDirectiveValue(2);
1400 case DK_LONG:
1401 case DK_INT:
1402 case DK_4BYTE:
1403 return parseDirectiveValue(4);
1404 case DK_QUAD:
1405 case DK_8BYTE:
1406 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001407 case DK_OCTA:
1408 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001409 case DK_SINGLE:
1410 case DK_FLOAT:
1411 return parseDirectiveRealValue(APFloat::IEEEsingle);
1412 case DK_DOUBLE:
1413 return parseDirectiveRealValue(APFloat::IEEEdouble);
1414 case DK_ALIGN: {
1415 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1416 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1417 }
1418 case DK_ALIGN32: {
1419 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1420 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1421 }
1422 case DK_BALIGN:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1424 case DK_BALIGNW:
1425 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1426 case DK_BALIGNL:
1427 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1428 case DK_P2ALIGN:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1430 case DK_P2ALIGNW:
1431 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1432 case DK_P2ALIGNL:
1433 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1434 case DK_ORG:
1435 return parseDirectiveOrg();
1436 case DK_FILL:
1437 return parseDirectiveFill();
1438 case DK_ZERO:
1439 return parseDirectiveZero();
1440 case DK_EXTERN:
1441 eatToEndOfStatement(); // .extern is the default, ignore it.
1442 return false;
1443 case DK_GLOBL:
1444 case DK_GLOBAL:
1445 return parseDirectiveSymbolAttribute(MCSA_Global);
1446 case DK_LAZY_REFERENCE:
1447 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1448 case DK_NO_DEAD_STRIP:
1449 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1450 case DK_SYMBOL_RESOLVER:
1451 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1452 case DK_PRIVATE_EXTERN:
1453 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1454 case DK_REFERENCE:
1455 return parseDirectiveSymbolAttribute(MCSA_Reference);
1456 case DK_WEAK_DEFINITION:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1458 case DK_WEAK_REFERENCE:
1459 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1460 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1461 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1462 case DK_COMM:
1463 case DK_COMMON:
1464 return parseDirectiveComm(/*IsLocal=*/false);
1465 case DK_LCOMM:
1466 return parseDirectiveComm(/*IsLocal=*/true);
1467 case DK_ABORT:
1468 return parseDirectiveAbort();
1469 case DK_INCLUDE:
1470 return parseDirectiveInclude();
1471 case DK_INCBIN:
1472 return parseDirectiveIncbin();
1473 case DK_CODE16:
1474 case DK_CODE16GCC:
1475 return TokError(Twine(IDVal) + " not supported yet");
1476 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001477 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001478 case DK_IRP:
1479 return parseDirectiveIrp(IDLoc);
1480 case DK_IRPC:
1481 return parseDirectiveIrpc(IDLoc);
1482 case DK_ENDR:
1483 return parseDirectiveEndr(IDLoc);
1484 case DK_BUNDLE_ALIGN_MODE:
1485 return parseDirectiveBundleAlignMode();
1486 case DK_BUNDLE_LOCK:
1487 return parseDirectiveBundleLock();
1488 case DK_BUNDLE_UNLOCK:
1489 return parseDirectiveBundleUnlock();
1490 case DK_SLEB128:
1491 return parseDirectiveLEB128(true);
1492 case DK_ULEB128:
1493 return parseDirectiveLEB128(false);
1494 case DK_SPACE:
1495 case DK_SKIP:
1496 return parseDirectiveSpace(IDVal);
1497 case DK_FILE:
1498 return parseDirectiveFile(IDLoc);
1499 case DK_LINE:
1500 return parseDirectiveLine();
1501 case DK_LOC:
1502 return parseDirectiveLoc();
1503 case DK_STABS:
1504 return parseDirectiveStabs();
1505 case DK_CFI_SECTIONS:
1506 return parseDirectiveCFISections();
1507 case DK_CFI_STARTPROC:
1508 return parseDirectiveCFIStartProc();
1509 case DK_CFI_ENDPROC:
1510 return parseDirectiveCFIEndProc();
1511 case DK_CFI_DEF_CFA:
1512 return parseDirectiveCFIDefCfa(IDLoc);
1513 case DK_CFI_DEF_CFA_OFFSET:
1514 return parseDirectiveCFIDefCfaOffset();
1515 case DK_CFI_ADJUST_CFA_OFFSET:
1516 return parseDirectiveCFIAdjustCfaOffset();
1517 case DK_CFI_DEF_CFA_REGISTER:
1518 return parseDirectiveCFIDefCfaRegister(IDLoc);
1519 case DK_CFI_OFFSET:
1520 return parseDirectiveCFIOffset(IDLoc);
1521 case DK_CFI_REL_OFFSET:
1522 return parseDirectiveCFIRelOffset(IDLoc);
1523 case DK_CFI_PERSONALITY:
1524 return parseDirectiveCFIPersonalityOrLsda(true);
1525 case DK_CFI_LSDA:
1526 return parseDirectiveCFIPersonalityOrLsda(false);
1527 case DK_CFI_REMEMBER_STATE:
1528 return parseDirectiveCFIRememberState();
1529 case DK_CFI_RESTORE_STATE:
1530 return parseDirectiveCFIRestoreState();
1531 case DK_CFI_SAME_VALUE:
1532 return parseDirectiveCFISameValue(IDLoc);
1533 case DK_CFI_RESTORE:
1534 return parseDirectiveCFIRestore(IDLoc);
1535 case DK_CFI_ESCAPE:
1536 return parseDirectiveCFIEscape();
1537 case DK_CFI_SIGNAL_FRAME:
1538 return parseDirectiveCFISignalFrame();
1539 case DK_CFI_UNDEFINED:
1540 return parseDirectiveCFIUndefined(IDLoc);
1541 case DK_CFI_REGISTER:
1542 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001543 case DK_CFI_WINDOW_SAVE:
1544 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 case DK_MACROS_ON:
1546 case DK_MACROS_OFF:
1547 return parseDirectiveMacrosOnOff(IDVal);
1548 case DK_MACRO:
1549 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001550 case DK_EXITM:
1551 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001552 case DK_ENDM:
1553 case DK_ENDMACRO:
1554 return parseDirectiveEndMacro(IDVal);
1555 case DK_PURGEM:
1556 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001557 case DK_END:
1558 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001559 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001560 return parseDirectiveError(IDLoc, false);
1561 case DK_ERROR:
1562 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001563 case DK_WARNING:
1564 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001565 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001566
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001567 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001568 }
Chris Lattner36e02122009-06-21 20:54:55 +00001569
Chad Rosierc7f552c2013-02-12 21:33:51 +00001570 // __asm _emit or __asm __emit
1571 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1572 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001573 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001574
1575 // __asm align
1576 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001577 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001578
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001579 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001580
Chris Lattner7cbfa442010-05-19 23:34:33 +00001581 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001582 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001583 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001585 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001586 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001587
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001588 // Dump the parsed representation, if requested.
1589 if (getShowParsedOperands()) {
1590 SmallString<256> Str;
1591 raw_svector_ostream OS(Str);
1592 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001593 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001594 if (i != 0)
1595 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001596 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001597 }
1598 OS << "]";
1599
Jim Grosbach4b905842013-09-20 23:08:21 +00001600 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001601 }
1602
Oliver Stannard8b273082014-06-19 15:52:37 +00001603 // If we are generating dwarf for the current section then generate a .loc
1604 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001605 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001606 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001607 getStreamer().getCurrentSection().first)) {
1608 unsigned Line;
1609 if (ActiveMacros.empty())
1610 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1611 else
1612 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1613 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001614
Eli Bendersky88024712013-01-16 19:32:36 +00001615 // If we previously parsed a cpp hash file line comment then make sure the
1616 // current Dwarf File is for the CppHashFilename if not then emit the
1617 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001618 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001619 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1620 0, StringRef(), CppHashFilename);
1621 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001622
Jim Grosbach4b905842013-09-20 23:08:21 +00001623 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1624 // cache with the different Loc from the call above we save the last
1625 // info we queried here with SrcMgr.FindLineNumber().
1626 unsigned CppHashLocLineNo;
1627 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1628 CppHashLocLineNo = LastQueryLine;
1629 else {
1630 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1631 LastQueryLine = CppHashLocLineNo;
1632 LastQueryIDLoc = CppHashLoc;
1633 LastQueryBuffer = CppHashBuf;
1634 }
1635 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001636 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001637
Jim Grosbach4b905842013-09-20 23:08:21 +00001638 getStreamer().EmitDwarfLocDirective(
1639 getContext().getGenDwarfFileNumber(), Line, 0,
1640 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1641 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001642 }
1643
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001644 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001645 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001646 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001647 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1648 Info.ParsedOperands, Out,
1649 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001650 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001651
Chris Lattnera2a9d162010-09-11 16:18:25 +00001652 // Don't skip the rest of the line, the instruction parser is responsible for
1653 // that.
1654 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001655}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001656
Jim Grosbach4b905842013-09-20 23:08:21 +00001657/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001658/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001659void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001660 if (!Lexer.is(AsmToken::EndOfStatement))
1661 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 // Eat EOL.
1663 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001664}
1665
Jim Grosbach4b905842013-09-20 23:08:21 +00001666/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001667/// ::= # number "filename"
1668/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001669bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001670 Lex(); // Eat the hash token.
1671
1672 if (getLexer().isNot(AsmToken::Integer)) {
1673 // Consume the line since in cases it is not a well-formed line directive,
1674 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001675 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001676 return false;
1677 }
1678
1679 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001680 Lex();
1681
1682 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001684 return false;
1685 }
1686
1687 StringRef Filename = getTok().getString();
1688 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001689 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001690
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1692 CppHashLoc = L;
1693 CppHashFilename = Filename;
1694 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001695 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001696
1697 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001698 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001699 return false;
1700}
1701
Jim Grosbach4b905842013-09-20 23:08:21 +00001702/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703/// for the Filename and LineNo if any in the diagnostic.
1704void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001705 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706 raw_ostream &OS = errs();
1707
1708 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1709 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001710 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1711 unsigned CppHashBuf =
1712 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001716 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1717 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1718 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001719 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1720 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721 }
1722
Eric Christophera7c32732012-12-18 00:30:54 +00001723 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 // manager changed or buffer changed (like in a nested include) then just
1725 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001726 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001727 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001728 if (Parser->SavedDiagHandler)
1729 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1730 else
Craig Topper353eda42014-04-24 06:44:33 +00001731 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 return;
1733 }
1734
Eric Christophera7c32732012-12-18 00:30:54 +00001735 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001736 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1737 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001738 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001739
1740 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1741 int CppHashLocLineNo =
1742 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001743 int LineNo =
1744 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001745
Jim Grosbach4b905842013-09-20 23:08:21 +00001746 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1747 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001748 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001749
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001750 if (Parser->SavedDiagHandler)
1751 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1752 else
Craig Topper353eda42014-04-24 06:44:33 +00001753 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001754}
1755
Rafael Espindola2c064482012-08-21 18:29:30 +00001756// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1757// difference being that that function accepts '@' as part of identifiers and
1758// we can't do that. AsmLexer.cpp should probably be changed to handle
1759// '@' as a special case when needed.
1760static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001761 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1762 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001763}
1764
Rafael Espindola34b9c512012-06-03 23:57:14 +00001765bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001766 ArrayRef<MCAsmMacroParameter> Parameters,
1767 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001768 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001769 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001770 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001771 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001772
Preston Gurd05500642012-09-19 20:36:12 +00001773 // A macro without parameters is handled differently on Darwin:
1774 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001775 while (!Body.empty()) {
1776 // Scan for the next substitution.
1777 std::size_t End = Body.size(), Pos = 0;
1778 for (; Pos != End; ++Pos) {
1779 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001780 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 // This macro has no parameters, look for $0, $1, etc.
1782 if (Body[Pos] != '$' || Pos + 1 == End)
1783 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001784
Rafael Espindola1134ab232011-06-05 02:43:45 +00001785 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001786 if (Next == '$' || Next == 'n' ||
1787 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001788 break;
1789 } else {
1790 // This macro has parameters, look for \foo, \bar, etc.
1791 if (Body[Pos] == '\\' && Pos + 1 != End)
1792 break;
1793 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001794 }
1795
1796 // Add the prefix.
1797 OS << Body.slice(0, Pos);
1798
1799 // Check if we reached the end.
1800 if (Pos == End)
1801 break;
1802
Benjamin Kramer513e7442014-02-20 13:36:32 +00001803 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 switch (Body[Pos + 1]) {
1805 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001806 case '$':
1807 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001808 break;
1809
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001811 case 'n':
1812 OS << A.size();
1813 break;
1814
Jim Grosbach4b905842013-09-20 23:08:21 +00001815 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816 default: {
1817 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001818 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001819 if (Index >= A.size())
1820 break;
1821
1822 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001823 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001824 ie = A[Index].end();
1825 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 OS << it->getString();
1827 break;
1828 }
1829 }
1830 Pos += 2;
1831 } else {
1832 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001833 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 ++I;
1835
Jim Grosbach4b905842013-09-20 23:08:21 +00001836 const char *Begin = Body.data() + Pos + 1;
1837 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001838 unsigned Index = 0;
1839 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001840 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001841 break;
1842
Preston Gurd05500642012-09-19 20:36:12 +00001843 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001844 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1845 Pos += 3;
1846 else {
1847 OS << '\\' << Argument;
1848 Pos = I;
1849 }
Preston Gurd05500642012-09-19 20:36:12 +00001850 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001851 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001852 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001853 ie = A[Index].end();
1854 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001855 // We expect no quotes around the string's contents when
1856 // parsing for varargs.
1857 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001858 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001859 else
1860 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001861
Preston Gurd05500642012-09-19 20:36:12 +00001862 Pos += 1 + Argument.size();
1863 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001864 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001865 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001866 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001867 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001868
Rafael Espindola1134ab232011-06-05 02:43:45 +00001869 return false;
1870}
Daniel Dunbar43235712010-07-18 18:54:11 +00001871
Nico Weber2a8f9222014-07-24 16:29:04 +00001872MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001873 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001874 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001875 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001876
Jim Grosbach4b905842013-09-20 23:08:21 +00001877static bool isOperator(AsmToken::TokenKind kind) {
1878 switch (kind) {
1879 default:
1880 return false;
1881 case AsmToken::Plus:
1882 case AsmToken::Minus:
1883 case AsmToken::Tilde:
1884 case AsmToken::Slash:
1885 case AsmToken::Star:
1886 case AsmToken::Dot:
1887 case AsmToken::Equal:
1888 case AsmToken::EqualEqual:
1889 case AsmToken::Pipe:
1890 case AsmToken::PipePipe:
1891 case AsmToken::Caret:
1892 case AsmToken::Amp:
1893 case AsmToken::AmpAmp:
1894 case AsmToken::Exclaim:
1895 case AsmToken::ExclaimEqual:
1896 case AsmToken::Percent:
1897 case AsmToken::Less:
1898 case AsmToken::LessEqual:
1899 case AsmToken::LessLess:
1900 case AsmToken::LessGreater:
1901 case AsmToken::Greater:
1902 case AsmToken::GreaterEqual:
1903 case AsmToken::GreaterGreater:
1904 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001905 }
1906}
1907
David Majnemer16252452014-01-29 00:07:39 +00001908namespace {
1909class AsmLexerSkipSpaceRAII {
1910public:
1911 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1912 Lexer.setSkipSpace(SkipSpace);
1913 }
1914
1915 ~AsmLexerSkipSpaceRAII() {
1916 Lexer.setSkipSpace(true);
1917 }
1918
1919private:
1920 AsmLexer &Lexer;
1921};
1922}
1923
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001924bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1925
1926 if (Vararg) {
1927 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1928 StringRef Str = parseStringToEndOfStatement();
1929 MA.push_back(AsmToken(AsmToken::String, Str));
1930 }
1931 return false;
1932 }
1933
Rafael Espindola768b41c2012-06-15 14:02:34 +00001934 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001935 unsigned AddTokens = 0;
1936
David Majnemer16252452014-01-29 00:07:39 +00001937 // Darwin doesn't use spaces to delmit arguments.
1938 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001939
1940 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001941 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001942 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001943
David Majnemer91fc4c22014-01-29 18:57:46 +00001944 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001945 break;
Preston Gurd05500642012-09-19 20:36:12 +00001946
1947 if (Lexer.is(AsmToken::Space)) {
1948 Lex(); // Eat spaces
1949
1950 // Spaces can delimit parameters, but could also be part an expression.
1951 // If the token after a space is an operator, add the token and the next
1952 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001953 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001954 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001955 // Check to see whether the token is used as an operator,
1956 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001957 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001958 if (*NextChar == ' ')
1959 AddTokens = 2;
1960 }
1961
1962 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001963 break;
1964 }
1965 }
1966 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001967
Jim Grosbach4b905842013-09-20 23:08:21 +00001968 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001969 // to be able to fill in the remaining default parameter values
1970 if (Lexer.is(AsmToken::EndOfStatement))
1971 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001972
1973 // Adjust the current parentheses level.
1974 if (Lexer.is(AsmToken::LParen))
1975 ++ParenLevel;
1976 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1977 --ParenLevel;
1978
1979 // Append the token to the current argument list.
1980 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001981 if (AddTokens)
1982 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983 Lex();
1984 }
Preston Gurd05500642012-09-19 20:36:12 +00001985
Rafael Espindola768b41c2012-06-15 14:02:34 +00001986 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001987 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001988 return false;
1989}
1990
1991// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001992bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001993 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001994 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001995 bool NamedParametersFound = false;
1996 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001999 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002000
Rafael Espindola768b41c2012-06-15 14:02:34 +00002001 // Parse two kinds of macro invocations:
2002 // - macros defined without any parameters accept an arbitrary number of them
2003 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002004 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002005 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2006 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002007 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002008 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002009
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002010 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002011 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002012 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002013 eatToEndOfStatement();
2014 return true;
2015 }
2016
2017 if (!Lexer.is(AsmToken::Equal)) {
2018 TokError("expected '=' after formal parameter identifier");
2019 eatToEndOfStatement();
2020 return true;
2021 }
2022 Lex();
2023
2024 NamedParametersFound = true;
2025 }
2026
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002027 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002028 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002029 eatToEndOfStatement();
2030 return true;
2031 }
2032
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002033 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2034 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002035 return true;
2036
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002037 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002038 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002039 unsigned FAI = 0;
2040 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002041 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002042 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002043
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002044 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002045 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002046 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002047 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002048 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002049 return true;
2050 }
2051 PI = FAI;
2052 }
2053
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002054 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002055 if (A.size() <= PI)
2056 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002057 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002058
2059 if (FALocs.size() <= PI)
2060 FALocs.resize(PI + 1);
2061
2062 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002063 }
Jim Grosbach206661622012-07-30 22:44:17 +00002064
Preston Gurd242ed3152012-09-19 20:29:04 +00002065 // At the end of the statement, fill in remaining arguments that have
2066 // default values. If there aren't any, then the next argument is
2067 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002068 if (Lexer.is(AsmToken::EndOfStatement)) {
2069 bool Failure = false;
2070 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2071 if (A[FAI].empty()) {
2072 if (M->Parameters[FAI].Required) {
2073 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2074 "missing value for required parameter "
2075 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2076 Failure = true;
2077 }
2078
2079 if (!M->Parameters[FAI].Value.empty())
2080 A[FAI] = M->Parameters[FAI].Value;
2081 }
2082 }
2083 return Failure;
2084 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002085
2086 if (Lexer.is(AsmToken::Comma))
2087 Lex();
2088 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002089
2090 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002091}
2092
Jim Grosbach4b905842013-09-20 23:08:21 +00002093const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002094 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2095 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002096}
2097
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002098void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2099 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002100}
2101
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002102void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002103
Jim Grosbach4b905842013-09-20 23:08:21 +00002104bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002105 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2106 // this, although we should protect against infinite loops.
2107 if (ActiveMacros.size() == 20)
2108 return TokError("macros cannot be nested more than 20 levels deep");
2109
Eli Bendersky38274122013-01-14 23:22:36 +00002110 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002111 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002112 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002113
Rafael Espindola1134ab232011-06-05 02:43:45 +00002114 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2115 // to hold the macro body with substitutions.
2116 SmallString<256> Buf;
2117 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002118 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002119
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002120 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002121 return true;
2122
Eli Bendersky38274122013-01-14 23:22:36 +00002123 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002124 // instantiation.
2125 OS << ".endmacro\n";
2126
Rafael Espindola3560ff22014-08-27 20:03:13 +00002127 std::unique_ptr<MemoryBuffer> Instantiation =
2128 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002129
Daniel Dunbar43235712010-07-18 18:54:11 +00002130 // Create the macro instantiation object and add to the current macro
2131 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002132 MacroInstantiation *MI = new MacroInstantiation(
2133 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002134 ActiveMacros.push_back(MI);
2135
2136 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002137 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002138 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 Lex();
2140
2141 return false;
2142}
2143
Jim Grosbach4b905842013-09-20 23:08:21 +00002144void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002145 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002146 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002147 Lex();
2148
2149 // Pop the instantiation entry.
2150 delete ActiveMacros.back();
2151 ActiveMacros.pop_back();
2152}
2153
Jim Grosbach4b905842013-09-20 23:08:21 +00002154static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002156 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002157 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2158 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002159 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002160 case MCExpr::Target:
2161 case MCExpr::Constant:
2162 return false;
2163 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002164 const MCSymbol &S =
2165 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002166 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002168 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002169 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002170 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002171 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002172 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002173
2174 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002175}
2176
Jim Grosbach4b905842013-09-20 23:08:21 +00002177bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002178 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002179 // FIXME: Use better location, we should use proper tokens.
2180 SMLoc EqualLoc = Lexer.getLoc();
2181
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002182 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002183 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002184 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002185
Rafael Espindola72f5f172012-01-28 05:57:00 +00002186 // Note: we don't count b as used in "a = b". This is to allow
2187 // a = b
2188 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002189
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002190 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002191 return TokError("unexpected token in assignment");
2192
2193 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002194 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002195
Daniel Dunbar5f339242009-10-16 01:57:39 +00002196 // Validate that the LHS is allowed to be a variable (either it has not been
2197 // used as a symbol, or it is an absolute symbol).
2198 MCSymbol *Sym = getContext().LookupSymbol(Name);
2199 if (Sym) {
2200 // Diagnose assignment to a label.
2201 //
2202 // FIXME: Diagnostics. Note the location of the definition as a label.
2203 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002204 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002205 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2206 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002207 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002208 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2209 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002210 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002211 return Error(EqualLoc, "redefinition of '" + Name + "'");
2212 else if (!Sym->isVariable())
2213 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002214 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002215 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002216 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002217
2218 // Don't count these checks as uses.
2219 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002220 } else if (Name == ".") {
2221 if (Out.EmitValueToOffset(Value, 0)) {
2222 Error(EqualLoc, "expected absolute expression");
2223 eatToEndOfStatement();
2224 }
2225 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002226 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002227 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002228
David Majnemer58cb80c2014-12-24 10:27:50 +00002229 Sym->setRedefinable(allow_redef);
2230
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002231 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002232 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002233 if (NoDeadStrip)
2234 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2235
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002236 return false;
2237}
2238
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002239/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002240/// ::= identifier
2241/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002242bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002243 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002244 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2245 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002246 // handle this as a context dependent token, instead we detect adjacent tokens
2247 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002248 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2249 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250
Hans Wennborgce69d772013-10-18 20:46:28 +00002251 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002252 Lex();
2253 if (Lexer.isNot(AsmToken::Identifier))
2254 return true;
2255
Hans Wennborgce69d772013-10-18 20:46:28 +00002256 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2257 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002258 return true;
2259
2260 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002261 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002262 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002263 Lex();
2264 return false;
2265 }
2266
Jim Grosbach4b905842013-09-20 23:08:21 +00002267 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002268 return true;
2269
Sean Callanan936b0d32010-01-19 21:44:56 +00002270 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002271
Sean Callanan686ed8d2010-01-19 20:22:31 +00002272 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002273
2274 return false;
2275}
2276
Jim Grosbach4b905842013-09-20 23:08:21 +00002277/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002278/// ::= .equ identifier ',' expression
2279/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002280/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002281bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002282 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002283
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002284 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002285 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002286
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002288 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002289 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002290
Jim Grosbach4b905842013-09-20 23:08:21 +00002291 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002292}
2293
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002294bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002295 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002296
2297 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002298 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002299 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2300 if (Str[i] != '\\') {
2301 Data += Str[i];
2302 continue;
2303 }
2304
2305 // Recognize escaped characters. Note that this escape semantics currently
2306 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2307 ++i;
2308 if (i == e)
2309 return TokError("unexpected backslash at end of string");
2310
2311 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 // Consume up to three octal characters.
2314 unsigned Value = Str[i] - '0';
2315
Jim Grosbach4b905842013-09-20 23:08:21 +00002316 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002317 ++i;
2318 Value = Value * 8 + (Str[i] - '0');
2319
Jim Grosbach4b905842013-09-20 23:08:21 +00002320 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002321 ++i;
2322 Value = Value * 8 + (Str[i] - '0');
2323 }
2324 }
2325
2326 if (Value > 255)
2327 return TokError("invalid octal escape sequence (out of range)");
2328
Jim Grosbach4b905842013-09-20 23:08:21 +00002329 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002330 continue;
2331 }
2332
2333 // Otherwise recognize individual escapes.
2334 switch (Str[i]) {
2335 default:
2336 // Just reject invalid escape sequences for now.
2337 return TokError("invalid escape sequence (unrecognized character)");
2338
2339 case 'b': Data += '\b'; break;
2340 case 'f': Data += '\f'; break;
2341 case 'n': Data += '\n'; break;
2342 case 'r': Data += '\r'; break;
2343 case 't': Data += '\t'; break;
2344 case '"': Data += '"'; break;
2345 case '\\': Data += '\\'; break;
2346 }
2347 }
2348
2349 return false;
2350}
2351
Jim Grosbach4b905842013-09-20 23:08:21 +00002352/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002353/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002354bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002356 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002357
Daniel Dunbara10e5192009-06-24 23:30:00 +00002358 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002360 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Daniel Dunbaref668c12009-08-14 18:19:52 +00002362 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002363 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002364 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365
Rafael Espindola64e1af82013-07-02 15:49:13 +00002366 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002367 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002368 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002369
Sean Callanan686ed8d2010-01-19 20:22:31 +00002370 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002371
2372 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002373 break;
2374
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002375 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002376 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002377 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378 }
2379 }
2380
Sean Callanan686ed8d2010-01-19 20:22:31 +00002381 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002382 return false;
2383}
2384
Jim Grosbach4b905842013-09-20 23:08:21 +00002385/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002386/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002387bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002388 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002389 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002390
Daniel Dunbara10e5192009-06-24 23:30:00 +00002391 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002392 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002393 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002394 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002395 return true;
2396
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002397 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002398 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2399 assert(Size <= 8 && "Invalid size");
2400 uint64_t IntValue = MCE->getValue();
2401 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2402 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002403 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002404 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002405 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002407 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002409
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002411 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002412 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002413 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002414 }
2415 }
2416
Sean Callanan686ed8d2010-01-19 20:22:31 +00002417 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002418 return false;
2419}
2420
David Woodhoused6de0d92014-02-01 16:20:59 +00002421/// ParseDirectiveOctaValue
2422/// ::= .octa [ hexconstant (, hexconstant)* ]
2423bool AsmParser::parseDirectiveOctaValue() {
2424 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2425 checkForValidSection();
2426
2427 for (;;) {
2428 if (Lexer.getKind() == AsmToken::Error)
2429 return true;
2430 if (Lexer.getKind() != AsmToken::Integer &&
2431 Lexer.getKind() != AsmToken::BigNum)
2432 return TokError("unknown token in expression");
2433
2434 SMLoc ExprLoc = getLexer().getLoc();
2435 APInt IntValue = getTok().getAPIntVal();
2436 Lex();
2437
2438 uint64_t hi, lo;
2439 if (IntValue.isIntN(64)) {
2440 hi = 0;
2441 lo = IntValue.getZExtValue();
2442 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002443 // It might actually have more than 128 bits, but the top ones are zero.
2444 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002445 lo = IntValue.getLoBits(64).getZExtValue();
2446 } else
2447 return Error(ExprLoc, "literal value out of range for directive");
2448
2449 if (MAI.isLittleEndian()) {
2450 getStreamer().EmitIntValue(lo, 8);
2451 getStreamer().EmitIntValue(hi, 8);
2452 } else {
2453 getStreamer().EmitIntValue(hi, 8);
2454 getStreamer().EmitIntValue(lo, 8);
2455 }
2456
2457 if (getLexer().is(AsmToken::EndOfStatement))
2458 break;
2459
2460 // FIXME: Improve diagnostic.
2461 if (getLexer().isNot(AsmToken::Comma))
2462 return TokError("unexpected token in directive");
2463 Lex();
2464 }
2465 }
2466
2467 Lex();
2468 return false;
2469}
2470
Jim Grosbach4b905842013-09-20 23:08:21 +00002471/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002472/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002473bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002474 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002475 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002476
2477 for (;;) {
2478 // We don't truly support arithmetic on floating point expressions, so we
2479 // have to manually parse unary prefixes.
2480 bool IsNeg = false;
2481 if (getLexer().is(AsmToken::Minus)) {
2482 Lex();
2483 IsNeg = true;
2484 } else if (getLexer().is(AsmToken::Plus))
2485 Lex();
2486
Michael J. Spencer530ce852010-10-09 11:00:50 +00002487 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002488 getLexer().isNot(AsmToken::Real) &&
2489 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002490 return TokError("unexpected token in directive");
2491
2492 // Convert to an APFloat.
2493 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002494 StringRef IDVal = getTok().getString();
2495 if (getLexer().is(AsmToken::Identifier)) {
2496 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2497 Value = APFloat::getInf(Semantics);
2498 else if (!IDVal.compare_lower("nan"))
2499 Value = APFloat::getNaN(Semantics, false, ~0);
2500 else
2501 return TokError("invalid floating point literal");
2502 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002503 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002504 return TokError("invalid floating point literal");
2505 if (IsNeg)
2506 Value.changeSign();
2507
2508 // Consume the numeric token.
2509 Lex();
2510
2511 // Emit the value as an integer.
2512 APInt AsInt = Value.bitcastToAPInt();
2513 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002514 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002515
2516 if (getLexer().is(AsmToken::EndOfStatement))
2517 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002518
Daniel Dunbar2af16532010-09-24 01:59:56 +00002519 if (getLexer().isNot(AsmToken::Comma))
2520 return TokError("unexpected token in directive");
2521 Lex();
2522 }
2523 }
2524
2525 Lex();
2526 return false;
2527}
2528
Jim Grosbach4b905842013-09-20 23:08:21 +00002529/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002530/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002531bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002532 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002533
2534 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002535 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002536 return true;
2537
Rafael Espindolab91bac62010-10-05 19:42:57 +00002538 int64_t Val = 0;
2539 if (getLexer().is(AsmToken::Comma)) {
2540 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002541 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002542 return true;
2543 }
2544
Rafael Espindola922e3f42010-09-16 15:03:59 +00002545 if (getLexer().isNot(AsmToken::EndOfStatement))
2546 return TokError("unexpected token in '.zero' directive");
2547
2548 Lex();
2549
Rafael Espindola64e1af82013-07-02 15:49:13 +00002550 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002551
2552 return false;
2553}
2554
Jim Grosbach4b905842013-09-20 23:08:21 +00002555/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002556/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002557bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002558 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002559
David Majnemer522d3db2014-02-01 07:19:38 +00002560 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002561 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002562 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002563 return true;
2564
David Majnemer522d3db2014-02-01 07:19:38 +00002565 if (NumValues < 0) {
2566 Warning(RepeatLoc,
2567 "'.fill' directive with negative repeat count has no effect");
2568 NumValues = 0;
2569 }
2570
Roman Divackye33098f2013-09-24 17:44:41 +00002571 int64_t FillSize = 1;
2572 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002573
David Majnemer522d3db2014-02-01 07:19:38 +00002574 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002575 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2576 if (getLexer().isNot(AsmToken::Comma))
2577 return TokError("unexpected token in '.fill' directive");
2578 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002579
David Majnemer522d3db2014-02-01 07:19:38 +00002580 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002581 if (parseAbsoluteExpression(FillSize))
2582 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Roman Divackye33098f2013-09-24 17:44:41 +00002584 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2585 if (getLexer().isNot(AsmToken::Comma))
2586 return TokError("unexpected token in '.fill' directive");
2587 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002588
David Majnemer522d3db2014-02-01 07:19:38 +00002589 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002590 if (parseAbsoluteExpression(FillExpr))
2591 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002592
Roman Divackye33098f2013-09-24 17:44:41 +00002593 if (getLexer().isNot(AsmToken::EndOfStatement))
2594 return TokError("unexpected token in '.fill' directive");
2595
2596 Lex();
2597 }
2598 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002599
David Majnemer522d3db2014-02-01 07:19:38 +00002600 if (FillSize < 0) {
2601 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2602 NumValues = 0;
2603 }
2604 if (FillSize > 8) {
2605 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2606 FillSize = 8;
2607 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002608
David Majnemer522d3db2014-02-01 07:19:38 +00002609 if (!isUInt<32>(FillExpr) && FillSize > 4)
2610 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2611
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002612 if (NumValues > 0) {
2613 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2614 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2615 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2616 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2617 if (NonZeroFillSize < FillSize)
2618 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2619 }
David Majnemer522d3db2014-02-01 07:19:38 +00002620 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002621
2622 return false;
2623}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002624
Jim Grosbach4b905842013-09-20 23:08:21 +00002625/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002626/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002627bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002628 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002629
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002630 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002631 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002632 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002633 return true;
2634
2635 // Parse optional fill expression.
2636 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2638 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002639 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002640 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002641
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002642 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002643 return true;
2644
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002645 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002646 return TokError("unexpected token in '.org' directive");
2647 }
2648
Sean Callanan686ed8d2010-01-19 20:22:31 +00002649 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002650
Jim Grosbachb5912772012-01-27 00:37:08 +00002651 // Only limited forms of relocatable expressions are accepted here, it
2652 // has to be relative to the current section. The streamer will return
2653 // 'true' if the expression wasn't evaluatable.
2654 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2655 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002656
2657 return false;
2658}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002659
Jim Grosbach4b905842013-09-20 23:08:21 +00002660/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002662bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002663 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002664
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002665 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002666 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002667 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668 return true;
2669
2670 SMLoc MaxBytesLoc;
2671 bool HasFillExpr = false;
2672 int64_t FillExpr = 0;
2673 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002674 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2675 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002676 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002677 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678
2679 // The fill expression can be omitted while specifying a maximum number of
2680 // alignment bytes, e.g:
2681 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002682 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002684 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002685 return true;
2686 }
2687
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002688 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2689 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002690 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002691 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002692
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002693 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002694 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002696
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002697 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698 return TokError("unexpected token in directive");
2699 }
2700 }
2701
Sean Callanan686ed8d2010-01-19 20:22:31 +00002702 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002703
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002704 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002705 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002706
2707 // Compute alignment in bytes.
2708 if (IsPow2) {
2709 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002710 if (Alignment >= 32) {
2711 Error(AlignmentLoc, "invalid alignment value");
2712 Alignment = 31;
2713 }
2714
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002715 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002716 } else {
2717 // Reject alignments that aren't a power of two, for gas compatibility.
2718 if (!isPowerOf2_64(Alignment))
2719 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 }
2721
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002722 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002723 if (MaxBytesLoc.isValid()) {
2724 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002725 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002726 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002727 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002728 }
2729
2730 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002731 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002732 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002733 MaxBytesToFill = 0;
2734 }
2735 }
2736
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002737 // Check whether we should use optimal code alignment for this .align
2738 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002739 const MCSection *Section = getStreamer().getCurrentSection().first;
2740 assert(Section && "must have section to emit alignment");
2741 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002742 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2743 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002744 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002745 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002746 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002747 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2748 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002749 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002750
2751 return false;
2752}
2753
Jim Grosbach4b905842013-09-20 23:08:21 +00002754/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002755/// ::= .file [number] filename
2756/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002757bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002758 // FIXME: I'm not sure what this is.
2759 int64_t FileNumber = -1;
2760 SMLoc FileNumberLoc = getLexer().getLoc();
2761 if (getLexer().is(AsmToken::Integer)) {
2762 FileNumber = getTok().getIntVal();
2763 Lex();
2764
2765 if (FileNumber < 1)
2766 return TokError("file number less than one");
2767 }
2768
2769 if (getLexer().isNot(AsmToken::String))
2770 return TokError("unexpected token in '.file' directive");
2771
2772 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002773 // Allow the strings to have escaped octal character sequence.
2774 std::string Path = getTok().getString();
2775 if (parseEscapedString(Path))
2776 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002777 Lex();
2778
2779 StringRef Directory;
2780 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002781 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002782 if (getLexer().is(AsmToken::String)) {
2783 if (FileNumber == -1)
2784 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002785 if (parseEscapedString(FilenameData))
2786 return true;
2787 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002788 Directory = Path;
2789 Lex();
2790 } else {
2791 Filename = Path;
2792 }
2793
2794 if (getLexer().isNot(AsmToken::EndOfStatement))
2795 return TokError("unexpected token in '.file' directive");
2796
2797 if (FileNumber == -1)
2798 getStreamer().EmitFileDirective(Filename);
2799 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002800 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002801 Error(DirectiveLoc,
2802 "input can't have .file dwarf directives when -g is "
2803 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002804
David Blaikiec714ef42014-03-17 01:52:11 +00002805 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2806 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002807 Error(FileNumberLoc, "file number already allocated");
2808 }
2809
2810 return false;
2811}
2812
Jim Grosbach4b905842013-09-20 23:08:21 +00002813/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002814/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002815bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002816 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2817 if (getLexer().isNot(AsmToken::Integer))
2818 return TokError("unexpected token in '.line' directive");
2819
2820 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002821 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002822 Lex();
2823
2824 // FIXME: Do something with the .line.
2825 }
2826
2827 if (getLexer().isNot(AsmToken::EndOfStatement))
2828 return TokError("unexpected token in '.line' directive");
2829
2830 return false;
2831}
2832
Jim Grosbach4b905842013-09-20 23:08:21 +00002833/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002834/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2835/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2836/// The first number is a file number, must have been previously assigned with
2837/// a .file directive, the second number is the line number and optionally the
2838/// third number is a column position (zero if not specified). The remaining
2839/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002840bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002841 if (getLexer().isNot(AsmToken::Integer))
2842 return TokError("unexpected token in '.loc' directive");
2843 int64_t FileNumber = getTok().getIntVal();
2844 if (FileNumber < 1)
2845 return TokError("file number less than one in '.loc' directive");
2846 if (!getContext().isValidDwarfFileNumber(FileNumber))
2847 return TokError("unassigned file number in '.loc' directive");
2848 Lex();
2849
2850 int64_t LineNumber = 0;
2851 if (getLexer().is(AsmToken::Integer)) {
2852 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002853 if (LineNumber < 0)
2854 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002855 Lex();
2856 }
2857
2858 int64_t ColumnPos = 0;
2859 if (getLexer().is(AsmToken::Integer)) {
2860 ColumnPos = getTok().getIntVal();
2861 if (ColumnPos < 0)
2862 return TokError("column position less than zero in '.loc' directive");
2863 Lex();
2864 }
2865
2866 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2867 unsigned Isa = 0;
2868 int64_t Discriminator = 0;
2869 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2870 for (;;) {
2871 if (getLexer().is(AsmToken::EndOfStatement))
2872 break;
2873
2874 StringRef Name;
2875 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002876 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return TokError("unexpected token in '.loc' directive");
2878
2879 if (Name == "basic_block")
2880 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2881 else if (Name == "prologue_end")
2882 Flags |= DWARF2_FLAG_PROLOGUE_END;
2883 else if (Name == "epilogue_begin")
2884 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2885 else if (Name == "is_stmt") {
2886 Loc = getTok().getLoc();
2887 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002888 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002889 return true;
2890 // The expression must be the constant 0 or 1.
2891 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2892 int Value = MCE->getValue();
2893 if (Value == 0)
2894 Flags &= ~DWARF2_FLAG_IS_STMT;
2895 else if (Value == 1)
2896 Flags |= DWARF2_FLAG_IS_STMT;
2897 else
2898 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002899 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2901 }
Craig Topperf15655b2013-04-22 04:22:40 +00002902 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002903 Loc = getTok().getLoc();
2904 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002905 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return true;
2907 // The expression must be a constant greater or equal to 0.
2908 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2909 int Value = MCE->getValue();
2910 if (Value < 0)
2911 return Error(Loc, "isa number less than zero");
2912 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002913 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return Error(Loc, "isa number not a constant value");
2915 }
Craig Topperf15655b2013-04-22 04:22:40 +00002916 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002917 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002918 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002919 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002920 return Error(Loc, "unknown sub-directive in '.loc' directive");
2921 }
2922
2923 if (getLexer().is(AsmToken::EndOfStatement))
2924 break;
2925 }
2926 }
2927
2928 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2929 Isa, Discriminator, StringRef());
2930
2931 return false;
2932}
2933
Jim Grosbach4b905842013-09-20 23:08:21 +00002934/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002935/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002936bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002937 return TokError("unsupported directive '.stabs'");
2938}
2939
Jim Grosbach4b905842013-09-20 23:08:21 +00002940/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002941/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002942bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002943 StringRef Name;
2944 bool EH = false;
2945 bool Debug = false;
2946
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002947 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002948 return TokError("Expected an identifier");
2949
2950 if (Name == ".eh_frame")
2951 EH = true;
2952 else if (Name == ".debug_frame")
2953 Debug = true;
2954
2955 if (getLexer().is(AsmToken::Comma)) {
2956 Lex();
2957
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002958 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002959 return TokError("Expected an identifier");
2960
2961 if (Name == ".eh_frame")
2962 EH = true;
2963 else if (Name == ".debug_frame")
2964 Debug = true;
2965 }
2966
2967 getStreamer().EmitCFISections(EH, Debug);
2968 return false;
2969}
2970
Jim Grosbach4b905842013-09-20 23:08:21 +00002971/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002972/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002973bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002974 StringRef Simple;
2975 if (getLexer().isNot(AsmToken::EndOfStatement))
2976 if (parseIdentifier(Simple) || Simple != "simple")
2977 return TokError("unexpected token in .cfi_startproc directive");
2978
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00002979 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002980 return false;
2981}
2982
Jim Grosbach4b905842013-09-20 23:08:21 +00002983/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002984/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002985bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002986 getStreamer().EmitCFIEndProc();
2987 return false;
2988}
2989
Jim Grosbach4b905842013-09-20 23:08:21 +00002990/// \brief parse register name or number.
2991bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002992 SMLoc DirectiveLoc) {
2993 unsigned RegNo;
2994
2995 if (getLexer().isNot(AsmToken::Integer)) {
2996 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2997 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002998 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002999 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003000 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003001
3002 return false;
3003}
3004
Jim Grosbach4b905842013-09-20 23:08:21 +00003005/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003006/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003007bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003008 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003009 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003010 return true;
3011
3012 if (getLexer().isNot(AsmToken::Comma))
3013 return TokError("unexpected token in directive");
3014 Lex();
3015
3016 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003017 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003018 return true;
3019
3020 getStreamer().EmitCFIDefCfa(Register, Offset);
3021 return false;
3022}
3023
Jim Grosbach4b905842013-09-20 23:08:21 +00003024/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003025/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003026bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003027 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003028 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003029 return true;
3030
3031 getStreamer().EmitCFIDefCfaOffset(Offset);
3032 return false;
3033}
3034
Jim Grosbach4b905842013-09-20 23:08:21 +00003035/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003036/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003037bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003038 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003039 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 if (getLexer().isNot(AsmToken::Comma))
3043 return TokError("unexpected token in directive");
3044 Lex();
3045
3046 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003047 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return true;
3049
3050 getStreamer().EmitCFIRegister(Register1, Register2);
3051 return false;
3052}
3053
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003054/// parseDirectiveCFIWindowSave
3055/// ::= .cfi_window_save
3056bool AsmParser::parseDirectiveCFIWindowSave() {
3057 getStreamer().EmitCFIWindowSave();
3058 return false;
3059}
3060
Jim Grosbach4b905842013-09-20 23:08:21 +00003061/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003062/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003063bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003064 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003065 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003066 return true;
3067
3068 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3069 return false;
3070}
3071
Jim Grosbach4b905842013-09-20 23:08:21 +00003072/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003073/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003074bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003075 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003076 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003077 return true;
3078
3079 getStreamer().EmitCFIDefCfaRegister(Register);
3080 return false;
3081}
3082
Jim Grosbach4b905842013-09-20 23:08:21 +00003083/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003084/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003085bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003086 int64_t Register = 0;
3087 int64_t Offset = 0;
3088
Jim Grosbach4b905842013-09-20 23:08:21 +00003089 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003090 return true;
3091
3092 if (getLexer().isNot(AsmToken::Comma))
3093 return TokError("unexpected token in directive");
3094 Lex();
3095
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003096 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003097 return true;
3098
3099 getStreamer().EmitCFIOffset(Register, Offset);
3100 return false;
3101}
3102
Jim Grosbach4b905842013-09-20 23:08:21 +00003103/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003104/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003105bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003106 int64_t Register = 0;
3107
Jim Grosbach4b905842013-09-20 23:08:21 +00003108 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 if (getLexer().isNot(AsmToken::Comma))
3112 return TokError("unexpected token in directive");
3113 Lex();
3114
3115 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003116 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003117 return true;
3118
3119 getStreamer().EmitCFIRelOffset(Register, Offset);
3120 return false;
3121}
3122
3123static bool isValidEncoding(int64_t Encoding) {
3124 if (Encoding & ~0xff)
3125 return false;
3126
3127 if (Encoding == dwarf::DW_EH_PE_omit)
3128 return true;
3129
3130 const unsigned Format = Encoding & 0xf;
3131 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3132 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3133 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3134 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3135 return false;
3136
3137 const unsigned Application = Encoding & 0x70;
3138 if (Application != dwarf::DW_EH_PE_absptr &&
3139 Application != dwarf::DW_EH_PE_pcrel)
3140 return false;
3141
3142 return true;
3143}
3144
Jim Grosbach4b905842013-09-20 23:08:21 +00003145/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003146/// IsPersonality true for cfi_personality, false for cfi_lsda
3147/// ::= .cfi_personality encoding, [symbol_name]
3148/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003149bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003150 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003151 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003152 return true;
3153 if (Encoding == dwarf::DW_EH_PE_omit)
3154 return false;
3155
3156 if (!isValidEncoding(Encoding))
3157 return TokError("unsupported encoding.");
3158
3159 if (getLexer().isNot(AsmToken::Comma))
3160 return TokError("unexpected token in directive");
3161 Lex();
3162
3163 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003164 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003165 return TokError("expected identifier in directive");
3166
3167 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3168
3169 if (IsPersonality)
3170 getStreamer().EmitCFIPersonality(Sym, Encoding);
3171 else
3172 getStreamer().EmitCFILsda(Sym, Encoding);
3173 return false;
3174}
3175
Jim Grosbach4b905842013-09-20 23:08:21 +00003176/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003177/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003178bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003179 getStreamer().EmitCFIRememberState();
3180 return false;
3181}
3182
Jim Grosbach4b905842013-09-20 23:08:21 +00003183/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003184/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003185bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003186 getStreamer().EmitCFIRestoreState();
3187 return false;
3188}
3189
Jim Grosbach4b905842013-09-20 23:08:21 +00003190/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003191/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003192bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003193 int64_t Register = 0;
3194
Jim Grosbach4b905842013-09-20 23:08:21 +00003195 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003196 return true;
3197
3198 getStreamer().EmitCFISameValue(Register);
3199 return false;
3200}
3201
Jim Grosbach4b905842013-09-20 23:08:21 +00003202/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003203/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003204bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003205 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003206 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003207 return true;
3208
3209 getStreamer().EmitCFIRestore(Register);
3210 return false;
3211}
3212
Jim Grosbach4b905842013-09-20 23:08:21 +00003213/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003214/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003215bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003216 std::string Values;
3217 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003218 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003219 return true;
3220
3221 Values.push_back((uint8_t)CurrValue);
3222
3223 while (getLexer().is(AsmToken::Comma)) {
3224 Lex();
3225
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003226 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003227 return true;
3228
3229 Values.push_back((uint8_t)CurrValue);
3230 }
3231
3232 getStreamer().EmitCFIEscape(Values);
3233 return false;
3234}
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003237/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003238bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 if (getLexer().isNot(AsmToken::EndOfStatement))
3240 return Error(getLexer().getLoc(),
3241 "unexpected token in '.cfi_signal_frame'");
3242
3243 getStreamer().EmitCFISignalFrame();
3244 return false;
3245}
3246
Jim Grosbach4b905842013-09-20 23:08:21 +00003247/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003248/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003249bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003250 int64_t Register = 0;
3251
Jim Grosbach4b905842013-09-20 23:08:21 +00003252 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003253 return true;
3254
3255 getStreamer().EmitCFIUndefined(Register);
3256 return false;
3257}
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003260/// ::= .macros_on
3261/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003262bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003263 if (getLexer().isNot(AsmToken::EndOfStatement))
3264 return Error(getLexer().getLoc(),
3265 "unexpected token in '" + Directive + "' directive");
3266
Jim Grosbach4b905842013-09-20 23:08:21 +00003267 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003268 return false;
3269}
3270
Jim Grosbach4b905842013-09-20 23:08:21 +00003271/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003272/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003273bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003274 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003275 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003276 return TokError("expected identifier in '.macro' directive");
3277
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003278 if (getLexer().is(AsmToken::Comma))
3279 Lex();
3280
Eli Bendersky17233942013-01-15 22:59:42 +00003281 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003282 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003283
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003284 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003285 return Error(Lexer.getLoc(),
3286 "Vararg parameter '" + Parameters.back().Name +
3287 "' should be last one in the list of parameters.");
3288
David Majnemer91fc4c22014-01-29 18:57:46 +00003289 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003290 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003291 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003292
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003293 if (Lexer.is(AsmToken::Colon)) {
3294 Lex(); // consume ':'
3295
3296 SMLoc QualLoc;
3297 StringRef Qualifier;
3298
3299 QualLoc = Lexer.getLoc();
3300 if (parseIdentifier(Qualifier))
3301 return Error(QualLoc, "missing parameter qualifier for "
3302 "'" + Parameter.Name + "' in macro '" + Name + "'");
3303
3304 if (Qualifier == "req")
3305 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003306 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003307 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003308 else
3309 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3310 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3311 }
3312
David Majnemer91fc4c22014-01-29 18:57:46 +00003313 if (getLexer().is(AsmToken::Equal)) {
3314 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003315
3316 SMLoc ParamLoc;
3317
3318 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003319 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003320 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003321
3322 if (Parameter.Required)
3323 Warning(ParamLoc, "pointless default value for required parameter "
3324 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003325 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003326
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003327 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003328
3329 if (getLexer().is(AsmToken::Comma))
3330 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003331 }
3332
3333 // Eat the end of statement.
3334 Lex();
3335
3336 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003337 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003338
3339 // Lex the macro definition.
3340 for (;;) {
3341 // Check whether we have reached the end of the file.
3342 if (getLexer().is(AsmToken::Eof))
3343 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3344
3345 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003346 if (getLexer().is(AsmToken::Identifier)) {
3347 if (getTok().getIdentifier() == ".endm" ||
3348 getTok().getIdentifier() == ".endmacro") {
3349 if (MacroDepth == 0) { // Outermost macro.
3350 EndToken = getTok();
3351 Lex();
3352 if (getLexer().isNot(AsmToken::EndOfStatement))
3353 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3354 "' directive");
3355 break;
3356 } else {
3357 // Otherwise we just found the end of an inner macro.
3358 --MacroDepth;
3359 }
3360 } else if (getTok().getIdentifier() == ".macro") {
3361 // We allow nested macros. Those aren't instantiated until the outermost
3362 // macro is expanded so just ignore them for now.
3363 ++MacroDepth;
3364 }
Eli Bendersky17233942013-01-15 22:59:42 +00003365 }
3366
3367 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003368 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003369 }
3370
Jim Grosbach4b905842013-09-20 23:08:21 +00003371 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003372 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3373 }
3374
3375 const char *BodyStart = StartToken.getLoc().getPointer();
3376 const char *BodyEnd = EndToken.getLoc().getPointer();
3377 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003378 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003379 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003380 return false;
3381}
3382
Jim Grosbach4b905842013-09-20 23:08:21 +00003383/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384///
3385/// With the support added for named parameters there may be code out there that
3386/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003387/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003389/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3391/// warning that the positional parameter found in body which have no effect.
3392/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003393/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394/// intended or change the macro to use the named parameters. It is possible
3395/// this warning will trigger when the none of the named parameters are used
3396/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003397void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003398 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003399 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003400 // If this macro is not defined with named parameters the warning we are
3401 // checking for here doesn't apply.
3402 unsigned NParameters = Parameters.size();
3403 if (NParameters == 0)
3404 return;
3405
3406 bool NamedParametersFound = false;
3407 bool PositionalParametersFound = false;
3408
3409 // Look at the body of the macro for use of both the named parameters and what
3410 // are likely to be positional parameters. This is what expandMacro() is
3411 // doing when it finds the parameters in the body.
3412 while (!Body.empty()) {
3413 // Scan for the next possible parameter.
3414 std::size_t End = Body.size(), Pos = 0;
3415 for (; Pos != End; ++Pos) {
3416 // Check for a substitution or escape.
3417 // This macro is defined with parameters, look for \foo, \bar, etc.
3418 if (Body[Pos] == '\\' && Pos + 1 != End)
3419 break;
3420
3421 // This macro should have parameters, but look for $0, $1, ..., $n too.
3422 if (Body[Pos] != '$' || Pos + 1 == End)
3423 continue;
3424 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003425 if (Next == '$' || Next == 'n' ||
3426 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003427 break;
3428 }
3429
3430 // Check if we reached the end.
3431 if (Pos == End)
3432 break;
3433
3434 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003435 switch (Body[Pos + 1]) {
3436 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003437 case '$':
3438 break;
3439
Jim Grosbach4b905842013-09-20 23:08:21 +00003440 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003441 case 'n':
3442 PositionalParametersFound = true;
3443 break;
3444
Jim Grosbach4b905842013-09-20 23:08:21 +00003445 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003446 default: {
3447 PositionalParametersFound = true;
3448 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003449 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003450 }
3451 Pos += 2;
3452 } else {
3453 unsigned I = Pos + 1;
3454 while (isIdentifierChar(Body[I]) && I + 1 != End)
3455 ++I;
3456
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 const char *Begin = Body.data() + Pos + 1;
3458 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003459 unsigned Index = 0;
3460 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003461 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003462 break;
3463
3464 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003465 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3466 Pos += 3;
3467 else {
3468 Pos = I;
3469 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003470 } else {
3471 NamedParametersFound = true;
3472 Pos += 1 + Argument.size();
3473 }
3474 }
3475 // Update the scan point.
3476 Body = Body.substr(Pos);
3477 }
3478
3479 if (!NamedParametersFound && PositionalParametersFound)
3480 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3481 "used in macro body, possible positional parameter "
3482 "found in body which will have no effect");
3483}
3484
Nico Weber155dccd12014-07-24 17:08:39 +00003485/// parseDirectiveExitMacro
3486/// ::= .exitm
3487bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3488 if (getLexer().isNot(AsmToken::EndOfStatement))
3489 return TokError("unexpected token in '" + Directive + "' directive");
3490
3491 if (!isInsideMacroInstantiation())
3492 return TokError("unexpected '" + Directive + "' in file, "
3493 "no current macro definition");
3494
3495 // Exit all conditionals that are active in the current macro.
3496 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3497 TheCondState = TheCondStack.back();
3498 TheCondStack.pop_back();
3499 }
3500
3501 handleMacroExit();
3502 return false;
3503}
3504
Jim Grosbach4b905842013-09-20 23:08:21 +00003505/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003506/// ::= .endm
3507/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003508bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003509 if (getLexer().isNot(AsmToken::EndOfStatement))
3510 return TokError("unexpected token in '" + Directive + "' directive");
3511
3512 // If we are inside a macro instantiation, terminate the current
3513 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 if (isInsideMacroInstantiation()) {
3515 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003516 return false;
3517 }
3518
3519 // Otherwise, this .endmacro is a stray entry in the file; well formed
3520 // .endmacro directives are handled during the macro definition parsing.
3521 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003522 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003523}
3524
Jim Grosbach4b905842013-09-20 23:08:21 +00003525/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003526/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003528 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003529 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003530 return TokError("expected identifier in '.purgem' directive");
3531
3532 if (getLexer().isNot(AsmToken::EndOfStatement))
3533 return TokError("unexpected token in '.purgem' directive");
3534
Jim Grosbach4b905842013-09-20 23:08:21 +00003535 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003536 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3537
Jim Grosbach4b905842013-09-20 23:08:21 +00003538 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003539 return false;
3540}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003541
Jim Grosbach4b905842013-09-20 23:08:21 +00003542/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003543/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003544bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003545 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003546
3547 // Expect a single argument: an expression that evaluates to a constant
3548 // in the inclusive range 0-30.
3549 SMLoc ExprLoc = getLexer().getLoc();
3550 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003551 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003552 return true;
3553 else if (getLexer().isNot(AsmToken::EndOfStatement))
3554 return TokError("unexpected token after expression in"
3555 " '.bundle_align_mode' directive");
3556 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3557 return Error(ExprLoc,
3558 "invalid bundle alignment size (expected between 0 and 30)");
3559
3560 Lex();
3561
3562 // Because of AlignSizePow2's verified range we can safely truncate it to
3563 // unsigned.
3564 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3565 return false;
3566}
3567
Jim Grosbach4b905842013-09-20 23:08:21 +00003568/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003569/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003570bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003571 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003572 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003573
Eli Bendersky802b6282013-01-07 21:51:08 +00003574 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3575 StringRef Option;
3576 SMLoc Loc = getTok().getLoc();
3577 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003578 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003579
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003580 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003581 return Error(Loc, kInvalidOptionError);
3582
3583 if (Option != "align_to_end")
3584 return Error(Loc, kInvalidOptionError);
3585 else if (getLexer().isNot(AsmToken::EndOfStatement))
3586 return Error(Loc,
3587 "unexpected token after '.bundle_lock' directive option");
3588 AlignToEnd = true;
3589 }
3590
Eli Benderskyf483ff92012-12-20 19:05:53 +00003591 Lex();
3592
Eli Bendersky802b6282013-01-07 21:51:08 +00003593 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003594 return false;
3595}
3596
Jim Grosbach4b905842013-09-20 23:08:21 +00003597/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003598/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003599bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003600 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003601
3602 if (getLexer().isNot(AsmToken::EndOfStatement))
3603 return TokError("unexpected token in '.bundle_unlock' directive");
3604 Lex();
3605
3606 getStreamer().EmitBundleUnlock();
3607 return false;
3608}
3609
Jim Grosbach4b905842013-09-20 23:08:21 +00003610/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003611/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003612bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003613 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003614
3615 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003616 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003617 return true;
3618
3619 int64_t FillExpr = 0;
3620 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3621 if (getLexer().isNot(AsmToken::Comma))
3622 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3623 Lex();
3624
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003625 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003626 return true;
3627
3628 if (getLexer().isNot(AsmToken::EndOfStatement))
3629 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3630 }
3631
3632 Lex();
3633
3634 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003635 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3636 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003637
3638 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003639 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003640
3641 return false;
3642}
3643
Jim Grosbach4b905842013-09-20 23:08:21 +00003644/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003645/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003646bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003647 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003648 const MCExpr *Value;
3649
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003650 for (;;) {
3651 if (parseExpression(Value))
3652 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003653
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003654 if (Signed)
3655 getStreamer().EmitSLEB128Value(Value);
3656 else
3657 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003658
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003659 if (getLexer().is(AsmToken::EndOfStatement))
3660 break;
3661
3662 if (getLexer().isNot(AsmToken::Comma))
3663 return TokError("unexpected token in directive");
3664 Lex();
3665 }
Eli Bendersky17233942013-01-15 22:59:42 +00003666
3667 return false;
3668}
3669
Jim Grosbach4b905842013-09-20 23:08:21 +00003670/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003671/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003672bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003674 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003675 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003676 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003677
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003678 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003679 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003680
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003681 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003682
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003683 // Assembler local symbols don't make any sense here. Complain loudly.
3684 if (Sym->isTemporary())
3685 return Error(Loc, "non-local symbol required in directive");
3686
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003687 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3688 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003689
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003690 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003691 break;
3692
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003693 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003694 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003695 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003696 }
3697 }
3698
Sean Callanan686ed8d2010-01-19 20:22:31 +00003699 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003700 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003701}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003702
Jim Grosbach4b905842013-09-20 23:08:21 +00003703/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003704/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003705bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003706 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003707
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003708 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003709 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003710 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003711 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003712
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003713 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003714 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003716 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003717 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003718 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003719
3720 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003721 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003722 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003723 return true;
3724
3725 int64_t Pow2Alignment = 0;
3726 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003727 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003728 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003729 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003730 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003731 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003732
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003733 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3734 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003735 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3736
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003737 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003738 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3739 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003740 if (!isPowerOf2_64(Pow2Alignment))
3741 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3742 Pow2Alignment = Log2_64(Pow2Alignment);
3743 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003744 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003745
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003746 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003747 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003748
Sean Callanan686ed8d2010-01-19 20:22:31 +00003749 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003750
Chris Lattner28ad7542009-07-09 17:25:12 +00003751 // NOTE: a size of zero for a .comm should create a undefined symbol
3752 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003753 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003754 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003755 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003756
Eric Christopherbc818852010-05-14 01:38:54 +00003757 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003758 // may internally end up wanting an alignment in bytes.
3759 // FIXME: Diagnose overflow.
3760 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003761 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003762 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003763
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003764 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003765 return Error(IDLoc, "invalid symbol redefinition");
3766
Chris Lattner28ad7542009-07-09 17:25:12 +00003767 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003768 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003769 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003770 return false;
3771 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003772
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003773 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003774 return false;
3775}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003776
Jim Grosbach4b905842013-09-20 23:08:21 +00003777/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003778/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003779bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003780 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003781 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003782
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003783 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003784 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003785 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003786
Sean Callanan686ed8d2010-01-19 20:22:31 +00003787 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003788
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003789 if (Str.empty())
3790 Error(Loc, ".abort detected. Assembly stopping.");
3791 else
3792 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003793 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003794
3795 return false;
3796}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003797
Jim Grosbach4b905842013-09-20 23:08:21 +00003798/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003799/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003800bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003801 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003802 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003803
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003804 // Allow the strings to have escaped octal character sequence.
3805 std::string Filename;
3806 if (parseEscapedString(Filename))
3807 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003808 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003809 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003810
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003811 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003812 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003813
Chris Lattner693fbb82009-07-16 06:14:39 +00003814 // Attempt to switch the lexer to the included file before consuming the end
3815 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003816 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003817 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003818 return true;
3819 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003820
3821 return false;
3822}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003823
Jim Grosbach4b905842013-09-20 23:08:21 +00003824/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003825/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003826bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003827 if (getLexer().isNot(AsmToken::String))
3828 return TokError("expected string in '.incbin' directive");
3829
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003830 // Allow the strings to have escaped octal character sequence.
3831 std::string Filename;
3832 if (parseEscapedString(Filename))
3833 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003834 SMLoc IncbinLoc = getLexer().getLoc();
3835 Lex();
3836
3837 if (getLexer().isNot(AsmToken::EndOfStatement))
3838 return TokError("unexpected token in '.incbin' directive");
3839
Kevin Enderby109f25c2011-12-14 21:47:48 +00003840 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003841 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003842 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3843 return true;
3844 }
3845
3846 return false;
3847}
3848
Jim Grosbach4b905842013-09-20 23:08:21 +00003849/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003850/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3851bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852 TheCondStack.push_back(TheCondState);
3853 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003854 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003855 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003856 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003857 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003858 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003859 return true;
3860
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003861 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003862 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003863
Sean Callanan686ed8d2010-01-19 20:22:31 +00003864 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003865
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003866 switch (DirKind) {
3867 default:
3868 llvm_unreachable("unsupported directive");
3869 case DK_IF:
3870 case DK_IFNE:
3871 break;
3872 case DK_IFEQ:
3873 ExprValue = ExprValue == 0;
3874 break;
3875 case DK_IFGE:
3876 ExprValue = ExprValue >= 0;
3877 break;
3878 case DK_IFGT:
3879 ExprValue = ExprValue > 0;
3880 break;
3881 case DK_IFLE:
3882 ExprValue = ExprValue <= 0;
3883 break;
3884 case DK_IFLT:
3885 ExprValue = ExprValue < 0;
3886 break;
3887 }
3888
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003889 TheCondState.CondMet = ExprValue;
3890 TheCondState.Ignore = !TheCondState.CondMet;
3891 }
3892
3893 return false;
3894}
3895
Jim Grosbach4b905842013-09-20 23:08:21 +00003896/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003897/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003898bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003899 TheCondStack.push_back(TheCondState);
3900 TheCondState.TheCond = AsmCond::IfCond;
3901
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003902 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003903 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003904 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003905 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003906
3907 if (getLexer().isNot(AsmToken::EndOfStatement))
3908 return TokError("unexpected token in '.ifb' directive");
3909
3910 Lex();
3911
3912 TheCondState.CondMet = ExpectBlank == Str.empty();
3913 TheCondState.Ignore = !TheCondState.CondMet;
3914 }
3915
3916 return false;
3917}
3918
Jim Grosbach4b905842013-09-20 23:08:21 +00003919/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003920/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003921/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003922bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003923 TheCondStack.push_back(TheCondState);
3924 TheCondState.TheCond = AsmCond::IfCond;
3925
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003926 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003927 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003928 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003929 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003930
3931 if (getLexer().isNot(AsmToken::Comma))
3932 return TokError("unexpected token in '.ifc' directive");
3933
3934 Lex();
3935
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003936 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003937
3938 if (getLexer().isNot(AsmToken::EndOfStatement))
3939 return TokError("unexpected token in '.ifc' directive");
3940
3941 Lex();
3942
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003943 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003944 TheCondState.Ignore = !TheCondState.CondMet;
3945 }
3946
3947 return false;
3948}
3949
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003950/// parseDirectiveIfeqs
3951/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003952bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003953 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003954 if (ExpectEqual)
3955 TokError("expected string parameter for '.ifeqs' directive");
3956 else
3957 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003958 eatToEndOfStatement();
3959 return true;
3960 }
3961
3962 StringRef String1 = getTok().getStringContents();
3963 Lex();
3964
3965 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003966 if (ExpectEqual)
3967 TokError("expected comma after first string for '.ifeqs' directive");
3968 else
3969 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003970 eatToEndOfStatement();
3971 return true;
3972 }
3973
3974 Lex();
3975
3976 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003977 if (ExpectEqual)
3978 TokError("expected string parameter for '.ifeqs' directive");
3979 else
3980 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003981 eatToEndOfStatement();
3982 return true;
3983 }
3984
3985 StringRef String2 = getTok().getStringContents();
3986 Lex();
3987
3988 TheCondStack.push_back(TheCondState);
3989 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00003990 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003991 TheCondState.Ignore = !TheCondState.CondMet;
3992
3993 return false;
3994}
3995
Jim Grosbach4b905842013-09-20 23:08:21 +00003996/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003997/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003998bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003999 StringRef Name;
4000 TheCondStack.push_back(TheCondState);
4001 TheCondState.TheCond = AsmCond::IfCond;
4002
4003 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004004 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004005 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004006 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004007 return TokError("expected identifier after '.ifdef'");
4008
4009 Lex();
4010
4011 MCSymbol *Sym = getContext().LookupSymbol(Name);
4012
4013 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004014 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004015 else
Craig Topper353eda42014-04-24 06:44:33 +00004016 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004017 TheCondState.Ignore = !TheCondState.CondMet;
4018 }
4019
4020 return false;
4021}
4022
Jim Grosbach4b905842013-09-20 23:08:21 +00004023/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004024/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004025bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004026 if (TheCondState.TheCond != AsmCond::IfCond &&
4027 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004028 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4029 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004030 TheCondState.TheCond = AsmCond::ElseIfCond;
4031
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004032 bool LastIgnoreState = false;
4033 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004034 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004035 if (LastIgnoreState || TheCondState.CondMet) {
4036 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004037 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004038 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004039 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004040 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004041 return true;
4042
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004043 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004044 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004045
Sean Callanan686ed8d2010-01-19 20:22:31 +00004046 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004047 TheCondState.CondMet = ExprValue;
4048 TheCondState.Ignore = !TheCondState.CondMet;
4049 }
4050
4051 return false;
4052}
4053
Jim Grosbach4b905842013-09-20 23:08:21 +00004054/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004055/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004056bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004057 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004058 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004059
Sean Callanan686ed8d2010-01-19 20:22:31 +00004060 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004061
4062 if (TheCondState.TheCond != AsmCond::IfCond &&
4063 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004064 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4065 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004066 TheCondState.TheCond = AsmCond::ElseCond;
4067 bool LastIgnoreState = false;
4068 if (!TheCondStack.empty())
4069 LastIgnoreState = TheCondStack.back().Ignore;
4070 if (LastIgnoreState || TheCondState.CondMet)
4071 TheCondState.Ignore = true;
4072 else
4073 TheCondState.Ignore = false;
4074
4075 return false;
4076}
4077
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004078/// parseDirectiveEnd
4079/// ::= .end
4080bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4081 if (getLexer().isNot(AsmToken::EndOfStatement))
4082 return TokError("unexpected token in '.end' directive");
4083
4084 Lex();
4085
4086 while (Lexer.isNot(AsmToken::Eof))
4087 Lex();
4088
4089 return false;
4090}
4091
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004092/// parseDirectiveError
4093/// ::= .err
4094/// ::= .error [string]
4095bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4096 if (!TheCondStack.empty()) {
4097 if (TheCondStack.back().Ignore) {
4098 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004099 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004100 }
4101 }
4102
4103 if (!WithMessage)
4104 return Error(L, ".err encountered");
4105
4106 StringRef Message = ".error directive invoked in source file";
4107 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4108 if (Lexer.isNot(AsmToken::String)) {
4109 TokError(".error argument must be a string");
4110 eatToEndOfStatement();
4111 return true;
4112 }
4113
4114 Message = getTok().getStringContents();
4115 Lex();
4116 }
4117
4118 Error(L, Message);
4119 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004120}
4121
Nico Weber404012b2014-07-24 16:26:06 +00004122/// parseDirectiveWarning
4123/// ::= .warning [string]
4124bool AsmParser::parseDirectiveWarning(SMLoc L) {
4125 if (!TheCondStack.empty()) {
4126 if (TheCondStack.back().Ignore) {
4127 eatToEndOfStatement();
4128 return false;
4129 }
4130 }
4131
4132 StringRef Message = ".warning directive invoked in source file";
4133 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4134 if (Lexer.isNot(AsmToken::String)) {
4135 TokError(".warning argument must be a string");
4136 eatToEndOfStatement();
4137 return true;
4138 }
4139
4140 Message = getTok().getStringContents();
4141 Lex();
4142 }
4143
4144 Warning(L, Message);
4145 return false;
4146}
4147
Jim Grosbach4b905842013-09-20 23:08:21 +00004148/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004149/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004150bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004151 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004152 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004153
Sean Callanan686ed8d2010-01-19 20:22:31 +00004154 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004155
Jim Grosbach4b905842013-09-20 23:08:21 +00004156 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004157 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4158 ".else");
4159 if (!TheCondStack.empty()) {
4160 TheCondState = TheCondStack.back();
4161 TheCondStack.pop_back();
4162 }
4163
4164 return false;
4165}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004166
Eli Bendersky17233942013-01-15 22:59:42 +00004167void AsmParser::initializeDirectiveKindMap() {
4168 DirectiveKindMap[".set"] = DK_SET;
4169 DirectiveKindMap[".equ"] = DK_EQU;
4170 DirectiveKindMap[".equiv"] = DK_EQUIV;
4171 DirectiveKindMap[".ascii"] = DK_ASCII;
4172 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4173 DirectiveKindMap[".string"] = DK_STRING;
4174 DirectiveKindMap[".byte"] = DK_BYTE;
4175 DirectiveKindMap[".short"] = DK_SHORT;
4176 DirectiveKindMap[".value"] = DK_VALUE;
4177 DirectiveKindMap[".2byte"] = DK_2BYTE;
4178 DirectiveKindMap[".long"] = DK_LONG;
4179 DirectiveKindMap[".int"] = DK_INT;
4180 DirectiveKindMap[".4byte"] = DK_4BYTE;
4181 DirectiveKindMap[".quad"] = DK_QUAD;
4182 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004183 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004184 DirectiveKindMap[".single"] = DK_SINGLE;
4185 DirectiveKindMap[".float"] = DK_FLOAT;
4186 DirectiveKindMap[".double"] = DK_DOUBLE;
4187 DirectiveKindMap[".align"] = DK_ALIGN;
4188 DirectiveKindMap[".align32"] = DK_ALIGN32;
4189 DirectiveKindMap[".balign"] = DK_BALIGN;
4190 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4191 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4192 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4193 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4194 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4195 DirectiveKindMap[".org"] = DK_ORG;
4196 DirectiveKindMap[".fill"] = DK_FILL;
4197 DirectiveKindMap[".zero"] = DK_ZERO;
4198 DirectiveKindMap[".extern"] = DK_EXTERN;
4199 DirectiveKindMap[".globl"] = DK_GLOBL;
4200 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004201 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4202 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4203 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4204 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4205 DirectiveKindMap[".reference"] = DK_REFERENCE;
4206 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4207 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4208 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4209 DirectiveKindMap[".comm"] = DK_COMM;
4210 DirectiveKindMap[".common"] = DK_COMMON;
4211 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4212 DirectiveKindMap[".abort"] = DK_ABORT;
4213 DirectiveKindMap[".include"] = DK_INCLUDE;
4214 DirectiveKindMap[".incbin"] = DK_INCBIN;
4215 DirectiveKindMap[".code16"] = DK_CODE16;
4216 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4217 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004218 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004219 DirectiveKindMap[".irp"] = DK_IRP;
4220 DirectiveKindMap[".irpc"] = DK_IRPC;
4221 DirectiveKindMap[".endr"] = DK_ENDR;
4222 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4223 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4224 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4225 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004226 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4227 DirectiveKindMap[".ifge"] = DK_IFGE;
4228 DirectiveKindMap[".ifgt"] = DK_IFGT;
4229 DirectiveKindMap[".ifle"] = DK_IFLE;
4230 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004231 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004232 DirectiveKindMap[".ifb"] = DK_IFB;
4233 DirectiveKindMap[".ifnb"] = DK_IFNB;
4234 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004235 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004236 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004237 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004238 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4239 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4240 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4241 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4242 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004243 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004244 DirectiveKindMap[".endif"] = DK_ENDIF;
4245 DirectiveKindMap[".skip"] = DK_SKIP;
4246 DirectiveKindMap[".space"] = DK_SPACE;
4247 DirectiveKindMap[".file"] = DK_FILE;
4248 DirectiveKindMap[".line"] = DK_LINE;
4249 DirectiveKindMap[".loc"] = DK_LOC;
4250 DirectiveKindMap[".stabs"] = DK_STABS;
4251 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4252 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4253 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4254 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4255 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4256 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4257 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4258 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4259 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4260 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4261 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4262 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4263 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4264 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4265 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4266 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4267 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4268 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4269 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4270 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4271 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004272 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004273 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4274 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4275 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004276 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004277 DirectiveKindMap[".endm"] = DK_ENDM;
4278 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4279 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004280 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004281 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004282 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004283}
4284
Jim Grosbach4b905842013-09-20 23:08:21 +00004285MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004286 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004287
Rafael Espindola34b9c512012-06-03 23:57:14 +00004288 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004289 for (;;) {
4290 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004291 if (getLexer().is(AsmToken::Eof)) {
4292 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004293 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004294 }
4295
Rafael Espindola34b9c512012-06-03 23:57:14 +00004296 if (Lexer.is(AsmToken::Identifier) &&
4297 (getTok().getIdentifier() == ".rept")) {
4298 ++NestLevel;
4299 }
4300
4301 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004302 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004303 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004304 EndToken = getTok();
4305 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004306 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4307 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004308 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004309 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004310 break;
4311 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004312 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004313 }
4314
Rafael Espindola34b9c512012-06-03 23:57:14 +00004315 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004316 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004317 }
4318
4319 const char *BodyStart = StartToken.getLoc().getPointer();
4320 const char *BodyEnd = EndToken.getLoc().getPointer();
4321 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4322
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004324 MacroLikeBodies.push_back(
4325 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004326 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004327}
4328
Jim Grosbach4b905842013-09-20 23:08:21 +00004329void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004330 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004331 OS << ".endr\n";
4332
Rafael Espindola3560ff22014-08-27 20:03:13 +00004333 std::unique_ptr<MemoryBuffer> Instantiation =
4334 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004335
Rafael Espindola34b9c512012-06-03 23:57:14 +00004336 // Create the macro instantiation object and add to the current macro
4337 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004338 MacroInstantiation *MI = new MacroInstantiation(
4339 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004340 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004341
Rafael Espindola34b9c512012-06-03 23:57:14 +00004342 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004343 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004344 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004345 Lex();
4346}
4347
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004348/// parseDirectiveRept
4349/// ::= .rep | .rept count
4350bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004351 const MCExpr *CountExpr;
4352 SMLoc CountLoc = getTok().getLoc();
4353 if (parseExpression(CountExpr))
4354 return true;
4355
Rafael Espindola34b9c512012-06-03 23:57:14 +00004356 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004357 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4358 eatToEndOfStatement();
4359 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4360 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004361
4362 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004363 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004364
4365 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004366 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004367
4368 // Eat the end of statement.
4369 Lex();
4370
4371 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004372 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004373 if (!M)
4374 return true;
4375
4376 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4377 // to hold the macro body with substitutions.
4378 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004379 raw_svector_ostream OS(Buf);
4380 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004381 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004382 return true;
4383 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004384 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004385
4386 return false;
4387}
4388
Jim Grosbach4b905842013-09-20 23:08:21 +00004389/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004390/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004391bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004392 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004393
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004394 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004395 return TokError("expected identifier in '.irp' directive");
4396
Rafael Espindola768b41c2012-06-15 14:02:34 +00004397 if (Lexer.isNot(AsmToken::Comma))
4398 return TokError("expected comma in '.irp' directive");
4399
4400 Lex();
4401
Eli Bendersky38274122013-01-14 23:22:36 +00004402 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004403 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004404 return true;
4405
4406 // Eat the end of statement.
4407 Lex();
4408
4409 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004410 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004411 if (!M)
4412 return true;
4413
4414 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4415 // to hold the macro body with substitutions.
4416 SmallString<256> Buf;
4417 raw_svector_ostream OS(Buf);
4418
Eli Bendersky38274122013-01-14 23:22:36 +00004419 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004420 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004421 return true;
4422 }
4423
Jim Grosbach4b905842013-09-20 23:08:21 +00004424 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004425
4426 return false;
4427}
4428
Jim Grosbach4b905842013-09-20 23:08:21 +00004429/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004430/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004431bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004432 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004433
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004434 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004435 return TokError("expected identifier in '.irpc' directive");
4436
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004437 if (Lexer.isNot(AsmToken::Comma))
4438 return TokError("expected comma in '.irpc' directive");
4439
4440 Lex();
4441
Eli Bendersky38274122013-01-14 23:22:36 +00004442 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004443 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004444 return true;
4445
4446 if (A.size() != 1 || A.front().size() != 1)
4447 return TokError("unexpected token in '.irpc' directive");
4448
4449 // Eat the end of statement.
4450 Lex();
4451
4452 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004453 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004454 if (!M)
4455 return true;
4456
4457 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4458 // to hold the macro body with substitutions.
4459 SmallString<256> Buf;
4460 raw_svector_ostream OS(Buf);
4461
4462 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004463 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004464 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004465 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004466
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004467 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004468 return true;
4469 }
4470
Jim Grosbach4b905842013-09-20 23:08:21 +00004471 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004472
4473 return false;
4474}
4475
Jim Grosbach4b905842013-09-20 23:08:21 +00004476bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004477 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004478 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004479
4480 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004481 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004482 assert(getLexer().is(AsmToken::EndOfStatement));
4483
Jim Grosbach4b905842013-09-20 23:08:21 +00004484 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004485 return false;
4486}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004487
Jim Grosbach4b905842013-09-20 23:08:21 +00004488bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004489 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004490 const MCExpr *Value;
4491 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004492 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004493 return true;
4494 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4495 if (!MCE)
4496 return Error(ExprLoc, "unexpected expression in _emit");
4497 uint64_t IntValue = MCE->getValue();
4498 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4499 return Error(ExprLoc, "literal value out of range for directive");
4500
Chad Rosierc7f552c2013-02-12 21:33:51 +00004501 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4502 return false;
4503}
4504
Jim Grosbach4b905842013-09-20 23:08:21 +00004505bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004506 const MCExpr *Value;
4507 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004508 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004509 return true;
4510 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4511 if (!MCE)
4512 return Error(ExprLoc, "unexpected expression in align");
4513 uint64_t IntValue = MCE->getValue();
4514 if (!isPowerOf2_64(IntValue))
4515 return Error(ExprLoc, "literal value not a power of two greater then zero");
4516
Jim Grosbach4b905842013-09-20 23:08:21 +00004517 Info.AsmRewrites->push_back(
4518 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004519 return false;
4520}
4521
Chad Rosierf43fcf52013-02-13 21:27:17 +00004522// We are comparing pointers, but the pointers are relative to a single string.
4523// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004524static int rewritesSort(const AsmRewrite *AsmRewriteA,
4525 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004526 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4527 return -1;
4528 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4529 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004530
Chad Rosierfce4fab2013-04-08 17:43:47 +00004531 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4532 // rewrite to the same location. Make sure the SizeDirective rewrite is
4533 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4534 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004535 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4536 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004537 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004538
Jim Grosbach4b905842013-09-20 23:08:21 +00004539 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4540 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004541 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004542 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004543}
4544
Jim Grosbach4b905842013-09-20 23:08:21 +00004545bool AsmParser::parseMSInlineAsm(
4546 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4547 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4548 SmallVectorImpl<std::string> &Constraints,
4549 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4550 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004551 SmallVector<void *, 4> InputDecls;
4552 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004553 SmallVector<bool, 4> InputDeclsAddressOf;
4554 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004555 SmallVector<std::string, 4> InputConstraints;
4556 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004557 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004558
Benjamin Kramer1a136112013-02-15 20:37:21 +00004559 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004560
4561 // Prime the lexer.
4562 Lex();
4563
4564 // While we have input, parse each statement.
4565 unsigned InputIdx = 0;
4566 unsigned OutputIdx = 0;
4567 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004568 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004569 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004570 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004571
Chad Rosier149e8e02012-12-12 22:45:52 +00004572 if (Info.ParseError)
4573 return true;
4574
Benjamin Kramer1a136112013-02-15 20:37:21 +00004575 if (Info.Opcode == ~0U)
4576 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004577
Benjamin Kramer1a136112013-02-15 20:37:21 +00004578 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004579
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 // Build the list of clobbers, outputs and inputs.
4581 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004582 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004583
Benjamin Kramer1a136112013-02-15 20:37:21 +00004584 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004585 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004586 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004587
Benjamin Kramer1a136112013-02-15 20:37:21 +00004588 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004589 if (Operand.isReg() && !Operand.needAddressOf() &&
4590 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004591 unsigned NumDefs = Desc.getNumDefs();
4592 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004593 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4594 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004595 continue;
4596 }
4597
4598 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004599 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004600 if (SymName.empty())
4601 continue;
4602
David Blaikie960ea3f2014-06-08 16:18:35 +00004603 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004604 if (!OpDecl)
4605 continue;
4606
4607 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004608 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004609 if (isOutput) {
4610 ++InputIdx;
4611 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004612 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004613 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004614 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 } else {
4616 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004617 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4618 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004619 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004620 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004621 }
Reid Kleckneree088972013-12-10 18:27:32 +00004622
4623 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004624 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4625 Desc.getNumImplicitDefs());
4626 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004627 }
4628
4629 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004630 NumOutputs = OutputDecls.size();
4631 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004632
4633 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004634 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4635 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4636 ClobberRegs.end());
4637 Clobbers.assign(ClobberRegs.size(), std::string());
4638 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4639 raw_string_ostream OS(Clobbers[I]);
4640 IP->printRegName(OS, ClobberRegs[I]);
4641 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004642
4643 // Merge the various outputs and inputs. Output are expected first.
4644 if (NumOutputs || NumInputs) {
4645 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004646 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004647 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004648 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004649 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004650 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004651 }
4652 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004653 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004654 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004655 }
4656 }
4657
4658 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004659 std::string AsmStringIR;
4660 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004661 StringRef ASMString =
4662 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4663 const char *AsmStart = ASMString.begin();
4664 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004665 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004666 for (const AsmRewrite &AR : AsmStrRewrites) {
4667 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004668 if (Kind == AOK_Delete)
4669 continue;
4670
David Majnemer8114c1a2014-06-23 02:17:16 +00004671 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004672 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004673
Chad Rosier120eefd2013-03-19 17:32:17 +00004674 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004675 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004676 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004677
Chad Rosier37e755c2012-10-23 17:43:43 +00004678 // Skip the original expression.
4679 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004680 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004681 continue;
4682 }
4683
Chad Rosierff10ed12013-04-12 16:26:42 +00004684 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004685 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004686 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004687 default:
4688 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004689 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004690 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004691 break;
4692 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004693 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004694 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004695 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004696 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004697 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004698 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004699 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004700 break;
4701 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004702 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004703 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004704 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004705 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004706 default: break;
4707 case 8: OS << "byte ptr "; break;
4708 case 16: OS << "word ptr "; break;
4709 case 32: OS << "dword ptr "; break;
4710 case 64: OS << "qword ptr "; break;
4711 case 80: OS << "xword ptr "; break;
4712 case 128: OS << "xmmword ptr "; break;
4713 case 256: OS << "ymmword ptr "; break;
4714 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004715 break;
4716 case AOK_Emit:
4717 OS << ".byte";
4718 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004719 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004720 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004721 OS << ".align " << Val;
4722
4723 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004724 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004725 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4726 break;
4727 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004728 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004729 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004730 OS.flush();
4731 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004732 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004733 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004734 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004735 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004736
Chad Rosier8bce6642012-10-18 15:49:34 +00004737 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004738 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004739 }
4740
4741 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004742 if (AsmStart != AsmEnd)
4743 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004744
4745 AsmString = OS.str();
4746 return false;
4747}
4748
Daniel Dunbar01e36072010-07-17 02:26:10 +00004749/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004750MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4751 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004752 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004753}