blob: 92a75074ab5559ac9286e7ba078a7333d1af5b87 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Eric Christophera7c32732012-12-18 00:30:54 +000048MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000049
Daniel Dunbar86033402010-07-12 17:54:38 +000050namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000051/// \brief Helper types for tracking macro definitions.
52typedef std::vector<AsmToken> MCAsmMacroArgument;
53typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000054
55struct MCAsmMacroParameter {
56 StringRef Name;
57 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000058 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000059 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000062};
63
Eli Benderskya313ae62013-01-16 18:56:50 +000064typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
65
66struct MCAsmMacro {
67 StringRef Name;
68 StringRef Body;
69 MCAsmMacroParameters Parameters;
70
71public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000072 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
73 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000074};
75
Daniel Dunbar43235712010-07-18 18:54:11 +000076/// \brief Helper class for storing information about an active macro
77/// instantiation.
78struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000079 /// The location of the instantiation.
80 SMLoc InstantiationLoc;
81
Daniel Dunbar40f1d852012-12-01 01:38:48 +000082 /// The buffer where parsing should resume upon instantiation completion.
83 int ExitBuffer;
84
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location where parsing should resume upon instantiation completion.
86 SMLoc ExitLoc;
87
Nico Weber155dccd12014-07-24 17:08:39 +000088 /// The depth of TheCondStack at the start of the instantiation.
89 size_t CondStackDepth;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000092 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000093};
94
Eli Friedman0f4871d2012-10-22 23:58:19 +000095struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000096 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000097 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000098
Jim Grosbach4b905842013-09-20 23:08:21 +000099 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100 unsigned Opcode;
101
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000103 bool ParseError;
104
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVectorImpl<AsmRewrite> *AsmRewrites;
106
Craig Topper353eda42014-04-24 06:44:33 +0000107 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110};
111
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000112/// \brief The concrete assembly parser instance.
113class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000114 AsmParser(const AsmParser &) = delete;
115 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000116private:
117 AsmLexer Lexer;
118 MCContext &Ctx;
119 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000120 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000122 SourceMgr::DiagHandlerTy SavedDiagHandler;
123 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000124 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000125
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126 /// This is the current buffer index we're lexing from as managed by the
127 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000128 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129
130 AsmCond TheCondState;
131 std::vector<AsmCond> TheCondStack;
132
Jim Grosbach4b905842013-09-20 23:08:21 +0000133 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000134 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000135 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000136 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000137
Jim Grosbach4b905842013-09-20 23:08:21 +0000138 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000139 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000142 std::vector<MacroInstantiation*> ActiveMacros;
143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000145 std::deque<MCAsmMacro> MacroLikeBodies;
146
Daniel Dunbar828984f2010-07-18 18:38:02 +0000147 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000148 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149
Daniel Dunbar43325c42010-09-09 22:42:56 +0000150 /// Flag tracking whether any errors have been encountered.
151 unsigned HadError : 1;
152
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000153 /// The values from the last parsed cpp hash file line comment if any.
154 StringRef CppHashFilename;
155 int64_t CppHashLineNumber;
156 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000157 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000158 /// When generating dwarf for assembly source files we need to calculate the
159 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000160 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
162 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000163 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000165
Devang Patela173ee52012-01-31 18:14:05 +0000166 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
167 unsigned AssemblerDialect;
168
Jim Grosbach4b905842013-09-20 23:08:21 +0000169 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000170 bool IsDarwin;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000173 bool ParsingInlineAsm;
174
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000175public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000176 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000177 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000178 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179
Craig Topper59be68f2014-03-08 07:14:16 +0000180 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181
Craig Topper59be68f2014-03-08 07:14:16 +0000182 void addDirectiveHandler(StringRef Directive,
183 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000184 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
Craig Topper59be68f2014-03-08 07:14:16 +0000191 SourceMgr &getSourceManager() override { return SrcMgr; }
192 MCAsmLexer &getLexer() override { return Lexer; }
193 MCContext &getContext() override { return Ctx; }
194 MCStreamer &getStreamer() override { return Out; }
195 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
Craig Topper59be68f2014-03-08 07:14:16 +0000201 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000202 AssemblerDialect = i;
203 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 void Note(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = None) override;
207 bool Warning(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = None) override;
209 bool Error(SMLoc L, const Twine &Msg,
210 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
215 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000216
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000217 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000218 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000219 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000220 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000222 const MCInstrInfo *MII, const MCInstPrinter *IP,
223 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000226 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
227 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
228 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
229 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000230
Jim Grosbach4b905842013-09-20 23:08:21 +0000231 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000232 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseIdentifier(StringRef &Res) override;
234 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000235
Craig Topper59be68f2014-03-08 07:14:16 +0000236 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000240
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000241 bool parseStatement(ParseStatementInfo &Info,
242 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000243 void eatToEndOfLine();
244 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000247 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000248 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000249 ArrayRef<MCAsmMacroParameter> Parameters,
250 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000251 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000252
Eli Benderskya313ae62013-01-16 18:56:50 +0000253 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000255
256 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000258
259 /// \brief Lookup a previously defined macro.
260 /// \param Name Macro name.
261 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000265 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000273 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000274 ///
275 /// \param M The macro.
276 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
David Majnemer91fc4c22014-01-29 18:57:46 +0000282 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000283 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void printMacroInstantiations();
289 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000290 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000291 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000292 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000293 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 /// \brief Enter the specified file. This returns true on failure.
296 bool enterIncludeFile(const std::string &Filename);
297
298 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000299 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000301
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000302 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000303 /// current token is not set; clients should ensure Lex() is called
304 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000305 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000306 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000307 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000308 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000309
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000310 /// \brief Parse up to the end of statement and a return the contents from the
311 /// current token until the end of the statement; the current token on exit
312 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000313 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000315 /// \brief Parse until the end of a statement or a comma is encountered,
316 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000317 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000318
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000320 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000321
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
323 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
324 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000327
Eli Bendersky17233942013-01-15 22:59:42 +0000328 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000329 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000330 DK_NO_DIRECTIVE, // Placeholder
331 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000332 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
333 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000334 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000335 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000336 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
338 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
339 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
340 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000341 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000342 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
343 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000344 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
345 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
346 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
347 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
348 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
349 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000350 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000351 DK_MACROS_ON, DK_MACROS_OFF,
352 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000353 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000354 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000355 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000356 };
357
Jim Grosbach4b905842013-09-20 23:08:21 +0000358 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000359 /// directives parsed by this class.
360 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361
362 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
364 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000365 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
367 bool parseDirectiveFill(); // ".fill"
368 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000369 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
371 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000372 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000374
Eli Bendersky17233942013-01-15 22:59:42 +0000375 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveFile(SMLoc DirectiveLoc);
377 bool parseDirectiveLine();
378 bool parseDirectiveLoc();
379 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000380
381 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000383 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveCFISections();
385 bool parseDirectiveCFIStartProc();
386 bool parseDirectiveCFIEndProc();
387 bool parseDirectiveCFIDefCfaOffset();
388 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
389 bool parseDirectiveCFIAdjustCfaOffset();
390 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
394 bool parseDirectiveCFIRememberState();
395 bool parseDirectiveCFIRestoreState();
396 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIEscape();
399 bool parseDirectiveCFISignalFrame();
400 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000401
402 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000403 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000404 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000405 bool parseDirectiveEndMacro(StringRef Directive);
406 bool parseDirectiveMacro(SMLoc DirectiveLoc);
407 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
Eli Benderskyf483ff92012-12-20 19:05:53 +0000409 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000411 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415
Eli Bendersky17233942013-01-15 22:59:42 +0000416 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
419 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000423 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveAbort(); // ".abort"
429 bool parseDirectiveInclude(); // ".include"
430 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000432 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
433 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000434 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000436 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000438 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
439 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000445 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000466 // "end"
467 bool parseDirectiveEnd(SMLoc DirectiveLoc);
468
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000469 // ".err" or ".error"
470 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000471
Nico Weber404012b2014-07-24 16:26:06 +0000472 // ".warning"
473 bool parseDirectiveWarning(SMLoc DirectiveLoc);
474
Eli Bendersky17233942013-01-15 22:59:42 +0000475 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000476};
Daniel Dunbar86033402010-07-12 17:54:38 +0000477}
478
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000479namespace llvm {
480
481extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000482extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000483extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484
485}
486
Chris Lattnerc35681b2010-01-19 19:46:13 +0000487enum { DEFAULT_ADDRSPACE = 0 };
488
David Blaikie9f380a32015-03-16 18:06:57 +0000489AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
490 const MCAsmInfo &MAI)
491 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
492 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000493 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000494 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000495 // Save the old handler.
496 SavedDiagHandler = SrcMgr.getDiagHandler();
497 SavedDiagContext = SrcMgr.getDiagContext();
498 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000499 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000500 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000501
Daniel Dunbarc5011082010-07-12 18:12:02 +0000502 // Initialize the platform / file format parser.
David Blaikie9f380a32015-03-16 18:06:57 +0000503 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +0000504 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000505 PlatformParser.reset(createCOFFAsmParser());
506 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000507 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000508 PlatformParser.reset(createDarwinAsmParser());
509 IsDarwin = true;
510 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000512 PlatformParser.reset(createELFAsmParser());
513 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000514 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000515
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000516 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000517 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000518}
519
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000520AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000521 assert((HadError || ActiveMacros.empty()) &&
522 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000523}
524
Jim Grosbach4b905842013-09-20 23:08:21 +0000525void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000526 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000527 for (std::vector<MacroInstantiation *>::const_reverse_iterator
528 it = ActiveMacros.rbegin(),
529 ie = ActiveMacros.rend();
530 it != ie; ++it)
531 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000532 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000533}
534
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000535void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
536 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
537 printMacroInstantiations();
538}
539
Chris Lattnera3a06812011-10-16 04:47:35 +0000540bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000541 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000542 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000543 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
544 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000545 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000546}
547
Chris Lattnera3a06812011-10-16 04:47:35 +0000548bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000549 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000550 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
551 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000552 return true;
553}
554
Jim Grosbach4b905842013-09-20 23:08:21 +0000555bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000556 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000557 unsigned NewBuf =
558 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
559 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000560 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000561
Sean Callanan7a77eae2010-01-21 00:19:58 +0000562 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000563 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 return false;
565}
Daniel Dunbar43235712010-07-18 18:54:11 +0000566
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000567/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000568/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000569/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000570bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000571 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000572 unsigned NewBuf =
573 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
574 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 return true;
576
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000577 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000578 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579 return false;
580}
581
Alp Tokera55b95b2014-07-06 10:33:31 +0000582void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
583 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000584 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
585 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000586}
587
Sean Callanan7a77eae2010-01-21 00:19:58 +0000588const AsmToken &AsmParser::Lex() {
589 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000590
Sean Callanan7a77eae2010-01-21 00:19:58 +0000591 if (tok->is(AsmToken::Eof)) {
592 // If this is the end of an included file, pop the parent file off the
593 // include stack.
594 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
595 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000596 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597 tok = &Lexer.Lex();
598 }
599 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000600
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000602 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000603
Sean Callanan7a77eae2010-01-21 00:19:58 +0000604 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000605}
606
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000607bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000608 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000609 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000610 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000611
Chris Lattner36e02122009-06-21 20:54:55 +0000612 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000613 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000614
615 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000616 AsmCond StartingCondState = TheCondState;
617
Kevin Enderby6469fc22011-11-01 22:27:22 +0000618 // If we are generating dwarf for assembly source files save the initial text
619 // section and generate a .file directive.
620 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000621 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
622 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000623 auto InsertResult = getContext().addGenDwarfSection(
624 getStreamer().getCurrentSection().first);
625 assert(InsertResult.second && ".text section should not have debug info yet");
626 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000627 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
628 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000629 }
630
Chris Lattner73f36112009-07-02 21:53:43 +0000631 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000632 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000633 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000634 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000635 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000636
Daniel Dunbar43325c42010-09-09 22:42:56 +0000637 // We had an error, validate that one was emitted and recover by skipping to
638 // the next line.
639 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000640 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000641 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000642
643 if (TheCondState.TheCond != StartingCondState.TheCond ||
644 TheCondState.Ignore != StartingCondState.Ignore)
645 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000646
647 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000648 const auto &LineTables = getContext().getMCDwarfLineTables();
649 if (!LineTables.empty()) {
650 unsigned Index = 0;
651 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
652 if (File.Name.empty() && Index != 0)
653 TokError("unassigned file number: " + Twine(Index) +
654 " for .file directives");
655 ++Index;
656 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000657 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000658
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000659 // Check to see that all assembler local symbols were actually defined.
660 // Targets that don't do subsections via symbols may not want this, though,
661 // so conservatively exclude them. Only do this if we're finalizing, though,
662 // as otherwise we won't necessarilly have seen everything yet.
663 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
664 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
665 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000666 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000667 i != e; ++i) {
668 MCSymbol *Sym = i->getValue();
669 // Variable symbols may not be marked as defined, so check those
670 // explicitly. If we know it's a variable, we have a definition for
671 // the purposes of this check.
672 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
673 // FIXME: We would really like to refer back to where the symbol was
674 // first referenced for a source location. We need to add something
675 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000676 printMessage(
677 getLexer().getLoc(), SourceMgr::DK_Error,
678 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 }
680 }
681
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000682 // Finalize the output stream if there are no errors and if the client wants
683 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000684 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000685 Out.Finish();
686
Chris Lattner73f36112009-07-02 21:53:43 +0000687 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000688}
689
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000690void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000691 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000692 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000693 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000694 }
695}
696
Jim Grosbach4b905842013-09-20 23:08:21 +0000697/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000698void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000699 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000700 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000701
Chris Lattnere5074c42009-06-22 01:29:09 +0000702 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000703 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000704 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000705}
706
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000708 const char *Start = getTok().getLoc().getPointer();
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000711 Lex();
712
713 const char *End = getTok().getLoc().getPointer();
714 return StringRef(Start, End - Start);
715}
Chris Lattner78db3622009-06-22 05:51:26 +0000716
Jim Grosbach4b905842013-09-20 23:08:21 +0000717StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000718 const char *Start = getTok().getLoc().getPointer();
719
720 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000721 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000722 Lex();
723
724 const char *End = getTok().getLoc().getPointer();
725 return StringRef(Start, End - Start);
726}
727
Jim Grosbach4b905842013-09-20 23:08:21 +0000728/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000729/// NOTE: This assumes the leading '(' has already been consumed.
730///
731/// parenexpr ::= expr)
732///
Jim Grosbach4b905842013-09-20 23:08:21 +0000733bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
734 if (parseExpression(Res))
735 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000736 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000737 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000738 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000739 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000740 return false;
741}
Chris Lattner78db3622009-06-22 05:51:26 +0000742
Jim Grosbach4b905842013-09-20 23:08:21 +0000743/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000744/// NOTE: This assumes the leading '[' has already been consumed.
745///
746/// bracketexpr ::= expr]
747///
Jim Grosbach4b905842013-09-20 23:08:21 +0000748bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
749 if (parseExpression(Res))
750 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000751 if (Lexer.isNot(AsmToken::RBrac))
752 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000753 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754 Lex();
755 return false;
756}
757
Jim Grosbach4b905842013-09-20 23:08:21 +0000758/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000759/// primaryexpr ::= (parenexpr
760/// primaryexpr ::= symbol
761/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000762/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000763/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000764bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000765 SMLoc FirstTokenLoc = getLexer().getLoc();
766 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
767 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000768 default:
769 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000770 // If we have an error assume that we've already handled it.
771 case AsmToken::Error:
772 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000773 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000774 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000775 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000776 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000777 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000778 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000779 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000780 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000781 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000782 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000783 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000784 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000785 if (FirstTokenKind == AsmToken::Dollar) {
786 if (Lexer.getMAI().getDollarIsPC()) {
787 // This is a '$' reference, which references the current PC. Emit a
788 // temporary label to the streamer and refer to it.
789 MCSymbol *Sym = Ctx.CreateTempSymbol();
790 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000791 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
792 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000793 EndLoc = FirstTokenLoc;
794 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000795 }
796 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000798 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000799 // Parse symbol variant
800 std::pair<StringRef, StringRef> Split;
801 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000802 if (FirstTokenKind == AsmToken::String) {
803 if (Lexer.is(AsmToken::At)) {
804 Lexer.Lex(); // eat @
805 SMLoc AtLoc = getLexer().getLoc();
806 StringRef VName;
807 if (parseIdentifier(VName))
808 return Error(AtLoc, "expected symbol variant after '@'");
809
810 Split = std::make_pair(Identifier, VName);
811 }
812 } else {
813 Split = Identifier.split('@');
814 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000815 } else if (Lexer.is(AsmToken::LParen)) {
816 Lexer.Lex(); // eat (
817 StringRef VName;
818 parseIdentifier(VName);
819 if (Lexer.isNot(AsmToken::RParen)) {
820 return Error(Lexer.getTok().getLoc(),
821 "unexpected token in variant, expected ')'");
822 }
823 Lexer.Lex(); // eat )
824 Split = std::make_pair(Identifier, VName);
825 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000826
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000827 EndLoc = SMLoc::getFromPointer(Identifier.end());
828
Daniel Dunbard20cda02009-10-16 01:34:54 +0000829 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 StringRef SymbolName = Identifier;
831 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000832
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000833 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000834 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000835 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 if (Variant != MCSymbolRefExpr::VK_Invalid) {
837 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 Variant = MCSymbolRefExpr::VK_None;
840 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000841 return Error(SMLoc::getFromPointer(Split.second.begin()),
842 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000843 }
844 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000845
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
847
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 // If this is an absolute variable reference, substitute it now to preserve
849 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000850 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000851 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000852 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000853
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000855 return false;
856 }
857
858 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000859 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000860 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 }
David Woodhousef42a6662014-02-01 16:20:54 +0000862 case AsmToken::BigNum:
863 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000864 case AsmToken::Integer: {
865 SMLoc Loc = getTok().getLoc();
866 int64_t IntVal = getTok().getIntVal();
867 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000868 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000869 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 // Look for 'b' or 'f' following an Integer as a directional label
871 if (Lexer.getKind() == AsmToken::Identifier) {
872 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000873 // Lookup the symbol variant if used.
874 std::pair<StringRef, StringRef> Split = IDVal.split('@');
875 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
876 if (Split.first.size() != IDVal.size()) {
877 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000878 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000880 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000881 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000882 if (IDVal == "f" || IDVal == "b") {
883 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000884 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000886 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000888 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000889 Lex(); // Eat identifier.
890 }
891 }
Chris Lattner78db3622009-06-22 05:51:26 +0000892 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 case AsmToken::Real: {
895 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000896 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000898 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000899 Lex(); // Eat token.
900 return false;
901 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000902 case AsmToken::Dot: {
903 // This is a '.' reference, which references the current PC. Emit a
904 // temporary label to the streamer and refer to it.
905 MCSymbol *Sym = Ctx.CreateTempSymbol();
906 Out.EmitLabel(Sym);
907 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000909 Lex(); // Eat identifier.
910 return false;
911 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000912 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000913 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000914 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000915 case AsmToken::LBrac:
916 if (!PlatformParser->HasBracketExpressions())
917 return TokError("brackets expression not supported on this target");
918 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000920 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000921 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000922 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000923 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000924 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000925 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000926 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000927 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000930 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000931 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000936 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000938 }
939}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000940
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000941bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000942 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000943 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000944}
945
Daniel Dunbar55f16672010-09-17 02:47:07 +0000946const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000947AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000949 // Ask the target implementation about this expression first.
950 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
951 if (NewE)
952 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000953 // Recurse over the given expression, rebuilding it to apply the given variant
954 // if there is exactly one symbol.
955 switch (E->getKind()) {
956 case MCExpr::Target:
957 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000958 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959
960 case MCExpr::SymbolRef: {
961 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
962
963 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000964 TokError("invalid variant on expression '" + getTok().getIdentifier() +
965 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 return E;
967 }
968
969 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
970 }
971
972 case MCExpr::Unary: {
973 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000976 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000977 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
978 }
979
980 case MCExpr::Binary: {
981 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000982 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
983 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984
985 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000986 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 if (!LHS)
989 LHS = BE->getLHS();
990 if (!RHS)
991 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992
993 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
994 }
995 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000996
Craig Toppera2886c22012-02-07 05:05:23 +0000997 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998}
999
Jim Grosbach4b905842013-09-20 23:08:21 +00001000/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001001///
Jim Grosbachbd164242011-08-20 16:24:13 +00001002/// expr ::= expr &&,|| expr -> lowest.
1003/// expr ::= expr |,^,&,! expr
1004/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1005/// expr ::= expr <<,>> expr
1006/// expr ::= expr +,- expr
1007/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001008/// expr ::= primaryexpr
1009///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001010bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001011 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001012 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001013 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001014 return true;
1015
Daniel Dunbar55f16672010-09-17 02:47:07 +00001016 // As a special case, we support 'a op b @ modifier' by rewriting the
1017 // expression to include the modifier. This is inefficient, but in general we
1018 // expect users to use 'a@modifier op b'.
1019 if (Lexer.getKind() == AsmToken::At) {
1020 Lex();
1021
1022 if (Lexer.isNot(AsmToken::Identifier))
1023 return TokError("unexpected symbol modifier following '@'");
1024
1025 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001026 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 if (Variant == MCSymbolRefExpr::VK_Invalid)
1028 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1029
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (!ModifiedRes) {
1032 return TokError("invalid modifier '" + getTok().getIdentifier() +
1033 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001035
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 Res = ModifiedRes;
1037 Lex();
1038 }
1039
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001040 // Try to constant fold it up front, if possible.
1041 int64_t Value;
1042 if (Res->EvaluateAsAbsolute(Value))
1043 Res = MCConstantExpr::Create(Value, getContext());
1044
1045 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001046}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001049 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001050 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001051}
1052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001053bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001054 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001055
Daniel Dunbar75630b32009-06-30 02:10:03 +00001056 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001058 return true;
1059
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001060 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001061 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062
1063 return false;
1064}
1065
Michael J. Spencer530ce852010-10-09 11:00:50 +00001066static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001067 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001068 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001069 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071
Jim Grosbach4b905842013-09-20 23:08:21 +00001072 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001073 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001075 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001076 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001078 return 1;
1079
Jim Grosbach4b905842013-09-20 23:08:21 +00001080 // Low Precedence: |, &, ^
1081 //
1082 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001092
Jim Grosbach4b905842013-09-20 23:08:21 +00001093 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 case AsmToken::EqualEqual:
1095 Kind = MCBinaryExpr::EQ;
1096 return 3;
1097 case AsmToken::ExclaimEqual:
1098 case AsmToken::LessGreater:
1099 Kind = MCBinaryExpr::NE;
1100 return 3;
1101 case AsmToken::Less:
1102 Kind = MCBinaryExpr::LT;
1103 return 3;
1104 case AsmToken::LessEqual:
1105 Kind = MCBinaryExpr::LTE;
1106 return 3;
1107 case AsmToken::Greater:
1108 Kind = MCBinaryExpr::GT;
1109 return 3;
1110 case AsmToken::GreaterEqual:
1111 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001112 return 3;
1113
Jim Grosbach4b905842013-09-20 23:08:21 +00001114 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001115 case AsmToken::LessLess:
1116 Kind = MCBinaryExpr::Shl;
1117 return 4;
1118 case AsmToken::GreaterGreater:
1119 Kind = MCBinaryExpr::Shr;
1120 return 4;
1121
Jim Grosbach4b905842013-09-20 23:08:21 +00001122 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001123 case AsmToken::Plus:
1124 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126 case AsmToken::Minus:
1127 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001129
Jim Grosbach4b905842013-09-20 23:08:21 +00001130 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001137 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001138 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001140 }
1141}
1142
Jim Grosbach4b905842013-09-20 23:08:21 +00001143/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001145bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001146 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001147 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001149 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 // If the next token is lower precedence than we are allowed to eat, return
1152 // successfully with what we ate already.
1153 if (TokPrec < Precedence)
1154 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Sean Callanan686ed8d2010-01-19 20:22:31 +00001156 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001159 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001160 if (parsePrimaryExpr(RHS, EndLoc))
1161 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // If BinOp binds less tightly with RHS than the operator after RHS, let
1164 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001165 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001166 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001167 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1168 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001171 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 }
1173}
1174
Chris Lattner36e02122009-06-21 20:54:55 +00001175/// ParseStatement:
1176/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001177/// ::= Label* Directive ...Operands... EndOfStatement
1178/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001179bool AsmParser::parseStatement(ParseStatementInfo &Info,
1180 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001181 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001182 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001183 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001184 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001185 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001187 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001188 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001189 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001193 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001194 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001197 if (Lexer.is(AsmToken::Integer)) {
1198 LocalLabelVal = getTok().getIntVal();
1199 if (LocalLabelVal < 0) {
1200 if (!TheCondState.Ignore)
1201 return TokError("unexpected token at start of statement");
1202 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001203 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 IDVal = getTok().getString();
1205 Lex(); // Consume the integer token to be used as an identifier token.
1206 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001207 if (!TheCondState.Ignore)
1208 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 }
1210 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001211 } else if (Lexer.is(AsmToken::Dot)) {
1212 // Treat '.' as a valid identifier in this context.
1213 Lex();
1214 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001215 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
1218 IDVal = "";
1219 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001220
Chris Lattner926885c2010-04-17 18:14:27 +00001221 // Handle conditional assembly here before checking for skipping. We
1222 // have to do this so that .endif isn't skipped in a ".if 0" block for
1223 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001224 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001225 DirectiveKindMap.find(IDVal);
1226 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1227 ? DK_NO_DIRECTIVE
1228 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001229 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 default:
1231 break;
1232 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001233 case DK_IFEQ:
1234 case DK_IFGE:
1235 case DK_IFGT:
1236 case DK_IFLE:
1237 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001238 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001239 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 case DK_IFB:
1241 return parseDirectiveIfb(IDLoc, true);
1242 case DK_IFNB:
1243 return parseDirectiveIfb(IDLoc, false);
1244 case DK_IFC:
1245 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001246 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001247 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001248 case DK_IFNC:
1249 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001250 case DK_IFNES:
1251 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001252 case DK_IFDEF:
1253 return parseDirectiveIfdef(IDLoc, true);
1254 case DK_IFNDEF:
1255 case DK_IFNOTDEF:
1256 return parseDirectiveIfdef(IDLoc, false);
1257 case DK_ELSEIF:
1258 return parseDirectiveElseIf(IDLoc);
1259 case DK_ELSE:
1260 return parseDirectiveElse(IDLoc);
1261 case DK_ENDIF:
1262 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001263 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001264
Eli Bendersky88024712013-01-16 19:32:36 +00001265 // Ignore the statement if in the middle of inactive conditional
1266 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001267 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001268 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001269 return false;
1270 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001271
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001272 // FIXME: Recurse on local labels?
1273
1274 // See what kind of statement we have.
1275 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001276 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001277 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001278
Chris Lattner36e02122009-06-21 20:54:55 +00001279 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001281
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001282 // Diagnose attempt to use '.' as a label.
1283 if (IDVal == ".")
1284 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1285
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001286 // Diagnose attempt to use a variable as a label.
1287 //
1288 // FIXME: Diagnostics. Note the location of the definition as a label.
1289 // FIXME: This doesn't diagnose assignment to a symbol which has been
1290 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001291 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001292 if (LocalLabelVal == -1) {
1293 if (ParsingInlineAsm && SI) {
1294 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1295 assert(RewrittenLabel.size() && "We should have an internal name here.");
1296 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1297 IDVal.size(), RewrittenLabel));
1298 IDVal = RewrittenLabel;
1299 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001300 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001301 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001302 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001303
1304 Sym->redefineIfPossible();
1305
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001306 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001307 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001308
Daniel Dunbare73b2672009-08-26 22:13:22 +00001309 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001310 if (!ParsingInlineAsm)
1311 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001312
Kevin Enderbye7739d42011-12-09 18:09:40 +00001313 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001314 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001315 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001316 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1317 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001318
Tim Northover1744d0a2013-10-25 12:49:50 +00001319 getTargetParser().onLabelParsed(Sym);
1320
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001321 // Consume any end of statement token, if present, to avoid spurious
1322 // AddBlankLine calls().
1323 if (Lexer.is(AsmToken::EndOfStatement)) {
1324 Lex();
1325 if (Lexer.is(AsmToken::Eof))
1326 return false;
1327 }
1328
Eli Friedman0f4871d2012-10-22 23:58:19 +00001329 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001330 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001331
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001332 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001334 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335
Jim Grosbach4b905842013-09-20 23:08:21 +00001336 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337
1338 default: // Normal instruction or directive.
1339 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001340 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001341
1342 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001343 if (areMacrosEnabled())
1344 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1345 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001346 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001347
Michael J. Spencer530ce852010-10-09 11:00:50 +00001348 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001349
Eli Bendersky17233942013-01-15 22:59:42 +00001350 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001351 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001353 //
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // 1. The target-specific assembly parser. Some directives are target
1355 // specific or may potentially behave differently on certain targets.
1356 // 2. Asm parser extensions. For example, platform-specific parsers
1357 // (like the ELF parser) register themselves as extensions.
1358 // 3. The generic directive parser implemented by this class. These are
1359 // all the directives that behave in a target and platform independent
1360 // manner, or at least have a default behavior that's shared between
1361 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001362
Eli Bendersky17233942013-01-15 22:59:42 +00001363 // First query the target-specific parser. It will return 'true' if it
1364 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001365 if (!getTargetParser().ParseDirective(ID))
1366 return false;
1367
Alp Tokercb402912014-01-24 17:20:08 +00001368 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001369 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001370 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1371 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001372 if (Handler.first)
1373 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1374
1375 // Finally, if no one else is interested in this directive, it must be
1376 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001377 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001378 default:
1379 break;
1380 case DK_SET:
1381 case DK_EQU:
1382 return parseDirectiveSet(IDVal, true);
1383 case DK_EQUIV:
1384 return parseDirectiveSet(IDVal, false);
1385 case DK_ASCII:
1386 return parseDirectiveAscii(IDVal, false);
1387 case DK_ASCIZ:
1388 case DK_STRING:
1389 return parseDirectiveAscii(IDVal, true);
1390 case DK_BYTE:
1391 return parseDirectiveValue(1);
1392 case DK_SHORT:
1393 case DK_VALUE:
1394 case DK_2BYTE:
1395 return parseDirectiveValue(2);
1396 case DK_LONG:
1397 case DK_INT:
1398 case DK_4BYTE:
1399 return parseDirectiveValue(4);
1400 case DK_QUAD:
1401 case DK_8BYTE:
1402 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001403 case DK_OCTA:
1404 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001405 case DK_SINGLE:
1406 case DK_FLOAT:
1407 return parseDirectiveRealValue(APFloat::IEEEsingle);
1408 case DK_DOUBLE:
1409 return parseDirectiveRealValue(APFloat::IEEEdouble);
1410 case DK_ALIGN: {
1411 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1412 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1413 }
1414 case DK_ALIGN32: {
1415 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1416 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1417 }
1418 case DK_BALIGN:
1419 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1420 case DK_BALIGNW:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1422 case DK_BALIGNL:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1424 case DK_P2ALIGN:
1425 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1426 case DK_P2ALIGNW:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1428 case DK_P2ALIGNL:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1430 case DK_ORG:
1431 return parseDirectiveOrg();
1432 case DK_FILL:
1433 return parseDirectiveFill();
1434 case DK_ZERO:
1435 return parseDirectiveZero();
1436 case DK_EXTERN:
1437 eatToEndOfStatement(); // .extern is the default, ignore it.
1438 return false;
1439 case DK_GLOBL:
1440 case DK_GLOBAL:
1441 return parseDirectiveSymbolAttribute(MCSA_Global);
1442 case DK_LAZY_REFERENCE:
1443 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1444 case DK_NO_DEAD_STRIP:
1445 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1446 case DK_SYMBOL_RESOLVER:
1447 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1448 case DK_PRIVATE_EXTERN:
1449 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1450 case DK_REFERENCE:
1451 return parseDirectiveSymbolAttribute(MCSA_Reference);
1452 case DK_WEAK_DEFINITION:
1453 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1454 case DK_WEAK_REFERENCE:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1456 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1458 case DK_COMM:
1459 case DK_COMMON:
1460 return parseDirectiveComm(/*IsLocal=*/false);
1461 case DK_LCOMM:
1462 return parseDirectiveComm(/*IsLocal=*/true);
1463 case DK_ABORT:
1464 return parseDirectiveAbort();
1465 case DK_INCLUDE:
1466 return parseDirectiveInclude();
1467 case DK_INCBIN:
1468 return parseDirectiveIncbin();
1469 case DK_CODE16:
1470 case DK_CODE16GCC:
1471 return TokError(Twine(IDVal) + " not supported yet");
1472 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001473 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001474 case DK_IRP:
1475 return parseDirectiveIrp(IDLoc);
1476 case DK_IRPC:
1477 return parseDirectiveIrpc(IDLoc);
1478 case DK_ENDR:
1479 return parseDirectiveEndr(IDLoc);
1480 case DK_BUNDLE_ALIGN_MODE:
1481 return parseDirectiveBundleAlignMode();
1482 case DK_BUNDLE_LOCK:
1483 return parseDirectiveBundleLock();
1484 case DK_BUNDLE_UNLOCK:
1485 return parseDirectiveBundleUnlock();
1486 case DK_SLEB128:
1487 return parseDirectiveLEB128(true);
1488 case DK_ULEB128:
1489 return parseDirectiveLEB128(false);
1490 case DK_SPACE:
1491 case DK_SKIP:
1492 return parseDirectiveSpace(IDVal);
1493 case DK_FILE:
1494 return parseDirectiveFile(IDLoc);
1495 case DK_LINE:
1496 return parseDirectiveLine();
1497 case DK_LOC:
1498 return parseDirectiveLoc();
1499 case DK_STABS:
1500 return parseDirectiveStabs();
1501 case DK_CFI_SECTIONS:
1502 return parseDirectiveCFISections();
1503 case DK_CFI_STARTPROC:
1504 return parseDirectiveCFIStartProc();
1505 case DK_CFI_ENDPROC:
1506 return parseDirectiveCFIEndProc();
1507 case DK_CFI_DEF_CFA:
1508 return parseDirectiveCFIDefCfa(IDLoc);
1509 case DK_CFI_DEF_CFA_OFFSET:
1510 return parseDirectiveCFIDefCfaOffset();
1511 case DK_CFI_ADJUST_CFA_OFFSET:
1512 return parseDirectiveCFIAdjustCfaOffset();
1513 case DK_CFI_DEF_CFA_REGISTER:
1514 return parseDirectiveCFIDefCfaRegister(IDLoc);
1515 case DK_CFI_OFFSET:
1516 return parseDirectiveCFIOffset(IDLoc);
1517 case DK_CFI_REL_OFFSET:
1518 return parseDirectiveCFIRelOffset(IDLoc);
1519 case DK_CFI_PERSONALITY:
1520 return parseDirectiveCFIPersonalityOrLsda(true);
1521 case DK_CFI_LSDA:
1522 return parseDirectiveCFIPersonalityOrLsda(false);
1523 case DK_CFI_REMEMBER_STATE:
1524 return parseDirectiveCFIRememberState();
1525 case DK_CFI_RESTORE_STATE:
1526 return parseDirectiveCFIRestoreState();
1527 case DK_CFI_SAME_VALUE:
1528 return parseDirectiveCFISameValue(IDLoc);
1529 case DK_CFI_RESTORE:
1530 return parseDirectiveCFIRestore(IDLoc);
1531 case DK_CFI_ESCAPE:
1532 return parseDirectiveCFIEscape();
1533 case DK_CFI_SIGNAL_FRAME:
1534 return parseDirectiveCFISignalFrame();
1535 case DK_CFI_UNDEFINED:
1536 return parseDirectiveCFIUndefined(IDLoc);
1537 case DK_CFI_REGISTER:
1538 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001539 case DK_CFI_WINDOW_SAVE:
1540 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001541 case DK_MACROS_ON:
1542 case DK_MACROS_OFF:
1543 return parseDirectiveMacrosOnOff(IDVal);
1544 case DK_MACRO:
1545 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001546 case DK_EXITM:
1547 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001548 case DK_ENDM:
1549 case DK_ENDMACRO:
1550 return parseDirectiveEndMacro(IDVal);
1551 case DK_PURGEM:
1552 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001553 case DK_END:
1554 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001555 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001556 return parseDirectiveError(IDLoc, false);
1557 case DK_ERROR:
1558 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001559 case DK_WARNING:
1560 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001561 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001562
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001563 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001564 }
Chris Lattner36e02122009-06-21 20:54:55 +00001565
Chad Rosierc7f552c2013-02-12 21:33:51 +00001566 // __asm _emit or __asm __emit
1567 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1568 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001569 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001570
1571 // __asm align
1572 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001573 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001574
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001575 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001576
Chris Lattner7cbfa442010-05-19 23:34:33 +00001577 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001578 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001579 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001580 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001581 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001582 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001583
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001584 // Dump the parsed representation, if requested.
1585 if (getShowParsedOperands()) {
1586 SmallString<256> Str;
1587 raw_svector_ostream OS(Str);
1588 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001589 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001590 if (i != 0)
1591 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001592 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001593 }
1594 OS << "]";
1595
Jim Grosbach4b905842013-09-20 23:08:21 +00001596 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001597 }
1598
Oliver Stannard8b273082014-06-19 15:52:37 +00001599 // If we are generating dwarf for the current section then generate a .loc
1600 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001601 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001602 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001603 getStreamer().getCurrentSection().first)) {
1604 unsigned Line;
1605 if (ActiveMacros.empty())
1606 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1607 else
1608 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1609 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001610
Eli Bendersky88024712013-01-16 19:32:36 +00001611 // If we previously parsed a cpp hash file line comment then make sure the
1612 // current Dwarf File is for the CppHashFilename if not then emit the
1613 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001614 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001615 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1616 0, StringRef(), CppHashFilename);
1617 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001618
Jim Grosbach4b905842013-09-20 23:08:21 +00001619 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1620 // cache with the different Loc from the call above we save the last
1621 // info we queried here with SrcMgr.FindLineNumber().
1622 unsigned CppHashLocLineNo;
1623 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1624 CppHashLocLineNo = LastQueryLine;
1625 else {
1626 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1627 LastQueryLine = CppHashLocLineNo;
1628 LastQueryIDLoc = CppHashLoc;
1629 LastQueryBuffer = CppHashBuf;
1630 }
1631 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001632 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001633
Jim Grosbach4b905842013-09-20 23:08:21 +00001634 getStreamer().EmitDwarfLocDirective(
1635 getContext().getGenDwarfFileNumber(), Line, 0,
1636 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1637 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001638 }
1639
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001640 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001641 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001642 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001643 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1644 Info.ParsedOperands, Out,
1645 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001646 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001647
Chris Lattnera2a9d162010-09-11 16:18:25 +00001648 // Don't skip the rest of the line, the instruction parser is responsible for
1649 // that.
1650 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001651}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001652
Jim Grosbach4b905842013-09-20 23:08:21 +00001653/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001654/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001655void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001656 if (!Lexer.is(AsmToken::EndOfStatement))
1657 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001658 // Eat EOL.
1659 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001660}
1661
Jim Grosbach4b905842013-09-20 23:08:21 +00001662/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001663/// ::= # number "filename"
1664/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001665bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001666 Lex(); // Eat the hash token.
1667
1668 if (getLexer().isNot(AsmToken::Integer)) {
1669 // Consume the line since in cases it is not a well-formed line directive,
1670 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001672 return false;
1673 }
1674
1675 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001676 Lex();
1677
1678 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001679 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001680 return false;
1681 }
1682
1683 StringRef Filename = getTok().getString();
1684 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001685 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001686
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001687 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1688 CppHashLoc = L;
1689 CppHashFilename = Filename;
1690 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001691 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001692
1693 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001695 return false;
1696}
1697
Jim Grosbach4b905842013-09-20 23:08:21 +00001698/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699/// for the Filename and LineNo if any in the diagnostic.
1700void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702 raw_ostream &OS = errs();
1703
1704 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1705 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001706 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1707 unsigned CppHashBuf =
1708 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709
Jim Grosbach4b905842013-09-20 23:08:21 +00001710 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001712 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1713 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1714 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1716 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001717 }
1718
Eric Christophera7c32732012-12-18 00:30:54 +00001719 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 // manager changed or buffer changed (like in a nested include) then just
1721 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001724 if (Parser->SavedDiagHandler)
1725 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1726 else
Craig Topper353eda42014-04-24 06:44:33 +00001727 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001728 return;
1729 }
1730
Eric Christophera7c32732012-12-18 00:30:54 +00001731 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1733 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001734 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001735
1736 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1737 int CppHashLocLineNo =
1738 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001739 int LineNo =
1740 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741
Jim Grosbach4b905842013-09-20 23:08:21 +00001742 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1743 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001744 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001745
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001746 if (Parser->SavedDiagHandler)
1747 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1748 else
Craig Topper353eda42014-04-24 06:44:33 +00001749 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001750}
1751
Rafael Espindola2c064482012-08-21 18:29:30 +00001752// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1753// difference being that that function accepts '@' as part of identifiers and
1754// we can't do that. AsmLexer.cpp should probably be changed to handle
1755// '@' as a special case when needed.
1756static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001757 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1758 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001759}
1760
Rafael Espindola34b9c512012-06-03 23:57:14 +00001761bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001762 ArrayRef<MCAsmMacroParameter> Parameters,
1763 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001765 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001766 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768
Preston Gurd05500642012-09-19 20:36:12 +00001769 // A macro without parameters is handled differently on Darwin:
1770 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001771 while (!Body.empty()) {
1772 // Scan for the next substitution.
1773 std::size_t End = Body.size(), Pos = 0;
1774 for (; Pos != End; ++Pos) {
1775 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001776 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 // This macro has no parameters, look for $0, $1, etc.
1778 if (Body[Pos] != '$' || Pos + 1 == End)
1779 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001780
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001782 if (Next == '$' || Next == 'n' ||
1783 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784 break;
1785 } else {
1786 // This macro has parameters, look for \foo, \bar, etc.
1787 if (Body[Pos] == '\\' && Pos + 1 != End)
1788 break;
1789 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001790 }
1791
1792 // Add the prefix.
1793 OS << Body.slice(0, Pos);
1794
1795 // Check if we reached the end.
1796 if (Pos == End)
1797 break;
1798
Benjamin Kramer513e7442014-02-20 13:36:32 +00001799 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 switch (Body[Pos + 1]) {
1801 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 case '$':
1803 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001804 break;
1805
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001807 case 'n':
1808 OS << A.size();
1809 break;
1810
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 default: {
1813 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001814 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 if (Index >= A.size())
1816 break;
1817
1818 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001819 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 ie = A[Index].end();
1821 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 OS << it->getString();
1823 break;
1824 }
1825 }
1826 Pos += 2;
1827 } else {
1828 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001829 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001830 ++I;
1831
Jim Grosbach4b905842013-09-20 23:08:21 +00001832 const char *Begin = Body.data() + Pos + 1;
1833 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 unsigned Index = 0;
1835 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001836 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001837 break;
1838
Preston Gurd05500642012-09-19 20:36:12 +00001839 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001840 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1841 Pos += 3;
1842 else {
1843 OS << '\\' << Argument;
1844 Pos = I;
1845 }
Preston Gurd05500642012-09-19 20:36:12 +00001846 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001847 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001848 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001849 ie = A[Index].end();
1850 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001851 // We expect no quotes around the string's contents when
1852 // parsing for varargs.
1853 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001854 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001855 else
1856 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857
Preston Gurd05500642012-09-19 20:36:12 +00001858 Pos += 1 + Argument.size();
1859 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001860 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001861 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001862 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001863 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001864
Rafael Espindola1134ab232011-06-05 02:43:45 +00001865 return false;
1866}
Daniel Dunbar43235712010-07-18 18:54:11 +00001867
Nico Weber2a8f9222014-07-24 16:29:04 +00001868MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001869 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001870 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001871 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001872
Jim Grosbach4b905842013-09-20 23:08:21 +00001873static bool isOperator(AsmToken::TokenKind kind) {
1874 switch (kind) {
1875 default:
1876 return false;
1877 case AsmToken::Plus:
1878 case AsmToken::Minus:
1879 case AsmToken::Tilde:
1880 case AsmToken::Slash:
1881 case AsmToken::Star:
1882 case AsmToken::Dot:
1883 case AsmToken::Equal:
1884 case AsmToken::EqualEqual:
1885 case AsmToken::Pipe:
1886 case AsmToken::PipePipe:
1887 case AsmToken::Caret:
1888 case AsmToken::Amp:
1889 case AsmToken::AmpAmp:
1890 case AsmToken::Exclaim:
1891 case AsmToken::ExclaimEqual:
1892 case AsmToken::Percent:
1893 case AsmToken::Less:
1894 case AsmToken::LessEqual:
1895 case AsmToken::LessLess:
1896 case AsmToken::LessGreater:
1897 case AsmToken::Greater:
1898 case AsmToken::GreaterEqual:
1899 case AsmToken::GreaterGreater:
1900 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001901 }
1902}
1903
David Majnemer16252452014-01-29 00:07:39 +00001904namespace {
1905class AsmLexerSkipSpaceRAII {
1906public:
1907 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1908 Lexer.setSkipSpace(SkipSpace);
1909 }
1910
1911 ~AsmLexerSkipSpaceRAII() {
1912 Lexer.setSkipSpace(true);
1913 }
1914
1915private:
1916 AsmLexer &Lexer;
1917};
1918}
1919
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001920bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1921
1922 if (Vararg) {
1923 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1924 StringRef Str = parseStringToEndOfStatement();
1925 MA.push_back(AsmToken(AsmToken::String, Str));
1926 }
1927 return false;
1928 }
1929
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001931 unsigned AddTokens = 0;
1932
David Majnemer16252452014-01-29 00:07:39 +00001933 // Darwin doesn't use spaces to delmit arguments.
1934 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001935
1936 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001937 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001939
David Majnemer91fc4c22014-01-29 18:57:46 +00001940 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001941 break;
Preston Gurd05500642012-09-19 20:36:12 +00001942
1943 if (Lexer.is(AsmToken::Space)) {
1944 Lex(); // Eat spaces
1945
1946 // Spaces can delimit parameters, but could also be part an expression.
1947 // If the token after a space is an operator, add the token and the next
1948 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001949 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001950 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001951 // Check to see whether the token is used as an operator,
1952 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001953 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001954 if (*NextChar == ' ')
1955 AddTokens = 2;
1956 }
1957
1958 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001959 break;
1960 }
1961 }
1962 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963
Jim Grosbach4b905842013-09-20 23:08:21 +00001964 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965 // to be able to fill in the remaining default parameter values
1966 if (Lexer.is(AsmToken::EndOfStatement))
1967 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001968
1969 // Adjust the current parentheses level.
1970 if (Lexer.is(AsmToken::LParen))
1971 ++ParenLevel;
1972 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1973 --ParenLevel;
1974
1975 // Append the token to the current argument list.
1976 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001977 if (AddTokens)
1978 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001979 Lex();
1980 }
Preston Gurd05500642012-09-19 20:36:12 +00001981
Rafael Espindola768b41c2012-06-15 14:02:34 +00001982 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001983 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984 return false;
1985}
1986
1987// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001988bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001989 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001991 bool NamedParametersFound = false;
1992 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001994 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001995 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997 // Parse two kinds of macro invocations:
1998 // - macros defined without any parameters accept an arbitrary number of them
1999 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002000 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002001 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2002 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002003 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002004 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002005
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002006 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002007 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002008 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002009 eatToEndOfStatement();
2010 return true;
2011 }
2012
2013 if (!Lexer.is(AsmToken::Equal)) {
2014 TokError("expected '=' after formal parameter identifier");
2015 eatToEndOfStatement();
2016 return true;
2017 }
2018 Lex();
2019
2020 NamedParametersFound = true;
2021 }
2022
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002023 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002024 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 eatToEndOfStatement();
2026 return true;
2027 }
2028
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002029 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2030 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002031 return true;
2032
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002034 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002035 unsigned FAI = 0;
2036 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002037 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002038 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002039
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002040 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002041 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002042 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002043 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002044 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002045 return true;
2046 }
2047 PI = FAI;
2048 }
2049
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002050 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002051 if (A.size() <= PI)
2052 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002053 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002054
2055 if (FALocs.size() <= PI)
2056 FALocs.resize(PI + 1);
2057
2058 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002059 }
Jim Grosbach206661622012-07-30 22:44:17 +00002060
Preston Gurd242ed3152012-09-19 20:29:04 +00002061 // At the end of the statement, fill in remaining arguments that have
2062 // default values. If there aren't any, then the next argument is
2063 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002064 if (Lexer.is(AsmToken::EndOfStatement)) {
2065 bool Failure = false;
2066 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2067 if (A[FAI].empty()) {
2068 if (M->Parameters[FAI].Required) {
2069 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2070 "missing value for required parameter "
2071 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2072 Failure = true;
2073 }
2074
2075 if (!M->Parameters[FAI].Value.empty())
2076 A[FAI] = M->Parameters[FAI].Value;
2077 }
2078 }
2079 return Failure;
2080 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002081
2082 if (Lexer.is(AsmToken::Comma))
2083 Lex();
2084 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002085
2086 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002087}
2088
Jim Grosbach4b905842013-09-20 23:08:21 +00002089const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002090 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2091 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002092}
2093
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002094void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2095 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002096}
2097
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002098void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002099
Jim Grosbach4b905842013-09-20 23:08:21 +00002100bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002101 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2102 // this, although we should protect against infinite loops.
2103 if (ActiveMacros.size() == 20)
2104 return TokError("macros cannot be nested more than 20 levels deep");
2105
Eli Bendersky38274122013-01-14 23:22:36 +00002106 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002107 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002108 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002109
Rafael Espindola1134ab232011-06-05 02:43:45 +00002110 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2111 // to hold the macro body with substitutions.
2112 SmallString<256> Buf;
2113 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002114 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002115
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002116 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002117 return true;
2118
Eli Bendersky38274122013-01-14 23:22:36 +00002119 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002120 // instantiation.
2121 OS << ".endmacro\n";
2122
Rafael Espindola3560ff22014-08-27 20:03:13 +00002123 std::unique_ptr<MemoryBuffer> Instantiation =
2124 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002125
Daniel Dunbar43235712010-07-18 18:54:11 +00002126 // Create the macro instantiation object and add to the current macro
2127 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002128 MacroInstantiation *MI = new MacroInstantiation(
2129 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002130 ActiveMacros.push_back(MI);
2131
2132 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002133 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002134 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002135 Lex();
2136
2137 return false;
2138}
2139
Jim Grosbach4b905842013-09-20 23:08:21 +00002140void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002141 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002142 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002143 Lex();
2144
2145 // Pop the instantiation entry.
2146 delete ActiveMacros.back();
2147 ActiveMacros.pop_back();
2148}
2149
Jim Grosbach4b905842013-09-20 23:08:21 +00002150static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002151 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002152 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002153 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2154 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002156 case MCExpr::Target:
2157 case MCExpr::Constant:
2158 return false;
2159 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 const MCSymbol &S =
2161 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002162 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002163 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002164 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002165 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002166 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002168 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002169
2170 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002171}
2172
Jim Grosbach4b905842013-09-20 23:08:21 +00002173bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002174 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002175 // FIXME: Use better location, we should use proper tokens.
2176 SMLoc EqualLoc = Lexer.getLoc();
2177
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002178 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002179 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002180 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002181
Rafael Espindola72f5f172012-01-28 05:57:00 +00002182 // Note: we don't count b as used in "a = b". This is to allow
2183 // a = b
2184 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002185
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002186 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187 return TokError("unexpected token in assignment");
2188
2189 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002190 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002191
Daniel Dunbar5f339242009-10-16 01:57:39 +00002192 // Validate that the LHS is allowed to be a variable (either it has not been
2193 // used as a symbol, or it is an absolute symbol).
2194 MCSymbol *Sym = getContext().LookupSymbol(Name);
2195 if (Sym) {
2196 // Diagnose assignment to a label.
2197 //
2198 // FIXME: Diagnostics. Note the location of the definition as a label.
2199 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002200 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002201 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2202 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002203 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002204 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2205 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002206 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002207 return Error(EqualLoc, "redefinition of '" + Name + "'");
2208 else if (!Sym->isVariable())
2209 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002210 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002211 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002212 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002213
2214 // Don't count these checks as uses.
2215 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002216 } else if (Name == ".") {
2217 if (Out.EmitValueToOffset(Value, 0)) {
2218 Error(EqualLoc, "expected absolute expression");
2219 eatToEndOfStatement();
2220 }
2221 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002223 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002224
David Majnemer58cb80c2014-12-24 10:27:50 +00002225 Sym->setRedefinable(allow_redef);
2226
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002227 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002228 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002229 if (NoDeadStrip)
2230 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2231
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002232 return false;
2233}
2234
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002235/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002236/// ::= identifier
2237/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002238bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002239 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2241 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242 // handle this as a context dependent token, instead we detect adjacent tokens
2243 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002244 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2245 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002246
Hans Wennborgce69d772013-10-18 20:46:28 +00002247 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002248 Lex();
2249 if (Lexer.isNot(AsmToken::Identifier))
2250 return true;
2251
Hans Wennborgce69d772013-10-18 20:46:28 +00002252 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2253 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002254 return true;
2255
2256 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002257 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002258 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002259 Lex();
2260 return false;
2261 }
2262
Jim Grosbach4b905842013-09-20 23:08:21 +00002263 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002264 return true;
2265
Sean Callanan936b0d32010-01-19 21:44:56 +00002266 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002267
Sean Callanan686ed8d2010-01-19 20:22:31 +00002268 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002269
2270 return false;
2271}
2272
Jim Grosbach4b905842013-09-20 23:08:21 +00002273/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002274/// ::= .equ identifier ',' expression
2275/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002276/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002277bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002278 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002279
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002280 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002281 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002282
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002283 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002284 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002285 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002286
Jim Grosbach4b905842013-09-20 23:08:21 +00002287 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002288}
2289
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002290bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002291 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002292
2293 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002294 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002295 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2296 if (Str[i] != '\\') {
2297 Data += Str[i];
2298 continue;
2299 }
2300
2301 // Recognize escaped characters. Note that this escape semantics currently
2302 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2303 ++i;
2304 if (i == e)
2305 return TokError("unexpected backslash at end of string");
2306
2307 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 // Consume up to three octal characters.
2310 unsigned Value = Str[i] - '0';
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 ++i;
2314 Value = Value * 8 + (Str[i] - '0');
2315
Jim Grosbach4b905842013-09-20 23:08:21 +00002316 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002317 ++i;
2318 Value = Value * 8 + (Str[i] - '0');
2319 }
2320 }
2321
2322 if (Value > 255)
2323 return TokError("invalid octal escape sequence (out of range)");
2324
Jim Grosbach4b905842013-09-20 23:08:21 +00002325 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002326 continue;
2327 }
2328
2329 // Otherwise recognize individual escapes.
2330 switch (Str[i]) {
2331 default:
2332 // Just reject invalid escape sequences for now.
2333 return TokError("invalid escape sequence (unrecognized character)");
2334
2335 case 'b': Data += '\b'; break;
2336 case 'f': Data += '\f'; break;
2337 case 'n': Data += '\n'; break;
2338 case 'r': Data += '\r'; break;
2339 case 't': Data += '\t'; break;
2340 case '"': Data += '"'; break;
2341 case '\\': Data += '\\'; break;
2342 }
2343 }
2344
2345 return false;
2346}
2347
Jim Grosbach4b905842013-09-20 23:08:21 +00002348/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002349/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002350bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002352 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002353
Daniel Dunbara10e5192009-06-24 23:30:00 +00002354 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002356 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357
Daniel Dunbaref668c12009-08-14 18:19:52 +00002358 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002359 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002360 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Rafael Espindola64e1af82013-07-02 15:49:13 +00002362 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002363 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002364 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365
Sean Callanan686ed8d2010-01-19 20:22:31 +00002366 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367
2368 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002369 break;
2370
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002371 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002372 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 }
2375 }
2376
Sean Callanan686ed8d2010-01-19 20:22:31 +00002377 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378 return false;
2379}
2380
Jim Grosbach4b905842013-09-20 23:08:21 +00002381/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002382/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002383bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002384 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002385 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002386
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002388 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002389 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002391 return true;
2392
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002393 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002394 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2395 assert(Size <= 8 && "Invalid size");
2396 uint64_t IntValue = MCE->getValue();
2397 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2398 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002399 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002400 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002401 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002403 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002405
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002407 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 }
2411 }
2412
Sean Callanan686ed8d2010-01-19 20:22:31 +00002413 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002414 return false;
2415}
2416
David Woodhoused6de0d92014-02-01 16:20:59 +00002417/// ParseDirectiveOctaValue
2418/// ::= .octa [ hexconstant (, hexconstant)* ]
2419bool AsmParser::parseDirectiveOctaValue() {
2420 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2421 checkForValidSection();
2422
2423 for (;;) {
2424 if (Lexer.getKind() == AsmToken::Error)
2425 return true;
2426 if (Lexer.getKind() != AsmToken::Integer &&
2427 Lexer.getKind() != AsmToken::BigNum)
2428 return TokError("unknown token in expression");
2429
2430 SMLoc ExprLoc = getLexer().getLoc();
2431 APInt IntValue = getTok().getAPIntVal();
2432 Lex();
2433
2434 uint64_t hi, lo;
2435 if (IntValue.isIntN(64)) {
2436 hi = 0;
2437 lo = IntValue.getZExtValue();
2438 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002439 // It might actually have more than 128 bits, but the top ones are zero.
2440 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002441 lo = IntValue.getLoBits(64).getZExtValue();
2442 } else
2443 return Error(ExprLoc, "literal value out of range for directive");
2444
2445 if (MAI.isLittleEndian()) {
2446 getStreamer().EmitIntValue(lo, 8);
2447 getStreamer().EmitIntValue(hi, 8);
2448 } else {
2449 getStreamer().EmitIntValue(hi, 8);
2450 getStreamer().EmitIntValue(lo, 8);
2451 }
2452
2453 if (getLexer().is(AsmToken::EndOfStatement))
2454 break;
2455
2456 // FIXME: Improve diagnostic.
2457 if (getLexer().isNot(AsmToken::Comma))
2458 return TokError("unexpected token in directive");
2459 Lex();
2460 }
2461 }
2462
2463 Lex();
2464 return false;
2465}
2466
Jim Grosbach4b905842013-09-20 23:08:21 +00002467/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002469bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002471 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002472
2473 for (;;) {
2474 // We don't truly support arithmetic on floating point expressions, so we
2475 // have to manually parse unary prefixes.
2476 bool IsNeg = false;
2477 if (getLexer().is(AsmToken::Minus)) {
2478 Lex();
2479 IsNeg = true;
2480 } else if (getLexer().is(AsmToken::Plus))
2481 Lex();
2482
Michael J. Spencer530ce852010-10-09 11:00:50 +00002483 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002484 getLexer().isNot(AsmToken::Real) &&
2485 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002486 return TokError("unexpected token in directive");
2487
2488 // Convert to an APFloat.
2489 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002490 StringRef IDVal = getTok().getString();
2491 if (getLexer().is(AsmToken::Identifier)) {
2492 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2493 Value = APFloat::getInf(Semantics);
2494 else if (!IDVal.compare_lower("nan"))
2495 Value = APFloat::getNaN(Semantics, false, ~0);
2496 else
2497 return TokError("invalid floating point literal");
2498 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002499 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002500 return TokError("invalid floating point literal");
2501 if (IsNeg)
2502 Value.changeSign();
2503
2504 // Consume the numeric token.
2505 Lex();
2506
2507 // Emit the value as an integer.
2508 APInt AsInt = Value.bitcastToAPInt();
2509 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002510 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002511
2512 if (getLexer().is(AsmToken::EndOfStatement))
2513 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002514
Daniel Dunbar2af16532010-09-24 01:59:56 +00002515 if (getLexer().isNot(AsmToken::Comma))
2516 return TokError("unexpected token in directive");
2517 Lex();
2518 }
2519 }
2520
2521 Lex();
2522 return false;
2523}
2524
Jim Grosbach4b905842013-09-20 23:08:21 +00002525/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002526/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002527bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002528 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002529
2530 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002531 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002532 return true;
2533
Rafael Espindolab91bac62010-10-05 19:42:57 +00002534 int64_t Val = 0;
2535 if (getLexer().is(AsmToken::Comma)) {
2536 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002537 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002538 return true;
2539 }
2540
Rafael Espindola922e3f42010-09-16 15:03:59 +00002541 if (getLexer().isNot(AsmToken::EndOfStatement))
2542 return TokError("unexpected token in '.zero' directive");
2543
2544 Lex();
2545
Rafael Espindola64e1af82013-07-02 15:49:13 +00002546 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002547
2548 return false;
2549}
2550
Jim Grosbach4b905842013-09-20 23:08:21 +00002551/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002552/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002553bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002555
David Majnemer522d3db2014-02-01 07:19:38 +00002556 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002557 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002558 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002559 return true;
2560
David Majnemer522d3db2014-02-01 07:19:38 +00002561 if (NumValues < 0) {
2562 Warning(RepeatLoc,
2563 "'.fill' directive with negative repeat count has no effect");
2564 NumValues = 0;
2565 }
2566
Roman Divackye33098f2013-09-24 17:44:41 +00002567 int64_t FillSize = 1;
2568 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002569
David Majnemer522d3db2014-02-01 07:19:38 +00002570 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002571 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2572 if (getLexer().isNot(AsmToken::Comma))
2573 return TokError("unexpected token in '.fill' directive");
2574 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002575
David Majnemer522d3db2014-02-01 07:19:38 +00002576 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002577 if (parseAbsoluteExpression(FillSize))
2578 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002579
Roman Divackye33098f2013-09-24 17:44:41 +00002580 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2581 if (getLexer().isNot(AsmToken::Comma))
2582 return TokError("unexpected token in '.fill' directive");
2583 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002584
David Majnemer522d3db2014-02-01 07:19:38 +00002585 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002586 if (parseAbsoluteExpression(FillExpr))
2587 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002588
Roman Divackye33098f2013-09-24 17:44:41 +00002589 if (getLexer().isNot(AsmToken::EndOfStatement))
2590 return TokError("unexpected token in '.fill' directive");
2591
2592 Lex();
2593 }
2594 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002595
David Majnemer522d3db2014-02-01 07:19:38 +00002596 if (FillSize < 0) {
2597 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2598 NumValues = 0;
2599 }
2600 if (FillSize > 8) {
2601 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2602 FillSize = 8;
2603 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002604
David Majnemer522d3db2014-02-01 07:19:38 +00002605 if (!isUInt<32>(FillExpr) && FillSize > 4)
2606 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2607
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002608 if (NumValues > 0) {
2609 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2610 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2611 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2612 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2613 if (NonZeroFillSize < FillSize)
2614 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2615 }
David Majnemer522d3db2014-02-01 07:19:38 +00002616 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002617
2618 return false;
2619}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002620
Jim Grosbach4b905842013-09-20 23:08:21 +00002621/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002622/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002623bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002624 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002625
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002626 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002627 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002628 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002629 return true;
2630
2631 // Parse optional fill expression.
2632 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002633 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2634 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002635 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002636 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002637
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002638 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002639 return true;
2640
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002641 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002642 return TokError("unexpected token in '.org' directive");
2643 }
2644
Sean Callanan686ed8d2010-01-19 20:22:31 +00002645 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002646
Jim Grosbachb5912772012-01-27 00:37:08 +00002647 // Only limited forms of relocatable expressions are accepted here, it
2648 // has to be relative to the current section. The streamer will return
2649 // 'true' if the expression wasn't evaluatable.
2650 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2651 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002652
2653 return false;
2654}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655
Jim Grosbach4b905842013-09-20 23:08:21 +00002656/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002657/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002658bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002659 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002660
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002661 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002663 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002664 return true;
2665
2666 SMLoc MaxBytesLoc;
2667 bool HasFillExpr = false;
2668 int64_t FillExpr = 0;
2669 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002670 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2671 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002673 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002674
2675 // The fill expression can be omitted while specifying a maximum number of
2676 // alignment bytes, e.g:
2677 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002678 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002680 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681 return true;
2682 }
2683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002684 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2685 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002687 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002688
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002689 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002690 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002692
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002693 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002694 return TokError("unexpected token in directive");
2695 }
2696 }
2697
Sean Callanan686ed8d2010-01-19 20:22:31 +00002698 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002700 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002701 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002702
2703 // Compute alignment in bytes.
2704 if (IsPow2) {
2705 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002706 if (Alignment >= 32) {
2707 Error(AlignmentLoc, "invalid alignment value");
2708 Alignment = 31;
2709 }
2710
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002711 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002712 } else {
2713 // Reject alignments that aren't a power of two, for gas compatibility.
2714 if (!isPowerOf2_64(Alignment))
2715 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002716 }
2717
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002718 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002719 if (MaxBytesLoc.isValid()) {
2720 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002721 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002722 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002723 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002724 }
2725
2726 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002727 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002728 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002729 MaxBytesToFill = 0;
2730 }
2731 }
2732
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002733 // Check whether we should use optimal code alignment for this .align
2734 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002735 const MCSection *Section = getStreamer().getCurrentSection().first;
2736 assert(Section && "must have section to emit alignment");
2737 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002738 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2739 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002740 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002741 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002742 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002743 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2744 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002745 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002746
2747 return false;
2748}
2749
Jim Grosbach4b905842013-09-20 23:08:21 +00002750/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002751/// ::= .file [number] filename
2752/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002753bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002754 // FIXME: I'm not sure what this is.
2755 int64_t FileNumber = -1;
2756 SMLoc FileNumberLoc = getLexer().getLoc();
2757 if (getLexer().is(AsmToken::Integer)) {
2758 FileNumber = getTok().getIntVal();
2759 Lex();
2760
2761 if (FileNumber < 1)
2762 return TokError("file number less than one");
2763 }
2764
2765 if (getLexer().isNot(AsmToken::String))
2766 return TokError("unexpected token in '.file' directive");
2767
2768 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002769 // Allow the strings to have escaped octal character sequence.
2770 std::string Path = getTok().getString();
2771 if (parseEscapedString(Path))
2772 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002773 Lex();
2774
2775 StringRef Directory;
2776 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002777 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002778 if (getLexer().is(AsmToken::String)) {
2779 if (FileNumber == -1)
2780 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002781 if (parseEscapedString(FilenameData))
2782 return true;
2783 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002784 Directory = Path;
2785 Lex();
2786 } else {
2787 Filename = Path;
2788 }
2789
2790 if (getLexer().isNot(AsmToken::EndOfStatement))
2791 return TokError("unexpected token in '.file' directive");
2792
2793 if (FileNumber == -1)
2794 getStreamer().EmitFileDirective(Filename);
2795 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002796 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002797 Error(DirectiveLoc,
2798 "input can't have .file dwarf directives when -g is "
2799 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002800
David Blaikiec714ef42014-03-17 01:52:11 +00002801 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2802 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002803 Error(FileNumberLoc, "file number already allocated");
2804 }
2805
2806 return false;
2807}
2808
Jim Grosbach4b905842013-09-20 23:08:21 +00002809/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002810/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002811bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002812 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2813 if (getLexer().isNot(AsmToken::Integer))
2814 return TokError("unexpected token in '.line' directive");
2815
2816 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002817 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002818 Lex();
2819
2820 // FIXME: Do something with the .line.
2821 }
2822
2823 if (getLexer().isNot(AsmToken::EndOfStatement))
2824 return TokError("unexpected token in '.line' directive");
2825
2826 return false;
2827}
2828
Jim Grosbach4b905842013-09-20 23:08:21 +00002829/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002830/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2831/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2832/// The first number is a file number, must have been previously assigned with
2833/// a .file directive, the second number is the line number and optionally the
2834/// third number is a column position (zero if not specified). The remaining
2835/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002836bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002837 if (getLexer().isNot(AsmToken::Integer))
2838 return TokError("unexpected token in '.loc' directive");
2839 int64_t FileNumber = getTok().getIntVal();
2840 if (FileNumber < 1)
2841 return TokError("file number less than one in '.loc' directive");
2842 if (!getContext().isValidDwarfFileNumber(FileNumber))
2843 return TokError("unassigned file number in '.loc' directive");
2844 Lex();
2845
2846 int64_t LineNumber = 0;
2847 if (getLexer().is(AsmToken::Integer)) {
2848 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002849 if (LineNumber < 0)
2850 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002851 Lex();
2852 }
2853
2854 int64_t ColumnPos = 0;
2855 if (getLexer().is(AsmToken::Integer)) {
2856 ColumnPos = getTok().getIntVal();
2857 if (ColumnPos < 0)
2858 return TokError("column position less than zero in '.loc' directive");
2859 Lex();
2860 }
2861
2862 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2863 unsigned Isa = 0;
2864 int64_t Discriminator = 0;
2865 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2866 for (;;) {
2867 if (getLexer().is(AsmToken::EndOfStatement))
2868 break;
2869
2870 StringRef Name;
2871 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002872 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002873 return TokError("unexpected token in '.loc' directive");
2874
2875 if (Name == "basic_block")
2876 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2877 else if (Name == "prologue_end")
2878 Flags |= DWARF2_FLAG_PROLOGUE_END;
2879 else if (Name == "epilogue_begin")
2880 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2881 else if (Name == "is_stmt") {
2882 Loc = getTok().getLoc();
2883 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002884 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002885 return true;
2886 // The expression must be the constant 0 or 1.
2887 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2888 int Value = MCE->getValue();
2889 if (Value == 0)
2890 Flags &= ~DWARF2_FLAG_IS_STMT;
2891 else if (Value == 1)
2892 Flags |= DWARF2_FLAG_IS_STMT;
2893 else
2894 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002895 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2897 }
Craig Topperf15655b2013-04-22 04:22:40 +00002898 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002899 Loc = getTok().getLoc();
2900 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002901 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002902 return true;
2903 // The expression must be a constant greater or equal to 0.
2904 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2905 int Value = MCE->getValue();
2906 if (Value < 0)
2907 return Error(Loc, "isa number less than zero");
2908 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002909 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002910 return Error(Loc, "isa number not a constant value");
2911 }
Craig Topperf15655b2013-04-22 04:22:40 +00002912 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002913 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002915 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002916 return Error(Loc, "unknown sub-directive in '.loc' directive");
2917 }
2918
2919 if (getLexer().is(AsmToken::EndOfStatement))
2920 break;
2921 }
2922 }
2923
2924 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2925 Isa, Discriminator, StringRef());
2926
2927 return false;
2928}
2929
Jim Grosbach4b905842013-09-20 23:08:21 +00002930/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002931/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002933 return TokError("unsupported directive '.stabs'");
2934}
2935
Jim Grosbach4b905842013-09-20 23:08:21 +00002936/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002937/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002938bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002939 StringRef Name;
2940 bool EH = false;
2941 bool Debug = false;
2942
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002943 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002944 return TokError("Expected an identifier");
2945
2946 if (Name == ".eh_frame")
2947 EH = true;
2948 else if (Name == ".debug_frame")
2949 Debug = true;
2950
2951 if (getLexer().is(AsmToken::Comma)) {
2952 Lex();
2953
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002954 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002955 return TokError("Expected an identifier");
2956
2957 if (Name == ".eh_frame")
2958 EH = true;
2959 else if (Name == ".debug_frame")
2960 Debug = true;
2961 }
2962
2963 getStreamer().EmitCFISections(EH, Debug);
2964 return false;
2965}
2966
Jim Grosbach4b905842013-09-20 23:08:21 +00002967/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002968/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002969bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002970 StringRef Simple;
2971 if (getLexer().isNot(AsmToken::EndOfStatement))
2972 if (parseIdentifier(Simple) || Simple != "simple")
2973 return TokError("unexpected token in .cfi_startproc directive");
2974
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00002975 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002976 return false;
2977}
2978
Jim Grosbach4b905842013-09-20 23:08:21 +00002979/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002980/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002981bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002982 getStreamer().EmitCFIEndProc();
2983 return false;
2984}
2985
Jim Grosbach4b905842013-09-20 23:08:21 +00002986/// \brief parse register name or number.
2987bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002988 SMLoc DirectiveLoc) {
2989 unsigned RegNo;
2990
2991 if (getLexer().isNot(AsmToken::Integer)) {
2992 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2993 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002994 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002995 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002996 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002997
2998 return false;
2999}
3000
Jim Grosbach4b905842013-09-20 23:08:21 +00003001/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003002/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003003bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003004 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003005 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return true;
3007
3008 if (getLexer().isNot(AsmToken::Comma))
3009 return TokError("unexpected token in directive");
3010 Lex();
3011
3012 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003013 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003014 return true;
3015
3016 getStreamer().EmitCFIDefCfa(Register, Offset);
3017 return false;
3018}
3019
Jim Grosbach4b905842013-09-20 23:08:21 +00003020/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003021/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003022bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003023 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003024 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003025 return true;
3026
3027 getStreamer().EmitCFIDefCfaOffset(Offset);
3028 return false;
3029}
3030
Jim Grosbach4b905842013-09-20 23:08:21 +00003031/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003032/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003033bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003034 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003035 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003036 return true;
3037
3038 if (getLexer().isNot(AsmToken::Comma))
3039 return TokError("unexpected token in directive");
3040 Lex();
3041
3042 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003043 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003044 return true;
3045
3046 getStreamer().EmitCFIRegister(Register1, Register2);
3047 return false;
3048}
3049
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003050/// parseDirectiveCFIWindowSave
3051/// ::= .cfi_window_save
3052bool AsmParser::parseDirectiveCFIWindowSave() {
3053 getStreamer().EmitCFIWindowSave();
3054 return false;
3055}
3056
Jim Grosbach4b905842013-09-20 23:08:21 +00003057/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003058/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003059bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003060 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003061 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003062 return true;
3063
3064 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3065 return false;
3066}
3067
Jim Grosbach4b905842013-09-20 23:08:21 +00003068/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003069/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003070bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003071 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003072 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return true;
3074
3075 getStreamer().EmitCFIDefCfaRegister(Register);
3076 return false;
3077}
3078
Jim Grosbach4b905842013-09-20 23:08:21 +00003079/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003080/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003081bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003082 int64_t Register = 0;
3083 int64_t Offset = 0;
3084
Jim Grosbach4b905842013-09-20 23:08:21 +00003085 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003086 return true;
3087
3088 if (getLexer().isNot(AsmToken::Comma))
3089 return TokError("unexpected token in directive");
3090 Lex();
3091
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003092 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003093 return true;
3094
3095 getStreamer().EmitCFIOffset(Register, Offset);
3096 return false;
3097}
3098
Jim Grosbach4b905842013-09-20 23:08:21 +00003099/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003100/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003101bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003102 int64_t Register = 0;
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003105 return true;
3106
3107 if (getLexer().isNot(AsmToken::Comma))
3108 return TokError("unexpected token in directive");
3109 Lex();
3110
3111 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003112 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003113 return true;
3114
3115 getStreamer().EmitCFIRelOffset(Register, Offset);
3116 return false;
3117}
3118
3119static bool isValidEncoding(int64_t Encoding) {
3120 if (Encoding & ~0xff)
3121 return false;
3122
3123 if (Encoding == dwarf::DW_EH_PE_omit)
3124 return true;
3125
3126 const unsigned Format = Encoding & 0xf;
3127 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3128 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3129 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3130 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3131 return false;
3132
3133 const unsigned Application = Encoding & 0x70;
3134 if (Application != dwarf::DW_EH_PE_absptr &&
3135 Application != dwarf::DW_EH_PE_pcrel)
3136 return false;
3137
3138 return true;
3139}
3140
Jim Grosbach4b905842013-09-20 23:08:21 +00003141/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003142/// IsPersonality true for cfi_personality, false for cfi_lsda
3143/// ::= .cfi_personality encoding, [symbol_name]
3144/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003145bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003146 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003147 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003148 return true;
3149 if (Encoding == dwarf::DW_EH_PE_omit)
3150 return false;
3151
3152 if (!isValidEncoding(Encoding))
3153 return TokError("unsupported encoding.");
3154
3155 if (getLexer().isNot(AsmToken::Comma))
3156 return TokError("unexpected token in directive");
3157 Lex();
3158
3159 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003160 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003161 return TokError("expected identifier in directive");
3162
3163 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3164
3165 if (IsPersonality)
3166 getStreamer().EmitCFIPersonality(Sym, Encoding);
3167 else
3168 getStreamer().EmitCFILsda(Sym, Encoding);
3169 return false;
3170}
3171
Jim Grosbach4b905842013-09-20 23:08:21 +00003172/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003173/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003174bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003175 getStreamer().EmitCFIRememberState();
3176 return false;
3177}
3178
Jim Grosbach4b905842013-09-20 23:08:21 +00003179/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003180/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003181bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003182 getStreamer().EmitCFIRestoreState();
3183 return false;
3184}
3185
Jim Grosbach4b905842013-09-20 23:08:21 +00003186/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003187/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003188bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003189 int64_t Register = 0;
3190
Jim Grosbach4b905842013-09-20 23:08:21 +00003191 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003192 return true;
3193
3194 getStreamer().EmitCFISameValue(Register);
3195 return false;
3196}
3197
Jim Grosbach4b905842013-09-20 23:08:21 +00003198/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003199/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003200bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003201 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003202 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003203 return true;
3204
3205 getStreamer().EmitCFIRestore(Register);
3206 return false;
3207}
3208
Jim Grosbach4b905842013-09-20 23:08:21 +00003209/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003210/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003211bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003212 std::string Values;
3213 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003214 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003215 return true;
3216
3217 Values.push_back((uint8_t)CurrValue);
3218
3219 while (getLexer().is(AsmToken::Comma)) {
3220 Lex();
3221
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003222 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003223 return true;
3224
3225 Values.push_back((uint8_t)CurrValue);
3226 }
3227
3228 getStreamer().EmitCFIEscape(Values);
3229 return false;
3230}
3231
Jim Grosbach4b905842013-09-20 23:08:21 +00003232/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003233/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003234bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003235 if (getLexer().isNot(AsmToken::EndOfStatement))
3236 return Error(getLexer().getLoc(),
3237 "unexpected token in '.cfi_signal_frame'");
3238
3239 getStreamer().EmitCFISignalFrame();
3240 return false;
3241}
3242
Jim Grosbach4b905842013-09-20 23:08:21 +00003243/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003244/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003245bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003246 int64_t Register = 0;
3247
Jim Grosbach4b905842013-09-20 23:08:21 +00003248 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003249 return true;
3250
3251 getStreamer().EmitCFIUndefined(Register);
3252 return false;
3253}
3254
Jim Grosbach4b905842013-09-20 23:08:21 +00003255/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003256/// ::= .macros_on
3257/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003258bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003259 if (getLexer().isNot(AsmToken::EndOfStatement))
3260 return Error(getLexer().getLoc(),
3261 "unexpected token in '" + Directive + "' directive");
3262
Jim Grosbach4b905842013-09-20 23:08:21 +00003263 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003264 return false;
3265}
3266
Jim Grosbach4b905842013-09-20 23:08:21 +00003267/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003268/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003269bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003270 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003271 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003272 return TokError("expected identifier in '.macro' directive");
3273
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003274 if (getLexer().is(AsmToken::Comma))
3275 Lex();
3276
Eli Bendersky17233942013-01-15 22:59:42 +00003277 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003278 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003279
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003280 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003281 return Error(Lexer.getLoc(),
3282 "Vararg parameter '" + Parameters.back().Name +
3283 "' should be last one in the list of parameters.");
3284
David Majnemer91fc4c22014-01-29 18:57:46 +00003285 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003286 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003287 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003288
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003289 if (Lexer.is(AsmToken::Colon)) {
3290 Lex(); // consume ':'
3291
3292 SMLoc QualLoc;
3293 StringRef Qualifier;
3294
3295 QualLoc = Lexer.getLoc();
3296 if (parseIdentifier(Qualifier))
3297 return Error(QualLoc, "missing parameter qualifier for "
3298 "'" + Parameter.Name + "' in macro '" + Name + "'");
3299
3300 if (Qualifier == "req")
3301 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003302 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003303 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003304 else
3305 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3306 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3307 }
3308
David Majnemer91fc4c22014-01-29 18:57:46 +00003309 if (getLexer().is(AsmToken::Equal)) {
3310 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003311
3312 SMLoc ParamLoc;
3313
3314 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003315 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003316 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003317
3318 if (Parameter.Required)
3319 Warning(ParamLoc, "pointless default value for required parameter "
3320 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003321 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003322
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003323 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003324
3325 if (getLexer().is(AsmToken::Comma))
3326 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003327 }
3328
3329 // Eat the end of statement.
3330 Lex();
3331
3332 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003333 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003334
3335 // Lex the macro definition.
3336 for (;;) {
3337 // Check whether we have reached the end of the file.
3338 if (getLexer().is(AsmToken::Eof))
3339 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3340
3341 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003342 if (getLexer().is(AsmToken::Identifier)) {
3343 if (getTok().getIdentifier() == ".endm" ||
3344 getTok().getIdentifier() == ".endmacro") {
3345 if (MacroDepth == 0) { // Outermost macro.
3346 EndToken = getTok();
3347 Lex();
3348 if (getLexer().isNot(AsmToken::EndOfStatement))
3349 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3350 "' directive");
3351 break;
3352 } else {
3353 // Otherwise we just found the end of an inner macro.
3354 --MacroDepth;
3355 }
3356 } else if (getTok().getIdentifier() == ".macro") {
3357 // We allow nested macros. Those aren't instantiated until the outermost
3358 // macro is expanded so just ignore them for now.
3359 ++MacroDepth;
3360 }
Eli Bendersky17233942013-01-15 22:59:42 +00003361 }
3362
3363 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003364 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003365 }
3366
Jim Grosbach4b905842013-09-20 23:08:21 +00003367 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003368 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3369 }
3370
3371 const char *BodyStart = StartToken.getLoc().getPointer();
3372 const char *BodyEnd = EndToken.getLoc().getPointer();
3373 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003374 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003375 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003376 return false;
3377}
3378
Jim Grosbach4b905842013-09-20 23:08:21 +00003379/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003380///
3381/// With the support added for named parameters there may be code out there that
3382/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003383/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003385/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003386/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3387/// warning that the positional parameter found in body which have no effect.
3388/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003389/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390/// intended or change the macro to use the named parameters. It is possible
3391/// this warning will trigger when the none of the named parameters are used
3392/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003393void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003395 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003396 // If this macro is not defined with named parameters the warning we are
3397 // checking for here doesn't apply.
3398 unsigned NParameters = Parameters.size();
3399 if (NParameters == 0)
3400 return;
3401
3402 bool NamedParametersFound = false;
3403 bool PositionalParametersFound = false;
3404
3405 // Look at the body of the macro for use of both the named parameters and what
3406 // are likely to be positional parameters. This is what expandMacro() is
3407 // doing when it finds the parameters in the body.
3408 while (!Body.empty()) {
3409 // Scan for the next possible parameter.
3410 std::size_t End = Body.size(), Pos = 0;
3411 for (; Pos != End; ++Pos) {
3412 // Check for a substitution or escape.
3413 // This macro is defined with parameters, look for \foo, \bar, etc.
3414 if (Body[Pos] == '\\' && Pos + 1 != End)
3415 break;
3416
3417 // This macro should have parameters, but look for $0, $1, ..., $n too.
3418 if (Body[Pos] != '$' || Pos + 1 == End)
3419 continue;
3420 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003421 if (Next == '$' || Next == 'n' ||
3422 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003423 break;
3424 }
3425
3426 // Check if we reached the end.
3427 if (Pos == End)
3428 break;
3429
3430 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003431 switch (Body[Pos + 1]) {
3432 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003433 case '$':
3434 break;
3435
Jim Grosbach4b905842013-09-20 23:08:21 +00003436 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003437 case 'n':
3438 PositionalParametersFound = true;
3439 break;
3440
Jim Grosbach4b905842013-09-20 23:08:21 +00003441 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003442 default: {
3443 PositionalParametersFound = true;
3444 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003445 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003446 }
3447 Pos += 2;
3448 } else {
3449 unsigned I = Pos + 1;
3450 while (isIdentifierChar(Body[I]) && I + 1 != End)
3451 ++I;
3452
Jim Grosbach4b905842013-09-20 23:08:21 +00003453 const char *Begin = Body.data() + Pos + 1;
3454 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003455 unsigned Index = 0;
3456 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003457 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003458 break;
3459
3460 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003461 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3462 Pos += 3;
3463 else {
3464 Pos = I;
3465 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003466 } else {
3467 NamedParametersFound = true;
3468 Pos += 1 + Argument.size();
3469 }
3470 }
3471 // Update the scan point.
3472 Body = Body.substr(Pos);
3473 }
3474
3475 if (!NamedParametersFound && PositionalParametersFound)
3476 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3477 "used in macro body, possible positional parameter "
3478 "found in body which will have no effect");
3479}
3480
Nico Weber155dccd12014-07-24 17:08:39 +00003481/// parseDirectiveExitMacro
3482/// ::= .exitm
3483bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3484 if (getLexer().isNot(AsmToken::EndOfStatement))
3485 return TokError("unexpected token in '" + Directive + "' directive");
3486
3487 if (!isInsideMacroInstantiation())
3488 return TokError("unexpected '" + Directive + "' in file, "
3489 "no current macro definition");
3490
3491 // Exit all conditionals that are active in the current macro.
3492 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3493 TheCondState = TheCondStack.back();
3494 TheCondStack.pop_back();
3495 }
3496
3497 handleMacroExit();
3498 return false;
3499}
3500
Jim Grosbach4b905842013-09-20 23:08:21 +00003501/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003502/// ::= .endm
3503/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003504bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003505 if (getLexer().isNot(AsmToken::EndOfStatement))
3506 return TokError("unexpected token in '" + Directive + "' directive");
3507
3508 // If we are inside a macro instantiation, terminate the current
3509 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003510 if (isInsideMacroInstantiation()) {
3511 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003512 return false;
3513 }
3514
3515 // Otherwise, this .endmacro is a stray entry in the file; well formed
3516 // .endmacro directives are handled during the macro definition parsing.
3517 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003518 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003519}
3520
Jim Grosbach4b905842013-09-20 23:08:21 +00003521/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003522/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003523bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003524 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003525 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003526 return TokError("expected identifier in '.purgem' directive");
3527
3528 if (getLexer().isNot(AsmToken::EndOfStatement))
3529 return TokError("unexpected token in '.purgem' directive");
3530
Jim Grosbach4b905842013-09-20 23:08:21 +00003531 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003532 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return false;
3536}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003537
Jim Grosbach4b905842013-09-20 23:08:21 +00003538/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003539/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003540bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003541 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003542
3543 // Expect a single argument: an expression that evaluates to a constant
3544 // in the inclusive range 0-30.
3545 SMLoc ExprLoc = getLexer().getLoc();
3546 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003547 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003548 return true;
3549 else if (getLexer().isNot(AsmToken::EndOfStatement))
3550 return TokError("unexpected token after expression in"
3551 " '.bundle_align_mode' directive");
3552 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3553 return Error(ExprLoc,
3554 "invalid bundle alignment size (expected between 0 and 30)");
3555
3556 Lex();
3557
3558 // Because of AlignSizePow2's verified range we can safely truncate it to
3559 // unsigned.
3560 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3561 return false;
3562}
3563
Jim Grosbach4b905842013-09-20 23:08:21 +00003564/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003565/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003566bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003567 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003568 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003569
Eli Bendersky802b6282013-01-07 21:51:08 +00003570 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3571 StringRef Option;
3572 SMLoc Loc = getTok().getLoc();
3573 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003574 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003575
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003576 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003577 return Error(Loc, kInvalidOptionError);
3578
3579 if (Option != "align_to_end")
3580 return Error(Loc, kInvalidOptionError);
3581 else if (getLexer().isNot(AsmToken::EndOfStatement))
3582 return Error(Loc,
3583 "unexpected token after '.bundle_lock' directive option");
3584 AlignToEnd = true;
3585 }
3586
Eli Benderskyf483ff92012-12-20 19:05:53 +00003587 Lex();
3588
Eli Bendersky802b6282013-01-07 21:51:08 +00003589 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003590 return false;
3591}
3592
Jim Grosbach4b905842013-09-20 23:08:21 +00003593/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003594/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003595bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003596 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003597
3598 if (getLexer().isNot(AsmToken::EndOfStatement))
3599 return TokError("unexpected token in '.bundle_unlock' directive");
3600 Lex();
3601
3602 getStreamer().EmitBundleUnlock();
3603 return false;
3604}
3605
Jim Grosbach4b905842013-09-20 23:08:21 +00003606/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003607/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003608bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003609 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003610
3611 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003612 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003613 return true;
3614
3615 int64_t FillExpr = 0;
3616 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3617 if (getLexer().isNot(AsmToken::Comma))
3618 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3619 Lex();
3620
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003621 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003622 return true;
3623
3624 if (getLexer().isNot(AsmToken::EndOfStatement))
3625 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3626 }
3627
3628 Lex();
3629
3630 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003631 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3632 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003633
3634 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003635 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003636
3637 return false;
3638}
3639
Jim Grosbach4b905842013-09-20 23:08:21 +00003640/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003641/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003642bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003643 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003644 const MCExpr *Value;
3645
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003646 for (;;) {
3647 if (parseExpression(Value))
3648 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003649
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003650 if (Signed)
3651 getStreamer().EmitSLEB128Value(Value);
3652 else
3653 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003654
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003655 if (getLexer().is(AsmToken::EndOfStatement))
3656 break;
3657
3658 if (getLexer().isNot(AsmToken::Comma))
3659 return TokError("unexpected token in directive");
3660 Lex();
3661 }
Eli Bendersky17233942013-01-15 22:59:42 +00003662
3663 return false;
3664}
3665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003667/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003668bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003669 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003670 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003671 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003672 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003673
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003675 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003676
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003677 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003678
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003679 // Assembler local symbols don't make any sense here. Complain loudly.
3680 if (Sym->isTemporary())
3681 return Error(Loc, "non-local symbol required in directive");
3682
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003683 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3684 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003685
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003686 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003687 break;
3688
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003689 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003690 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003691 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003692 }
3693 }
3694
Sean Callanan686ed8d2010-01-19 20:22:31 +00003695 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003696 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003697}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003698
Jim Grosbach4b905842013-09-20 23:08:21 +00003699/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003700/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003701bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003702 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003703
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003704 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003705 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003706 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003707 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003708
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003709 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003710 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003711
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003712 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003713 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003714 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715
3716 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003717 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003718 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003719 return true;
3720
3721 int64_t Pow2Alignment = 0;
3722 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003723 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003724 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003725 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003726 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003727 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003728
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003729 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3730 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003731 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3732
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003733 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003734 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3735 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003736 if (!isPowerOf2_64(Pow2Alignment))
3737 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3738 Pow2Alignment = Log2_64(Pow2Alignment);
3739 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003740 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003741
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003742 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003743 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003744
Sean Callanan686ed8d2010-01-19 20:22:31 +00003745 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003746
Chris Lattner28ad7542009-07-09 17:25:12 +00003747 // NOTE: a size of zero for a .comm should create a undefined symbol
3748 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003749 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003750 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003751 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003752
Eric Christopherbc818852010-05-14 01:38:54 +00003753 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003754 // may internally end up wanting an alignment in bytes.
3755 // FIXME: Diagnose overflow.
3756 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003757 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003758 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003759
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003760 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003761 return Error(IDLoc, "invalid symbol redefinition");
3762
Chris Lattner28ad7542009-07-09 17:25:12 +00003763 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003764 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003765 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003766 return false;
3767 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003768
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003769 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003770 return false;
3771}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003772
Jim Grosbach4b905842013-09-20 23:08:21 +00003773/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003774/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003775bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003776 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003777 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003778
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003779 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003780 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003781 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003782
Sean Callanan686ed8d2010-01-19 20:22:31 +00003783 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003784
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003785 if (Str.empty())
3786 Error(Loc, ".abort detected. Assembly stopping.");
3787 else
3788 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003789 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003790
3791 return false;
3792}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003793
Jim Grosbach4b905842013-09-20 23:08:21 +00003794/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003795/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003796bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003797 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003798 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003799
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003800 // Allow the strings to have escaped octal character sequence.
3801 std::string Filename;
3802 if (parseEscapedString(Filename))
3803 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003804 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003805 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003806
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003807 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003808 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003809
Chris Lattner693fbb82009-07-16 06:14:39 +00003810 // Attempt to switch the lexer to the included file before consuming the end
3811 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003812 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003813 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003814 return true;
3815 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003816
3817 return false;
3818}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003819
Jim Grosbach4b905842013-09-20 23:08:21 +00003820/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003821/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003822bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003823 if (getLexer().isNot(AsmToken::String))
3824 return TokError("expected string in '.incbin' directive");
3825
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003826 // Allow the strings to have escaped octal character sequence.
3827 std::string Filename;
3828 if (parseEscapedString(Filename))
3829 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003830 SMLoc IncbinLoc = getLexer().getLoc();
3831 Lex();
3832
3833 if (getLexer().isNot(AsmToken::EndOfStatement))
3834 return TokError("unexpected token in '.incbin' directive");
3835
Kevin Enderby109f25c2011-12-14 21:47:48 +00003836 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003837 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003838 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3839 return true;
3840 }
3841
3842 return false;
3843}
3844
Jim Grosbach4b905842013-09-20 23:08:21 +00003845/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003846/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3847bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003848 TheCondStack.push_back(TheCondState);
3849 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003850 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003851 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003852 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003853 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003854 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003855 return true;
3856
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003857 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003858 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003859
Sean Callanan686ed8d2010-01-19 20:22:31 +00003860 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003861
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003862 switch (DirKind) {
3863 default:
3864 llvm_unreachable("unsupported directive");
3865 case DK_IF:
3866 case DK_IFNE:
3867 break;
3868 case DK_IFEQ:
3869 ExprValue = ExprValue == 0;
3870 break;
3871 case DK_IFGE:
3872 ExprValue = ExprValue >= 0;
3873 break;
3874 case DK_IFGT:
3875 ExprValue = ExprValue > 0;
3876 break;
3877 case DK_IFLE:
3878 ExprValue = ExprValue <= 0;
3879 break;
3880 case DK_IFLT:
3881 ExprValue = ExprValue < 0;
3882 break;
3883 }
3884
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003885 TheCondState.CondMet = ExprValue;
3886 TheCondState.Ignore = !TheCondState.CondMet;
3887 }
3888
3889 return false;
3890}
3891
Jim Grosbach4b905842013-09-20 23:08:21 +00003892/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003893/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003894bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003895 TheCondStack.push_back(TheCondState);
3896 TheCondState.TheCond = AsmCond::IfCond;
3897
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003898 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003899 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003900 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003901 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003902
3903 if (getLexer().isNot(AsmToken::EndOfStatement))
3904 return TokError("unexpected token in '.ifb' directive");
3905
3906 Lex();
3907
3908 TheCondState.CondMet = ExpectBlank == Str.empty();
3909 TheCondState.Ignore = !TheCondState.CondMet;
3910 }
3911
3912 return false;
3913}
3914
Jim Grosbach4b905842013-09-20 23:08:21 +00003915/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003916/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003917/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003918bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003919 TheCondStack.push_back(TheCondState);
3920 TheCondState.TheCond = AsmCond::IfCond;
3921
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003922 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003923 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003924 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003925 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003926
3927 if (getLexer().isNot(AsmToken::Comma))
3928 return TokError("unexpected token in '.ifc' directive");
3929
3930 Lex();
3931
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003932 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003933
3934 if (getLexer().isNot(AsmToken::EndOfStatement))
3935 return TokError("unexpected token in '.ifc' directive");
3936
3937 Lex();
3938
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003939 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003940 TheCondState.Ignore = !TheCondState.CondMet;
3941 }
3942
3943 return false;
3944}
3945
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003946/// parseDirectiveIfeqs
3947/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003948bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003949 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003950 if (ExpectEqual)
3951 TokError("expected string parameter for '.ifeqs' directive");
3952 else
3953 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003954 eatToEndOfStatement();
3955 return true;
3956 }
3957
3958 StringRef String1 = getTok().getStringContents();
3959 Lex();
3960
3961 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003962 if (ExpectEqual)
3963 TokError("expected comma after first string for '.ifeqs' directive");
3964 else
3965 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003966 eatToEndOfStatement();
3967 return true;
3968 }
3969
3970 Lex();
3971
3972 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 String2 = getTok().getStringContents();
3982 Lex();
3983
3984 TheCondStack.push_back(TheCondState);
3985 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00003986 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003987 TheCondState.Ignore = !TheCondState.CondMet;
3988
3989 return false;
3990}
3991
Jim Grosbach4b905842013-09-20 23:08:21 +00003992/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003993/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003994bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003995 StringRef Name;
3996 TheCondStack.push_back(TheCondState);
3997 TheCondState.TheCond = AsmCond::IfCond;
3998
3999 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004000 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004001 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004002 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004003 return TokError("expected identifier after '.ifdef'");
4004
4005 Lex();
4006
4007 MCSymbol *Sym = getContext().LookupSymbol(Name);
4008
4009 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004010 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004011 else
Craig Topper353eda42014-04-24 06:44:33 +00004012 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004013 TheCondState.Ignore = !TheCondState.CondMet;
4014 }
4015
4016 return false;
4017}
4018
Jim Grosbach4b905842013-09-20 23:08:21 +00004019/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004021bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022 if (TheCondState.TheCond != AsmCond::IfCond &&
4023 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004024 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4025 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004026 TheCondState.TheCond = AsmCond::ElseIfCond;
4027
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004028 bool LastIgnoreState = false;
4029 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004030 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004031 if (LastIgnoreState || TheCondState.CondMet) {
4032 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004033 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004034 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004035 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004036 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004037 return true;
4038
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004039 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004040 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004041
Sean Callanan686ed8d2010-01-19 20:22:31 +00004042 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004043 TheCondState.CondMet = ExprValue;
4044 TheCondState.Ignore = !TheCondState.CondMet;
4045 }
4046
4047 return false;
4048}
4049
Jim Grosbach4b905842013-09-20 23:08:21 +00004050/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004051/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004052bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004053 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004054 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004055
Sean Callanan686ed8d2010-01-19 20:22:31 +00004056 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004057
4058 if (TheCondState.TheCond != AsmCond::IfCond &&
4059 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004060 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4061 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004062 TheCondState.TheCond = AsmCond::ElseCond;
4063 bool LastIgnoreState = false;
4064 if (!TheCondStack.empty())
4065 LastIgnoreState = TheCondStack.back().Ignore;
4066 if (LastIgnoreState || TheCondState.CondMet)
4067 TheCondState.Ignore = true;
4068 else
4069 TheCondState.Ignore = false;
4070
4071 return false;
4072}
4073
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004074/// parseDirectiveEnd
4075/// ::= .end
4076bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4077 if (getLexer().isNot(AsmToken::EndOfStatement))
4078 return TokError("unexpected token in '.end' directive");
4079
4080 Lex();
4081
4082 while (Lexer.isNot(AsmToken::Eof))
4083 Lex();
4084
4085 return false;
4086}
4087
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004088/// parseDirectiveError
4089/// ::= .err
4090/// ::= .error [string]
4091bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4092 if (!TheCondStack.empty()) {
4093 if (TheCondStack.back().Ignore) {
4094 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004095 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004096 }
4097 }
4098
4099 if (!WithMessage)
4100 return Error(L, ".err encountered");
4101
4102 StringRef Message = ".error directive invoked in source file";
4103 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4104 if (Lexer.isNot(AsmToken::String)) {
4105 TokError(".error argument must be a string");
4106 eatToEndOfStatement();
4107 return true;
4108 }
4109
4110 Message = getTok().getStringContents();
4111 Lex();
4112 }
4113
4114 Error(L, Message);
4115 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004116}
4117
Nico Weber404012b2014-07-24 16:26:06 +00004118/// parseDirectiveWarning
4119/// ::= .warning [string]
4120bool AsmParser::parseDirectiveWarning(SMLoc L) {
4121 if (!TheCondStack.empty()) {
4122 if (TheCondStack.back().Ignore) {
4123 eatToEndOfStatement();
4124 return false;
4125 }
4126 }
4127
4128 StringRef Message = ".warning directive invoked in source file";
4129 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4130 if (Lexer.isNot(AsmToken::String)) {
4131 TokError(".warning argument must be a string");
4132 eatToEndOfStatement();
4133 return true;
4134 }
4135
4136 Message = getTok().getStringContents();
4137 Lex();
4138 }
4139
4140 Warning(L, Message);
4141 return false;
4142}
4143
Jim Grosbach4b905842013-09-20 23:08:21 +00004144/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004145/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004146bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004147 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004148 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004149
Sean Callanan686ed8d2010-01-19 20:22:31 +00004150 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004151
Jim Grosbach4b905842013-09-20 23:08:21 +00004152 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004153 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4154 ".else");
4155 if (!TheCondStack.empty()) {
4156 TheCondState = TheCondStack.back();
4157 TheCondStack.pop_back();
4158 }
4159
4160 return false;
4161}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004162
Eli Bendersky17233942013-01-15 22:59:42 +00004163void AsmParser::initializeDirectiveKindMap() {
4164 DirectiveKindMap[".set"] = DK_SET;
4165 DirectiveKindMap[".equ"] = DK_EQU;
4166 DirectiveKindMap[".equiv"] = DK_EQUIV;
4167 DirectiveKindMap[".ascii"] = DK_ASCII;
4168 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4169 DirectiveKindMap[".string"] = DK_STRING;
4170 DirectiveKindMap[".byte"] = DK_BYTE;
4171 DirectiveKindMap[".short"] = DK_SHORT;
4172 DirectiveKindMap[".value"] = DK_VALUE;
4173 DirectiveKindMap[".2byte"] = DK_2BYTE;
4174 DirectiveKindMap[".long"] = DK_LONG;
4175 DirectiveKindMap[".int"] = DK_INT;
4176 DirectiveKindMap[".4byte"] = DK_4BYTE;
4177 DirectiveKindMap[".quad"] = DK_QUAD;
4178 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004179 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004180 DirectiveKindMap[".single"] = DK_SINGLE;
4181 DirectiveKindMap[".float"] = DK_FLOAT;
4182 DirectiveKindMap[".double"] = DK_DOUBLE;
4183 DirectiveKindMap[".align"] = DK_ALIGN;
4184 DirectiveKindMap[".align32"] = DK_ALIGN32;
4185 DirectiveKindMap[".balign"] = DK_BALIGN;
4186 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4187 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4188 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4189 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4190 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4191 DirectiveKindMap[".org"] = DK_ORG;
4192 DirectiveKindMap[".fill"] = DK_FILL;
4193 DirectiveKindMap[".zero"] = DK_ZERO;
4194 DirectiveKindMap[".extern"] = DK_EXTERN;
4195 DirectiveKindMap[".globl"] = DK_GLOBL;
4196 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004197 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4198 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4199 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4200 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4201 DirectiveKindMap[".reference"] = DK_REFERENCE;
4202 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4203 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4204 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4205 DirectiveKindMap[".comm"] = DK_COMM;
4206 DirectiveKindMap[".common"] = DK_COMMON;
4207 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4208 DirectiveKindMap[".abort"] = DK_ABORT;
4209 DirectiveKindMap[".include"] = DK_INCLUDE;
4210 DirectiveKindMap[".incbin"] = DK_INCBIN;
4211 DirectiveKindMap[".code16"] = DK_CODE16;
4212 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4213 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004214 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004215 DirectiveKindMap[".irp"] = DK_IRP;
4216 DirectiveKindMap[".irpc"] = DK_IRPC;
4217 DirectiveKindMap[".endr"] = DK_ENDR;
4218 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4219 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4220 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4221 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004222 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4223 DirectiveKindMap[".ifge"] = DK_IFGE;
4224 DirectiveKindMap[".ifgt"] = DK_IFGT;
4225 DirectiveKindMap[".ifle"] = DK_IFLE;
4226 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004227 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004228 DirectiveKindMap[".ifb"] = DK_IFB;
4229 DirectiveKindMap[".ifnb"] = DK_IFNB;
4230 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004231 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004232 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004233 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004234 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4235 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4236 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4237 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4238 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004239 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004240 DirectiveKindMap[".endif"] = DK_ENDIF;
4241 DirectiveKindMap[".skip"] = DK_SKIP;
4242 DirectiveKindMap[".space"] = DK_SPACE;
4243 DirectiveKindMap[".file"] = DK_FILE;
4244 DirectiveKindMap[".line"] = DK_LINE;
4245 DirectiveKindMap[".loc"] = DK_LOC;
4246 DirectiveKindMap[".stabs"] = DK_STABS;
4247 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4248 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4249 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4250 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4251 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4252 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4253 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4254 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4255 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4256 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4257 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4258 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4259 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4260 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4261 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4262 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4263 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4264 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4265 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4266 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4267 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004268 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004269 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4270 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4271 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004272 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004273 DirectiveKindMap[".endm"] = DK_ENDM;
4274 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4275 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004276 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004277 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004278 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004279}
4280
Jim Grosbach4b905842013-09-20 23:08:21 +00004281MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004282 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004283
Rafael Espindola34b9c512012-06-03 23:57:14 +00004284 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004285 for (;;) {
4286 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004287 if (getLexer().is(AsmToken::Eof)) {
4288 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004289 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004290 }
4291
Rafael Espindola34b9c512012-06-03 23:57:14 +00004292 if (Lexer.is(AsmToken::Identifier) &&
4293 (getTok().getIdentifier() == ".rept")) {
4294 ++NestLevel;
4295 }
4296
4297 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004298 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004299 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004300 EndToken = getTok();
4301 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004302 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4303 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004304 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004305 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004306 break;
4307 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004308 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309 }
4310
Rafael Espindola34b9c512012-06-03 23:57:14 +00004311 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004312 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004313 }
4314
4315 const char *BodyStart = StartToken.getLoc().getPointer();
4316 const char *BodyEnd = EndToken.getLoc().getPointer();
4317 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4318
Rafael Espindola34b9c512012-06-03 23:57:14 +00004319 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004320 MacroLikeBodies.push_back(
4321 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004322 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323}
4324
Jim Grosbach4b905842013-09-20 23:08:21 +00004325void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004326 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004327 OS << ".endr\n";
4328
Rafael Espindola3560ff22014-08-27 20:03:13 +00004329 std::unique_ptr<MemoryBuffer> Instantiation =
4330 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004331
Rafael Espindola34b9c512012-06-03 23:57:14 +00004332 // Create the macro instantiation object and add to the current macro
4333 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004334 MacroInstantiation *MI = new MacroInstantiation(
4335 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004336 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004337
Rafael Espindola34b9c512012-06-03 23:57:14 +00004338 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004339 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004340 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004341 Lex();
4342}
4343
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004344/// parseDirectiveRept
4345/// ::= .rep | .rept count
4346bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004347 const MCExpr *CountExpr;
4348 SMLoc CountLoc = getTok().getLoc();
4349 if (parseExpression(CountExpr))
4350 return true;
4351
Rafael Espindola34b9c512012-06-03 23:57:14 +00004352 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004353 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4354 eatToEndOfStatement();
4355 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4356 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004357
4358 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004359 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004360
4361 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004362 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004363
4364 // Eat the end of statement.
4365 Lex();
4366
4367 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004368 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004369 if (!M)
4370 return true;
4371
4372 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4373 // to hold the macro body with substitutions.
4374 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004375 raw_svector_ostream OS(Buf);
4376 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004377 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004378 return true;
4379 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004380 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004381
4382 return false;
4383}
4384
Jim Grosbach4b905842013-09-20 23:08:21 +00004385/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004386/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004387bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004388 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004389
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004390 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004391 return TokError("expected identifier in '.irp' directive");
4392
Rafael Espindola768b41c2012-06-15 14:02:34 +00004393 if (Lexer.isNot(AsmToken::Comma))
4394 return TokError("expected comma in '.irp' directive");
4395
4396 Lex();
4397
Eli Bendersky38274122013-01-14 23:22:36 +00004398 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004399 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004400 return true;
4401
4402 // Eat the end of statement.
4403 Lex();
4404
4405 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004406 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004407 if (!M)
4408 return true;
4409
4410 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4411 // to hold the macro body with substitutions.
4412 SmallString<256> Buf;
4413 raw_svector_ostream OS(Buf);
4414
Eli Bendersky38274122013-01-14 23:22:36 +00004415 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004416 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004417 return true;
4418 }
4419
Jim Grosbach4b905842013-09-20 23:08:21 +00004420 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004421
4422 return false;
4423}
4424
Jim Grosbach4b905842013-09-20 23:08:21 +00004425/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004426/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004427bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004428 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004429
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004430 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004431 return TokError("expected identifier in '.irpc' directive");
4432
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004433 if (Lexer.isNot(AsmToken::Comma))
4434 return TokError("expected comma in '.irpc' directive");
4435
4436 Lex();
4437
Eli Bendersky38274122013-01-14 23:22:36 +00004438 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004439 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004440 return true;
4441
4442 if (A.size() != 1 || A.front().size() != 1)
4443 return TokError("unexpected token in '.irpc' directive");
4444
4445 // Eat the end of statement.
4446 Lex();
4447
4448 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004449 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004450 if (!M)
4451 return true;
4452
4453 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4454 // to hold the macro body with substitutions.
4455 SmallString<256> Buf;
4456 raw_svector_ostream OS(Buf);
4457
4458 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004459 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004460 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004461 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004462
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004463 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004464 return true;
4465 }
4466
Jim Grosbach4b905842013-09-20 23:08:21 +00004467 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004468
4469 return false;
4470}
4471
Jim Grosbach4b905842013-09-20 23:08:21 +00004472bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004473 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004474 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004475
4476 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004477 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004478 assert(getLexer().is(AsmToken::EndOfStatement));
4479
Jim Grosbach4b905842013-09-20 23:08:21 +00004480 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004481 return false;
4482}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004483
Jim Grosbach4b905842013-09-20 23:08:21 +00004484bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004485 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004486 const MCExpr *Value;
4487 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004488 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004489 return true;
4490 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4491 if (!MCE)
4492 return Error(ExprLoc, "unexpected expression in _emit");
4493 uint64_t IntValue = MCE->getValue();
4494 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4495 return Error(ExprLoc, "literal value out of range for directive");
4496
Chad Rosierc7f552c2013-02-12 21:33:51 +00004497 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4498 return false;
4499}
4500
Jim Grosbach4b905842013-09-20 23:08:21 +00004501bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004502 const MCExpr *Value;
4503 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004504 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004505 return true;
4506 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4507 if (!MCE)
4508 return Error(ExprLoc, "unexpected expression in align");
4509 uint64_t IntValue = MCE->getValue();
4510 if (!isPowerOf2_64(IntValue))
4511 return Error(ExprLoc, "literal value not a power of two greater then zero");
4512
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 Info.AsmRewrites->push_back(
4514 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004515 return false;
4516}
4517
Chad Rosierf43fcf52013-02-13 21:27:17 +00004518// We are comparing pointers, but the pointers are relative to a single string.
4519// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004520static int rewritesSort(const AsmRewrite *AsmRewriteA,
4521 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004522 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4523 return -1;
4524 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4525 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004526
Chad Rosierfce4fab2013-04-08 17:43:47 +00004527 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4528 // rewrite to the same location. Make sure the SizeDirective rewrite is
4529 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4530 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004531 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4532 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004533 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004534
Jim Grosbach4b905842013-09-20 23:08:21 +00004535 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4536 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004537 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004538 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004539}
4540
Jim Grosbach4b905842013-09-20 23:08:21 +00004541bool AsmParser::parseMSInlineAsm(
4542 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4543 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4544 SmallVectorImpl<std::string> &Constraints,
4545 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4546 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004547 SmallVector<void *, 4> InputDecls;
4548 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004549 SmallVector<bool, 4> InputDeclsAddressOf;
4550 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004551 SmallVector<std::string, 4> InputConstraints;
4552 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004553 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004554
Benjamin Kramer1a136112013-02-15 20:37:21 +00004555 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004556
4557 // Prime the lexer.
4558 Lex();
4559
4560 // While we have input, parse each statement.
4561 unsigned InputIdx = 0;
4562 unsigned OutputIdx = 0;
4563 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004564 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004565 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004566 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004567
Chad Rosier149e8e02012-12-12 22:45:52 +00004568 if (Info.ParseError)
4569 return true;
4570
Benjamin Kramer1a136112013-02-15 20:37:21 +00004571 if (Info.Opcode == ~0U)
4572 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004573
Benjamin Kramer1a136112013-02-15 20:37:21 +00004574 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004575
Benjamin Kramer1a136112013-02-15 20:37:21 +00004576 // Build the list of clobbers, outputs and inputs.
4577 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004578 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004579
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004581 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004582 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004583
Benjamin Kramer1a136112013-02-15 20:37:21 +00004584 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004585 if (Operand.isReg() && !Operand.needAddressOf() &&
4586 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 unsigned NumDefs = Desc.getNumDefs();
4588 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004589 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4590 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004591 continue;
4592 }
4593
4594 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004595 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004596 if (SymName.empty())
4597 continue;
4598
David Blaikie960ea3f2014-06-08 16:18:35 +00004599 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004600 if (!OpDecl)
4601 continue;
4602
4603 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004604 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004605 if (isOutput) {
4606 ++InputIdx;
4607 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004608 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004609 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004610 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004611 } else {
4612 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004613 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4614 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004615 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004616 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004617 }
Reid Kleckneree088972013-12-10 18:27:32 +00004618
4619 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004620 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4621 Desc.getNumImplicitDefs());
4622 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004623 }
4624
4625 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004626 NumOutputs = OutputDecls.size();
4627 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004628
4629 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004630 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4631 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4632 ClobberRegs.end());
4633 Clobbers.assign(ClobberRegs.size(), std::string());
4634 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4635 raw_string_ostream OS(Clobbers[I]);
4636 IP->printRegName(OS, ClobberRegs[I]);
4637 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004638
4639 // Merge the various outputs and inputs. Output are expected first.
4640 if (NumOutputs || NumInputs) {
4641 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004642 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004643 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004644 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004645 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004646 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004647 }
4648 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004649 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004650 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004651 }
4652 }
4653
4654 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004655 std::string AsmStringIR;
4656 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004657 StringRef ASMString =
4658 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4659 const char *AsmStart = ASMString.begin();
4660 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004661 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004662 for (const AsmRewrite &AR : AsmStrRewrites) {
4663 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004664 if (Kind == AOK_Delete)
4665 continue;
4666
David Majnemer8114c1a2014-06-23 02:17:16 +00004667 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004668 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004669
Chad Rosier120eefd2013-03-19 17:32:17 +00004670 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004671 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004672 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004673
Chad Rosier37e755c2012-10-23 17:43:43 +00004674 // Skip the original expression.
4675 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004676 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004677 continue;
4678 }
4679
Chad Rosierff10ed12013-04-12 16:26:42 +00004680 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004681 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004682 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004683 default:
4684 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004685 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004686 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004687 break;
4688 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004689 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004690 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004691 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004692 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004693 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004694 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004695 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004696 break;
4697 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004698 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004699 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004700 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004701 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004702 default: break;
4703 case 8: OS << "byte ptr "; break;
4704 case 16: OS << "word ptr "; break;
4705 case 32: OS << "dword ptr "; break;
4706 case 64: OS << "qword ptr "; break;
4707 case 80: OS << "xword ptr "; break;
4708 case 128: OS << "xmmword ptr "; break;
4709 case 256: OS << "ymmword ptr "; break;
4710 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004711 break;
4712 case AOK_Emit:
4713 OS << ".byte";
4714 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004715 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004716 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004717 OS << ".align " << Val;
4718
4719 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004720 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004721 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4722 break;
4723 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004724 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004725 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004726 OS.flush();
4727 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004728 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004729 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004730 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004731 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004732
Chad Rosier8bce6642012-10-18 15:49:34 +00004733 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004734 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004735 }
4736
4737 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004738 if (AsmStart != AsmEnd)
4739 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004740
4741 AsmString = OS.str();
4742 return false;
4743}
4744
Daniel Dunbar01e36072010-07-17 02:26:10 +00004745/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004746MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4747 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004748 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004749}