blob: 0d5a4d7d0caecba340438331e43778b2d3731545 [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
Toma Tabacu217116e2015-04-27 10:50:29 +0000150 /// \brief Keeps track of how many .macro's have been instantiated.
151 unsigned NumOfMacroInstantiations;
152
Daniel Dunbar43325c42010-09-09 22:42:56 +0000153 /// Flag tracking whether any errors have been encountered.
154 unsigned HadError : 1;
155
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000156 /// The values from the last parsed cpp hash file line comment if any.
157 StringRef CppHashFilename;
158 int64_t CppHashLineNumber;
159 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000160 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// When generating dwarf for assembly source files we need to calculate the
162 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000163 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
165 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000166 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000168
Devang Patela173ee52012-01-31 18:14:05 +0000169 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
170 unsigned AssemblerDialect;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000173 bool IsDarwin;
174
Jim Grosbach4b905842013-09-20 23:08:21 +0000175 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000176 bool ParsingInlineAsm;
177
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000178public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000179 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000181 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182
Craig Topper59be68f2014-03-08 07:14:16 +0000183 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 void addDirectiveHandler(StringRef Directive,
186 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000187 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188 }
189
Toma Tabacu11e14a92015-04-21 11:50:52 +0000190 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
191 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
192 }
193
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194public:
195 /// @name MCAsmParser Interface
196 /// {
197
Craig Topper59be68f2014-03-08 07:14:16 +0000198 SourceMgr &getSourceManager() override { return SrcMgr; }
199 MCAsmLexer &getLexer() override { return Lexer; }
200 MCContext &getContext() override { return Ctx; }
201 MCStreamer &getStreamer() override { return Out; }
202 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000203 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000204 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000205 else
206 return AssemblerDialect;
207 }
Craig Topper59be68f2014-03-08 07:14:16 +0000208 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000209 AssemblerDialect = i;
210 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 void Note(SMLoc L, const Twine &Msg,
213 ArrayRef<SMRange> Ranges = None) override;
214 bool Warning(SMLoc L, const Twine &Msg,
215 ArrayRef<SMRange> Ranges = None) override;
216 bool Error(SMLoc L, const Twine &Msg,
217 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper59be68f2014-03-08 07:14:16 +0000219 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper59be68f2014-03-08 07:14:16 +0000221 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
222 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000223
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000224 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000226 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000229 const MCInstrInfo *MII, const MCInstPrinter *IP,
230 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000231
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
234 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237
Jim Grosbach4b905842013-09-20 23:08:21 +0000238 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000239 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseIdentifier(StringRef &Res) override;
241 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000242
Craig Topper59be68f2014-03-08 07:14:16 +0000243 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244 /// }
245
246private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000247
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000248 bool parseStatement(ParseStatementInfo &Info,
249 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000257 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000272 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000313 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000329 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
330 MCBinaryExpr::Opcode &Kind);
331
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
334 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000337
Eli Bendersky17233942013-01-15 22:59:42 +0000338 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000339 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_NO_DIRECTIVE, // Placeholder
341 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000342 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
343 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000344 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000346 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
348 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
349 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
350 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000351 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000352 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
353 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000354 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
355 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
356 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
357 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
358 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
359 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000360 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000361 DK_MACROS_ON, DK_MACROS_OFF,
362 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000363 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000364 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366 };
367
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000369 /// directives parsed by this class.
370 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371
372 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
374 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000375 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
377 bool parseDirectiveFill(); // ".fill"
378 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000379 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
381 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000382 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384
Eli Bendersky17233942013-01-15 22:59:42 +0000385 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveFile(SMLoc DirectiveLoc);
387 bool parseDirectiveLine();
388 bool parseDirectiveLoc();
389 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000390
391 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000393 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFISections();
395 bool parseDirectiveCFIStartProc();
396 bool parseDirectiveCFIEndProc();
397 bool parseDirectiveCFIDefCfaOffset();
398 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIAdjustCfaOffset();
400 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
404 bool parseDirectiveCFIRememberState();
405 bool parseDirectiveCFIRestoreState();
406 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIEscape();
409 bool parseDirectiveCFISignalFrame();
410 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
412 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000414 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveEndMacro(StringRef Directive);
416 bool parseDirectiveMacro(SMLoc DirectiveLoc);
417 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000421 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000423 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000425
Eli Bendersky17233942013-01-15 22:59:42 +0000426 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000428
429 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000433 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000437
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveAbort(); // ".abort"
439 bool parseDirectiveInclude(); // ".include"
440 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000441
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000442 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
443 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000444 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000446 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000448 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
449 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000450 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
452 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
453 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
454 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000455 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000456
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000458 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000459
Rafael Espindola34b9c512012-06-03 23:57:14 +0000460 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
462 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000463 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000464 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
466 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
467 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000468
Chad Rosierc7f552c2013-02-12 21:33:51 +0000469 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000471 size_t Len);
472
473 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000474 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000475
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000476 // "end"
477 bool parseDirectiveEnd(SMLoc DirectiveLoc);
478
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000479 // ".err" or ".error"
480 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000481
Nico Weber404012b2014-07-24 16:26:06 +0000482 // ".warning"
483 bool parseDirectiveWarning(SMLoc DirectiveLoc);
484
Eli Bendersky17233942013-01-15 22:59:42 +0000485 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000486};
Daniel Dunbar86033402010-07-12 17:54:38 +0000487}
488
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000489namespace llvm {
490
491extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000492extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000493extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000494
495}
496
Chris Lattnerc35681b2010-01-19 19:46:13 +0000497enum { DEFAULT_ADDRSPACE = 0 };
498
David Blaikie9f380a32015-03-16 18:06:57 +0000499AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
500 const MCAsmInfo &MAI)
501 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
502 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000503 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000504 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000505 // Save the old handler.
506 SavedDiagHandler = SrcMgr.getDiagHandler();
507 SavedDiagContext = SrcMgr.getDiagContext();
508 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000509 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000510 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000511
Daniel Dunbarc5011082010-07-12 18:12:02 +0000512 // Initialize the platform / file format parser.
David Blaikie9f380a32015-03-16 18:06:57 +0000513 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +0000514 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000515 PlatformParser.reset(createCOFFAsmParser());
516 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000517 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000518 PlatformParser.reset(createDarwinAsmParser());
519 IsDarwin = true;
520 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000521 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000522 PlatformParser.reset(createELFAsmParser());
523 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000524 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000525
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000526 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000527 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000528
529 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000530}
531
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000532AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000533 assert((HadError || ActiveMacros.empty()) &&
534 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000553 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000569 unsigned NewBuf =
570 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
571 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000573
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 return false;
577}
Daniel Dunbar43235712010-07-18 18:54:11 +0000578
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000579/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000580/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000582bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000584 unsigned NewBuf =
585 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Alp Tokera55b95b2014-07-06 10:33:31 +0000594void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
595 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
597 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000598}
599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600const AsmToken &AsmParser::Lex() {
601 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603 if (tok->is(AsmToken::Eof)) {
604 // If this is the end of an included file, pop the parent file off the
605 // include stack.
606 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
607 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000608 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 tok = &Lexer.Lex();
610 }
611 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000614 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617}
618
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000619bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000620 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000621 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000622 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000623
Chris Lattner36e02122009-06-21 20:54:55 +0000624 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000625 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000626
627 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000628 AsmCond StartingCondState = TheCondState;
629
Kevin Enderby6469fc22011-11-01 22:27:22 +0000630 // If we are generating dwarf for assembly source files save the initial text
631 // section and generate a .file directive.
632 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000633 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000634 if (!Sec->getBeginSymbol()) {
635 MCSymbol *SectionStartSym = getContext().createTempSymbol();
636 getStreamer().EmitLabel(SectionStartSym);
637 Sec->setBeginSymbol(SectionStartSym);
638 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000639 bool InsertResult = getContext().addGenDwarfSection(Sec);
640 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000641 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000642 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
643 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000644 }
645
Chris Lattner73f36112009-07-02 21:53:43 +0000646 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000647 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000648 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000649 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000650 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000651
Daniel Dunbar43325c42010-09-09 22:42:56 +0000652 // We had an error, validate that one was emitted and recover by skipping to
653 // the next line.
654 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000655 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000656 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000657
658 if (TheCondState.TheCond != StartingCondState.TheCond ||
659 TheCondState.Ignore != StartingCondState.Ignore)
660 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661
662 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000663 const auto &LineTables = getContext().getMCDwarfLineTables();
664 if (!LineTables.empty()) {
665 unsigned Index = 0;
666 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
667 if (File.Name.empty() && Index != 0)
668 TokError("unassigned file number: " + Twine(Index) +
669 " for .file directives");
670 ++Index;
671 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000672 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000673
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000674 // Check to see that all assembler local symbols were actually defined.
675 // Targets that don't do subsections via symbols may not want this, though,
676 // so conservatively exclude them. Only do this if we're finalizing, though,
677 // as otherwise we won't necessarilly have seen everything yet.
678 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
679 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
680 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000681 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000682 i != e; ++i) {
683 MCSymbol *Sym = i->getValue();
684 // Variable symbols may not be marked as defined, so check those
685 // explicitly. If we know it's a variable, we have a definition for
686 // the purposes of this check.
687 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
688 // FIXME: We would really like to refer back to where the symbol was
689 // first referenced for a source location. We need to add something
690 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000691 printMessage(
692 getLexer().getLoc(), SourceMgr::DK_Error,
693 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000694 }
695 }
696
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000697 // Finalize the output stream if there are no errors and if the client wants
698 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000699 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000700 Out.Finish();
701
Chris Lattner73f36112009-07-02 21:53:43 +0000702 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000703}
704
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000705void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000706 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000708 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000709 }
710}
711
Jim Grosbach4b905842013-09-20 23:08:21 +0000712/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000715 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000716
Chris Lattnere5074c42009-06-22 01:29:09 +0000717 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000718 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000719 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000720}
721
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000722StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 const char *Start = getTok().getLoc().getPointer();
724
Jim Grosbach4b905842013-09-20 23:08:21 +0000725 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000726 Lex();
727
728 const char *End = getTok().getLoc().getPointer();
729 return StringRef(Start, End - Start);
730}
Chris Lattner78db3622009-06-22 05:51:26 +0000731
Jim Grosbach4b905842013-09-20 23:08:21 +0000732StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000733 const char *Start = getTok().getLoc().getPointer();
734
735 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000736 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000737 Lex();
738
739 const char *End = getTok().getLoc().getPointer();
740 return StringRef(Start, End - Start);
741}
742
Jim Grosbach4b905842013-09-20 23:08:21 +0000743/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000744/// NOTE: This assumes the leading '(' has already been consumed.
745///
746/// parenexpr ::= expr)
747///
Jim Grosbach4b905842013-09-20 23:08:21 +0000748bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
749 if (parseExpression(Res))
750 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000751 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000753 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000754 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000755 return false;
756}
Chris Lattner78db3622009-06-22 05:51:26 +0000757
Jim Grosbach4b905842013-09-20 23:08:21 +0000758/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000759/// NOTE: This assumes the leading '[' has already been consumed.
760///
761/// bracketexpr ::= expr]
762///
Jim Grosbach4b905842013-09-20 23:08:21 +0000763bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
764 if (parseExpression(Res))
765 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 if (Lexer.isNot(AsmToken::RBrac))
767 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000768 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000769 Lex();
770 return false;
771}
772
Jim Grosbach4b905842013-09-20 23:08:21 +0000773/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000774/// primaryexpr ::= (parenexpr
775/// primaryexpr ::= symbol
776/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000777/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000778/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000779bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000780 SMLoc FirstTokenLoc = getLexer().getLoc();
781 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
782 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000783 default:
784 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000785 // If we have an error assume that we've already handled it.
786 case AsmToken::Error:
787 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000788 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000789 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000790 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000792 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000793 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000794 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000795 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000796 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000797 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000798 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000799 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000800 if (FirstTokenKind == AsmToken::Dollar) {
801 if (Lexer.getMAI().getDollarIsPC()) {
802 // This is a '$' reference, which references the current PC. Emit a
803 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000804 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000806 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
807 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000808 EndLoc = FirstTokenLoc;
809 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000810 }
811 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000812 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000813 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000814 // Parse symbol variant
815 std::pair<StringRef, StringRef> Split;
816 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000817 if (FirstTokenKind == AsmToken::String) {
818 if (Lexer.is(AsmToken::At)) {
819 Lexer.Lex(); // eat @
820 SMLoc AtLoc = getLexer().getLoc();
821 StringRef VName;
822 if (parseIdentifier(VName))
823 return Error(AtLoc, "expected symbol variant after '@'");
824
825 Split = std::make_pair(Identifier, VName);
826 }
827 } else {
828 Split = Identifier.split('@');
829 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000830 } else if (Lexer.is(AsmToken::LParen)) {
831 Lexer.Lex(); // eat (
832 StringRef VName;
833 parseIdentifier(VName);
834 if (Lexer.isNot(AsmToken::RParen)) {
835 return Error(Lexer.getTok().getLoc(),
836 "unexpected token in variant, expected ')'");
837 }
838 Lexer.Lex(); // eat )
839 Split = std::make_pair(Identifier, VName);
840 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000841
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000842 EndLoc = SMLoc::getFromPointer(Identifier.end());
843
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 StringRef SymbolName = Identifier;
846 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000847
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000848 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000849 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000850 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 if (Variant != MCSymbolRefExpr::VK_Invalid) {
852 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000853 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000854 Variant = MCSymbolRefExpr::VK_None;
855 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000856 return Error(SMLoc::getFromPointer(Split.second.begin()),
857 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000858 }
859 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000860
Jim Grosbach6f482002015-05-18 18:43:14 +0000861 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000862
Daniel Dunbard20cda02009-10-16 01:34:54 +0000863 // If this is an absolute variable reference, substitute it now to preserve
864 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000865 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000866 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000867 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000868
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000869 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000870 return false;
871 }
872
873 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000874 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000875 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000876 }
David Woodhousef42a6662014-02-01 16:20:54 +0000877 case AsmToken::BigNum:
878 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000879 case AsmToken::Integer: {
880 SMLoc Loc = getTok().getLoc();
881 int64_t IntVal = getTok().getIntVal();
882 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000883 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000884 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 // Look for 'b' or 'f' following an Integer as a directional label
886 if (Lexer.getKind() == AsmToken::Identifier) {
887 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 // Lookup the symbol variant if used.
889 std::pair<StringRef, StringRef> Split = IDVal.split('@');
890 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
891 if (Split.first.size() != IDVal.size()) {
892 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000893 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000895 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000897 if (IDVal == "f" || IDVal == "b") {
898 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000899 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000900 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000901 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 Lex(); // Eat identifier.
905 }
906 }
Chris Lattner78db3622009-06-22 05:51:26 +0000907 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000908 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 case AsmToken::Real: {
910 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000911 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000912 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000913 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000914 Lex(); // Eat token.
915 return false;
916 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000917 case AsmToken::Dot: {
918 // This is a '.' reference, which references the current PC. Emit a
919 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000920 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000921 Out.EmitLabel(Sym);
922 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000923 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000924 Lex(); // Eat identifier.
925 return false;
926 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000930 case AsmToken::LBrac:
931 if (!PlatformParser->HasBracketExpressions())
932 return TokError("brackets expression not supported on this target");
933 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000945 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000947 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000948 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000949 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000950 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000951 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000953 }
954}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000955
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000956bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000957 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000958 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000959}
960
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000962AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000964 // Ask the target implementation about this expression first.
965 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
966 if (NewE)
967 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968 // Recurse over the given expression, rebuilding it to apply the given variant
969 // if there is exactly one symbol.
970 switch (E->getKind()) {
971 case MCExpr::Target:
972 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000973 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000974
975 case MCExpr::SymbolRef: {
976 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
977
978 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 TokError("invalid variant on expression '" + getTok().getIdentifier() +
980 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 return E;
982 }
983
984 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
985 }
986
987 case MCExpr::Unary: {
988 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000991 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
993 }
994
995 case MCExpr::Binary: {
996 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
998 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
1000 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001001 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002
Jim Grosbach4b905842013-09-20 23:08:21 +00001003 if (!LHS)
1004 LHS = BE->getLHS();
1005 if (!RHS)
1006 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007
1008 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1009 }
1010 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001011
Craig Toppera2886c22012-02-07 05:05:23 +00001012 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013}
1014
Jim Grosbach4b905842013-09-20 23:08:21 +00001015/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001016///
Jim Grosbachbd164242011-08-20 16:24:13 +00001017/// expr ::= expr &&,|| expr -> lowest.
1018/// expr ::= expr |,^,&,! expr
1019/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1020/// expr ::= expr <<,>> expr
1021/// expr ::= expr +,- expr
1022/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001023/// expr ::= primaryexpr
1024///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001025bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001026 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001027 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001029 return true;
1030
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 // As a special case, we support 'a op b @ modifier' by rewriting the
1032 // expression to include the modifier. This is inefficient, but in general we
1033 // expect users to use 'a@modifier op b'.
1034 if (Lexer.getKind() == AsmToken::At) {
1035 Lex();
1036
1037 if (Lexer.isNot(AsmToken::Identifier))
1038 return TokError("unexpected symbol modifier following '@'");
1039
1040 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042 if (Variant == MCSymbolRefExpr::VK_Invalid)
1043 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1044
Jim Grosbach4b905842013-09-20 23:08:21 +00001045 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 if (!ModifiedRes) {
1047 return TokError("invalid modifier '" + getTok().getIdentifier() +
1048 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001050
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 Res = ModifiedRes;
1052 Lex();
1053 }
1054
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001055 // Try to constant fold it up front, if possible.
1056 int64_t Value;
1057 if (Res->EvaluateAsAbsolute(Value))
1058 Res = MCConstantExpr::Create(Value, getContext());
1059
1060 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001061}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001064 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001065 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001066}
1067
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001068bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001069 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070
Daniel Dunbar75630b32009-06-30 02:10:03 +00001071 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001072 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073 return true;
1074
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001075 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001076 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001077
1078 return false;
1079}
1080
Ahmed Bougacha457852f2015-04-28 00:17:39 +00001081unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1082 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001083 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001084 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001086
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001090 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001091 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001093 return 1;
1094
Jim Grosbach4b905842013-09-20 23:08:21 +00001095 // Low Precedence: |, &, ^
1096 //
1097 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001104 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001105 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001106 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001107
Jim Grosbach4b905842013-09-20 23:08:21 +00001108 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001109 case AsmToken::EqualEqual:
1110 Kind = MCBinaryExpr::EQ;
1111 return 3;
1112 case AsmToken::ExclaimEqual:
1113 case AsmToken::LessGreater:
1114 Kind = MCBinaryExpr::NE;
1115 return 3;
1116 case AsmToken::Less:
1117 Kind = MCBinaryExpr::LT;
1118 return 3;
1119 case AsmToken::LessEqual:
1120 Kind = MCBinaryExpr::LTE;
1121 return 3;
1122 case AsmToken::Greater:
1123 Kind = MCBinaryExpr::GT;
1124 return 3;
1125 case AsmToken::GreaterEqual:
1126 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001127 return 3;
1128
Jim Grosbach4b905842013-09-20 23:08:21 +00001129 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 case AsmToken::LessLess:
1131 Kind = MCBinaryExpr::Shl;
1132 return 4;
1133 case AsmToken::GreaterGreater:
Ahmed Bougacha177c1482015-04-28 00:21:32 +00001134 Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001135 return 4;
1136
Jim Grosbach4b905842013-09-20 23:08:21 +00001137 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138 case AsmToken::Plus:
1139 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001141 case AsmToken::Minus:
1142 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001144
Jim Grosbach4b905842013-09-20 23:08:21 +00001145 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001149 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001151 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001152 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001153 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001154 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 }
1156}
1157
Jim Grosbach4b905842013-09-20 23:08:21 +00001158/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001160bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001161 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001163 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001164 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166 // If the next token is lower precedence than we are allowed to eat, return
1167 // successfully with what we ate already.
1168 if (TokPrec < Precedence)
1169 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001170
Sean Callanan686ed8d2010-01-19 20:22:31 +00001171 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001174 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001175 if (parsePrimaryExpr(RHS, EndLoc))
1176 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001177
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178 // If BinOp binds less tightly with RHS than the operator after RHS, let
1179 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001181 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001182 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1183 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001184
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001185 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001186 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001187 }
1188}
1189
Chris Lattner36e02122009-06-21 20:54:55 +00001190/// ParseStatement:
1191/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001192/// ::= Label* Directive ...Operands... EndOfStatement
1193/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001194bool AsmParser::parseStatement(ParseStatementInfo &Info,
1195 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001196 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001197 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001198 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001199 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001200 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001201
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001202 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001203 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001204 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001205 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001207 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001208 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001209 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001210
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001211 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001212 if (Lexer.is(AsmToken::Integer)) {
1213 LocalLabelVal = getTok().getIntVal();
1214 if (LocalLabelVal < 0) {
1215 if (!TheCondState.Ignore)
1216 return TokError("unexpected token at start of statement");
1217 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001218 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001219 IDVal = getTok().getString();
1220 Lex(); // Consume the integer token to be used as an identifier token.
1221 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001222 if (!TheCondState.Ignore)
1223 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001224 }
1225 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001226 } else if (Lexer.is(AsmToken::Dot)) {
1227 // Treat '.' as a valid identifier in this context.
1228 Lex();
1229 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001230 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001231 if (!TheCondState.Ignore)
1232 return TokError("unexpected token at start of statement");
1233 IDVal = "";
1234 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001235
Chris Lattner926885c2010-04-17 18:14:27 +00001236 // Handle conditional assembly here before checking for skipping. We
1237 // have to do this so that .endif isn't skipped in a ".if 0" block for
1238 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001239 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 DirectiveKindMap.find(IDVal);
1241 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1242 ? DK_NO_DIRECTIVE
1243 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001244 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001245 default:
1246 break;
1247 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001248 case DK_IFEQ:
1249 case DK_IFGE:
1250 case DK_IFGT:
1251 case DK_IFLE:
1252 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001253 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001254 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001255 case DK_IFB:
1256 return parseDirectiveIfb(IDLoc, true);
1257 case DK_IFNB:
1258 return parseDirectiveIfb(IDLoc, false);
1259 case DK_IFC:
1260 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001261 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001262 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001263 case DK_IFNC:
1264 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001265 case DK_IFNES:
1266 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001267 case DK_IFDEF:
1268 return parseDirectiveIfdef(IDLoc, true);
1269 case DK_IFNDEF:
1270 case DK_IFNOTDEF:
1271 return parseDirectiveIfdef(IDLoc, false);
1272 case DK_ELSEIF:
1273 return parseDirectiveElseIf(IDLoc);
1274 case DK_ELSE:
1275 return parseDirectiveElse(IDLoc);
1276 case DK_ENDIF:
1277 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001278 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001279
Eli Bendersky88024712013-01-16 19:32:36 +00001280 // Ignore the statement if in the middle of inactive conditional
1281 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001282 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001283 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001284 return false;
1285 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001286
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001287 // FIXME: Recurse on local labels?
1288
1289 // See what kind of statement we have.
1290 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001291 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001292 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001293
Chris Lattner36e02122009-06-21 20:54:55 +00001294 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001295 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001296
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001297 // Diagnose attempt to use '.' as a label.
1298 if (IDVal == ".")
1299 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1300
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001301 // Diagnose attempt to use a variable as a label.
1302 //
1303 // FIXME: Diagnostics. Note the location of the definition as a label.
1304 // FIXME: This doesn't diagnose assignment to a symbol which has been
1305 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001306 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001307 if (LocalLabelVal == -1) {
1308 if (ParsingInlineAsm && SI) {
1309 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1310 assert(RewrittenLabel.size() && "We should have an internal name here.");
1311 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1312 IDVal.size(), RewrittenLabel));
1313 IDVal = RewrittenLabel;
1314 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001315 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001316 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001317 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001318
1319 Sym->redefineIfPossible();
1320
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001321 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001322 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001323
Daniel Dunbare73b2672009-08-26 22:13:22 +00001324 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001325 if (!ParsingInlineAsm)
1326 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001327
Kevin Enderbye7739d42011-12-09 18:09:40 +00001328 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001329 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001330 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001331 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1332 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001333
Tim Northover1744d0a2013-10-25 12:49:50 +00001334 getTargetParser().onLabelParsed(Sym);
1335
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001336 // Consume any end of statement token, if present, to avoid spurious
1337 // AddBlankLine calls().
1338 if (Lexer.is(AsmToken::EndOfStatement)) {
1339 Lex();
1340 if (Lexer.is(AsmToken::Eof))
1341 return false;
1342 }
1343
Eli Friedman0f4871d2012-10-22 23:58:19 +00001344 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001345 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001346
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001347 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001348 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001349 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001350
Jim Grosbach4b905842013-09-20 23:08:21 +00001351 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001352
1353 default: // Normal instruction or directive.
1354 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001355 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001356
1357 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001358 if (areMacrosEnabled())
1359 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1360 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001361 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001362
Michael J. Spencer530ce852010-10-09 11:00:50 +00001363 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001364
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001366 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001368 //
Eli Bendersky17233942013-01-15 22:59:42 +00001369 // 1. The target-specific assembly parser. Some directives are target
1370 // specific or may potentially behave differently on certain targets.
1371 // 2. Asm parser extensions. For example, platform-specific parsers
1372 // (like the ELF parser) register themselves as extensions.
1373 // 3. The generic directive parser implemented by this class. These are
1374 // all the directives that behave in a target and platform independent
1375 // manner, or at least have a default behavior that's shared between
1376 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001377
Eli Bendersky17233942013-01-15 22:59:42 +00001378 // First query the target-specific parser. It will return 'true' if it
1379 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001380 if (!getTargetParser().ParseDirective(ID))
1381 return false;
1382
Alp Tokercb402912014-01-24 17:20:08 +00001383 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001384 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001385 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1386 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001387 if (Handler.first)
1388 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1389
1390 // Finally, if no one else is interested in this directive, it must be
1391 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001392 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001393 default:
1394 break;
1395 case DK_SET:
1396 case DK_EQU:
1397 return parseDirectiveSet(IDVal, true);
1398 case DK_EQUIV:
1399 return parseDirectiveSet(IDVal, false);
1400 case DK_ASCII:
1401 return parseDirectiveAscii(IDVal, false);
1402 case DK_ASCIZ:
1403 case DK_STRING:
1404 return parseDirectiveAscii(IDVal, true);
1405 case DK_BYTE:
1406 return parseDirectiveValue(1);
1407 case DK_SHORT:
1408 case DK_VALUE:
1409 case DK_2BYTE:
1410 return parseDirectiveValue(2);
1411 case DK_LONG:
1412 case DK_INT:
1413 case DK_4BYTE:
1414 return parseDirectiveValue(4);
1415 case DK_QUAD:
1416 case DK_8BYTE:
1417 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001418 case DK_OCTA:
1419 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001420 case DK_SINGLE:
1421 case DK_FLOAT:
1422 return parseDirectiveRealValue(APFloat::IEEEsingle);
1423 case DK_DOUBLE:
1424 return parseDirectiveRealValue(APFloat::IEEEdouble);
1425 case DK_ALIGN: {
1426 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1427 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1428 }
1429 case DK_ALIGN32: {
1430 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1431 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1432 }
1433 case DK_BALIGN:
1434 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1435 case DK_BALIGNW:
1436 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1437 case DK_BALIGNL:
1438 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1439 case DK_P2ALIGN:
1440 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1441 case DK_P2ALIGNW:
1442 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1443 case DK_P2ALIGNL:
1444 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1445 case DK_ORG:
1446 return parseDirectiveOrg();
1447 case DK_FILL:
1448 return parseDirectiveFill();
1449 case DK_ZERO:
1450 return parseDirectiveZero();
1451 case DK_EXTERN:
1452 eatToEndOfStatement(); // .extern is the default, ignore it.
1453 return false;
1454 case DK_GLOBL:
1455 case DK_GLOBAL:
1456 return parseDirectiveSymbolAttribute(MCSA_Global);
1457 case DK_LAZY_REFERENCE:
1458 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1459 case DK_NO_DEAD_STRIP:
1460 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1461 case DK_SYMBOL_RESOLVER:
1462 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1463 case DK_PRIVATE_EXTERN:
1464 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1465 case DK_REFERENCE:
1466 return parseDirectiveSymbolAttribute(MCSA_Reference);
1467 case DK_WEAK_DEFINITION:
1468 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1469 case DK_WEAK_REFERENCE:
1470 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1471 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1472 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1473 case DK_COMM:
1474 case DK_COMMON:
1475 return parseDirectiveComm(/*IsLocal=*/false);
1476 case DK_LCOMM:
1477 return parseDirectiveComm(/*IsLocal=*/true);
1478 case DK_ABORT:
1479 return parseDirectiveAbort();
1480 case DK_INCLUDE:
1481 return parseDirectiveInclude();
1482 case DK_INCBIN:
1483 return parseDirectiveIncbin();
1484 case DK_CODE16:
1485 case DK_CODE16GCC:
1486 return TokError(Twine(IDVal) + " not supported yet");
1487 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001488 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001489 case DK_IRP:
1490 return parseDirectiveIrp(IDLoc);
1491 case DK_IRPC:
1492 return parseDirectiveIrpc(IDLoc);
1493 case DK_ENDR:
1494 return parseDirectiveEndr(IDLoc);
1495 case DK_BUNDLE_ALIGN_MODE:
1496 return parseDirectiveBundleAlignMode();
1497 case DK_BUNDLE_LOCK:
1498 return parseDirectiveBundleLock();
1499 case DK_BUNDLE_UNLOCK:
1500 return parseDirectiveBundleUnlock();
1501 case DK_SLEB128:
1502 return parseDirectiveLEB128(true);
1503 case DK_ULEB128:
1504 return parseDirectiveLEB128(false);
1505 case DK_SPACE:
1506 case DK_SKIP:
1507 return parseDirectiveSpace(IDVal);
1508 case DK_FILE:
1509 return parseDirectiveFile(IDLoc);
1510 case DK_LINE:
1511 return parseDirectiveLine();
1512 case DK_LOC:
1513 return parseDirectiveLoc();
1514 case DK_STABS:
1515 return parseDirectiveStabs();
1516 case DK_CFI_SECTIONS:
1517 return parseDirectiveCFISections();
1518 case DK_CFI_STARTPROC:
1519 return parseDirectiveCFIStartProc();
1520 case DK_CFI_ENDPROC:
1521 return parseDirectiveCFIEndProc();
1522 case DK_CFI_DEF_CFA:
1523 return parseDirectiveCFIDefCfa(IDLoc);
1524 case DK_CFI_DEF_CFA_OFFSET:
1525 return parseDirectiveCFIDefCfaOffset();
1526 case DK_CFI_ADJUST_CFA_OFFSET:
1527 return parseDirectiveCFIAdjustCfaOffset();
1528 case DK_CFI_DEF_CFA_REGISTER:
1529 return parseDirectiveCFIDefCfaRegister(IDLoc);
1530 case DK_CFI_OFFSET:
1531 return parseDirectiveCFIOffset(IDLoc);
1532 case DK_CFI_REL_OFFSET:
1533 return parseDirectiveCFIRelOffset(IDLoc);
1534 case DK_CFI_PERSONALITY:
1535 return parseDirectiveCFIPersonalityOrLsda(true);
1536 case DK_CFI_LSDA:
1537 return parseDirectiveCFIPersonalityOrLsda(false);
1538 case DK_CFI_REMEMBER_STATE:
1539 return parseDirectiveCFIRememberState();
1540 case DK_CFI_RESTORE_STATE:
1541 return parseDirectiveCFIRestoreState();
1542 case DK_CFI_SAME_VALUE:
1543 return parseDirectiveCFISameValue(IDLoc);
1544 case DK_CFI_RESTORE:
1545 return parseDirectiveCFIRestore(IDLoc);
1546 case DK_CFI_ESCAPE:
1547 return parseDirectiveCFIEscape();
1548 case DK_CFI_SIGNAL_FRAME:
1549 return parseDirectiveCFISignalFrame();
1550 case DK_CFI_UNDEFINED:
1551 return parseDirectiveCFIUndefined(IDLoc);
1552 case DK_CFI_REGISTER:
1553 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001554 case DK_CFI_WINDOW_SAVE:
1555 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001556 case DK_MACROS_ON:
1557 case DK_MACROS_OFF:
1558 return parseDirectiveMacrosOnOff(IDVal);
1559 case DK_MACRO:
1560 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001561 case DK_EXITM:
1562 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001563 case DK_ENDM:
1564 case DK_ENDMACRO:
1565 return parseDirectiveEndMacro(IDVal);
1566 case DK_PURGEM:
1567 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001568 case DK_END:
1569 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001570 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001571 return parseDirectiveError(IDLoc, false);
1572 case DK_ERROR:
1573 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001574 case DK_WARNING:
1575 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001576 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001577
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001578 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001579 }
Chris Lattner36e02122009-06-21 20:54:55 +00001580
Chad Rosierc7f552c2013-02-12 21:33:51 +00001581 // __asm _emit or __asm __emit
1582 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1583 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001585
1586 // __asm align
1587 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001588 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001589
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001590 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001591
Chris Lattner7cbfa442010-05-19 23:34:33 +00001592 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001593 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001594 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001595 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001596 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001597 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001598
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001599 // Dump the parsed representation, if requested.
1600 if (getShowParsedOperands()) {
1601 SmallString<256> Str;
1602 raw_svector_ostream OS(Str);
1603 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001604 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001605 if (i != 0)
1606 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001607 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001608 }
1609 OS << "]";
1610
Jim Grosbach4b905842013-09-20 23:08:21 +00001611 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001612 }
1613
Oliver Stannard8b273082014-06-19 15:52:37 +00001614 // If we are generating dwarf for the current section then generate a .loc
1615 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001616 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001617 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001618 getStreamer().getCurrentSection().first)) {
1619 unsigned Line;
1620 if (ActiveMacros.empty())
1621 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1622 else
1623 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1624 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001625
Eli Bendersky88024712013-01-16 19:32:36 +00001626 // If we previously parsed a cpp hash file line comment then make sure the
1627 // current Dwarf File is for the CppHashFilename if not then emit the
1628 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001629 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001630 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1631 0, StringRef(), CppHashFilename);
1632 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001633
Jim Grosbach4b905842013-09-20 23:08:21 +00001634 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1635 // cache with the different Loc from the call above we save the last
1636 // info we queried here with SrcMgr.FindLineNumber().
1637 unsigned CppHashLocLineNo;
1638 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1639 CppHashLocLineNo = LastQueryLine;
1640 else {
1641 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1642 LastQueryLine = CppHashLocLineNo;
1643 LastQueryIDLoc = CppHashLoc;
1644 LastQueryBuffer = CppHashBuf;
1645 }
1646 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001647 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001648
Jim Grosbach4b905842013-09-20 23:08:21 +00001649 getStreamer().EmitDwarfLocDirective(
1650 getContext().getGenDwarfFileNumber(), Line, 0,
1651 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1652 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001653 }
1654
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001655 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001656 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001657 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001658 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1659 Info.ParsedOperands, Out,
1660 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001661 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001662
Chris Lattnera2a9d162010-09-11 16:18:25 +00001663 // Don't skip the rest of the line, the instruction parser is responsible for
1664 // that.
1665 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001666}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001667
Jim Grosbach4b905842013-09-20 23:08:21 +00001668/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001669/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001670void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001671 if (!Lexer.is(AsmToken::EndOfStatement))
1672 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001673 // Eat EOL.
1674 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001675}
1676
Jim Grosbach4b905842013-09-20 23:08:21 +00001677/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001678/// ::= # number "filename"
1679/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001680bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001681 Lex(); // Eat the hash token.
1682
1683 if (getLexer().isNot(AsmToken::Integer)) {
1684 // Consume the line since in cases it is not a well-formed line directive,
1685 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001686 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001687 return false;
1688 }
1689
1690 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001691 Lex();
1692
1693 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001695 return false;
1696 }
1697
1698 StringRef Filename = getTok().getString();
1699 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001700 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001701
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1703 CppHashLoc = L;
1704 CppHashFilename = Filename;
1705 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001706 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001707
1708 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001709 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001710 return false;
1711}
1712
Jim Grosbach4b905842013-09-20 23:08:21 +00001713/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714/// for the Filename and LineNo if any in the diagnostic.
1715void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001716 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001717 raw_ostream &OS = errs();
1718
1719 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1720 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001721 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1722 unsigned CppHashBuf =
1723 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724
Jim Grosbach4b905842013-09-20 23:08:21 +00001725 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001726 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001727 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1728 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1729 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001730 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1731 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 }
1733
Eric Christophera7c32732012-12-18 00:30:54 +00001734 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001735 // manager changed or buffer changed (like in a nested include) then just
1736 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001737 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001738 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001739 if (Parser->SavedDiagHandler)
1740 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1741 else
Craig Topper353eda42014-04-24 06:44:33 +00001742 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001743 return;
1744 }
1745
Eric Christophera7c32732012-12-18 00:30:54 +00001746 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001747 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1748 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001749 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001750
1751 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1752 int CppHashLocLineNo =
1753 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001754 int LineNo =
1755 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001756
Jim Grosbach4b905842013-09-20 23:08:21 +00001757 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1758 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001759 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001760
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001761 if (Parser->SavedDiagHandler)
1762 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1763 else
Craig Topper353eda42014-04-24 06:44:33 +00001764 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001765}
1766
Rafael Espindola2c064482012-08-21 18:29:30 +00001767// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1768// difference being that that function accepts '@' as part of identifiers and
1769// we can't do that. AsmLexer.cpp should probably be changed to handle
1770// '@' as a special case when needed.
1771static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001772 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1773 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001774}
1775
Rafael Espindola34b9c512012-06-03 23:57:14 +00001776bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001777 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001778 ArrayRef<MCAsmMacroArgument> A,
1779 bool EnableAtPseudoVariable, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001781 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001782 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001783 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001784
Preston Gurd05500642012-09-19 20:36:12 +00001785 // A macro without parameters is handled differently on Darwin:
1786 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001787 while (!Body.empty()) {
1788 // Scan for the next substitution.
1789 std::size_t End = Body.size(), Pos = 0;
1790 for (; Pos != End; ++Pos) {
1791 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001792 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001793 // This macro has no parameters, look for $0, $1, etc.
1794 if (Body[Pos] != '$' || Pos + 1 == End)
1795 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001796
Rafael Espindola1134ab232011-06-05 02:43:45 +00001797 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001798 if (Next == '$' || Next == 'n' ||
1799 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001800 break;
1801 } else {
1802 // This macro has parameters, look for \foo, \bar, etc.
1803 if (Body[Pos] == '\\' && Pos + 1 != End)
1804 break;
1805 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001806 }
1807
1808 // Add the prefix.
1809 OS << Body.slice(0, Pos);
1810
1811 // Check if we reached the end.
1812 if (Pos == End)
1813 break;
1814
Benjamin Kramer513e7442014-02-20 13:36:32 +00001815 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001816 switch (Body[Pos + 1]) {
1817 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001818 case '$':
1819 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001820 break;
1821
Jim Grosbach4b905842013-09-20 23:08:21 +00001822 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 case 'n':
1824 OS << A.size();
1825 break;
1826
Jim Grosbach4b905842013-09-20 23:08:21 +00001827 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828 default: {
1829 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001830 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 if (Index >= A.size())
1832 break;
1833
1834 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001835 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001836 ie = A[Index].end();
1837 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001838 OS << it->getString();
1839 break;
1840 }
1841 }
1842 Pos += 2;
1843 } else {
1844 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001845
1846 // Check for the \@ pseudo-variable.
1847 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001848 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001849 else
1850 while (isIdentifierChar(Body[I]) && I + 1 != End)
1851 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852
Jim Grosbach4b905842013-09-20 23:08:21 +00001853 const char *Begin = Body.data() + Pos + 1;
1854 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001855 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001856
Toma Tabacu217116e2015-04-27 10:50:29 +00001857 if (Argument == "@") {
1858 OS << NumOfMacroInstantiations;
1859 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001860 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001861 for (; Index < NParameters; ++Index)
1862 if (Parameters[Index].Name == Argument)
1863 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001864
Toma Tabacu217116e2015-04-27 10:50:29 +00001865 if (Index == NParameters) {
1866 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1867 Pos += 3;
1868 else {
1869 OS << '\\' << Argument;
1870 Pos = I;
1871 }
1872 } else {
1873 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1874 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1875 ie = A[Index].end();
1876 it != ie; ++it)
1877 // We expect no quotes around the string's contents when
1878 // parsing for varargs.
1879 if (it->getKind() != AsmToken::String || VarargParameter)
1880 OS << it->getString();
1881 else
1882 OS << it->getStringContents();
1883
1884 Pos += 1 + Argument.size();
1885 }
Preston Gurd05500642012-09-19 20:36:12 +00001886 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001887 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001888 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001889 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001890 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001891
Rafael Espindola1134ab232011-06-05 02:43:45 +00001892 return false;
1893}
Daniel Dunbar43235712010-07-18 18:54:11 +00001894
Nico Weber2a8f9222014-07-24 16:29:04 +00001895MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001896 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001897 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001898 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001899
Jim Grosbach4b905842013-09-20 23:08:21 +00001900static bool isOperator(AsmToken::TokenKind kind) {
1901 switch (kind) {
1902 default:
1903 return false;
1904 case AsmToken::Plus:
1905 case AsmToken::Minus:
1906 case AsmToken::Tilde:
1907 case AsmToken::Slash:
1908 case AsmToken::Star:
1909 case AsmToken::Dot:
1910 case AsmToken::Equal:
1911 case AsmToken::EqualEqual:
1912 case AsmToken::Pipe:
1913 case AsmToken::PipePipe:
1914 case AsmToken::Caret:
1915 case AsmToken::Amp:
1916 case AsmToken::AmpAmp:
1917 case AsmToken::Exclaim:
1918 case AsmToken::ExclaimEqual:
1919 case AsmToken::Percent:
1920 case AsmToken::Less:
1921 case AsmToken::LessEqual:
1922 case AsmToken::LessLess:
1923 case AsmToken::LessGreater:
1924 case AsmToken::Greater:
1925 case AsmToken::GreaterEqual:
1926 case AsmToken::GreaterGreater:
1927 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001928 }
1929}
1930
David Majnemer16252452014-01-29 00:07:39 +00001931namespace {
1932class AsmLexerSkipSpaceRAII {
1933public:
1934 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1935 Lexer.setSkipSpace(SkipSpace);
1936 }
1937
1938 ~AsmLexerSkipSpaceRAII() {
1939 Lexer.setSkipSpace(true);
1940 }
1941
1942private:
1943 AsmLexer &Lexer;
1944};
1945}
1946
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001947bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1948
1949 if (Vararg) {
1950 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1951 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001952 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001953 }
1954 return false;
1955 }
1956
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001958 unsigned AddTokens = 0;
1959
David Majnemer16252452014-01-29 00:07:39 +00001960 // Darwin doesn't use spaces to delmit arguments.
1961 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001962
1963 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001964 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001966
David Majnemer91fc4c22014-01-29 18:57:46 +00001967 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001968 break;
Preston Gurd05500642012-09-19 20:36:12 +00001969
1970 if (Lexer.is(AsmToken::Space)) {
1971 Lex(); // Eat spaces
1972
1973 // Spaces can delimit parameters, but could also be part an expression.
1974 // If the token after a space is an operator, add the token and the next
1975 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001976 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001977 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001978 // Check to see whether the token is used as an operator,
1979 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001980 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001981 if (*NextChar == ' ')
1982 AddTokens = 2;
1983 }
1984
1985 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001986 break;
1987 }
1988 }
1989 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990
Jim Grosbach4b905842013-09-20 23:08:21 +00001991 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001992 // to be able to fill in the remaining default parameter values
1993 if (Lexer.is(AsmToken::EndOfStatement))
1994 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001995
1996 // Adjust the current parentheses level.
1997 if (Lexer.is(AsmToken::LParen))
1998 ++ParenLevel;
1999 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2000 --ParenLevel;
2001
2002 // Append the token to the current argument list.
2003 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002004 if (AddTokens)
2005 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002006 Lex();
2007 }
Preston Gurd05500642012-09-19 20:36:12 +00002008
Rafael Espindola768b41c2012-06-15 14:02:34 +00002009 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002010 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002011 return false;
2012}
2013
2014// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002015bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002016 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002017 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002018 bool NamedParametersFound = false;
2019 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002020
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002021 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002022 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002023
Rafael Espindola768b41c2012-06-15 14:02:34 +00002024 // Parse two kinds of macro invocations:
2025 // - macros defined without any parameters accept an arbitrary number of them
2026 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002027 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002028 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2029 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002030 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002032
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002034 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002035 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036 eatToEndOfStatement();
2037 return true;
2038 }
2039
2040 if (!Lexer.is(AsmToken::Equal)) {
2041 TokError("expected '=' after formal parameter identifier");
2042 eatToEndOfStatement();
2043 return true;
2044 }
2045 Lex();
2046
2047 NamedParametersFound = true;
2048 }
2049
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002050 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002051 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002052 eatToEndOfStatement();
2053 return true;
2054 }
2055
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002056 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2057 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002058 return true;
2059
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002060 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002061 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002062 unsigned FAI = 0;
2063 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002064 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002065 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002066
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002067 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002068 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002069 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002070 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002071 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002072 return true;
2073 }
2074 PI = FAI;
2075 }
2076
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002077 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002078 if (A.size() <= PI)
2079 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002080 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002081
2082 if (FALocs.size() <= PI)
2083 FALocs.resize(PI + 1);
2084
2085 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002086 }
Jim Grosbach206661622012-07-30 22:44:17 +00002087
Preston Gurd242ed3152012-09-19 20:29:04 +00002088 // At the end of the statement, fill in remaining arguments that have
2089 // default values. If there aren't any, then the next argument is
2090 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002091 if (Lexer.is(AsmToken::EndOfStatement)) {
2092 bool Failure = false;
2093 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2094 if (A[FAI].empty()) {
2095 if (M->Parameters[FAI].Required) {
2096 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2097 "missing value for required parameter "
2098 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2099 Failure = true;
2100 }
2101
2102 if (!M->Parameters[FAI].Value.empty())
2103 A[FAI] = M->Parameters[FAI].Value;
2104 }
2105 }
2106 return Failure;
2107 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002108
2109 if (Lexer.is(AsmToken::Comma))
2110 Lex();
2111 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002112
2113 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002114}
2115
Jim Grosbach4b905842013-09-20 23:08:21 +00002116const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002117 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2118 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002119}
2120
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002121void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2122 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002123}
2124
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002125void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002126
Jim Grosbach4b905842013-09-20 23:08:21 +00002127bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2129 // this, although we should protect against infinite loops.
2130 if (ActiveMacros.size() == 20)
2131 return TokError("macros cannot be nested more than 20 levels deep");
2132
Eli Bendersky38274122013-01-14 23:22:36 +00002133 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002134 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002135 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002136
Rafael Espindola1134ab232011-06-05 02:43:45 +00002137 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2138 // to hold the macro body with substitutions.
2139 SmallString<256> Buf;
2140 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002141 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002142
Toma Tabacu217116e2015-04-27 10:50:29 +00002143 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002144 return true;
2145
Eli Bendersky38274122013-01-14 23:22:36 +00002146 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002147 // instantiation.
2148 OS << ".endmacro\n";
2149
Rafael Espindola3560ff22014-08-27 20:03:13 +00002150 std::unique_ptr<MemoryBuffer> Instantiation =
2151 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002152
Daniel Dunbar43235712010-07-18 18:54:11 +00002153 // Create the macro instantiation object and add to the current macro
2154 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002155 MacroInstantiation *MI = new MacroInstantiation(
2156 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002157 ActiveMacros.push_back(MI);
2158
Toma Tabacu217116e2015-04-27 10:50:29 +00002159 ++NumOfMacroInstantiations;
2160
Daniel Dunbar43235712010-07-18 18:54:11 +00002161 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002162 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002163 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002164 Lex();
2165
2166 return false;
2167}
2168
Jim Grosbach4b905842013-09-20 23:08:21 +00002169void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002170 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002171 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002172 Lex();
2173
2174 // Pop the instantiation entry.
2175 delete ActiveMacros.back();
2176 ActiveMacros.pop_back();
2177}
2178
Jim Grosbach4b905842013-09-20 23:08:21 +00002179static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002180 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002181 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002182 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2183 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002184 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002185 case MCExpr::Target:
2186 case MCExpr::Constant:
2187 return false;
2188 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002189 const MCSymbol &S =
2190 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002191 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002192 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002193 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002194 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002195 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002196 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002197 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002198
2199 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002200}
2201
Jim Grosbach4b905842013-09-20 23:08:21 +00002202bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002203 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002204 // FIXME: Use better location, we should use proper tokens.
2205 SMLoc EqualLoc = Lexer.getLoc();
2206
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002207 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002208 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002209 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002210
Rafael Espindola72f5f172012-01-28 05:57:00 +00002211 // Note: we don't count b as used in "a = b". This is to allow
2212 // a = b
2213 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002214
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002215 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002216 return TokError("unexpected token in assignment");
2217
2218 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002219 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002220
Daniel Dunbar5f339242009-10-16 01:57:39 +00002221 // Validate that the LHS is allowed to be a variable (either it has not been
2222 // used as a symbol, or it is an absolute symbol).
Jim Grosbach6f482002015-05-18 18:43:14 +00002223 MCSymbol *Sym = getContext().lookupSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002224 if (Sym) {
2225 // Diagnose assignment to a label.
2226 //
2227 // FIXME: Diagnostics. Note the location of the definition as a label.
2228 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002229 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002230 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2231 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002232 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002233 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2234 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002235 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002236 return Error(EqualLoc, "redefinition of '" + Name + "'");
2237 else if (!Sym->isVariable())
2238 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002239 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002240 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002241 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002242
2243 // Don't count these checks as uses.
2244 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002245 } else if (Name == ".") {
2246 if (Out.EmitValueToOffset(Value, 0)) {
2247 Error(EqualLoc, "expected absolute expression");
2248 eatToEndOfStatement();
2249 }
2250 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002251 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00002252 Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002253
David Majnemer58cb80c2014-12-24 10:27:50 +00002254 Sym->setRedefinable(allow_redef);
2255
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002256 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002257 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002258 if (NoDeadStrip)
2259 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2260
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002261 return false;
2262}
2263
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002264/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265/// ::= identifier
2266/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002267bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002268 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002269 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2270 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002271 // handle this as a context dependent token, instead we detect adjacent tokens
2272 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002273 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2274 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002275
Hans Wennborgce69d772013-10-18 20:46:28 +00002276 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002277 Lex();
2278 if (Lexer.isNot(AsmToken::Identifier))
2279 return true;
2280
Hans Wennborgce69d772013-10-18 20:46:28 +00002281 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2282 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002283 return true;
2284
2285 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002286 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002287 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002288 Lex();
2289 return false;
2290 }
2291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002293 return true;
2294
Sean Callanan936b0d32010-01-19 21:44:56 +00002295 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002296
Sean Callanan686ed8d2010-01-19 20:22:31 +00002297 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002298
2299 return false;
2300}
2301
Jim Grosbach4b905842013-09-20 23:08:21 +00002302/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002303/// ::= .equ identifier ',' expression
2304/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002305/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002306bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002307 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002308
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002309 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002310 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002311
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002312 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002313 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002314 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002315
Jim Grosbach4b905842013-09-20 23:08:21 +00002316 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002317}
2318
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002319bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002320 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002321
2322 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002323 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002324 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2325 if (Str[i] != '\\') {
2326 Data += Str[i];
2327 continue;
2328 }
2329
2330 // Recognize escaped characters. Note that this escape semantics currently
2331 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2332 ++i;
2333 if (i == e)
2334 return TokError("unexpected backslash at end of string");
2335
2336 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002337 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002338 // Consume up to three octal characters.
2339 unsigned Value = Str[i] - '0';
2340
Jim Grosbach4b905842013-09-20 23:08:21 +00002341 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002342 ++i;
2343 Value = Value * 8 + (Str[i] - '0');
2344
Jim Grosbach4b905842013-09-20 23:08:21 +00002345 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002346 ++i;
2347 Value = Value * 8 + (Str[i] - '0');
2348 }
2349 }
2350
2351 if (Value > 255)
2352 return TokError("invalid octal escape sequence (out of range)");
2353
Jim Grosbach4b905842013-09-20 23:08:21 +00002354 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002355 continue;
2356 }
2357
2358 // Otherwise recognize individual escapes.
2359 switch (Str[i]) {
2360 default:
2361 // Just reject invalid escape sequences for now.
2362 return TokError("invalid escape sequence (unrecognized character)");
2363
2364 case 'b': Data += '\b'; break;
2365 case 'f': Data += '\f'; break;
2366 case 'n': Data += '\n'; break;
2367 case 'r': Data += '\r'; break;
2368 case 't': Data += '\t'; break;
2369 case '"': Data += '"'; break;
2370 case '\\': Data += '\\'; break;
2371 }
2372 }
2373
2374 return false;
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002378/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002379bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002382
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002384 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002385 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002386
Daniel Dunbaref668c12009-08-14 18:19:52 +00002387 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002388 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002389 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002390
Rafael Espindola64e1af82013-07-02 15:49:13 +00002391 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002393 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002394
Sean Callanan686ed8d2010-01-19 20:22:31 +00002395 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002396
2397 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398 break;
2399
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002400 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002401 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002402 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002403 }
2404 }
2405
Sean Callanan686ed8d2010-01-19 20:22:31 +00002406 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002407 return false;
2408}
2409
Jim Grosbach4b905842013-09-20 23:08:21 +00002410/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002411/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002412bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002413 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002414 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002415
Daniel Dunbara10e5192009-06-24 23:30:00 +00002416 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002417 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002418 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002419 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002420 return true;
2421
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002422 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002423 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2424 assert(Size <= 8 && "Invalid size");
2425 uint64_t IntValue = MCE->getValue();
2426 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2427 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002428 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002429 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002430 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002431
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002432 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002433 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002434
Daniel Dunbara10e5192009-06-24 23:30:00 +00002435 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002436 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002437 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002438 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002439 }
2440 }
2441
Sean Callanan686ed8d2010-01-19 20:22:31 +00002442 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002443 return false;
2444}
2445
David Woodhoused6de0d92014-02-01 16:20:59 +00002446/// ParseDirectiveOctaValue
2447/// ::= .octa [ hexconstant (, hexconstant)* ]
2448bool AsmParser::parseDirectiveOctaValue() {
2449 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2450 checkForValidSection();
2451
2452 for (;;) {
2453 if (Lexer.getKind() == AsmToken::Error)
2454 return true;
2455 if (Lexer.getKind() != AsmToken::Integer &&
2456 Lexer.getKind() != AsmToken::BigNum)
2457 return TokError("unknown token in expression");
2458
2459 SMLoc ExprLoc = getLexer().getLoc();
2460 APInt IntValue = getTok().getAPIntVal();
2461 Lex();
2462
2463 uint64_t hi, lo;
2464 if (IntValue.isIntN(64)) {
2465 hi = 0;
2466 lo = IntValue.getZExtValue();
2467 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002468 // It might actually have more than 128 bits, but the top ones are zero.
2469 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002470 lo = IntValue.getLoBits(64).getZExtValue();
2471 } else
2472 return Error(ExprLoc, "literal value out of range for directive");
2473
2474 if (MAI.isLittleEndian()) {
2475 getStreamer().EmitIntValue(lo, 8);
2476 getStreamer().EmitIntValue(hi, 8);
2477 } else {
2478 getStreamer().EmitIntValue(hi, 8);
2479 getStreamer().EmitIntValue(lo, 8);
2480 }
2481
2482 if (getLexer().is(AsmToken::EndOfStatement))
2483 break;
2484
2485 // FIXME: Improve diagnostic.
2486 if (getLexer().isNot(AsmToken::Comma))
2487 return TokError("unexpected token in directive");
2488 Lex();
2489 }
2490 }
2491
2492 Lex();
2493 return false;
2494}
2495
Jim Grosbach4b905842013-09-20 23:08:21 +00002496/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002497/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002498bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002499 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002500 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002501
2502 for (;;) {
2503 // We don't truly support arithmetic on floating point expressions, so we
2504 // have to manually parse unary prefixes.
2505 bool IsNeg = false;
2506 if (getLexer().is(AsmToken::Minus)) {
2507 Lex();
2508 IsNeg = true;
2509 } else if (getLexer().is(AsmToken::Plus))
2510 Lex();
2511
Michael J. Spencer530ce852010-10-09 11:00:50 +00002512 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002513 getLexer().isNot(AsmToken::Real) &&
2514 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002515 return TokError("unexpected token in directive");
2516
2517 // Convert to an APFloat.
2518 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002519 StringRef IDVal = getTok().getString();
2520 if (getLexer().is(AsmToken::Identifier)) {
2521 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2522 Value = APFloat::getInf(Semantics);
2523 else if (!IDVal.compare_lower("nan"))
2524 Value = APFloat::getNaN(Semantics, false, ~0);
2525 else
2526 return TokError("invalid floating point literal");
2527 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002528 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002529 return TokError("invalid floating point literal");
2530 if (IsNeg)
2531 Value.changeSign();
2532
2533 // Consume the numeric token.
2534 Lex();
2535
2536 // Emit the value as an integer.
2537 APInt AsInt = Value.bitcastToAPInt();
2538 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002539 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002540
2541 if (getLexer().is(AsmToken::EndOfStatement))
2542 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002543
Daniel Dunbar2af16532010-09-24 01:59:56 +00002544 if (getLexer().isNot(AsmToken::Comma))
2545 return TokError("unexpected token in directive");
2546 Lex();
2547 }
2548 }
2549
2550 Lex();
2551 return false;
2552}
2553
Jim Grosbach4b905842013-09-20 23:08:21 +00002554/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002555/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002556bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002557 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002558
2559 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002560 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002561 return true;
2562
Rafael Espindolab91bac62010-10-05 19:42:57 +00002563 int64_t Val = 0;
2564 if (getLexer().is(AsmToken::Comma)) {
2565 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002566 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002567 return true;
2568 }
2569
Rafael Espindola922e3f42010-09-16 15:03:59 +00002570 if (getLexer().isNot(AsmToken::EndOfStatement))
2571 return TokError("unexpected token in '.zero' directive");
2572
2573 Lex();
2574
Rafael Espindola64e1af82013-07-02 15:49:13 +00002575 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002576
2577 return false;
2578}
2579
Jim Grosbach4b905842013-09-20 23:08:21 +00002580/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002581/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002582bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002583 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002584
David Majnemer522d3db2014-02-01 07:19:38 +00002585 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002586 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002587 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002588 return true;
2589
David Majnemer522d3db2014-02-01 07:19:38 +00002590 if (NumValues < 0) {
2591 Warning(RepeatLoc,
2592 "'.fill' directive with negative repeat count has no effect");
2593 NumValues = 0;
2594 }
2595
Roman Divackye33098f2013-09-24 17:44:41 +00002596 int64_t FillSize = 1;
2597 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002598
David Majnemer522d3db2014-02-01 07:19:38 +00002599 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002600 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2601 if (getLexer().isNot(AsmToken::Comma))
2602 return TokError("unexpected token in '.fill' directive");
2603 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002604
David Majnemer522d3db2014-02-01 07:19:38 +00002605 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002606 if (parseAbsoluteExpression(FillSize))
2607 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002608
Roman Divackye33098f2013-09-24 17:44:41 +00002609 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2610 if (getLexer().isNot(AsmToken::Comma))
2611 return TokError("unexpected token in '.fill' directive");
2612 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002613
David Majnemer522d3db2014-02-01 07:19:38 +00002614 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002615 if (parseAbsoluteExpression(FillExpr))
2616 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002617
Roman Divackye33098f2013-09-24 17:44:41 +00002618 if (getLexer().isNot(AsmToken::EndOfStatement))
2619 return TokError("unexpected token in '.fill' directive");
2620
2621 Lex();
2622 }
2623 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002624
David Majnemer522d3db2014-02-01 07:19:38 +00002625 if (FillSize < 0) {
2626 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2627 NumValues = 0;
2628 }
2629 if (FillSize > 8) {
2630 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2631 FillSize = 8;
2632 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002633
David Majnemer522d3db2014-02-01 07:19:38 +00002634 if (!isUInt<32>(FillExpr) && FillSize > 4)
2635 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2636
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002637 if (NumValues > 0) {
2638 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2639 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2640 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2641 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2642 if (NonZeroFillSize < FillSize)
2643 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2644 }
David Majnemer522d3db2014-02-01 07:19:38 +00002645 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002646
2647 return false;
2648}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002649
Jim Grosbach4b905842013-09-20 23:08:21 +00002650/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002651/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002652bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002653 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002654
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002655 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002656 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002658 return true;
2659
2660 // Parse optional fill expression.
2661 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002662 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2663 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002664 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002665 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002666
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002667 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002668 return true;
2669
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002670 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002671 return TokError("unexpected token in '.org' directive");
2672 }
2673
Sean Callanan686ed8d2010-01-19 20:22:31 +00002674 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002675
Jim Grosbachb5912772012-01-27 00:37:08 +00002676 // Only limited forms of relocatable expressions are accepted here, it
2677 // has to be relative to the current section. The streamer will return
2678 // 'true' if the expression wasn't evaluatable.
2679 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2680 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002681
2682 return false;
2683}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684
Jim Grosbach4b905842013-09-20 23:08:21 +00002685/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002687bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002688 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002689
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002690 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002692 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002693 return true;
2694
2695 SMLoc MaxBytesLoc;
2696 bool HasFillExpr = false;
2697 int64_t FillExpr = 0;
2698 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002699 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2700 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002701 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002702 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002703
2704 // The fill expression can be omitted while specifying a maximum number of
2705 // alignment bytes, e.g:
2706 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002707 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002708 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002709 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002710 return true;
2711 }
2712
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002713 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2714 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002715 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002716 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002718 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002719 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002721
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002722 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002723 return TokError("unexpected token in directive");
2724 }
2725 }
2726
Sean Callanan686ed8d2010-01-19 20:22:31 +00002727 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002728
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002729 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002730 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002731
2732 // Compute alignment in bytes.
2733 if (IsPow2) {
2734 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002735 if (Alignment >= 32) {
2736 Error(AlignmentLoc, "invalid alignment value");
2737 Alignment = 31;
2738 }
2739
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002740 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002741 } else {
2742 // Reject alignments that aren't a power of two, for gas compatibility.
2743 if (!isPowerOf2_64(Alignment))
2744 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002745 }
2746
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002747 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002748 if (MaxBytesLoc.isValid()) {
2749 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002750 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002751 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002752 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002753 }
2754
2755 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002756 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002757 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002758 MaxBytesToFill = 0;
2759 }
2760 }
2761
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002762 // Check whether we should use optimal code alignment for this .align
2763 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002764 const MCSection *Section = getStreamer().getCurrentSection().first;
2765 assert(Section && "must have section to emit alignment");
2766 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002767 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2768 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002769 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002770 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002771 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002772 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2773 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002774 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002775
2776 return false;
2777}
2778
Jim Grosbach4b905842013-09-20 23:08:21 +00002779/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002780/// ::= .file [number] filename
2781/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002782bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002783 // FIXME: I'm not sure what this is.
2784 int64_t FileNumber = -1;
2785 SMLoc FileNumberLoc = getLexer().getLoc();
2786 if (getLexer().is(AsmToken::Integer)) {
2787 FileNumber = getTok().getIntVal();
2788 Lex();
2789
2790 if (FileNumber < 1)
2791 return TokError("file number less than one");
2792 }
2793
2794 if (getLexer().isNot(AsmToken::String))
2795 return TokError("unexpected token in '.file' directive");
2796
2797 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002798 // Allow the strings to have escaped octal character sequence.
2799 std::string Path = getTok().getString();
2800 if (parseEscapedString(Path))
2801 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002802 Lex();
2803
2804 StringRef Directory;
2805 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002806 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002807 if (getLexer().is(AsmToken::String)) {
2808 if (FileNumber == -1)
2809 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002810 if (parseEscapedString(FilenameData))
2811 return true;
2812 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002813 Directory = Path;
2814 Lex();
2815 } else {
2816 Filename = Path;
2817 }
2818
2819 if (getLexer().isNot(AsmToken::EndOfStatement))
2820 return TokError("unexpected token in '.file' directive");
2821
2822 if (FileNumber == -1)
2823 getStreamer().EmitFileDirective(Filename);
2824 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002825 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002826 Error(DirectiveLoc,
2827 "input can't have .file dwarf directives when -g is "
2828 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002829
David Blaikiec714ef42014-03-17 01:52:11 +00002830 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2831 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002832 Error(FileNumberLoc, "file number already allocated");
2833 }
2834
2835 return false;
2836}
2837
Jim Grosbach4b905842013-09-20 23:08:21 +00002838/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002839/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002840bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002841 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2842 if (getLexer().isNot(AsmToken::Integer))
2843 return TokError("unexpected token in '.line' directive");
2844
2845 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002846 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002847 Lex();
2848
2849 // FIXME: Do something with the .line.
2850 }
2851
2852 if (getLexer().isNot(AsmToken::EndOfStatement))
2853 return TokError("unexpected token in '.line' directive");
2854
2855 return false;
2856}
2857
Jim Grosbach4b905842013-09-20 23:08:21 +00002858/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002859/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2860/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2861/// The first number is a file number, must have been previously assigned with
2862/// a .file directive, the second number is the line number and optionally the
2863/// third number is a column position (zero if not specified). The remaining
2864/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002865bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002866 if (getLexer().isNot(AsmToken::Integer))
2867 return TokError("unexpected token in '.loc' directive");
2868 int64_t FileNumber = getTok().getIntVal();
2869 if (FileNumber < 1)
2870 return TokError("file number less than one in '.loc' directive");
2871 if (!getContext().isValidDwarfFileNumber(FileNumber))
2872 return TokError("unassigned file number in '.loc' directive");
2873 Lex();
2874
2875 int64_t LineNumber = 0;
2876 if (getLexer().is(AsmToken::Integer)) {
2877 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002878 if (LineNumber < 0)
2879 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002880 Lex();
2881 }
2882
2883 int64_t ColumnPos = 0;
2884 if (getLexer().is(AsmToken::Integer)) {
2885 ColumnPos = getTok().getIntVal();
2886 if (ColumnPos < 0)
2887 return TokError("column position less than zero in '.loc' directive");
2888 Lex();
2889 }
2890
2891 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2892 unsigned Isa = 0;
2893 int64_t Discriminator = 0;
2894 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2895 for (;;) {
2896 if (getLexer().is(AsmToken::EndOfStatement))
2897 break;
2898
2899 StringRef Name;
2900 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002901 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002902 return TokError("unexpected token in '.loc' directive");
2903
2904 if (Name == "basic_block")
2905 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2906 else if (Name == "prologue_end")
2907 Flags |= DWARF2_FLAG_PROLOGUE_END;
2908 else if (Name == "epilogue_begin")
2909 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2910 else if (Name == "is_stmt") {
2911 Loc = getTok().getLoc();
2912 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002913 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return true;
2915 // The expression must be the constant 0 or 1.
2916 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2917 int Value = MCE->getValue();
2918 if (Value == 0)
2919 Flags &= ~DWARF2_FLAG_IS_STMT;
2920 else if (Value == 1)
2921 Flags |= DWARF2_FLAG_IS_STMT;
2922 else
2923 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002924 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002925 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2926 }
Craig Topperf15655b2013-04-22 04:22:40 +00002927 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002928 Loc = getTok().getLoc();
2929 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002930 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002931 return true;
2932 // The expression must be a constant greater or equal to 0.
2933 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2934 int Value = MCE->getValue();
2935 if (Value < 0)
2936 return Error(Loc, "isa number less than zero");
2937 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002938 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002939 return Error(Loc, "isa number not a constant value");
2940 }
Craig Topperf15655b2013-04-22 04:22:40 +00002941 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002942 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002943 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002944 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002945 return Error(Loc, "unknown sub-directive in '.loc' directive");
2946 }
2947
2948 if (getLexer().is(AsmToken::EndOfStatement))
2949 break;
2950 }
2951 }
2952
2953 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2954 Isa, Discriminator, StringRef());
2955
2956 return false;
2957}
2958
Jim Grosbach4b905842013-09-20 23:08:21 +00002959/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002960/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002961bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002962 return TokError("unsupported directive '.stabs'");
2963}
2964
Jim Grosbach4b905842013-09-20 23:08:21 +00002965/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002966/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002967bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002968 StringRef Name;
2969 bool EH = false;
2970 bool Debug = false;
2971
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002972 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002973 return TokError("Expected an identifier");
2974
2975 if (Name == ".eh_frame")
2976 EH = true;
2977 else if (Name == ".debug_frame")
2978 Debug = true;
2979
2980 if (getLexer().is(AsmToken::Comma)) {
2981 Lex();
2982
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002983 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002984 return TokError("Expected an identifier");
2985
2986 if (Name == ".eh_frame")
2987 EH = true;
2988 else if (Name == ".debug_frame")
2989 Debug = true;
2990 }
2991
2992 getStreamer().EmitCFISections(EH, Debug);
2993 return false;
2994}
2995
Jim Grosbach4b905842013-09-20 23:08:21 +00002996/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002997/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002998bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002999 StringRef Simple;
3000 if (getLexer().isNot(AsmToken::EndOfStatement))
3001 if (parseIdentifier(Simple) || Simple != "simple")
3002 return TokError("unexpected token in .cfi_startproc directive");
3003
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003004 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003005 return false;
3006}
3007
Jim Grosbach4b905842013-09-20 23:08:21 +00003008/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003009/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003010bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003011 getStreamer().EmitCFIEndProc();
3012 return false;
3013}
3014
Jim Grosbach4b905842013-09-20 23:08:21 +00003015/// \brief parse register name or number.
3016bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003017 SMLoc DirectiveLoc) {
3018 unsigned RegNo;
3019
3020 if (getLexer().isNot(AsmToken::Integer)) {
3021 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3022 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003023 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003024 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003025 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003026
3027 return false;
3028}
3029
Jim Grosbach4b905842013-09-20 23:08:21 +00003030/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003031/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003032bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003033 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003034 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003035 return true;
3036
3037 if (getLexer().isNot(AsmToken::Comma))
3038 return TokError("unexpected token in directive");
3039 Lex();
3040
3041 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003042 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003043 return true;
3044
3045 getStreamer().EmitCFIDefCfa(Register, Offset);
3046 return false;
3047}
3048
Jim Grosbach4b905842013-09-20 23:08:21 +00003049/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003050/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003051bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003052 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003053 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003054 return true;
3055
3056 getStreamer().EmitCFIDefCfaOffset(Offset);
3057 return false;
3058}
3059
Jim Grosbach4b905842013-09-20 23:08:21 +00003060/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003061/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003062bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003063 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003064 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003065 return true;
3066
3067 if (getLexer().isNot(AsmToken::Comma))
3068 return TokError("unexpected token in directive");
3069 Lex();
3070
3071 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003072 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return true;
3074
3075 getStreamer().EmitCFIRegister(Register1, Register2);
3076 return false;
3077}
3078
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003079/// parseDirectiveCFIWindowSave
3080/// ::= .cfi_window_save
3081bool AsmParser::parseDirectiveCFIWindowSave() {
3082 getStreamer().EmitCFIWindowSave();
3083 return false;
3084}
3085
Jim Grosbach4b905842013-09-20 23:08:21 +00003086/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003087/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003088bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003089 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003090 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return true;
3092
3093 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3094 return false;
3095}
3096
Jim Grosbach4b905842013-09-20 23:08:21 +00003097/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003098/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003099bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003100 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003101 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
3103
3104 getStreamer().EmitCFIDefCfaRegister(Register);
3105 return false;
3106}
3107
Jim Grosbach4b905842013-09-20 23:08:21 +00003108/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003109/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003110bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003111 int64_t Register = 0;
3112 int64_t Offset = 0;
3113
Jim Grosbach4b905842013-09-20 23:08:21 +00003114 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003115 return true;
3116
3117 if (getLexer().isNot(AsmToken::Comma))
3118 return TokError("unexpected token in directive");
3119 Lex();
3120
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003121 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003122 return true;
3123
3124 getStreamer().EmitCFIOffset(Register, Offset);
3125 return false;
3126}
3127
Jim Grosbach4b905842013-09-20 23:08:21 +00003128/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003129/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003130bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003131 int64_t Register = 0;
3132
Jim Grosbach4b905842013-09-20 23:08:21 +00003133 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003134 return true;
3135
3136 if (getLexer().isNot(AsmToken::Comma))
3137 return TokError("unexpected token in directive");
3138 Lex();
3139
3140 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003141 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003142 return true;
3143
3144 getStreamer().EmitCFIRelOffset(Register, Offset);
3145 return false;
3146}
3147
3148static bool isValidEncoding(int64_t Encoding) {
3149 if (Encoding & ~0xff)
3150 return false;
3151
3152 if (Encoding == dwarf::DW_EH_PE_omit)
3153 return true;
3154
3155 const unsigned Format = Encoding & 0xf;
3156 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3157 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3158 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3159 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3160 return false;
3161
3162 const unsigned Application = Encoding & 0x70;
3163 if (Application != dwarf::DW_EH_PE_absptr &&
3164 Application != dwarf::DW_EH_PE_pcrel)
3165 return false;
3166
3167 return true;
3168}
3169
Jim Grosbach4b905842013-09-20 23:08:21 +00003170/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003171/// IsPersonality true for cfi_personality, false for cfi_lsda
3172/// ::= .cfi_personality encoding, [symbol_name]
3173/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003174bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003175 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003176 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003177 return true;
3178 if (Encoding == dwarf::DW_EH_PE_omit)
3179 return false;
3180
3181 if (!isValidEncoding(Encoding))
3182 return TokError("unsupported encoding.");
3183
3184 if (getLexer().isNot(AsmToken::Comma))
3185 return TokError("unexpected token in directive");
3186 Lex();
3187
3188 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003189 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003190 return TokError("expected identifier in directive");
3191
Jim Grosbach6f482002015-05-18 18:43:14 +00003192 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003193
3194 if (IsPersonality)
3195 getStreamer().EmitCFIPersonality(Sym, Encoding);
3196 else
3197 getStreamer().EmitCFILsda(Sym, Encoding);
3198 return false;
3199}
3200
Jim Grosbach4b905842013-09-20 23:08:21 +00003201/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003202/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003203bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003204 getStreamer().EmitCFIRememberState();
3205 return false;
3206}
3207
Jim Grosbach4b905842013-09-20 23:08:21 +00003208/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003209/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003210bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003211 getStreamer().EmitCFIRestoreState();
3212 return false;
3213}
3214
Jim Grosbach4b905842013-09-20 23:08:21 +00003215/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003216/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003217bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003218 int64_t Register = 0;
3219
Jim Grosbach4b905842013-09-20 23:08:21 +00003220 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003221 return true;
3222
3223 getStreamer().EmitCFISameValue(Register);
3224 return false;
3225}
3226
Jim Grosbach4b905842013-09-20 23:08:21 +00003227/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003228/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003229bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003230 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003231 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003232 return true;
3233
3234 getStreamer().EmitCFIRestore(Register);
3235 return false;
3236}
3237
Jim Grosbach4b905842013-09-20 23:08:21 +00003238/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003239/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003240bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003241 std::string Values;
3242 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003243 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003244 return true;
3245
3246 Values.push_back((uint8_t)CurrValue);
3247
3248 while (getLexer().is(AsmToken::Comma)) {
3249 Lex();
3250
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003251 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003252 return true;
3253
3254 Values.push_back((uint8_t)CurrValue);
3255 }
3256
3257 getStreamer().EmitCFIEscape(Values);
3258 return false;
3259}
3260
Jim Grosbach4b905842013-09-20 23:08:21 +00003261/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003262/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003263bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003264 if (getLexer().isNot(AsmToken::EndOfStatement))
3265 return Error(getLexer().getLoc(),
3266 "unexpected token in '.cfi_signal_frame'");
3267
3268 getStreamer().EmitCFISignalFrame();
3269 return false;
3270}
3271
Jim Grosbach4b905842013-09-20 23:08:21 +00003272/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003273/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003274bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003275 int64_t Register = 0;
3276
Jim Grosbach4b905842013-09-20 23:08:21 +00003277 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003278 return true;
3279
3280 getStreamer().EmitCFIUndefined(Register);
3281 return false;
3282}
3283
Jim Grosbach4b905842013-09-20 23:08:21 +00003284/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003285/// ::= .macros_on
3286/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003287bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003288 if (getLexer().isNot(AsmToken::EndOfStatement))
3289 return Error(getLexer().getLoc(),
3290 "unexpected token in '" + Directive + "' directive");
3291
Jim Grosbach4b905842013-09-20 23:08:21 +00003292 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003293 return false;
3294}
3295
Jim Grosbach4b905842013-09-20 23:08:21 +00003296/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003297/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003298bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003299 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003300 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003301 return TokError("expected identifier in '.macro' directive");
3302
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003303 if (getLexer().is(AsmToken::Comma))
3304 Lex();
3305
Eli Bendersky17233942013-01-15 22:59:42 +00003306 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003307 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003308
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003309 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003310 return Error(Lexer.getLoc(),
3311 "Vararg parameter '" + Parameters.back().Name +
3312 "' should be last one in the list of parameters.");
3313
David Majnemer91fc4c22014-01-29 18:57:46 +00003314 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003315 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003316 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003317
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003318 if (Lexer.is(AsmToken::Colon)) {
3319 Lex(); // consume ':'
3320
3321 SMLoc QualLoc;
3322 StringRef Qualifier;
3323
3324 QualLoc = Lexer.getLoc();
3325 if (parseIdentifier(Qualifier))
3326 return Error(QualLoc, "missing parameter qualifier for "
3327 "'" + Parameter.Name + "' in macro '" + Name + "'");
3328
3329 if (Qualifier == "req")
3330 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003331 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003332 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003333 else
3334 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3335 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3336 }
3337
David Majnemer91fc4c22014-01-29 18:57:46 +00003338 if (getLexer().is(AsmToken::Equal)) {
3339 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003340
3341 SMLoc ParamLoc;
3342
3343 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003344 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003345 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003346
3347 if (Parameter.Required)
3348 Warning(ParamLoc, "pointless default value for required parameter "
3349 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003350 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003351
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003352 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003353
3354 if (getLexer().is(AsmToken::Comma))
3355 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003356 }
3357
3358 // Eat the end of statement.
3359 Lex();
3360
3361 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003362 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003363
3364 // Lex the macro definition.
3365 for (;;) {
3366 // Check whether we have reached the end of the file.
3367 if (getLexer().is(AsmToken::Eof))
3368 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3369
3370 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003371 if (getLexer().is(AsmToken::Identifier)) {
3372 if (getTok().getIdentifier() == ".endm" ||
3373 getTok().getIdentifier() == ".endmacro") {
3374 if (MacroDepth == 0) { // Outermost macro.
3375 EndToken = getTok();
3376 Lex();
3377 if (getLexer().isNot(AsmToken::EndOfStatement))
3378 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3379 "' directive");
3380 break;
3381 } else {
3382 // Otherwise we just found the end of an inner macro.
3383 --MacroDepth;
3384 }
3385 } else if (getTok().getIdentifier() == ".macro") {
3386 // We allow nested macros. Those aren't instantiated until the outermost
3387 // macro is expanded so just ignore them for now.
3388 ++MacroDepth;
3389 }
Eli Bendersky17233942013-01-15 22:59:42 +00003390 }
3391
3392 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003393 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003394 }
3395
Jim Grosbach4b905842013-09-20 23:08:21 +00003396 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003397 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3398 }
3399
3400 const char *BodyStart = StartToken.getLoc().getPointer();
3401 const char *BodyEnd = EndToken.getLoc().getPointer();
3402 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003403 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003404 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003405 return false;
3406}
3407
Jim Grosbach4b905842013-09-20 23:08:21 +00003408/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003409///
3410/// With the support added for named parameters there may be code out there that
3411/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003412/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003413/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003414/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003415/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3416/// warning that the positional parameter found in body which have no effect.
3417/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003418/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003419/// intended or change the macro to use the named parameters. It is possible
3420/// this warning will trigger when the none of the named parameters are used
3421/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003422void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003423 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003424 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003425 // If this macro is not defined with named parameters the warning we are
3426 // checking for here doesn't apply.
3427 unsigned NParameters = Parameters.size();
3428 if (NParameters == 0)
3429 return;
3430
3431 bool NamedParametersFound = false;
3432 bool PositionalParametersFound = false;
3433
3434 // Look at the body of the macro for use of both the named parameters and what
3435 // are likely to be positional parameters. This is what expandMacro() is
3436 // doing when it finds the parameters in the body.
3437 while (!Body.empty()) {
3438 // Scan for the next possible parameter.
3439 std::size_t End = Body.size(), Pos = 0;
3440 for (; Pos != End; ++Pos) {
3441 // Check for a substitution or escape.
3442 // This macro is defined with parameters, look for \foo, \bar, etc.
3443 if (Body[Pos] == '\\' && Pos + 1 != End)
3444 break;
3445
3446 // This macro should have parameters, but look for $0, $1, ..., $n too.
3447 if (Body[Pos] != '$' || Pos + 1 == End)
3448 continue;
3449 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003450 if (Next == '$' || Next == 'n' ||
3451 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003452 break;
3453 }
3454
3455 // Check if we reached the end.
3456 if (Pos == End)
3457 break;
3458
3459 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003460 switch (Body[Pos + 1]) {
3461 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003462 case '$':
3463 break;
3464
Jim Grosbach4b905842013-09-20 23:08:21 +00003465 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003466 case 'n':
3467 PositionalParametersFound = true;
3468 break;
3469
Jim Grosbach4b905842013-09-20 23:08:21 +00003470 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003471 default: {
3472 PositionalParametersFound = true;
3473 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003474 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003475 }
3476 Pos += 2;
3477 } else {
3478 unsigned I = Pos + 1;
3479 while (isIdentifierChar(Body[I]) && I + 1 != End)
3480 ++I;
3481
Jim Grosbach4b905842013-09-20 23:08:21 +00003482 const char *Begin = Body.data() + Pos + 1;
3483 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003484 unsigned Index = 0;
3485 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003486 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003487 break;
3488
3489 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003490 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3491 Pos += 3;
3492 else {
3493 Pos = I;
3494 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003495 } else {
3496 NamedParametersFound = true;
3497 Pos += 1 + Argument.size();
3498 }
3499 }
3500 // Update the scan point.
3501 Body = Body.substr(Pos);
3502 }
3503
3504 if (!NamedParametersFound && PositionalParametersFound)
3505 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3506 "used in macro body, possible positional parameter "
3507 "found in body which will have no effect");
3508}
3509
Nico Weber155dccd12014-07-24 17:08:39 +00003510/// parseDirectiveExitMacro
3511/// ::= .exitm
3512bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3513 if (getLexer().isNot(AsmToken::EndOfStatement))
3514 return TokError("unexpected token in '" + Directive + "' directive");
3515
3516 if (!isInsideMacroInstantiation())
3517 return TokError("unexpected '" + Directive + "' in file, "
3518 "no current macro definition");
3519
3520 // Exit all conditionals that are active in the current macro.
3521 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3522 TheCondState = TheCondStack.back();
3523 TheCondStack.pop_back();
3524 }
3525
3526 handleMacroExit();
3527 return false;
3528}
3529
Jim Grosbach4b905842013-09-20 23:08:21 +00003530/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003531/// ::= .endm
3532/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003533bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003534 if (getLexer().isNot(AsmToken::EndOfStatement))
3535 return TokError("unexpected token in '" + Directive + "' directive");
3536
3537 // If we are inside a macro instantiation, terminate the current
3538 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003539 if (isInsideMacroInstantiation()) {
3540 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003541 return false;
3542 }
3543
3544 // Otherwise, this .endmacro is a stray entry in the file; well formed
3545 // .endmacro directives are handled during the macro definition parsing.
3546 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003547 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003548}
3549
Jim Grosbach4b905842013-09-20 23:08:21 +00003550/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003551/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003552bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003553 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003554 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003555 return TokError("expected identifier in '.purgem' directive");
3556
3557 if (getLexer().isNot(AsmToken::EndOfStatement))
3558 return TokError("unexpected token in '.purgem' directive");
3559
Jim Grosbach4b905842013-09-20 23:08:21 +00003560 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003561 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3562
Jim Grosbach4b905842013-09-20 23:08:21 +00003563 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003564 return false;
3565}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003566
Jim Grosbach4b905842013-09-20 23:08:21 +00003567/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003568/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003569bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003570 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003571
3572 // Expect a single argument: an expression that evaluates to a constant
3573 // in the inclusive range 0-30.
3574 SMLoc ExprLoc = getLexer().getLoc();
3575 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003576 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003577 return true;
3578 else if (getLexer().isNot(AsmToken::EndOfStatement))
3579 return TokError("unexpected token after expression in"
3580 " '.bundle_align_mode' directive");
3581 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3582 return Error(ExprLoc,
3583 "invalid bundle alignment size (expected between 0 and 30)");
3584
3585 Lex();
3586
3587 // Because of AlignSizePow2's verified range we can safely truncate it to
3588 // unsigned.
3589 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3590 return false;
3591}
3592
Jim Grosbach4b905842013-09-20 23:08:21 +00003593/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003594/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003595bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003596 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003597 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003598
Eli Bendersky802b6282013-01-07 21:51:08 +00003599 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3600 StringRef Option;
3601 SMLoc Loc = getTok().getLoc();
3602 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003603 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003604
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003605 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003606 return Error(Loc, kInvalidOptionError);
3607
3608 if (Option != "align_to_end")
3609 return Error(Loc, kInvalidOptionError);
3610 else if (getLexer().isNot(AsmToken::EndOfStatement))
3611 return Error(Loc,
3612 "unexpected token after '.bundle_lock' directive option");
3613 AlignToEnd = true;
3614 }
3615
Eli Benderskyf483ff92012-12-20 19:05:53 +00003616 Lex();
3617
Eli Bendersky802b6282013-01-07 21:51:08 +00003618 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003619 return false;
3620}
3621
Jim Grosbach4b905842013-09-20 23:08:21 +00003622/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003623/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003624bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003625 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003626
3627 if (getLexer().isNot(AsmToken::EndOfStatement))
3628 return TokError("unexpected token in '.bundle_unlock' directive");
3629 Lex();
3630
3631 getStreamer().EmitBundleUnlock();
3632 return false;
3633}
3634
Jim Grosbach4b905842013-09-20 23:08:21 +00003635/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003636/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003637bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003638 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003639
3640 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003642 return true;
3643
3644 int64_t FillExpr = 0;
3645 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3646 if (getLexer().isNot(AsmToken::Comma))
3647 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3648 Lex();
3649
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003650 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003651 return true;
3652
3653 if (getLexer().isNot(AsmToken::EndOfStatement))
3654 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3655 }
3656
3657 Lex();
3658
3659 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003660 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3661 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003662
3663 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003664 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003665
3666 return false;
3667}
3668
Jim Grosbach4b905842013-09-20 23:08:21 +00003669/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003670/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003671bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003672 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003673 const MCExpr *Value;
3674
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003675 for (;;) {
3676 if (parseExpression(Value))
3677 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003678
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003679 if (Signed)
3680 getStreamer().EmitSLEB128Value(Value);
3681 else
3682 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003683
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003684 if (getLexer().is(AsmToken::EndOfStatement))
3685 break;
3686
3687 if (getLexer().isNot(AsmToken::Comma))
3688 return TokError("unexpected token in directive");
3689 Lex();
3690 }
Eli Bendersky17233942013-01-15 22:59:42 +00003691
3692 return false;
3693}
3694
Jim Grosbach4b905842013-09-20 23:08:21 +00003695/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003696/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003697bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003698 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003699 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003700 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003701 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003702
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003703 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003704 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003705
Jim Grosbach6f482002015-05-18 18:43:14 +00003706 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003707
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003708 // Assembler local symbols don't make any sense here. Complain loudly.
3709 if (Sym->isTemporary())
3710 return Error(Loc, "non-local symbol required in directive");
3711
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003712 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3713 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003714
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003715 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003716 break;
3717
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003718 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003719 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003720 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003721 }
3722 }
3723
Sean Callanan686ed8d2010-01-19 20:22:31 +00003724 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003725 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003726}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003727
Jim Grosbach4b905842013-09-20 23:08:21 +00003728/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003729/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003730bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003731 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003732
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003734 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003735 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003737
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003738 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003739 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003740
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003741 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003743 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003744
3745 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003746 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003747 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003748 return true;
3749
3750 int64_t Pow2Alignment = 0;
3751 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003752 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003753 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003754 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003755 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003756 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003757
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003758 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3759 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003760 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3761
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003762 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003763 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3764 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003765 if (!isPowerOf2_64(Pow2Alignment))
3766 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3767 Pow2Alignment = Log2_64(Pow2Alignment);
3768 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003769 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003770
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003771 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003772 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003773
Sean Callanan686ed8d2010-01-19 20:22:31 +00003774 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003775
Chris Lattner28ad7542009-07-09 17:25:12 +00003776 // NOTE: a size of zero for a .comm should create a undefined symbol
3777 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003778 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003779 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003780 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003781
Eric Christopherbc818852010-05-14 01:38:54 +00003782 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003783 // may internally end up wanting an alignment in bytes.
3784 // FIXME: Diagnose overflow.
3785 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003786 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003787 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003788
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003789 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003790 return Error(IDLoc, "invalid symbol redefinition");
3791
Chris Lattner28ad7542009-07-09 17:25:12 +00003792 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003793 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003794 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003795 return false;
3796 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003797
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003798 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003799 return false;
3800}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003801
Jim Grosbach4b905842013-09-20 23:08:21 +00003802/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003803/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003804bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003805 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003806 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003807
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003808 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003809 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003810 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003811
Sean Callanan686ed8d2010-01-19 20:22:31 +00003812 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003813
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003814 if (Str.empty())
3815 Error(Loc, ".abort detected. Assembly stopping.");
3816 else
3817 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003818 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003819
3820 return false;
3821}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003822
Jim Grosbach4b905842013-09-20 23:08:21 +00003823/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003824/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003825bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003826 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003827 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003828
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003829 // Allow the strings to have escaped octal character sequence.
3830 std::string Filename;
3831 if (parseEscapedString(Filename))
3832 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003833 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003834 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003835
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003836 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003837 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003838
Chris Lattner693fbb82009-07-16 06:14:39 +00003839 // Attempt to switch the lexer to the included file before consuming the end
3840 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003841 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003842 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003843 return true;
3844 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003845
3846 return false;
3847}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003848
Jim Grosbach4b905842013-09-20 23:08:21 +00003849/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003850/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003851bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003852 if (getLexer().isNot(AsmToken::String))
3853 return TokError("expected string in '.incbin' directive");
3854
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003855 // Allow the strings to have escaped octal character sequence.
3856 std::string Filename;
3857 if (parseEscapedString(Filename))
3858 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003859 SMLoc IncbinLoc = getLexer().getLoc();
3860 Lex();
3861
3862 if (getLexer().isNot(AsmToken::EndOfStatement))
3863 return TokError("unexpected token in '.incbin' directive");
3864
Kevin Enderby109f25c2011-12-14 21:47:48 +00003865 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003866 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003867 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3868 return true;
3869 }
3870
3871 return false;
3872}
3873
Jim Grosbach4b905842013-09-20 23:08:21 +00003874/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003875/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3876bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003877 TheCondStack.push_back(TheCondState);
3878 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003879 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003880 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003881 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003882 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003883 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003884 return true;
3885
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003886 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003887 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003888
Sean Callanan686ed8d2010-01-19 20:22:31 +00003889 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003890
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003891 switch (DirKind) {
3892 default:
3893 llvm_unreachable("unsupported directive");
3894 case DK_IF:
3895 case DK_IFNE:
3896 break;
3897 case DK_IFEQ:
3898 ExprValue = ExprValue == 0;
3899 break;
3900 case DK_IFGE:
3901 ExprValue = ExprValue >= 0;
3902 break;
3903 case DK_IFGT:
3904 ExprValue = ExprValue > 0;
3905 break;
3906 case DK_IFLE:
3907 ExprValue = ExprValue <= 0;
3908 break;
3909 case DK_IFLT:
3910 ExprValue = ExprValue < 0;
3911 break;
3912 }
3913
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003914 TheCondState.CondMet = ExprValue;
3915 TheCondState.Ignore = !TheCondState.CondMet;
3916 }
3917
3918 return false;
3919}
3920
Jim Grosbach4b905842013-09-20 23:08:21 +00003921/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003922/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003923bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003924 TheCondStack.push_back(TheCondState);
3925 TheCondState.TheCond = AsmCond::IfCond;
3926
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003927 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003928 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003929 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003930 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003931
3932 if (getLexer().isNot(AsmToken::EndOfStatement))
3933 return TokError("unexpected token in '.ifb' directive");
3934
3935 Lex();
3936
3937 TheCondState.CondMet = ExpectBlank == Str.empty();
3938 TheCondState.Ignore = !TheCondState.CondMet;
3939 }
3940
3941 return false;
3942}
3943
Jim Grosbach4b905842013-09-20 23:08:21 +00003944/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003945/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003946/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003947bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003948 TheCondStack.push_back(TheCondState);
3949 TheCondState.TheCond = AsmCond::IfCond;
3950
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003951 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003952 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003953 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003954 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003955
3956 if (getLexer().isNot(AsmToken::Comma))
3957 return TokError("unexpected token in '.ifc' directive");
3958
3959 Lex();
3960
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003961 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003962
3963 if (getLexer().isNot(AsmToken::EndOfStatement))
3964 return TokError("unexpected token in '.ifc' directive");
3965
3966 Lex();
3967
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003968 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003969 TheCondState.Ignore = !TheCondState.CondMet;
3970 }
3971
3972 return false;
3973}
3974
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003975/// parseDirectiveIfeqs
3976/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003977bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003978 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003979 if (ExpectEqual)
3980 TokError("expected string parameter for '.ifeqs' directive");
3981 else
3982 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003983 eatToEndOfStatement();
3984 return true;
3985 }
3986
3987 StringRef String1 = getTok().getStringContents();
3988 Lex();
3989
3990 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003991 if (ExpectEqual)
3992 TokError("expected comma after first string for '.ifeqs' directive");
3993 else
3994 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003995 eatToEndOfStatement();
3996 return true;
3997 }
3998
3999 Lex();
4000
4001 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004002 if (ExpectEqual)
4003 TokError("expected string parameter for '.ifeqs' directive");
4004 else
4005 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004006 eatToEndOfStatement();
4007 return true;
4008 }
4009
4010 StringRef String2 = getTok().getStringContents();
4011 Lex();
4012
4013 TheCondStack.push_back(TheCondState);
4014 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004015 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004016 TheCondState.Ignore = !TheCondState.CondMet;
4017
4018 return false;
4019}
4020
Jim Grosbach4b905842013-09-20 23:08:21 +00004021/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004022/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004023bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004024 StringRef Name;
4025 TheCondStack.push_back(TheCondState);
4026 TheCondState.TheCond = AsmCond::IfCond;
4027
4028 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004029 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004030 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004031 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004032 return TokError("expected identifier after '.ifdef'");
4033
4034 Lex();
4035
Jim Grosbach6f482002015-05-18 18:43:14 +00004036 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004037
4038 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004039 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004040 else
Craig Topper353eda42014-04-24 06:44:33 +00004041 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004042 TheCondState.Ignore = !TheCondState.CondMet;
4043 }
4044
4045 return false;
4046}
4047
Jim Grosbach4b905842013-09-20 23:08:21 +00004048/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004049/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004050bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004051 if (TheCondState.TheCond != AsmCond::IfCond &&
4052 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004053 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4054 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004055 TheCondState.TheCond = AsmCond::ElseIfCond;
4056
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004057 bool LastIgnoreState = false;
4058 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004059 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004060 if (LastIgnoreState || TheCondState.CondMet) {
4061 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004062 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004063 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004064 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004065 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004066 return true;
4067
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004068 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004069 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004070
Sean Callanan686ed8d2010-01-19 20:22:31 +00004071 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004072 TheCondState.CondMet = ExprValue;
4073 TheCondState.Ignore = !TheCondState.CondMet;
4074 }
4075
4076 return false;
4077}
4078
Jim Grosbach4b905842013-09-20 23:08:21 +00004079/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004080/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004081bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004082 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004083 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004084
Sean Callanan686ed8d2010-01-19 20:22:31 +00004085 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004086
4087 if (TheCondState.TheCond != AsmCond::IfCond &&
4088 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004089 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4090 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004091 TheCondState.TheCond = AsmCond::ElseCond;
4092 bool LastIgnoreState = false;
4093 if (!TheCondStack.empty())
4094 LastIgnoreState = TheCondStack.back().Ignore;
4095 if (LastIgnoreState || TheCondState.CondMet)
4096 TheCondState.Ignore = true;
4097 else
4098 TheCondState.Ignore = false;
4099
4100 return false;
4101}
4102
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004103/// parseDirectiveEnd
4104/// ::= .end
4105bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4106 if (getLexer().isNot(AsmToken::EndOfStatement))
4107 return TokError("unexpected token in '.end' directive");
4108
4109 Lex();
4110
4111 while (Lexer.isNot(AsmToken::Eof))
4112 Lex();
4113
4114 return false;
4115}
4116
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004117/// parseDirectiveError
4118/// ::= .err
4119/// ::= .error [string]
4120bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4121 if (!TheCondStack.empty()) {
4122 if (TheCondStack.back().Ignore) {
4123 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004124 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004125 }
4126 }
4127
4128 if (!WithMessage)
4129 return Error(L, ".err encountered");
4130
4131 StringRef Message = ".error directive invoked in source file";
4132 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4133 if (Lexer.isNot(AsmToken::String)) {
4134 TokError(".error argument must be a string");
4135 eatToEndOfStatement();
4136 return true;
4137 }
4138
4139 Message = getTok().getStringContents();
4140 Lex();
4141 }
4142
4143 Error(L, Message);
4144 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004145}
4146
Nico Weber404012b2014-07-24 16:26:06 +00004147/// parseDirectiveWarning
4148/// ::= .warning [string]
4149bool AsmParser::parseDirectiveWarning(SMLoc L) {
4150 if (!TheCondStack.empty()) {
4151 if (TheCondStack.back().Ignore) {
4152 eatToEndOfStatement();
4153 return false;
4154 }
4155 }
4156
4157 StringRef Message = ".warning directive invoked in source file";
4158 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4159 if (Lexer.isNot(AsmToken::String)) {
4160 TokError(".warning argument must be a string");
4161 eatToEndOfStatement();
4162 return true;
4163 }
4164
4165 Message = getTok().getStringContents();
4166 Lex();
4167 }
4168
4169 Warning(L, Message);
4170 return false;
4171}
4172
Jim Grosbach4b905842013-09-20 23:08:21 +00004173/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004174/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004175bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004176 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004177 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004178
Sean Callanan686ed8d2010-01-19 20:22:31 +00004179 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004180
Jim Grosbach4b905842013-09-20 23:08:21 +00004181 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004182 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4183 ".else");
4184 if (!TheCondStack.empty()) {
4185 TheCondState = TheCondStack.back();
4186 TheCondStack.pop_back();
4187 }
4188
4189 return false;
4190}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004191
Eli Bendersky17233942013-01-15 22:59:42 +00004192void AsmParser::initializeDirectiveKindMap() {
4193 DirectiveKindMap[".set"] = DK_SET;
4194 DirectiveKindMap[".equ"] = DK_EQU;
4195 DirectiveKindMap[".equiv"] = DK_EQUIV;
4196 DirectiveKindMap[".ascii"] = DK_ASCII;
4197 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4198 DirectiveKindMap[".string"] = DK_STRING;
4199 DirectiveKindMap[".byte"] = DK_BYTE;
4200 DirectiveKindMap[".short"] = DK_SHORT;
4201 DirectiveKindMap[".value"] = DK_VALUE;
4202 DirectiveKindMap[".2byte"] = DK_2BYTE;
4203 DirectiveKindMap[".long"] = DK_LONG;
4204 DirectiveKindMap[".int"] = DK_INT;
4205 DirectiveKindMap[".4byte"] = DK_4BYTE;
4206 DirectiveKindMap[".quad"] = DK_QUAD;
4207 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004208 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004209 DirectiveKindMap[".single"] = DK_SINGLE;
4210 DirectiveKindMap[".float"] = DK_FLOAT;
4211 DirectiveKindMap[".double"] = DK_DOUBLE;
4212 DirectiveKindMap[".align"] = DK_ALIGN;
4213 DirectiveKindMap[".align32"] = DK_ALIGN32;
4214 DirectiveKindMap[".balign"] = DK_BALIGN;
4215 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4216 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4217 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4218 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4219 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4220 DirectiveKindMap[".org"] = DK_ORG;
4221 DirectiveKindMap[".fill"] = DK_FILL;
4222 DirectiveKindMap[".zero"] = DK_ZERO;
4223 DirectiveKindMap[".extern"] = DK_EXTERN;
4224 DirectiveKindMap[".globl"] = DK_GLOBL;
4225 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004226 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4227 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4228 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4229 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4230 DirectiveKindMap[".reference"] = DK_REFERENCE;
4231 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4232 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4233 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4234 DirectiveKindMap[".comm"] = DK_COMM;
4235 DirectiveKindMap[".common"] = DK_COMMON;
4236 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4237 DirectiveKindMap[".abort"] = DK_ABORT;
4238 DirectiveKindMap[".include"] = DK_INCLUDE;
4239 DirectiveKindMap[".incbin"] = DK_INCBIN;
4240 DirectiveKindMap[".code16"] = DK_CODE16;
4241 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4242 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004243 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004244 DirectiveKindMap[".irp"] = DK_IRP;
4245 DirectiveKindMap[".irpc"] = DK_IRPC;
4246 DirectiveKindMap[".endr"] = DK_ENDR;
4247 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4248 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4249 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4250 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004251 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4252 DirectiveKindMap[".ifge"] = DK_IFGE;
4253 DirectiveKindMap[".ifgt"] = DK_IFGT;
4254 DirectiveKindMap[".ifle"] = DK_IFLE;
4255 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004256 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004257 DirectiveKindMap[".ifb"] = DK_IFB;
4258 DirectiveKindMap[".ifnb"] = DK_IFNB;
4259 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004260 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004261 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004262 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004263 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4264 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4265 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4266 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4267 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004268 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004269 DirectiveKindMap[".endif"] = DK_ENDIF;
4270 DirectiveKindMap[".skip"] = DK_SKIP;
4271 DirectiveKindMap[".space"] = DK_SPACE;
4272 DirectiveKindMap[".file"] = DK_FILE;
4273 DirectiveKindMap[".line"] = DK_LINE;
4274 DirectiveKindMap[".loc"] = DK_LOC;
4275 DirectiveKindMap[".stabs"] = DK_STABS;
4276 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4277 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4278 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4279 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4280 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4281 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4282 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4283 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4284 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4285 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4286 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4287 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4288 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4289 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4290 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4291 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4292 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4293 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4294 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4295 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4296 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004297 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004298 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4299 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4300 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004301 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004302 DirectiveKindMap[".endm"] = DK_ENDM;
4303 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4304 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004305 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004306 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004307 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004308}
4309
Jim Grosbach4b905842013-09-20 23:08:21 +00004310MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004311 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004312
Rafael Espindola34b9c512012-06-03 23:57:14 +00004313 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004314 for (;;) {
4315 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004316 if (getLexer().is(AsmToken::Eof)) {
4317 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004318 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004319 }
4320
Rafael Espindola34b9c512012-06-03 23:57:14 +00004321 if (Lexer.is(AsmToken::Identifier) &&
4322 (getTok().getIdentifier() == ".rept")) {
4323 ++NestLevel;
4324 }
4325
4326 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004327 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004328 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004329 EndToken = getTok();
4330 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004331 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4332 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004333 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004334 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004335 break;
4336 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004337 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004338 }
4339
Rafael Espindola34b9c512012-06-03 23:57:14 +00004340 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004341 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004342 }
4343
4344 const char *BodyStart = StartToken.getLoc().getPointer();
4345 const char *BodyEnd = EndToken.getLoc().getPointer();
4346 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4347
Rafael Espindola34b9c512012-06-03 23:57:14 +00004348 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004349 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004350 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004351}
4352
Jim Grosbach4b905842013-09-20 23:08:21 +00004353void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004354 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004355 OS << ".endr\n";
4356
Rafael Espindola3560ff22014-08-27 20:03:13 +00004357 std::unique_ptr<MemoryBuffer> Instantiation =
4358 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004359
Rafael Espindola34b9c512012-06-03 23:57:14 +00004360 // Create the macro instantiation object and add to the current macro
4361 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004362 MacroInstantiation *MI = new MacroInstantiation(
4363 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004364 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004365
Rafael Espindola34b9c512012-06-03 23:57:14 +00004366 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004367 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004368 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004369 Lex();
4370}
4371
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004372/// parseDirectiveRept
4373/// ::= .rep | .rept count
4374bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004375 const MCExpr *CountExpr;
4376 SMLoc CountLoc = getTok().getLoc();
4377 if (parseExpression(CountExpr))
4378 return true;
4379
Rafael Espindola34b9c512012-06-03 23:57:14 +00004380 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004381 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4382 eatToEndOfStatement();
4383 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4384 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004385
4386 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004387 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004388
4389 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004390 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004391
4392 // Eat the end of statement.
4393 Lex();
4394
4395 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004396 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004397 if (!M)
4398 return true;
4399
4400 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4401 // to hold the macro body with substitutions.
4402 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004403 raw_svector_ostream OS(Buf);
4404 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004405 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4406 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004407 return true;
4408 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004409 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004410
4411 return false;
4412}
4413
Jim Grosbach4b905842013-09-20 23:08:21 +00004414/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004415/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004416bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004417 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004418
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004419 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004420 return TokError("expected identifier in '.irp' directive");
4421
Rafael Espindola768b41c2012-06-15 14:02:34 +00004422 if (Lexer.isNot(AsmToken::Comma))
4423 return TokError("expected comma in '.irp' directive");
4424
4425 Lex();
4426
Eli Bendersky38274122013-01-14 23:22:36 +00004427 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004428 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004429 return true;
4430
4431 // Eat the end of statement.
4432 Lex();
4433
4434 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004435 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004436 if (!M)
4437 return true;
4438
4439 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4440 // to hold the macro body with substitutions.
4441 SmallString<256> Buf;
4442 raw_svector_ostream OS(Buf);
4443
Eli Bendersky38274122013-01-14 23:22:36 +00004444 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004445 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4446 // This is undocumented, but GAS seems to support it.
4447 if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004448 return true;
4449 }
4450
Jim Grosbach4b905842013-09-20 23:08:21 +00004451 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004452
4453 return false;
4454}
4455
Jim Grosbach4b905842013-09-20 23:08:21 +00004456/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004457/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004458bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004459 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004460
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004461 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004462 return TokError("expected identifier in '.irpc' directive");
4463
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004464 if (Lexer.isNot(AsmToken::Comma))
4465 return TokError("expected comma in '.irpc' directive");
4466
4467 Lex();
4468
Eli Bendersky38274122013-01-14 23:22:36 +00004469 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004470 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004471 return true;
4472
4473 if (A.size() != 1 || A.front().size() != 1)
4474 return TokError("unexpected token in '.irpc' directive");
4475
4476 // Eat the end of statement.
4477 Lex();
4478
4479 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004480 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004481 if (!M)
4482 return true;
4483
4484 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4485 // to hold the macro body with substitutions.
4486 SmallString<256> Buf;
4487 raw_svector_ostream OS(Buf);
4488
4489 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004490 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004491 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004492 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004493
Toma Tabacu217116e2015-04-27 10:50:29 +00004494 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4495 // This is undocumented, but GAS seems to support it.
4496 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004497 return true;
4498 }
4499
Jim Grosbach4b905842013-09-20 23:08:21 +00004500 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004501
4502 return false;
4503}
4504
Jim Grosbach4b905842013-09-20 23:08:21 +00004505bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004506 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004507 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004508
4509 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004510 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004511 assert(getLexer().is(AsmToken::EndOfStatement));
4512
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004514 return false;
4515}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004516
Jim Grosbach4b905842013-09-20 23:08:21 +00004517bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004518 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004519 const MCExpr *Value;
4520 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004521 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004522 return true;
4523 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4524 if (!MCE)
4525 return Error(ExprLoc, "unexpected expression in _emit");
4526 uint64_t IntValue = MCE->getValue();
4527 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4528 return Error(ExprLoc, "literal value out of range for directive");
4529
Chad Rosierc7f552c2013-02-12 21:33:51 +00004530 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4531 return false;
4532}
4533
Jim Grosbach4b905842013-09-20 23:08:21 +00004534bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004535 const MCExpr *Value;
4536 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004537 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004538 return true;
4539 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4540 if (!MCE)
4541 return Error(ExprLoc, "unexpected expression in align");
4542 uint64_t IntValue = MCE->getValue();
4543 if (!isPowerOf2_64(IntValue))
4544 return Error(ExprLoc, "literal value not a power of two greater then zero");
4545
Jim Grosbach4b905842013-09-20 23:08:21 +00004546 Info.AsmRewrites->push_back(
4547 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004548 return false;
4549}
4550
Chad Rosierf43fcf52013-02-13 21:27:17 +00004551// We are comparing pointers, but the pointers are relative to a single string.
4552// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004553static int rewritesSort(const AsmRewrite *AsmRewriteA,
4554 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004555 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4556 return -1;
4557 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4558 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004559
Chad Rosierfce4fab2013-04-08 17:43:47 +00004560 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4561 // rewrite to the same location. Make sure the SizeDirective rewrite is
4562 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4563 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004564 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4565 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004566 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004567
Jim Grosbach4b905842013-09-20 23:08:21 +00004568 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4569 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004570 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004571 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004572}
4573
Jim Grosbach4b905842013-09-20 23:08:21 +00004574bool AsmParser::parseMSInlineAsm(
4575 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4576 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4577 SmallVectorImpl<std::string> &Constraints,
4578 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4579 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004580 SmallVector<void *, 4> InputDecls;
4581 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004582 SmallVector<bool, 4> InputDeclsAddressOf;
4583 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004584 SmallVector<std::string, 4> InputConstraints;
4585 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004586 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004587
Benjamin Kramer1a136112013-02-15 20:37:21 +00004588 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004589
4590 // Prime the lexer.
4591 Lex();
4592
4593 // While we have input, parse each statement.
4594 unsigned InputIdx = 0;
4595 unsigned OutputIdx = 0;
4596 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004597 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004598 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004599 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004600
Chad Rosier149e8e02012-12-12 22:45:52 +00004601 if (Info.ParseError)
4602 return true;
4603
Benjamin Kramer1a136112013-02-15 20:37:21 +00004604 if (Info.Opcode == ~0U)
4605 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004606
Benjamin Kramer1a136112013-02-15 20:37:21 +00004607 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004608
Benjamin Kramer1a136112013-02-15 20:37:21 +00004609 // Build the list of clobbers, outputs and inputs.
4610 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004611 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004612
Benjamin Kramer1a136112013-02-15 20:37:21 +00004613 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004614 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004616
Benjamin Kramer1a136112013-02-15 20:37:21 +00004617 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004618 if (Operand.isReg() && !Operand.needAddressOf() &&
4619 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004620 unsigned NumDefs = Desc.getNumDefs();
4621 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004622 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4623 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004624 continue;
4625 }
4626
4627 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004628 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004629 if (SymName.empty())
4630 continue;
4631
David Blaikie960ea3f2014-06-08 16:18:35 +00004632 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004633 if (!OpDecl)
4634 continue;
4635
4636 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004637 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004638 if (isOutput) {
4639 ++InputIdx;
4640 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004641 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004642 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004643 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004644 } else {
4645 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004646 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4647 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004648 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004649 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004650 }
Reid Kleckneree088972013-12-10 18:27:32 +00004651
4652 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004653 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4654 Desc.getNumImplicitDefs());
4655 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004656 }
4657
4658 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004659 NumOutputs = OutputDecls.size();
4660 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004661
4662 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004663 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4664 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4665 ClobberRegs.end());
4666 Clobbers.assign(ClobberRegs.size(), std::string());
4667 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4668 raw_string_ostream OS(Clobbers[I]);
4669 IP->printRegName(OS, ClobberRegs[I]);
4670 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004671
4672 // Merge the various outputs and inputs. Output are expected first.
4673 if (NumOutputs || NumInputs) {
4674 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004675 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004676 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004677 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004678 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004679 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004680 }
4681 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004682 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004683 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004684 }
4685 }
4686
4687 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004688 std::string AsmStringIR;
4689 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004690 StringRef ASMString =
4691 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4692 const char *AsmStart = ASMString.begin();
4693 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004694 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004695 for (const AsmRewrite &AR : AsmStrRewrites) {
4696 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004697 if (Kind == AOK_Delete)
4698 continue;
4699
David Majnemer8114c1a2014-06-23 02:17:16 +00004700 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004701 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004702
Chad Rosier120eefd2013-03-19 17:32:17 +00004703 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004704 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004705 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004706
Chad Rosier37e755c2012-10-23 17:43:43 +00004707 // Skip the original expression.
4708 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004709 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004710 continue;
4711 }
4712
Chad Rosierff10ed12013-04-12 16:26:42 +00004713 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004714 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004715 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004716 default:
4717 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004718 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004719 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004720 break;
4721 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004722 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004723 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004724 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004725 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004726 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004727 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004728 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004729 break;
4730 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004731 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004732 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004733 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004734 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004735 default: break;
4736 case 8: OS << "byte ptr "; break;
4737 case 16: OS << "word ptr "; break;
4738 case 32: OS << "dword ptr "; break;
4739 case 64: OS << "qword ptr "; break;
4740 case 80: OS << "xword ptr "; break;
4741 case 128: OS << "xmmword ptr "; break;
4742 case 256: OS << "ymmword ptr "; break;
4743 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004744 break;
4745 case AOK_Emit:
4746 OS << ".byte";
4747 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004748 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004749 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004750 OS << ".align " << Val;
4751
4752 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004753 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004754 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4755 break;
4756 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004757 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004758 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004759 OS.flush();
4760 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004761 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004762 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004763 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004764 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004765
Chad Rosier8bce6642012-10-18 15:49:34 +00004766 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004767 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004768 }
4769
4770 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004771 if (AsmStart != AsmEnd)
4772 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004773
4774 AsmString = OS.str();
4775 return false;
4776}
4777
Daniel Dunbar01e36072010-07-17 02:26:10 +00004778/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004779MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4780 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004781 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004782}