blob: f1ec64c8a790ed6dc0929336c62795f27debdbb6 [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
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000048static cl::opt<bool>
49FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
51
Eric Christophera7c32732012-12-18 00:30:54 +000052MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000053
Daniel Dunbar86033402010-07-12 17:54:38 +000054namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000058
59struct MCAsmMacroParameter {
60 StringRef Name;
61 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000066};
67
Eli Benderskya313ae62013-01-16 18:56:50 +000068typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
69
70struct MCAsmMacro {
71 StringRef Name;
72 StringRef Body;
73 MCAsmMacroParameters Parameters;
74
75public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000076 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000077 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000078};
79
Daniel Dunbar43235712010-07-18 18:54:11 +000080/// \brief Helper class for storing information about an active macro
81/// instantiation.
82struct MacroInstantiation {
83 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000084 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000085
86 /// The macro instantiation with substitutions.
87 MemoryBuffer *Instantiation;
88
89 /// The location of the instantiation.
90 SMLoc InstantiationLoc;
91
Daniel Dunbar40f1d852012-12-01 01:38:48 +000092 /// The buffer where parsing should resume upon instantiation completion.
93 int ExitBuffer;
94
Daniel Dunbar43235712010-07-18 18:54:11 +000095 /// The location where parsing should resume upon instantiation completion.
96 SMLoc ExitLoc;
97
98public:
Eli Bendersky38274122013-01-14 23:22:36 +000099 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000100 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +0000101};
102
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000105 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 unsigned Opcode;
109
Jim Grosbach4b905842013-09-20 23:08:21 +0000110 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 bool ParseError;
112
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
114
Craig Topper353eda42014-04-24 06:44:33 +0000115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000118};
119
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000133
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000136 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000165 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000171 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
Craig Topper59be68f2014-03-08 07:14:16 +0000188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
Craig Topper59be68f2014-03-08 07:14:16 +0000199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Craig Topper59be68f2014-03-08 07:14:16 +0000222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseIdentifier(StringRef &Res) override;
242 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Craig Topper59be68f2014-03-08 07:14:16 +0000244 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
257 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000313 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000334
Eli Bendersky17233942013-01-15 22:59:42 +0000335 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000336 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000358 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000360 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000361 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000362 };
363
Jim Grosbach4b905842013-09-20 23:08:21 +0000364 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367
368 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000371 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
373 bool parseDirectiveFill(); // ".fill"
374 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000375 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
377 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000380
Eli Bendersky17233942013-01-15 22:59:42 +0000381 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveFile(SMLoc DirectiveLoc);
383 bool parseDirectiveLine();
384 bool parseDirectiveLoc();
385 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000386
387 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000389 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000390 bool parseDirectiveCFISections();
391 bool parseDirectiveCFIStartProc();
392 bool parseDirectiveCFIEndProc();
393 bool parseDirectiveCFIDefCfaOffset();
394 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIAdjustCfaOffset();
396 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
400 bool parseDirectiveCFIRememberState();
401 bool parseDirectiveCFIRestoreState();
402 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIEscape();
405 bool parseDirectiveCFISignalFrame();
406 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000407
408 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveEndMacro(StringRef Directive);
411 bool parseDirectiveMacro(SMLoc DirectiveLoc);
412 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420
Eli Bendersky17233942013-01-15 22:59:42 +0000421 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000423
424 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveAbort(); // ".abort"
434 bool parseDirectiveInclude(); // ".include"
435 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000437 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
438 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000439 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000441 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000443 // ".ifeqs"
444 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000445 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
447 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
448 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
449 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000450 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000454
Rafael Espindola34b9c512012-06-03 23:57:14 +0000455 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
457 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000458 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000459 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
461 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
462 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000463
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 size_t Len);
467
468 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000470
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000471 // "end"
472 bool parseDirectiveEnd(SMLoc DirectiveLoc);
473
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000474 // ".err" or ".error"
475 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000476
Nico Weber404012b2014-07-24 16:26:06 +0000477 // ".warning"
478 bool parseDirectiveWarning(SMLoc DirectiveLoc);
479
Eli Bendersky17233942013-01-15 22:59:42 +0000480 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000481};
Daniel Dunbar86033402010-07-12 17:54:38 +0000482}
483
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484namespace llvm {
485
486extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000487extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000488extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000489
490}
491
Chris Lattnerc35681b2010-01-19 19:46:13 +0000492enum { DEFAULT_ADDRSPACE = 0 };
493
Jim Grosbach4b905842013-09-20 23:08:21 +0000494AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
495 const MCAsmInfo &_MAI)
496 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000497 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
498 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
499 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000500 // Save the old handler.
501 SavedDiagHandler = SrcMgr.getDiagHandler();
502 SavedDiagContext = SrcMgr.getDiagContext();
503 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000504 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000505 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000506
Daniel Dunbarc5011082010-07-12 18:12:02 +0000507 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000508 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
509 case MCObjectFileInfo::IsCOFF:
510 PlatformParser = createCOFFAsmParser();
511 PlatformParser->Initialize(*this);
512 break;
513 case MCObjectFileInfo::IsMachO:
514 PlatformParser = createDarwinAsmParser();
515 PlatformParser->Initialize(*this);
516 IsDarwin = true;
517 break;
518 case MCObjectFileInfo::IsELF:
519 PlatformParser = createELFAsmParser();
520 PlatformParser->Initialize(*this);
521 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000522 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000523
Eli Bendersky17233942013-01-15 22:59:42 +0000524 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000525}
526
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000527AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000528 assert((HadError || ActiveMacros.empty()) &&
529 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000530
531 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000532 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
533 ie = MacroMap.end();
534 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000535 delete it->getValue();
536
Daniel Dunbarc5011082010-07-12 18:12:02 +0000537 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000538}
539
Jim Grosbach4b905842013-09-20 23:08:21 +0000540void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000541 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000542 for (std::vector<MacroInstantiation *>::const_reverse_iterator
543 it = ActiveMacros.rbegin(),
544 ie = ActiveMacros.rend();
545 it != ie; ++it)
546 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000547 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000548}
549
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000550void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
551 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
552 printMacroInstantiations();
553}
554
Chris Lattnera3a06812011-10-16 04:47:35 +0000555bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000556 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000557 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000558 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
559 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000560 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000561}
562
Chris Lattnera3a06812011-10-16 04:47:35 +0000563bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000564 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000565 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
566 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000567 return true;
568}
569
Jim Grosbach4b905842013-09-20 23:08:21 +0000570bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000571 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000572 unsigned NewBuf =
573 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
574 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000576
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000578 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000579 return false;
580}
Daniel Dunbar43235712010-07-18 18:54:11 +0000581
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000582/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000583/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000585bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000586 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000587 unsigned NewBuf =
588 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
589 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000590 return true;
591
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000592 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000593 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000594 return false;
595}
596
Alp Tokera55b95b2014-07-06 10:33:31 +0000597void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
598 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000599 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
600 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000601}
602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603const AsmToken &AsmParser::Lex() {
604 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 if (tok->is(AsmToken::Eof)) {
607 // If this is the end of an included file, pop the parent file off the
608 // include stack.
609 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
610 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000611 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 tok = &Lexer.Lex();
613 }
614 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000617 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000620}
621
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000622bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000623 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000624 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000625 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000626
Chris Lattner36e02122009-06-21 20:54:55 +0000627 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000628 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000629
630 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000631 AsmCond StartingCondState = TheCondState;
632
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 // If we are generating dwarf for assembly source files save the initial text
634 // section and generate a .file directive.
635 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000636 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
637 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000638 auto InsertResult = getContext().addGenDwarfSection(
639 getStreamer().getCurrentSection().first);
640 assert(InsertResult.second && ".text section should not have debug info yet");
641 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000642 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
643 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000644 }
645
Chris Lattner73f36112009-07-02 21:53:43 +0000646 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000647 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000648 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000649 if (!parseStatement(Info))
650 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000651
Daniel Dunbar43325c42010-09-09 22:42:56 +0000652 // We had an error, validate that one was emitted and recover by skipping to
653 // the next line.
654 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000655 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000656 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000657
658 if (TheCondState.TheCond != StartingCondState.TheCond ||
659 TheCondState.Ignore != StartingCondState.Ignore)
660 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661
662 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000663 const auto &LineTables = getContext().getMCDwarfLineTables();
664 if (!LineTables.empty()) {
665 unsigned Index = 0;
666 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
667 if (File.Name.empty() && Index != 0)
668 TokError("unassigned file number: " + Twine(Index) +
669 " for .file directives");
670 ++Index;
671 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000672 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000673
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000674 // Check to see that all assembler local symbols were actually defined.
675 // Targets that don't do subsections via symbols may not want this, though,
676 // so conservatively exclude them. Only do this if we're finalizing, though,
677 // as otherwise we won't necessarilly have seen everything yet.
678 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
679 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
680 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000681 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000682 i != e; ++i) {
683 MCSymbol *Sym = i->getValue();
684 // Variable symbols may not be marked as defined, so check those
685 // explicitly. If we know it's a variable, we have a definition for
686 // the purposes of this check.
687 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
688 // FIXME: We would really like to refer back to where the symbol was
689 // first referenced for a source location. We need to add something
690 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000691 printMessage(
692 getLexer().getLoc(), SourceMgr::DK_Error,
693 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000694 }
695 }
696
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000697 // Finalize the output stream if there are no errors and if the client wants
698 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000699 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000700 Out.Finish();
701
Chris Lattner73f36112009-07-02 21:53:43 +0000702 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000703}
704
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000705void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000706 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000708 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000709 }
710}
711
Jim Grosbach4b905842013-09-20 23:08:21 +0000712/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000715 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000716
Chris Lattnere5074c42009-06-22 01:29:09 +0000717 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000718 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000719 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000720}
721
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000722StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 const char *Start = getTok().getLoc().getPointer();
724
Jim Grosbach4b905842013-09-20 23:08:21 +0000725 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000726 Lex();
727
728 const char *End = getTok().getLoc().getPointer();
729 return StringRef(Start, End - Start);
730}
Chris Lattner78db3622009-06-22 05:51:26 +0000731
Jim Grosbach4b905842013-09-20 23:08:21 +0000732StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000733 const char *Start = getTok().getLoc().getPointer();
734
735 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000736 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000737 Lex();
738
739 const char *End = getTok().getLoc().getPointer();
740 return StringRef(Start, End - Start);
741}
742
Jim Grosbach4b905842013-09-20 23:08:21 +0000743/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000744/// NOTE: This assumes the leading '(' has already been consumed.
745///
746/// parenexpr ::= expr)
747///
Jim Grosbach4b905842013-09-20 23:08:21 +0000748bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
749 if (parseExpression(Res))
750 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000751 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000753 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000754 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000755 return false;
756}
Chris Lattner78db3622009-06-22 05:51:26 +0000757
Jim Grosbach4b905842013-09-20 23:08:21 +0000758/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000759/// NOTE: This assumes the leading '[' has already been consumed.
760///
761/// bracketexpr ::= expr]
762///
Jim Grosbach4b905842013-09-20 23:08:21 +0000763bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
764 if (parseExpression(Res))
765 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 if (Lexer.isNot(AsmToken::RBrac))
767 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000768 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000769 Lex();
770 return false;
771}
772
Jim Grosbach4b905842013-09-20 23:08:21 +0000773/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000774/// primaryexpr ::= (parenexpr
775/// primaryexpr ::= symbol
776/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000777/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000778/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000779bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000780 SMLoc FirstTokenLoc = getLexer().getLoc();
781 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
782 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000783 default:
784 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000785 // If we have an error assume that we've already handled it.
786 case AsmToken::Error:
787 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000788 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000789 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000790 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000792 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000793 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000794 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000795 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000796 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000797 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000798 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000799 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000800 if (FirstTokenKind == AsmToken::Dollar) {
801 if (Lexer.getMAI().getDollarIsPC()) {
802 // This is a '$' reference, which references the current PC. Emit a
803 // temporary label to the streamer and refer to it.
804 MCSymbol *Sym = Ctx.CreateTempSymbol();
805 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000806 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
807 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000808 EndLoc = FirstTokenLoc;
809 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000810 }
811 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000812 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000813 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000814 // Parse symbol variant
815 std::pair<StringRef, StringRef> Split;
816 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000817 if (FirstTokenKind == AsmToken::String) {
818 if (Lexer.is(AsmToken::At)) {
819 Lexer.Lex(); // eat @
820 SMLoc AtLoc = getLexer().getLoc();
821 StringRef VName;
822 if (parseIdentifier(VName))
823 return Error(AtLoc, "expected symbol variant after '@'");
824
825 Split = std::make_pair(Identifier, VName);
826 }
827 } else {
828 Split = Identifier.split('@');
829 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000830 } else if (Lexer.is(AsmToken::LParen)) {
831 Lexer.Lex(); // eat (
832 StringRef VName;
833 parseIdentifier(VName);
834 if (Lexer.isNot(AsmToken::RParen)) {
835 return Error(Lexer.getTok().getLoc(),
836 "unexpected token in variant, expected ')'");
837 }
838 Lexer.Lex(); // eat )
839 Split = std::make_pair(Identifier, VName);
840 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000841
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000842 EndLoc = SMLoc::getFromPointer(Identifier.end());
843
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 StringRef SymbolName = Identifier;
846 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000847
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000848 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000849 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000850 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 if (Variant != MCSymbolRefExpr::VK_Invalid) {
852 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000853 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000854 Variant = MCSymbolRefExpr::VK_None;
855 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000856 return Error(SMLoc::getFromPointer(Split.second.begin()),
857 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000858 }
859 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000860
Hans Wennborgce69d772013-10-18 20:46:28 +0000861 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
862
Daniel Dunbard20cda02009-10-16 01:34:54 +0000863 // If this is an absolute variable reference, substitute it now to preserve
864 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000865 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000866 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000867 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000868
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000869 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000870 return false;
871 }
872
873 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000874 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000875 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000876 }
David Woodhousef42a6662014-02-01 16:20:54 +0000877 case AsmToken::BigNum:
878 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000879 case AsmToken::Integer: {
880 SMLoc Loc = getTok().getLoc();
881 int64_t IntVal = getTok().getIntVal();
882 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000883 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000884 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 // Look for 'b' or 'f' following an Integer as a directional label
886 if (Lexer.getKind() == AsmToken::Identifier) {
887 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 // Lookup the symbol variant if used.
889 std::pair<StringRef, StringRef> Split = IDVal.split('@');
890 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
891 if (Split.first.size() != IDVal.size()) {
892 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000893 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000895 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000897 if (IDVal == "f" || IDVal == "b") {
898 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000899 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000900 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000901 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 Lex(); // Eat identifier.
905 }
906 }
Chris Lattner78db3622009-06-22 05:51:26 +0000907 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000908 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 case AsmToken::Real: {
910 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000911 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000912 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000913 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000914 Lex(); // Eat token.
915 return false;
916 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000917 case AsmToken::Dot: {
918 // This is a '.' reference, which references the current PC. Emit a
919 // temporary label to the streamer and refer to it.
920 MCSymbol *Sym = Ctx.CreateTempSymbol();
921 Out.EmitLabel(Sym);
922 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000923 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000924 Lex(); // Eat identifier.
925 return false;
926 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000930 case AsmToken::LBrac:
931 if (!PlatformParser->HasBracketExpressions())
932 return TokError("brackets expression not supported on this target");
933 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000945 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000947 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000948 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000949 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000950 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000951 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000953 }
954}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000955
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000956bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000957 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000958 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000959}
960
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000962AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000964 // Ask the target implementation about this expression first.
965 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
966 if (NewE)
967 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968 // Recurse over the given expression, rebuilding it to apply the given variant
969 // if there is exactly one symbol.
970 switch (E->getKind()) {
971 case MCExpr::Target:
972 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000973 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000974
975 case MCExpr::SymbolRef: {
976 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
977
978 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 TokError("invalid variant on expression '" + getTok().getIdentifier() +
980 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 return E;
982 }
983
984 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
985 }
986
987 case MCExpr::Unary: {
988 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000991 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
993 }
994
995 case MCExpr::Binary: {
996 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
998 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
1000 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001001 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002
Jim Grosbach4b905842013-09-20 23:08:21 +00001003 if (!LHS)
1004 LHS = BE->getLHS();
1005 if (!RHS)
1006 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007
1008 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1009 }
1010 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001011
Craig Toppera2886c22012-02-07 05:05:23 +00001012 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013}
1014
Jim Grosbach4b905842013-09-20 23:08:21 +00001015/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001016///
Jim Grosbachbd164242011-08-20 16:24:13 +00001017/// expr ::= expr &&,|| expr -> lowest.
1018/// expr ::= expr |,^,&,! expr
1019/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1020/// expr ::= expr <<,>> expr
1021/// expr ::= expr +,- expr
1022/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001023/// expr ::= primaryexpr
1024///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001025bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001026 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001027 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001029 return true;
1030
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 // As a special case, we support 'a op b @ modifier' by rewriting the
1032 // expression to include the modifier. This is inefficient, but in general we
1033 // expect users to use 'a@modifier op b'.
1034 if (Lexer.getKind() == AsmToken::At) {
1035 Lex();
1036
1037 if (Lexer.isNot(AsmToken::Identifier))
1038 return TokError("unexpected symbol modifier following '@'");
1039
1040 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042 if (Variant == MCSymbolRefExpr::VK_Invalid)
1043 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1044
Jim Grosbach4b905842013-09-20 23:08:21 +00001045 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 if (!ModifiedRes) {
1047 return TokError("invalid modifier '" + getTok().getIdentifier() +
1048 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001050
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 Res = ModifiedRes;
1052 Lex();
1053 }
1054
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001055 // Try to constant fold it up front, if possible.
1056 int64_t Value;
1057 if (Res->EvaluateAsAbsolute(Value))
1058 Res = MCConstantExpr::Create(Value, getContext());
1059
1060 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001061}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001064 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001065 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001066}
1067
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001068bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001069 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070
Daniel Dunbar75630b32009-06-30 02:10:03 +00001071 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001072 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073 return true;
1074
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001075 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001076 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001077
1078 return false;
1079}
1080
Michael J. Spencer530ce852010-10-09 11:00:50 +00001081static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001083 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001084 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001086
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001090 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001091 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001093 return 1;
1094
Jim Grosbach4b905842013-09-20 23:08:21 +00001095 // Low Precedence: |, &, ^
1096 //
1097 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001104 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001105 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001106 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001107
Jim Grosbach4b905842013-09-20 23:08:21 +00001108 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001109 case AsmToken::EqualEqual:
1110 Kind = MCBinaryExpr::EQ;
1111 return 3;
1112 case AsmToken::ExclaimEqual:
1113 case AsmToken::LessGreater:
1114 Kind = MCBinaryExpr::NE;
1115 return 3;
1116 case AsmToken::Less:
1117 Kind = MCBinaryExpr::LT;
1118 return 3;
1119 case AsmToken::LessEqual:
1120 Kind = MCBinaryExpr::LTE;
1121 return 3;
1122 case AsmToken::Greater:
1123 Kind = MCBinaryExpr::GT;
1124 return 3;
1125 case AsmToken::GreaterEqual:
1126 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001127 return 3;
1128
Jim Grosbach4b905842013-09-20 23:08:21 +00001129 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 case AsmToken::LessLess:
1131 Kind = MCBinaryExpr::Shl;
1132 return 4;
1133 case AsmToken::GreaterGreater:
1134 Kind = MCBinaryExpr::Shr;
1135 return 4;
1136
Jim Grosbach4b905842013-09-20 23:08:21 +00001137 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138 case AsmToken::Plus:
1139 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001141 case AsmToken::Minus:
1142 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001144
Jim Grosbach4b905842013-09-20 23:08:21 +00001145 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001149 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001151 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001152 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001153 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001154 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 }
1156}
1157
Jim Grosbach4b905842013-09-20 23:08:21 +00001158/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001160bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001161 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001163 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001164 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166 // If the next token is lower precedence than we are allowed to eat, return
1167 // successfully with what we ate already.
1168 if (TokPrec < Precedence)
1169 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001170
Sean Callanan686ed8d2010-01-19 20:22:31 +00001171 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001174 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001175 if (parsePrimaryExpr(RHS, EndLoc))
1176 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001177
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178 // If BinOp binds less tightly with RHS than the operator after RHS, let
1179 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001181 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001182 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1183 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001184
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001185 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001186 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001187 }
1188}
1189
Chris Lattner36e02122009-06-21 20:54:55 +00001190/// ParseStatement:
1191/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001192/// ::= Label* Directive ...Operands... EndOfStatement
1193/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001194bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001195 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001196 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001197 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001198 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001199 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001200
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001201 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001202 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001203 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001204 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001206 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001207 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001208 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001209
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001210 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001211 if (Lexer.is(AsmToken::Integer)) {
1212 LocalLabelVal = getTok().getIntVal();
1213 if (LocalLabelVal < 0) {
1214 if (!TheCondState.Ignore)
1215 return TokError("unexpected token at start of statement");
1216 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001217 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001218 IDVal = getTok().getString();
1219 Lex(); // Consume the integer token to be used as an identifier token.
1220 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001221 if (!TheCondState.Ignore)
1222 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001223 }
1224 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001225 } else if (Lexer.is(AsmToken::Dot)) {
1226 // Treat '.' as a valid identifier in this context.
1227 Lex();
1228 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001229 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001230 if (!TheCondState.Ignore)
1231 return TokError("unexpected token at start of statement");
1232 IDVal = "";
1233 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001234
Chris Lattner926885c2010-04-17 18:14:27 +00001235 // Handle conditional assembly here before checking for skipping. We
1236 // have to do this so that .endif isn't skipped in a ".if 0" block for
1237 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001238 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001239 DirectiveKindMap.find(IDVal);
1240 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1241 ? DK_NO_DIRECTIVE
1242 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001243 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 default:
1245 break;
1246 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001247 case DK_IFEQ:
1248 case DK_IFGE:
1249 case DK_IFGT:
1250 case DK_IFLE:
1251 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001252 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001253 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001254 case DK_IFB:
1255 return parseDirectiveIfb(IDLoc, true);
1256 case DK_IFNB:
1257 return parseDirectiveIfb(IDLoc, false);
1258 case DK_IFC:
1259 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001260 case DK_IFEQS:
1261 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001262 case DK_IFNC:
1263 return parseDirectiveIfc(IDLoc, false);
1264 case DK_IFDEF:
1265 return parseDirectiveIfdef(IDLoc, true);
1266 case DK_IFNDEF:
1267 case DK_IFNOTDEF:
1268 return parseDirectiveIfdef(IDLoc, false);
1269 case DK_ELSEIF:
1270 return parseDirectiveElseIf(IDLoc);
1271 case DK_ELSE:
1272 return parseDirectiveElse(IDLoc);
1273 case DK_ENDIF:
1274 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001275 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001276
Eli Bendersky88024712013-01-16 19:32:36 +00001277 // Ignore the statement if in the middle of inactive conditional
1278 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001279 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001280 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001281 return false;
1282 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001283
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001284 // FIXME: Recurse on local labels?
1285
1286 // See what kind of statement we have.
1287 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001288 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001289 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001290
Chris Lattner36e02122009-06-21 20:54:55 +00001291 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001292 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001293
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001294 // Diagnose attempt to use '.' as a label.
1295 if (IDVal == ".")
1296 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1297
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001298 // Diagnose attempt to use a variable as a label.
1299 //
1300 // FIXME: Diagnostics. Note the location of the definition as a label.
1301 // FIXME: This doesn't diagnose assignment to a symbol which has been
1302 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001303 MCSymbol *Sym;
1304 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001305 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001306 else
1307 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001308 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001309 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310
Daniel Dunbare73b2672009-08-26 22:13:22 +00001311 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001312 if (!ParsingInlineAsm)
1313 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001314
Kevin Enderbye7739d42011-12-09 18:09:40 +00001315 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001316 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001318 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1319 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001320
Tim Northover1744d0a2013-10-25 12:49:50 +00001321 getTargetParser().onLabelParsed(Sym);
1322
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001323 // Consume any end of statement token, if present, to avoid spurious
1324 // AddBlankLine calls().
1325 if (Lexer.is(AsmToken::EndOfStatement)) {
1326 Lex();
1327 if (Lexer.is(AsmToken::Eof))
1328 return false;
1329 }
1330
Eli Friedman0f4871d2012-10-22 23:58:19 +00001331 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001332 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001334 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001336 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337
Jim Grosbach4b905842013-09-20 23:08:21 +00001338 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001339
1340 default: // Normal instruction or directive.
1341 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001342 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001343
1344 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001345 if (areMacrosEnabled())
1346 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1347 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001348 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001349
Michael J. Spencer530ce852010-10-09 11:00:50 +00001350 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001351
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001353 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001355 //
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // 1. The target-specific assembly parser. Some directives are target
1357 // specific or may potentially behave differently on certain targets.
1358 // 2. Asm parser extensions. For example, platform-specific parsers
1359 // (like the ELF parser) register themselves as extensions.
1360 // 3. The generic directive parser implemented by this class. These are
1361 // all the directives that behave in a target and platform independent
1362 // manner, or at least have a default behavior that's shared between
1363 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001364
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // First query the target-specific parser. It will return 'true' if it
1366 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001367 if (!getTargetParser().ParseDirective(ID))
1368 return false;
1369
Alp Tokercb402912014-01-24 17:20:08 +00001370 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001371 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001372 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1373 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001374 if (Handler.first)
1375 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1376
1377 // Finally, if no one else is interested in this directive, it must be
1378 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001379 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001380 default:
1381 break;
1382 case DK_SET:
1383 case DK_EQU:
1384 return parseDirectiveSet(IDVal, true);
1385 case DK_EQUIV:
1386 return parseDirectiveSet(IDVal, false);
1387 case DK_ASCII:
1388 return parseDirectiveAscii(IDVal, false);
1389 case DK_ASCIZ:
1390 case DK_STRING:
1391 return parseDirectiveAscii(IDVal, true);
1392 case DK_BYTE:
1393 return parseDirectiveValue(1);
1394 case DK_SHORT:
1395 case DK_VALUE:
1396 case DK_2BYTE:
1397 return parseDirectiveValue(2);
1398 case DK_LONG:
1399 case DK_INT:
1400 case DK_4BYTE:
1401 return parseDirectiveValue(4);
1402 case DK_QUAD:
1403 case DK_8BYTE:
1404 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001405 case DK_OCTA:
1406 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001407 case DK_SINGLE:
1408 case DK_FLOAT:
1409 return parseDirectiveRealValue(APFloat::IEEEsingle);
1410 case DK_DOUBLE:
1411 return parseDirectiveRealValue(APFloat::IEEEdouble);
1412 case DK_ALIGN: {
1413 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1414 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1415 }
1416 case DK_ALIGN32: {
1417 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1418 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1419 }
1420 case DK_BALIGN:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1422 case DK_BALIGNW:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1424 case DK_BALIGNL:
1425 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1426 case DK_P2ALIGN:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1428 case DK_P2ALIGNW:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1430 case DK_P2ALIGNL:
1431 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1432 case DK_ORG:
1433 return parseDirectiveOrg();
1434 case DK_FILL:
1435 return parseDirectiveFill();
1436 case DK_ZERO:
1437 return parseDirectiveZero();
1438 case DK_EXTERN:
1439 eatToEndOfStatement(); // .extern is the default, ignore it.
1440 return false;
1441 case DK_GLOBL:
1442 case DK_GLOBAL:
1443 return parseDirectiveSymbolAttribute(MCSA_Global);
1444 case DK_LAZY_REFERENCE:
1445 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1446 case DK_NO_DEAD_STRIP:
1447 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1448 case DK_SYMBOL_RESOLVER:
1449 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1450 case DK_PRIVATE_EXTERN:
1451 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1452 case DK_REFERENCE:
1453 return parseDirectiveSymbolAttribute(MCSA_Reference);
1454 case DK_WEAK_DEFINITION:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1456 case DK_WEAK_REFERENCE:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1458 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1459 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1460 case DK_COMM:
1461 case DK_COMMON:
1462 return parseDirectiveComm(/*IsLocal=*/false);
1463 case DK_LCOMM:
1464 return parseDirectiveComm(/*IsLocal=*/true);
1465 case DK_ABORT:
1466 return parseDirectiveAbort();
1467 case DK_INCLUDE:
1468 return parseDirectiveInclude();
1469 case DK_INCBIN:
1470 return parseDirectiveIncbin();
1471 case DK_CODE16:
1472 case DK_CODE16GCC:
1473 return TokError(Twine(IDVal) + " not supported yet");
1474 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001475 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001476 case DK_IRP:
1477 return parseDirectiveIrp(IDLoc);
1478 case DK_IRPC:
1479 return parseDirectiveIrpc(IDLoc);
1480 case DK_ENDR:
1481 return parseDirectiveEndr(IDLoc);
1482 case DK_BUNDLE_ALIGN_MODE:
1483 return parseDirectiveBundleAlignMode();
1484 case DK_BUNDLE_LOCK:
1485 return parseDirectiveBundleLock();
1486 case DK_BUNDLE_UNLOCK:
1487 return parseDirectiveBundleUnlock();
1488 case DK_SLEB128:
1489 return parseDirectiveLEB128(true);
1490 case DK_ULEB128:
1491 return parseDirectiveLEB128(false);
1492 case DK_SPACE:
1493 case DK_SKIP:
1494 return parseDirectiveSpace(IDVal);
1495 case DK_FILE:
1496 return parseDirectiveFile(IDLoc);
1497 case DK_LINE:
1498 return parseDirectiveLine();
1499 case DK_LOC:
1500 return parseDirectiveLoc();
1501 case DK_STABS:
1502 return parseDirectiveStabs();
1503 case DK_CFI_SECTIONS:
1504 return parseDirectiveCFISections();
1505 case DK_CFI_STARTPROC:
1506 return parseDirectiveCFIStartProc();
1507 case DK_CFI_ENDPROC:
1508 return parseDirectiveCFIEndProc();
1509 case DK_CFI_DEF_CFA:
1510 return parseDirectiveCFIDefCfa(IDLoc);
1511 case DK_CFI_DEF_CFA_OFFSET:
1512 return parseDirectiveCFIDefCfaOffset();
1513 case DK_CFI_ADJUST_CFA_OFFSET:
1514 return parseDirectiveCFIAdjustCfaOffset();
1515 case DK_CFI_DEF_CFA_REGISTER:
1516 return parseDirectiveCFIDefCfaRegister(IDLoc);
1517 case DK_CFI_OFFSET:
1518 return parseDirectiveCFIOffset(IDLoc);
1519 case DK_CFI_REL_OFFSET:
1520 return parseDirectiveCFIRelOffset(IDLoc);
1521 case DK_CFI_PERSONALITY:
1522 return parseDirectiveCFIPersonalityOrLsda(true);
1523 case DK_CFI_LSDA:
1524 return parseDirectiveCFIPersonalityOrLsda(false);
1525 case DK_CFI_REMEMBER_STATE:
1526 return parseDirectiveCFIRememberState();
1527 case DK_CFI_RESTORE_STATE:
1528 return parseDirectiveCFIRestoreState();
1529 case DK_CFI_SAME_VALUE:
1530 return parseDirectiveCFISameValue(IDLoc);
1531 case DK_CFI_RESTORE:
1532 return parseDirectiveCFIRestore(IDLoc);
1533 case DK_CFI_ESCAPE:
1534 return parseDirectiveCFIEscape();
1535 case DK_CFI_SIGNAL_FRAME:
1536 return parseDirectiveCFISignalFrame();
1537 case DK_CFI_UNDEFINED:
1538 return parseDirectiveCFIUndefined(IDLoc);
1539 case DK_CFI_REGISTER:
1540 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001541 case DK_CFI_WINDOW_SAVE:
1542 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001543 case DK_MACROS_ON:
1544 case DK_MACROS_OFF:
1545 return parseDirectiveMacrosOnOff(IDVal);
1546 case DK_MACRO:
1547 return parseDirectiveMacro(IDLoc);
1548 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(
1603 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001604
Eli Bendersky88024712013-01-16 19:32:36 +00001605 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001606
Eli Bendersky88024712013-01-16 19:32:36 +00001607 // If we previously parsed a cpp hash file line comment then make sure the
1608 // current Dwarf File is for the CppHashFilename if not then emit the
1609 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001610 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001611 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1612 0, StringRef(), CppHashFilename);
1613 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001614
Jim Grosbach4b905842013-09-20 23:08:21 +00001615 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1616 // cache with the different Loc from the call above we save the last
1617 // info we queried here with SrcMgr.FindLineNumber().
1618 unsigned CppHashLocLineNo;
1619 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1620 CppHashLocLineNo = LastQueryLine;
1621 else {
1622 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1623 LastQueryLine = CppHashLocLineNo;
1624 LastQueryIDLoc = CppHashLoc;
1625 LastQueryBuffer = CppHashBuf;
1626 }
1627 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001628 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001629
Jim Grosbach4b905842013-09-20 23:08:21 +00001630 getStreamer().EmitDwarfLocDirective(
1631 getContext().getGenDwarfFileNumber(), Line, 0,
1632 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1633 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001634 }
1635
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001636 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001637 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001638 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001639 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1640 Info.ParsedOperands, Out,
1641 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001642 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001643
Chris Lattnera2a9d162010-09-11 16:18:25 +00001644 // Don't skip the rest of the line, the instruction parser is responsible for
1645 // that.
1646 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001647}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001648
Jim Grosbach4b905842013-09-20 23:08:21 +00001649/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001650/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001651void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001652 if (!Lexer.is(AsmToken::EndOfStatement))
1653 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001654 // Eat EOL.
1655 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001656}
1657
Jim Grosbach4b905842013-09-20 23:08:21 +00001658/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001659/// ::= # number "filename"
1660/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001661bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001662 Lex(); // Eat the hash token.
1663
1664 if (getLexer().isNot(AsmToken::Integer)) {
1665 // Consume the line since in cases it is not a well-formed line directive,
1666 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001667 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001668 return false;
1669 }
1670
1671 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001672 Lex();
1673
1674 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001675 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001676 return false;
1677 }
1678
1679 StringRef Filename = getTok().getString();
1680 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001681 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001682
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001683 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1684 CppHashLoc = L;
1685 CppHashFilename = Filename;
1686 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001687 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001688
1689 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001690 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001691 return false;
1692}
1693
Jim Grosbach4b905842013-09-20 23:08:21 +00001694/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695/// for the Filename and LineNo if any in the diagnostic.
1696void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001697 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698 raw_ostream &OS = errs();
1699
1700 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1701 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001702 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1703 unsigned CppHashBuf =
1704 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001705
Jim Grosbach4b905842013-09-20 23:08:21 +00001706 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001708 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1709 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1710 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001711 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1712 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713 }
1714
Eric Christophera7c32732012-12-18 00:30:54 +00001715 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001716 // manager changed or buffer changed (like in a nested include) then just
1717 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001718 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001719 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001720 if (Parser->SavedDiagHandler)
1721 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1722 else
Craig Topper353eda42014-04-24 06:44:33 +00001723 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 return;
1725 }
1726
Eric Christophera7c32732012-12-18 00:30:54 +00001727 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001728 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1729 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001730 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001731
1732 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1733 int CppHashLocLineNo =
1734 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001735 int LineNo =
1736 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001737
Jim Grosbach4b905842013-09-20 23:08:21 +00001738 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1739 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001740 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001742 if (Parser->SavedDiagHandler)
1743 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1744 else
Craig Topper353eda42014-04-24 06:44:33 +00001745 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001746}
1747
Rafael Espindola2c064482012-08-21 18:29:30 +00001748// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1749// difference being that that function accepts '@' as part of identifiers and
1750// we can't do that. AsmLexer.cpp should probably be changed to handle
1751// '@' as a special case when needed.
1752static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001753 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1754 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001755}
1756
Rafael Espindola34b9c512012-06-03 23:57:14 +00001757bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001758 ArrayRef<MCAsmMacroParameter> Parameters,
1759 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001761 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001762 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001764
Preston Gurd05500642012-09-19 20:36:12 +00001765 // A macro without parameters is handled differently on Darwin:
1766 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001767 while (!Body.empty()) {
1768 // Scan for the next substitution.
1769 std::size_t End = Body.size(), Pos = 0;
1770 for (; Pos != End; ++Pos) {
1771 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001772 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001773 // This macro has no parameters, look for $0, $1, etc.
1774 if (Body[Pos] != '$' || Pos + 1 == End)
1775 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001776
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001778 if (Next == '$' || Next == 'n' ||
1779 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 break;
1781 } else {
1782 // This macro has parameters, look for \foo, \bar, etc.
1783 if (Body[Pos] == '\\' && Pos + 1 != End)
1784 break;
1785 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001786 }
1787
1788 // Add the prefix.
1789 OS << Body.slice(0, Pos);
1790
1791 // Check if we reached the end.
1792 if (Pos == End)
1793 break;
1794
Benjamin Kramer513e7442014-02-20 13:36:32 +00001795 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 switch (Body[Pos + 1]) {
1797 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001798 case '$':
1799 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001800 break;
1801
Jim Grosbach4b905842013-09-20 23:08:21 +00001802 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001803 case 'n':
1804 OS << A.size();
1805 break;
1806
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001808 default: {
1809 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001811 if (Index >= A.size())
1812 break;
1813
1814 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001815 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001816 ie = A[Index].end();
1817 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001818 OS << it->getString();
1819 break;
1820 }
1821 }
1822 Pos += 2;
1823 } else {
1824 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001825 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 ++I;
1827
Jim Grosbach4b905842013-09-20 23:08:21 +00001828 const char *Begin = Body.data() + Pos + 1;
1829 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001830 unsigned Index = 0;
1831 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001832 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001833 break;
1834
Preston Gurd05500642012-09-19 20:36:12 +00001835 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001836 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1837 Pos += 3;
1838 else {
1839 OS << '\\' << Argument;
1840 Pos = I;
1841 }
Preston Gurd05500642012-09-19 20:36:12 +00001842 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001843 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001844 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001845 ie = A[Index].end();
1846 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001847 // We expect no quotes around the string's contents when
1848 // parsing for varargs.
1849 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001850 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001851 else
1852 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001853
Preston Gurd05500642012-09-19 20:36:12 +00001854 Pos += 1 + Argument.size();
1855 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001856 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001857 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001858 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001859 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001860
Rafael Espindola1134ab232011-06-05 02:43:45 +00001861 return false;
1862}
Daniel Dunbar43235712010-07-18 18:54:11 +00001863
Jim Grosbach4b905842013-09-20 23:08:21 +00001864MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1865 SMLoc EL, MemoryBuffer *I)
1866 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1867 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001868
Jim Grosbach4b905842013-09-20 23:08:21 +00001869static bool isOperator(AsmToken::TokenKind kind) {
1870 switch (kind) {
1871 default:
1872 return false;
1873 case AsmToken::Plus:
1874 case AsmToken::Minus:
1875 case AsmToken::Tilde:
1876 case AsmToken::Slash:
1877 case AsmToken::Star:
1878 case AsmToken::Dot:
1879 case AsmToken::Equal:
1880 case AsmToken::EqualEqual:
1881 case AsmToken::Pipe:
1882 case AsmToken::PipePipe:
1883 case AsmToken::Caret:
1884 case AsmToken::Amp:
1885 case AsmToken::AmpAmp:
1886 case AsmToken::Exclaim:
1887 case AsmToken::ExclaimEqual:
1888 case AsmToken::Percent:
1889 case AsmToken::Less:
1890 case AsmToken::LessEqual:
1891 case AsmToken::LessLess:
1892 case AsmToken::LessGreater:
1893 case AsmToken::Greater:
1894 case AsmToken::GreaterEqual:
1895 case AsmToken::GreaterGreater:
1896 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001897 }
1898}
1899
David Majnemer16252452014-01-29 00:07:39 +00001900namespace {
1901class AsmLexerSkipSpaceRAII {
1902public:
1903 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1904 Lexer.setSkipSpace(SkipSpace);
1905 }
1906
1907 ~AsmLexerSkipSpaceRAII() {
1908 Lexer.setSkipSpace(true);
1909 }
1910
1911private:
1912 AsmLexer &Lexer;
1913};
1914}
1915
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001916bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1917
1918 if (Vararg) {
1919 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1920 StringRef Str = parseStringToEndOfStatement();
1921 MA.push_back(AsmToken(AsmToken::String, Str));
1922 }
1923 return false;
1924 }
1925
Rafael Espindola768b41c2012-06-15 14:02:34 +00001926 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001927 unsigned AddTokens = 0;
1928
David Majnemer16252452014-01-29 00:07:39 +00001929 // Darwin doesn't use spaces to delmit arguments.
1930 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931
1932 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001933 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001934 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001935
David Majnemer91fc4c22014-01-29 18:57:46 +00001936 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001937 break;
Preston Gurd05500642012-09-19 20:36:12 +00001938
1939 if (Lexer.is(AsmToken::Space)) {
1940 Lex(); // Eat spaces
1941
1942 // Spaces can delimit parameters, but could also be part an expression.
1943 // If the token after a space is an operator, add the token and the next
1944 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001945 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001946 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001947 // Check to see whether the token is used as an operator,
1948 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001949 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001950 if (*NextChar == ' ')
1951 AddTokens = 2;
1952 }
1953
1954 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001955 break;
1956 }
1957 }
1958 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959
Jim Grosbach4b905842013-09-20 23:08:21 +00001960 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961 // to be able to fill in the remaining default parameter values
1962 if (Lexer.is(AsmToken::EndOfStatement))
1963 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964
1965 // Adjust the current parentheses level.
1966 if (Lexer.is(AsmToken::LParen))
1967 ++ParenLevel;
1968 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1969 --ParenLevel;
1970
1971 // Append the token to the current argument list.
1972 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001973 if (AddTokens)
1974 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001975 Lex();
1976 }
Preston Gurd05500642012-09-19 20:36:12 +00001977
Rafael Espindola768b41c2012-06-15 14:02:34 +00001978 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001979 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980 return false;
1981}
1982
1983// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001984bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001985 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001986 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001987 bool NamedParametersFound = false;
1988 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001990 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001991 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001992
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993 // Parse two kinds of macro invocations:
1994 // - macros defined without any parameters accept an arbitrary number of them
1995 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001996 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1998 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001999 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002000 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002001
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002002 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002003 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002004 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002005 eatToEndOfStatement();
2006 return true;
2007 }
2008
2009 if (!Lexer.is(AsmToken::Equal)) {
2010 TokError("expected '=' after formal parameter identifier");
2011 eatToEndOfStatement();
2012 return true;
2013 }
2014 Lex();
2015
2016 NamedParametersFound = true;
2017 }
2018
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002019 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002020 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002021 eatToEndOfStatement();
2022 return true;
2023 }
2024
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002025 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2026 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002027 return true;
2028
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002029 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002030 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 unsigned FAI = 0;
2032 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002033 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002034 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002035
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002037 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002038 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002039 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002040 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002041 return true;
2042 }
2043 PI = FAI;
2044 }
2045
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002046 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002047 if (A.size() <= PI)
2048 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002049 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002050
2051 if (FALocs.size() <= PI)
2052 FALocs.resize(PI + 1);
2053
2054 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002055 }
Jim Grosbach206661622012-07-30 22:44:17 +00002056
Preston Gurd242ed3152012-09-19 20:29:04 +00002057 // At the end of the statement, fill in remaining arguments that have
2058 // default values. If there aren't any, then the next argument is
2059 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002060 if (Lexer.is(AsmToken::EndOfStatement)) {
2061 bool Failure = false;
2062 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2063 if (A[FAI].empty()) {
2064 if (M->Parameters[FAI].Required) {
2065 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2066 "missing value for required parameter "
2067 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2068 Failure = true;
2069 }
2070
2071 if (!M->Parameters[FAI].Value.empty())
2072 A[FAI] = M->Parameters[FAI].Value;
2073 }
2074 }
2075 return Failure;
2076 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002077
2078 if (Lexer.is(AsmToken::Comma))
2079 Lex();
2080 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002081
2082 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002083}
2084
Jim Grosbach4b905842013-09-20 23:08:21 +00002085const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2086 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002087 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002088}
2089
Jim Grosbach4b905842013-09-20 23:08:21 +00002090void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002091 MacroMap[Name] = new MCAsmMacro(Macro);
2092}
2093
Jim Grosbach4b905842013-09-20 23:08:21 +00002094void AsmParser::undefineMacro(StringRef Name) {
2095 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002096 if (I != MacroMap.end()) {
2097 delete I->getValue();
2098 MacroMap.erase(I);
2099 }
2100}
2101
Jim Grosbach4b905842013-09-20 23:08:21 +00002102bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002103 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2104 // this, although we should protect against infinite loops.
2105 if (ActiveMacros.size() == 20)
2106 return TokError("macros cannot be nested more than 20 levels deep");
2107
Eli Bendersky38274122013-01-14 23:22:36 +00002108 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002109 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002110 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002111
Rafael Espindola1134ab232011-06-05 02:43:45 +00002112 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2113 // to hold the macro body with substitutions.
2114 SmallString<256> Buf;
2115 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002116 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002117
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002118 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002119 return true;
2120
Eli Bendersky38274122013-01-14 23:22:36 +00002121 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002122 // instantiation.
2123 OS << ".endmacro\n";
2124
Rafael Espindola1134ab232011-06-05 02:43:45 +00002125 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002127
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 // Create the macro instantiation object and add to the current macro
2129 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002130 MacroInstantiation *MI = new MacroInstantiation(
2131 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002132 ActiveMacros.push_back(MI);
2133
2134 // Jump to the macro instantiation and prime the lexer.
2135 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002136 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002137 Lex();
2138
2139 return false;
2140}
2141
Jim Grosbach4b905842013-09-20 23:08:21 +00002142void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002143 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002144 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002145 Lex();
2146
2147 // Pop the instantiation entry.
2148 delete ActiveMacros.back();
2149 ActiveMacros.pop_back();
2150}
2151
Jim Grosbach4b905842013-09-20 23:08:21 +00002152static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002153 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002154 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002155 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2156 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002157 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002158 case MCExpr::Target:
2159 case MCExpr::Constant:
2160 return false;
2161 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002162 const MCSymbol &S =
2163 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002164 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002165 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002166 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002167 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002168 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002169 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002170 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002171
2172 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002173}
2174
Jim Grosbach4b905842013-09-20 23:08:21 +00002175bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002176 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002177 // FIXME: Use better location, we should use proper tokens.
2178 SMLoc EqualLoc = Lexer.getLoc();
2179
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002180 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002181 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002182 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002183
Rafael Espindola72f5f172012-01-28 05:57:00 +00002184 // Note: we don't count b as used in "a = b". This is to allow
2185 // a = b
2186 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002187
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002188 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189 return TokError("unexpected token in assignment");
2190
2191 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002192 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002193
Daniel Dunbar5f339242009-10-16 01:57:39 +00002194 // Validate that the LHS is allowed to be a variable (either it has not been
2195 // used as a symbol, or it is an absolute symbol).
2196 MCSymbol *Sym = getContext().LookupSymbol(Name);
2197 if (Sym) {
2198 // Diagnose assignment to a label.
2199 //
2200 // FIXME: Diagnostics. Note the location of the definition as a label.
2201 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002202 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002203 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2204 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002205 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002206 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2207 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002208 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002209 return Error(EqualLoc, "redefinition of '" + Name + "'");
2210 else if (!Sym->isVariable())
2211 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002212 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002213 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002214 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002215
2216 // Don't count these checks as uses.
2217 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002218 } else if (Name == ".") {
2219 if (Out.EmitValueToOffset(Value, 0)) {
2220 Error(EqualLoc, "expected absolute expression");
2221 eatToEndOfStatement();
2222 }
2223 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002224 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002225 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002226
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
2608 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2609 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2610
2611 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2612 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2613 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2614 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002615
2616 return false;
2617}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002618
Jim Grosbach4b905842013-09-20 23:08:21 +00002619/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002620/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002621bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002622 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002623
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002624 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002625 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002626 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002627 return true;
2628
2629 // Parse optional fill expression.
2630 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002631 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2632 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002633 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002634 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002635
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002636 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002637 return true;
2638
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002639 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002640 return TokError("unexpected token in '.org' directive");
2641 }
2642
Sean Callanan686ed8d2010-01-19 20:22:31 +00002643 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002644
Jim Grosbachb5912772012-01-27 00:37:08 +00002645 // Only limited forms of relocatable expressions are accepted here, it
2646 // has to be relative to the current section. The streamer will return
2647 // 'true' if the expression wasn't evaluatable.
2648 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2649 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002650
2651 return false;
2652}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653
Jim Grosbach4b905842013-09-20 23:08:21 +00002654/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002656bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002658
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002661 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662 return true;
2663
2664 SMLoc MaxBytesLoc;
2665 bool HasFillExpr = false;
2666 int64_t FillExpr = 0;
2667 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002668 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2669 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002670 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002671 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672
2673 // The fill expression can be omitted while specifying a maximum number of
2674 // alignment bytes, e.g:
2675 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002676 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002678 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679 return true;
2680 }
2681
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002682 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2683 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002685 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002687 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002688 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002690
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002691 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002692 return TokError("unexpected token in directive");
2693 }
2694 }
2695
Sean Callanan686ed8d2010-01-19 20:22:31 +00002696 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002698 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002700
2701 // Compute alignment in bytes.
2702 if (IsPow2) {
2703 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002704 if (Alignment >= 32) {
2705 Error(AlignmentLoc, "invalid alignment value");
2706 Alignment = 31;
2707 }
2708
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002709 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002710 } else {
2711 // Reject alignments that aren't a power of two, for gas compatibility.
2712 if (!isPowerOf2_64(Alignment))
2713 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002714 }
2715
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002716 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 if (MaxBytesLoc.isValid()) {
2718 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002719 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002720 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002721 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002722 }
2723
2724 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002725 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002726 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002727 MaxBytesToFill = 0;
2728 }
2729 }
2730
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002731 // Check whether we should use optimal code alignment for this .align
2732 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002733 const MCSection *Section = getStreamer().getCurrentSection().first;
2734 assert(Section && "must have section to emit alignment");
2735 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002736 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2737 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002738 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002739 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002740 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002741 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2742 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002743 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002744
2745 return false;
2746}
2747
Jim Grosbach4b905842013-09-20 23:08:21 +00002748/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002749/// ::= .file [number] filename
2750/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002751bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002752 // FIXME: I'm not sure what this is.
2753 int64_t FileNumber = -1;
2754 SMLoc FileNumberLoc = getLexer().getLoc();
2755 if (getLexer().is(AsmToken::Integer)) {
2756 FileNumber = getTok().getIntVal();
2757 Lex();
2758
2759 if (FileNumber < 1)
2760 return TokError("file number less than one");
2761 }
2762
2763 if (getLexer().isNot(AsmToken::String))
2764 return TokError("unexpected token in '.file' directive");
2765
2766 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002767 // Allow the strings to have escaped octal character sequence.
2768 std::string Path = getTok().getString();
2769 if (parseEscapedString(Path))
2770 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002771 Lex();
2772
2773 StringRef Directory;
2774 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002775 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002776 if (getLexer().is(AsmToken::String)) {
2777 if (FileNumber == -1)
2778 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002779 if (parseEscapedString(FilenameData))
2780 return true;
2781 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002782 Directory = Path;
2783 Lex();
2784 } else {
2785 Filename = Path;
2786 }
2787
2788 if (getLexer().isNot(AsmToken::EndOfStatement))
2789 return TokError("unexpected token in '.file' directive");
2790
2791 if (FileNumber == -1)
2792 getStreamer().EmitFileDirective(Filename);
2793 else {
2794 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002795 Error(DirectiveLoc,
2796 "input can't have .file dwarf directives when -g is "
2797 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002798
David Blaikiec714ef42014-03-17 01:52:11 +00002799 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2800 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002801 Error(FileNumberLoc, "file number already allocated");
2802 }
2803
2804 return false;
2805}
2806
Jim Grosbach4b905842013-09-20 23:08:21 +00002807/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002808/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002809bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002810 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2811 if (getLexer().isNot(AsmToken::Integer))
2812 return TokError("unexpected token in '.line' directive");
2813
2814 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002815 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002816 Lex();
2817
2818 // FIXME: Do something with the .line.
2819 }
2820
2821 if (getLexer().isNot(AsmToken::EndOfStatement))
2822 return TokError("unexpected token in '.line' directive");
2823
2824 return false;
2825}
2826
Jim Grosbach4b905842013-09-20 23:08:21 +00002827/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002828/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2829/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2830/// The first number is a file number, must have been previously assigned with
2831/// a .file directive, the second number is the line number and optionally the
2832/// third number is a column position (zero if not specified). The remaining
2833/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002834bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002835 if (getLexer().isNot(AsmToken::Integer))
2836 return TokError("unexpected token in '.loc' directive");
2837 int64_t FileNumber = getTok().getIntVal();
2838 if (FileNumber < 1)
2839 return TokError("file number less than one in '.loc' directive");
2840 if (!getContext().isValidDwarfFileNumber(FileNumber))
2841 return TokError("unassigned file number in '.loc' directive");
2842 Lex();
2843
2844 int64_t LineNumber = 0;
2845 if (getLexer().is(AsmToken::Integer)) {
2846 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002847 if (LineNumber < 0)
2848 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002849 Lex();
2850 }
2851
2852 int64_t ColumnPos = 0;
2853 if (getLexer().is(AsmToken::Integer)) {
2854 ColumnPos = getTok().getIntVal();
2855 if (ColumnPos < 0)
2856 return TokError("column position less than zero in '.loc' directive");
2857 Lex();
2858 }
2859
2860 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2861 unsigned Isa = 0;
2862 int64_t Discriminator = 0;
2863 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2864 for (;;) {
2865 if (getLexer().is(AsmToken::EndOfStatement))
2866 break;
2867
2868 StringRef Name;
2869 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002870 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002871 return TokError("unexpected token in '.loc' directive");
2872
2873 if (Name == "basic_block")
2874 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2875 else if (Name == "prologue_end")
2876 Flags |= DWARF2_FLAG_PROLOGUE_END;
2877 else if (Name == "epilogue_begin")
2878 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2879 else if (Name == "is_stmt") {
2880 Loc = getTok().getLoc();
2881 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002882 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002883 return true;
2884 // The expression must be the constant 0 or 1.
2885 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2886 int Value = MCE->getValue();
2887 if (Value == 0)
2888 Flags &= ~DWARF2_FLAG_IS_STMT;
2889 else if (Value == 1)
2890 Flags |= DWARF2_FLAG_IS_STMT;
2891 else
2892 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002893 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002894 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2895 }
Craig Topperf15655b2013-04-22 04:22:40 +00002896 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002897 Loc = getTok().getLoc();
2898 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002899 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return true;
2901 // The expression must be a constant greater or equal to 0.
2902 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2903 int Value = MCE->getValue();
2904 if (Value < 0)
2905 return Error(Loc, "isa number less than zero");
2906 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002907 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return Error(Loc, "isa number not a constant value");
2909 }
Craig Topperf15655b2013-04-22 04:22:40 +00002910 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002911 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002913 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return Error(Loc, "unknown sub-directive in '.loc' directive");
2915 }
2916
2917 if (getLexer().is(AsmToken::EndOfStatement))
2918 break;
2919 }
2920 }
2921
2922 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2923 Isa, Discriminator, StringRef());
2924
2925 return false;
2926}
2927
Jim Grosbach4b905842013-09-20 23:08:21 +00002928/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002929/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002930bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002931 return TokError("unsupported directive '.stabs'");
2932}
2933
Jim Grosbach4b905842013-09-20 23:08:21 +00002934/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002935/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002936bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002937 StringRef Name;
2938 bool EH = false;
2939 bool Debug = false;
2940
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002941 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002942 return TokError("Expected an identifier");
2943
2944 if (Name == ".eh_frame")
2945 EH = true;
2946 else if (Name == ".debug_frame")
2947 Debug = true;
2948
2949 if (getLexer().is(AsmToken::Comma)) {
2950 Lex();
2951
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002952 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002953 return TokError("Expected an identifier");
2954
2955 if (Name == ".eh_frame")
2956 EH = true;
2957 else if (Name == ".debug_frame")
2958 Debug = true;
2959 }
2960
2961 getStreamer().EmitCFISections(EH, Debug);
2962 return false;
2963}
2964
Jim Grosbach4b905842013-09-20 23:08:21 +00002965/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002966/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002967bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002968 StringRef Simple;
2969 if (getLexer().isNot(AsmToken::EndOfStatement))
2970 if (parseIdentifier(Simple) || Simple != "simple")
2971 return TokError("unexpected token in .cfi_startproc directive");
2972
2973 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002974 return false;
2975}
2976
Jim Grosbach4b905842013-09-20 23:08:21 +00002977/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002978/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002979bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002980 getStreamer().EmitCFIEndProc();
2981 return false;
2982}
2983
Jim Grosbach4b905842013-09-20 23:08:21 +00002984/// \brief parse register name or number.
2985bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002986 SMLoc DirectiveLoc) {
2987 unsigned RegNo;
2988
2989 if (getLexer().isNot(AsmToken::Integer)) {
2990 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2991 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002992 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002993 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002994 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002995
2996 return false;
2997}
2998
Jim Grosbach4b905842013-09-20 23:08:21 +00002999/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003000/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003001bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003002 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003003 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003004 return true;
3005
3006 if (getLexer().isNot(AsmToken::Comma))
3007 return TokError("unexpected token in directive");
3008 Lex();
3009
3010 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003011 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003012 return true;
3013
3014 getStreamer().EmitCFIDefCfa(Register, Offset);
3015 return false;
3016}
3017
Jim Grosbach4b905842013-09-20 23:08:21 +00003018/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003019/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003020bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003021 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003022 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003023 return true;
3024
3025 getStreamer().EmitCFIDefCfaOffset(Offset);
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003031bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003032 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003033 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003034 return true;
3035
3036 if (getLexer().isNot(AsmToken::Comma))
3037 return TokError("unexpected token in directive");
3038 Lex();
3039
3040 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003041 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 getStreamer().EmitCFIRegister(Register1, Register2);
3045 return false;
3046}
3047
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003048/// parseDirectiveCFIWindowSave
3049/// ::= .cfi_window_save
3050bool AsmParser::parseDirectiveCFIWindowSave() {
3051 getStreamer().EmitCFIWindowSave();
3052 return false;
3053}
3054
Jim Grosbach4b905842013-09-20 23:08:21 +00003055/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003056/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003057bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003058 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003059 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003060 return true;
3061
3062 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3063 return false;
3064}
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003067/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003068bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003069 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003070 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003071 return true;
3072
3073 getStreamer().EmitCFIDefCfaRegister(Register);
3074 return false;
3075}
3076
Jim Grosbach4b905842013-09-20 23:08:21 +00003077/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003078/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003079bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003080 int64_t Register = 0;
3081 int64_t Offset = 0;
3082
Jim Grosbach4b905842013-09-20 23:08:21 +00003083 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003084 return true;
3085
3086 if (getLexer().isNot(AsmToken::Comma))
3087 return TokError("unexpected token in directive");
3088 Lex();
3089
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003090 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return true;
3092
3093 getStreamer().EmitCFIOffset(Register, Offset);
3094 return false;
3095}
3096
Jim Grosbach4b905842013-09-20 23:08:21 +00003097/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003098/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003099bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003100 int64_t Register = 0;
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003103 return true;
3104
3105 if (getLexer().isNot(AsmToken::Comma))
3106 return TokError("unexpected token in directive");
3107 Lex();
3108
3109 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003110 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003111 return true;
3112
3113 getStreamer().EmitCFIRelOffset(Register, Offset);
3114 return false;
3115}
3116
3117static bool isValidEncoding(int64_t Encoding) {
3118 if (Encoding & ~0xff)
3119 return false;
3120
3121 if (Encoding == dwarf::DW_EH_PE_omit)
3122 return true;
3123
3124 const unsigned Format = Encoding & 0xf;
3125 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3126 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3127 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3128 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3129 return false;
3130
3131 const unsigned Application = Encoding & 0x70;
3132 if (Application != dwarf::DW_EH_PE_absptr &&
3133 Application != dwarf::DW_EH_PE_pcrel)
3134 return false;
3135
3136 return true;
3137}
3138
Jim Grosbach4b905842013-09-20 23:08:21 +00003139/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003140/// IsPersonality true for cfi_personality, false for cfi_lsda
3141/// ::= .cfi_personality encoding, [symbol_name]
3142/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003143bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003144 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003145 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003146 return true;
3147 if (Encoding == dwarf::DW_EH_PE_omit)
3148 return false;
3149
3150 if (!isValidEncoding(Encoding))
3151 return TokError("unsupported encoding.");
3152
3153 if (getLexer().isNot(AsmToken::Comma))
3154 return TokError("unexpected token in directive");
3155 Lex();
3156
3157 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003158 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return TokError("expected identifier in directive");
3160
3161 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3162
3163 if (IsPersonality)
3164 getStreamer().EmitCFIPersonality(Sym, Encoding);
3165 else
3166 getStreamer().EmitCFILsda(Sym, Encoding);
3167 return false;
3168}
3169
Jim Grosbach4b905842013-09-20 23:08:21 +00003170/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003171/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003172bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003173 getStreamer().EmitCFIRememberState();
3174 return false;
3175}
3176
Jim Grosbach4b905842013-09-20 23:08:21 +00003177/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003178/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003179bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003180 getStreamer().EmitCFIRestoreState();
3181 return false;
3182}
3183
Jim Grosbach4b905842013-09-20 23:08:21 +00003184/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003185/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003186bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003187 int64_t Register = 0;
3188
Jim Grosbach4b905842013-09-20 23:08:21 +00003189 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003190 return true;
3191
3192 getStreamer().EmitCFISameValue(Register);
3193 return false;
3194}
3195
Jim Grosbach4b905842013-09-20 23:08:21 +00003196/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003197/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003198bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003199 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003200 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003201 return true;
3202
3203 getStreamer().EmitCFIRestore(Register);
3204 return false;
3205}
3206
Jim Grosbach4b905842013-09-20 23:08:21 +00003207/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003208/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003209bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003210 std::string Values;
3211 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003212 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003213 return true;
3214
3215 Values.push_back((uint8_t)CurrValue);
3216
3217 while (getLexer().is(AsmToken::Comma)) {
3218 Lex();
3219
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003220 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003221 return true;
3222
3223 Values.push_back((uint8_t)CurrValue);
3224 }
3225
3226 getStreamer().EmitCFIEscape(Values);
3227 return false;
3228}
3229
Jim Grosbach4b905842013-09-20 23:08:21 +00003230/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003231/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003232bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003233 if (getLexer().isNot(AsmToken::EndOfStatement))
3234 return Error(getLexer().getLoc(),
3235 "unexpected token in '.cfi_signal_frame'");
3236
3237 getStreamer().EmitCFISignalFrame();
3238 return false;
3239}
3240
Jim Grosbach4b905842013-09-20 23:08:21 +00003241/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003242/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003243bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003244 int64_t Register = 0;
3245
Jim Grosbach4b905842013-09-20 23:08:21 +00003246 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003247 return true;
3248
3249 getStreamer().EmitCFIUndefined(Register);
3250 return false;
3251}
3252
Jim Grosbach4b905842013-09-20 23:08:21 +00003253/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003254/// ::= .macros_on
3255/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003256bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003257 if (getLexer().isNot(AsmToken::EndOfStatement))
3258 return Error(getLexer().getLoc(),
3259 "unexpected token in '" + Directive + "' directive");
3260
Jim Grosbach4b905842013-09-20 23:08:21 +00003261 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003262 return false;
3263}
3264
Jim Grosbach4b905842013-09-20 23:08:21 +00003265/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003266/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003267bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003268 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003269 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003270 return TokError("expected identifier in '.macro' directive");
3271
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003272 if (getLexer().is(AsmToken::Comma))
3273 Lex();
3274
Eli Bendersky17233942013-01-15 22:59:42 +00003275 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003276 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003277
3278 if (Parameters.size() && Parameters.back().Vararg)
3279 return Error(Lexer.getLoc(),
3280 "Vararg parameter '" + Parameters.back().Name +
3281 "' should be last one in the list of parameters.");
3282
David Majnemer91fc4c22014-01-29 18:57:46 +00003283 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003284 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003285 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003286
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003287 if (Lexer.is(AsmToken::Colon)) {
3288 Lex(); // consume ':'
3289
3290 SMLoc QualLoc;
3291 StringRef Qualifier;
3292
3293 QualLoc = Lexer.getLoc();
3294 if (parseIdentifier(Qualifier))
3295 return Error(QualLoc, "missing parameter qualifier for "
3296 "'" + Parameter.Name + "' in macro '" + Name + "'");
3297
3298 if (Qualifier == "req")
3299 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003300 else if (Qualifier == "vararg" && !IsDarwin)
3301 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003302 else
3303 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3304 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3305 }
3306
David Majnemer91fc4c22014-01-29 18:57:46 +00003307 if (getLexer().is(AsmToken::Equal)) {
3308 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003309
3310 SMLoc ParamLoc;
3311
3312 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003313 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003314 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003315
3316 if (Parameter.Required)
3317 Warning(ParamLoc, "pointless default value for required parameter "
3318 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003319 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003320
3321 Parameters.push_back(Parameter);
3322
3323 if (getLexer().is(AsmToken::Comma))
3324 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003325 }
3326
3327 // Eat the end of statement.
3328 Lex();
3329
3330 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003331 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003332
3333 // Lex the macro definition.
3334 for (;;) {
3335 // Check whether we have reached the end of the file.
3336 if (getLexer().is(AsmToken::Eof))
3337 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3338
3339 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003340 if (getLexer().is(AsmToken::Identifier)) {
3341 if (getTok().getIdentifier() == ".endm" ||
3342 getTok().getIdentifier() == ".endmacro") {
3343 if (MacroDepth == 0) { // Outermost macro.
3344 EndToken = getTok();
3345 Lex();
3346 if (getLexer().isNot(AsmToken::EndOfStatement))
3347 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3348 "' directive");
3349 break;
3350 } else {
3351 // Otherwise we just found the end of an inner macro.
3352 --MacroDepth;
3353 }
3354 } else if (getTok().getIdentifier() == ".macro") {
3355 // We allow nested macros. Those aren't instantiated until the outermost
3356 // macro is expanded so just ignore them for now.
3357 ++MacroDepth;
3358 }
Eli Bendersky17233942013-01-15 22:59:42 +00003359 }
3360
3361 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003362 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003363 }
3364
Jim Grosbach4b905842013-09-20 23:08:21 +00003365 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003366 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3367 }
3368
3369 const char *BodyStart = StartToken.getLoc().getPointer();
3370 const char *BodyEnd = EndToken.getLoc().getPointer();
3371 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003372 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3373 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003374 return false;
3375}
3376
Jim Grosbach4b905842013-09-20 23:08:21 +00003377/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378///
3379/// With the support added for named parameters there may be code out there that
3380/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003381/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003382/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003383/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3385/// warning that the positional parameter found in body which have no effect.
3386/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003387/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388/// intended or change the macro to use the named parameters. It is possible
3389/// this warning will trigger when the none of the named parameters are used
3390/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003391void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003392 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003393 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394 // If this macro is not defined with named parameters the warning we are
3395 // checking for here doesn't apply.
3396 unsigned NParameters = Parameters.size();
3397 if (NParameters == 0)
3398 return;
3399
3400 bool NamedParametersFound = false;
3401 bool PositionalParametersFound = false;
3402
3403 // Look at the body of the macro for use of both the named parameters and what
3404 // are likely to be positional parameters. This is what expandMacro() is
3405 // doing when it finds the parameters in the body.
3406 while (!Body.empty()) {
3407 // Scan for the next possible parameter.
3408 std::size_t End = Body.size(), Pos = 0;
3409 for (; Pos != End; ++Pos) {
3410 // Check for a substitution or escape.
3411 // This macro is defined with parameters, look for \foo, \bar, etc.
3412 if (Body[Pos] == '\\' && Pos + 1 != End)
3413 break;
3414
3415 // This macro should have parameters, but look for $0, $1, ..., $n too.
3416 if (Body[Pos] != '$' || Pos + 1 == End)
3417 continue;
3418 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003419 if (Next == '$' || Next == 'n' ||
3420 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003421 break;
3422 }
3423
3424 // Check if we reached the end.
3425 if (Pos == End)
3426 break;
3427
3428 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003429 switch (Body[Pos + 1]) {
3430 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003431 case '$':
3432 break;
3433
Jim Grosbach4b905842013-09-20 23:08:21 +00003434 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003435 case 'n':
3436 PositionalParametersFound = true;
3437 break;
3438
Jim Grosbach4b905842013-09-20 23:08:21 +00003439 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003440 default: {
3441 PositionalParametersFound = true;
3442 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003443 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003444 }
3445 Pos += 2;
3446 } else {
3447 unsigned I = Pos + 1;
3448 while (isIdentifierChar(Body[I]) && I + 1 != End)
3449 ++I;
3450
Jim Grosbach4b905842013-09-20 23:08:21 +00003451 const char *Begin = Body.data() + Pos + 1;
3452 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003453 unsigned Index = 0;
3454 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003455 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003456 break;
3457
3458 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003459 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3460 Pos += 3;
3461 else {
3462 Pos = I;
3463 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003464 } else {
3465 NamedParametersFound = true;
3466 Pos += 1 + Argument.size();
3467 }
3468 }
3469 // Update the scan point.
3470 Body = Body.substr(Pos);
3471 }
3472
3473 if (!NamedParametersFound && PositionalParametersFound)
3474 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3475 "used in macro body, possible positional parameter "
3476 "found in body which will have no effect");
3477}
3478
Jim Grosbach4b905842013-09-20 23:08:21 +00003479/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003480/// ::= .endm
3481/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003482bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003483 if (getLexer().isNot(AsmToken::EndOfStatement))
3484 return TokError("unexpected token in '" + Directive + "' directive");
3485
3486 // If we are inside a macro instantiation, terminate the current
3487 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003488 if (isInsideMacroInstantiation()) {
3489 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003490 return false;
3491 }
3492
3493 // Otherwise, this .endmacro is a stray entry in the file; well formed
3494 // .endmacro directives are handled during the macro definition parsing.
3495 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003496 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003500/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003502 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003504 return TokError("expected identifier in '.purgem' directive");
3505
3506 if (getLexer().isNot(AsmToken::EndOfStatement))
3507 return TokError("unexpected token in '.purgem' directive");
3508
Jim Grosbach4b905842013-09-20 23:08:21 +00003509 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003510 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3511
Jim Grosbach4b905842013-09-20 23:08:21 +00003512 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003513 return false;
3514}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003515
Jim Grosbach4b905842013-09-20 23:08:21 +00003516/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003517/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003518bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003519 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003520
3521 // Expect a single argument: an expression that evaluates to a constant
3522 // in the inclusive range 0-30.
3523 SMLoc ExprLoc = getLexer().getLoc();
3524 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003525 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003526 return true;
3527 else if (getLexer().isNot(AsmToken::EndOfStatement))
3528 return TokError("unexpected token after expression in"
3529 " '.bundle_align_mode' directive");
3530 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3531 return Error(ExprLoc,
3532 "invalid bundle alignment size (expected between 0 and 30)");
3533
3534 Lex();
3535
3536 // Because of AlignSizePow2's verified range we can safely truncate it to
3537 // unsigned.
3538 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3539 return false;
3540}
3541
Jim Grosbach4b905842013-09-20 23:08:21 +00003542/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003543/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003544bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003545 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003546 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003547
Eli Bendersky802b6282013-01-07 21:51:08 +00003548 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3549 StringRef Option;
3550 SMLoc Loc = getTok().getLoc();
3551 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003552 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003553
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003554 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003555 return Error(Loc, kInvalidOptionError);
3556
3557 if (Option != "align_to_end")
3558 return Error(Loc, kInvalidOptionError);
3559 else if (getLexer().isNot(AsmToken::EndOfStatement))
3560 return Error(Loc,
3561 "unexpected token after '.bundle_lock' directive option");
3562 AlignToEnd = true;
3563 }
3564
Eli Benderskyf483ff92012-12-20 19:05:53 +00003565 Lex();
3566
Eli Bendersky802b6282013-01-07 21:51:08 +00003567 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003568 return false;
3569}
3570
Jim Grosbach4b905842013-09-20 23:08:21 +00003571/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003572/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003573bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003574 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003575
3576 if (getLexer().isNot(AsmToken::EndOfStatement))
3577 return TokError("unexpected token in '.bundle_unlock' directive");
3578 Lex();
3579
3580 getStreamer().EmitBundleUnlock();
3581 return false;
3582}
3583
Jim Grosbach4b905842013-09-20 23:08:21 +00003584/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003585/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003586bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003587 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003588
3589 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003590 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003591 return true;
3592
3593 int64_t FillExpr = 0;
3594 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3595 if (getLexer().isNot(AsmToken::Comma))
3596 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3597 Lex();
3598
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003599 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003600 return true;
3601
3602 if (getLexer().isNot(AsmToken::EndOfStatement))
3603 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3604 }
3605
3606 Lex();
3607
3608 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003609 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3610 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003611
3612 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003613 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003614
3615 return false;
3616}
3617
Jim Grosbach4b905842013-09-20 23:08:21 +00003618/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003619/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003620bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003621 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003622 const MCExpr *Value;
3623
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003624 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003625 return true;
3626
3627 if (getLexer().isNot(AsmToken::EndOfStatement))
3628 return TokError("unexpected token in directive");
3629
3630 if (Signed)
3631 getStreamer().EmitSLEB128Value(Value);
3632 else
3633 getStreamer().EmitULEB128Value(Value);
3634
3635 return false;
3636}
3637
Jim Grosbach4b905842013-09-20 23:08:21 +00003638/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003639/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003640bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003641 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003642 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003643 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003644 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003645
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003646 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003647 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003648
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003649 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003650
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003651 // Assembler local symbols don't make any sense here. Complain loudly.
3652 if (Sym->isTemporary())
3653 return Error(Loc, "non-local symbol required in directive");
3654
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003655 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3656 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003657
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003658 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003659 break;
3660
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003661 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003662 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003663 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003664 }
3665 }
3666
Sean Callanan686ed8d2010-01-19 20:22:31 +00003667 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003668 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003669}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003670
Jim Grosbach4b905842013-09-20 23:08:21 +00003671/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003672/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003673bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003675
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003676 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003677 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003678 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003679 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003680
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003681 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003682 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003684 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003685 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003686 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003687
3688 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003689 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003690 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003691 return true;
3692
3693 int64_t Pow2Alignment = 0;
3694 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003695 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003696 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003697 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003698 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003699 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003700
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003701 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3702 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003703 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3704
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003705 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003706 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3707 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003708 if (!isPowerOf2_64(Pow2Alignment))
3709 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3710 Pow2Alignment = Log2_64(Pow2Alignment);
3711 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003712 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003713
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003714 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003715 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003716
Sean Callanan686ed8d2010-01-19 20:22:31 +00003717 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003718
Chris Lattner28ad7542009-07-09 17:25:12 +00003719 // NOTE: a size of zero for a .comm should create a undefined symbol
3720 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003721 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003722 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003723 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003724
Eric Christopherbc818852010-05-14 01:38:54 +00003725 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003726 // may internally end up wanting an alignment in bytes.
3727 // FIXME: Diagnose overflow.
3728 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003729 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003730 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003731
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003732 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003733 return Error(IDLoc, "invalid symbol redefinition");
3734
Chris Lattner28ad7542009-07-09 17:25:12 +00003735 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003736 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003737 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003738 return false;
3739 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003740
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003741 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742 return false;
3743}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003744
Jim Grosbach4b905842013-09-20 23:08:21 +00003745/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003746/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003747bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003748 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003749 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003750
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003751 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003752 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003753 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003754
Sean Callanan686ed8d2010-01-19 20:22:31 +00003755 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003756
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003757 if (Str.empty())
3758 Error(Loc, ".abort detected. Assembly stopping.");
3759 else
3760 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003761 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003762
3763 return false;
3764}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003765
Jim Grosbach4b905842013-09-20 23:08:21 +00003766/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003767/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003768bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003769 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003770 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003771
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003772 // Allow the strings to have escaped octal character sequence.
3773 std::string Filename;
3774 if (parseEscapedString(Filename))
3775 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003776 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003777 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003778
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003779 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003780 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003781
Chris Lattner693fbb82009-07-16 06:14:39 +00003782 // Attempt to switch the lexer to the included file before consuming the end
3783 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003784 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003785 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003786 return true;
3787 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003788
3789 return false;
3790}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003791
Jim Grosbach4b905842013-09-20 23:08:21 +00003792/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003793/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003794bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003795 if (getLexer().isNot(AsmToken::String))
3796 return TokError("expected string in '.incbin' directive");
3797
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003798 // Allow the strings to have escaped octal character sequence.
3799 std::string Filename;
3800 if (parseEscapedString(Filename))
3801 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003802 SMLoc IncbinLoc = getLexer().getLoc();
3803 Lex();
3804
3805 if (getLexer().isNot(AsmToken::EndOfStatement))
3806 return TokError("unexpected token in '.incbin' directive");
3807
Kevin Enderby109f25c2011-12-14 21:47:48 +00003808 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003809 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003810 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3811 return true;
3812 }
3813
3814 return false;
3815}
3816
Jim Grosbach4b905842013-09-20 23:08:21 +00003817/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003818/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3819bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003820 TheCondStack.push_back(TheCondState);
3821 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003822 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003823 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003824 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003825 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003826 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003827 return true;
3828
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003829 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003830 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003831
Sean Callanan686ed8d2010-01-19 20:22:31 +00003832 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003833
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003834 switch (DirKind) {
3835 default:
3836 llvm_unreachable("unsupported directive");
3837 case DK_IF:
3838 case DK_IFNE:
3839 break;
3840 case DK_IFEQ:
3841 ExprValue = ExprValue == 0;
3842 break;
3843 case DK_IFGE:
3844 ExprValue = ExprValue >= 0;
3845 break;
3846 case DK_IFGT:
3847 ExprValue = ExprValue > 0;
3848 break;
3849 case DK_IFLE:
3850 ExprValue = ExprValue <= 0;
3851 break;
3852 case DK_IFLT:
3853 ExprValue = ExprValue < 0;
3854 break;
3855 }
3856
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003857 TheCondState.CondMet = ExprValue;
3858 TheCondState.Ignore = !TheCondState.CondMet;
3859 }
3860
3861 return false;
3862}
3863
Jim Grosbach4b905842013-09-20 23:08:21 +00003864/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003865/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003866bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003867 TheCondStack.push_back(TheCondState);
3868 TheCondState.TheCond = AsmCond::IfCond;
3869
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003870 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003871 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003872 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003873 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003874
3875 if (getLexer().isNot(AsmToken::EndOfStatement))
3876 return TokError("unexpected token in '.ifb' directive");
3877
3878 Lex();
3879
3880 TheCondState.CondMet = ExpectBlank == Str.empty();
3881 TheCondState.Ignore = !TheCondState.CondMet;
3882 }
3883
3884 return false;
3885}
3886
Jim Grosbach4b905842013-09-20 23:08:21 +00003887/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003888/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003889/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003890bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003891 TheCondStack.push_back(TheCondState);
3892 TheCondState.TheCond = AsmCond::IfCond;
3893
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003894 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003895 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003896 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003897 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003898
3899 if (getLexer().isNot(AsmToken::Comma))
3900 return TokError("unexpected token in '.ifc' directive");
3901
3902 Lex();
3903
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003904 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003905
3906 if (getLexer().isNot(AsmToken::EndOfStatement))
3907 return TokError("unexpected token in '.ifc' directive");
3908
3909 Lex();
3910
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003911 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003912 TheCondState.Ignore = !TheCondState.CondMet;
3913 }
3914
3915 return false;
3916}
3917
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003918/// parseDirectiveIfeqs
3919/// ::= .ifeqs string1, string2
3920bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3921 if (Lexer.isNot(AsmToken::String)) {
3922 TokError("expected string parameter for '.ifeqs' directive");
3923 eatToEndOfStatement();
3924 return true;
3925 }
3926
3927 StringRef String1 = getTok().getStringContents();
3928 Lex();
3929
3930 if (Lexer.isNot(AsmToken::Comma)) {
3931 TokError("expected comma after first string for '.ifeqs' directive");
3932 eatToEndOfStatement();
3933 return true;
3934 }
3935
3936 Lex();
3937
3938 if (Lexer.isNot(AsmToken::String)) {
3939 TokError("expected string parameter for '.ifeqs' directive");
3940 eatToEndOfStatement();
3941 return true;
3942 }
3943
3944 StringRef String2 = getTok().getStringContents();
3945 Lex();
3946
3947 TheCondStack.push_back(TheCondState);
3948 TheCondState.TheCond = AsmCond::IfCond;
3949 TheCondState.CondMet = String1 == String2;
3950 TheCondState.Ignore = !TheCondState.CondMet;
3951
3952 return false;
3953}
3954
Jim Grosbach4b905842013-09-20 23:08:21 +00003955/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003956/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003957bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003958 StringRef Name;
3959 TheCondStack.push_back(TheCondState);
3960 TheCondState.TheCond = AsmCond::IfCond;
3961
3962 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003963 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003964 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003965 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003966 return TokError("expected identifier after '.ifdef'");
3967
3968 Lex();
3969
3970 MCSymbol *Sym = getContext().LookupSymbol(Name);
3971
3972 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003973 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003974 else
Craig Topper353eda42014-04-24 06:44:33 +00003975 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003976 TheCondState.Ignore = !TheCondState.CondMet;
3977 }
3978
3979 return false;
3980}
3981
Jim Grosbach4b905842013-09-20 23:08:21 +00003982/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003983/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003984bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 if (TheCondState.TheCond != AsmCond::IfCond &&
3986 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003987 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3988 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003989 TheCondState.TheCond = AsmCond::ElseIfCond;
3990
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003991 bool LastIgnoreState = false;
3992 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003993 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003994 if (LastIgnoreState || TheCondState.CondMet) {
3995 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003996 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003997 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003998 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003999 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004000 return true;
4001
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004002 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004003 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004004
Sean Callanan686ed8d2010-01-19 20:22:31 +00004005 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004006 TheCondState.CondMet = ExprValue;
4007 TheCondState.Ignore = !TheCondState.CondMet;
4008 }
4009
4010 return false;
4011}
4012
Jim Grosbach4b905842013-09-20 23:08:21 +00004013/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004015bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004016 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004018
Sean Callanan686ed8d2010-01-19 20:22:31 +00004019 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020
4021 if (TheCondState.TheCond != AsmCond::IfCond &&
4022 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004023 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4024 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004025 TheCondState.TheCond = AsmCond::ElseCond;
4026 bool LastIgnoreState = false;
4027 if (!TheCondStack.empty())
4028 LastIgnoreState = TheCondStack.back().Ignore;
4029 if (LastIgnoreState || TheCondState.CondMet)
4030 TheCondState.Ignore = true;
4031 else
4032 TheCondState.Ignore = false;
4033
4034 return false;
4035}
4036
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004037/// parseDirectiveEnd
4038/// ::= .end
4039bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4040 if (getLexer().isNot(AsmToken::EndOfStatement))
4041 return TokError("unexpected token in '.end' directive");
4042
4043 Lex();
4044
4045 while (Lexer.isNot(AsmToken::Eof))
4046 Lex();
4047
4048 return false;
4049}
4050
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004051/// parseDirectiveError
4052/// ::= .err
4053/// ::= .error [string]
4054bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4055 if (!TheCondStack.empty()) {
4056 if (TheCondStack.back().Ignore) {
4057 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004058 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004059 }
4060 }
4061
4062 if (!WithMessage)
4063 return Error(L, ".err encountered");
4064
4065 StringRef Message = ".error directive invoked in source file";
4066 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4067 if (Lexer.isNot(AsmToken::String)) {
4068 TokError(".error argument must be a string");
4069 eatToEndOfStatement();
4070 return true;
4071 }
4072
4073 Message = getTok().getStringContents();
4074 Lex();
4075 }
4076
4077 Error(L, Message);
4078 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004079}
4080
Nico Weber404012b2014-07-24 16:26:06 +00004081/// parseDirectiveWarning
4082/// ::= .warning [string]
4083bool AsmParser::parseDirectiveWarning(SMLoc L) {
4084 if (!TheCondStack.empty()) {
4085 if (TheCondStack.back().Ignore) {
4086 eatToEndOfStatement();
4087 return false;
4088 }
4089 }
4090
4091 StringRef Message = ".warning directive invoked in source file";
4092 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4093 if (Lexer.isNot(AsmToken::String)) {
4094 TokError(".warning argument must be a string");
4095 eatToEndOfStatement();
4096 return true;
4097 }
4098
4099 Message = getTok().getStringContents();
4100 Lex();
4101 }
4102
4103 Warning(L, Message);
4104 return false;
4105}
4106
Jim Grosbach4b905842013-09-20 23:08:21 +00004107/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004108/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004109bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004110 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004111 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004112
Sean Callanan686ed8d2010-01-19 20:22:31 +00004113 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004114
Jim Grosbach4b905842013-09-20 23:08:21 +00004115 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004116 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4117 ".else");
4118 if (!TheCondStack.empty()) {
4119 TheCondState = TheCondStack.back();
4120 TheCondStack.pop_back();
4121 }
4122
4123 return false;
4124}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004125
Eli Bendersky17233942013-01-15 22:59:42 +00004126void AsmParser::initializeDirectiveKindMap() {
4127 DirectiveKindMap[".set"] = DK_SET;
4128 DirectiveKindMap[".equ"] = DK_EQU;
4129 DirectiveKindMap[".equiv"] = DK_EQUIV;
4130 DirectiveKindMap[".ascii"] = DK_ASCII;
4131 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4132 DirectiveKindMap[".string"] = DK_STRING;
4133 DirectiveKindMap[".byte"] = DK_BYTE;
4134 DirectiveKindMap[".short"] = DK_SHORT;
4135 DirectiveKindMap[".value"] = DK_VALUE;
4136 DirectiveKindMap[".2byte"] = DK_2BYTE;
4137 DirectiveKindMap[".long"] = DK_LONG;
4138 DirectiveKindMap[".int"] = DK_INT;
4139 DirectiveKindMap[".4byte"] = DK_4BYTE;
4140 DirectiveKindMap[".quad"] = DK_QUAD;
4141 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004142 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004143 DirectiveKindMap[".single"] = DK_SINGLE;
4144 DirectiveKindMap[".float"] = DK_FLOAT;
4145 DirectiveKindMap[".double"] = DK_DOUBLE;
4146 DirectiveKindMap[".align"] = DK_ALIGN;
4147 DirectiveKindMap[".align32"] = DK_ALIGN32;
4148 DirectiveKindMap[".balign"] = DK_BALIGN;
4149 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4150 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4151 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4152 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4153 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4154 DirectiveKindMap[".org"] = DK_ORG;
4155 DirectiveKindMap[".fill"] = DK_FILL;
4156 DirectiveKindMap[".zero"] = DK_ZERO;
4157 DirectiveKindMap[".extern"] = DK_EXTERN;
4158 DirectiveKindMap[".globl"] = DK_GLOBL;
4159 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004160 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4161 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4162 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4163 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4164 DirectiveKindMap[".reference"] = DK_REFERENCE;
4165 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4166 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4167 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4168 DirectiveKindMap[".comm"] = DK_COMM;
4169 DirectiveKindMap[".common"] = DK_COMMON;
4170 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4171 DirectiveKindMap[".abort"] = DK_ABORT;
4172 DirectiveKindMap[".include"] = DK_INCLUDE;
4173 DirectiveKindMap[".incbin"] = DK_INCBIN;
4174 DirectiveKindMap[".code16"] = DK_CODE16;
4175 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4176 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004177 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004178 DirectiveKindMap[".irp"] = DK_IRP;
4179 DirectiveKindMap[".irpc"] = DK_IRPC;
4180 DirectiveKindMap[".endr"] = DK_ENDR;
4181 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4182 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4183 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4184 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004185 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4186 DirectiveKindMap[".ifge"] = DK_IFGE;
4187 DirectiveKindMap[".ifgt"] = DK_IFGT;
4188 DirectiveKindMap[".ifle"] = DK_IFLE;
4189 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004190 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004191 DirectiveKindMap[".ifb"] = DK_IFB;
4192 DirectiveKindMap[".ifnb"] = DK_IFNB;
4193 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004194 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004195 DirectiveKindMap[".ifnc"] = DK_IFNC;
4196 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4197 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4198 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4199 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4200 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004201 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004202 DirectiveKindMap[".endif"] = DK_ENDIF;
4203 DirectiveKindMap[".skip"] = DK_SKIP;
4204 DirectiveKindMap[".space"] = DK_SPACE;
4205 DirectiveKindMap[".file"] = DK_FILE;
4206 DirectiveKindMap[".line"] = DK_LINE;
4207 DirectiveKindMap[".loc"] = DK_LOC;
4208 DirectiveKindMap[".stabs"] = DK_STABS;
4209 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4210 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4211 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4212 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4213 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4214 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4215 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4216 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4217 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4218 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4219 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4220 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4221 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4222 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4223 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4224 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4225 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4226 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4227 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4228 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4229 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004230 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004231 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4232 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4233 DirectiveKindMap[".macro"] = DK_MACRO;
4234 DirectiveKindMap[".endm"] = DK_ENDM;
4235 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4236 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004237 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004238 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004239 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004240}
4241
Jim Grosbach4b905842013-09-20 23:08:21 +00004242MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004243 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004244
Rafael Espindola34b9c512012-06-03 23:57:14 +00004245 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004246 for (;;) {
4247 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004248 if (getLexer().is(AsmToken::Eof)) {
4249 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004250 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004251 }
4252
Rafael Espindola34b9c512012-06-03 23:57:14 +00004253 if (Lexer.is(AsmToken::Identifier) &&
4254 (getTok().getIdentifier() == ".rept")) {
4255 ++NestLevel;
4256 }
4257
4258 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004259 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004260 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004261 EndToken = getTok();
4262 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004263 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4264 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004265 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004267 break;
4268 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004269 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004270 }
4271
Rafael Espindola34b9c512012-06-03 23:57:14 +00004272 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004273 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004274 }
4275
4276 const char *BodyStart = StartToken.getLoc().getPointer();
4277 const char *BodyEnd = EndToken.getLoc().getPointer();
4278 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4279
Rafael Espindola34b9c512012-06-03 23:57:14 +00004280 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004281 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004282 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004283}
4284
Jim Grosbach4b905842013-09-20 23:08:21 +00004285void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004286 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004287 OS << ".endr\n";
4288
4289 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004290 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004291
Rafael Espindola34b9c512012-06-03 23:57:14 +00004292 // Create the macro instantiation object and add to the current macro
4293 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004294 MacroInstantiation *MI = new MacroInstantiation(
4295 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004296 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004297
Rafael Espindola34b9c512012-06-03 23:57:14 +00004298 // Jump to the macro instantiation and prime the lexer.
4299 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004300 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004301 Lex();
4302}
4303
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004304/// parseDirectiveRept
4305/// ::= .rep | .rept count
4306bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004307 const MCExpr *CountExpr;
4308 SMLoc CountLoc = getTok().getLoc();
4309 if (parseExpression(CountExpr))
4310 return true;
4311
Rafael Espindola34b9c512012-06-03 23:57:14 +00004312 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004313 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4314 eatToEndOfStatement();
4315 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4316 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004317
4318 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004319 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004320
4321 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004322 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323
4324 // Eat the end of statement.
4325 Lex();
4326
4327 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004328 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004329 if (!M)
4330 return true;
4331
4332 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4333 // to hold the macro body with substitutions.
4334 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004335 raw_svector_ostream OS(Buf);
4336 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004337 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004338 return true;
4339 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004340 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004341
4342 return false;
4343}
4344
Jim Grosbach4b905842013-09-20 23:08:21 +00004345/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004346/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004347bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004348 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004349
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004350 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004351 return TokError("expected identifier in '.irp' directive");
4352
Rafael Espindola768b41c2012-06-15 14:02:34 +00004353 if (Lexer.isNot(AsmToken::Comma))
4354 return TokError("expected comma in '.irp' directive");
4355
4356 Lex();
4357
Eli Bendersky38274122013-01-14 23:22:36 +00004358 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004359 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004360 return true;
4361
4362 // Eat the end of statement.
4363 Lex();
4364
4365 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004366 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004367 if (!M)
4368 return true;
4369
4370 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4371 // to hold the macro body with substitutions.
4372 SmallString<256> Buf;
4373 raw_svector_ostream OS(Buf);
4374
Eli Bendersky38274122013-01-14 23:22:36 +00004375 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004376 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004377 return true;
4378 }
4379
Jim Grosbach4b905842013-09-20 23:08:21 +00004380 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004381
4382 return false;
4383}
4384
Jim Grosbach4b905842013-09-20 23:08:21 +00004385/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004386/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004387bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004388 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004389
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004390 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004391 return TokError("expected identifier in '.irpc' directive");
4392
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004393 if (Lexer.isNot(AsmToken::Comma))
4394 return TokError("expected comma in '.irpc' 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 Espindolaf70bea92012-06-16 18:03:25 +00004400 return true;
4401
4402 if (A.size() != 1 || A.front().size() != 1)
4403 return TokError("unexpected token in '.irpc' directive");
4404
4405 // Eat the end of statement.
4406 Lex();
4407
4408 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004409 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004410 if (!M)
4411 return true;
4412
4413 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4414 // to hold the macro body with substitutions.
4415 SmallString<256> Buf;
4416 raw_svector_ostream OS(Buf);
4417
4418 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004419 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004420 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004421 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004422
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004423 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004424 return true;
4425 }
4426
Jim Grosbach4b905842013-09-20 23:08:21 +00004427 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004428
4429 return false;
4430}
4431
Jim Grosbach4b905842013-09-20 23:08:21 +00004432bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004433 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004434 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004435
4436 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004437 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004438 assert(getLexer().is(AsmToken::EndOfStatement));
4439
Jim Grosbach4b905842013-09-20 23:08:21 +00004440 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004441 return false;
4442}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004443
Jim Grosbach4b905842013-09-20 23:08:21 +00004444bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004445 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004446 const MCExpr *Value;
4447 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004448 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004449 return true;
4450 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4451 if (!MCE)
4452 return Error(ExprLoc, "unexpected expression in _emit");
4453 uint64_t IntValue = MCE->getValue();
4454 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4455 return Error(ExprLoc, "literal value out of range for directive");
4456
Chad Rosierc7f552c2013-02-12 21:33:51 +00004457 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4458 return false;
4459}
4460
Jim Grosbach4b905842013-09-20 23:08:21 +00004461bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004462 const MCExpr *Value;
4463 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004464 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004465 return true;
4466 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4467 if (!MCE)
4468 return Error(ExprLoc, "unexpected expression in align");
4469 uint64_t IntValue = MCE->getValue();
4470 if (!isPowerOf2_64(IntValue))
4471 return Error(ExprLoc, "literal value not a power of two greater then zero");
4472
Jim Grosbach4b905842013-09-20 23:08:21 +00004473 Info.AsmRewrites->push_back(
4474 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004475 return false;
4476}
4477
Chad Rosierf43fcf52013-02-13 21:27:17 +00004478// We are comparing pointers, but the pointers are relative to a single string.
4479// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004480static int rewritesSort(const AsmRewrite *AsmRewriteA,
4481 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004482 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4483 return -1;
4484 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4485 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004486
Chad Rosierfce4fab2013-04-08 17:43:47 +00004487 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4488 // rewrite to the same location. Make sure the SizeDirective rewrite is
4489 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4490 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004491 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4492 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004493 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004494
Jim Grosbach4b905842013-09-20 23:08:21 +00004495 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4496 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004497 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004498 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004499}
4500
Jim Grosbach4b905842013-09-20 23:08:21 +00004501bool AsmParser::parseMSInlineAsm(
4502 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4503 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4504 SmallVectorImpl<std::string> &Constraints,
4505 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4506 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004507 SmallVector<void *, 4> InputDecls;
4508 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004509 SmallVector<bool, 4> InputDeclsAddressOf;
4510 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004511 SmallVector<std::string, 4> InputConstraints;
4512 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004513 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004514
Benjamin Kramer1a136112013-02-15 20:37:21 +00004515 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004516
4517 // Prime the lexer.
4518 Lex();
4519
4520 // While we have input, parse each statement.
4521 unsigned InputIdx = 0;
4522 unsigned OutputIdx = 0;
4523 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004524 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004525 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004526 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004527
Chad Rosier149e8e02012-12-12 22:45:52 +00004528 if (Info.ParseError)
4529 return true;
4530
Benjamin Kramer1a136112013-02-15 20:37:21 +00004531 if (Info.Opcode == ~0U)
4532 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004533
Benjamin Kramer1a136112013-02-15 20:37:21 +00004534 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004535
Benjamin Kramer1a136112013-02-15 20:37:21 +00004536 // Build the list of clobbers, outputs and inputs.
4537 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004538 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004539
Benjamin Kramer1a136112013-02-15 20:37:21 +00004540 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004541 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004542 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004543
Benjamin Kramer1a136112013-02-15 20:37:21 +00004544 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004545 if (Operand.isReg() && !Operand.needAddressOf() &&
4546 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004547 unsigned NumDefs = Desc.getNumDefs();
4548 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004549 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4550 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004551 continue;
4552 }
4553
4554 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004555 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004556 if (SymName.empty())
4557 continue;
4558
David Blaikie960ea3f2014-06-08 16:18:35 +00004559 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004560 if (!OpDecl)
4561 continue;
4562
4563 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004564 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004565 if (isOutput) {
4566 ++InputIdx;
4567 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004568 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4569 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004570 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004571 } else {
4572 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004573 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4574 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004575 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004576 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004577 }
Reid Kleckneree088972013-12-10 18:27:32 +00004578
4579 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004580 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4581 Desc.getNumImplicitDefs());
4582 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004583 }
4584
4585 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004586 NumOutputs = OutputDecls.size();
4587 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004588
4589 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004590 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4591 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4592 ClobberRegs.end());
4593 Clobbers.assign(ClobberRegs.size(), std::string());
4594 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4595 raw_string_ostream OS(Clobbers[I]);
4596 IP->printRegName(OS, ClobberRegs[I]);
4597 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004598
4599 // Merge the various outputs and inputs. Output are expected first.
4600 if (NumOutputs || NumInputs) {
4601 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004602 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004603 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004604 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004605 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004606 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004607 }
4608 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004609 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004610 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004611 }
4612 }
4613
4614 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004615 std::string AsmStringIR;
4616 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004617 StringRef ASMString =
4618 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4619 const char *AsmStart = ASMString.begin();
4620 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004621 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004622 for (const AsmRewrite &AR : AsmStrRewrites) {
4623 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004624 if (Kind == AOK_Delete)
4625 continue;
4626
David Majnemer8114c1a2014-06-23 02:17:16 +00004627 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004628 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004629
Chad Rosier120eefd2013-03-19 17:32:17 +00004630 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004631 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004632 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004633
Chad Rosier37e755c2012-10-23 17:43:43 +00004634 // Skip the original expression.
4635 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004636 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004637 continue;
4638 }
4639
Chad Rosierff10ed12013-04-12 16:26:42 +00004640 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004641 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004642 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004643 default:
4644 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004645 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004646 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004647 break;
4648 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004649 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004650 break;
4651 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004652 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004653 break;
4654 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004655 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004656 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004657 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004658 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004659 default: break;
4660 case 8: OS << "byte ptr "; break;
4661 case 16: OS << "word ptr "; break;
4662 case 32: OS << "dword ptr "; break;
4663 case 64: OS << "qword ptr "; break;
4664 case 80: OS << "xword ptr "; break;
4665 case 128: OS << "xmmword ptr "; break;
4666 case 256: OS << "ymmword ptr "; break;
4667 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004668 break;
4669 case AOK_Emit:
4670 OS << ".byte";
4671 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004672 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004673 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004674 OS << ".align " << Val;
4675
4676 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004677 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004678 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4679 break;
4680 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004681 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004682 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004683 OS.flush();
4684 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004685 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004686 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004687 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004688 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004689
Chad Rosier8bce6642012-10-18 15:49:34 +00004690 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004691 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004692 }
4693
4694 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004695 if (AsmStart != AsmEnd)
4696 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004697
4698 AsmString = OS.str();
4699 return false;
4700}
4701
Daniel Dunbar01e36072010-07-17 02:26:10 +00004702/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004703MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4704 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004705 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004706}