blob: 1e805fdfcf158f89d355f55ca990e4dab05b1927 [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()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000633 MCSymbol *SectionStartSym = getContext().createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000634 getStreamer().EmitLabel(SectionStartSym);
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000635 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindolae0746792015-05-21 16:52:32 +0000636 bool InsertResult = getContext().addGenDwarfSection(Sec);
637 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000638 (void)InsertResult;
Rafael Espindolae0746792015-05-21 16:52:32 +0000639 Sec->setBeginSymbol(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000640 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
641 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000642 }
643
Chris Lattner73f36112009-07-02 21:53:43 +0000644 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000645 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000646 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000647 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000648 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000649
Daniel Dunbar43325c42010-09-09 22:42:56 +0000650 // We had an error, validate that one was emitted and recover by skipping to
651 // the next line.
652 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000653 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000654 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000655
656 if (TheCondState.TheCond != StartingCondState.TheCond ||
657 TheCondState.Ignore != StartingCondState.Ignore)
658 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000659
660 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000661 const auto &LineTables = getContext().getMCDwarfLineTables();
662 if (!LineTables.empty()) {
663 unsigned Index = 0;
664 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
665 if (File.Name.empty() && Index != 0)
666 TokError("unassigned file number: " + Twine(Index) +
667 " for .file directives");
668 ++Index;
669 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000670 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000671
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000672 // Check to see that all assembler local symbols were actually defined.
673 // Targets that don't do subsections via symbols may not want this, though,
674 // so conservatively exclude them. Only do this if we're finalizing, though,
675 // as otherwise we won't necessarilly have seen everything yet.
676 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
677 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
678 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000679 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000680 i != e; ++i) {
681 MCSymbol *Sym = i->getValue();
682 // Variable symbols may not be marked as defined, so check those
683 // explicitly. If we know it's a variable, we have a definition for
684 // the purposes of this check.
685 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
686 // FIXME: We would really like to refer back to where the symbol was
687 // first referenced for a source location. We need to add something
688 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000689 printMessage(
690 getLexer().getLoc(), SourceMgr::DK_Error,
691 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000692 }
693 }
694
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000695 // Finalize the output stream if there are no errors and if the client wants
696 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000697 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000698 Out.Finish();
699
Chris Lattner73f36112009-07-02 21:53:43 +0000700 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000701}
702
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000703void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000704 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000705 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000706 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 }
708}
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000712 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000714
Chris Lattnere5074c42009-06-22 01:29:09 +0000715 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000716 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000718}
719
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000720StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 const char *Start = getTok().getLoc().getPointer();
722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000724 Lex();
725
726 const char *End = getTok().getLoc().getPointer();
727 return StringRef(Start, End - Start);
728}
Chris Lattner78db3622009-06-22 05:51:26 +0000729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 const char *Start = getTok().getLoc().getPointer();
732
733 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000734 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 Lex();
736
737 const char *End = getTok().getLoc().getPointer();
738 return StringRef(Start, End - Start);
739}
740
Jim Grosbach4b905842013-09-20 23:08:21 +0000741/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742/// NOTE: This assumes the leading '(' has already been consumed.
743///
744/// parenexpr ::= expr)
745///
Jim Grosbach4b905842013-09-20 23:08:21 +0000746bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
747 if (parseExpression(Res))
748 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000749 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000751 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000752 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753 return false;
754}
Chris Lattner78db3622009-06-22 05:51:26 +0000755
Jim Grosbach4b905842013-09-20 23:08:21 +0000756/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757/// NOTE: This assumes the leading '[' has already been consumed.
758///
759/// bracketexpr ::= expr]
760///
Jim Grosbach4b905842013-09-20 23:08:21 +0000761bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
762 if (parseExpression(Res))
763 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 if (Lexer.isNot(AsmToken::RBrac))
765 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000766 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000767 Lex();
768 return false;
769}
770
Jim Grosbach4b905842013-09-20 23:08:21 +0000771/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= (parenexpr
773/// primaryexpr ::= symbol
774/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000775/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000777bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000778 SMLoc FirstTokenLoc = getLexer().getLoc();
779 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
780 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000781 default:
782 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000783 // If we have an error assume that we've already handled it.
784 case AsmToken::Error:
785 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000786 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000787 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000788 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000789 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000790 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000793 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000794 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000795 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000797 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000798 if (FirstTokenKind == AsmToken::Dollar) {
799 if (Lexer.getMAI().getDollarIsPC()) {
800 // This is a '$' reference, which references the current PC. Emit a
801 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000802 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000803 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000804 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
805 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 EndLoc = FirstTokenLoc;
807 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000808 }
809 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000811 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000812 // Parse symbol variant
813 std::pair<StringRef, StringRef> Split;
814 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000815 if (FirstTokenKind == AsmToken::String) {
816 if (Lexer.is(AsmToken::At)) {
817 Lexer.Lex(); // eat @
818 SMLoc AtLoc = getLexer().getLoc();
819 StringRef VName;
820 if (parseIdentifier(VName))
821 return Error(AtLoc, "expected symbol variant after '@'");
822
823 Split = std::make_pair(Identifier, VName);
824 }
825 } else {
826 Split = Identifier.split('@');
827 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000828 } else if (Lexer.is(AsmToken::LParen)) {
829 Lexer.Lex(); // eat (
830 StringRef VName;
831 parseIdentifier(VName);
832 if (Lexer.isNot(AsmToken::RParen)) {
833 return Error(Lexer.getTok().getLoc(),
834 "unexpected token in variant, expected ')'");
835 }
836 Lexer.Lex(); // eat )
837 Split = std::make_pair(Identifier, VName);
838 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000839
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000840 EndLoc = SMLoc::getFromPointer(Identifier.end());
841
Daniel Dunbard20cda02009-10-16 01:34:54 +0000842 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000843 StringRef SymbolName = Identifier;
844 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000845
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000846 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000847 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000848 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000849 if (Variant != MCSymbolRefExpr::VK_Invalid) {
850 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000851 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000852 Variant = MCSymbolRefExpr::VK_None;
853 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000854 return Error(SMLoc::getFromPointer(Split.second.begin()),
855 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000856 }
857 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000858
Jim Grosbach6f482002015-05-18 18:43:14 +0000859 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000860
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 // If this is an absolute variable reference, substitute it now to preserve
862 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000863 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000864 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000865 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000866
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000867 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000868 return false;
869 }
870
871 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000872 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000873 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000874 }
David Woodhousef42a6662014-02-01 16:20:54 +0000875 case AsmToken::BigNum:
876 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000877 case AsmToken::Integer: {
878 SMLoc Loc = getTok().getLoc();
879 int64_t IntVal = getTok().getIntVal();
880 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000881 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000882 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000883 // Look for 'b' or 'f' following an Integer as a directional label
884 if (Lexer.getKind() == AsmToken::Identifier) {
885 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000886 // Lookup the symbol variant if used.
887 std::pair<StringRef, StringRef> Split = IDVal.split('@');
888 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
889 if (Split.first.size() != IDVal.size()) {
890 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000891 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000892 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000893 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000895 if (IDVal == "f" || IDVal == "b") {
896 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000897 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000898 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000899 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000900 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000901 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 Lex(); // Eat identifier.
903 }
904 }
Chris Lattner78db3622009-06-22 05:51:26 +0000905 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000906 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000907 case AsmToken::Real: {
908 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000909 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000910 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000911 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000912 Lex(); // Eat token.
913 return false;
914 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000915 case AsmToken::Dot: {
916 // This is a '.' reference, which references the current PC. Emit a
917 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000918 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000919 Out.EmitLabel(Sym);
920 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000921 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000922 Lex(); // Eat identifier.
923 return false;
924 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000925 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000926 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000927 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000928 case AsmToken::LBrac:
929 if (!PlatformParser->HasBracketExpressions())
930 return TokError("brackets expression not supported on this target");
931 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000933 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000934 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000935 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000937 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000939 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000940 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000941 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000943 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000945 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000946 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000947 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000948 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000949 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000950 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000951 }
952}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000953
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000954bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000955 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000956 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000957}
958
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000960AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000962 // Ask the target implementation about this expression first.
963 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
964 if (NewE)
965 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 // Recurse over the given expression, rebuilding it to apply the given variant
967 // if there is exactly one symbol.
968 switch (E->getKind()) {
969 case MCExpr::Target:
970 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000971 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000972
973 case MCExpr::SymbolRef: {
974 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
975
976 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000977 TokError("invalid variant on expression '" + getTok().getIdentifier() +
978 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979 return E;
980 }
981
982 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
983 }
984
985 case MCExpr::Unary: {
986 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000987 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000989 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
991 }
992
993 case MCExpr::Binary: {
994 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000995 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
996 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000997
998 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000999 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001000
Jim Grosbach4b905842013-09-20 23:08:21 +00001001 if (!LHS)
1002 LHS = BE->getLHS();
1003 if (!RHS)
1004 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001005
1006 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1007 }
1008 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001009
Craig Toppera2886c22012-02-07 05:05:23 +00001010 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001011}
1012
Jim Grosbach4b905842013-09-20 23:08:21 +00001013/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001014///
Jim Grosbachbd164242011-08-20 16:24:13 +00001015/// expr ::= expr &&,|| expr -> lowest.
1016/// expr ::= expr |,^,&,! expr
1017/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1018/// expr ::= expr <<,>> expr
1019/// expr ::= expr +,- expr
1020/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001021/// expr ::= primaryexpr
1022///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001023bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001024 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001025 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001026 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001027 return true;
1028
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 // As a special case, we support 'a op b @ modifier' by rewriting the
1030 // expression to include the modifier. This is inefficient, but in general we
1031 // expect users to use 'a@modifier op b'.
1032 if (Lexer.getKind() == AsmToken::At) {
1033 Lex();
1034
1035 if (Lexer.isNot(AsmToken::Identifier))
1036 return TokError("unexpected symbol modifier following '@'");
1037
1038 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001039 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 if (Variant == MCSymbolRefExpr::VK_Invalid)
1041 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1042
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 if (!ModifiedRes) {
1045 return TokError("invalid modifier '" + getTok().getIdentifier() +
1046 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001047 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001048
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 Res = ModifiedRes;
1050 Lex();
1051 }
1052
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001053 // Try to constant fold it up front, if possible.
1054 int64_t Value;
1055 if (Res->EvaluateAsAbsolute(Value))
1056 Res = MCConstantExpr::Create(Value, getContext());
1057
1058 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001059}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001060
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001061bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001062 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001063 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001064}
1065
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001066bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001067 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001068
Daniel Dunbar75630b32009-06-30 02:10:03 +00001069 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001070 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071 return true;
1072
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001073 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001074 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075
1076 return false;
1077}
1078
Ahmed Bougacha457852f2015-04-28 00:17:39 +00001079unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1080 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001081 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001082 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001083 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001084
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001088 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001091 return 1;
1092
Jim Grosbach4b905842013-09-20 23:08:21 +00001093 // Low Precedence: |, &, ^
1094 //
1095 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001096 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001097 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001099 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001100 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001101 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001102 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001103 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001104 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001105
Jim Grosbach4b905842013-09-20 23:08:21 +00001106 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001107 case AsmToken::EqualEqual:
1108 Kind = MCBinaryExpr::EQ;
1109 return 3;
1110 case AsmToken::ExclaimEqual:
1111 case AsmToken::LessGreater:
1112 Kind = MCBinaryExpr::NE;
1113 return 3;
1114 case AsmToken::Less:
1115 Kind = MCBinaryExpr::LT;
1116 return 3;
1117 case AsmToken::LessEqual:
1118 Kind = MCBinaryExpr::LTE;
1119 return 3;
1120 case AsmToken::Greater:
1121 Kind = MCBinaryExpr::GT;
1122 return 3;
1123 case AsmToken::GreaterEqual:
1124 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001125 return 3;
1126
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 case AsmToken::LessLess:
1129 Kind = MCBinaryExpr::Shl;
1130 return 4;
1131 case AsmToken::GreaterGreater:
Ahmed Bougacha177c1482015-04-28 00:21:32 +00001132 Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 4;
1134
Jim Grosbach4b905842013-09-20 23:08:21 +00001135 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001136 case AsmToken::Plus:
1137 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001138 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001139 case AsmToken::Minus:
1140 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001141 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001142
Jim Grosbach4b905842013-09-20 23:08:21 +00001143 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001144 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001146 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001147 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001149 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001150 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001151 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001152 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153 }
1154}
1155
Jim Grosbach4b905842013-09-20 23:08:21 +00001156/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001157/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001158bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001159 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001161 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001162 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001163
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 // If the next token is lower precedence than we are allowed to eat, return
1165 // successfully with what we ate already.
1166 if (TokPrec < Precedence)
1167 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001168
Sean Callanan686ed8d2010-01-19 20:22:31 +00001169 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001170
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001171 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001172 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001173 if (parsePrimaryExpr(RHS, EndLoc))
1174 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001175
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001176 // If BinOp binds less tightly with RHS than the operator after RHS, let
1177 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001178 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001179 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001180 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1181 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001182
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001183 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001184 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001185 }
1186}
1187
Chris Lattner36e02122009-06-21 20:54:55 +00001188/// ParseStatement:
1189/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001190/// ::= Label* Directive ...Operands... EndOfStatement
1191/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001192bool AsmParser::parseStatement(ParseStatementInfo &Info,
1193 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001195 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001196 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001197 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001198 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001199
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001201 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001202 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001203 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001205 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001206 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001207 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001208
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001209 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001210 if (Lexer.is(AsmToken::Integer)) {
1211 LocalLabelVal = getTok().getIntVal();
1212 if (LocalLabelVal < 0) {
1213 if (!TheCondState.Ignore)
1214 return TokError("unexpected token at start of statement");
1215 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001216 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001217 IDVal = getTok().getString();
1218 Lex(); // Consume the integer token to be used as an identifier token.
1219 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001220 if (!TheCondState.Ignore)
1221 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001222 }
1223 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001224 } else if (Lexer.is(AsmToken::Dot)) {
1225 // Treat '.' as a valid identifier in this context.
1226 Lex();
1227 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001228 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001229 if (!TheCondState.Ignore)
1230 return TokError("unexpected token at start of statement");
1231 IDVal = "";
1232 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001233
Chris Lattner926885c2010-04-17 18:14:27 +00001234 // Handle conditional assembly here before checking for skipping. We
1235 // have to do this so that .endif isn't skipped in a ".if 0" block for
1236 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001237 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001238 DirectiveKindMap.find(IDVal);
1239 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1240 ? DK_NO_DIRECTIVE
1241 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001242 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001243 default:
1244 break;
1245 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001246 case DK_IFEQ:
1247 case DK_IFGE:
1248 case DK_IFGT:
1249 case DK_IFLE:
1250 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001251 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001252 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001253 case DK_IFB:
1254 return parseDirectiveIfb(IDLoc, true);
1255 case DK_IFNB:
1256 return parseDirectiveIfb(IDLoc, false);
1257 case DK_IFC:
1258 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001259 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001260 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001261 case DK_IFNC:
1262 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001263 case DK_IFNES:
1264 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001265 case DK_IFDEF:
1266 return parseDirectiveIfdef(IDLoc, true);
1267 case DK_IFNDEF:
1268 case DK_IFNOTDEF:
1269 return parseDirectiveIfdef(IDLoc, false);
1270 case DK_ELSEIF:
1271 return parseDirectiveElseIf(IDLoc);
1272 case DK_ELSE:
1273 return parseDirectiveElse(IDLoc);
1274 case DK_ENDIF:
1275 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001276 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001277
Eli Bendersky88024712013-01-16 19:32:36 +00001278 // Ignore the statement if in the middle of inactive conditional
1279 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001280 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001281 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001282 return false;
1283 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001284
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001285 // FIXME: Recurse on local labels?
1286
1287 // See what kind of statement we have.
1288 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001289 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001290 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001291
Chris Lattner36e02122009-06-21 20:54:55 +00001292 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001293 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001294
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001295 // Diagnose attempt to use '.' as a label.
1296 if (IDVal == ".")
1297 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1298
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001299 // Diagnose attempt to use a variable as a label.
1300 //
1301 // FIXME: Diagnostics. Note the location of the definition as a label.
1302 // FIXME: This doesn't diagnose assignment to a symbol which has been
1303 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001304 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001305 if (LocalLabelVal == -1) {
1306 if (ParsingInlineAsm && SI) {
1307 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1308 assert(RewrittenLabel.size() && "We should have an internal name here.");
1309 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1310 IDVal.size(), RewrittenLabel));
1311 IDVal = RewrittenLabel;
1312 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001313 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001314 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001315 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001316
1317 Sym->redefineIfPossible();
1318
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001319 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001320 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001321
Daniel Dunbare73b2672009-08-26 22:13:22 +00001322 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001323 if (!ParsingInlineAsm)
1324 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001325
Kevin Enderbye7739d42011-12-09 18:09:40 +00001326 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001327 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001328 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001329 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1330 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001331
Tim Northover1744d0a2013-10-25 12:49:50 +00001332 getTargetParser().onLabelParsed(Sym);
1333
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001334 // Consume any end of statement token, if present, to avoid spurious
1335 // AddBlankLine calls().
1336 if (Lexer.is(AsmToken::EndOfStatement)) {
1337 Lex();
1338 if (Lexer.is(AsmToken::Eof))
1339 return false;
1340 }
1341
Eli Friedman0f4871d2012-10-22 23:58:19 +00001342 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001343 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001344
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001345 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001346 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001347 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001348
Jim Grosbach4b905842013-09-20 23:08:21 +00001349 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001350
1351 default: // Normal instruction or directive.
1352 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001353 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001354
1355 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001356 if (areMacrosEnabled())
1357 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1358 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001359 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001360
Michael J. Spencer530ce852010-10-09 11:00:50 +00001361 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001362
Eli Bendersky17233942013-01-15 22:59:42 +00001363 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001364 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001366 //
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // 1. The target-specific assembly parser. Some directives are target
1368 // specific or may potentially behave differently on certain targets.
1369 // 2. Asm parser extensions. For example, platform-specific parsers
1370 // (like the ELF parser) register themselves as extensions.
1371 // 3. The generic directive parser implemented by this class. These are
1372 // all the directives that behave in a target and platform independent
1373 // manner, or at least have a default behavior that's shared between
1374 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001375
Eli Bendersky17233942013-01-15 22:59:42 +00001376 // First query the target-specific parser. It will return 'true' if it
1377 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001378 if (!getTargetParser().ParseDirective(ID))
1379 return false;
1380
Alp Tokercb402912014-01-24 17:20:08 +00001381 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001382 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001383 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1384 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001385 if (Handler.first)
1386 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1387
1388 // Finally, if no one else is interested in this directive, it must be
1389 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001390 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001391 default:
1392 break;
1393 case DK_SET:
1394 case DK_EQU:
1395 return parseDirectiveSet(IDVal, true);
1396 case DK_EQUIV:
1397 return parseDirectiveSet(IDVal, false);
1398 case DK_ASCII:
1399 return parseDirectiveAscii(IDVal, false);
1400 case DK_ASCIZ:
1401 case DK_STRING:
1402 return parseDirectiveAscii(IDVal, true);
1403 case DK_BYTE:
1404 return parseDirectiveValue(1);
1405 case DK_SHORT:
1406 case DK_VALUE:
1407 case DK_2BYTE:
1408 return parseDirectiveValue(2);
1409 case DK_LONG:
1410 case DK_INT:
1411 case DK_4BYTE:
1412 return parseDirectiveValue(4);
1413 case DK_QUAD:
1414 case DK_8BYTE:
1415 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001416 case DK_OCTA:
1417 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001418 case DK_SINGLE:
1419 case DK_FLOAT:
1420 return parseDirectiveRealValue(APFloat::IEEEsingle);
1421 case DK_DOUBLE:
1422 return parseDirectiveRealValue(APFloat::IEEEdouble);
1423 case DK_ALIGN: {
1424 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1425 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1426 }
1427 case DK_ALIGN32: {
1428 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1429 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1430 }
1431 case DK_BALIGN:
1432 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1433 case DK_BALIGNW:
1434 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1435 case DK_BALIGNL:
1436 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1437 case DK_P2ALIGN:
1438 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1439 case DK_P2ALIGNW:
1440 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1441 case DK_P2ALIGNL:
1442 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1443 case DK_ORG:
1444 return parseDirectiveOrg();
1445 case DK_FILL:
1446 return parseDirectiveFill();
1447 case DK_ZERO:
1448 return parseDirectiveZero();
1449 case DK_EXTERN:
1450 eatToEndOfStatement(); // .extern is the default, ignore it.
1451 return false;
1452 case DK_GLOBL:
1453 case DK_GLOBAL:
1454 return parseDirectiveSymbolAttribute(MCSA_Global);
1455 case DK_LAZY_REFERENCE:
1456 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1457 case DK_NO_DEAD_STRIP:
1458 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1459 case DK_SYMBOL_RESOLVER:
1460 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1461 case DK_PRIVATE_EXTERN:
1462 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1463 case DK_REFERENCE:
1464 return parseDirectiveSymbolAttribute(MCSA_Reference);
1465 case DK_WEAK_DEFINITION:
1466 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1467 case DK_WEAK_REFERENCE:
1468 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1469 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1470 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1471 case DK_COMM:
1472 case DK_COMMON:
1473 return parseDirectiveComm(/*IsLocal=*/false);
1474 case DK_LCOMM:
1475 return parseDirectiveComm(/*IsLocal=*/true);
1476 case DK_ABORT:
1477 return parseDirectiveAbort();
1478 case DK_INCLUDE:
1479 return parseDirectiveInclude();
1480 case DK_INCBIN:
1481 return parseDirectiveIncbin();
1482 case DK_CODE16:
1483 case DK_CODE16GCC:
1484 return TokError(Twine(IDVal) + " not supported yet");
1485 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001486 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001487 case DK_IRP:
1488 return parseDirectiveIrp(IDLoc);
1489 case DK_IRPC:
1490 return parseDirectiveIrpc(IDLoc);
1491 case DK_ENDR:
1492 return parseDirectiveEndr(IDLoc);
1493 case DK_BUNDLE_ALIGN_MODE:
1494 return parseDirectiveBundleAlignMode();
1495 case DK_BUNDLE_LOCK:
1496 return parseDirectiveBundleLock();
1497 case DK_BUNDLE_UNLOCK:
1498 return parseDirectiveBundleUnlock();
1499 case DK_SLEB128:
1500 return parseDirectiveLEB128(true);
1501 case DK_ULEB128:
1502 return parseDirectiveLEB128(false);
1503 case DK_SPACE:
1504 case DK_SKIP:
1505 return parseDirectiveSpace(IDVal);
1506 case DK_FILE:
1507 return parseDirectiveFile(IDLoc);
1508 case DK_LINE:
1509 return parseDirectiveLine();
1510 case DK_LOC:
1511 return parseDirectiveLoc();
1512 case DK_STABS:
1513 return parseDirectiveStabs();
1514 case DK_CFI_SECTIONS:
1515 return parseDirectiveCFISections();
1516 case DK_CFI_STARTPROC:
1517 return parseDirectiveCFIStartProc();
1518 case DK_CFI_ENDPROC:
1519 return parseDirectiveCFIEndProc();
1520 case DK_CFI_DEF_CFA:
1521 return parseDirectiveCFIDefCfa(IDLoc);
1522 case DK_CFI_DEF_CFA_OFFSET:
1523 return parseDirectiveCFIDefCfaOffset();
1524 case DK_CFI_ADJUST_CFA_OFFSET:
1525 return parseDirectiveCFIAdjustCfaOffset();
1526 case DK_CFI_DEF_CFA_REGISTER:
1527 return parseDirectiveCFIDefCfaRegister(IDLoc);
1528 case DK_CFI_OFFSET:
1529 return parseDirectiveCFIOffset(IDLoc);
1530 case DK_CFI_REL_OFFSET:
1531 return parseDirectiveCFIRelOffset(IDLoc);
1532 case DK_CFI_PERSONALITY:
1533 return parseDirectiveCFIPersonalityOrLsda(true);
1534 case DK_CFI_LSDA:
1535 return parseDirectiveCFIPersonalityOrLsda(false);
1536 case DK_CFI_REMEMBER_STATE:
1537 return parseDirectiveCFIRememberState();
1538 case DK_CFI_RESTORE_STATE:
1539 return parseDirectiveCFIRestoreState();
1540 case DK_CFI_SAME_VALUE:
1541 return parseDirectiveCFISameValue(IDLoc);
1542 case DK_CFI_RESTORE:
1543 return parseDirectiveCFIRestore(IDLoc);
1544 case DK_CFI_ESCAPE:
1545 return parseDirectiveCFIEscape();
1546 case DK_CFI_SIGNAL_FRAME:
1547 return parseDirectiveCFISignalFrame();
1548 case DK_CFI_UNDEFINED:
1549 return parseDirectiveCFIUndefined(IDLoc);
1550 case DK_CFI_REGISTER:
1551 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001552 case DK_CFI_WINDOW_SAVE:
1553 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001554 case DK_MACROS_ON:
1555 case DK_MACROS_OFF:
1556 return parseDirectiveMacrosOnOff(IDVal);
1557 case DK_MACRO:
1558 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001559 case DK_EXITM:
1560 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001561 case DK_ENDM:
1562 case DK_ENDMACRO:
1563 return parseDirectiveEndMacro(IDVal);
1564 case DK_PURGEM:
1565 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001566 case DK_END:
1567 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001568 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001569 return parseDirectiveError(IDLoc, false);
1570 case DK_ERROR:
1571 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001572 case DK_WARNING:
1573 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001574 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001575
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001576 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001577 }
Chris Lattner36e02122009-06-21 20:54:55 +00001578
Chad Rosierc7f552c2013-02-12 21:33:51 +00001579 // __asm _emit or __asm __emit
1580 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1581 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001582 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001583
1584 // __asm align
1585 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001587
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001588 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001589
Chris Lattner7cbfa442010-05-19 23:34:33 +00001590 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001591 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001592 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001593 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001594 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001595 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001596
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001597 // Dump the parsed representation, if requested.
1598 if (getShowParsedOperands()) {
1599 SmallString<256> Str;
1600 raw_svector_ostream OS(Str);
1601 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001602 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001603 if (i != 0)
1604 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001605 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001606 }
1607 OS << "]";
1608
Jim Grosbach4b905842013-09-20 23:08:21 +00001609 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001610 }
1611
Oliver Stannard8b273082014-06-19 15:52:37 +00001612 // If we are generating dwarf for the current section then generate a .loc
1613 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001614 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001615 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001616 getStreamer().getCurrentSection().first)) {
1617 unsigned Line;
1618 if (ActiveMacros.empty())
1619 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1620 else
1621 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1622 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001623
Eli Bendersky88024712013-01-16 19:32:36 +00001624 // If we previously parsed a cpp hash file line comment then make sure the
1625 // current Dwarf File is for the CppHashFilename if not then emit the
1626 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001627 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001628 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1629 0, StringRef(), CppHashFilename);
1630 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001631
Jim Grosbach4b905842013-09-20 23:08:21 +00001632 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1633 // cache with the different Loc from the call above we save the last
1634 // info we queried here with SrcMgr.FindLineNumber().
1635 unsigned CppHashLocLineNo;
1636 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1637 CppHashLocLineNo = LastQueryLine;
1638 else {
1639 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1640 LastQueryLine = CppHashLocLineNo;
1641 LastQueryIDLoc = CppHashLoc;
1642 LastQueryBuffer = CppHashBuf;
1643 }
1644 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001645 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001646
Jim Grosbach4b905842013-09-20 23:08:21 +00001647 getStreamer().EmitDwarfLocDirective(
1648 getContext().getGenDwarfFileNumber(), Line, 0,
1649 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1650 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001651 }
1652
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001653 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001654 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001655 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001656 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1657 Info.ParsedOperands, Out,
1658 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001659 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001660
Chris Lattnera2a9d162010-09-11 16:18:25 +00001661 // Don't skip the rest of the line, the instruction parser is responsible for
1662 // that.
1663 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001664}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001665
Jim Grosbach4b905842013-09-20 23:08:21 +00001666/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001667/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001668void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001669 if (!Lexer.is(AsmToken::EndOfStatement))
1670 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 // Eat EOL.
1672 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001673}
1674
Jim Grosbach4b905842013-09-20 23:08:21 +00001675/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001676/// ::= # number "filename"
1677/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001678bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001679 Lex(); // Eat the hash token.
1680
1681 if (getLexer().isNot(AsmToken::Integer)) {
1682 // Consume the line since in cases it is not a well-formed line directive,
1683 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001684 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001685 return false;
1686 }
1687
1688 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001689 Lex();
1690
1691 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001692 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001693 return false;
1694 }
1695
1696 StringRef Filename = getTok().getString();
1697 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001698 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001699
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1701 CppHashLoc = L;
1702 CppHashFilename = Filename;
1703 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001704 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001705
1706 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001708 return false;
1709}
1710
Jim Grosbach4b905842013-09-20 23:08:21 +00001711/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712/// for the Filename and LineNo if any in the diagnostic.
1713void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715 raw_ostream &OS = errs();
1716
1717 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1718 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001719 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1720 unsigned CppHashBuf =
1721 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001722
Jim Grosbach4b905842013-09-20 23:08:21 +00001723 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001725 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1726 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1727 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001728 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1729 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001730 }
1731
Eric Christophera7c32732012-12-18 00:30:54 +00001732 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001733 // manager changed or buffer changed (like in a nested include) then just
1734 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001735 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001736 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001737 if (Parser->SavedDiagHandler)
1738 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1739 else
Craig Topper353eda42014-04-24 06:44:33 +00001740 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741 return;
1742 }
1743
Eric Christophera7c32732012-12-18 00:30:54 +00001744 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001745 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1746 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001747 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001748
1749 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1750 int CppHashLocLineNo =
1751 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001752 int LineNo =
1753 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001754
Jim Grosbach4b905842013-09-20 23:08:21 +00001755 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1756 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001757 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001758
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001759 if (Parser->SavedDiagHandler)
1760 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1761 else
Craig Topper353eda42014-04-24 06:44:33 +00001762 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001763}
1764
Rafael Espindola2c064482012-08-21 18:29:30 +00001765// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1766// difference being that that function accepts '@' as part of identifiers and
1767// we can't do that. AsmLexer.cpp should probably be changed to handle
1768// '@' as a special case when needed.
1769static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001770 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1771 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001772}
1773
Rafael Espindola34b9c512012-06-03 23:57:14 +00001774bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001775 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001776 ArrayRef<MCAsmMacroArgument> A,
1777 bool EnableAtPseudoVariable, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001778 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001779 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001780 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001782
Preston Gurd05500642012-09-19 20:36:12 +00001783 // A macro without parameters is handled differently on Darwin:
1784 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001785 while (!Body.empty()) {
1786 // Scan for the next substitution.
1787 std::size_t End = Body.size(), Pos = 0;
1788 for (; Pos != End; ++Pos) {
1789 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001790 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001791 // This macro has no parameters, look for $0, $1, etc.
1792 if (Body[Pos] != '$' || Pos + 1 == End)
1793 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001794
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001796 if (Next == '$' || Next == 'n' ||
1797 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001798 break;
1799 } else {
1800 // This macro has parameters, look for \foo, \bar, etc.
1801 if (Body[Pos] == '\\' && Pos + 1 != End)
1802 break;
1803 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001804 }
1805
1806 // Add the prefix.
1807 OS << Body.slice(0, Pos);
1808
1809 // Check if we reached the end.
1810 if (Pos == End)
1811 break;
1812
Benjamin Kramer513e7442014-02-20 13:36:32 +00001813 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001814 switch (Body[Pos + 1]) {
1815 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816 case '$':
1817 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001818 break;
1819
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821 case 'n':
1822 OS << A.size();
1823 break;
1824
Jim Grosbach4b905842013-09-20 23:08:21 +00001825 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 default: {
1827 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001828 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 if (Index >= A.size())
1830 break;
1831
1832 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001833 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001834 ie = A[Index].end();
1835 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001836 OS << it->getString();
1837 break;
1838 }
1839 }
1840 Pos += 2;
1841 } else {
1842 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001843
1844 // Check for the \@ pseudo-variable.
1845 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001846 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001847 else
1848 while (isIdentifierChar(Body[I]) && I + 1 != End)
1849 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001850
Jim Grosbach4b905842013-09-20 23:08:21 +00001851 const char *Begin = Body.data() + Pos + 1;
1852 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001853 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001854
Toma Tabacu217116e2015-04-27 10:50:29 +00001855 if (Argument == "@") {
1856 OS << NumOfMacroInstantiations;
1857 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001858 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001859 for (; Index < NParameters; ++Index)
1860 if (Parameters[Index].Name == Argument)
1861 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001862
Toma Tabacu217116e2015-04-27 10:50:29 +00001863 if (Index == NParameters) {
1864 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1865 Pos += 3;
1866 else {
1867 OS << '\\' << Argument;
1868 Pos = I;
1869 }
1870 } else {
1871 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1872 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1873 ie = A[Index].end();
1874 it != ie; ++it)
1875 // We expect no quotes around the string's contents when
1876 // parsing for varargs.
1877 if (it->getKind() != AsmToken::String || VarargParameter)
1878 OS << it->getString();
1879 else
1880 OS << it->getStringContents();
1881
1882 Pos += 1 + Argument.size();
1883 }
Preston Gurd05500642012-09-19 20:36:12 +00001884 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001885 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001886 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001887 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001888 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001889
Rafael Espindola1134ab232011-06-05 02:43:45 +00001890 return false;
1891}
Daniel Dunbar43235712010-07-18 18:54:11 +00001892
Nico Weber2a8f9222014-07-24 16:29:04 +00001893MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001894 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001895 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001896 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001897
Jim Grosbach4b905842013-09-20 23:08:21 +00001898static bool isOperator(AsmToken::TokenKind kind) {
1899 switch (kind) {
1900 default:
1901 return false;
1902 case AsmToken::Plus:
1903 case AsmToken::Minus:
1904 case AsmToken::Tilde:
1905 case AsmToken::Slash:
1906 case AsmToken::Star:
1907 case AsmToken::Dot:
1908 case AsmToken::Equal:
1909 case AsmToken::EqualEqual:
1910 case AsmToken::Pipe:
1911 case AsmToken::PipePipe:
1912 case AsmToken::Caret:
1913 case AsmToken::Amp:
1914 case AsmToken::AmpAmp:
1915 case AsmToken::Exclaim:
1916 case AsmToken::ExclaimEqual:
1917 case AsmToken::Percent:
1918 case AsmToken::Less:
1919 case AsmToken::LessEqual:
1920 case AsmToken::LessLess:
1921 case AsmToken::LessGreater:
1922 case AsmToken::Greater:
1923 case AsmToken::GreaterEqual:
1924 case AsmToken::GreaterGreater:
1925 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001926 }
1927}
1928
David Majnemer16252452014-01-29 00:07:39 +00001929namespace {
1930class AsmLexerSkipSpaceRAII {
1931public:
1932 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1933 Lexer.setSkipSpace(SkipSpace);
1934 }
1935
1936 ~AsmLexerSkipSpaceRAII() {
1937 Lexer.setSkipSpace(true);
1938 }
1939
1940private:
1941 AsmLexer &Lexer;
1942};
1943}
1944
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001945bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1946
1947 if (Vararg) {
1948 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1949 StringRef Str = parseStringToEndOfStatement();
1950 MA.push_back(AsmToken(AsmToken::String, Str));
1951 }
1952 return false;
1953 }
1954
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001956 unsigned AddTokens = 0;
1957
David Majnemer16252452014-01-29 00:07:39 +00001958 // Darwin doesn't use spaces to delmit arguments.
1959 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960
1961 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001962 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001964
David Majnemer91fc4c22014-01-29 18:57:46 +00001965 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001966 break;
Preston Gurd05500642012-09-19 20:36:12 +00001967
1968 if (Lexer.is(AsmToken::Space)) {
1969 Lex(); // Eat spaces
1970
1971 // Spaces can delimit parameters, but could also be part an expression.
1972 // If the token after a space is an operator, add the token and the next
1973 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001974 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001975 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001976 // Check to see whether the token is used as an operator,
1977 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001978 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001979 if (*NextChar == ' ')
1980 AddTokens = 2;
1981 }
1982
1983 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001984 break;
1985 }
1986 }
1987 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001988
Jim Grosbach4b905842013-09-20 23:08:21 +00001989 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990 // to be able to fill in the remaining default parameter values
1991 if (Lexer.is(AsmToken::EndOfStatement))
1992 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993
1994 // Adjust the current parentheses level.
1995 if (Lexer.is(AsmToken::LParen))
1996 ++ParenLevel;
1997 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1998 --ParenLevel;
1999
2000 // Append the token to the current argument list.
2001 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002002 if (AddTokens)
2003 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002004 Lex();
2005 }
Preston Gurd05500642012-09-19 20:36:12 +00002006
Rafael Espindola768b41c2012-06-15 14:02:34 +00002007 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002008 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002009 return false;
2010}
2011
2012// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002013bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002014 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002015 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002016 bool NamedParametersFound = false;
2017 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002018
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002019 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002020 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002021
Rafael Espindola768b41c2012-06-15 14:02:34 +00002022 // Parse two kinds of macro invocations:
2023 // - macros defined without any parameters accept an arbitrary number of them
2024 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002025 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002026 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2027 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002028 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002029 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002030
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002032 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002033 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002034 eatToEndOfStatement();
2035 return true;
2036 }
2037
2038 if (!Lexer.is(AsmToken::Equal)) {
2039 TokError("expected '=' after formal parameter identifier");
2040 eatToEndOfStatement();
2041 return true;
2042 }
2043 Lex();
2044
2045 NamedParametersFound = true;
2046 }
2047
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002048 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002049 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002050 eatToEndOfStatement();
2051 return true;
2052 }
2053
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002054 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2055 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002056 return true;
2057
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002058 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002059 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002060 unsigned FAI = 0;
2061 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002062 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002063 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002064
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002065 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002066 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002067 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002068 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002069 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002070 return true;
2071 }
2072 PI = FAI;
2073 }
2074
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002075 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002076 if (A.size() <= PI)
2077 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002078 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002079
2080 if (FALocs.size() <= PI)
2081 FALocs.resize(PI + 1);
2082
2083 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002084 }
Jim Grosbach206661622012-07-30 22:44:17 +00002085
Preston Gurd242ed3152012-09-19 20:29:04 +00002086 // At the end of the statement, fill in remaining arguments that have
2087 // default values. If there aren't any, then the next argument is
2088 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002089 if (Lexer.is(AsmToken::EndOfStatement)) {
2090 bool Failure = false;
2091 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2092 if (A[FAI].empty()) {
2093 if (M->Parameters[FAI].Required) {
2094 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2095 "missing value for required parameter "
2096 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2097 Failure = true;
2098 }
2099
2100 if (!M->Parameters[FAI].Value.empty())
2101 A[FAI] = M->Parameters[FAI].Value;
2102 }
2103 }
2104 return Failure;
2105 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002106
2107 if (Lexer.is(AsmToken::Comma))
2108 Lex();
2109 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002110
2111 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002112}
2113
Jim Grosbach4b905842013-09-20 23:08:21 +00002114const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002115 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2116 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002117}
2118
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002119void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2120 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002121}
2122
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002123void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002124
Jim Grosbach4b905842013-09-20 23:08:21 +00002125bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002126 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2127 // this, although we should protect against infinite loops.
2128 if (ActiveMacros.size() == 20)
2129 return TokError("macros cannot be nested more than 20 levels deep");
2130
Eli Bendersky38274122013-01-14 23:22:36 +00002131 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002132 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002133 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002134
Rafael Espindola1134ab232011-06-05 02:43:45 +00002135 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2136 // to hold the macro body with substitutions.
2137 SmallString<256> Buf;
2138 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002139 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002140
Toma Tabacu217116e2015-04-27 10:50:29 +00002141 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002142 return true;
2143
Eli Bendersky38274122013-01-14 23:22:36 +00002144 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002145 // instantiation.
2146 OS << ".endmacro\n";
2147
Rafael Espindola3560ff22014-08-27 20:03:13 +00002148 std::unique_ptr<MemoryBuffer> Instantiation =
2149 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002150
Daniel Dunbar43235712010-07-18 18:54:11 +00002151 // Create the macro instantiation object and add to the current macro
2152 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002153 MacroInstantiation *MI = new MacroInstantiation(
2154 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002155 ActiveMacros.push_back(MI);
2156
Toma Tabacu217116e2015-04-27 10:50:29 +00002157 ++NumOfMacroInstantiations;
2158
Daniel Dunbar43235712010-07-18 18:54:11 +00002159 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002160 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002161 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002162 Lex();
2163
2164 return false;
2165}
2166
Jim Grosbach4b905842013-09-20 23:08:21 +00002167void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002168 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002169 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002170 Lex();
2171
2172 // Pop the instantiation entry.
2173 delete ActiveMacros.back();
2174 ActiveMacros.pop_back();
2175}
2176
Jim Grosbach4b905842013-09-20 23:08:21 +00002177static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002178 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002179 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002180 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2181 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002182 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002183 case MCExpr::Target:
2184 case MCExpr::Constant:
2185 return false;
2186 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002187 const MCSymbol &S =
2188 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002189 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002190 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002191 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002192 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002193 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002194 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002195 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002196
2197 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002198}
2199
Jim Grosbach4b905842013-09-20 23:08:21 +00002200bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002201 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002202 // FIXME: Use better location, we should use proper tokens.
2203 SMLoc EqualLoc = Lexer.getLoc();
2204
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002205 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002206 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002207 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002208
Rafael Espindola72f5f172012-01-28 05:57:00 +00002209 // Note: we don't count b as used in "a = b". This is to allow
2210 // a = b
2211 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002212
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002213 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002214 return TokError("unexpected token in assignment");
2215
2216 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002217 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002218
Daniel Dunbar5f339242009-10-16 01:57:39 +00002219 // Validate that the LHS is allowed to be a variable (either it has not been
2220 // used as a symbol, or it is an absolute symbol).
Jim Grosbach6f482002015-05-18 18:43:14 +00002221 MCSymbol *Sym = getContext().lookupSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222 if (Sym) {
2223 // Diagnose assignment to a label.
2224 //
2225 // FIXME: Diagnostics. Note the location of the definition as a label.
2226 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002227 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002228 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2229 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002230 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002231 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2232 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002233 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002234 return Error(EqualLoc, "redefinition of '" + Name + "'");
2235 else if (!Sym->isVariable())
2236 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002237 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002238 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002239 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002240
2241 // Don't count these checks as uses.
2242 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002243 } else if (Name == ".") {
2244 if (Out.EmitValueToOffset(Value, 0)) {
2245 Error(EqualLoc, "expected absolute expression");
2246 eatToEndOfStatement();
2247 }
2248 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002249 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00002250 Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002251
David Majnemer58cb80c2014-12-24 10:27:50 +00002252 Sym->setRedefinable(allow_redef);
2253
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002254 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002255 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002256 if (NoDeadStrip)
2257 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2258
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002259 return false;
2260}
2261
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002262/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263/// ::= identifier
2264/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002265bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002266 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002267 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2268 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002269 // handle this as a context dependent token, instead we detect adjacent tokens
2270 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002271 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2272 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002273
Hans Wennborgce69d772013-10-18 20:46:28 +00002274 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002275 Lex();
2276 if (Lexer.isNot(AsmToken::Identifier))
2277 return true;
2278
Hans Wennborgce69d772013-10-18 20:46:28 +00002279 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2280 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002281 return true;
2282
2283 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002284 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002285 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002286 Lex();
2287 return false;
2288 }
2289
Jim Grosbach4b905842013-09-20 23:08:21 +00002290 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002291 return true;
2292
Sean Callanan936b0d32010-01-19 21:44:56 +00002293 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002294
Sean Callanan686ed8d2010-01-19 20:22:31 +00002295 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002296
2297 return false;
2298}
2299
Jim Grosbach4b905842013-09-20 23:08:21 +00002300/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002301/// ::= .equ identifier ',' expression
2302/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002303/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002304bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002305 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002306
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002307 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002308 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002309
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002310 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002311 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002312 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002313
Jim Grosbach4b905842013-09-20 23:08:21 +00002314 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002315}
2316
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002317bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002318 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002319
2320 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002321 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2323 if (Str[i] != '\\') {
2324 Data += Str[i];
2325 continue;
2326 }
2327
2328 // Recognize escaped characters. Note that this escape semantics currently
2329 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2330 ++i;
2331 if (i == e)
2332 return TokError("unexpected backslash at end of string");
2333
2334 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002335 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002336 // Consume up to three octal characters.
2337 unsigned Value = Str[i] - '0';
2338
Jim Grosbach4b905842013-09-20 23:08:21 +00002339 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002340 ++i;
2341 Value = Value * 8 + (Str[i] - '0');
2342
Jim Grosbach4b905842013-09-20 23:08:21 +00002343 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002344 ++i;
2345 Value = Value * 8 + (Str[i] - '0');
2346 }
2347 }
2348
2349 if (Value > 255)
2350 return TokError("invalid octal escape sequence (out of range)");
2351
Jim Grosbach4b905842013-09-20 23:08:21 +00002352 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002353 continue;
2354 }
2355
2356 // Otherwise recognize individual escapes.
2357 switch (Str[i]) {
2358 default:
2359 // Just reject invalid escape sequences for now.
2360 return TokError("invalid escape sequence (unrecognized character)");
2361
2362 case 'b': Data += '\b'; break;
2363 case 'f': Data += '\f'; break;
2364 case 'n': Data += '\n'; break;
2365 case 'r': Data += '\r'; break;
2366 case 't': Data += '\t'; break;
2367 case '"': Data += '"'; break;
2368 case '\\': Data += '\\'; break;
2369 }
2370 }
2371
2372 return false;
2373}
2374
Jim Grosbach4b905842013-09-20 23:08:21 +00002375/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002376/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002377bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002378 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002379 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002380
Daniel Dunbara10e5192009-06-24 23:30:00 +00002381 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002383 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002384
Daniel Dunbaref668c12009-08-14 18:19:52 +00002385 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002386 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002387 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002388
Rafael Espindola64e1af82013-07-02 15:49:13 +00002389 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002390 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002391 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002392
Sean Callanan686ed8d2010-01-19 20:22:31 +00002393 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002394
2395 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002396 break;
2397
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002398 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002399 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002400 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002401 }
2402 }
2403
Sean Callanan686ed8d2010-01-19 20:22:31 +00002404 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002405 return false;
2406}
2407
Jim Grosbach4b905842013-09-20 23:08:21 +00002408/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002409/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002410bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002411 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002412 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002413
Daniel Dunbara10e5192009-06-24 23:30:00 +00002414 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002415 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002416 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002417 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002418 return true;
2419
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002420 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002421 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2422 assert(Size <= 8 && "Invalid size");
2423 uint64_t IntValue = MCE->getValue();
2424 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2425 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002426 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002427 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002428 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002429
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002430 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002431 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002432
Daniel Dunbara10e5192009-06-24 23:30:00 +00002433 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002434 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002435 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002436 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002437 }
2438 }
2439
Sean Callanan686ed8d2010-01-19 20:22:31 +00002440 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002441 return false;
2442}
2443
David Woodhoused6de0d92014-02-01 16:20:59 +00002444/// ParseDirectiveOctaValue
2445/// ::= .octa [ hexconstant (, hexconstant)* ]
2446bool AsmParser::parseDirectiveOctaValue() {
2447 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2448 checkForValidSection();
2449
2450 for (;;) {
2451 if (Lexer.getKind() == AsmToken::Error)
2452 return true;
2453 if (Lexer.getKind() != AsmToken::Integer &&
2454 Lexer.getKind() != AsmToken::BigNum)
2455 return TokError("unknown token in expression");
2456
2457 SMLoc ExprLoc = getLexer().getLoc();
2458 APInt IntValue = getTok().getAPIntVal();
2459 Lex();
2460
2461 uint64_t hi, lo;
2462 if (IntValue.isIntN(64)) {
2463 hi = 0;
2464 lo = IntValue.getZExtValue();
2465 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002466 // It might actually have more than 128 bits, but the top ones are zero.
2467 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002468 lo = IntValue.getLoBits(64).getZExtValue();
2469 } else
2470 return Error(ExprLoc, "literal value out of range for directive");
2471
2472 if (MAI.isLittleEndian()) {
2473 getStreamer().EmitIntValue(lo, 8);
2474 getStreamer().EmitIntValue(hi, 8);
2475 } else {
2476 getStreamer().EmitIntValue(hi, 8);
2477 getStreamer().EmitIntValue(lo, 8);
2478 }
2479
2480 if (getLexer().is(AsmToken::EndOfStatement))
2481 break;
2482
2483 // FIXME: Improve diagnostic.
2484 if (getLexer().isNot(AsmToken::Comma))
2485 return TokError("unexpected token in directive");
2486 Lex();
2487 }
2488 }
2489
2490 Lex();
2491 return false;
2492}
2493
Jim Grosbach4b905842013-09-20 23:08:21 +00002494/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002495/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002496bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002497 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002498 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002499
2500 for (;;) {
2501 // We don't truly support arithmetic on floating point expressions, so we
2502 // have to manually parse unary prefixes.
2503 bool IsNeg = false;
2504 if (getLexer().is(AsmToken::Minus)) {
2505 Lex();
2506 IsNeg = true;
2507 } else if (getLexer().is(AsmToken::Plus))
2508 Lex();
2509
Michael J. Spencer530ce852010-10-09 11:00:50 +00002510 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002511 getLexer().isNot(AsmToken::Real) &&
2512 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002513 return TokError("unexpected token in directive");
2514
2515 // Convert to an APFloat.
2516 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002517 StringRef IDVal = getTok().getString();
2518 if (getLexer().is(AsmToken::Identifier)) {
2519 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2520 Value = APFloat::getInf(Semantics);
2521 else if (!IDVal.compare_lower("nan"))
2522 Value = APFloat::getNaN(Semantics, false, ~0);
2523 else
2524 return TokError("invalid floating point literal");
2525 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002526 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002527 return TokError("invalid floating point literal");
2528 if (IsNeg)
2529 Value.changeSign();
2530
2531 // Consume the numeric token.
2532 Lex();
2533
2534 // Emit the value as an integer.
2535 APInt AsInt = Value.bitcastToAPInt();
2536 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002537 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002538
2539 if (getLexer().is(AsmToken::EndOfStatement))
2540 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002541
Daniel Dunbar2af16532010-09-24 01:59:56 +00002542 if (getLexer().isNot(AsmToken::Comma))
2543 return TokError("unexpected token in directive");
2544 Lex();
2545 }
2546 }
2547
2548 Lex();
2549 return false;
2550}
2551
Jim Grosbach4b905842013-09-20 23:08:21 +00002552/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002553/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002554bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002555 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002556
2557 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002558 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002559 return true;
2560
Rafael Espindolab91bac62010-10-05 19:42:57 +00002561 int64_t Val = 0;
2562 if (getLexer().is(AsmToken::Comma)) {
2563 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002564 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002565 return true;
2566 }
2567
Rafael Espindola922e3f42010-09-16 15:03:59 +00002568 if (getLexer().isNot(AsmToken::EndOfStatement))
2569 return TokError("unexpected token in '.zero' directive");
2570
2571 Lex();
2572
Rafael Espindola64e1af82013-07-02 15:49:13 +00002573 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002574
2575 return false;
2576}
2577
Jim Grosbach4b905842013-09-20 23:08:21 +00002578/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002579/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002580bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002581 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002582
David Majnemer522d3db2014-02-01 07:19:38 +00002583 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002584 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002585 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002586 return true;
2587
David Majnemer522d3db2014-02-01 07:19:38 +00002588 if (NumValues < 0) {
2589 Warning(RepeatLoc,
2590 "'.fill' directive with negative repeat count has no effect");
2591 NumValues = 0;
2592 }
2593
Roman Divackye33098f2013-09-24 17:44:41 +00002594 int64_t FillSize = 1;
2595 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002596
David Majnemer522d3db2014-02-01 07:19:38 +00002597 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002598 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2599 if (getLexer().isNot(AsmToken::Comma))
2600 return TokError("unexpected token in '.fill' directive");
2601 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002602
David Majnemer522d3db2014-02-01 07:19:38 +00002603 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002604 if (parseAbsoluteExpression(FillSize))
2605 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002606
Roman Divackye33098f2013-09-24 17:44:41 +00002607 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2608 if (getLexer().isNot(AsmToken::Comma))
2609 return TokError("unexpected token in '.fill' directive");
2610 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002611
David Majnemer522d3db2014-02-01 07:19:38 +00002612 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002613 if (parseAbsoluteExpression(FillExpr))
2614 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002615
Roman Divackye33098f2013-09-24 17:44:41 +00002616 if (getLexer().isNot(AsmToken::EndOfStatement))
2617 return TokError("unexpected token in '.fill' directive");
2618
2619 Lex();
2620 }
2621 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002622
David Majnemer522d3db2014-02-01 07:19:38 +00002623 if (FillSize < 0) {
2624 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2625 NumValues = 0;
2626 }
2627 if (FillSize > 8) {
2628 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2629 FillSize = 8;
2630 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002631
David Majnemer522d3db2014-02-01 07:19:38 +00002632 if (!isUInt<32>(FillExpr) && FillSize > 4)
2633 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2634
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002635 if (NumValues > 0) {
2636 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2637 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2638 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2639 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2640 if (NonZeroFillSize < FillSize)
2641 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2642 }
David Majnemer522d3db2014-02-01 07:19:38 +00002643 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002644
2645 return false;
2646}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002647
Jim Grosbach4b905842013-09-20 23:08:21 +00002648/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002649/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002650bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002651 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002652
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002653 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002654 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002655 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002656 return true;
2657
2658 // Parse optional fill expression.
2659 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002660 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2661 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002662 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002663 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002664
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002665 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002666 return true;
2667
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002668 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002669 return TokError("unexpected token in '.org' directive");
2670 }
2671
Sean Callanan686ed8d2010-01-19 20:22:31 +00002672 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002673
Jim Grosbachb5912772012-01-27 00:37:08 +00002674 // Only limited forms of relocatable expressions are accepted here, it
2675 // has to be relative to the current section. The streamer will return
2676 // 'true' if the expression wasn't evaluatable.
2677 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2678 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002679
2680 return false;
2681}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682
Jim Grosbach4b905842013-09-20 23:08:21 +00002683/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002685bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002686 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002687
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002688 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002690 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 return true;
2692
2693 SMLoc MaxBytesLoc;
2694 bool HasFillExpr = false;
2695 int64_t FillExpr = 0;
2696 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002697 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2698 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002700 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002701
2702 // The fill expression can be omitted while specifying a maximum number of
2703 // alignment bytes, e.g:
2704 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002705 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002706 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002707 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002708 return true;
2709 }
2710
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002711 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2712 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002713 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002714 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002715
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002716 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002717 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002718 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002719
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002720 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002721 return TokError("unexpected token in directive");
2722 }
2723 }
2724
Sean Callanan686ed8d2010-01-19 20:22:31 +00002725 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002726
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002727 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002728 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002729
2730 // Compute alignment in bytes.
2731 if (IsPow2) {
2732 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002733 if (Alignment >= 32) {
2734 Error(AlignmentLoc, "invalid alignment value");
2735 Alignment = 31;
2736 }
2737
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002738 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002739 } else {
2740 // Reject alignments that aren't a power of two, for gas compatibility.
2741 if (!isPowerOf2_64(Alignment))
2742 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002743 }
2744
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002745 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002746 if (MaxBytesLoc.isValid()) {
2747 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002748 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002749 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002750 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002751 }
2752
2753 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002754 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002755 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002756 MaxBytesToFill = 0;
2757 }
2758 }
2759
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002760 // Check whether we should use optimal code alignment for this .align
2761 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002762 const MCSection *Section = getStreamer().getCurrentSection().first;
2763 assert(Section && "must have section to emit alignment");
2764 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002765 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2766 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002767 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002768 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002769 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002770 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2771 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002772 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002773
2774 return false;
2775}
2776
Jim Grosbach4b905842013-09-20 23:08:21 +00002777/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002778/// ::= .file [number] filename
2779/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002780bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002781 // FIXME: I'm not sure what this is.
2782 int64_t FileNumber = -1;
2783 SMLoc FileNumberLoc = getLexer().getLoc();
2784 if (getLexer().is(AsmToken::Integer)) {
2785 FileNumber = getTok().getIntVal();
2786 Lex();
2787
2788 if (FileNumber < 1)
2789 return TokError("file number less than one");
2790 }
2791
2792 if (getLexer().isNot(AsmToken::String))
2793 return TokError("unexpected token in '.file' directive");
2794
2795 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002796 // Allow the strings to have escaped octal character sequence.
2797 std::string Path = getTok().getString();
2798 if (parseEscapedString(Path))
2799 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002800 Lex();
2801
2802 StringRef Directory;
2803 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002804 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002805 if (getLexer().is(AsmToken::String)) {
2806 if (FileNumber == -1)
2807 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002808 if (parseEscapedString(FilenameData))
2809 return true;
2810 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002811 Directory = Path;
2812 Lex();
2813 } else {
2814 Filename = Path;
2815 }
2816
2817 if (getLexer().isNot(AsmToken::EndOfStatement))
2818 return TokError("unexpected token in '.file' directive");
2819
2820 if (FileNumber == -1)
2821 getStreamer().EmitFileDirective(Filename);
2822 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002823 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002824 Error(DirectiveLoc,
2825 "input can't have .file dwarf directives when -g is "
2826 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002827
David Blaikiec714ef42014-03-17 01:52:11 +00002828 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2829 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002830 Error(FileNumberLoc, "file number already allocated");
2831 }
2832
2833 return false;
2834}
2835
Jim Grosbach4b905842013-09-20 23:08:21 +00002836/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002837/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002838bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002839 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2840 if (getLexer().isNot(AsmToken::Integer))
2841 return TokError("unexpected token in '.line' directive");
2842
2843 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002844 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002845 Lex();
2846
2847 // FIXME: Do something with the .line.
2848 }
2849
2850 if (getLexer().isNot(AsmToken::EndOfStatement))
2851 return TokError("unexpected token in '.line' directive");
2852
2853 return false;
2854}
2855
Jim Grosbach4b905842013-09-20 23:08:21 +00002856/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002857/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2858/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2859/// The first number is a file number, must have been previously assigned with
2860/// a .file directive, the second number is the line number and optionally the
2861/// third number is a column position (zero if not specified). The remaining
2862/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002863bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002864 if (getLexer().isNot(AsmToken::Integer))
2865 return TokError("unexpected token in '.loc' directive");
2866 int64_t FileNumber = getTok().getIntVal();
2867 if (FileNumber < 1)
2868 return TokError("file number less than one in '.loc' directive");
2869 if (!getContext().isValidDwarfFileNumber(FileNumber))
2870 return TokError("unassigned file number in '.loc' directive");
2871 Lex();
2872
2873 int64_t LineNumber = 0;
2874 if (getLexer().is(AsmToken::Integer)) {
2875 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002876 if (LineNumber < 0)
2877 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002878 Lex();
2879 }
2880
2881 int64_t ColumnPos = 0;
2882 if (getLexer().is(AsmToken::Integer)) {
2883 ColumnPos = getTok().getIntVal();
2884 if (ColumnPos < 0)
2885 return TokError("column position less than zero in '.loc' directive");
2886 Lex();
2887 }
2888
2889 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2890 unsigned Isa = 0;
2891 int64_t Discriminator = 0;
2892 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2893 for (;;) {
2894 if (getLexer().is(AsmToken::EndOfStatement))
2895 break;
2896
2897 StringRef Name;
2898 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002899 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return TokError("unexpected token in '.loc' directive");
2901
2902 if (Name == "basic_block")
2903 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2904 else if (Name == "prologue_end")
2905 Flags |= DWARF2_FLAG_PROLOGUE_END;
2906 else if (Name == "epilogue_begin")
2907 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2908 else if (Name == "is_stmt") {
2909 Loc = getTok().getLoc();
2910 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002911 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
2913 // The expression must be the constant 0 or 1.
2914 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2915 int Value = MCE->getValue();
2916 if (Value == 0)
2917 Flags &= ~DWARF2_FLAG_IS_STMT;
2918 else if (Value == 1)
2919 Flags |= DWARF2_FLAG_IS_STMT;
2920 else
2921 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002922 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002923 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2924 }
Craig Topperf15655b2013-04-22 04:22:40 +00002925 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002926 Loc = getTok().getLoc();
2927 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002928 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002929 return true;
2930 // The expression must be a constant greater or equal to 0.
2931 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2932 int Value = MCE->getValue();
2933 if (Value < 0)
2934 return Error(Loc, "isa number less than zero");
2935 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002936 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002937 return Error(Loc, "isa number not a constant value");
2938 }
Craig Topperf15655b2013-04-22 04:22:40 +00002939 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002940 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002941 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002942 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002943 return Error(Loc, "unknown sub-directive in '.loc' directive");
2944 }
2945
2946 if (getLexer().is(AsmToken::EndOfStatement))
2947 break;
2948 }
2949 }
2950
2951 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2952 Isa, Discriminator, StringRef());
2953
2954 return false;
2955}
2956
Jim Grosbach4b905842013-09-20 23:08:21 +00002957/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002958/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002959bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002960 return TokError("unsupported directive '.stabs'");
2961}
2962
Jim Grosbach4b905842013-09-20 23:08:21 +00002963/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002964/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002965bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002966 StringRef Name;
2967 bool EH = false;
2968 bool Debug = false;
2969
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002970 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002971 return TokError("Expected an identifier");
2972
2973 if (Name == ".eh_frame")
2974 EH = true;
2975 else if (Name == ".debug_frame")
2976 Debug = true;
2977
2978 if (getLexer().is(AsmToken::Comma)) {
2979 Lex();
2980
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002981 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002982 return TokError("Expected an identifier");
2983
2984 if (Name == ".eh_frame")
2985 EH = true;
2986 else if (Name == ".debug_frame")
2987 Debug = true;
2988 }
2989
2990 getStreamer().EmitCFISections(EH, Debug);
2991 return false;
2992}
2993
Jim Grosbach4b905842013-09-20 23:08:21 +00002994/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002995/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002996bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002997 StringRef Simple;
2998 if (getLexer().isNot(AsmToken::EndOfStatement))
2999 if (parseIdentifier(Simple) || Simple != "simple")
3000 return TokError("unexpected token in .cfi_startproc directive");
3001
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003002 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003003 return false;
3004}
3005
Jim Grosbach4b905842013-09-20 23:08:21 +00003006/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003007/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003008bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003009 getStreamer().EmitCFIEndProc();
3010 return false;
3011}
3012
Jim Grosbach4b905842013-09-20 23:08:21 +00003013/// \brief parse register name or number.
3014bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003015 SMLoc DirectiveLoc) {
3016 unsigned RegNo;
3017
3018 if (getLexer().isNot(AsmToken::Integer)) {
3019 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3020 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003021 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003022 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003023 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003024
3025 return false;
3026}
3027
Jim Grosbach4b905842013-09-20 23:08:21 +00003028/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003029/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003030bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003031 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003032 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003033 return true;
3034
3035 if (getLexer().isNot(AsmToken::Comma))
3036 return TokError("unexpected token in directive");
3037 Lex();
3038
3039 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003040 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003041 return true;
3042
3043 getStreamer().EmitCFIDefCfa(Register, Offset);
3044 return false;
3045}
3046
Jim Grosbach4b905842013-09-20 23:08:21 +00003047/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003048/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003049bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003050 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003051 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003052 return true;
3053
3054 getStreamer().EmitCFIDefCfaOffset(Offset);
3055 return false;
3056}
3057
Jim Grosbach4b905842013-09-20 23:08:21 +00003058/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003059/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003060bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003061 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003062 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003063 return true;
3064
3065 if (getLexer().isNot(AsmToken::Comma))
3066 return TokError("unexpected token in directive");
3067 Lex();
3068
3069 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003070 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003071 return true;
3072
3073 getStreamer().EmitCFIRegister(Register1, Register2);
3074 return false;
3075}
3076
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003077/// parseDirectiveCFIWindowSave
3078/// ::= .cfi_window_save
3079bool AsmParser::parseDirectiveCFIWindowSave() {
3080 getStreamer().EmitCFIWindowSave();
3081 return false;
3082}
3083
Jim Grosbach4b905842013-09-20 23:08:21 +00003084/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003085/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003086bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003087 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003088 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003089 return true;
3090
3091 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3092 return false;
3093}
3094
Jim Grosbach4b905842013-09-20 23:08:21 +00003095/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003096/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003097bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003098 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003099 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003100 return true;
3101
3102 getStreamer().EmitCFIDefCfaRegister(Register);
3103 return false;
3104}
3105
Jim Grosbach4b905842013-09-20 23:08:21 +00003106/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003107/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003108bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003109 int64_t Register = 0;
3110 int64_t Offset = 0;
3111
Jim Grosbach4b905842013-09-20 23:08:21 +00003112 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003113 return true;
3114
3115 if (getLexer().isNot(AsmToken::Comma))
3116 return TokError("unexpected token in directive");
3117 Lex();
3118
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003119 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003120 return true;
3121
3122 getStreamer().EmitCFIOffset(Register, Offset);
3123 return false;
3124}
3125
Jim Grosbach4b905842013-09-20 23:08:21 +00003126/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003127/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003128bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003129 int64_t Register = 0;
3130
Jim Grosbach4b905842013-09-20 23:08:21 +00003131 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003132 return true;
3133
3134 if (getLexer().isNot(AsmToken::Comma))
3135 return TokError("unexpected token in directive");
3136 Lex();
3137
3138 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003139 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003140 return true;
3141
3142 getStreamer().EmitCFIRelOffset(Register, Offset);
3143 return false;
3144}
3145
3146static bool isValidEncoding(int64_t Encoding) {
3147 if (Encoding & ~0xff)
3148 return false;
3149
3150 if (Encoding == dwarf::DW_EH_PE_omit)
3151 return true;
3152
3153 const unsigned Format = Encoding & 0xf;
3154 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3155 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3156 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3157 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3158 return false;
3159
3160 const unsigned Application = Encoding & 0x70;
3161 if (Application != dwarf::DW_EH_PE_absptr &&
3162 Application != dwarf::DW_EH_PE_pcrel)
3163 return false;
3164
3165 return true;
3166}
3167
Jim Grosbach4b905842013-09-20 23:08:21 +00003168/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003169/// IsPersonality true for cfi_personality, false for cfi_lsda
3170/// ::= .cfi_personality encoding, [symbol_name]
3171/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003172bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003173 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003174 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003175 return true;
3176 if (Encoding == dwarf::DW_EH_PE_omit)
3177 return false;
3178
3179 if (!isValidEncoding(Encoding))
3180 return TokError("unsupported encoding.");
3181
3182 if (getLexer().isNot(AsmToken::Comma))
3183 return TokError("unexpected token in directive");
3184 Lex();
3185
3186 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003187 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003188 return TokError("expected identifier in directive");
3189
Jim Grosbach6f482002015-05-18 18:43:14 +00003190 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003191
3192 if (IsPersonality)
3193 getStreamer().EmitCFIPersonality(Sym, Encoding);
3194 else
3195 getStreamer().EmitCFILsda(Sym, Encoding);
3196 return false;
3197}
3198
Jim Grosbach4b905842013-09-20 23:08:21 +00003199/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003200/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003201bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003202 getStreamer().EmitCFIRememberState();
3203 return false;
3204}
3205
Jim Grosbach4b905842013-09-20 23:08:21 +00003206/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003207/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003208bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003209 getStreamer().EmitCFIRestoreState();
3210 return false;
3211}
3212
Jim Grosbach4b905842013-09-20 23:08:21 +00003213/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003214/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003215bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003216 int64_t Register = 0;
3217
Jim Grosbach4b905842013-09-20 23:08:21 +00003218 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003219 return true;
3220
3221 getStreamer().EmitCFISameValue(Register);
3222 return false;
3223}
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003226/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003227bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003228 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003229 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003230 return true;
3231
3232 getStreamer().EmitCFIRestore(Register);
3233 return false;
3234}
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003237/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003238bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 std::string Values;
3240 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003241 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003242 return true;
3243
3244 Values.push_back((uint8_t)CurrValue);
3245
3246 while (getLexer().is(AsmToken::Comma)) {
3247 Lex();
3248
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003249 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003250 return true;
3251
3252 Values.push_back((uint8_t)CurrValue);
3253 }
3254
3255 getStreamer().EmitCFIEscape(Values);
3256 return false;
3257}
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003260/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003261bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003262 if (getLexer().isNot(AsmToken::EndOfStatement))
3263 return Error(getLexer().getLoc(),
3264 "unexpected token in '.cfi_signal_frame'");
3265
3266 getStreamer().EmitCFISignalFrame();
3267 return false;
3268}
3269
Jim Grosbach4b905842013-09-20 23:08:21 +00003270/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003271/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003272bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003273 int64_t Register = 0;
3274
Jim Grosbach4b905842013-09-20 23:08:21 +00003275 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003276 return true;
3277
3278 getStreamer().EmitCFIUndefined(Register);
3279 return false;
3280}
3281
Jim Grosbach4b905842013-09-20 23:08:21 +00003282/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003283/// ::= .macros_on
3284/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003285bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003286 if (getLexer().isNot(AsmToken::EndOfStatement))
3287 return Error(getLexer().getLoc(),
3288 "unexpected token in '" + Directive + "' directive");
3289
Jim Grosbach4b905842013-09-20 23:08:21 +00003290 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003291 return false;
3292}
3293
Jim Grosbach4b905842013-09-20 23:08:21 +00003294/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003295/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003296bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003297 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003298 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003299 return TokError("expected identifier in '.macro' directive");
3300
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003301 if (getLexer().is(AsmToken::Comma))
3302 Lex();
3303
Eli Bendersky17233942013-01-15 22:59:42 +00003304 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003305 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003306
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003307 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003308 return Error(Lexer.getLoc(),
3309 "Vararg parameter '" + Parameters.back().Name +
3310 "' should be last one in the list of parameters.");
3311
David Majnemer91fc4c22014-01-29 18:57:46 +00003312 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003313 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003314 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003315
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003316 if (Lexer.is(AsmToken::Colon)) {
3317 Lex(); // consume ':'
3318
3319 SMLoc QualLoc;
3320 StringRef Qualifier;
3321
3322 QualLoc = Lexer.getLoc();
3323 if (parseIdentifier(Qualifier))
3324 return Error(QualLoc, "missing parameter qualifier for "
3325 "'" + Parameter.Name + "' in macro '" + Name + "'");
3326
3327 if (Qualifier == "req")
3328 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003329 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003330 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003331 else
3332 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3333 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3334 }
3335
David Majnemer91fc4c22014-01-29 18:57:46 +00003336 if (getLexer().is(AsmToken::Equal)) {
3337 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003338
3339 SMLoc ParamLoc;
3340
3341 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003342 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003343 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003344
3345 if (Parameter.Required)
3346 Warning(ParamLoc, "pointless default value for required parameter "
3347 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003348 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003349
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003350 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003351
3352 if (getLexer().is(AsmToken::Comma))
3353 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003354 }
3355
3356 // Eat the end of statement.
3357 Lex();
3358
3359 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003360 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003361
3362 // Lex the macro definition.
3363 for (;;) {
3364 // Check whether we have reached the end of the file.
3365 if (getLexer().is(AsmToken::Eof))
3366 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3367
3368 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003369 if (getLexer().is(AsmToken::Identifier)) {
3370 if (getTok().getIdentifier() == ".endm" ||
3371 getTok().getIdentifier() == ".endmacro") {
3372 if (MacroDepth == 0) { // Outermost macro.
3373 EndToken = getTok();
3374 Lex();
3375 if (getLexer().isNot(AsmToken::EndOfStatement))
3376 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3377 "' directive");
3378 break;
3379 } else {
3380 // Otherwise we just found the end of an inner macro.
3381 --MacroDepth;
3382 }
3383 } else if (getTok().getIdentifier() == ".macro") {
3384 // We allow nested macros. Those aren't instantiated until the outermost
3385 // macro is expanded so just ignore them for now.
3386 ++MacroDepth;
3387 }
Eli Bendersky17233942013-01-15 22:59:42 +00003388 }
3389
3390 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003391 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003392 }
3393
Jim Grosbach4b905842013-09-20 23:08:21 +00003394 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003395 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3396 }
3397
3398 const char *BodyStart = StartToken.getLoc().getPointer();
3399 const char *BodyEnd = EndToken.getLoc().getPointer();
3400 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003401 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003402 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003403 return false;
3404}
3405
Jim Grosbach4b905842013-09-20 23:08:21 +00003406/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003407///
3408/// With the support added for named parameters there may be code out there that
3409/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003410/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003411/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003412/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003413/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3414/// warning that the positional parameter found in body which have no effect.
3415/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003416/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003417/// intended or change the macro to use the named parameters. It is possible
3418/// this warning will trigger when the none of the named parameters are used
3419/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003420void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003421 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003422 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003423 // If this macro is not defined with named parameters the warning we are
3424 // checking for here doesn't apply.
3425 unsigned NParameters = Parameters.size();
3426 if (NParameters == 0)
3427 return;
3428
3429 bool NamedParametersFound = false;
3430 bool PositionalParametersFound = false;
3431
3432 // Look at the body of the macro for use of both the named parameters and what
3433 // are likely to be positional parameters. This is what expandMacro() is
3434 // doing when it finds the parameters in the body.
3435 while (!Body.empty()) {
3436 // Scan for the next possible parameter.
3437 std::size_t End = Body.size(), Pos = 0;
3438 for (; Pos != End; ++Pos) {
3439 // Check for a substitution or escape.
3440 // This macro is defined with parameters, look for \foo, \bar, etc.
3441 if (Body[Pos] == '\\' && Pos + 1 != End)
3442 break;
3443
3444 // This macro should have parameters, but look for $0, $1, ..., $n too.
3445 if (Body[Pos] != '$' || Pos + 1 == End)
3446 continue;
3447 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003448 if (Next == '$' || Next == 'n' ||
3449 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003450 break;
3451 }
3452
3453 // Check if we reached the end.
3454 if (Pos == End)
3455 break;
3456
3457 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003458 switch (Body[Pos + 1]) {
3459 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003460 case '$':
3461 break;
3462
Jim Grosbach4b905842013-09-20 23:08:21 +00003463 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003464 case 'n':
3465 PositionalParametersFound = true;
3466 break;
3467
Jim Grosbach4b905842013-09-20 23:08:21 +00003468 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003469 default: {
3470 PositionalParametersFound = true;
3471 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003472 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003473 }
3474 Pos += 2;
3475 } else {
3476 unsigned I = Pos + 1;
3477 while (isIdentifierChar(Body[I]) && I + 1 != End)
3478 ++I;
3479
Jim Grosbach4b905842013-09-20 23:08:21 +00003480 const char *Begin = Body.data() + Pos + 1;
3481 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003482 unsigned Index = 0;
3483 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003484 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003485 break;
3486
3487 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003488 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3489 Pos += 3;
3490 else {
3491 Pos = I;
3492 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003493 } else {
3494 NamedParametersFound = true;
3495 Pos += 1 + Argument.size();
3496 }
3497 }
3498 // Update the scan point.
3499 Body = Body.substr(Pos);
3500 }
3501
3502 if (!NamedParametersFound && PositionalParametersFound)
3503 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3504 "used in macro body, possible positional parameter "
3505 "found in body which will have no effect");
3506}
3507
Nico Weber155dccd12014-07-24 17:08:39 +00003508/// parseDirectiveExitMacro
3509/// ::= .exitm
3510bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3511 if (getLexer().isNot(AsmToken::EndOfStatement))
3512 return TokError("unexpected token in '" + Directive + "' directive");
3513
3514 if (!isInsideMacroInstantiation())
3515 return TokError("unexpected '" + Directive + "' in file, "
3516 "no current macro definition");
3517
3518 // Exit all conditionals that are active in the current macro.
3519 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3520 TheCondState = TheCondStack.back();
3521 TheCondStack.pop_back();
3522 }
3523
3524 handleMacroExit();
3525 return false;
3526}
3527
Jim Grosbach4b905842013-09-20 23:08:21 +00003528/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003529/// ::= .endm
3530/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003531bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003532 if (getLexer().isNot(AsmToken::EndOfStatement))
3533 return TokError("unexpected token in '" + Directive + "' directive");
3534
3535 // If we are inside a macro instantiation, terminate the current
3536 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003537 if (isInsideMacroInstantiation()) {
3538 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003539 return false;
3540 }
3541
3542 // Otherwise, this .endmacro is a stray entry in the file; well formed
3543 // .endmacro directives are handled during the macro definition parsing.
3544 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003545 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003546}
3547
Jim Grosbach4b905842013-09-20 23:08:21 +00003548/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003549/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003550bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003551 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003552 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003553 return TokError("expected identifier in '.purgem' directive");
3554
3555 if (getLexer().isNot(AsmToken::EndOfStatement))
3556 return TokError("unexpected token in '.purgem' directive");
3557
Jim Grosbach4b905842013-09-20 23:08:21 +00003558 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003559 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3560
Jim Grosbach4b905842013-09-20 23:08:21 +00003561 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003562 return false;
3563}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003564
Jim Grosbach4b905842013-09-20 23:08:21 +00003565/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003566/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003567bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003568 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003569
3570 // Expect a single argument: an expression that evaluates to a constant
3571 // in the inclusive range 0-30.
3572 SMLoc ExprLoc = getLexer().getLoc();
3573 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003574 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003575 return true;
3576 else if (getLexer().isNot(AsmToken::EndOfStatement))
3577 return TokError("unexpected token after expression in"
3578 " '.bundle_align_mode' directive");
3579 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3580 return Error(ExprLoc,
3581 "invalid bundle alignment size (expected between 0 and 30)");
3582
3583 Lex();
3584
3585 // Because of AlignSizePow2's verified range we can safely truncate it to
3586 // unsigned.
3587 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3588 return false;
3589}
3590
Jim Grosbach4b905842013-09-20 23:08:21 +00003591/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003592/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003593bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003594 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003595 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003596
Eli Bendersky802b6282013-01-07 21:51:08 +00003597 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3598 StringRef Option;
3599 SMLoc Loc = getTok().getLoc();
3600 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003601 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003602
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003603 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003604 return Error(Loc, kInvalidOptionError);
3605
3606 if (Option != "align_to_end")
3607 return Error(Loc, kInvalidOptionError);
3608 else if (getLexer().isNot(AsmToken::EndOfStatement))
3609 return Error(Loc,
3610 "unexpected token after '.bundle_lock' directive option");
3611 AlignToEnd = true;
3612 }
3613
Eli Benderskyf483ff92012-12-20 19:05:53 +00003614 Lex();
3615
Eli Bendersky802b6282013-01-07 21:51:08 +00003616 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003617 return false;
3618}
3619
Jim Grosbach4b905842013-09-20 23:08:21 +00003620/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003621/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003622bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003623 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003624
3625 if (getLexer().isNot(AsmToken::EndOfStatement))
3626 return TokError("unexpected token in '.bundle_unlock' directive");
3627 Lex();
3628
3629 getStreamer().EmitBundleUnlock();
3630 return false;
3631}
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003634/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003635bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003636 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003637
3638 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003639 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003640 return true;
3641
3642 int64_t FillExpr = 0;
3643 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3644 if (getLexer().isNot(AsmToken::Comma))
3645 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3646 Lex();
3647
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003648 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003649 return true;
3650
3651 if (getLexer().isNot(AsmToken::EndOfStatement))
3652 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3653 }
3654
3655 Lex();
3656
3657 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003658 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3659 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003660
3661 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003662 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003663
3664 return false;
3665}
3666
Jim Grosbach4b905842013-09-20 23:08:21 +00003667/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003668/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003669bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003670 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003671 const MCExpr *Value;
3672
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003673 for (;;) {
3674 if (parseExpression(Value))
3675 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003676
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003677 if (Signed)
3678 getStreamer().EmitSLEB128Value(Value);
3679 else
3680 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003681
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003682 if (getLexer().is(AsmToken::EndOfStatement))
3683 break;
3684
3685 if (getLexer().isNot(AsmToken::Comma))
3686 return TokError("unexpected token in directive");
3687 Lex();
3688 }
Eli Bendersky17233942013-01-15 22:59:42 +00003689
3690 return false;
3691}
3692
Jim Grosbach4b905842013-09-20 23:08:21 +00003693/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003694/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003695bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003696 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003697 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003698 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003699 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003700
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003701 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003702 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003703
Jim Grosbach6f482002015-05-18 18:43:14 +00003704 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003705
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003706 // Assembler local symbols don't make any sense here. Complain loudly.
3707 if (Sym->isTemporary())
3708 return Error(Loc, "non-local symbol required in directive");
3709
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003710 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3711 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003712
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003713 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003714 break;
3715
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003716 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003717 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003718 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003719 }
3720 }
3721
Sean Callanan686ed8d2010-01-19 20:22:31 +00003722 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003723 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003724}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003725
Jim Grosbach4b905842013-09-20 23:08:21 +00003726/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003727/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003728bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003729 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003730
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003731 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003732 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003733 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003734 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003735
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003736 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003737 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003738
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003739 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003740 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003741 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742
3743 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003744 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003745 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003746 return true;
3747
3748 int64_t Pow2Alignment = 0;
3749 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003750 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003751 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003752 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003753 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003754 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003755
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003756 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3757 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003758 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3759
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003760 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003761 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3762 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003763 if (!isPowerOf2_64(Pow2Alignment))
3764 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3765 Pow2Alignment = Log2_64(Pow2Alignment);
3766 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003767 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003768
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003769 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003770 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003771
Sean Callanan686ed8d2010-01-19 20:22:31 +00003772 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003773
Chris Lattner28ad7542009-07-09 17:25:12 +00003774 // NOTE: a size of zero for a .comm should create a undefined symbol
3775 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003776 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003777 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003778 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003779
Eric Christopherbc818852010-05-14 01:38:54 +00003780 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003781 // may internally end up wanting an alignment in bytes.
3782 // FIXME: Diagnose overflow.
3783 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003784 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003785 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003786
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003787 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003788 return Error(IDLoc, "invalid symbol redefinition");
3789
Chris Lattner28ad7542009-07-09 17:25:12 +00003790 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003791 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003792 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003793 return false;
3794 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003795
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003797 return false;
3798}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003799
Jim Grosbach4b905842013-09-20 23:08:21 +00003800/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003801/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003802bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003803 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003804 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003805
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003807 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003808 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003809
Sean Callanan686ed8d2010-01-19 20:22:31 +00003810 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003811
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003812 if (Str.empty())
3813 Error(Loc, ".abort detected. Assembly stopping.");
3814 else
3815 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003816 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003817
3818 return false;
3819}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003820
Jim Grosbach4b905842013-09-20 23:08:21 +00003821/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003822/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003823bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003824 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003825 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003826
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003827 // Allow the strings to have escaped octal character sequence.
3828 std::string Filename;
3829 if (parseEscapedString(Filename))
3830 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003831 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003832 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003833
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003834 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003835 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003836
Chris Lattner693fbb82009-07-16 06:14:39 +00003837 // Attempt to switch the lexer to the included file before consuming the end
3838 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003839 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003840 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003841 return true;
3842 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003843
3844 return false;
3845}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003846
Jim Grosbach4b905842013-09-20 23:08:21 +00003847/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003848/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003849bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003850 if (getLexer().isNot(AsmToken::String))
3851 return TokError("expected string in '.incbin' directive");
3852
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003853 // Allow the strings to have escaped octal character sequence.
3854 std::string Filename;
3855 if (parseEscapedString(Filename))
3856 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003857 SMLoc IncbinLoc = getLexer().getLoc();
3858 Lex();
3859
3860 if (getLexer().isNot(AsmToken::EndOfStatement))
3861 return TokError("unexpected token in '.incbin' directive");
3862
Kevin Enderby109f25c2011-12-14 21:47:48 +00003863 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003864 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003865 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3866 return true;
3867 }
3868
3869 return false;
3870}
3871
Jim Grosbach4b905842013-09-20 23:08:21 +00003872/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003873/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3874bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003875 TheCondStack.push_back(TheCondState);
3876 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003877 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003878 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003879 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003880 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003881 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003882 return true;
3883
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003884 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003885 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003886
Sean Callanan686ed8d2010-01-19 20:22:31 +00003887 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003888
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003889 switch (DirKind) {
3890 default:
3891 llvm_unreachable("unsupported directive");
3892 case DK_IF:
3893 case DK_IFNE:
3894 break;
3895 case DK_IFEQ:
3896 ExprValue = ExprValue == 0;
3897 break;
3898 case DK_IFGE:
3899 ExprValue = ExprValue >= 0;
3900 break;
3901 case DK_IFGT:
3902 ExprValue = ExprValue > 0;
3903 break;
3904 case DK_IFLE:
3905 ExprValue = ExprValue <= 0;
3906 break;
3907 case DK_IFLT:
3908 ExprValue = ExprValue < 0;
3909 break;
3910 }
3911
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003912 TheCondState.CondMet = ExprValue;
3913 TheCondState.Ignore = !TheCondState.CondMet;
3914 }
3915
3916 return false;
3917}
3918
Jim Grosbach4b905842013-09-20 23:08:21 +00003919/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003920/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003921bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003922 TheCondStack.push_back(TheCondState);
3923 TheCondState.TheCond = AsmCond::IfCond;
3924
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003925 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003926 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003927 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003928 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003929
3930 if (getLexer().isNot(AsmToken::EndOfStatement))
3931 return TokError("unexpected token in '.ifb' directive");
3932
3933 Lex();
3934
3935 TheCondState.CondMet = ExpectBlank == Str.empty();
3936 TheCondState.Ignore = !TheCondState.CondMet;
3937 }
3938
3939 return false;
3940}
3941
Jim Grosbach4b905842013-09-20 23:08:21 +00003942/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003943/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003944/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003945bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003946 TheCondStack.push_back(TheCondState);
3947 TheCondState.TheCond = AsmCond::IfCond;
3948
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003949 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003950 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003951 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003952 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003953
3954 if (getLexer().isNot(AsmToken::Comma))
3955 return TokError("unexpected token in '.ifc' directive");
3956
3957 Lex();
3958
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003959 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003960
3961 if (getLexer().isNot(AsmToken::EndOfStatement))
3962 return TokError("unexpected token in '.ifc' directive");
3963
3964 Lex();
3965
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003966 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003967 TheCondState.Ignore = !TheCondState.CondMet;
3968 }
3969
3970 return false;
3971}
3972
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003973/// parseDirectiveIfeqs
3974/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003975bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003976 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003977 if (ExpectEqual)
3978 TokError("expected string parameter for '.ifeqs' directive");
3979 else
3980 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003981 eatToEndOfStatement();
3982 return true;
3983 }
3984
3985 StringRef String1 = getTok().getStringContents();
3986 Lex();
3987
3988 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003989 if (ExpectEqual)
3990 TokError("expected comma after first string for '.ifeqs' directive");
3991 else
3992 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003993 eatToEndOfStatement();
3994 return true;
3995 }
3996
3997 Lex();
3998
3999 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004000 if (ExpectEqual)
4001 TokError("expected string parameter for '.ifeqs' directive");
4002 else
4003 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004004 eatToEndOfStatement();
4005 return true;
4006 }
4007
4008 StringRef String2 = getTok().getStringContents();
4009 Lex();
4010
4011 TheCondStack.push_back(TheCondState);
4012 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004013 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004014 TheCondState.Ignore = !TheCondState.CondMet;
4015
4016 return false;
4017}
4018
Jim Grosbach4b905842013-09-20 23:08:21 +00004019/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004020/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004021bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004022 StringRef Name;
4023 TheCondStack.push_back(TheCondState);
4024 TheCondState.TheCond = AsmCond::IfCond;
4025
4026 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004027 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004028 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004029 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004030 return TokError("expected identifier after '.ifdef'");
4031
4032 Lex();
4033
Jim Grosbach6f482002015-05-18 18:43:14 +00004034 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004035
4036 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004037 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004038 else
Craig Topper353eda42014-04-24 06:44:33 +00004039 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004040 TheCondState.Ignore = !TheCondState.CondMet;
4041 }
4042
4043 return false;
4044}
4045
Jim Grosbach4b905842013-09-20 23:08:21 +00004046/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004047/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004048bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004049 if (TheCondState.TheCond != AsmCond::IfCond &&
4050 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004051 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4052 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004053 TheCondState.TheCond = AsmCond::ElseIfCond;
4054
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004055 bool LastIgnoreState = false;
4056 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004057 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004058 if (LastIgnoreState || TheCondState.CondMet) {
4059 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004060 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004061 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004062 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004063 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004064 return true;
4065
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004066 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004067 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004068
Sean Callanan686ed8d2010-01-19 20:22:31 +00004069 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004070 TheCondState.CondMet = ExprValue;
4071 TheCondState.Ignore = !TheCondState.CondMet;
4072 }
4073
4074 return false;
4075}
4076
Jim Grosbach4b905842013-09-20 23:08:21 +00004077/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004078/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004079bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004080 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004081 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004082
Sean Callanan686ed8d2010-01-19 20:22:31 +00004083 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004084
4085 if (TheCondState.TheCond != AsmCond::IfCond &&
4086 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004087 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4088 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004089 TheCondState.TheCond = AsmCond::ElseCond;
4090 bool LastIgnoreState = false;
4091 if (!TheCondStack.empty())
4092 LastIgnoreState = TheCondStack.back().Ignore;
4093 if (LastIgnoreState || TheCondState.CondMet)
4094 TheCondState.Ignore = true;
4095 else
4096 TheCondState.Ignore = false;
4097
4098 return false;
4099}
4100
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004101/// parseDirectiveEnd
4102/// ::= .end
4103bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4104 if (getLexer().isNot(AsmToken::EndOfStatement))
4105 return TokError("unexpected token in '.end' directive");
4106
4107 Lex();
4108
4109 while (Lexer.isNot(AsmToken::Eof))
4110 Lex();
4111
4112 return false;
4113}
4114
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004115/// parseDirectiveError
4116/// ::= .err
4117/// ::= .error [string]
4118bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4119 if (!TheCondStack.empty()) {
4120 if (TheCondStack.back().Ignore) {
4121 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004122 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004123 }
4124 }
4125
4126 if (!WithMessage)
4127 return Error(L, ".err encountered");
4128
4129 StringRef Message = ".error directive invoked in source file";
4130 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4131 if (Lexer.isNot(AsmToken::String)) {
4132 TokError(".error argument must be a string");
4133 eatToEndOfStatement();
4134 return true;
4135 }
4136
4137 Message = getTok().getStringContents();
4138 Lex();
4139 }
4140
4141 Error(L, Message);
4142 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004143}
4144
Nico Weber404012b2014-07-24 16:26:06 +00004145/// parseDirectiveWarning
4146/// ::= .warning [string]
4147bool AsmParser::parseDirectiveWarning(SMLoc L) {
4148 if (!TheCondStack.empty()) {
4149 if (TheCondStack.back().Ignore) {
4150 eatToEndOfStatement();
4151 return false;
4152 }
4153 }
4154
4155 StringRef Message = ".warning directive invoked in source file";
4156 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4157 if (Lexer.isNot(AsmToken::String)) {
4158 TokError(".warning argument must be a string");
4159 eatToEndOfStatement();
4160 return true;
4161 }
4162
4163 Message = getTok().getStringContents();
4164 Lex();
4165 }
4166
4167 Warning(L, Message);
4168 return false;
4169}
4170
Jim Grosbach4b905842013-09-20 23:08:21 +00004171/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004172/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004173bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004174 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004175 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004176
Sean Callanan686ed8d2010-01-19 20:22:31 +00004177 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004178
Jim Grosbach4b905842013-09-20 23:08:21 +00004179 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004180 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4181 ".else");
4182 if (!TheCondStack.empty()) {
4183 TheCondState = TheCondStack.back();
4184 TheCondStack.pop_back();
4185 }
4186
4187 return false;
4188}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004189
Eli Bendersky17233942013-01-15 22:59:42 +00004190void AsmParser::initializeDirectiveKindMap() {
4191 DirectiveKindMap[".set"] = DK_SET;
4192 DirectiveKindMap[".equ"] = DK_EQU;
4193 DirectiveKindMap[".equiv"] = DK_EQUIV;
4194 DirectiveKindMap[".ascii"] = DK_ASCII;
4195 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4196 DirectiveKindMap[".string"] = DK_STRING;
4197 DirectiveKindMap[".byte"] = DK_BYTE;
4198 DirectiveKindMap[".short"] = DK_SHORT;
4199 DirectiveKindMap[".value"] = DK_VALUE;
4200 DirectiveKindMap[".2byte"] = DK_2BYTE;
4201 DirectiveKindMap[".long"] = DK_LONG;
4202 DirectiveKindMap[".int"] = DK_INT;
4203 DirectiveKindMap[".4byte"] = DK_4BYTE;
4204 DirectiveKindMap[".quad"] = DK_QUAD;
4205 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004206 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004207 DirectiveKindMap[".single"] = DK_SINGLE;
4208 DirectiveKindMap[".float"] = DK_FLOAT;
4209 DirectiveKindMap[".double"] = DK_DOUBLE;
4210 DirectiveKindMap[".align"] = DK_ALIGN;
4211 DirectiveKindMap[".align32"] = DK_ALIGN32;
4212 DirectiveKindMap[".balign"] = DK_BALIGN;
4213 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4214 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4215 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4216 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4217 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4218 DirectiveKindMap[".org"] = DK_ORG;
4219 DirectiveKindMap[".fill"] = DK_FILL;
4220 DirectiveKindMap[".zero"] = DK_ZERO;
4221 DirectiveKindMap[".extern"] = DK_EXTERN;
4222 DirectiveKindMap[".globl"] = DK_GLOBL;
4223 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004224 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4225 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4226 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4227 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4228 DirectiveKindMap[".reference"] = DK_REFERENCE;
4229 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4230 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4231 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4232 DirectiveKindMap[".comm"] = DK_COMM;
4233 DirectiveKindMap[".common"] = DK_COMMON;
4234 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4235 DirectiveKindMap[".abort"] = DK_ABORT;
4236 DirectiveKindMap[".include"] = DK_INCLUDE;
4237 DirectiveKindMap[".incbin"] = DK_INCBIN;
4238 DirectiveKindMap[".code16"] = DK_CODE16;
4239 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4240 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004241 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004242 DirectiveKindMap[".irp"] = DK_IRP;
4243 DirectiveKindMap[".irpc"] = DK_IRPC;
4244 DirectiveKindMap[".endr"] = DK_ENDR;
4245 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4246 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4247 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4248 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004249 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4250 DirectiveKindMap[".ifge"] = DK_IFGE;
4251 DirectiveKindMap[".ifgt"] = DK_IFGT;
4252 DirectiveKindMap[".ifle"] = DK_IFLE;
4253 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004254 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004255 DirectiveKindMap[".ifb"] = DK_IFB;
4256 DirectiveKindMap[".ifnb"] = DK_IFNB;
4257 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004258 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004259 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004260 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004261 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4262 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4263 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4264 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4265 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004266 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004267 DirectiveKindMap[".endif"] = DK_ENDIF;
4268 DirectiveKindMap[".skip"] = DK_SKIP;
4269 DirectiveKindMap[".space"] = DK_SPACE;
4270 DirectiveKindMap[".file"] = DK_FILE;
4271 DirectiveKindMap[".line"] = DK_LINE;
4272 DirectiveKindMap[".loc"] = DK_LOC;
4273 DirectiveKindMap[".stabs"] = DK_STABS;
4274 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4275 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4276 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4277 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4278 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4279 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4280 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4281 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4282 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4283 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4284 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4285 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4286 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4287 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4288 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4289 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4290 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4291 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4292 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4293 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4294 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004295 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004296 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4297 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4298 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004299 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004300 DirectiveKindMap[".endm"] = DK_ENDM;
4301 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4302 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004303 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004304 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004305 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004306}
4307
Jim Grosbach4b905842013-09-20 23:08:21 +00004308MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004310
Rafael Espindola34b9c512012-06-03 23:57:14 +00004311 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004312 for (;;) {
4313 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004314 if (getLexer().is(AsmToken::Eof)) {
4315 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004316 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004317 }
4318
Rafael Espindola34b9c512012-06-03 23:57:14 +00004319 if (Lexer.is(AsmToken::Identifier) &&
4320 (getTok().getIdentifier() == ".rept")) {
4321 ++NestLevel;
4322 }
4323
4324 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004325 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004326 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004327 EndToken = getTok();
4328 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004329 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4330 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004331 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004332 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004333 break;
4334 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004335 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004336 }
4337
Rafael Espindola34b9c512012-06-03 23:57:14 +00004338 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004339 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004340 }
4341
4342 const char *BodyStart = StartToken.getLoc().getPointer();
4343 const char *BodyEnd = EndToken.getLoc().getPointer();
4344 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4345
Rafael Espindola34b9c512012-06-03 23:57:14 +00004346 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004347 MacroLikeBodies.push_back(
4348 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004349 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004350}
4351
Jim Grosbach4b905842013-09-20 23:08:21 +00004352void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004353 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004354 OS << ".endr\n";
4355
Rafael Espindola3560ff22014-08-27 20:03:13 +00004356 std::unique_ptr<MemoryBuffer> Instantiation =
4357 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004358
Rafael Espindola34b9c512012-06-03 23:57:14 +00004359 // Create the macro instantiation object and add to the current macro
4360 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004361 MacroInstantiation *MI = new MacroInstantiation(
4362 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004363 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004364
Rafael Espindola34b9c512012-06-03 23:57:14 +00004365 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004366 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004367 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004368 Lex();
4369}
4370
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004371/// parseDirectiveRept
4372/// ::= .rep | .rept count
4373bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004374 const MCExpr *CountExpr;
4375 SMLoc CountLoc = getTok().getLoc();
4376 if (parseExpression(CountExpr))
4377 return true;
4378
Rafael Espindola34b9c512012-06-03 23:57:14 +00004379 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004380 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4381 eatToEndOfStatement();
4382 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4383 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004384
4385 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004386 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004387
4388 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004389 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004390
4391 // Eat the end of statement.
4392 Lex();
4393
4394 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004395 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004396 if (!M)
4397 return true;
4398
4399 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4400 // to hold the macro body with substitutions.
4401 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004402 raw_svector_ostream OS(Buf);
4403 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004404 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4405 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004406 return true;
4407 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004408 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004409
4410 return false;
4411}
4412
Jim Grosbach4b905842013-09-20 23:08:21 +00004413/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004414/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004415bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004416 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004417
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004418 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004419 return TokError("expected identifier in '.irp' directive");
4420
Rafael Espindola768b41c2012-06-15 14:02:34 +00004421 if (Lexer.isNot(AsmToken::Comma))
4422 return TokError("expected comma in '.irp' directive");
4423
4424 Lex();
4425
Eli Bendersky38274122013-01-14 23:22:36 +00004426 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004427 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004428 return true;
4429
4430 // Eat the end of statement.
4431 Lex();
4432
4433 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004434 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004435 if (!M)
4436 return true;
4437
4438 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4439 // to hold the macro body with substitutions.
4440 SmallString<256> Buf;
4441 raw_svector_ostream OS(Buf);
4442
Eli Bendersky38274122013-01-14 23:22:36 +00004443 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004444 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4445 // This is undocumented, but GAS seems to support it.
4446 if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004447 return true;
4448 }
4449
Jim Grosbach4b905842013-09-20 23:08:21 +00004450 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004451
4452 return false;
4453}
4454
Jim Grosbach4b905842013-09-20 23:08:21 +00004455/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004456/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004457bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004458 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004459
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004460 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004461 return TokError("expected identifier in '.irpc' directive");
4462
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004463 if (Lexer.isNot(AsmToken::Comma))
4464 return TokError("expected comma in '.irpc' directive");
4465
4466 Lex();
4467
Eli Bendersky38274122013-01-14 23:22:36 +00004468 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004469 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004470 return true;
4471
4472 if (A.size() != 1 || A.front().size() != 1)
4473 return TokError("unexpected token in '.irpc' directive");
4474
4475 // Eat the end of statement.
4476 Lex();
4477
4478 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004479 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004480 if (!M)
4481 return true;
4482
4483 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4484 // to hold the macro body with substitutions.
4485 SmallString<256> Buf;
4486 raw_svector_ostream OS(Buf);
4487
4488 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004489 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004490 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004491 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004492
Toma Tabacu217116e2015-04-27 10:50:29 +00004493 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4494 // This is undocumented, but GAS seems to support it.
4495 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004496 return true;
4497 }
4498
Jim Grosbach4b905842013-09-20 23:08:21 +00004499 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004500
4501 return false;
4502}
4503
Jim Grosbach4b905842013-09-20 23:08:21 +00004504bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004505 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004506 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004507
4508 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004509 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004510 assert(getLexer().is(AsmToken::EndOfStatement));
4511
Jim Grosbach4b905842013-09-20 23:08:21 +00004512 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004513 return false;
4514}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004515
Jim Grosbach4b905842013-09-20 23:08:21 +00004516bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004517 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004518 const MCExpr *Value;
4519 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004520 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004521 return true;
4522 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4523 if (!MCE)
4524 return Error(ExprLoc, "unexpected expression in _emit");
4525 uint64_t IntValue = MCE->getValue();
4526 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4527 return Error(ExprLoc, "literal value out of range for directive");
4528
Chad Rosierc7f552c2013-02-12 21:33:51 +00004529 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4530 return false;
4531}
4532
Jim Grosbach4b905842013-09-20 23:08:21 +00004533bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004534 const MCExpr *Value;
4535 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004536 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004537 return true;
4538 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4539 if (!MCE)
4540 return Error(ExprLoc, "unexpected expression in align");
4541 uint64_t IntValue = MCE->getValue();
4542 if (!isPowerOf2_64(IntValue))
4543 return Error(ExprLoc, "literal value not a power of two greater then zero");
4544
Jim Grosbach4b905842013-09-20 23:08:21 +00004545 Info.AsmRewrites->push_back(
4546 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004547 return false;
4548}
4549
Chad Rosierf43fcf52013-02-13 21:27:17 +00004550// We are comparing pointers, but the pointers are relative to a single string.
4551// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004552static int rewritesSort(const AsmRewrite *AsmRewriteA,
4553 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004554 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4555 return -1;
4556 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4557 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004558
Chad Rosierfce4fab2013-04-08 17:43:47 +00004559 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4560 // rewrite to the same location. Make sure the SizeDirective rewrite is
4561 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4562 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004563 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4564 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004565 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004566
Jim Grosbach4b905842013-09-20 23:08:21 +00004567 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4568 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004569 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004570 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004571}
4572
Jim Grosbach4b905842013-09-20 23:08:21 +00004573bool AsmParser::parseMSInlineAsm(
4574 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4575 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4576 SmallVectorImpl<std::string> &Constraints,
4577 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4578 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004579 SmallVector<void *, 4> InputDecls;
4580 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004581 SmallVector<bool, 4> InputDeclsAddressOf;
4582 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004583 SmallVector<std::string, 4> InputConstraints;
4584 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004585 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004586
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004588
4589 // Prime the lexer.
4590 Lex();
4591
4592 // While we have input, parse each statement.
4593 unsigned InputIdx = 0;
4594 unsigned OutputIdx = 0;
4595 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004596 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004597 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004598 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004599
Chad Rosier149e8e02012-12-12 22:45:52 +00004600 if (Info.ParseError)
4601 return true;
4602
Benjamin Kramer1a136112013-02-15 20:37:21 +00004603 if (Info.Opcode == ~0U)
4604 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004605
Benjamin Kramer1a136112013-02-15 20:37:21 +00004606 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004607
Benjamin Kramer1a136112013-02-15 20:37:21 +00004608 // Build the list of clobbers, outputs and inputs.
4609 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004610 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004611
Benjamin Kramer1a136112013-02-15 20:37:21 +00004612 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004613 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004614 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004615
Benjamin Kramer1a136112013-02-15 20:37:21 +00004616 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004617 if (Operand.isReg() && !Operand.needAddressOf() &&
4618 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004619 unsigned NumDefs = Desc.getNumDefs();
4620 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004621 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4622 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004623 continue;
4624 }
4625
4626 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004627 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004628 if (SymName.empty())
4629 continue;
4630
David Blaikie960ea3f2014-06-08 16:18:35 +00004631 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004632 if (!OpDecl)
4633 continue;
4634
4635 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004636 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004637 if (isOutput) {
4638 ++InputIdx;
4639 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004640 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004641 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004642 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004643 } else {
4644 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004645 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4646 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004647 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004648 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004649 }
Reid Kleckneree088972013-12-10 18:27:32 +00004650
4651 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004652 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4653 Desc.getNumImplicitDefs());
4654 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004655 }
4656
4657 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004658 NumOutputs = OutputDecls.size();
4659 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004660
4661 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004662 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4663 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4664 ClobberRegs.end());
4665 Clobbers.assign(ClobberRegs.size(), std::string());
4666 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4667 raw_string_ostream OS(Clobbers[I]);
4668 IP->printRegName(OS, ClobberRegs[I]);
4669 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004670
4671 // Merge the various outputs and inputs. Output are expected first.
4672 if (NumOutputs || NumInputs) {
4673 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004674 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004675 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004676 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004677 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004678 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004679 }
4680 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004681 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004682 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004683 }
4684 }
4685
4686 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004687 std::string AsmStringIR;
4688 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004689 StringRef ASMString =
4690 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4691 const char *AsmStart = ASMString.begin();
4692 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004693 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004694 for (const AsmRewrite &AR : AsmStrRewrites) {
4695 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004696 if (Kind == AOK_Delete)
4697 continue;
4698
David Majnemer8114c1a2014-06-23 02:17:16 +00004699 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004700 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004701
Chad Rosier120eefd2013-03-19 17:32:17 +00004702 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004703 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004704 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004705
Chad Rosier37e755c2012-10-23 17:43:43 +00004706 // Skip the original expression.
4707 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004708 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004709 continue;
4710 }
4711
Chad Rosierff10ed12013-04-12 16:26:42 +00004712 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004714 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004715 default:
4716 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004717 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004718 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004719 break;
4720 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004721 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004722 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004723 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004724 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004725 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004726 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004727 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004728 break;
4729 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004730 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004731 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004732 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004733 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004734 default: break;
4735 case 8: OS << "byte ptr "; break;
4736 case 16: OS << "word ptr "; break;
4737 case 32: OS << "dword ptr "; break;
4738 case 64: OS << "qword ptr "; break;
4739 case 80: OS << "xword ptr "; break;
4740 case 128: OS << "xmmword ptr "; break;
4741 case 256: OS << "ymmword ptr "; break;
4742 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004743 break;
4744 case AOK_Emit:
4745 OS << ".byte";
4746 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004747 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004748 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004749 OS << ".align " << Val;
4750
4751 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004752 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004753 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4754 break;
4755 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004756 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004757 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004758 OS.flush();
4759 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004760 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004761 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004762 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004763 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004764
Chad Rosier8bce6642012-10-18 15:49:34 +00004765 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004766 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004767 }
4768
4769 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004770 if (AsmStart != AsmEnd)
4771 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004772
4773 AsmString = OS.str();
4774 return false;
4775}
4776
Daniel Dunbar01e36072010-07-17 02:26:10 +00004777/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004778MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4779 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004780 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004781}