blob: fe8b8844366a733701239e99361bd4b0b5e081d4 [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
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000334
Eli Bendersky17233942013-01-15 22:59:42 +0000335 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000336 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
350 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000358 DK_MACROS_ON, DK_MACROS_OFF,
359 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000360 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000361 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000362 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000363 };
364
Jim Grosbach4b905842013-09-20 23:08:21 +0000365 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000366 /// directives parsed by this class.
367 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000368
369 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
371 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000372 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
374 bool parseDirectiveFill(); // ".fill"
375 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000376 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
378 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000379 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000381
Eli Bendersky17233942013-01-15 22:59:42 +0000382 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveFile(SMLoc DirectiveLoc);
384 bool parseDirectiveLine();
385 bool parseDirectiveLoc();
386 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000387
388 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000390 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000391 bool parseDirectiveCFISections();
392 bool parseDirectiveCFIStartProc();
393 bool parseDirectiveCFIEndProc();
394 bool parseDirectiveCFIDefCfaOffset();
395 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIAdjustCfaOffset();
397 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
401 bool parseDirectiveCFIRememberState();
402 bool parseDirectiveCFIRestoreState();
403 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIEscape();
406 bool parseDirectiveCFISignalFrame();
407 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
409 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000411 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveEndMacro(StringRef Directive);
413 bool parseDirectiveMacro(SMLoc DirectiveLoc);
414 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000415
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422
Eli Bendersky17233942013-01-15 22:59:42 +0000423 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
426 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveAbort(); // ".abort"
436 bool parseDirectiveInclude(); // ".include"
437 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000439 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
440 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000441 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000443 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000445 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
446 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000447 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
449 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
450 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
451 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000452 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000455 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000456
Rafael Espindola34b9c512012-06-03 23:57:14 +0000457 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
459 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000460 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000461 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
463 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
464 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000465
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000468 size_t Len);
469
470 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000472
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000473 // "end"
474 bool parseDirectiveEnd(SMLoc DirectiveLoc);
475
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000476 // ".err" or ".error"
477 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000478
Nico Weber404012b2014-07-24 16:26:06 +0000479 // ".warning"
480 bool parseDirectiveWarning(SMLoc DirectiveLoc);
481
Eli Bendersky17233942013-01-15 22:59:42 +0000482 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000483};
Daniel Dunbar86033402010-07-12 17:54:38 +0000484}
485
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486namespace llvm {
487
488extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000489extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000490extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000491
492}
493
Chris Lattnerc35681b2010-01-19 19:46:13 +0000494enum { DEFAULT_ADDRSPACE = 0 };
495
David Blaikie9f380a32015-03-16 18:06:57 +0000496AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
497 const MCAsmInfo &MAI)
498 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
499 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000500 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000501 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000502 // Save the old handler.
503 SavedDiagHandler = SrcMgr.getDiagHandler();
504 SavedDiagContext = SrcMgr.getDiagContext();
505 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000506 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000507 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000508
Daniel Dunbarc5011082010-07-12 18:12:02 +0000509 // Initialize the platform / file format parser.
David Blaikie9f380a32015-03-16 18:06:57 +0000510 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000512 PlatformParser.reset(createCOFFAsmParser());
513 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000514 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000515 PlatformParser.reset(createDarwinAsmParser());
516 IsDarwin = true;
517 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000518 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000519 PlatformParser.reset(createELFAsmParser());
520 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000521 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000522
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000523 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000524 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000525
526 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000527}
528
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000529AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000530 assert((HadError || ActiveMacros.empty()) &&
531 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000532}
533
Jim Grosbach4b905842013-09-20 23:08:21 +0000534void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000535 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000536 for (std::vector<MacroInstantiation *>::const_reverse_iterator
537 it = ActiveMacros.rbegin(),
538 ie = ActiveMacros.rend();
539 it != ie; ++it)
540 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000541 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000542}
543
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000544void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
545 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
546 printMacroInstantiations();
547}
548
Chris Lattnera3a06812011-10-16 04:47:35 +0000549bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000550 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000551 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000552 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
553 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000554 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000555}
556
Chris Lattnera3a06812011-10-16 04:47:35 +0000557bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000558 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000559 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
560 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000561 return true;
562}
563
Jim Grosbach4b905842013-09-20 23:08:21 +0000564bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000565 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000566 unsigned NewBuf =
567 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
568 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000569 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000570
Sean Callanan7a77eae2010-01-21 00:19:58 +0000571 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000572 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 return false;
574}
Daniel Dunbar43235712010-07-18 18:54:11 +0000575
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000576/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000577/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000578/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000579bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000580 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000581 unsigned NewBuf =
582 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
583 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 return true;
585
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000586 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000587 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588 return false;
589}
590
Alp Tokera55b95b2014-07-06 10:33:31 +0000591void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
592 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000593 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
594 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000595}
596
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597const AsmToken &AsmParser::Lex() {
598 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 if (tok->is(AsmToken::Eof)) {
601 // If this is the end of an included file, pop the parent file off the
602 // include stack.
603 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
604 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000605 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 tok = &Lexer.Lex();
607 }
608 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000609
Sean Callanan7a77eae2010-01-21 00:19:58 +0000610 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000611 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000614}
615
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000616bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000617 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000618 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000619 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000620
Chris Lattner36e02122009-06-21 20:54:55 +0000621 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000622 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000623
624 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000625 AsmCond StartingCondState = TheCondState;
626
Kevin Enderby6469fc22011-11-01 22:27:22 +0000627 // If we are generating dwarf for assembly source files save the initial text
628 // section and generate a .file directive.
629 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000630 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
631 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000632 auto InsertResult = getContext().addGenDwarfSection(
633 getStreamer().getCurrentSection().first);
634 assert(InsertResult.second && ".text section should not have debug info yet");
635 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000636 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
637 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000638 }
639
Chris Lattner73f36112009-07-02 21:53:43 +0000640 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000641 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000642 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000643 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000644 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000645
Daniel Dunbar43325c42010-09-09 22:42:56 +0000646 // We had an error, validate that one was emitted and recover by skipping to
647 // the next line.
648 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000649 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000650 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000651
652 if (TheCondState.TheCond != StartingCondState.TheCond ||
653 TheCondState.Ignore != StartingCondState.Ignore)
654 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000655
656 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000657 const auto &LineTables = getContext().getMCDwarfLineTables();
658 if (!LineTables.empty()) {
659 unsigned Index = 0;
660 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
661 if (File.Name.empty() && Index != 0)
662 TokError("unassigned file number: " + Twine(Index) +
663 " for .file directives");
664 ++Index;
665 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000667
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000668 // Check to see that all assembler local symbols were actually defined.
669 // Targets that don't do subsections via symbols may not want this, though,
670 // so conservatively exclude them. Only do this if we're finalizing, though,
671 // as otherwise we won't necessarilly have seen everything yet.
672 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
673 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
674 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000675 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000676 i != e; ++i) {
677 MCSymbol *Sym = i->getValue();
678 // Variable symbols may not be marked as defined, so check those
679 // explicitly. If we know it's a variable, we have a definition for
680 // the purposes of this check.
681 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
682 // FIXME: We would really like to refer back to where the symbol was
683 // first referenced for a source location. We need to add something
684 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000685 printMessage(
686 getLexer().getLoc(), SourceMgr::DK_Error,
687 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000688 }
689 }
690
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000691 // Finalize the output stream if there are no errors and if the client wants
692 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000693 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000694 Out.Finish();
695
Chris Lattner73f36112009-07-02 21:53:43 +0000696 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000697}
698
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000699void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000700 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000701 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000702 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000703 }
704}
705
Jim Grosbach4b905842013-09-20 23:08:21 +0000706/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000708 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000709 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000710
Chris Lattnere5074c42009-06-22 01:29:09 +0000711 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000712 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000714}
715
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000716StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000717 const char *Start = getTok().getLoc().getPointer();
718
Jim Grosbach4b905842013-09-20 23:08:21 +0000719 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 Lex();
721
722 const char *End = getTok().getLoc().getPointer();
723 return StringRef(Start, End - Start);
724}
Chris Lattner78db3622009-06-22 05:51:26 +0000725
Jim Grosbach4b905842013-09-20 23:08:21 +0000726StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000727 const char *Start = getTok().getLoc().getPointer();
728
729 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000730 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 Lex();
732
733 const char *End = getTok().getLoc().getPointer();
734 return StringRef(Start, End - Start);
735}
736
Jim Grosbach4b905842013-09-20 23:08:21 +0000737/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000738/// NOTE: This assumes the leading '(' has already been consumed.
739///
740/// parenexpr ::= expr)
741///
Jim Grosbach4b905842013-09-20 23:08:21 +0000742bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
743 if (parseExpression(Res))
744 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000745 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000747 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000748 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return false;
750}
Chris Lattner78db3622009-06-22 05:51:26 +0000751
Jim Grosbach4b905842013-09-20 23:08:21 +0000752/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000753/// NOTE: This assumes the leading '[' has already been consumed.
754///
755/// bracketexpr ::= expr]
756///
Jim Grosbach4b905842013-09-20 23:08:21 +0000757bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
758 if (parseExpression(Res))
759 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000760 if (Lexer.isNot(AsmToken::RBrac))
761 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000762 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 Lex();
764 return false;
765}
766
Jim Grosbach4b905842013-09-20 23:08:21 +0000767/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000768/// primaryexpr ::= (parenexpr
769/// primaryexpr ::= symbol
770/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000771/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000773bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000774 SMLoc FirstTokenLoc = getLexer().getLoc();
775 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
776 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000777 default:
778 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000779 // If we have an error assume that we've already handled it.
780 case AsmToken::Error:
781 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000782 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000783 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000784 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000785 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000786 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000787 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000788 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000789 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000790 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000791 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000793 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000794 if (FirstTokenKind == AsmToken::Dollar) {
795 if (Lexer.getMAI().getDollarIsPC()) {
796 // This is a '$' reference, which references the current PC. Emit a
797 // temporary label to the streamer and refer to it.
798 MCSymbol *Sym = Ctx.CreateTempSymbol();
799 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000800 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
801 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000802 EndLoc = FirstTokenLoc;
803 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000804 }
805 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000807 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000808 // Parse symbol variant
809 std::pair<StringRef, StringRef> Split;
810 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000811 if (FirstTokenKind == AsmToken::String) {
812 if (Lexer.is(AsmToken::At)) {
813 Lexer.Lex(); // eat @
814 SMLoc AtLoc = getLexer().getLoc();
815 StringRef VName;
816 if (parseIdentifier(VName))
817 return Error(AtLoc, "expected symbol variant after '@'");
818
819 Split = std::make_pair(Identifier, VName);
820 }
821 } else {
822 Split = Identifier.split('@');
823 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000824 } else if (Lexer.is(AsmToken::LParen)) {
825 Lexer.Lex(); // eat (
826 StringRef VName;
827 parseIdentifier(VName);
828 if (Lexer.isNot(AsmToken::RParen)) {
829 return Error(Lexer.getTok().getLoc(),
830 "unexpected token in variant, expected ')'");
831 }
832 Lexer.Lex(); // eat )
833 Split = std::make_pair(Identifier, VName);
834 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000835
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000836 EndLoc = SMLoc::getFromPointer(Identifier.end());
837
Daniel Dunbard20cda02009-10-16 01:34:54 +0000838 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 StringRef SymbolName = Identifier;
840 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000841
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000842 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000843 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000844 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 if (Variant != MCSymbolRefExpr::VK_Invalid) {
846 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000847 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000848 Variant = MCSymbolRefExpr::VK_None;
849 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000850 return Error(SMLoc::getFromPointer(Split.second.begin()),
851 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000852 }
853 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000854
Hans Wennborgce69d772013-10-18 20:46:28 +0000855 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
856
Daniel Dunbard20cda02009-10-16 01:34:54 +0000857 // If this is an absolute variable reference, substitute it now to preserve
858 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000859 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000860 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000861 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000862
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000863 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000864 return false;
865 }
866
867 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000868 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000869 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000870 }
David Woodhousef42a6662014-02-01 16:20:54 +0000871 case AsmToken::BigNum:
872 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000873 case AsmToken::Integer: {
874 SMLoc Loc = getTok().getLoc();
875 int64_t IntVal = getTok().getIntVal();
876 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000877 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000878 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000879 // Look for 'b' or 'f' following an Integer as a directional label
880 if (Lexer.getKind() == AsmToken::Identifier) {
881 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 // Lookup the symbol variant if used.
883 std::pair<StringRef, StringRef> Split = IDVal.split('@');
884 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
885 if (Split.first.size() != IDVal.size()) {
886 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000887 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000889 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000891 if (IDVal == "f" || IDVal == "b") {
892 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000893 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000895 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000896 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000897 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000898 Lex(); // Eat identifier.
899 }
900 }
Chris Lattner78db3622009-06-22 05:51:26 +0000901 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 case AsmToken::Real: {
904 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000905 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000906 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000907 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000908 Lex(); // Eat token.
909 return false;
910 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000911 case AsmToken::Dot: {
912 // This is a '.' reference, which references the current PC. Emit a
913 // temporary label to the streamer and refer to it.
914 MCSymbol *Sym = Ctx.CreateTempSymbol();
915 Out.EmitLabel(Sym);
916 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000917 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000918 Lex(); // Eat identifier.
919 return false;
920 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000924 case AsmToken::LBrac:
925 if (!PlatformParser->HasBracketExpressions())
926 return TokError("brackets expression not supported on this target");
927 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000933 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000945 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000947 }
948}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000949
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000950bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000951 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000952 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000953}
954
Daniel Dunbar55f16672010-09-17 02:47:07 +0000955const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000956AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000958 // Ask the target implementation about this expression first.
959 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
960 if (NewE)
961 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000962 // Recurse over the given expression, rebuilding it to apply the given variant
963 // if there is exactly one symbol.
964 switch (E->getKind()) {
965 case MCExpr::Target:
966 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000967 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968
969 case MCExpr::SymbolRef: {
970 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
971
972 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000973 TokError("invalid variant on expression '" + getTok().getIdentifier() +
974 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975 return E;
976 }
977
978 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
979 }
980
981 case MCExpr::Unary: {
982 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000985 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
987 }
988
989 case MCExpr::Binary: {
990 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000991 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
992 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993
994 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000995 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 if (!LHS)
998 LHS = BE->getLHS();
999 if (!RHS)
1000 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001
1002 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1003 }
1004 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001005
Craig Toppera2886c22012-02-07 05:05:23 +00001006 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007}
1008
Jim Grosbach4b905842013-09-20 23:08:21 +00001009/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001010///
Jim Grosbachbd164242011-08-20 16:24:13 +00001011/// expr ::= expr &&,|| expr -> lowest.
1012/// expr ::= expr |,^,&,! expr
1013/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1014/// expr ::= expr <<,>> expr
1015/// expr ::= expr +,- expr
1016/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001017/// expr ::= primaryexpr
1018///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001019bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001020 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001021 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001022 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001023 return true;
1024
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 // As a special case, we support 'a op b @ modifier' by rewriting the
1026 // expression to include the modifier. This is inefficient, but in general we
1027 // expect users to use 'a@modifier op b'.
1028 if (Lexer.getKind() == AsmToken::At) {
1029 Lex();
1030
1031 if (Lexer.isNot(AsmToken::Identifier))
1032 return TokError("unexpected symbol modifier following '@'");
1033
1034 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001035 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 if (Variant == MCSymbolRefExpr::VK_Invalid)
1037 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1038
Jim Grosbach4b905842013-09-20 23:08:21 +00001039 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 if (!ModifiedRes) {
1041 return TokError("invalid modifier '" + getTok().getIdentifier() +
1042 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001043 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001044
Daniel Dunbar55f16672010-09-17 02:47:07 +00001045 Res = ModifiedRes;
1046 Lex();
1047 }
1048
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001049 // Try to constant fold it up front, if possible.
1050 int64_t Value;
1051 if (Res->EvaluateAsAbsolute(Value))
1052 Res = MCConstantExpr::Create(Value, getContext());
1053
1054 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001055}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001056
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001058 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001059 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001060}
1061
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001062bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001063 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001064
Daniel Dunbar75630b32009-06-30 02:10:03 +00001065 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001066 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001067 return true;
1068
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001069 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001070 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071
1072 return false;
1073}
1074
Michael J. Spencer530ce852010-10-09 11:00:50 +00001075static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001076 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001077 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001078 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001079 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001080
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001082 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001083 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001084 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001085 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001087 return 1;
1088
Jim Grosbach4b905842013-09-20 23:08:21 +00001089 // Low Precedence: |, &, ^
1090 //
1091 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001092 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001095 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001101
Jim Grosbach4b905842013-09-20 23:08:21 +00001102 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 case AsmToken::EqualEqual:
1104 Kind = MCBinaryExpr::EQ;
1105 return 3;
1106 case AsmToken::ExclaimEqual:
1107 case AsmToken::LessGreater:
1108 Kind = MCBinaryExpr::NE;
1109 return 3;
1110 case AsmToken::Less:
1111 Kind = MCBinaryExpr::LT;
1112 return 3;
1113 case AsmToken::LessEqual:
1114 Kind = MCBinaryExpr::LTE;
1115 return 3;
1116 case AsmToken::Greater:
1117 Kind = MCBinaryExpr::GT;
1118 return 3;
1119 case AsmToken::GreaterEqual:
1120 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001121 return 3;
1122
Jim Grosbach4b905842013-09-20 23:08:21 +00001123 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001124 case AsmToken::LessLess:
1125 Kind = MCBinaryExpr::Shl;
1126 return 4;
1127 case AsmToken::GreaterGreater:
1128 Kind = MCBinaryExpr::Shr;
1129 return 4;
1130
Jim Grosbach4b905842013-09-20 23:08:21 +00001131 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001132 case AsmToken::Plus:
1133 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001135 case AsmToken::Minus:
1136 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138
Jim Grosbach4b905842013-09-20 23:08:21 +00001139 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001140 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001141 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001143 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001144 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 }
1150}
1151
Jim Grosbach4b905842013-09-20 23:08:21 +00001152/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001154bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001155 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001157 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001158 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 // If the next token is lower precedence than we are allowed to eat, return
1161 // successfully with what we ate already.
1162 if (TokPrec < Precedence)
1163 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001164
Sean Callanan686ed8d2010-01-19 20:22:31 +00001165 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001168 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001169 if (parsePrimaryExpr(RHS, EndLoc))
1170 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001171
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 // If BinOp binds less tightly with RHS than the operator after RHS, let
1173 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001174 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001175 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001176 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1177 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001179 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001180 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001181 }
1182}
1183
Chris Lattner36e02122009-06-21 20:54:55 +00001184/// ParseStatement:
1185/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001186/// ::= Label* Directive ...Operands... EndOfStatement
1187/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001188bool AsmParser::parseStatement(ParseStatementInfo &Info,
1189 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001191 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001192 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001193 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001194 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001197 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001198 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001199 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001200 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001201 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001202 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001203 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001204
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001205 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 if (Lexer.is(AsmToken::Integer)) {
1207 LocalLabelVal = getTok().getIntVal();
1208 if (LocalLabelVal < 0) {
1209 if (!TheCondState.Ignore)
1210 return TokError("unexpected token at start of statement");
1211 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001212 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 IDVal = getTok().getString();
1214 Lex(); // Consume the integer token to be used as an identifier token.
1215 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001218 }
1219 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001220 } else if (Lexer.is(AsmToken::Dot)) {
1221 // Treat '.' as a valid identifier in this context.
1222 Lex();
1223 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001224 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001225 if (!TheCondState.Ignore)
1226 return TokError("unexpected token at start of statement");
1227 IDVal = "";
1228 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001229
Chris Lattner926885c2010-04-17 18:14:27 +00001230 // Handle conditional assembly here before checking for skipping. We
1231 // have to do this so that .endif isn't skipped in a ".if 0" block for
1232 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001233 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001234 DirectiveKindMap.find(IDVal);
1235 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1236 ? DK_NO_DIRECTIVE
1237 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001238 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001239 default:
1240 break;
1241 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001242 case DK_IFEQ:
1243 case DK_IFGE:
1244 case DK_IFGT:
1245 case DK_IFLE:
1246 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001247 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001248 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001249 case DK_IFB:
1250 return parseDirectiveIfb(IDLoc, true);
1251 case DK_IFNB:
1252 return parseDirectiveIfb(IDLoc, false);
1253 case DK_IFC:
1254 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001255 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001256 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001257 case DK_IFNC:
1258 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001259 case DK_IFNES:
1260 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001261 case DK_IFDEF:
1262 return parseDirectiveIfdef(IDLoc, true);
1263 case DK_IFNDEF:
1264 case DK_IFNOTDEF:
1265 return parseDirectiveIfdef(IDLoc, false);
1266 case DK_ELSEIF:
1267 return parseDirectiveElseIf(IDLoc);
1268 case DK_ELSE:
1269 return parseDirectiveElse(IDLoc);
1270 case DK_ENDIF:
1271 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001272 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001273
Eli Bendersky88024712013-01-16 19:32:36 +00001274 // Ignore the statement if in the middle of inactive conditional
1275 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001276 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001277 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001278 return false;
1279 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001280
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001281 // FIXME: Recurse on local labels?
1282
1283 // See what kind of statement we have.
1284 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001285 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001286 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001287
Chris Lattner36e02122009-06-21 20:54:55 +00001288 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001289 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001290
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001291 // Diagnose attempt to use '.' as a label.
1292 if (IDVal == ".")
1293 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1294
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001295 // Diagnose attempt to use a variable as a label.
1296 //
1297 // FIXME: Diagnostics. Note the location of the definition as a label.
1298 // FIXME: This doesn't diagnose assignment to a symbol which has been
1299 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001300 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001301 if (LocalLabelVal == -1) {
1302 if (ParsingInlineAsm && SI) {
1303 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1304 assert(RewrittenLabel.size() && "We should have an internal name here.");
1305 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1306 IDVal.size(), RewrittenLabel));
1307 IDVal = RewrittenLabel;
1308 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001309 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001310 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001311 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001312
1313 Sym->redefineIfPossible();
1314
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001315 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001316 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001317
Daniel Dunbare73b2672009-08-26 22:13:22 +00001318 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001319 if (!ParsingInlineAsm)
1320 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001321
Kevin Enderbye7739d42011-12-09 18:09:40 +00001322 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001323 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001324 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001325 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1326 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001327
Tim Northover1744d0a2013-10-25 12:49:50 +00001328 getTargetParser().onLabelParsed(Sym);
1329
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001330 // Consume any end of statement token, if present, to avoid spurious
1331 // AddBlankLine calls().
1332 if (Lexer.is(AsmToken::EndOfStatement)) {
1333 Lex();
1334 if (Lexer.is(AsmToken::Eof))
1335 return false;
1336 }
1337
Eli Friedman0f4871d2012-10-22 23:58:19 +00001338 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001339 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001340
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001341 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001342 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001343 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001344
Jim Grosbach4b905842013-09-20 23:08:21 +00001345 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001346
1347 default: // Normal instruction or directive.
1348 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001349 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001350
1351 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001352 if (areMacrosEnabled())
1353 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1354 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001355 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001356
Michael J. Spencer530ce852010-10-09 11:00:50 +00001357 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001358
Eli Bendersky17233942013-01-15 22:59:42 +00001359 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001360 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001361 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001362 //
Eli Bendersky17233942013-01-15 22:59:42 +00001363 // 1. The target-specific assembly parser. Some directives are target
1364 // specific or may potentially behave differently on certain targets.
1365 // 2. Asm parser extensions. For example, platform-specific parsers
1366 // (like the ELF parser) register themselves as extensions.
1367 // 3. The generic directive parser implemented by this class. These are
1368 // all the directives that behave in a target and platform independent
1369 // manner, or at least have a default behavior that's shared between
1370 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001371
Eli Bendersky17233942013-01-15 22:59:42 +00001372 // First query the target-specific parser. It will return 'true' if it
1373 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001374 if (!getTargetParser().ParseDirective(ID))
1375 return false;
1376
Alp Tokercb402912014-01-24 17:20:08 +00001377 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001378 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001379 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1380 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001381 if (Handler.first)
1382 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1383
1384 // Finally, if no one else is interested in this directive, it must be
1385 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001386 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001387 default:
1388 break;
1389 case DK_SET:
1390 case DK_EQU:
1391 return parseDirectiveSet(IDVal, true);
1392 case DK_EQUIV:
1393 return parseDirectiveSet(IDVal, false);
1394 case DK_ASCII:
1395 return parseDirectiveAscii(IDVal, false);
1396 case DK_ASCIZ:
1397 case DK_STRING:
1398 return parseDirectiveAscii(IDVal, true);
1399 case DK_BYTE:
1400 return parseDirectiveValue(1);
1401 case DK_SHORT:
1402 case DK_VALUE:
1403 case DK_2BYTE:
1404 return parseDirectiveValue(2);
1405 case DK_LONG:
1406 case DK_INT:
1407 case DK_4BYTE:
1408 return parseDirectiveValue(4);
1409 case DK_QUAD:
1410 case DK_8BYTE:
1411 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001412 case DK_OCTA:
1413 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001414 case DK_SINGLE:
1415 case DK_FLOAT:
1416 return parseDirectiveRealValue(APFloat::IEEEsingle);
1417 case DK_DOUBLE:
1418 return parseDirectiveRealValue(APFloat::IEEEdouble);
1419 case DK_ALIGN: {
1420 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1421 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1422 }
1423 case DK_ALIGN32: {
1424 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1425 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1426 }
1427 case DK_BALIGN:
1428 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1429 case DK_BALIGNW:
1430 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1431 case DK_BALIGNL:
1432 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1433 case DK_P2ALIGN:
1434 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1435 case DK_P2ALIGNW:
1436 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1437 case DK_P2ALIGNL:
1438 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1439 case DK_ORG:
1440 return parseDirectiveOrg();
1441 case DK_FILL:
1442 return parseDirectiveFill();
1443 case DK_ZERO:
1444 return parseDirectiveZero();
1445 case DK_EXTERN:
1446 eatToEndOfStatement(); // .extern is the default, ignore it.
1447 return false;
1448 case DK_GLOBL:
1449 case DK_GLOBAL:
1450 return parseDirectiveSymbolAttribute(MCSA_Global);
1451 case DK_LAZY_REFERENCE:
1452 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1453 case DK_NO_DEAD_STRIP:
1454 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1455 case DK_SYMBOL_RESOLVER:
1456 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1457 case DK_PRIVATE_EXTERN:
1458 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1459 case DK_REFERENCE:
1460 return parseDirectiveSymbolAttribute(MCSA_Reference);
1461 case DK_WEAK_DEFINITION:
1462 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1463 case DK_WEAK_REFERENCE:
1464 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1465 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1466 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1467 case DK_COMM:
1468 case DK_COMMON:
1469 return parseDirectiveComm(/*IsLocal=*/false);
1470 case DK_LCOMM:
1471 return parseDirectiveComm(/*IsLocal=*/true);
1472 case DK_ABORT:
1473 return parseDirectiveAbort();
1474 case DK_INCLUDE:
1475 return parseDirectiveInclude();
1476 case DK_INCBIN:
1477 return parseDirectiveIncbin();
1478 case DK_CODE16:
1479 case DK_CODE16GCC:
1480 return TokError(Twine(IDVal) + " not supported yet");
1481 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001482 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001483 case DK_IRP:
1484 return parseDirectiveIrp(IDLoc);
1485 case DK_IRPC:
1486 return parseDirectiveIrpc(IDLoc);
1487 case DK_ENDR:
1488 return parseDirectiveEndr(IDLoc);
1489 case DK_BUNDLE_ALIGN_MODE:
1490 return parseDirectiveBundleAlignMode();
1491 case DK_BUNDLE_LOCK:
1492 return parseDirectiveBundleLock();
1493 case DK_BUNDLE_UNLOCK:
1494 return parseDirectiveBundleUnlock();
1495 case DK_SLEB128:
1496 return parseDirectiveLEB128(true);
1497 case DK_ULEB128:
1498 return parseDirectiveLEB128(false);
1499 case DK_SPACE:
1500 case DK_SKIP:
1501 return parseDirectiveSpace(IDVal);
1502 case DK_FILE:
1503 return parseDirectiveFile(IDLoc);
1504 case DK_LINE:
1505 return parseDirectiveLine();
1506 case DK_LOC:
1507 return parseDirectiveLoc();
1508 case DK_STABS:
1509 return parseDirectiveStabs();
1510 case DK_CFI_SECTIONS:
1511 return parseDirectiveCFISections();
1512 case DK_CFI_STARTPROC:
1513 return parseDirectiveCFIStartProc();
1514 case DK_CFI_ENDPROC:
1515 return parseDirectiveCFIEndProc();
1516 case DK_CFI_DEF_CFA:
1517 return parseDirectiveCFIDefCfa(IDLoc);
1518 case DK_CFI_DEF_CFA_OFFSET:
1519 return parseDirectiveCFIDefCfaOffset();
1520 case DK_CFI_ADJUST_CFA_OFFSET:
1521 return parseDirectiveCFIAdjustCfaOffset();
1522 case DK_CFI_DEF_CFA_REGISTER:
1523 return parseDirectiveCFIDefCfaRegister(IDLoc);
1524 case DK_CFI_OFFSET:
1525 return parseDirectiveCFIOffset(IDLoc);
1526 case DK_CFI_REL_OFFSET:
1527 return parseDirectiveCFIRelOffset(IDLoc);
1528 case DK_CFI_PERSONALITY:
1529 return parseDirectiveCFIPersonalityOrLsda(true);
1530 case DK_CFI_LSDA:
1531 return parseDirectiveCFIPersonalityOrLsda(false);
1532 case DK_CFI_REMEMBER_STATE:
1533 return parseDirectiveCFIRememberState();
1534 case DK_CFI_RESTORE_STATE:
1535 return parseDirectiveCFIRestoreState();
1536 case DK_CFI_SAME_VALUE:
1537 return parseDirectiveCFISameValue(IDLoc);
1538 case DK_CFI_RESTORE:
1539 return parseDirectiveCFIRestore(IDLoc);
1540 case DK_CFI_ESCAPE:
1541 return parseDirectiveCFIEscape();
1542 case DK_CFI_SIGNAL_FRAME:
1543 return parseDirectiveCFISignalFrame();
1544 case DK_CFI_UNDEFINED:
1545 return parseDirectiveCFIUndefined(IDLoc);
1546 case DK_CFI_REGISTER:
1547 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001548 case DK_CFI_WINDOW_SAVE:
1549 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001550 case DK_MACROS_ON:
1551 case DK_MACROS_OFF:
1552 return parseDirectiveMacrosOnOff(IDVal);
1553 case DK_MACRO:
1554 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001555 case DK_EXITM:
1556 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001557 case DK_ENDM:
1558 case DK_ENDMACRO:
1559 return parseDirectiveEndMacro(IDVal);
1560 case DK_PURGEM:
1561 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001562 case DK_END:
1563 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001564 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001565 return parseDirectiveError(IDLoc, false);
1566 case DK_ERROR:
1567 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001568 case DK_WARNING:
1569 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001570 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001571
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001572 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001573 }
Chris Lattner36e02122009-06-21 20:54:55 +00001574
Chad Rosierc7f552c2013-02-12 21:33:51 +00001575 // __asm _emit or __asm __emit
1576 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1577 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001579
1580 // __asm align
1581 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001582 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001583
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001584 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001585
Chris Lattner7cbfa442010-05-19 23:34:33 +00001586 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001587 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001588 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001589 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001590 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001591 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001592
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001593 // Dump the parsed representation, if requested.
1594 if (getShowParsedOperands()) {
1595 SmallString<256> Str;
1596 raw_svector_ostream OS(Str);
1597 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001598 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001599 if (i != 0)
1600 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001601 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001602 }
1603 OS << "]";
1604
Jim Grosbach4b905842013-09-20 23:08:21 +00001605 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001606 }
1607
Oliver Stannard8b273082014-06-19 15:52:37 +00001608 // If we are generating dwarf for the current section then generate a .loc
1609 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001610 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001611 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001612 getStreamer().getCurrentSection().first)) {
1613 unsigned Line;
1614 if (ActiveMacros.empty())
1615 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1616 else
1617 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1618 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001619
Eli Bendersky88024712013-01-16 19:32:36 +00001620 // If we previously parsed a cpp hash file line comment then make sure the
1621 // current Dwarf File is for the CppHashFilename if not then emit the
1622 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001623 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001624 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1625 0, StringRef(), CppHashFilename);
1626 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001627
Jim Grosbach4b905842013-09-20 23:08:21 +00001628 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1629 // cache with the different Loc from the call above we save the last
1630 // info we queried here with SrcMgr.FindLineNumber().
1631 unsigned CppHashLocLineNo;
1632 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1633 CppHashLocLineNo = LastQueryLine;
1634 else {
1635 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1636 LastQueryLine = CppHashLocLineNo;
1637 LastQueryIDLoc = CppHashLoc;
1638 LastQueryBuffer = CppHashBuf;
1639 }
1640 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001641 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001642
Jim Grosbach4b905842013-09-20 23:08:21 +00001643 getStreamer().EmitDwarfLocDirective(
1644 getContext().getGenDwarfFileNumber(), Line, 0,
1645 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1646 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001647 }
1648
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001649 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001650 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001651 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001652 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1653 Info.ParsedOperands, Out,
1654 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001655 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001656
Chris Lattnera2a9d162010-09-11 16:18:25 +00001657 // Don't skip the rest of the line, the instruction parser is responsible for
1658 // that.
1659 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001660}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001661
Jim Grosbach4b905842013-09-20 23:08:21 +00001662/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001663/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001664void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001665 if (!Lexer.is(AsmToken::EndOfStatement))
1666 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001667 // Eat EOL.
1668 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001669}
1670
Jim Grosbach4b905842013-09-20 23:08:21 +00001671/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001672/// ::= # number "filename"
1673/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001674bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001675 Lex(); // Eat the hash token.
1676
1677 if (getLexer().isNot(AsmToken::Integer)) {
1678 // Consume the line since in cases it is not a well-formed line directive,
1679 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001680 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001681 return false;
1682 }
1683
1684 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001685 Lex();
1686
1687 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001689 return false;
1690 }
1691
1692 StringRef Filename = getTok().getString();
1693 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001695
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1697 CppHashLoc = L;
1698 CppHashFilename = Filename;
1699 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001700 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001701
1702 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001703 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001704 return false;
1705}
1706
Jim Grosbach4b905842013-09-20 23:08:21 +00001707/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708/// for the Filename and LineNo if any in the diagnostic.
1709void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001710 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711 raw_ostream &OS = errs();
1712
1713 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1714 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001715 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1716 unsigned CppHashBuf =
1717 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718
Jim Grosbach4b905842013-09-20 23:08:21 +00001719 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001721 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1722 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1723 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001724 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1725 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001726 }
1727
Eric Christophera7c32732012-12-18 00:30:54 +00001728 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001729 // manager changed or buffer changed (like in a nested include) then just
1730 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001731 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001733 if (Parser->SavedDiagHandler)
1734 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1735 else
Craig Topper353eda42014-04-24 06:44:33 +00001736 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001737 return;
1738 }
1739
Eric Christophera7c32732012-12-18 00:30:54 +00001740 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1742 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001743 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001744
1745 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1746 int CppHashLocLineNo =
1747 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001748 int LineNo =
1749 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001750
Jim Grosbach4b905842013-09-20 23:08:21 +00001751 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1752 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001753 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001754
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001755 if (Parser->SavedDiagHandler)
1756 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1757 else
Craig Topper353eda42014-04-24 06:44:33 +00001758 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001759}
1760
Rafael Espindola2c064482012-08-21 18:29:30 +00001761// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1762// difference being that that function accepts '@' as part of identifiers and
1763// we can't do that. AsmLexer.cpp should probably be changed to handle
1764// '@' as a special case when needed.
1765static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001766 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1767 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001768}
1769
Rafael Espindola34b9c512012-06-03 23:57:14 +00001770bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001771 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001772 ArrayRef<MCAsmMacroArgument> A,
1773 bool EnableAtPseudoVariable, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001775 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001776 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001778
Preston Gurd05500642012-09-19 20:36:12 +00001779 // A macro without parameters is handled differently on Darwin:
1780 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001781 while (!Body.empty()) {
1782 // Scan for the next substitution.
1783 std::size_t End = Body.size(), Pos = 0;
1784 for (; Pos != End; ++Pos) {
1785 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001786 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 // This macro has no parameters, look for $0, $1, etc.
1788 if (Body[Pos] != '$' || Pos + 1 == End)
1789 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001790
Rafael Espindola1134ab232011-06-05 02:43:45 +00001791 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001792 if (Next == '$' || Next == 'n' ||
1793 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 break;
1795 } else {
1796 // This macro has parameters, look for \foo, \bar, etc.
1797 if (Body[Pos] == '\\' && Pos + 1 != End)
1798 break;
1799 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001800 }
1801
1802 // Add the prefix.
1803 OS << Body.slice(0, Pos);
1804
1805 // Check if we reached the end.
1806 if (Pos == End)
1807 break;
1808
Benjamin Kramer513e7442014-02-20 13:36:32 +00001809 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 switch (Body[Pos + 1]) {
1811 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 case '$':
1813 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001814 break;
1815
Jim Grosbach4b905842013-09-20 23:08:21 +00001816 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001817 case 'n':
1818 OS << A.size();
1819 break;
1820
Jim Grosbach4b905842013-09-20 23:08:21 +00001821 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 default: {
1823 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001824 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001825 if (Index >= A.size())
1826 break;
1827
1828 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001829 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001830 ie = A[Index].end();
1831 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001832 OS << it->getString();
1833 break;
1834 }
1835 }
1836 Pos += 2;
1837 } else {
1838 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001839
1840 // Check for the \@ pseudo-variable.
1841 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001842 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001843 else
1844 while (isIdentifierChar(Body[I]) && I + 1 != End)
1845 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001846
Jim Grosbach4b905842013-09-20 23:08:21 +00001847 const char *Begin = Body.data() + Pos + 1;
1848 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001849 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001850
Toma Tabacu217116e2015-04-27 10:50:29 +00001851 if (Argument == "@") {
1852 OS << NumOfMacroInstantiations;
1853 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001854 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001855 for (; Index < NParameters; ++Index)
1856 if (Parameters[Index].Name == Argument)
1857 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001858
Toma Tabacu217116e2015-04-27 10:50:29 +00001859 if (Index == NParameters) {
1860 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1861 Pos += 3;
1862 else {
1863 OS << '\\' << Argument;
1864 Pos = I;
1865 }
1866 } else {
1867 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1868 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1869 ie = A[Index].end();
1870 it != ie; ++it)
1871 // We expect no quotes around the string's contents when
1872 // parsing for varargs.
1873 if (it->getKind() != AsmToken::String || VarargParameter)
1874 OS << it->getString();
1875 else
1876 OS << it->getStringContents();
1877
1878 Pos += 1 + Argument.size();
1879 }
Preston Gurd05500642012-09-19 20:36:12 +00001880 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001881 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001882 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001883 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001884 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001885
Rafael Espindola1134ab232011-06-05 02:43:45 +00001886 return false;
1887}
Daniel Dunbar43235712010-07-18 18:54:11 +00001888
Nico Weber2a8f9222014-07-24 16:29:04 +00001889MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001890 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001891 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001892 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001893
Jim Grosbach4b905842013-09-20 23:08:21 +00001894static bool isOperator(AsmToken::TokenKind kind) {
1895 switch (kind) {
1896 default:
1897 return false;
1898 case AsmToken::Plus:
1899 case AsmToken::Minus:
1900 case AsmToken::Tilde:
1901 case AsmToken::Slash:
1902 case AsmToken::Star:
1903 case AsmToken::Dot:
1904 case AsmToken::Equal:
1905 case AsmToken::EqualEqual:
1906 case AsmToken::Pipe:
1907 case AsmToken::PipePipe:
1908 case AsmToken::Caret:
1909 case AsmToken::Amp:
1910 case AsmToken::AmpAmp:
1911 case AsmToken::Exclaim:
1912 case AsmToken::ExclaimEqual:
1913 case AsmToken::Percent:
1914 case AsmToken::Less:
1915 case AsmToken::LessEqual:
1916 case AsmToken::LessLess:
1917 case AsmToken::LessGreater:
1918 case AsmToken::Greater:
1919 case AsmToken::GreaterEqual:
1920 case AsmToken::GreaterGreater:
1921 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001922 }
1923}
1924
David Majnemer16252452014-01-29 00:07:39 +00001925namespace {
1926class AsmLexerSkipSpaceRAII {
1927public:
1928 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1929 Lexer.setSkipSpace(SkipSpace);
1930 }
1931
1932 ~AsmLexerSkipSpaceRAII() {
1933 Lexer.setSkipSpace(true);
1934 }
1935
1936private:
1937 AsmLexer &Lexer;
1938};
1939}
1940
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001941bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1942
1943 if (Vararg) {
1944 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1945 StringRef Str = parseStringToEndOfStatement();
1946 MA.push_back(AsmToken(AsmToken::String, Str));
1947 }
1948 return false;
1949 }
1950
Rafael Espindola768b41c2012-06-15 14:02:34 +00001951 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001952 unsigned AddTokens = 0;
1953
David Majnemer16252452014-01-29 00:07:39 +00001954 // Darwin doesn't use spaces to delmit arguments.
1955 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001956
1957 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001958 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001960
David Majnemer91fc4c22014-01-29 18:57:46 +00001961 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001962 break;
Preston Gurd05500642012-09-19 20:36:12 +00001963
1964 if (Lexer.is(AsmToken::Space)) {
1965 Lex(); // Eat spaces
1966
1967 // Spaces can delimit parameters, but could also be part an expression.
1968 // If the token after a space is an operator, add the token and the next
1969 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001970 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001971 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001972 // Check to see whether the token is used as an operator,
1973 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001974 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001975 if (*NextChar == ' ')
1976 AddTokens = 2;
1977 }
1978
1979 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001980 break;
1981 }
1982 }
1983 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984
Jim Grosbach4b905842013-09-20 23:08:21 +00001985 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001986 // to be able to fill in the remaining default parameter values
1987 if (Lexer.is(AsmToken::EndOfStatement))
1988 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989
1990 // Adjust the current parentheses level.
1991 if (Lexer.is(AsmToken::LParen))
1992 ++ParenLevel;
1993 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1994 --ParenLevel;
1995
1996 // Append the token to the current argument list.
1997 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001998 if (AddTokens)
1999 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002000 Lex();
2001 }
Preston Gurd05500642012-09-19 20:36:12 +00002002
Rafael Espindola768b41c2012-06-15 14:02:34 +00002003 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002004 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002005 return false;
2006}
2007
2008// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002009bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002010 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002011 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002012 bool NamedParametersFound = false;
2013 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002014
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002015 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002016 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002017
Rafael Espindola768b41c2012-06-15 14:02:34 +00002018 // Parse two kinds of macro invocations:
2019 // - macros defined without any parameters accept an arbitrary number of them
2020 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002021 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002022 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2023 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002024 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002026
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002027 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002028 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002029 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 eatToEndOfStatement();
2031 return true;
2032 }
2033
2034 if (!Lexer.is(AsmToken::Equal)) {
2035 TokError("expected '=' after formal parameter identifier");
2036 eatToEndOfStatement();
2037 return true;
2038 }
2039 Lex();
2040
2041 NamedParametersFound = true;
2042 }
2043
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002044 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002045 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002046 eatToEndOfStatement();
2047 return true;
2048 }
2049
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002050 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2051 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002052 return true;
2053
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002054 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002055 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002056 unsigned FAI = 0;
2057 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002058 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002059 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002060
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002061 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002062 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002063 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002064 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002065 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002066 return true;
2067 }
2068 PI = FAI;
2069 }
2070
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002071 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002072 if (A.size() <= PI)
2073 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002074 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002075
2076 if (FALocs.size() <= PI)
2077 FALocs.resize(PI + 1);
2078
2079 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002080 }
Jim Grosbach206661622012-07-30 22:44:17 +00002081
Preston Gurd242ed3152012-09-19 20:29:04 +00002082 // At the end of the statement, fill in remaining arguments that have
2083 // default values. If there aren't any, then the next argument is
2084 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002085 if (Lexer.is(AsmToken::EndOfStatement)) {
2086 bool Failure = false;
2087 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2088 if (A[FAI].empty()) {
2089 if (M->Parameters[FAI].Required) {
2090 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2091 "missing value for required parameter "
2092 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2093 Failure = true;
2094 }
2095
2096 if (!M->Parameters[FAI].Value.empty())
2097 A[FAI] = M->Parameters[FAI].Value;
2098 }
2099 }
2100 return Failure;
2101 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002102
2103 if (Lexer.is(AsmToken::Comma))
2104 Lex();
2105 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002106
2107 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002108}
2109
Jim Grosbach4b905842013-09-20 23:08:21 +00002110const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002111 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2112 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002113}
2114
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002115void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2116 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002117}
2118
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002119void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002120
Jim Grosbach4b905842013-09-20 23:08:21 +00002121bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002122 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2123 // this, although we should protect against infinite loops.
2124 if (ActiveMacros.size() == 20)
2125 return TokError("macros cannot be nested more than 20 levels deep");
2126
Eli Bendersky38274122013-01-14 23:22:36 +00002127 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002128 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002129 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002130
Rafael Espindola1134ab232011-06-05 02:43:45 +00002131 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2132 // to hold the macro body with substitutions.
2133 SmallString<256> Buf;
2134 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002135 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002136
Toma Tabacu217116e2015-04-27 10:50:29 +00002137 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002138 return true;
2139
Eli Bendersky38274122013-01-14 23:22:36 +00002140 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002141 // instantiation.
2142 OS << ".endmacro\n";
2143
Rafael Espindola3560ff22014-08-27 20:03:13 +00002144 std::unique_ptr<MemoryBuffer> Instantiation =
2145 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002146
Daniel Dunbar43235712010-07-18 18:54:11 +00002147 // Create the macro instantiation object and add to the current macro
2148 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002149 MacroInstantiation *MI = new MacroInstantiation(
2150 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002151 ActiveMacros.push_back(MI);
2152
Toma Tabacu217116e2015-04-27 10:50:29 +00002153 ++NumOfMacroInstantiations;
2154
Daniel Dunbar43235712010-07-18 18:54:11 +00002155 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002156 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002157 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002158 Lex();
2159
2160 return false;
2161}
2162
Jim Grosbach4b905842013-09-20 23:08:21 +00002163void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002164 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002165 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002166 Lex();
2167
2168 // Pop the instantiation entry.
2169 delete ActiveMacros.back();
2170 ActiveMacros.pop_back();
2171}
2172
Jim Grosbach4b905842013-09-20 23:08:21 +00002173static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002174 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002175 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002176 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2177 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002178 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002179 case MCExpr::Target:
2180 case MCExpr::Constant:
2181 return false;
2182 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002183 const MCSymbol &S =
2184 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002185 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002186 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002187 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002188 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002189 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002190 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002191 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002192
2193 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002194}
2195
Jim Grosbach4b905842013-09-20 23:08:21 +00002196bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002197 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002198 // FIXME: Use better location, we should use proper tokens.
2199 SMLoc EqualLoc = Lexer.getLoc();
2200
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002201 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002202 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002203 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002204
Rafael Espindola72f5f172012-01-28 05:57:00 +00002205 // Note: we don't count b as used in "a = b". This is to allow
2206 // a = b
2207 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002208
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002209 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002210 return TokError("unexpected token in assignment");
2211
2212 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002213 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002214
Daniel Dunbar5f339242009-10-16 01:57:39 +00002215 // Validate that the LHS is allowed to be a variable (either it has not been
2216 // used as a symbol, or it is an absolute symbol).
2217 MCSymbol *Sym = getContext().LookupSymbol(Name);
2218 if (Sym) {
2219 // Diagnose assignment to a label.
2220 //
2221 // FIXME: Diagnostics. Note the location of the definition as a label.
2222 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002223 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002224 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2225 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002226 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002227 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2228 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002229 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002230 return Error(EqualLoc, "redefinition of '" + Name + "'");
2231 else if (!Sym->isVariable())
2232 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002233 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002234 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002235 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002236
2237 // Don't count these checks as uses.
2238 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002239 } else if (Name == ".") {
2240 if (Out.EmitValueToOffset(Value, 0)) {
2241 Error(EqualLoc, "expected absolute expression");
2242 eatToEndOfStatement();
2243 }
2244 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002245 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002246 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002247
David Majnemer58cb80c2014-12-24 10:27:50 +00002248 Sym->setRedefinable(allow_redef);
2249
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002250 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002251 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002252 if (NoDeadStrip)
2253 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2254
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002255 return false;
2256}
2257
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002258/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002259/// ::= identifier
2260/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002261bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002262 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002263 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2264 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002265 // handle this as a context dependent token, instead we detect adjacent tokens
2266 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002267 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2268 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002269
Hans Wennborgce69d772013-10-18 20:46:28 +00002270 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002271 Lex();
2272 if (Lexer.isNot(AsmToken::Identifier))
2273 return true;
2274
Hans Wennborgce69d772013-10-18 20:46:28 +00002275 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2276 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002277 return true;
2278
2279 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002280 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002281 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002282 Lex();
2283 return false;
2284 }
2285
Jim Grosbach4b905842013-09-20 23:08:21 +00002286 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002287 return true;
2288
Sean Callanan936b0d32010-01-19 21:44:56 +00002289 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002290
Sean Callanan686ed8d2010-01-19 20:22:31 +00002291 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002292
2293 return false;
2294}
2295
Jim Grosbach4b905842013-09-20 23:08:21 +00002296/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002297/// ::= .equ identifier ',' expression
2298/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002299/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002300bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002301 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002302
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002303 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002304 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002305
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002306 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002307 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002308 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002311}
2312
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002313bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002314 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002315
2316 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002317 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002318 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2319 if (Str[i] != '\\') {
2320 Data += Str[i];
2321 continue;
2322 }
2323
2324 // Recognize escaped characters. Note that this escape semantics currently
2325 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2326 ++i;
2327 if (i == e)
2328 return TokError("unexpected backslash at end of string");
2329
2330 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002331 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002332 // Consume up to three octal characters.
2333 unsigned Value = Str[i] - '0';
2334
Jim Grosbach4b905842013-09-20 23:08:21 +00002335 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002336 ++i;
2337 Value = Value * 8 + (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 }
2343 }
2344
2345 if (Value > 255)
2346 return TokError("invalid octal escape sequence (out of range)");
2347
Jim Grosbach4b905842013-09-20 23:08:21 +00002348 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002349 continue;
2350 }
2351
2352 // Otherwise recognize individual escapes.
2353 switch (Str[i]) {
2354 default:
2355 // Just reject invalid escape sequences for now.
2356 return TokError("invalid escape sequence (unrecognized character)");
2357
2358 case 'b': Data += '\b'; break;
2359 case 'f': Data += '\f'; break;
2360 case 'n': Data += '\n'; break;
2361 case 'r': Data += '\r'; break;
2362 case 't': Data += '\t'; break;
2363 case '"': Data += '"'; break;
2364 case '\\': Data += '\\'; break;
2365 }
2366 }
2367
2368 return false;
2369}
2370
Jim Grosbach4b905842013-09-20 23:08:21 +00002371/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002372/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002373bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002374 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002375 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002376
Daniel Dunbara10e5192009-06-24 23:30:00 +00002377 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002378 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002379 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380
Daniel Dunbaref668c12009-08-14 18:19:52 +00002381 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002382 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002383 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002384
Rafael Espindola64e1af82013-07-02 15:49:13 +00002385 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002386 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002387 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002388
Sean Callanan686ed8d2010-01-19 20:22:31 +00002389 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002390
2391 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 break;
2393
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002394 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002395 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002396 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002397 }
2398 }
2399
Sean Callanan686ed8d2010-01-19 20:22:31 +00002400 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002401 return false;
2402}
2403
Jim Grosbach4b905842013-09-20 23:08:21 +00002404/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002405/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002406bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002407 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002408 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002409
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002411 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002412 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002413 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002414 return true;
2415
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002416 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002417 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2418 assert(Size <= 8 && "Invalid size");
2419 uint64_t IntValue = MCE->getValue();
2420 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2421 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002422 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002423 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002424 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002425
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002426 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002427 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002428
Daniel Dunbara10e5192009-06-24 23:30:00 +00002429 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002430 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002431 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002432 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002433 }
2434 }
2435
Sean Callanan686ed8d2010-01-19 20:22:31 +00002436 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002437 return false;
2438}
2439
David Woodhoused6de0d92014-02-01 16:20:59 +00002440/// ParseDirectiveOctaValue
2441/// ::= .octa [ hexconstant (, hexconstant)* ]
2442bool AsmParser::parseDirectiveOctaValue() {
2443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2444 checkForValidSection();
2445
2446 for (;;) {
2447 if (Lexer.getKind() == AsmToken::Error)
2448 return true;
2449 if (Lexer.getKind() != AsmToken::Integer &&
2450 Lexer.getKind() != AsmToken::BigNum)
2451 return TokError("unknown token in expression");
2452
2453 SMLoc ExprLoc = getLexer().getLoc();
2454 APInt IntValue = getTok().getAPIntVal();
2455 Lex();
2456
2457 uint64_t hi, lo;
2458 if (IntValue.isIntN(64)) {
2459 hi = 0;
2460 lo = IntValue.getZExtValue();
2461 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002462 // It might actually have more than 128 bits, but the top ones are zero.
2463 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002464 lo = IntValue.getLoBits(64).getZExtValue();
2465 } else
2466 return Error(ExprLoc, "literal value out of range for directive");
2467
2468 if (MAI.isLittleEndian()) {
2469 getStreamer().EmitIntValue(lo, 8);
2470 getStreamer().EmitIntValue(hi, 8);
2471 } else {
2472 getStreamer().EmitIntValue(hi, 8);
2473 getStreamer().EmitIntValue(lo, 8);
2474 }
2475
2476 if (getLexer().is(AsmToken::EndOfStatement))
2477 break;
2478
2479 // FIXME: Improve diagnostic.
2480 if (getLexer().isNot(AsmToken::Comma))
2481 return TokError("unexpected token in directive");
2482 Lex();
2483 }
2484 }
2485
2486 Lex();
2487 return false;
2488}
2489
Jim Grosbach4b905842013-09-20 23:08:21 +00002490/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002491/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002492bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002493 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002494 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002495
2496 for (;;) {
2497 // We don't truly support arithmetic on floating point expressions, so we
2498 // have to manually parse unary prefixes.
2499 bool IsNeg = false;
2500 if (getLexer().is(AsmToken::Minus)) {
2501 Lex();
2502 IsNeg = true;
2503 } else if (getLexer().is(AsmToken::Plus))
2504 Lex();
2505
Michael J. Spencer530ce852010-10-09 11:00:50 +00002506 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002507 getLexer().isNot(AsmToken::Real) &&
2508 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002509 return TokError("unexpected token in directive");
2510
2511 // Convert to an APFloat.
2512 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002513 StringRef IDVal = getTok().getString();
2514 if (getLexer().is(AsmToken::Identifier)) {
2515 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2516 Value = APFloat::getInf(Semantics);
2517 else if (!IDVal.compare_lower("nan"))
2518 Value = APFloat::getNaN(Semantics, false, ~0);
2519 else
2520 return TokError("invalid floating point literal");
2521 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002522 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002523 return TokError("invalid floating point literal");
2524 if (IsNeg)
2525 Value.changeSign();
2526
2527 // Consume the numeric token.
2528 Lex();
2529
2530 // Emit the value as an integer.
2531 APInt AsInt = Value.bitcastToAPInt();
2532 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002533 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002534
2535 if (getLexer().is(AsmToken::EndOfStatement))
2536 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002537
Daniel Dunbar2af16532010-09-24 01:59:56 +00002538 if (getLexer().isNot(AsmToken::Comma))
2539 return TokError("unexpected token in directive");
2540 Lex();
2541 }
2542 }
2543
2544 Lex();
2545 return false;
2546}
2547
Jim Grosbach4b905842013-09-20 23:08:21 +00002548/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002549/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002550bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002551 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002552
2553 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002555 return true;
2556
Rafael Espindolab91bac62010-10-05 19:42:57 +00002557 int64_t Val = 0;
2558 if (getLexer().is(AsmToken::Comma)) {
2559 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002560 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002561 return true;
2562 }
2563
Rafael Espindola922e3f42010-09-16 15:03:59 +00002564 if (getLexer().isNot(AsmToken::EndOfStatement))
2565 return TokError("unexpected token in '.zero' directive");
2566
2567 Lex();
2568
Rafael Espindola64e1af82013-07-02 15:49:13 +00002569 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002570
2571 return false;
2572}
2573
Jim Grosbach4b905842013-09-20 23:08:21 +00002574/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002575/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002576bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002577 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002578
David Majnemer522d3db2014-02-01 07:19:38 +00002579 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002580 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002581 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002582 return true;
2583
David Majnemer522d3db2014-02-01 07:19:38 +00002584 if (NumValues < 0) {
2585 Warning(RepeatLoc,
2586 "'.fill' directive with negative repeat count has no effect");
2587 NumValues = 0;
2588 }
2589
Roman Divackye33098f2013-09-24 17:44:41 +00002590 int64_t FillSize = 1;
2591 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002592
David Majnemer522d3db2014-02-01 07:19:38 +00002593 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002594 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2595 if (getLexer().isNot(AsmToken::Comma))
2596 return TokError("unexpected token in '.fill' directive");
2597 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002598
David Majnemer522d3db2014-02-01 07:19:38 +00002599 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002600 if (parseAbsoluteExpression(FillSize))
2601 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002602
Roman Divackye33098f2013-09-24 17:44:41 +00002603 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2604 if (getLexer().isNot(AsmToken::Comma))
2605 return TokError("unexpected token in '.fill' directive");
2606 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002607
David Majnemer522d3db2014-02-01 07:19:38 +00002608 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002609 if (parseAbsoluteExpression(FillExpr))
2610 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002611
Roman Divackye33098f2013-09-24 17:44:41 +00002612 if (getLexer().isNot(AsmToken::EndOfStatement))
2613 return TokError("unexpected token in '.fill' directive");
2614
2615 Lex();
2616 }
2617 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002618
David Majnemer522d3db2014-02-01 07:19:38 +00002619 if (FillSize < 0) {
2620 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2621 NumValues = 0;
2622 }
2623 if (FillSize > 8) {
2624 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2625 FillSize = 8;
2626 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002627
David Majnemer522d3db2014-02-01 07:19:38 +00002628 if (!isUInt<32>(FillExpr) && FillSize > 4)
2629 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2630
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002631 if (NumValues > 0) {
2632 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2633 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2634 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2635 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2636 if (NonZeroFillSize < FillSize)
2637 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2638 }
David Majnemer522d3db2014-02-01 07:19:38 +00002639 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002640
2641 return false;
2642}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002643
Jim Grosbach4b905842013-09-20 23:08:21 +00002644/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002645/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002646bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002647 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002648
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002649 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002650 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002651 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002652 return true;
2653
2654 // Parse optional fill expression.
2655 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002656 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2657 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002658 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002659 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002660
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002661 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002662 return true;
2663
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002664 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002665 return TokError("unexpected token in '.org' directive");
2666 }
2667
Sean Callanan686ed8d2010-01-19 20:22:31 +00002668 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002669
Jim Grosbachb5912772012-01-27 00:37:08 +00002670 // Only limited forms of relocatable expressions are accepted here, it
2671 // has to be relative to the current section. The streamer will return
2672 // 'true' if the expression wasn't evaluatable.
2673 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2674 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002675
2676 return false;
2677}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678
Jim Grosbach4b905842013-09-20 23:08:21 +00002679/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002680/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002681bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002682 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002684 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002685 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002686 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002687 return true;
2688
2689 SMLoc MaxBytesLoc;
2690 bool HasFillExpr = false;
2691 int64_t FillExpr = 0;
2692 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002693 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2694 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002696 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697
2698 // The fill expression can be omitted while specifying a maximum number of
2699 // alignment bytes, e.g:
2700 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002701 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002702 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002703 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002704 return true;
2705 }
2706
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002707 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2708 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002709 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002710 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002711
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002712 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002713 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002714 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002715
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002716 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 return TokError("unexpected token in directive");
2718 }
2719 }
2720
Sean Callanan686ed8d2010-01-19 20:22:31 +00002721 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002722
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002723 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002724 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002725
2726 // Compute alignment in bytes.
2727 if (IsPow2) {
2728 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002729 if (Alignment >= 32) {
2730 Error(AlignmentLoc, "invalid alignment value");
2731 Alignment = 31;
2732 }
2733
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002734 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002735 } else {
2736 // Reject alignments that aren't a power of two, for gas compatibility.
2737 if (!isPowerOf2_64(Alignment))
2738 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002739 }
2740
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002741 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002742 if (MaxBytesLoc.isValid()) {
2743 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002744 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002745 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002746 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002747 }
2748
2749 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002750 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002751 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002752 MaxBytesToFill = 0;
2753 }
2754 }
2755
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002756 // Check whether we should use optimal code alignment for this .align
2757 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002758 const MCSection *Section = getStreamer().getCurrentSection().first;
2759 assert(Section && "must have section to emit alignment");
2760 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002761 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2762 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002763 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002764 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002765 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002766 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2767 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002768 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002769
2770 return false;
2771}
2772
Jim Grosbach4b905842013-09-20 23:08:21 +00002773/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002774/// ::= .file [number] filename
2775/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002776bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002777 // FIXME: I'm not sure what this is.
2778 int64_t FileNumber = -1;
2779 SMLoc FileNumberLoc = getLexer().getLoc();
2780 if (getLexer().is(AsmToken::Integer)) {
2781 FileNumber = getTok().getIntVal();
2782 Lex();
2783
2784 if (FileNumber < 1)
2785 return TokError("file number less than one");
2786 }
2787
2788 if (getLexer().isNot(AsmToken::String))
2789 return TokError("unexpected token in '.file' directive");
2790
2791 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002792 // Allow the strings to have escaped octal character sequence.
2793 std::string Path = getTok().getString();
2794 if (parseEscapedString(Path))
2795 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002796 Lex();
2797
2798 StringRef Directory;
2799 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002800 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002801 if (getLexer().is(AsmToken::String)) {
2802 if (FileNumber == -1)
2803 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002804 if (parseEscapedString(FilenameData))
2805 return true;
2806 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002807 Directory = Path;
2808 Lex();
2809 } else {
2810 Filename = Path;
2811 }
2812
2813 if (getLexer().isNot(AsmToken::EndOfStatement))
2814 return TokError("unexpected token in '.file' directive");
2815
2816 if (FileNumber == -1)
2817 getStreamer().EmitFileDirective(Filename);
2818 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002819 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002820 Error(DirectiveLoc,
2821 "input can't have .file dwarf directives when -g is "
2822 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002823
David Blaikiec714ef42014-03-17 01:52:11 +00002824 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2825 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002826 Error(FileNumberLoc, "file number already allocated");
2827 }
2828
2829 return false;
2830}
2831
Jim Grosbach4b905842013-09-20 23:08:21 +00002832/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002833/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002834bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2836 if (getLexer().isNot(AsmToken::Integer))
2837 return TokError("unexpected token in '.line' directive");
2838
2839 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002840 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002841 Lex();
2842
2843 // FIXME: Do something with the .line.
2844 }
2845
2846 if (getLexer().isNot(AsmToken::EndOfStatement))
2847 return TokError("unexpected token in '.line' directive");
2848
2849 return false;
2850}
2851
Jim Grosbach4b905842013-09-20 23:08:21 +00002852/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002853/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2854/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2855/// The first number is a file number, must have been previously assigned with
2856/// a .file directive, the second number is the line number and optionally the
2857/// third number is a column position (zero if not specified). The remaining
2858/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002859bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002860 if (getLexer().isNot(AsmToken::Integer))
2861 return TokError("unexpected token in '.loc' directive");
2862 int64_t FileNumber = getTok().getIntVal();
2863 if (FileNumber < 1)
2864 return TokError("file number less than one in '.loc' directive");
2865 if (!getContext().isValidDwarfFileNumber(FileNumber))
2866 return TokError("unassigned file number in '.loc' directive");
2867 Lex();
2868
2869 int64_t LineNumber = 0;
2870 if (getLexer().is(AsmToken::Integer)) {
2871 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002872 if (LineNumber < 0)
2873 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002874 Lex();
2875 }
2876
2877 int64_t ColumnPos = 0;
2878 if (getLexer().is(AsmToken::Integer)) {
2879 ColumnPos = getTok().getIntVal();
2880 if (ColumnPos < 0)
2881 return TokError("column position less than zero in '.loc' directive");
2882 Lex();
2883 }
2884
2885 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2886 unsigned Isa = 0;
2887 int64_t Discriminator = 0;
2888 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2889 for (;;) {
2890 if (getLexer().is(AsmToken::EndOfStatement))
2891 break;
2892
2893 StringRef Name;
2894 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002895 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return TokError("unexpected token in '.loc' directive");
2897
2898 if (Name == "basic_block")
2899 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2900 else if (Name == "prologue_end")
2901 Flags |= DWARF2_FLAG_PROLOGUE_END;
2902 else if (Name == "epilogue_begin")
2903 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2904 else if (Name == "is_stmt") {
2905 Loc = getTok().getLoc();
2906 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002907 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return true;
2909 // The expression must be the constant 0 or 1.
2910 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2911 int Value = MCE->getValue();
2912 if (Value == 0)
2913 Flags &= ~DWARF2_FLAG_IS_STMT;
2914 else if (Value == 1)
2915 Flags |= DWARF2_FLAG_IS_STMT;
2916 else
2917 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002918 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002919 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2920 }
Craig Topperf15655b2013-04-22 04:22:40 +00002921 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002922 Loc = getTok().getLoc();
2923 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002924 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002925 return true;
2926 // The expression must be a constant greater or equal to 0.
2927 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2928 int Value = MCE->getValue();
2929 if (Value < 0)
2930 return Error(Loc, "isa number less than zero");
2931 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002932 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002933 return Error(Loc, "isa number not a constant value");
2934 }
Craig Topperf15655b2013-04-22 04:22:40 +00002935 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002936 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002937 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002938 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002939 return Error(Loc, "unknown sub-directive in '.loc' directive");
2940 }
2941
2942 if (getLexer().is(AsmToken::EndOfStatement))
2943 break;
2944 }
2945 }
2946
2947 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2948 Isa, Discriminator, StringRef());
2949
2950 return false;
2951}
2952
Jim Grosbach4b905842013-09-20 23:08:21 +00002953/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002954/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002955bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002956 return TokError("unsupported directive '.stabs'");
2957}
2958
Jim Grosbach4b905842013-09-20 23:08:21 +00002959/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002960/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002961bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002962 StringRef Name;
2963 bool EH = false;
2964 bool Debug = false;
2965
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002966 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002967 return TokError("Expected an identifier");
2968
2969 if (Name == ".eh_frame")
2970 EH = true;
2971 else if (Name == ".debug_frame")
2972 Debug = true;
2973
2974 if (getLexer().is(AsmToken::Comma)) {
2975 Lex();
2976
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002977 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002978 return TokError("Expected an identifier");
2979
2980 if (Name == ".eh_frame")
2981 EH = true;
2982 else if (Name == ".debug_frame")
2983 Debug = true;
2984 }
2985
2986 getStreamer().EmitCFISections(EH, Debug);
2987 return false;
2988}
2989
Jim Grosbach4b905842013-09-20 23:08:21 +00002990/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002991/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002992bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002993 StringRef Simple;
2994 if (getLexer().isNot(AsmToken::EndOfStatement))
2995 if (parseIdentifier(Simple) || Simple != "simple")
2996 return TokError("unexpected token in .cfi_startproc directive");
2997
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00002998 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002999 return false;
3000}
3001
Jim Grosbach4b905842013-09-20 23:08:21 +00003002/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003003/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003004bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003005 getStreamer().EmitCFIEndProc();
3006 return false;
3007}
3008
Jim Grosbach4b905842013-09-20 23:08:21 +00003009/// \brief parse register name or number.
3010bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003011 SMLoc DirectiveLoc) {
3012 unsigned RegNo;
3013
3014 if (getLexer().isNot(AsmToken::Integer)) {
3015 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3016 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003017 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003018 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003019 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003020
3021 return false;
3022}
3023
Jim Grosbach4b905842013-09-20 23:08:21 +00003024/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003025/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003026bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003027 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003028 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003029 return true;
3030
3031 if (getLexer().isNot(AsmToken::Comma))
3032 return TokError("unexpected token in directive");
3033 Lex();
3034
3035 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003036 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003037 return true;
3038
3039 getStreamer().EmitCFIDefCfa(Register, Offset);
3040 return false;
3041}
3042
Jim Grosbach4b905842013-09-20 23:08:21 +00003043/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003044/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003045bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003046 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003047 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return true;
3049
3050 getStreamer().EmitCFIDefCfaOffset(Offset);
3051 return false;
3052}
3053
Jim Grosbach4b905842013-09-20 23:08:21 +00003054/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003055/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003056bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003057 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003058 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003059 return true;
3060
3061 if (getLexer().isNot(AsmToken::Comma))
3062 return TokError("unexpected token in directive");
3063 Lex();
3064
3065 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003066 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003067 return true;
3068
3069 getStreamer().EmitCFIRegister(Register1, Register2);
3070 return false;
3071}
3072
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003073/// parseDirectiveCFIWindowSave
3074/// ::= .cfi_window_save
3075bool AsmParser::parseDirectiveCFIWindowSave() {
3076 getStreamer().EmitCFIWindowSave();
3077 return false;
3078}
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003081/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003082bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003083 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003084 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003085 return true;
3086
3087 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3088 return false;
3089}
3090
Jim Grosbach4b905842013-09-20 23:08:21 +00003091/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003092/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003093bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003094 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003095 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003096 return true;
3097
3098 getStreamer().EmitCFIDefCfaRegister(Register);
3099 return false;
3100}
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003103/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003104bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003105 int64_t Register = 0;
3106 int64_t Offset = 0;
3107
Jim Grosbach4b905842013-09-20 23:08:21 +00003108 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 if (getLexer().isNot(AsmToken::Comma))
3112 return TokError("unexpected token in directive");
3113 Lex();
3114
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003115 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003116 return true;
3117
3118 getStreamer().EmitCFIOffset(Register, Offset);
3119 return false;
3120}
3121
Jim Grosbach4b905842013-09-20 23:08:21 +00003122/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003123/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003124bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003125 int64_t Register = 0;
3126
Jim Grosbach4b905842013-09-20 23:08:21 +00003127 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003128 return true;
3129
3130 if (getLexer().isNot(AsmToken::Comma))
3131 return TokError("unexpected token in directive");
3132 Lex();
3133
3134 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003135 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003136 return true;
3137
3138 getStreamer().EmitCFIRelOffset(Register, Offset);
3139 return false;
3140}
3141
3142static bool isValidEncoding(int64_t Encoding) {
3143 if (Encoding & ~0xff)
3144 return false;
3145
3146 if (Encoding == dwarf::DW_EH_PE_omit)
3147 return true;
3148
3149 const unsigned Format = Encoding & 0xf;
3150 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3151 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3152 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3153 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3154 return false;
3155
3156 const unsigned Application = Encoding & 0x70;
3157 if (Application != dwarf::DW_EH_PE_absptr &&
3158 Application != dwarf::DW_EH_PE_pcrel)
3159 return false;
3160
3161 return true;
3162}
3163
Jim Grosbach4b905842013-09-20 23:08:21 +00003164/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003165/// IsPersonality true for cfi_personality, false for cfi_lsda
3166/// ::= .cfi_personality encoding, [symbol_name]
3167/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003168bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003169 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003170 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003171 return true;
3172 if (Encoding == dwarf::DW_EH_PE_omit)
3173 return false;
3174
3175 if (!isValidEncoding(Encoding))
3176 return TokError("unsupported encoding.");
3177
3178 if (getLexer().isNot(AsmToken::Comma))
3179 return TokError("unexpected token in directive");
3180 Lex();
3181
3182 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003183 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003184 return TokError("expected identifier in directive");
3185
3186 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3187
3188 if (IsPersonality)
3189 getStreamer().EmitCFIPersonality(Sym, Encoding);
3190 else
3191 getStreamer().EmitCFILsda(Sym, Encoding);
3192 return false;
3193}
3194
Jim Grosbach4b905842013-09-20 23:08:21 +00003195/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003196/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003197bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003198 getStreamer().EmitCFIRememberState();
3199 return false;
3200}
3201
Jim Grosbach4b905842013-09-20 23:08:21 +00003202/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003203/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003204bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003205 getStreamer().EmitCFIRestoreState();
3206 return false;
3207}
3208
Jim Grosbach4b905842013-09-20 23:08:21 +00003209/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003210/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003211bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003212 int64_t Register = 0;
3213
Jim Grosbach4b905842013-09-20 23:08:21 +00003214 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003215 return true;
3216
3217 getStreamer().EmitCFISameValue(Register);
3218 return false;
3219}
3220
Jim Grosbach4b905842013-09-20 23:08:21 +00003221/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003222/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003223bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003224 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003225 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003226 return true;
3227
3228 getStreamer().EmitCFIRestore(Register);
3229 return false;
3230}
3231
Jim Grosbach4b905842013-09-20 23:08:21 +00003232/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003233/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003234bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003235 std::string Values;
3236 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003237 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003238 return true;
3239
3240 Values.push_back((uint8_t)CurrValue);
3241
3242 while (getLexer().is(AsmToken::Comma)) {
3243 Lex();
3244
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003245 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003246 return true;
3247
3248 Values.push_back((uint8_t)CurrValue);
3249 }
3250
3251 getStreamer().EmitCFIEscape(Values);
3252 return false;
3253}
3254
Jim Grosbach4b905842013-09-20 23:08:21 +00003255/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003256/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003257bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003258 if (getLexer().isNot(AsmToken::EndOfStatement))
3259 return Error(getLexer().getLoc(),
3260 "unexpected token in '.cfi_signal_frame'");
3261
3262 getStreamer().EmitCFISignalFrame();
3263 return false;
3264}
3265
Jim Grosbach4b905842013-09-20 23:08:21 +00003266/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003267/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003268bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003269 int64_t Register = 0;
3270
Jim Grosbach4b905842013-09-20 23:08:21 +00003271 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003272 return true;
3273
3274 getStreamer().EmitCFIUndefined(Register);
3275 return false;
3276}
3277
Jim Grosbach4b905842013-09-20 23:08:21 +00003278/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003279/// ::= .macros_on
3280/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003281bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003282 if (getLexer().isNot(AsmToken::EndOfStatement))
3283 return Error(getLexer().getLoc(),
3284 "unexpected token in '" + Directive + "' directive");
3285
Jim Grosbach4b905842013-09-20 23:08:21 +00003286 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003287 return false;
3288}
3289
Jim Grosbach4b905842013-09-20 23:08:21 +00003290/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003291/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003292bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003293 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003294 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003295 return TokError("expected identifier in '.macro' directive");
3296
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003297 if (getLexer().is(AsmToken::Comma))
3298 Lex();
3299
Eli Bendersky17233942013-01-15 22:59:42 +00003300 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003301 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003302
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003303 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003304 return Error(Lexer.getLoc(),
3305 "Vararg parameter '" + Parameters.back().Name +
3306 "' should be last one in the list of parameters.");
3307
David Majnemer91fc4c22014-01-29 18:57:46 +00003308 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003309 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003310 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003311
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003312 if (Lexer.is(AsmToken::Colon)) {
3313 Lex(); // consume ':'
3314
3315 SMLoc QualLoc;
3316 StringRef Qualifier;
3317
3318 QualLoc = Lexer.getLoc();
3319 if (parseIdentifier(Qualifier))
3320 return Error(QualLoc, "missing parameter qualifier for "
3321 "'" + Parameter.Name + "' in macro '" + Name + "'");
3322
3323 if (Qualifier == "req")
3324 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003325 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003326 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003327 else
3328 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3329 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3330 }
3331
David Majnemer91fc4c22014-01-29 18:57:46 +00003332 if (getLexer().is(AsmToken::Equal)) {
3333 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003334
3335 SMLoc ParamLoc;
3336
3337 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003338 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003339 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003340
3341 if (Parameter.Required)
3342 Warning(ParamLoc, "pointless default value for required parameter "
3343 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003344 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003345
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003346 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003347
3348 if (getLexer().is(AsmToken::Comma))
3349 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003350 }
3351
3352 // Eat the end of statement.
3353 Lex();
3354
3355 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003356 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003357
3358 // Lex the macro definition.
3359 for (;;) {
3360 // Check whether we have reached the end of the file.
3361 if (getLexer().is(AsmToken::Eof))
3362 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3363
3364 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003365 if (getLexer().is(AsmToken::Identifier)) {
3366 if (getTok().getIdentifier() == ".endm" ||
3367 getTok().getIdentifier() == ".endmacro") {
3368 if (MacroDepth == 0) { // Outermost macro.
3369 EndToken = getTok();
3370 Lex();
3371 if (getLexer().isNot(AsmToken::EndOfStatement))
3372 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3373 "' directive");
3374 break;
3375 } else {
3376 // Otherwise we just found the end of an inner macro.
3377 --MacroDepth;
3378 }
3379 } else if (getTok().getIdentifier() == ".macro") {
3380 // We allow nested macros. Those aren't instantiated until the outermost
3381 // macro is expanded so just ignore them for now.
3382 ++MacroDepth;
3383 }
Eli Bendersky17233942013-01-15 22:59:42 +00003384 }
3385
3386 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003387 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003388 }
3389
Jim Grosbach4b905842013-09-20 23:08:21 +00003390 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003391 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3392 }
3393
3394 const char *BodyStart = StartToken.getLoc().getPointer();
3395 const char *BodyEnd = EndToken.getLoc().getPointer();
3396 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003397 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003398 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003399 return false;
3400}
3401
Jim Grosbach4b905842013-09-20 23:08:21 +00003402/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003403///
3404/// With the support added for named parameters there may be code out there that
3405/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003406/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003407/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003408/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003409/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3410/// warning that the positional parameter found in body which have no effect.
3411/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003412/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003413/// intended or change the macro to use the named parameters. It is possible
3414/// this warning will trigger when the none of the named parameters are used
3415/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003416void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003417 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003418 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003419 // If this macro is not defined with named parameters the warning we are
3420 // checking for here doesn't apply.
3421 unsigned NParameters = Parameters.size();
3422 if (NParameters == 0)
3423 return;
3424
3425 bool NamedParametersFound = false;
3426 bool PositionalParametersFound = false;
3427
3428 // Look at the body of the macro for use of both the named parameters and what
3429 // are likely to be positional parameters. This is what expandMacro() is
3430 // doing when it finds the parameters in the body.
3431 while (!Body.empty()) {
3432 // Scan for the next possible parameter.
3433 std::size_t End = Body.size(), Pos = 0;
3434 for (; Pos != End; ++Pos) {
3435 // Check for a substitution or escape.
3436 // This macro is defined with parameters, look for \foo, \bar, etc.
3437 if (Body[Pos] == '\\' && Pos + 1 != End)
3438 break;
3439
3440 // This macro should have parameters, but look for $0, $1, ..., $n too.
3441 if (Body[Pos] != '$' || Pos + 1 == End)
3442 continue;
3443 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003444 if (Next == '$' || Next == 'n' ||
3445 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003446 break;
3447 }
3448
3449 // Check if we reached the end.
3450 if (Pos == End)
3451 break;
3452
3453 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003454 switch (Body[Pos + 1]) {
3455 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003456 case '$':
3457 break;
3458
Jim Grosbach4b905842013-09-20 23:08:21 +00003459 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003460 case 'n':
3461 PositionalParametersFound = true;
3462 break;
3463
Jim Grosbach4b905842013-09-20 23:08:21 +00003464 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003465 default: {
3466 PositionalParametersFound = true;
3467 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003468 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003469 }
3470 Pos += 2;
3471 } else {
3472 unsigned I = Pos + 1;
3473 while (isIdentifierChar(Body[I]) && I + 1 != End)
3474 ++I;
3475
Jim Grosbach4b905842013-09-20 23:08:21 +00003476 const char *Begin = Body.data() + Pos + 1;
3477 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003478 unsigned Index = 0;
3479 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003480 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003481 break;
3482
3483 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003484 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3485 Pos += 3;
3486 else {
3487 Pos = I;
3488 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003489 } else {
3490 NamedParametersFound = true;
3491 Pos += 1 + Argument.size();
3492 }
3493 }
3494 // Update the scan point.
3495 Body = Body.substr(Pos);
3496 }
3497
3498 if (!NamedParametersFound && PositionalParametersFound)
3499 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3500 "used in macro body, possible positional parameter "
3501 "found in body which will have no effect");
3502}
3503
Nico Weber155dccd12014-07-24 17:08:39 +00003504/// parseDirectiveExitMacro
3505/// ::= .exitm
3506bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3507 if (getLexer().isNot(AsmToken::EndOfStatement))
3508 return TokError("unexpected token in '" + Directive + "' directive");
3509
3510 if (!isInsideMacroInstantiation())
3511 return TokError("unexpected '" + Directive + "' in file, "
3512 "no current macro definition");
3513
3514 // Exit all conditionals that are active in the current macro.
3515 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3516 TheCondState = TheCondStack.back();
3517 TheCondStack.pop_back();
3518 }
3519
3520 handleMacroExit();
3521 return false;
3522}
3523
Jim Grosbach4b905842013-09-20 23:08:21 +00003524/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003525/// ::= .endm
3526/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003528 if (getLexer().isNot(AsmToken::EndOfStatement))
3529 return TokError("unexpected token in '" + Directive + "' directive");
3530
3531 // If we are inside a macro instantiation, terminate the current
3532 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003533 if (isInsideMacroInstantiation()) {
3534 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return false;
3536 }
3537
3538 // Otherwise, this .endmacro is a stray entry in the file; well formed
3539 // .endmacro directives are handled during the macro definition parsing.
3540 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003541 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003542}
3543
Jim Grosbach4b905842013-09-20 23:08:21 +00003544/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003545/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003546bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003547 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003548 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003549 return TokError("expected identifier in '.purgem' directive");
3550
3551 if (getLexer().isNot(AsmToken::EndOfStatement))
3552 return TokError("unexpected token in '.purgem' directive");
3553
Jim Grosbach4b905842013-09-20 23:08:21 +00003554 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003555 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3556
Jim Grosbach4b905842013-09-20 23:08:21 +00003557 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003558 return false;
3559}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003560
Jim Grosbach4b905842013-09-20 23:08:21 +00003561/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003562/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003563bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003564 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003565
3566 // Expect a single argument: an expression that evaluates to a constant
3567 // in the inclusive range 0-30.
3568 SMLoc ExprLoc = getLexer().getLoc();
3569 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003570 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003571 return true;
3572 else if (getLexer().isNot(AsmToken::EndOfStatement))
3573 return TokError("unexpected token after expression in"
3574 " '.bundle_align_mode' directive");
3575 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3576 return Error(ExprLoc,
3577 "invalid bundle alignment size (expected between 0 and 30)");
3578
3579 Lex();
3580
3581 // Because of AlignSizePow2's verified range we can safely truncate it to
3582 // unsigned.
3583 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3584 return false;
3585}
3586
Jim Grosbach4b905842013-09-20 23:08:21 +00003587/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003588/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003589bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003590 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003591 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003592
Eli Bendersky802b6282013-01-07 21:51:08 +00003593 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3594 StringRef Option;
3595 SMLoc Loc = getTok().getLoc();
3596 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003597 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003598
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003599 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003600 return Error(Loc, kInvalidOptionError);
3601
3602 if (Option != "align_to_end")
3603 return Error(Loc, kInvalidOptionError);
3604 else if (getLexer().isNot(AsmToken::EndOfStatement))
3605 return Error(Loc,
3606 "unexpected token after '.bundle_lock' directive option");
3607 AlignToEnd = true;
3608 }
3609
Eli Benderskyf483ff92012-12-20 19:05:53 +00003610 Lex();
3611
Eli Bendersky802b6282013-01-07 21:51:08 +00003612 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003613 return false;
3614}
3615
Jim Grosbach4b905842013-09-20 23:08:21 +00003616/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003617/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003618bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003619 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003620
3621 if (getLexer().isNot(AsmToken::EndOfStatement))
3622 return TokError("unexpected token in '.bundle_unlock' directive");
3623 Lex();
3624
3625 getStreamer().EmitBundleUnlock();
3626 return false;
3627}
3628
Jim Grosbach4b905842013-09-20 23:08:21 +00003629/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003630/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003631bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003632 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003633
3634 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003635 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003636 return true;
3637
3638 int64_t FillExpr = 0;
3639 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3640 if (getLexer().isNot(AsmToken::Comma))
3641 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3642 Lex();
3643
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003644 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003645 return true;
3646
3647 if (getLexer().isNot(AsmToken::EndOfStatement))
3648 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3649 }
3650
3651 Lex();
3652
3653 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003654 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3655 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003656
3657 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003658 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003659
3660 return false;
3661}
3662
Jim Grosbach4b905842013-09-20 23:08:21 +00003663/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003664/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003665bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003666 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003667 const MCExpr *Value;
3668
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003669 for (;;) {
3670 if (parseExpression(Value))
3671 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003672
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003673 if (Signed)
3674 getStreamer().EmitSLEB128Value(Value);
3675 else
3676 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003677
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003678 if (getLexer().is(AsmToken::EndOfStatement))
3679 break;
3680
3681 if (getLexer().isNot(AsmToken::Comma))
3682 return TokError("unexpected token in directive");
3683 Lex();
3684 }
Eli Bendersky17233942013-01-15 22:59:42 +00003685
3686 return false;
3687}
3688
Jim Grosbach4b905842013-09-20 23:08:21 +00003689/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003690/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003691bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003692 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003693 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003694 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003695 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003696
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003697 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003698 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003699
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003700 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003701
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003702 // Assembler local symbols don't make any sense here. Complain loudly.
3703 if (Sym->isTemporary())
3704 return Error(Loc, "non-local symbol required in directive");
3705
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003706 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3707 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003708
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003709 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003710 break;
3711
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003712 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003713 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003714 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003715 }
3716 }
3717
Sean Callanan686ed8d2010-01-19 20:22:31 +00003718 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003719 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003720}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003721
Jim Grosbach4b905842013-09-20 23:08:21 +00003722/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003723/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003724bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003725 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003726
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003727 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003728 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003729 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003730 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003731
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003732 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003733 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003734
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003735 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003737 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003738
3739 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003740 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003741 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742 return true;
3743
3744 int64_t Pow2Alignment = 0;
3745 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003746 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003747 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003748 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003749 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003750 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003751
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003752 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3753 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003754 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3755
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003756 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003757 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3758 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003759 if (!isPowerOf2_64(Pow2Alignment))
3760 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3761 Pow2Alignment = Log2_64(Pow2Alignment);
3762 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003763 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003764
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003765 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003766 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003767
Sean Callanan686ed8d2010-01-19 20:22:31 +00003768 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003769
Chris Lattner28ad7542009-07-09 17:25:12 +00003770 // NOTE: a size of zero for a .comm should create a undefined symbol
3771 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003772 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003773 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003774 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003775
Eric Christopherbc818852010-05-14 01:38:54 +00003776 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003777 // may internally end up wanting an alignment in bytes.
3778 // FIXME: Diagnose overflow.
3779 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003780 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003781 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003782
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003783 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003784 return Error(IDLoc, "invalid symbol redefinition");
3785
Chris Lattner28ad7542009-07-09 17:25:12 +00003786 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003787 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003788 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003789 return false;
3790 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003791
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003792 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003793 return false;
3794}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003795
Jim Grosbach4b905842013-09-20 23:08:21 +00003796/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003797/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003798bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003799 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003800 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003801
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003802 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003803 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003804 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003805
Sean Callanan686ed8d2010-01-19 20:22:31 +00003806 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003807
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003808 if (Str.empty())
3809 Error(Loc, ".abort detected. Assembly stopping.");
3810 else
3811 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003812 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003813
3814 return false;
3815}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003816
Jim Grosbach4b905842013-09-20 23:08:21 +00003817/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003818/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003819bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003820 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003821 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003822
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003823 // Allow the strings to have escaped octal character sequence.
3824 std::string Filename;
3825 if (parseEscapedString(Filename))
3826 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003827 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003828 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003829
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003830 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003831 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003832
Chris Lattner693fbb82009-07-16 06:14:39 +00003833 // Attempt to switch the lexer to the included file before consuming the end
3834 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003835 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003836 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003837 return true;
3838 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003839
3840 return false;
3841}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003842
Jim Grosbach4b905842013-09-20 23:08:21 +00003843/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003844/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003845bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003846 if (getLexer().isNot(AsmToken::String))
3847 return TokError("expected string in '.incbin' directive");
3848
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003849 // Allow the strings to have escaped octal character sequence.
3850 std::string Filename;
3851 if (parseEscapedString(Filename))
3852 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003853 SMLoc IncbinLoc = getLexer().getLoc();
3854 Lex();
3855
3856 if (getLexer().isNot(AsmToken::EndOfStatement))
3857 return TokError("unexpected token in '.incbin' directive");
3858
Kevin Enderby109f25c2011-12-14 21:47:48 +00003859 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003860 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003861 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3862 return true;
3863 }
3864
3865 return false;
3866}
3867
Jim Grosbach4b905842013-09-20 23:08:21 +00003868/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003869/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3870bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003871 TheCondStack.push_back(TheCondState);
3872 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003873 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003874 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003875 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003876 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003877 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003878 return true;
3879
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003880 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003881 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003882
Sean Callanan686ed8d2010-01-19 20:22:31 +00003883 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003884
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003885 switch (DirKind) {
3886 default:
3887 llvm_unreachable("unsupported directive");
3888 case DK_IF:
3889 case DK_IFNE:
3890 break;
3891 case DK_IFEQ:
3892 ExprValue = ExprValue == 0;
3893 break;
3894 case DK_IFGE:
3895 ExprValue = ExprValue >= 0;
3896 break;
3897 case DK_IFGT:
3898 ExprValue = ExprValue > 0;
3899 break;
3900 case DK_IFLE:
3901 ExprValue = ExprValue <= 0;
3902 break;
3903 case DK_IFLT:
3904 ExprValue = ExprValue < 0;
3905 break;
3906 }
3907
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003908 TheCondState.CondMet = ExprValue;
3909 TheCondState.Ignore = !TheCondState.CondMet;
3910 }
3911
3912 return false;
3913}
3914
Jim Grosbach4b905842013-09-20 23:08:21 +00003915/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003916/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003917bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003918 TheCondStack.push_back(TheCondState);
3919 TheCondState.TheCond = AsmCond::IfCond;
3920
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003921 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003922 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003923 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003924 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003925
3926 if (getLexer().isNot(AsmToken::EndOfStatement))
3927 return TokError("unexpected token in '.ifb' directive");
3928
3929 Lex();
3930
3931 TheCondState.CondMet = ExpectBlank == Str.empty();
3932 TheCondState.Ignore = !TheCondState.CondMet;
3933 }
3934
3935 return false;
3936}
3937
Jim Grosbach4b905842013-09-20 23:08:21 +00003938/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003939/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003940/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003941bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003942 TheCondStack.push_back(TheCondState);
3943 TheCondState.TheCond = AsmCond::IfCond;
3944
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003945 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003946 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003947 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003948 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003949
3950 if (getLexer().isNot(AsmToken::Comma))
3951 return TokError("unexpected token in '.ifc' directive");
3952
3953 Lex();
3954
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003955 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003956
3957 if (getLexer().isNot(AsmToken::EndOfStatement))
3958 return TokError("unexpected token in '.ifc' directive");
3959
3960 Lex();
3961
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003962 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003963 TheCondState.Ignore = !TheCondState.CondMet;
3964 }
3965
3966 return false;
3967}
3968
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003969/// parseDirectiveIfeqs
3970/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003971bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003972 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003973 if (ExpectEqual)
3974 TokError("expected string parameter for '.ifeqs' directive");
3975 else
3976 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003977 eatToEndOfStatement();
3978 return true;
3979 }
3980
3981 StringRef String1 = getTok().getStringContents();
3982 Lex();
3983
3984 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003985 if (ExpectEqual)
3986 TokError("expected comma after first string for '.ifeqs' directive");
3987 else
3988 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003989 eatToEndOfStatement();
3990 return true;
3991 }
3992
3993 Lex();
3994
3995 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003996 if (ExpectEqual)
3997 TokError("expected string parameter for '.ifeqs' directive");
3998 else
3999 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004000 eatToEndOfStatement();
4001 return true;
4002 }
4003
4004 StringRef String2 = getTok().getStringContents();
4005 Lex();
4006
4007 TheCondStack.push_back(TheCondState);
4008 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004009 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004010 TheCondState.Ignore = !TheCondState.CondMet;
4011
4012 return false;
4013}
4014
Jim Grosbach4b905842013-09-20 23:08:21 +00004015/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004016/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004017bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004018 StringRef Name;
4019 TheCondStack.push_back(TheCondState);
4020 TheCondState.TheCond = AsmCond::IfCond;
4021
4022 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004023 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004024 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004025 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004026 return TokError("expected identifier after '.ifdef'");
4027
4028 Lex();
4029
4030 MCSymbol *Sym = getContext().LookupSymbol(Name);
4031
4032 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004033 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004034 else
Craig Topper353eda42014-04-24 06:44:33 +00004035 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004036 TheCondState.Ignore = !TheCondState.CondMet;
4037 }
4038
4039 return false;
4040}
4041
Jim Grosbach4b905842013-09-20 23:08:21 +00004042/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004043/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004044bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004045 if (TheCondState.TheCond != AsmCond::IfCond &&
4046 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004047 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4048 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004049 TheCondState.TheCond = AsmCond::ElseIfCond;
4050
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004051 bool LastIgnoreState = false;
4052 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004053 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004054 if (LastIgnoreState || TheCondState.CondMet) {
4055 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004056 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004057 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004058 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004059 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004060 return true;
4061
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004062 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004063 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004064
Sean Callanan686ed8d2010-01-19 20:22:31 +00004065 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004066 TheCondState.CondMet = ExprValue;
4067 TheCondState.Ignore = !TheCondState.CondMet;
4068 }
4069
4070 return false;
4071}
4072
Jim Grosbach4b905842013-09-20 23:08:21 +00004073/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004074/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004075bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004076 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004077 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004078
Sean Callanan686ed8d2010-01-19 20:22:31 +00004079 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004080
4081 if (TheCondState.TheCond != AsmCond::IfCond &&
4082 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004083 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4084 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004085 TheCondState.TheCond = AsmCond::ElseCond;
4086 bool LastIgnoreState = false;
4087 if (!TheCondStack.empty())
4088 LastIgnoreState = TheCondStack.back().Ignore;
4089 if (LastIgnoreState || TheCondState.CondMet)
4090 TheCondState.Ignore = true;
4091 else
4092 TheCondState.Ignore = false;
4093
4094 return false;
4095}
4096
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004097/// parseDirectiveEnd
4098/// ::= .end
4099bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4100 if (getLexer().isNot(AsmToken::EndOfStatement))
4101 return TokError("unexpected token in '.end' directive");
4102
4103 Lex();
4104
4105 while (Lexer.isNot(AsmToken::Eof))
4106 Lex();
4107
4108 return false;
4109}
4110
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004111/// parseDirectiveError
4112/// ::= .err
4113/// ::= .error [string]
4114bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4115 if (!TheCondStack.empty()) {
4116 if (TheCondStack.back().Ignore) {
4117 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004118 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004119 }
4120 }
4121
4122 if (!WithMessage)
4123 return Error(L, ".err encountered");
4124
4125 StringRef Message = ".error directive invoked in source file";
4126 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4127 if (Lexer.isNot(AsmToken::String)) {
4128 TokError(".error argument must be a string");
4129 eatToEndOfStatement();
4130 return true;
4131 }
4132
4133 Message = getTok().getStringContents();
4134 Lex();
4135 }
4136
4137 Error(L, Message);
4138 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004139}
4140
Nico Weber404012b2014-07-24 16:26:06 +00004141/// parseDirectiveWarning
4142/// ::= .warning [string]
4143bool AsmParser::parseDirectiveWarning(SMLoc L) {
4144 if (!TheCondStack.empty()) {
4145 if (TheCondStack.back().Ignore) {
4146 eatToEndOfStatement();
4147 return false;
4148 }
4149 }
4150
4151 StringRef Message = ".warning directive invoked in source file";
4152 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4153 if (Lexer.isNot(AsmToken::String)) {
4154 TokError(".warning argument must be a string");
4155 eatToEndOfStatement();
4156 return true;
4157 }
4158
4159 Message = getTok().getStringContents();
4160 Lex();
4161 }
4162
4163 Warning(L, Message);
4164 return false;
4165}
4166
Jim Grosbach4b905842013-09-20 23:08:21 +00004167/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004168/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004169bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004170 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004171 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004172
Sean Callanan686ed8d2010-01-19 20:22:31 +00004173 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004174
Jim Grosbach4b905842013-09-20 23:08:21 +00004175 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004176 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4177 ".else");
4178 if (!TheCondStack.empty()) {
4179 TheCondState = TheCondStack.back();
4180 TheCondStack.pop_back();
4181 }
4182
4183 return false;
4184}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004185
Eli Bendersky17233942013-01-15 22:59:42 +00004186void AsmParser::initializeDirectiveKindMap() {
4187 DirectiveKindMap[".set"] = DK_SET;
4188 DirectiveKindMap[".equ"] = DK_EQU;
4189 DirectiveKindMap[".equiv"] = DK_EQUIV;
4190 DirectiveKindMap[".ascii"] = DK_ASCII;
4191 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4192 DirectiveKindMap[".string"] = DK_STRING;
4193 DirectiveKindMap[".byte"] = DK_BYTE;
4194 DirectiveKindMap[".short"] = DK_SHORT;
4195 DirectiveKindMap[".value"] = DK_VALUE;
4196 DirectiveKindMap[".2byte"] = DK_2BYTE;
4197 DirectiveKindMap[".long"] = DK_LONG;
4198 DirectiveKindMap[".int"] = DK_INT;
4199 DirectiveKindMap[".4byte"] = DK_4BYTE;
4200 DirectiveKindMap[".quad"] = DK_QUAD;
4201 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004202 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004203 DirectiveKindMap[".single"] = DK_SINGLE;
4204 DirectiveKindMap[".float"] = DK_FLOAT;
4205 DirectiveKindMap[".double"] = DK_DOUBLE;
4206 DirectiveKindMap[".align"] = DK_ALIGN;
4207 DirectiveKindMap[".align32"] = DK_ALIGN32;
4208 DirectiveKindMap[".balign"] = DK_BALIGN;
4209 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4210 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4211 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4212 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4213 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4214 DirectiveKindMap[".org"] = DK_ORG;
4215 DirectiveKindMap[".fill"] = DK_FILL;
4216 DirectiveKindMap[".zero"] = DK_ZERO;
4217 DirectiveKindMap[".extern"] = DK_EXTERN;
4218 DirectiveKindMap[".globl"] = DK_GLOBL;
4219 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004220 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4221 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4222 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4223 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4224 DirectiveKindMap[".reference"] = DK_REFERENCE;
4225 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4226 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4227 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4228 DirectiveKindMap[".comm"] = DK_COMM;
4229 DirectiveKindMap[".common"] = DK_COMMON;
4230 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4231 DirectiveKindMap[".abort"] = DK_ABORT;
4232 DirectiveKindMap[".include"] = DK_INCLUDE;
4233 DirectiveKindMap[".incbin"] = DK_INCBIN;
4234 DirectiveKindMap[".code16"] = DK_CODE16;
4235 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4236 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004237 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004238 DirectiveKindMap[".irp"] = DK_IRP;
4239 DirectiveKindMap[".irpc"] = DK_IRPC;
4240 DirectiveKindMap[".endr"] = DK_ENDR;
4241 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4242 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4243 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4244 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004245 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4246 DirectiveKindMap[".ifge"] = DK_IFGE;
4247 DirectiveKindMap[".ifgt"] = DK_IFGT;
4248 DirectiveKindMap[".ifle"] = DK_IFLE;
4249 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004250 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004251 DirectiveKindMap[".ifb"] = DK_IFB;
4252 DirectiveKindMap[".ifnb"] = DK_IFNB;
4253 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004254 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004255 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004256 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004257 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4258 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4259 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4260 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4261 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004262 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004263 DirectiveKindMap[".endif"] = DK_ENDIF;
4264 DirectiveKindMap[".skip"] = DK_SKIP;
4265 DirectiveKindMap[".space"] = DK_SPACE;
4266 DirectiveKindMap[".file"] = DK_FILE;
4267 DirectiveKindMap[".line"] = DK_LINE;
4268 DirectiveKindMap[".loc"] = DK_LOC;
4269 DirectiveKindMap[".stabs"] = DK_STABS;
4270 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4271 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4272 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4273 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4274 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4275 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4276 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4277 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4278 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4279 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4280 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4281 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4282 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4283 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4284 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4285 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4286 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4287 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4288 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4289 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4290 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004291 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004292 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4293 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4294 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004295 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004296 DirectiveKindMap[".endm"] = DK_ENDM;
4297 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4298 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004299 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004300 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004301 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004302}
4303
Jim Grosbach4b905842013-09-20 23:08:21 +00004304MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004305 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004306
Rafael Espindola34b9c512012-06-03 23:57:14 +00004307 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004308 for (;;) {
4309 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004310 if (getLexer().is(AsmToken::Eof)) {
4311 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004312 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004313 }
4314
Rafael Espindola34b9c512012-06-03 23:57:14 +00004315 if (Lexer.is(AsmToken::Identifier) &&
4316 (getTok().getIdentifier() == ".rept")) {
4317 ++NestLevel;
4318 }
4319
4320 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004321 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004322 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004323 EndToken = getTok();
4324 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004325 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4326 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004327 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004328 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004329 break;
4330 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004331 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004332 }
4333
Rafael Espindola34b9c512012-06-03 23:57:14 +00004334 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004335 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004336 }
4337
4338 const char *BodyStart = StartToken.getLoc().getPointer();
4339 const char *BodyEnd = EndToken.getLoc().getPointer();
4340 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4341
Rafael Espindola34b9c512012-06-03 23:57:14 +00004342 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004343 MacroLikeBodies.push_back(
4344 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004345 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004346}
4347
Jim Grosbach4b905842013-09-20 23:08:21 +00004348void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004349 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004350 OS << ".endr\n";
4351
Rafael Espindola3560ff22014-08-27 20:03:13 +00004352 std::unique_ptr<MemoryBuffer> Instantiation =
4353 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004354
Rafael Espindola34b9c512012-06-03 23:57:14 +00004355 // Create the macro instantiation object and add to the current macro
4356 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004357 MacroInstantiation *MI = new MacroInstantiation(
4358 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004359 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004360
Rafael Espindola34b9c512012-06-03 23:57:14 +00004361 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004362 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004363 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004364 Lex();
4365}
4366
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004367/// parseDirectiveRept
4368/// ::= .rep | .rept count
4369bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004370 const MCExpr *CountExpr;
4371 SMLoc CountLoc = getTok().getLoc();
4372 if (parseExpression(CountExpr))
4373 return true;
4374
Rafael Espindola34b9c512012-06-03 23:57:14 +00004375 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004376 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4377 eatToEndOfStatement();
4378 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4379 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004380
4381 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004382 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004383
4384 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004385 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004386
4387 // Eat the end of statement.
4388 Lex();
4389
4390 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004391 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004392 if (!M)
4393 return true;
4394
4395 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4396 // to hold the macro body with substitutions.
4397 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004398 raw_svector_ostream OS(Buf);
4399 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004400 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4401 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004402 return true;
4403 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004404 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004405
4406 return false;
4407}
4408
Jim Grosbach4b905842013-09-20 23:08:21 +00004409/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004410/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004411bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004412 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004413
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004414 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004415 return TokError("expected identifier in '.irp' directive");
4416
Rafael Espindola768b41c2012-06-15 14:02:34 +00004417 if (Lexer.isNot(AsmToken::Comma))
4418 return TokError("expected comma in '.irp' directive");
4419
4420 Lex();
4421
Eli Bendersky38274122013-01-14 23:22:36 +00004422 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004423 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004424 return true;
4425
4426 // Eat the end of statement.
4427 Lex();
4428
4429 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004430 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004431 if (!M)
4432 return true;
4433
4434 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4435 // to hold the macro body with substitutions.
4436 SmallString<256> Buf;
4437 raw_svector_ostream OS(Buf);
4438
Eli Bendersky38274122013-01-14 23:22:36 +00004439 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004440 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4441 // This is undocumented, but GAS seems to support it.
4442 if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004443 return true;
4444 }
4445
Jim Grosbach4b905842013-09-20 23:08:21 +00004446 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004447
4448 return false;
4449}
4450
Jim Grosbach4b905842013-09-20 23:08:21 +00004451/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004452/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004453bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004454 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004455
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004456 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004457 return TokError("expected identifier in '.irpc' directive");
4458
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004459 if (Lexer.isNot(AsmToken::Comma))
4460 return TokError("expected comma in '.irpc' directive");
4461
4462 Lex();
4463
Eli Bendersky38274122013-01-14 23:22:36 +00004464 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004465 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004466 return true;
4467
4468 if (A.size() != 1 || A.front().size() != 1)
4469 return TokError("unexpected token in '.irpc' directive");
4470
4471 // Eat the end of statement.
4472 Lex();
4473
4474 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004475 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004476 if (!M)
4477 return true;
4478
4479 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4480 // to hold the macro body with substitutions.
4481 SmallString<256> Buf;
4482 raw_svector_ostream OS(Buf);
4483
4484 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004485 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004486 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004487 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004488
Toma Tabacu217116e2015-04-27 10:50:29 +00004489 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4490 // This is undocumented, but GAS seems to support it.
4491 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004492 return true;
4493 }
4494
Jim Grosbach4b905842013-09-20 23:08:21 +00004495 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004496
4497 return false;
4498}
4499
Jim Grosbach4b905842013-09-20 23:08:21 +00004500bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004501 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004502 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004503
4504 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004505 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004506 assert(getLexer().is(AsmToken::EndOfStatement));
4507
Jim Grosbach4b905842013-09-20 23:08:21 +00004508 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004509 return false;
4510}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004511
Jim Grosbach4b905842013-09-20 23:08:21 +00004512bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004513 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004514 const MCExpr *Value;
4515 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004516 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004517 return true;
4518 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4519 if (!MCE)
4520 return Error(ExprLoc, "unexpected expression in _emit");
4521 uint64_t IntValue = MCE->getValue();
4522 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4523 return Error(ExprLoc, "literal value out of range for directive");
4524
Chad Rosierc7f552c2013-02-12 21:33:51 +00004525 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4526 return false;
4527}
4528
Jim Grosbach4b905842013-09-20 23:08:21 +00004529bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004530 const MCExpr *Value;
4531 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004532 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004533 return true;
4534 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4535 if (!MCE)
4536 return Error(ExprLoc, "unexpected expression in align");
4537 uint64_t IntValue = MCE->getValue();
4538 if (!isPowerOf2_64(IntValue))
4539 return Error(ExprLoc, "literal value not a power of two greater then zero");
4540
Jim Grosbach4b905842013-09-20 23:08:21 +00004541 Info.AsmRewrites->push_back(
4542 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004543 return false;
4544}
4545
Chad Rosierf43fcf52013-02-13 21:27:17 +00004546// We are comparing pointers, but the pointers are relative to a single string.
4547// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004548static int rewritesSort(const AsmRewrite *AsmRewriteA,
4549 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004550 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4551 return -1;
4552 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4553 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004554
Chad Rosierfce4fab2013-04-08 17:43:47 +00004555 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4556 // rewrite to the same location. Make sure the SizeDirective rewrite is
4557 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4558 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004559 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4560 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004561 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004562
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;
Jim Grosbach4b905842013-09-20 23:08:21 +00004566 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004567}
4568
Jim Grosbach4b905842013-09-20 23:08:21 +00004569bool AsmParser::parseMSInlineAsm(
4570 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4571 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4572 SmallVectorImpl<std::string> &Constraints,
4573 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4574 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004575 SmallVector<void *, 4> InputDecls;
4576 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004577 SmallVector<bool, 4> InputDeclsAddressOf;
4578 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004579 SmallVector<std::string, 4> InputConstraints;
4580 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004581 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004582
Benjamin Kramer1a136112013-02-15 20:37:21 +00004583 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004584
4585 // Prime the lexer.
4586 Lex();
4587
4588 // While we have input, parse each statement.
4589 unsigned InputIdx = 0;
4590 unsigned OutputIdx = 0;
4591 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004592 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004593 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004594 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004595
Chad Rosier149e8e02012-12-12 22:45:52 +00004596 if (Info.ParseError)
4597 return true;
4598
Benjamin Kramer1a136112013-02-15 20:37:21 +00004599 if (Info.Opcode == ~0U)
4600 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004601
Benjamin Kramer1a136112013-02-15 20:37:21 +00004602 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004603
Benjamin Kramer1a136112013-02-15 20:37:21 +00004604 // Build the list of clobbers, outputs and inputs.
4605 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004606 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004607
Benjamin Kramer1a136112013-02-15 20:37:21 +00004608 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004609 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004610 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004611
Benjamin Kramer1a136112013-02-15 20:37:21 +00004612 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004613 if (Operand.isReg() && !Operand.needAddressOf() &&
4614 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 unsigned NumDefs = Desc.getNumDefs();
4616 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004617 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4618 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004619 continue;
4620 }
4621
4622 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004623 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004624 if (SymName.empty())
4625 continue;
4626
David Blaikie960ea3f2014-06-08 16:18:35 +00004627 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004628 if (!OpDecl)
4629 continue;
4630
4631 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004632 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004633 if (isOutput) {
4634 ++InputIdx;
4635 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004636 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004637 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004638 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004639 } else {
4640 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004641 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4642 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004643 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004644 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004645 }
Reid Kleckneree088972013-12-10 18:27:32 +00004646
4647 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004648 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4649 Desc.getNumImplicitDefs());
4650 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004651 }
4652
4653 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004654 NumOutputs = OutputDecls.size();
4655 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004656
4657 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004658 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4659 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4660 ClobberRegs.end());
4661 Clobbers.assign(ClobberRegs.size(), std::string());
4662 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4663 raw_string_ostream OS(Clobbers[I]);
4664 IP->printRegName(OS, ClobberRegs[I]);
4665 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004666
4667 // Merge the various outputs and inputs. Output are expected first.
4668 if (NumOutputs || NumInputs) {
4669 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004670 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004671 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004672 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004673 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004674 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004675 }
4676 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004677 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004678 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004679 }
4680 }
4681
4682 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004683 std::string AsmStringIR;
4684 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004685 StringRef ASMString =
4686 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4687 const char *AsmStart = ASMString.begin();
4688 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004689 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004690 for (const AsmRewrite &AR : AsmStrRewrites) {
4691 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004692 if (Kind == AOK_Delete)
4693 continue;
4694
David Majnemer8114c1a2014-06-23 02:17:16 +00004695 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004696 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004697
Chad Rosier120eefd2013-03-19 17:32:17 +00004698 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004699 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004700 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004701
Chad Rosier37e755c2012-10-23 17:43:43 +00004702 // Skip the original expression.
4703 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004704 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004705 continue;
4706 }
4707
Chad Rosierff10ed12013-04-12 16:26:42 +00004708 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004709 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004710 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004711 default:
4712 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004714 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004715 break;
4716 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004717 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004718 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004719 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004720 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004721 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004722 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004723 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004724 break;
4725 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004726 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004727 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004728 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004729 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004730 default: break;
4731 case 8: OS << "byte ptr "; break;
4732 case 16: OS << "word ptr "; break;
4733 case 32: OS << "dword ptr "; break;
4734 case 64: OS << "qword ptr "; break;
4735 case 80: OS << "xword ptr "; break;
4736 case 128: OS << "xmmword ptr "; break;
4737 case 256: OS << "ymmword ptr "; break;
4738 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004739 break;
4740 case AOK_Emit:
4741 OS << ".byte";
4742 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004743 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004744 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004745 OS << ".align " << Val;
4746
4747 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004748 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004749 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4750 break;
4751 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004752 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004753 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004754 OS.flush();
4755 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004756 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004757 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004758 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004759 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004760
Chad Rosier8bce6642012-10-18 15:49:34 +00004761 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004762 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004763 }
4764
4765 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004766 if (AsmStart != AsmEnd)
4767 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004768
4769 AsmString = OS.str();
4770 return false;
4771}
4772
Daniel Dunbar01e36072010-07-17 02:26:10 +00004773/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004774MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4775 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004776 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004777}