blob: ad67f51b7073adbe7b74d55f0f865741a0135984 [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,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000360 DK_ERR, DK_ERROR,
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
Eli Bendersky17233942013-01-15 22:59:42 +0000477 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000478};
Daniel Dunbar86033402010-07-12 17:54:38 +0000479}
480
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000481namespace llvm {
482
483extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000484extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000485extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486
487}
488
Chris Lattnerc35681b2010-01-19 19:46:13 +0000489enum { DEFAULT_ADDRSPACE = 0 };
490
Jim Grosbach4b905842013-09-20 23:08:21 +0000491AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
492 const MCAsmInfo &_MAI)
493 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000494 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
495 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
496 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000497 // Save the old handler.
498 SavedDiagHandler = SrcMgr.getDiagHandler();
499 SavedDiagContext = SrcMgr.getDiagContext();
500 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000501 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000503
Daniel Dunbarc5011082010-07-12 18:12:02 +0000504 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000505 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
506 case MCObjectFileInfo::IsCOFF:
507 PlatformParser = createCOFFAsmParser();
508 PlatformParser->Initialize(*this);
509 break;
510 case MCObjectFileInfo::IsMachO:
511 PlatformParser = createDarwinAsmParser();
512 PlatformParser->Initialize(*this);
513 IsDarwin = true;
514 break;
515 case MCObjectFileInfo::IsELF:
516 PlatformParser = createELFAsmParser();
517 PlatformParser->Initialize(*this);
518 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000519 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000520
Eli Bendersky17233942013-01-15 22:59:42 +0000521 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000522}
523
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000527
528 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
530 ie = MacroMap.end();
531 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000532 delete it->getValue();
533
Daniel Dunbarc5011082010-07-12 18:12:02 +0000534 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000553 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000569 unsigned NewBuf =
570 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
571 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000573
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 CurBuffer = NewBuf;
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 return false;
577}
Daniel Dunbar43235712010-07-18 18:54:11 +0000578
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000579/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000580/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000582bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000584 unsigned NewBuf =
585 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Alp Tokera55b95b2014-07-06 10:33:31 +0000594void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
595 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Daniel Dunbar43235712010-07-18 18:54:11 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
597}
598
Sean Callanan7a77eae2010-01-21 00:19:58 +0000599const AsmToken &AsmParser::Lex() {
600 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000601
Sean Callanan7a77eae2010-01-21 00:19:58 +0000602 if (tok->is(AsmToken::Eof)) {
603 // If this is the end of an included file, pop the parent file off the
604 // include stack.
605 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
606 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000607 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 tok = &Lexer.Lex();
609 }
610 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000611
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000613 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000614
Sean Callanan7a77eae2010-01-21 00:19:58 +0000615 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000616}
617
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000618bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000619 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000620 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000621 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000622
Chris Lattner36e02122009-06-21 20:54:55 +0000623 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000624 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000625
626 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000627 AsmCond StartingCondState = TheCondState;
628
Kevin Enderby6469fc22011-11-01 22:27:22 +0000629 // If we are generating dwarf for assembly source files save the initial text
630 // section and generate a .file directive.
631 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000632 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
633 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000634 auto InsertResult = getContext().addGenDwarfSection(
635 getStreamer().getCurrentSection().first);
636 assert(InsertResult.second && ".text section should not have debug info yet");
637 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000638 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
639 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000640 }
641
Chris Lattner73f36112009-07-02 21:53:43 +0000642 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000643 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000644 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000645 if (!parseStatement(Info))
646 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000647
Daniel Dunbar43325c42010-09-09 22:42:56 +0000648 // We had an error, validate that one was emitted and recover by skipping to
649 // the next line.
650 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000651 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000652 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000653
654 if (TheCondState.TheCond != StartingCondState.TheCond ||
655 TheCondState.Ignore != StartingCondState.Ignore)
656 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000657
658 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000659 const auto &LineTables = getContext().getMCDwarfLineTables();
660 if (!LineTables.empty()) {
661 unsigned Index = 0;
662 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
663 if (File.Name.empty() && Index != 0)
664 TokError("unassigned file number: " + Twine(Index) +
665 " for .file directives");
666 ++Index;
667 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000668 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000669
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000670 // Check to see that all assembler local symbols were actually defined.
671 // Targets that don't do subsections via symbols may not want this, though,
672 // so conservatively exclude them. Only do this if we're finalizing, though,
673 // as otherwise we won't necessarilly have seen everything yet.
674 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
675 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
676 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000677 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000678 i != e; ++i) {
679 MCSymbol *Sym = i->getValue();
680 // Variable symbols may not be marked as defined, so check those
681 // explicitly. If we know it's a variable, we have a definition for
682 // the purposes of this check.
683 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
684 // FIXME: We would really like to refer back to where the symbol was
685 // first referenced for a source location. We need to add something
686 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000687 printMessage(
688 getLexer().getLoc(), SourceMgr::DK_Error,
689 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000690 }
691 }
692
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000693 // Finalize the output stream if there are no errors and if the client wants
694 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000695 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000696 Out.Finish();
697
Chris Lattner73f36112009-07-02 21:53:43 +0000698 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000699}
700
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000701void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000702 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000703 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000704 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000705 }
706}
707
Jim Grosbach4b905842013-09-20 23:08:21 +0000708/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000709void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000710 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000711 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000712
Chris Lattnere5074c42009-06-22 01:29:09 +0000713 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000714 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000715 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000716}
717
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000718StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000719 const char *Start = getTok().getLoc().getPointer();
720
Jim Grosbach4b905842013-09-20 23:08:21 +0000721 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000722 Lex();
723
724 const char *End = getTok().getLoc().getPointer();
725 return StringRef(Start, End - Start);
726}
Chris Lattner78db3622009-06-22 05:51:26 +0000727
Jim Grosbach4b905842013-09-20 23:08:21 +0000728StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000729 const char *Start = getTok().getLoc().getPointer();
730
731 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000732 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000733 Lex();
734
735 const char *End = getTok().getLoc().getPointer();
736 return StringRef(Start, End - Start);
737}
738
Jim Grosbach4b905842013-09-20 23:08:21 +0000739/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000740/// NOTE: This assumes the leading '(' has already been consumed.
741///
742/// parenexpr ::= expr)
743///
Jim Grosbach4b905842013-09-20 23:08:21 +0000744bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
745 if (parseExpression(Res))
746 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000747 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000748 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000749 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000750 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000751 return false;
752}
Chris Lattner78db3622009-06-22 05:51:26 +0000753
Jim Grosbach4b905842013-09-20 23:08:21 +0000754/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000755/// NOTE: This assumes the leading '[' has already been consumed.
756///
757/// bracketexpr ::= expr]
758///
Jim Grosbach4b905842013-09-20 23:08:21 +0000759bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
760 if (parseExpression(Res))
761 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000762 if (Lexer.isNot(AsmToken::RBrac))
763 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000764 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000765 Lex();
766 return false;
767}
768
Jim Grosbach4b905842013-09-20 23:08:21 +0000769/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000770/// primaryexpr ::= (parenexpr
771/// primaryexpr ::= symbol
772/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000773/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000774/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000775bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000776 SMLoc FirstTokenLoc = getLexer().getLoc();
777 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
778 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000779 default:
780 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000781 // If we have an error assume that we've already handled it.
782 case AsmToken::Error:
783 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000784 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000785 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000786 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000787 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000788 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000789 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000790 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000791 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000792 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000793 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000794 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000795 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000796 if (FirstTokenKind == AsmToken::Dollar) {
797 if (Lexer.getMAI().getDollarIsPC()) {
798 // This is a '$' reference, which references the current PC. Emit a
799 // temporary label to the streamer and refer to it.
800 MCSymbol *Sym = Ctx.CreateTempSymbol();
801 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000802 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
803 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000804 EndLoc = FirstTokenLoc;
805 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000806 }
807 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000808 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000809 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000810 // Parse symbol variant
811 std::pair<StringRef, StringRef> Split;
812 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000813 if (FirstTokenKind == AsmToken::String) {
814 if (Lexer.is(AsmToken::At)) {
815 Lexer.Lex(); // eat @
816 SMLoc AtLoc = getLexer().getLoc();
817 StringRef VName;
818 if (parseIdentifier(VName))
819 return Error(AtLoc, "expected symbol variant after '@'");
820
821 Split = std::make_pair(Identifier, VName);
822 }
823 } else {
824 Split = Identifier.split('@');
825 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000826 } else if (Lexer.is(AsmToken::LParen)) {
827 Lexer.Lex(); // eat (
828 StringRef VName;
829 parseIdentifier(VName);
830 if (Lexer.isNot(AsmToken::RParen)) {
831 return Error(Lexer.getTok().getLoc(),
832 "unexpected token in variant, expected ')'");
833 }
834 Lexer.Lex(); // eat )
835 Split = std::make_pair(Identifier, VName);
836 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000837
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000838 EndLoc = SMLoc::getFromPointer(Identifier.end());
839
Daniel Dunbard20cda02009-10-16 01:34:54 +0000840 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000841 StringRef SymbolName = Identifier;
842 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000843
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000844 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000845 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000846 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000847 if (Variant != MCSymbolRefExpr::VK_Invalid) {
848 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000849 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000850 Variant = MCSymbolRefExpr::VK_None;
851 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000852 return Error(SMLoc::getFromPointer(Split.second.begin()),
853 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000854 }
855 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000856
Hans Wennborgce69d772013-10-18 20:46:28 +0000857 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
858
Daniel Dunbard20cda02009-10-16 01:34:54 +0000859 // If this is an absolute variable reference, substitute it now to preserve
860 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000861 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000862 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000863 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000864
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000865 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000866 return false;
867 }
868
869 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000870 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000871 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000872 }
David Woodhousef42a6662014-02-01 16:20:54 +0000873 case AsmToken::BigNum:
874 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000875 case AsmToken::Integer: {
876 SMLoc Loc = getTok().getLoc();
877 int64_t IntVal = getTok().getIntVal();
878 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000879 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000880 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000881 // Look for 'b' or 'f' following an Integer as a directional label
882 if (Lexer.getKind() == AsmToken::Identifier) {
883 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000884 // Lookup the symbol variant if used.
885 std::pair<StringRef, StringRef> Split = IDVal.split('@');
886 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
887 if (Split.first.size() != IDVal.size()) {
888 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000889 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000891 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000892 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000893 if (IDVal == "f" || IDVal == "b") {
894 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000895 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000897 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000898 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000900 Lex(); // Eat identifier.
901 }
902 }
Chris Lattner78db3622009-06-22 05:51:26 +0000903 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000905 case AsmToken::Real: {
906 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000907 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000908 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000909 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000910 Lex(); // Eat token.
911 return false;
912 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000913 case AsmToken::Dot: {
914 // This is a '.' reference, which references the current PC. Emit a
915 // temporary label to the streamer and refer to it.
916 MCSymbol *Sym = Ctx.CreateTempSymbol();
917 Out.EmitLabel(Sym);
918 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000919 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000920 Lex(); // Eat identifier.
921 return false;
922 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000926 case AsmToken::LBrac:
927 if (!PlatformParser->HasBracketExpressions())
928 return TokError("brackets expression not supported on this target");
929 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000930 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000931 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000932 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000933 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000935 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000937 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000938 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000939 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000941 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000943 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000944 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000945 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000947 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000948 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000949 }
950}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000951
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000952bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000953 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000954 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000955}
956
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000958AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000960 // Ask the target implementation about this expression first.
961 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
962 if (NewE)
963 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000964 // Recurse over the given expression, rebuilding it to apply the given variant
965 // if there is exactly one symbol.
966 switch (E->getKind()) {
967 case MCExpr::Target:
968 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000969 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970
971 case MCExpr::SymbolRef: {
972 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
973
974 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000975 TokError("invalid variant on expression '" + getTok().getIdentifier() +
976 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000977 return E;
978 }
979
980 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
981 }
982
983 case MCExpr::Unary: {
984 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000985 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000987 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
989 }
990
991 case MCExpr::Binary: {
992 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000993 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
994 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995
996 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000997 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998
Jim Grosbach4b905842013-09-20 23:08:21 +0000999 if (!LHS)
1000 LHS = BE->getLHS();
1001 if (!RHS)
1002 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001003
1004 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1005 }
1006 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001007
Craig Toppera2886c22012-02-07 05:05:23 +00001008 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001009}
1010
Jim Grosbach4b905842013-09-20 23:08:21 +00001011/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001012///
Jim Grosbachbd164242011-08-20 16:24:13 +00001013/// expr ::= expr &&,|| expr -> lowest.
1014/// expr ::= expr |,^,&,! expr
1015/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1016/// expr ::= expr <<,>> expr
1017/// expr ::= expr +,- expr
1018/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001019/// expr ::= primaryexpr
1020///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001021bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001022 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001023 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001024 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001025 return true;
1026
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 // As a special case, we support 'a op b @ modifier' by rewriting the
1028 // expression to include the modifier. This is inefficient, but in general we
1029 // expect users to use 'a@modifier op b'.
1030 if (Lexer.getKind() == AsmToken::At) {
1031 Lex();
1032
1033 if (Lexer.isNot(AsmToken::Identifier))
1034 return TokError("unexpected symbol modifier following '@'");
1035
1036 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 if (Variant == MCSymbolRefExpr::VK_Invalid)
1039 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1040
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042 if (!ModifiedRes) {
1043 return TokError("invalid modifier '" + getTok().getIdentifier() +
1044 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001045 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001046
Daniel Dunbar55f16672010-09-17 02:47:07 +00001047 Res = ModifiedRes;
1048 Lex();
1049 }
1050
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001051 // Try to constant fold it up front, if possible.
1052 int64_t Value;
1053 if (Res->EvaluateAsAbsolute(Value))
1054 Res = MCConstantExpr::Create(Value, getContext());
1055
1056 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001057}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001058
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001059bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001060 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001061 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001062}
1063
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001064bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001065 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001066
Daniel Dunbar75630b32009-06-30 02:10:03 +00001067 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001068 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069 return true;
1070
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001071 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001072 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073
1074 return false;
1075}
1076
Michael J. Spencer530ce852010-10-09 11:00:50 +00001077static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001079 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001080 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001082
Jim Grosbach4b905842013-09-20 23:08:21 +00001083 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001086 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001089 return 1;
1090
Jim Grosbach4b905842013-09-20 23:08:21 +00001091 // Low Precedence: |, &, ^
1092 //
1093 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001094 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001095 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001096 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001097 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001098 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001099 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001100 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001101 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001102 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001103
Jim Grosbach4b905842013-09-20 23:08:21 +00001104 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001105 case AsmToken::EqualEqual:
1106 Kind = MCBinaryExpr::EQ;
1107 return 3;
1108 case AsmToken::ExclaimEqual:
1109 case AsmToken::LessGreater:
1110 Kind = MCBinaryExpr::NE;
1111 return 3;
1112 case AsmToken::Less:
1113 Kind = MCBinaryExpr::LT;
1114 return 3;
1115 case AsmToken::LessEqual:
1116 Kind = MCBinaryExpr::LTE;
1117 return 3;
1118 case AsmToken::Greater:
1119 Kind = MCBinaryExpr::GT;
1120 return 3;
1121 case AsmToken::GreaterEqual:
1122 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001123 return 3;
1124
Jim Grosbach4b905842013-09-20 23:08:21 +00001125 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001126 case AsmToken::LessLess:
1127 Kind = MCBinaryExpr::Shl;
1128 return 4;
1129 case AsmToken::GreaterGreater:
1130 Kind = MCBinaryExpr::Shr;
1131 return 4;
1132
Jim Grosbach4b905842013-09-20 23:08:21 +00001133 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001134 case AsmToken::Plus:
1135 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001137 case AsmToken::Minus:
1138 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001140
Jim Grosbach4b905842013-09-20 23:08:21 +00001141 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001142 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001143 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001144 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001145 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001146 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001147 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001148 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001149 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001150 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 }
1152}
1153
Jim Grosbach4b905842013-09-20 23:08:21 +00001154/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001156bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001157 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001159 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001160 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 // If the next token is lower precedence than we are allowed to eat, return
1163 // successfully with what we ate already.
1164 if (TokPrec < Precedence)
1165 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Sean Callanan686ed8d2010-01-19 20:22:31 +00001167 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001168
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001170 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001171 if (parsePrimaryExpr(RHS, EndLoc))
1172 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001173
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001174 // If BinOp binds less tightly with RHS than the operator after RHS, let
1175 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001176 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001177 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001178 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1179 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001180
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001181 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001182 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001183 }
1184}
1185
Chris Lattner36e02122009-06-21 20:54:55 +00001186/// ParseStatement:
1187/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001188/// ::= Label* Directive ...Operands... EndOfStatement
1189/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001190bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001191 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001192 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001193 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001194 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001195 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001196
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001197 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001198 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001199 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001200 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001202 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001203 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001204 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001205
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001206 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001207 if (Lexer.is(AsmToken::Integer)) {
1208 LocalLabelVal = getTok().getIntVal();
1209 if (LocalLabelVal < 0) {
1210 if (!TheCondState.Ignore)
1211 return TokError("unexpected token at start of statement");
1212 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001213 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001214 IDVal = getTok().getString();
1215 Lex(); // Consume the integer token to be used as an identifier token.
1216 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001217 if (!TheCondState.Ignore)
1218 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001219 }
1220 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001221 } else if (Lexer.is(AsmToken::Dot)) {
1222 // Treat '.' as a valid identifier in this context.
1223 Lex();
1224 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001225 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001226 if (!TheCondState.Ignore)
1227 return TokError("unexpected token at start of statement");
1228 IDVal = "";
1229 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001230
Chris Lattner926885c2010-04-17 18:14:27 +00001231 // Handle conditional assembly here before checking for skipping. We
1232 // have to do this so that .endif isn't skipped in a ".if 0" block for
1233 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001234 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001235 DirectiveKindMap.find(IDVal);
1236 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1237 ? DK_NO_DIRECTIVE
1238 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001239 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 default:
1241 break;
1242 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001243 case DK_IFEQ:
1244 case DK_IFGE:
1245 case DK_IFGT:
1246 case DK_IFLE:
1247 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001248 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001249 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001250 case DK_IFB:
1251 return parseDirectiveIfb(IDLoc, true);
1252 case DK_IFNB:
1253 return parseDirectiveIfb(IDLoc, false);
1254 case DK_IFC:
1255 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001256 case DK_IFEQS:
1257 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001258 case DK_IFNC:
1259 return parseDirectiveIfc(IDLoc, false);
1260 case DK_IFDEF:
1261 return parseDirectiveIfdef(IDLoc, true);
1262 case DK_IFNDEF:
1263 case DK_IFNOTDEF:
1264 return parseDirectiveIfdef(IDLoc, false);
1265 case DK_ELSEIF:
1266 return parseDirectiveElseIf(IDLoc);
1267 case DK_ELSE:
1268 return parseDirectiveElse(IDLoc);
1269 case DK_ENDIF:
1270 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001271 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001272
Eli Bendersky88024712013-01-16 19:32:36 +00001273 // Ignore the statement if in the middle of inactive conditional
1274 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001275 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001276 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001277 return false;
1278 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001279
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001280 // FIXME: Recurse on local labels?
1281
1282 // See what kind of statement we have.
1283 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001284 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001285 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001286
Chris Lattner36e02122009-06-21 20:54:55 +00001287 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001288 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001289
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001290 // Diagnose attempt to use '.' as a label.
1291 if (IDVal == ".")
1292 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1293
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001294 // Diagnose attempt to use a variable as a label.
1295 //
1296 // FIXME: Diagnostics. Note the location of the definition as a label.
1297 // FIXME: This doesn't diagnose assignment to a symbol which has been
1298 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001299 MCSymbol *Sym;
1300 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001301 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001302 else
1303 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001304 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001305 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001306
Daniel Dunbare73b2672009-08-26 22:13:22 +00001307 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001308 if (!ParsingInlineAsm)
1309 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310
Kevin Enderbye7739d42011-12-09 18:09:40 +00001311 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001312 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001313 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001314 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1315 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001316
Tim Northover1744d0a2013-10-25 12:49:50 +00001317 getTargetParser().onLabelParsed(Sym);
1318
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001319 // Consume any end of statement token, if present, to avoid spurious
1320 // AddBlankLine calls().
1321 if (Lexer.is(AsmToken::EndOfStatement)) {
1322 Lex();
1323 if (Lexer.is(AsmToken::Eof))
1324 return false;
1325 }
1326
Eli Friedman0f4871d2012-10-22 23:58:19 +00001327 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001328 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001329
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001330 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001331 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001332 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
Jim Grosbach4b905842013-09-20 23:08:21 +00001334 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335
1336 default: // Normal instruction or directive.
1337 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001338 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001339
1340 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001341 if (areMacrosEnabled())
1342 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1343 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001344 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001345
Michael J. Spencer530ce852010-10-09 11:00:50 +00001346 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001347
Eli Bendersky17233942013-01-15 22:59:42 +00001348 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001349 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001350 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001351 //
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // 1. The target-specific assembly parser. Some directives are target
1353 // specific or may potentially behave differently on certain targets.
1354 // 2. Asm parser extensions. For example, platform-specific parsers
1355 // (like the ELF parser) register themselves as extensions.
1356 // 3. The generic directive parser implemented by this class. These are
1357 // all the directives that behave in a target and platform independent
1358 // manner, or at least have a default behavior that's shared between
1359 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001360
Eli Bendersky17233942013-01-15 22:59:42 +00001361 // First query the target-specific parser. It will return 'true' if it
1362 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001363 if (!getTargetParser().ParseDirective(ID))
1364 return false;
1365
Alp Tokercb402912014-01-24 17:20:08 +00001366 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001368 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1369 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001370 if (Handler.first)
1371 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1372
1373 // Finally, if no one else is interested in this directive, it must be
1374 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001375 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001376 default:
1377 break;
1378 case DK_SET:
1379 case DK_EQU:
1380 return parseDirectiveSet(IDVal, true);
1381 case DK_EQUIV:
1382 return parseDirectiveSet(IDVal, false);
1383 case DK_ASCII:
1384 return parseDirectiveAscii(IDVal, false);
1385 case DK_ASCIZ:
1386 case DK_STRING:
1387 return parseDirectiveAscii(IDVal, true);
1388 case DK_BYTE:
1389 return parseDirectiveValue(1);
1390 case DK_SHORT:
1391 case DK_VALUE:
1392 case DK_2BYTE:
1393 return parseDirectiveValue(2);
1394 case DK_LONG:
1395 case DK_INT:
1396 case DK_4BYTE:
1397 return parseDirectiveValue(4);
1398 case DK_QUAD:
1399 case DK_8BYTE:
1400 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001401 case DK_OCTA:
1402 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001403 case DK_SINGLE:
1404 case DK_FLOAT:
1405 return parseDirectiveRealValue(APFloat::IEEEsingle);
1406 case DK_DOUBLE:
1407 return parseDirectiveRealValue(APFloat::IEEEdouble);
1408 case DK_ALIGN: {
1409 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1410 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1411 }
1412 case DK_ALIGN32: {
1413 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1414 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1415 }
1416 case DK_BALIGN:
1417 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1418 case DK_BALIGNW:
1419 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1420 case DK_BALIGNL:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1422 case DK_P2ALIGN:
1423 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1424 case DK_P2ALIGNW:
1425 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1426 case DK_P2ALIGNL:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1428 case DK_ORG:
1429 return parseDirectiveOrg();
1430 case DK_FILL:
1431 return parseDirectiveFill();
1432 case DK_ZERO:
1433 return parseDirectiveZero();
1434 case DK_EXTERN:
1435 eatToEndOfStatement(); // .extern is the default, ignore it.
1436 return false;
1437 case DK_GLOBL:
1438 case DK_GLOBAL:
1439 return parseDirectiveSymbolAttribute(MCSA_Global);
1440 case DK_LAZY_REFERENCE:
1441 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1442 case DK_NO_DEAD_STRIP:
1443 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1444 case DK_SYMBOL_RESOLVER:
1445 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1446 case DK_PRIVATE_EXTERN:
1447 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1448 case DK_REFERENCE:
1449 return parseDirectiveSymbolAttribute(MCSA_Reference);
1450 case DK_WEAK_DEFINITION:
1451 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1452 case DK_WEAK_REFERENCE:
1453 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1454 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1456 case DK_COMM:
1457 case DK_COMMON:
1458 return parseDirectiveComm(/*IsLocal=*/false);
1459 case DK_LCOMM:
1460 return parseDirectiveComm(/*IsLocal=*/true);
1461 case DK_ABORT:
1462 return parseDirectiveAbort();
1463 case DK_INCLUDE:
1464 return parseDirectiveInclude();
1465 case DK_INCBIN:
1466 return parseDirectiveIncbin();
1467 case DK_CODE16:
1468 case DK_CODE16GCC:
1469 return TokError(Twine(IDVal) + " not supported yet");
1470 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001471 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001472 case DK_IRP:
1473 return parseDirectiveIrp(IDLoc);
1474 case DK_IRPC:
1475 return parseDirectiveIrpc(IDLoc);
1476 case DK_ENDR:
1477 return parseDirectiveEndr(IDLoc);
1478 case DK_BUNDLE_ALIGN_MODE:
1479 return parseDirectiveBundleAlignMode();
1480 case DK_BUNDLE_LOCK:
1481 return parseDirectiveBundleLock();
1482 case DK_BUNDLE_UNLOCK:
1483 return parseDirectiveBundleUnlock();
1484 case DK_SLEB128:
1485 return parseDirectiveLEB128(true);
1486 case DK_ULEB128:
1487 return parseDirectiveLEB128(false);
1488 case DK_SPACE:
1489 case DK_SKIP:
1490 return parseDirectiveSpace(IDVal);
1491 case DK_FILE:
1492 return parseDirectiveFile(IDLoc);
1493 case DK_LINE:
1494 return parseDirectiveLine();
1495 case DK_LOC:
1496 return parseDirectiveLoc();
1497 case DK_STABS:
1498 return parseDirectiveStabs();
1499 case DK_CFI_SECTIONS:
1500 return parseDirectiveCFISections();
1501 case DK_CFI_STARTPROC:
1502 return parseDirectiveCFIStartProc();
1503 case DK_CFI_ENDPROC:
1504 return parseDirectiveCFIEndProc();
1505 case DK_CFI_DEF_CFA:
1506 return parseDirectiveCFIDefCfa(IDLoc);
1507 case DK_CFI_DEF_CFA_OFFSET:
1508 return parseDirectiveCFIDefCfaOffset();
1509 case DK_CFI_ADJUST_CFA_OFFSET:
1510 return parseDirectiveCFIAdjustCfaOffset();
1511 case DK_CFI_DEF_CFA_REGISTER:
1512 return parseDirectiveCFIDefCfaRegister(IDLoc);
1513 case DK_CFI_OFFSET:
1514 return parseDirectiveCFIOffset(IDLoc);
1515 case DK_CFI_REL_OFFSET:
1516 return parseDirectiveCFIRelOffset(IDLoc);
1517 case DK_CFI_PERSONALITY:
1518 return parseDirectiveCFIPersonalityOrLsda(true);
1519 case DK_CFI_LSDA:
1520 return parseDirectiveCFIPersonalityOrLsda(false);
1521 case DK_CFI_REMEMBER_STATE:
1522 return parseDirectiveCFIRememberState();
1523 case DK_CFI_RESTORE_STATE:
1524 return parseDirectiveCFIRestoreState();
1525 case DK_CFI_SAME_VALUE:
1526 return parseDirectiveCFISameValue(IDLoc);
1527 case DK_CFI_RESTORE:
1528 return parseDirectiveCFIRestore(IDLoc);
1529 case DK_CFI_ESCAPE:
1530 return parseDirectiveCFIEscape();
1531 case DK_CFI_SIGNAL_FRAME:
1532 return parseDirectiveCFISignalFrame();
1533 case DK_CFI_UNDEFINED:
1534 return parseDirectiveCFIUndefined(IDLoc);
1535 case DK_CFI_REGISTER:
1536 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001537 case DK_CFI_WINDOW_SAVE:
1538 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001539 case DK_MACROS_ON:
1540 case DK_MACROS_OFF:
1541 return parseDirectiveMacrosOnOff(IDVal);
1542 case DK_MACRO:
1543 return parseDirectiveMacro(IDLoc);
1544 case DK_ENDM:
1545 case DK_ENDMACRO:
1546 return parseDirectiveEndMacro(IDVal);
1547 case DK_PURGEM:
1548 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001549 case DK_END:
1550 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001551 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001552 return parseDirectiveError(IDLoc, false);
1553 case DK_ERROR:
1554 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001555 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001556
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001557 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001558 }
Chris Lattner36e02122009-06-21 20:54:55 +00001559
Chad Rosierc7f552c2013-02-12 21:33:51 +00001560 // __asm _emit or __asm __emit
1561 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1562 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001563 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001564
1565 // __asm align
1566 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001568
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001569 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001570
Chris Lattner7cbfa442010-05-19 23:34:33 +00001571 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001572 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001573 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001575 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001576 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001577
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001578 // Dump the parsed representation, if requested.
1579 if (getShowParsedOperands()) {
1580 SmallString<256> Str;
1581 raw_svector_ostream OS(Str);
1582 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001583 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001584 if (i != 0)
1585 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001586 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001587 }
1588 OS << "]";
1589
Jim Grosbach4b905842013-09-20 23:08:21 +00001590 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001591 }
1592
Oliver Stannard8b273082014-06-19 15:52:37 +00001593 // If we are generating dwarf for the current section then generate a .loc
1594 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001595 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001596 getContext().getGenDwarfSectionSyms().count(
1597 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001598
Eli Bendersky88024712013-01-16 19:32:36 +00001599 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001600
Eli Bendersky88024712013-01-16 19:32:36 +00001601 // If we previously parsed a cpp hash file line comment then make sure the
1602 // current Dwarf File is for the CppHashFilename if not then emit the
1603 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001604 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001605 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1606 0, StringRef(), CppHashFilename);
1607 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001608
Jim Grosbach4b905842013-09-20 23:08:21 +00001609 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1610 // cache with the different Loc from the call above we save the last
1611 // info we queried here with SrcMgr.FindLineNumber().
1612 unsigned CppHashLocLineNo;
1613 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1614 CppHashLocLineNo = LastQueryLine;
1615 else {
1616 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1617 LastQueryLine = CppHashLocLineNo;
1618 LastQueryIDLoc = CppHashLoc;
1619 LastQueryBuffer = CppHashBuf;
1620 }
1621 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001622 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001623
Jim Grosbach4b905842013-09-20 23:08:21 +00001624 getStreamer().EmitDwarfLocDirective(
1625 getContext().getGenDwarfFileNumber(), Line, 0,
1626 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1627 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001628 }
1629
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001630 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001631 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001632 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001633 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1634 Info.ParsedOperands, Out,
1635 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001636 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001637
Chris Lattnera2a9d162010-09-11 16:18:25 +00001638 // Don't skip the rest of the line, the instruction parser is responsible for
1639 // that.
1640 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001641}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001642
Jim Grosbach4b905842013-09-20 23:08:21 +00001643/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001644/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001645void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001646 if (!Lexer.is(AsmToken::EndOfStatement))
1647 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001648 // Eat EOL.
1649 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001650}
1651
Jim Grosbach4b905842013-09-20 23:08:21 +00001652/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001653/// ::= # number "filename"
1654/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001655bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001656 Lex(); // Eat the hash token.
1657
1658 if (getLexer().isNot(AsmToken::Integer)) {
1659 // Consume the line since in cases it is not a well-formed line directive,
1660 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001661 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001662 return false;
1663 }
1664
1665 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001666 Lex();
1667
1668 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001670 return false;
1671 }
1672
1673 StringRef Filename = getTok().getString();
1674 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001675 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001676
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1678 CppHashLoc = L;
1679 CppHashFilename = Filename;
1680 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001681 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001682
1683 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001684 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001685 return false;
1686}
1687
Jim Grosbach4b905842013-09-20 23:08:21 +00001688/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001689/// for the Filename and LineNo if any in the diagnostic.
1690void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001691 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001692 raw_ostream &OS = errs();
1693
1694 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1695 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001696 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1697 unsigned CppHashBuf =
1698 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699
Jim Grosbach4b905842013-09-20 23:08:21 +00001700 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001702 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1703 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1704 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001705 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1706 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707 }
1708
Eric Christophera7c32732012-12-18 00:30:54 +00001709 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710 // manager changed or buffer changed (like in a nested include) then just
1711 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001712 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001714 if (Parser->SavedDiagHandler)
1715 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1716 else
Craig Topper353eda42014-04-24 06:44:33 +00001717 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718 return;
1719 }
1720
Eric Christophera7c32732012-12-18 00:30:54 +00001721 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001722 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1723 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001724 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001725
1726 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1727 int CppHashLocLineNo =
1728 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001729 int LineNo =
1730 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001731
Jim Grosbach4b905842013-09-20 23:08:21 +00001732 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1733 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001734 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001735
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001736 if (Parser->SavedDiagHandler)
1737 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1738 else
Craig Topper353eda42014-04-24 06:44:33 +00001739 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001740}
1741
Rafael Espindola2c064482012-08-21 18:29:30 +00001742// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1743// difference being that that function accepts '@' as part of identifiers and
1744// we can't do that. AsmLexer.cpp should probably be changed to handle
1745// '@' as a special case when needed.
1746static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001747 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1748 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001749}
1750
Rafael Espindola34b9c512012-06-03 23:57:14 +00001751bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001752 ArrayRef<MCAsmMacroParameter> Parameters,
1753 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001754 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001755 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001756 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001757 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001758
Preston Gurd05500642012-09-19 20:36:12 +00001759 // A macro without parameters is handled differently on Darwin:
1760 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001761 while (!Body.empty()) {
1762 // Scan for the next substitution.
1763 std::size_t End = Body.size(), Pos = 0;
1764 for (; Pos != End; ++Pos) {
1765 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001766 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 // This macro has no parameters, look for $0, $1, etc.
1768 if (Body[Pos] != '$' || Pos + 1 == End)
1769 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001770
Rafael Espindola1134ab232011-06-05 02:43:45 +00001771 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001772 if (Next == '$' || Next == 'n' ||
1773 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 break;
1775 } else {
1776 // This macro has parameters, look for \foo, \bar, etc.
1777 if (Body[Pos] == '\\' && Pos + 1 != End)
1778 break;
1779 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001780 }
1781
1782 // Add the prefix.
1783 OS << Body.slice(0, Pos);
1784
1785 // Check if we reached the end.
1786 if (Pos == End)
1787 break;
1788
Benjamin Kramer513e7442014-02-20 13:36:32 +00001789 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001790 switch (Body[Pos + 1]) {
1791 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 case '$':
1793 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001794 break;
1795
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001797 case 'n':
1798 OS << A.size();
1799 break;
1800
Jim Grosbach4b905842013-09-20 23:08:21 +00001801 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 default: {
1803 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001805 if (Index >= A.size())
1806 break;
1807
1808 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001809 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 ie = A[Index].end();
1811 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 OS << it->getString();
1813 break;
1814 }
1815 }
1816 Pos += 2;
1817 } else {
1818 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001819 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001820 ++I;
1821
Jim Grosbach4b905842013-09-20 23:08:21 +00001822 const char *Begin = Body.data() + Pos + 1;
1823 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001824 unsigned Index = 0;
1825 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001826 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001827 break;
1828
Preston Gurd05500642012-09-19 20:36:12 +00001829 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001830 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1831 Pos += 3;
1832 else {
1833 OS << '\\' << Argument;
1834 Pos = I;
1835 }
Preston Gurd05500642012-09-19 20:36:12 +00001836 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001837 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001838 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001839 ie = A[Index].end();
1840 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001841 // We expect no quotes around the string's contents when
1842 // parsing for varargs.
1843 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001844 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001845 else
1846 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001847
Preston Gurd05500642012-09-19 20:36:12 +00001848 Pos += 1 + Argument.size();
1849 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001850 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001851 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001853 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001854
Rafael Espindola1134ab232011-06-05 02:43:45 +00001855 return false;
1856}
Daniel Dunbar43235712010-07-18 18:54:11 +00001857
Jim Grosbach4b905842013-09-20 23:08:21 +00001858MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1859 SMLoc EL, MemoryBuffer *I)
1860 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1861 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001862
Jim Grosbach4b905842013-09-20 23:08:21 +00001863static bool isOperator(AsmToken::TokenKind kind) {
1864 switch (kind) {
1865 default:
1866 return false;
1867 case AsmToken::Plus:
1868 case AsmToken::Minus:
1869 case AsmToken::Tilde:
1870 case AsmToken::Slash:
1871 case AsmToken::Star:
1872 case AsmToken::Dot:
1873 case AsmToken::Equal:
1874 case AsmToken::EqualEqual:
1875 case AsmToken::Pipe:
1876 case AsmToken::PipePipe:
1877 case AsmToken::Caret:
1878 case AsmToken::Amp:
1879 case AsmToken::AmpAmp:
1880 case AsmToken::Exclaim:
1881 case AsmToken::ExclaimEqual:
1882 case AsmToken::Percent:
1883 case AsmToken::Less:
1884 case AsmToken::LessEqual:
1885 case AsmToken::LessLess:
1886 case AsmToken::LessGreater:
1887 case AsmToken::Greater:
1888 case AsmToken::GreaterEqual:
1889 case AsmToken::GreaterGreater:
1890 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001891 }
1892}
1893
David Majnemer16252452014-01-29 00:07:39 +00001894namespace {
1895class AsmLexerSkipSpaceRAII {
1896public:
1897 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1898 Lexer.setSkipSpace(SkipSpace);
1899 }
1900
1901 ~AsmLexerSkipSpaceRAII() {
1902 Lexer.setSkipSpace(true);
1903 }
1904
1905private:
1906 AsmLexer &Lexer;
1907};
1908}
1909
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001910bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1911
1912 if (Vararg) {
1913 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1914 StringRef Str = parseStringToEndOfStatement();
1915 MA.push_back(AsmToken(AsmToken::String, Str));
1916 }
1917 return false;
1918 }
1919
Rafael Espindola768b41c2012-06-15 14:02:34 +00001920 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001921 unsigned AddTokens = 0;
1922
David Majnemer16252452014-01-29 00:07:39 +00001923 // Darwin doesn't use spaces to delmit arguments.
1924 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001925
1926 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001927 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001928 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001929
David Majnemer91fc4c22014-01-29 18:57:46 +00001930 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001931 break;
Preston Gurd05500642012-09-19 20:36:12 +00001932
1933 if (Lexer.is(AsmToken::Space)) {
1934 Lex(); // Eat spaces
1935
1936 // Spaces can delimit parameters, but could also be part an expression.
1937 // If the token after a space is an operator, add the token and the next
1938 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001939 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001940 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001941 // Check to see whether the token is used as an operator,
1942 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001943 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001944 if (*NextChar == ' ')
1945 AddTokens = 2;
1946 }
1947
1948 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001949 break;
1950 }
1951 }
1952 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001953
Jim Grosbach4b905842013-09-20 23:08:21 +00001954 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955 // to be able to fill in the remaining default parameter values
1956 if (Lexer.is(AsmToken::EndOfStatement))
1957 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958
1959 // Adjust the current parentheses level.
1960 if (Lexer.is(AsmToken::LParen))
1961 ++ParenLevel;
1962 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1963 --ParenLevel;
1964
1965 // Append the token to the current argument list.
1966 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001967 if (AddTokens)
1968 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001969 Lex();
1970 }
Preston Gurd05500642012-09-19 20:36:12 +00001971
Rafael Espindola768b41c2012-06-15 14:02:34 +00001972 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001973 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001974 return false;
1975}
1976
1977// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001978bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001979 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001981 bool NamedParametersFound = false;
1982 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001984 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001985 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986
Rafael Espindola768b41c2012-06-15 14:02:34 +00001987 // Parse two kinds of macro invocations:
1988 // - macros defined without any parameters accept an arbitrary number of them
1989 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001990 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001991 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1992 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001993 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001994 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001995
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001997 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001998 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001999 eatToEndOfStatement();
2000 return true;
2001 }
2002
2003 if (!Lexer.is(AsmToken::Equal)) {
2004 TokError("expected '=' after formal parameter identifier");
2005 eatToEndOfStatement();
2006 return true;
2007 }
2008 Lex();
2009
2010 NamedParametersFound = true;
2011 }
2012
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002013 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002014 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002015 eatToEndOfStatement();
2016 return true;
2017 }
2018
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002019 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2020 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002021 return true;
2022
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002023 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002024 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 unsigned FAI = 0;
2026 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002027 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002028 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002029
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002031 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002032 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002033 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002034 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002035 return true;
2036 }
2037 PI = FAI;
2038 }
2039
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002040 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002041 if (A.size() <= PI)
2042 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002043 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002044
2045 if (FALocs.size() <= PI)
2046 FALocs.resize(PI + 1);
2047
2048 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002049 }
Jim Grosbach206661622012-07-30 22:44:17 +00002050
Preston Gurd242ed3152012-09-19 20:29:04 +00002051 // At the end of the statement, fill in remaining arguments that have
2052 // default values. If there aren't any, then the next argument is
2053 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002054 if (Lexer.is(AsmToken::EndOfStatement)) {
2055 bool Failure = false;
2056 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2057 if (A[FAI].empty()) {
2058 if (M->Parameters[FAI].Required) {
2059 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2060 "missing value for required parameter "
2061 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2062 Failure = true;
2063 }
2064
2065 if (!M->Parameters[FAI].Value.empty())
2066 A[FAI] = M->Parameters[FAI].Value;
2067 }
2068 }
2069 return Failure;
2070 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002071
2072 if (Lexer.is(AsmToken::Comma))
2073 Lex();
2074 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002075
2076 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002077}
2078
Jim Grosbach4b905842013-09-20 23:08:21 +00002079const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2080 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002081 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002082}
2083
Jim Grosbach4b905842013-09-20 23:08:21 +00002084void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002085 MacroMap[Name] = new MCAsmMacro(Macro);
2086}
2087
Jim Grosbach4b905842013-09-20 23:08:21 +00002088void AsmParser::undefineMacro(StringRef Name) {
2089 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002090 if (I != MacroMap.end()) {
2091 delete I->getValue();
2092 MacroMap.erase(I);
2093 }
2094}
2095
Jim Grosbach4b905842013-09-20 23:08:21 +00002096bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002097 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2098 // this, although we should protect against infinite loops.
2099 if (ActiveMacros.size() == 20)
2100 return TokError("macros cannot be nested more than 20 levels deep");
2101
Eli Bendersky38274122013-01-14 23:22:36 +00002102 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002103 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002104 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002105
Rafael Espindola1134ab232011-06-05 02:43:45 +00002106 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2107 // to hold the macro body with substitutions.
2108 SmallString<256> Buf;
2109 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002110 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002111
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002112 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113 return true;
2114
Eli Bendersky38274122013-01-14 23:22:36 +00002115 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002116 // instantiation.
2117 OS << ".endmacro\n";
2118
Rafael Espindola1134ab232011-06-05 02:43:45 +00002119 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002120 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002121
Daniel Dunbar43235712010-07-18 18:54:11 +00002122 // Create the macro instantiation object and add to the current macro
2123 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002124 MacroInstantiation *MI = new MacroInstantiation(
2125 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002126 ActiveMacros.push_back(MI);
2127
2128 // Jump to the macro instantiation and prime the lexer.
2129 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2130 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2131 Lex();
2132
2133 return false;
2134}
2135
Jim Grosbach4b905842013-09-20 23:08:21 +00002136void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002137 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002138 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 Lex();
2140
2141 // Pop the instantiation entry.
2142 delete ActiveMacros.back();
2143 ActiveMacros.pop_back();
2144}
2145
Jim Grosbach4b905842013-09-20 23:08:21 +00002146static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002147 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002148 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2150 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002151 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002152 case MCExpr::Target:
2153 case MCExpr::Constant:
2154 return false;
2155 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002156 const MCSymbol &S =
2157 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002158 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002159 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002160 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002161 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002162 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002163 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002164 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002165
2166 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002167}
2168
Jim Grosbach4b905842013-09-20 23:08:21 +00002169bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002170 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002171 // FIXME: Use better location, we should use proper tokens.
2172 SMLoc EqualLoc = Lexer.getLoc();
2173
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002174 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002175 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002176 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002177
Rafael Espindola72f5f172012-01-28 05:57:00 +00002178 // Note: we don't count b as used in "a = b". This is to allow
2179 // a = b
2180 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002181
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002182 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002183 return TokError("unexpected token in assignment");
2184
2185 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002186 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187
Daniel Dunbar5f339242009-10-16 01:57:39 +00002188 // Validate that the LHS is allowed to be a variable (either it has not been
2189 // used as a symbol, or it is an absolute symbol).
2190 MCSymbol *Sym = getContext().LookupSymbol(Name);
2191 if (Sym) {
2192 // Diagnose assignment to a label.
2193 //
2194 // FIXME: Diagnostics. Note the location of the definition as a label.
2195 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002196 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002197 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2198 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002199 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002200 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2201 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002202 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002203 return Error(EqualLoc, "redefinition of '" + Name + "'");
2204 else if (!Sym->isVariable())
2205 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002206 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002207 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002208 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002209
2210 // Don't count these checks as uses.
2211 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002212 } else if (Name == ".") {
2213 if (Out.EmitValueToOffset(Value, 0)) {
2214 Error(EqualLoc, "expected absolute expression");
2215 eatToEndOfStatement();
2216 }
2217 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002218 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002219 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002220
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002221 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002222 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002223 if (NoDeadStrip)
2224 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2225
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002226 return false;
2227}
2228
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002229/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002230/// ::= identifier
2231/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002232bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002233 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002234 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2235 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002236 // handle this as a context dependent token, instead we detect adjacent tokens
2237 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002238 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2239 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002240
Hans Wennborgce69d772013-10-18 20:46:28 +00002241 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242 Lex();
2243 if (Lexer.isNot(AsmToken::Identifier))
2244 return true;
2245
Hans Wennborgce69d772013-10-18 20:46:28 +00002246 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2247 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002248 return true;
2249
2250 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002251 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002252 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002253 Lex();
2254 return false;
2255 }
2256
Jim Grosbach4b905842013-09-20 23:08:21 +00002257 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002258 return true;
2259
Sean Callanan936b0d32010-01-19 21:44:56 +00002260 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002261
Sean Callanan686ed8d2010-01-19 20:22:31 +00002262 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263
2264 return false;
2265}
2266
Jim Grosbach4b905842013-09-20 23:08:21 +00002267/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002268/// ::= .equ identifier ',' expression
2269/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002270/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002271bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002272 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002273
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002274 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002275 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002276
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002278 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002279 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002280
Jim Grosbach4b905842013-09-20 23:08:21 +00002281 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002282}
2283
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002284bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002285 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002286
2287 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002288 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002289 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2290 if (Str[i] != '\\') {
2291 Data += Str[i];
2292 continue;
2293 }
2294
2295 // Recognize escaped characters. Note that this escape semantics currently
2296 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2297 ++i;
2298 if (i == e)
2299 return TokError("unexpected backslash at end of string");
2300
2301 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002302 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002303 // Consume up to three octal characters.
2304 unsigned Value = Str[i] - '0';
2305
Jim Grosbach4b905842013-09-20 23:08:21 +00002306 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002307 ++i;
2308 Value = Value * 8 + (Str[i] - '0');
2309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002311 ++i;
2312 Value = Value * 8 + (Str[i] - '0');
2313 }
2314 }
2315
2316 if (Value > 255)
2317 return TokError("invalid octal escape sequence (out of range)");
2318
Jim Grosbach4b905842013-09-20 23:08:21 +00002319 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002320 continue;
2321 }
2322
2323 // Otherwise recognize individual escapes.
2324 switch (Str[i]) {
2325 default:
2326 // Just reject invalid escape sequences for now.
2327 return TokError("invalid escape sequence (unrecognized character)");
2328
2329 case 'b': Data += '\b'; break;
2330 case 'f': Data += '\f'; break;
2331 case 'n': Data += '\n'; break;
2332 case 'r': Data += '\r'; break;
2333 case 't': Data += '\t'; break;
2334 case '"': Data += '"'; break;
2335 case '\\': Data += '\\'; break;
2336 }
2337 }
2338
2339 return false;
2340}
2341
Jim Grosbach4b905842013-09-20 23:08:21 +00002342/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002343/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002344bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002345 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002346 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002347
Daniel Dunbara10e5192009-06-24 23:30:00 +00002348 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002349 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002350 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351
Daniel Dunbaref668c12009-08-14 18:19:52 +00002352 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002353 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002354 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355
Rafael Espindola64e1af82013-07-02 15:49:13 +00002356 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002357 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002358 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359
Sean Callanan686ed8d2010-01-19 20:22:31 +00002360 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
2362 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002363 break;
2364
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002366 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002367 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002368 }
2369 }
2370
Sean Callanan686ed8d2010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002372 return false;
2373}
2374
Jim Grosbach4b905842013-09-20 23:08:21 +00002375/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002376/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002377bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002378 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002379 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002380
Daniel Dunbara10e5192009-06-24 23:30:00 +00002381 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002382 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002383 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002384 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002385 return true;
2386
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002387 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002388 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2389 assert(Size <= 8 && "Invalid size");
2390 uint64_t IntValue = MCE->getValue();
2391 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2392 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002393 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002394 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002395 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002396
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002397 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002399
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002401 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002403 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 }
2405 }
2406
Sean Callanan686ed8d2010-01-19 20:22:31 +00002407 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 return false;
2409}
2410
David Woodhoused6de0d92014-02-01 16:20:59 +00002411/// ParseDirectiveOctaValue
2412/// ::= .octa [ hexconstant (, hexconstant)* ]
2413bool AsmParser::parseDirectiveOctaValue() {
2414 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2415 checkForValidSection();
2416
2417 for (;;) {
2418 if (Lexer.getKind() == AsmToken::Error)
2419 return true;
2420 if (Lexer.getKind() != AsmToken::Integer &&
2421 Lexer.getKind() != AsmToken::BigNum)
2422 return TokError("unknown token in expression");
2423
2424 SMLoc ExprLoc = getLexer().getLoc();
2425 APInt IntValue = getTok().getAPIntVal();
2426 Lex();
2427
2428 uint64_t hi, lo;
2429 if (IntValue.isIntN(64)) {
2430 hi = 0;
2431 lo = IntValue.getZExtValue();
2432 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002433 // It might actually have more than 128 bits, but the top ones are zero.
2434 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002435 lo = IntValue.getLoBits(64).getZExtValue();
2436 } else
2437 return Error(ExprLoc, "literal value out of range for directive");
2438
2439 if (MAI.isLittleEndian()) {
2440 getStreamer().EmitIntValue(lo, 8);
2441 getStreamer().EmitIntValue(hi, 8);
2442 } else {
2443 getStreamer().EmitIntValue(hi, 8);
2444 getStreamer().EmitIntValue(lo, 8);
2445 }
2446
2447 if (getLexer().is(AsmToken::EndOfStatement))
2448 break;
2449
2450 // FIXME: Improve diagnostic.
2451 if (getLexer().isNot(AsmToken::Comma))
2452 return TokError("unexpected token in directive");
2453 Lex();
2454 }
2455 }
2456
2457 Lex();
2458 return false;
2459}
2460
Jim Grosbach4b905842013-09-20 23:08:21 +00002461/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002462/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002463bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002465 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466
2467 for (;;) {
2468 // We don't truly support arithmetic on floating point expressions, so we
2469 // have to manually parse unary prefixes.
2470 bool IsNeg = false;
2471 if (getLexer().is(AsmToken::Minus)) {
2472 Lex();
2473 IsNeg = true;
2474 } else if (getLexer().is(AsmToken::Plus))
2475 Lex();
2476
Michael J. Spencer530ce852010-10-09 11:00:50 +00002477 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002478 getLexer().isNot(AsmToken::Real) &&
2479 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002480 return TokError("unexpected token in directive");
2481
2482 // Convert to an APFloat.
2483 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002484 StringRef IDVal = getTok().getString();
2485 if (getLexer().is(AsmToken::Identifier)) {
2486 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2487 Value = APFloat::getInf(Semantics);
2488 else if (!IDVal.compare_lower("nan"))
2489 Value = APFloat::getNaN(Semantics, false, ~0);
2490 else
2491 return TokError("invalid floating point literal");
2492 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002493 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002494 return TokError("invalid floating point literal");
2495 if (IsNeg)
2496 Value.changeSign();
2497
2498 // Consume the numeric token.
2499 Lex();
2500
2501 // Emit the value as an integer.
2502 APInt AsInt = Value.bitcastToAPInt();
2503 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002504 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002505
2506 if (getLexer().is(AsmToken::EndOfStatement))
2507 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002508
Daniel Dunbar2af16532010-09-24 01:59:56 +00002509 if (getLexer().isNot(AsmToken::Comma))
2510 return TokError("unexpected token in directive");
2511 Lex();
2512 }
2513 }
2514
2515 Lex();
2516 return false;
2517}
2518
Jim Grosbach4b905842013-09-20 23:08:21 +00002519/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002520/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002521bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002522 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002523
2524 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002525 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002526 return true;
2527
Rafael Espindolab91bac62010-10-05 19:42:57 +00002528 int64_t Val = 0;
2529 if (getLexer().is(AsmToken::Comma)) {
2530 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002531 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002532 return true;
2533 }
2534
Rafael Espindola922e3f42010-09-16 15:03:59 +00002535 if (getLexer().isNot(AsmToken::EndOfStatement))
2536 return TokError("unexpected token in '.zero' directive");
2537
2538 Lex();
2539
Rafael Espindola64e1af82013-07-02 15:49:13 +00002540 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002541
2542 return false;
2543}
2544
Jim Grosbach4b905842013-09-20 23:08:21 +00002545/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002546/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002547bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002548 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002549
David Majnemer522d3db2014-02-01 07:19:38 +00002550 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002551 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002552 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553 return true;
2554
David Majnemer522d3db2014-02-01 07:19:38 +00002555 if (NumValues < 0) {
2556 Warning(RepeatLoc,
2557 "'.fill' directive with negative repeat count has no effect");
2558 NumValues = 0;
2559 }
2560
Roman Divackye33098f2013-09-24 17:44:41 +00002561 int64_t FillSize = 1;
2562 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002563
David Majnemer522d3db2014-02-01 07:19:38 +00002564 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002565 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2566 if (getLexer().isNot(AsmToken::Comma))
2567 return TokError("unexpected token in '.fill' directive");
2568 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002569
David Majnemer522d3db2014-02-01 07:19:38 +00002570 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002571 if (parseAbsoluteExpression(FillSize))
2572 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002573
Roman Divackye33098f2013-09-24 17:44:41 +00002574 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2575 if (getLexer().isNot(AsmToken::Comma))
2576 return TokError("unexpected token in '.fill' directive");
2577 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002578
David Majnemer522d3db2014-02-01 07:19:38 +00002579 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002580 if (parseAbsoluteExpression(FillExpr))
2581 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002582
Roman Divackye33098f2013-09-24 17:44:41 +00002583 if (getLexer().isNot(AsmToken::EndOfStatement))
2584 return TokError("unexpected token in '.fill' directive");
2585
2586 Lex();
2587 }
2588 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002589
David Majnemer522d3db2014-02-01 07:19:38 +00002590 if (FillSize < 0) {
2591 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2592 NumValues = 0;
2593 }
2594 if (FillSize > 8) {
2595 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2596 FillSize = 8;
2597 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002598
David Majnemer522d3db2014-02-01 07:19:38 +00002599 if (!isUInt<32>(FillExpr) && FillSize > 4)
2600 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2601
2602 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2603 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2604
2605 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2606 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2607 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2608 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002609
2610 return false;
2611}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002612
Jim Grosbach4b905842013-09-20 23:08:21 +00002613/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002614/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002615bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002616 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002617
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002618 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002619 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002620 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002621 return true;
2622
2623 // Parse optional fill expression.
2624 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002625 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2626 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002627 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002628 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002629
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002630 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002631 return true;
2632
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002633 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002634 return TokError("unexpected token in '.org' directive");
2635 }
2636
Sean Callanan686ed8d2010-01-19 20:22:31 +00002637 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002638
Jim Grosbachb5912772012-01-27 00:37:08 +00002639 // Only limited forms of relocatable expressions are accepted here, it
2640 // has to be relative to the current section. The streamer will return
2641 // 'true' if the expression wasn't evaluatable.
2642 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2643 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002644
2645 return false;
2646}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002647
Jim Grosbach4b905842013-09-20 23:08:21 +00002648/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002649/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002650bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002651 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002652
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002653 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002655 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002656 return true;
2657
2658 SMLoc MaxBytesLoc;
2659 bool HasFillExpr = false;
2660 int64_t FillExpr = 0;
2661 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002662 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2663 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002664 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002665 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002666
2667 // The fill expression can be omitted while specifying a maximum number of
2668 // alignment bytes, e.g:
2669 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002670 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002671 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002672 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002673 return true;
2674 }
2675
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002676 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2677 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002679 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002680
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002681 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002682 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002685 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 return TokError("unexpected token in directive");
2687 }
2688 }
2689
Sean Callanan686ed8d2010-01-19 20:22:31 +00002690 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002692 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002693 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002694
2695 // Compute alignment in bytes.
2696 if (IsPow2) {
2697 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002698 if (Alignment >= 32) {
2699 Error(AlignmentLoc, "invalid alignment value");
2700 Alignment = 31;
2701 }
2702
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002703 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002704 } else {
2705 // Reject alignments that aren't a power of two, for gas compatibility.
2706 if (!isPowerOf2_64(Alignment))
2707 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002708 }
2709
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002710 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002711 if (MaxBytesLoc.isValid()) {
2712 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002713 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002714 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002715 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002716 }
2717
2718 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002719 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002720 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002721 MaxBytesToFill = 0;
2722 }
2723 }
2724
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002725 // Check whether we should use optimal code alignment for this .align
2726 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002727 const MCSection *Section = getStreamer().getCurrentSection().first;
2728 assert(Section && "must have section to emit alignment");
2729 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002730 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2731 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002732 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002733 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002734 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002735 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2736 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002737 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002738
2739 return false;
2740}
2741
Jim Grosbach4b905842013-09-20 23:08:21 +00002742/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002743/// ::= .file [number] filename
2744/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002745bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002746 // FIXME: I'm not sure what this is.
2747 int64_t FileNumber = -1;
2748 SMLoc FileNumberLoc = getLexer().getLoc();
2749 if (getLexer().is(AsmToken::Integer)) {
2750 FileNumber = getTok().getIntVal();
2751 Lex();
2752
2753 if (FileNumber < 1)
2754 return TokError("file number less than one");
2755 }
2756
2757 if (getLexer().isNot(AsmToken::String))
2758 return TokError("unexpected token in '.file' directive");
2759
2760 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002761 // Allow the strings to have escaped octal character sequence.
2762 std::string Path = getTok().getString();
2763 if (parseEscapedString(Path))
2764 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002765 Lex();
2766
2767 StringRef Directory;
2768 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002769 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002770 if (getLexer().is(AsmToken::String)) {
2771 if (FileNumber == -1)
2772 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002773 if (parseEscapedString(FilenameData))
2774 return true;
2775 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002776 Directory = Path;
2777 Lex();
2778 } else {
2779 Filename = Path;
2780 }
2781
2782 if (getLexer().isNot(AsmToken::EndOfStatement))
2783 return TokError("unexpected token in '.file' directive");
2784
2785 if (FileNumber == -1)
2786 getStreamer().EmitFileDirective(Filename);
2787 else {
2788 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002789 Error(DirectiveLoc,
2790 "input can't have .file dwarf directives when -g is "
2791 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002792
David Blaikiec714ef42014-03-17 01:52:11 +00002793 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2794 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002795 Error(FileNumberLoc, "file number already allocated");
2796 }
2797
2798 return false;
2799}
2800
Jim Grosbach4b905842013-09-20 23:08:21 +00002801/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002802/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002803bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002804 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2805 if (getLexer().isNot(AsmToken::Integer))
2806 return TokError("unexpected token in '.line' directive");
2807
2808 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002809 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002810 Lex();
2811
2812 // FIXME: Do something with the .line.
2813 }
2814
2815 if (getLexer().isNot(AsmToken::EndOfStatement))
2816 return TokError("unexpected token in '.line' directive");
2817
2818 return false;
2819}
2820
Jim Grosbach4b905842013-09-20 23:08:21 +00002821/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002822/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2823/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2824/// The first number is a file number, must have been previously assigned with
2825/// a .file directive, the second number is the line number and optionally the
2826/// third number is a column position (zero if not specified). The remaining
2827/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002828bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002829 if (getLexer().isNot(AsmToken::Integer))
2830 return TokError("unexpected token in '.loc' directive");
2831 int64_t FileNumber = getTok().getIntVal();
2832 if (FileNumber < 1)
2833 return TokError("file number less than one in '.loc' directive");
2834 if (!getContext().isValidDwarfFileNumber(FileNumber))
2835 return TokError("unassigned file number in '.loc' directive");
2836 Lex();
2837
2838 int64_t LineNumber = 0;
2839 if (getLexer().is(AsmToken::Integer)) {
2840 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002841 if (LineNumber < 0)
2842 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002843 Lex();
2844 }
2845
2846 int64_t ColumnPos = 0;
2847 if (getLexer().is(AsmToken::Integer)) {
2848 ColumnPos = getTok().getIntVal();
2849 if (ColumnPos < 0)
2850 return TokError("column position less than zero in '.loc' directive");
2851 Lex();
2852 }
2853
2854 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2855 unsigned Isa = 0;
2856 int64_t Discriminator = 0;
2857 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2858 for (;;) {
2859 if (getLexer().is(AsmToken::EndOfStatement))
2860 break;
2861
2862 StringRef Name;
2863 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002864 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002865 return TokError("unexpected token in '.loc' directive");
2866
2867 if (Name == "basic_block")
2868 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2869 else if (Name == "prologue_end")
2870 Flags |= DWARF2_FLAG_PROLOGUE_END;
2871 else if (Name == "epilogue_begin")
2872 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2873 else if (Name == "is_stmt") {
2874 Loc = getTok().getLoc();
2875 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002876 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return true;
2878 // The expression must be the constant 0 or 1.
2879 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2880 int Value = MCE->getValue();
2881 if (Value == 0)
2882 Flags &= ~DWARF2_FLAG_IS_STMT;
2883 else if (Value == 1)
2884 Flags |= DWARF2_FLAG_IS_STMT;
2885 else
2886 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002887 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002888 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2889 }
Craig Topperf15655b2013-04-22 04:22:40 +00002890 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 Loc = getTok().getLoc();
2892 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002893 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002894 return true;
2895 // The expression must be a constant greater or equal to 0.
2896 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2897 int Value = MCE->getValue();
2898 if (Value < 0)
2899 return Error(Loc, "isa number less than zero");
2900 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002901 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002902 return Error(Loc, "isa number not a constant value");
2903 }
Craig Topperf15655b2013-04-22 04:22:40 +00002904 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002905 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002907 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return Error(Loc, "unknown sub-directive in '.loc' directive");
2909 }
2910
2911 if (getLexer().is(AsmToken::EndOfStatement))
2912 break;
2913 }
2914 }
2915
2916 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2917 Isa, Discriminator, StringRef());
2918
2919 return false;
2920}
2921
Jim Grosbach4b905842013-09-20 23:08:21 +00002922/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002923/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002924bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002925 return TokError("unsupported directive '.stabs'");
2926}
2927
Jim Grosbach4b905842013-09-20 23:08:21 +00002928/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002929/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002930bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002931 StringRef Name;
2932 bool EH = false;
2933 bool Debug = false;
2934
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002935 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return TokError("Expected an identifier");
2937
2938 if (Name == ".eh_frame")
2939 EH = true;
2940 else if (Name == ".debug_frame")
2941 Debug = true;
2942
2943 if (getLexer().is(AsmToken::Comma)) {
2944 Lex();
2945
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002946 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002947 return TokError("Expected an identifier");
2948
2949 if (Name == ".eh_frame")
2950 EH = true;
2951 else if (Name == ".debug_frame")
2952 Debug = true;
2953 }
2954
2955 getStreamer().EmitCFISections(EH, Debug);
2956 return false;
2957}
2958
Jim Grosbach4b905842013-09-20 23:08:21 +00002959/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002960/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002961bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002962 StringRef Simple;
2963 if (getLexer().isNot(AsmToken::EndOfStatement))
2964 if (parseIdentifier(Simple) || Simple != "simple")
2965 return TokError("unexpected token in .cfi_startproc directive");
2966
2967 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002968 return false;
2969}
2970
Jim Grosbach4b905842013-09-20 23:08:21 +00002971/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002972/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002973bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002974 getStreamer().EmitCFIEndProc();
2975 return false;
2976}
2977
Jim Grosbach4b905842013-09-20 23:08:21 +00002978/// \brief parse register name or number.
2979bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002980 SMLoc DirectiveLoc) {
2981 unsigned RegNo;
2982
2983 if (getLexer().isNot(AsmToken::Integer)) {
2984 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2985 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002986 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002987 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002988 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002989
2990 return false;
2991}
2992
Jim Grosbach4b905842013-09-20 23:08:21 +00002993/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002994/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002995bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002996 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002997 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002998 return true;
2999
3000 if (getLexer().isNot(AsmToken::Comma))
3001 return TokError("unexpected token in directive");
3002 Lex();
3003
3004 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003005 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return true;
3007
3008 getStreamer().EmitCFIDefCfa(Register, Offset);
3009 return false;
3010}
3011
Jim Grosbach4b905842013-09-20 23:08:21 +00003012/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003013/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003014bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003015 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003016 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return true;
3018
3019 getStreamer().EmitCFIDefCfaOffset(Offset);
3020 return false;
3021}
3022
Jim Grosbach4b905842013-09-20 23:08:21 +00003023/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003024/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003025bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003026 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003027 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003028 return true;
3029
3030 if (getLexer().isNot(AsmToken::Comma))
3031 return TokError("unexpected token in directive");
3032 Lex();
3033
3034 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003035 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003036 return true;
3037
3038 getStreamer().EmitCFIRegister(Register1, Register2);
3039 return false;
3040}
3041
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003042/// parseDirectiveCFIWindowSave
3043/// ::= .cfi_window_save
3044bool AsmParser::parseDirectiveCFIWindowSave() {
3045 getStreamer().EmitCFIWindowSave();
3046 return false;
3047}
3048
Jim Grosbach4b905842013-09-20 23:08:21 +00003049/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003050/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003051bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003052 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003053 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003054 return true;
3055
3056 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3057 return false;
3058}
3059
Jim Grosbach4b905842013-09-20 23:08:21 +00003060/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003061/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003062bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003063 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003064 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003065 return true;
3066
3067 getStreamer().EmitCFIDefCfaRegister(Register);
3068 return false;
3069}
3070
Jim Grosbach4b905842013-09-20 23:08:21 +00003071/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003072/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003073bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003074 int64_t Register = 0;
3075 int64_t Offset = 0;
3076
Jim Grosbach4b905842013-09-20 23:08:21 +00003077 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003078 return true;
3079
3080 if (getLexer().isNot(AsmToken::Comma))
3081 return TokError("unexpected token in directive");
3082 Lex();
3083
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003084 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003085 return true;
3086
3087 getStreamer().EmitCFIOffset(Register, Offset);
3088 return false;
3089}
3090
Jim Grosbach4b905842013-09-20 23:08:21 +00003091/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003092/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003093bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003094 int64_t Register = 0;
3095
Jim Grosbach4b905842013-09-20 23:08:21 +00003096 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003097 return true;
3098
3099 if (getLexer().isNot(AsmToken::Comma))
3100 return TokError("unexpected token in directive");
3101 Lex();
3102
3103 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003104 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003105 return true;
3106
3107 getStreamer().EmitCFIRelOffset(Register, Offset);
3108 return false;
3109}
3110
3111static bool isValidEncoding(int64_t Encoding) {
3112 if (Encoding & ~0xff)
3113 return false;
3114
3115 if (Encoding == dwarf::DW_EH_PE_omit)
3116 return true;
3117
3118 const unsigned Format = Encoding & 0xf;
3119 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3120 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3121 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3122 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3123 return false;
3124
3125 const unsigned Application = Encoding & 0x70;
3126 if (Application != dwarf::DW_EH_PE_absptr &&
3127 Application != dwarf::DW_EH_PE_pcrel)
3128 return false;
3129
3130 return true;
3131}
3132
Jim Grosbach4b905842013-09-20 23:08:21 +00003133/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003134/// IsPersonality true for cfi_personality, false for cfi_lsda
3135/// ::= .cfi_personality encoding, [symbol_name]
3136/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003137bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003138 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003139 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003140 return true;
3141 if (Encoding == dwarf::DW_EH_PE_omit)
3142 return false;
3143
3144 if (!isValidEncoding(Encoding))
3145 return TokError("unsupported encoding.");
3146
3147 if (getLexer().isNot(AsmToken::Comma))
3148 return TokError("unexpected token in directive");
3149 Lex();
3150
3151 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003152 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003153 return TokError("expected identifier in directive");
3154
3155 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3156
3157 if (IsPersonality)
3158 getStreamer().EmitCFIPersonality(Sym, Encoding);
3159 else
3160 getStreamer().EmitCFILsda(Sym, Encoding);
3161 return false;
3162}
3163
Jim Grosbach4b905842013-09-20 23:08:21 +00003164/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003165/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003166bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003167 getStreamer().EmitCFIRememberState();
3168 return false;
3169}
3170
Jim Grosbach4b905842013-09-20 23:08:21 +00003171/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003172/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003173bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003174 getStreamer().EmitCFIRestoreState();
3175 return false;
3176}
3177
Jim Grosbach4b905842013-09-20 23:08:21 +00003178/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003179/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003180bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003181 int64_t Register = 0;
3182
Jim Grosbach4b905842013-09-20 23:08:21 +00003183 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003184 return true;
3185
3186 getStreamer().EmitCFISameValue(Register);
3187 return false;
3188}
3189
Jim Grosbach4b905842013-09-20 23:08:21 +00003190/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003191/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003192bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003193 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003194 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003195 return true;
3196
3197 getStreamer().EmitCFIRestore(Register);
3198 return false;
3199}
3200
Jim Grosbach4b905842013-09-20 23:08:21 +00003201/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003202/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003203bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003204 std::string Values;
3205 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003206 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003207 return true;
3208
3209 Values.push_back((uint8_t)CurrValue);
3210
3211 while (getLexer().is(AsmToken::Comma)) {
3212 Lex();
3213
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003214 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003215 return true;
3216
3217 Values.push_back((uint8_t)CurrValue);
3218 }
3219
3220 getStreamer().EmitCFIEscape(Values);
3221 return false;
3222}
3223
Jim Grosbach4b905842013-09-20 23:08:21 +00003224/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003225/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003226bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003227 if (getLexer().isNot(AsmToken::EndOfStatement))
3228 return Error(getLexer().getLoc(),
3229 "unexpected token in '.cfi_signal_frame'");
3230
3231 getStreamer().EmitCFISignalFrame();
3232 return false;
3233}
3234
Jim Grosbach4b905842013-09-20 23:08:21 +00003235/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003236/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003237bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003238 int64_t Register = 0;
3239
Jim Grosbach4b905842013-09-20 23:08:21 +00003240 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003241 return true;
3242
3243 getStreamer().EmitCFIUndefined(Register);
3244 return false;
3245}
3246
Jim Grosbach4b905842013-09-20 23:08:21 +00003247/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003248/// ::= .macros_on
3249/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003250bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003251 if (getLexer().isNot(AsmToken::EndOfStatement))
3252 return Error(getLexer().getLoc(),
3253 "unexpected token in '" + Directive + "' directive");
3254
Jim Grosbach4b905842013-09-20 23:08:21 +00003255 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003256 return false;
3257}
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003260/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003261bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003262 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003263 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003264 return TokError("expected identifier in '.macro' directive");
3265
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003266 if (getLexer().is(AsmToken::Comma))
3267 Lex();
3268
Eli Bendersky17233942013-01-15 22:59:42 +00003269 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003270 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003271
3272 if (Parameters.size() && Parameters.back().Vararg)
3273 return Error(Lexer.getLoc(),
3274 "Vararg parameter '" + Parameters.back().Name +
3275 "' should be last one in the list of parameters.");
3276
David Majnemer91fc4c22014-01-29 18:57:46 +00003277 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003278 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003279 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003280
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003281 if (Lexer.is(AsmToken::Colon)) {
3282 Lex(); // consume ':'
3283
3284 SMLoc QualLoc;
3285 StringRef Qualifier;
3286
3287 QualLoc = Lexer.getLoc();
3288 if (parseIdentifier(Qualifier))
3289 return Error(QualLoc, "missing parameter qualifier for "
3290 "'" + Parameter.Name + "' in macro '" + Name + "'");
3291
3292 if (Qualifier == "req")
3293 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003294 else if (Qualifier == "vararg" && !IsDarwin)
3295 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003296 else
3297 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3298 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3299 }
3300
David Majnemer91fc4c22014-01-29 18:57:46 +00003301 if (getLexer().is(AsmToken::Equal)) {
3302 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003303
3304 SMLoc ParamLoc;
3305
3306 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003307 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003308 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003309
3310 if (Parameter.Required)
3311 Warning(ParamLoc, "pointless default value for required parameter "
3312 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003313 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003314
3315 Parameters.push_back(Parameter);
3316
3317 if (getLexer().is(AsmToken::Comma))
3318 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003319 }
3320
3321 // Eat the end of statement.
3322 Lex();
3323
3324 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003325 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003326
3327 // Lex the macro definition.
3328 for (;;) {
3329 // Check whether we have reached the end of the file.
3330 if (getLexer().is(AsmToken::Eof))
3331 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3332
3333 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003334 if (getLexer().is(AsmToken::Identifier)) {
3335 if (getTok().getIdentifier() == ".endm" ||
3336 getTok().getIdentifier() == ".endmacro") {
3337 if (MacroDepth == 0) { // Outermost macro.
3338 EndToken = getTok();
3339 Lex();
3340 if (getLexer().isNot(AsmToken::EndOfStatement))
3341 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3342 "' directive");
3343 break;
3344 } else {
3345 // Otherwise we just found the end of an inner macro.
3346 --MacroDepth;
3347 }
3348 } else if (getTok().getIdentifier() == ".macro") {
3349 // We allow nested macros. Those aren't instantiated until the outermost
3350 // macro is expanded so just ignore them for now.
3351 ++MacroDepth;
3352 }
Eli Bendersky17233942013-01-15 22:59:42 +00003353 }
3354
3355 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003356 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003357 }
3358
Jim Grosbach4b905842013-09-20 23:08:21 +00003359 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003360 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3361 }
3362
3363 const char *BodyStart = StartToken.getLoc().getPointer();
3364 const char *BodyEnd = EndToken.getLoc().getPointer();
3365 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003366 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3367 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003368 return false;
3369}
3370
Jim Grosbach4b905842013-09-20 23:08:21 +00003371/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003372///
3373/// With the support added for named parameters there may be code out there that
3374/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003375/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003376/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003377/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3379/// warning that the positional parameter found in body which have no effect.
3380/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003381/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003382/// intended or change the macro to use the named parameters. It is possible
3383/// this warning will trigger when the none of the named parameters are used
3384/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003385void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003386 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003387 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388 // If this macro is not defined with named parameters the warning we are
3389 // checking for here doesn't apply.
3390 unsigned NParameters = Parameters.size();
3391 if (NParameters == 0)
3392 return;
3393
3394 bool NamedParametersFound = false;
3395 bool PositionalParametersFound = false;
3396
3397 // Look at the body of the macro for use of both the named parameters and what
3398 // are likely to be positional parameters. This is what expandMacro() is
3399 // doing when it finds the parameters in the body.
3400 while (!Body.empty()) {
3401 // Scan for the next possible parameter.
3402 std::size_t End = Body.size(), Pos = 0;
3403 for (; Pos != End; ++Pos) {
3404 // Check for a substitution or escape.
3405 // This macro is defined with parameters, look for \foo, \bar, etc.
3406 if (Body[Pos] == '\\' && Pos + 1 != End)
3407 break;
3408
3409 // This macro should have parameters, but look for $0, $1, ..., $n too.
3410 if (Body[Pos] != '$' || Pos + 1 == End)
3411 continue;
3412 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003413 if (Next == '$' || Next == 'n' ||
3414 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003415 break;
3416 }
3417
3418 // Check if we reached the end.
3419 if (Pos == End)
3420 break;
3421
3422 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003423 switch (Body[Pos + 1]) {
3424 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003425 case '$':
3426 break;
3427
Jim Grosbach4b905842013-09-20 23:08:21 +00003428 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003429 case 'n':
3430 PositionalParametersFound = true;
3431 break;
3432
Jim Grosbach4b905842013-09-20 23:08:21 +00003433 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003434 default: {
3435 PositionalParametersFound = true;
3436 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003437 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 }
3439 Pos += 2;
3440 } else {
3441 unsigned I = Pos + 1;
3442 while (isIdentifierChar(Body[I]) && I + 1 != End)
3443 ++I;
3444
Jim Grosbach4b905842013-09-20 23:08:21 +00003445 const char *Begin = Body.data() + Pos + 1;
3446 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003447 unsigned Index = 0;
3448 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003449 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003450 break;
3451
3452 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003453 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3454 Pos += 3;
3455 else {
3456 Pos = I;
3457 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003458 } else {
3459 NamedParametersFound = true;
3460 Pos += 1 + Argument.size();
3461 }
3462 }
3463 // Update the scan point.
3464 Body = Body.substr(Pos);
3465 }
3466
3467 if (!NamedParametersFound && PositionalParametersFound)
3468 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3469 "used in macro body, possible positional parameter "
3470 "found in body which will have no effect");
3471}
3472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003474/// ::= .endm
3475/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003476bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003477 if (getLexer().isNot(AsmToken::EndOfStatement))
3478 return TokError("unexpected token in '" + Directive + "' directive");
3479
3480 // If we are inside a macro instantiation, terminate the current
3481 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003482 if (isInsideMacroInstantiation()) {
3483 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003484 return false;
3485 }
3486
3487 // Otherwise, this .endmacro is a stray entry in the file; well formed
3488 // .endmacro directives are handled during the macro definition parsing.
3489 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003490 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003491}
3492
Jim Grosbach4b905842013-09-20 23:08:21 +00003493/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003494/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003495bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003496 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003497 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003498 return TokError("expected identifier in '.purgem' directive");
3499
3500 if (getLexer().isNot(AsmToken::EndOfStatement))
3501 return TokError("unexpected token in '.purgem' directive");
3502
Jim Grosbach4b905842013-09-20 23:08:21 +00003503 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003504 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3505
Jim Grosbach4b905842013-09-20 23:08:21 +00003506 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003507 return false;
3508}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003509
Jim Grosbach4b905842013-09-20 23:08:21 +00003510/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003511/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003512bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003513 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003514
3515 // Expect a single argument: an expression that evaluates to a constant
3516 // in the inclusive range 0-30.
3517 SMLoc ExprLoc = getLexer().getLoc();
3518 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003519 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003520 return true;
3521 else if (getLexer().isNot(AsmToken::EndOfStatement))
3522 return TokError("unexpected token after expression in"
3523 " '.bundle_align_mode' directive");
3524 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3525 return Error(ExprLoc,
3526 "invalid bundle alignment size (expected between 0 and 30)");
3527
3528 Lex();
3529
3530 // Because of AlignSizePow2's verified range we can safely truncate it to
3531 // unsigned.
3532 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3533 return false;
3534}
3535
Jim Grosbach4b905842013-09-20 23:08:21 +00003536/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003537/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003538bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003539 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003540 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003541
Eli Bendersky802b6282013-01-07 21:51:08 +00003542 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3543 StringRef Option;
3544 SMLoc Loc = getTok().getLoc();
3545 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003546 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003547
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003548 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003549 return Error(Loc, kInvalidOptionError);
3550
3551 if (Option != "align_to_end")
3552 return Error(Loc, kInvalidOptionError);
3553 else if (getLexer().isNot(AsmToken::EndOfStatement))
3554 return Error(Loc,
3555 "unexpected token after '.bundle_lock' directive option");
3556 AlignToEnd = true;
3557 }
3558
Eli Benderskyf483ff92012-12-20 19:05:53 +00003559 Lex();
3560
Eli Bendersky802b6282013-01-07 21:51:08 +00003561 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003562 return false;
3563}
3564
Jim Grosbach4b905842013-09-20 23:08:21 +00003565/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003566/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003567bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003568 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003569
3570 if (getLexer().isNot(AsmToken::EndOfStatement))
3571 return TokError("unexpected token in '.bundle_unlock' directive");
3572 Lex();
3573
3574 getStreamer().EmitBundleUnlock();
3575 return false;
3576}
3577
Jim Grosbach4b905842013-09-20 23:08:21 +00003578/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003579/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003580bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003581 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003582
3583 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003584 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003585 return true;
3586
3587 int64_t FillExpr = 0;
3588 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3589 if (getLexer().isNot(AsmToken::Comma))
3590 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3591 Lex();
3592
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003593 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003594 return true;
3595
3596 if (getLexer().isNot(AsmToken::EndOfStatement))
3597 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3598 }
3599
3600 Lex();
3601
3602 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003603 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3604 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003605
3606 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003607 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003608
3609 return false;
3610}
3611
Jim Grosbach4b905842013-09-20 23:08:21 +00003612/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003613/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003614bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003615 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003616 const MCExpr *Value;
3617
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003618 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003619 return true;
3620
3621 if (getLexer().isNot(AsmToken::EndOfStatement))
3622 return TokError("unexpected token in directive");
3623
3624 if (Signed)
3625 getStreamer().EmitSLEB128Value(Value);
3626 else
3627 getStreamer().EmitULEB128Value(Value);
3628
3629 return false;
3630}
3631
Jim Grosbach4b905842013-09-20 23:08:21 +00003632/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003633/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003634bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003635 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003636 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003637 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003638 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003639
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003640 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003641 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003642
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003643 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003644
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003645 // Assembler local symbols don't make any sense here. Complain loudly.
3646 if (Sym->isTemporary())
3647 return Error(Loc, "non-local symbol required in directive");
3648
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003649 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3650 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003651
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003652 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003653 break;
3654
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003655 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003656 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003657 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003658 }
3659 }
3660
Sean Callanan686ed8d2010-01-19 20:22:31 +00003661 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003662 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003663}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003664
Jim Grosbach4b905842013-09-20 23:08:21 +00003665/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003666/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003667bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003668 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003669
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003670 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003671 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003672 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003673 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003674
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003675 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003676 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003677
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003678 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003679 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003680 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003681
3682 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003683 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003684 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003685 return true;
3686
3687 int64_t Pow2Alignment = 0;
3688 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003689 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003690 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003691 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003692 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003693 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003694
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003695 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3696 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003697 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3698
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003699 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003700 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3701 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003702 if (!isPowerOf2_64(Pow2Alignment))
3703 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3704 Pow2Alignment = Log2_64(Pow2Alignment);
3705 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003706 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003707
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003708 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003709 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003710
Sean Callanan686ed8d2010-01-19 20:22:31 +00003711 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003712
Chris Lattner28ad7542009-07-09 17:25:12 +00003713 // NOTE: a size of zero for a .comm should create a undefined symbol
3714 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003716 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003717 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003718
Eric Christopherbc818852010-05-14 01:38:54 +00003719 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003720 // may internally end up wanting an alignment in bytes.
3721 // FIXME: Diagnose overflow.
3722 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003723 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003724 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003725
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003726 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003727 return Error(IDLoc, "invalid symbol redefinition");
3728
Chris Lattner28ad7542009-07-09 17:25:12 +00003729 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003730 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003731 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003732 return false;
3733 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003734
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003735 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736 return false;
3737}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003738
Jim Grosbach4b905842013-09-20 23:08:21 +00003739/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003740/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003741bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003742 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003743 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003744
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003745 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003746 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003747 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003748
Sean Callanan686ed8d2010-01-19 20:22:31 +00003749 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003750
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003751 if (Str.empty())
3752 Error(Loc, ".abort detected. Assembly stopping.");
3753 else
3754 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003755 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003756
3757 return false;
3758}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003759
Jim Grosbach4b905842013-09-20 23:08:21 +00003760/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003761/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003762bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003763 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003764 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003765
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003766 // Allow the strings to have escaped octal character sequence.
3767 std::string Filename;
3768 if (parseEscapedString(Filename))
3769 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003770 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003771 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003772
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003773 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003774 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003775
Chris Lattner693fbb82009-07-16 06:14:39 +00003776 // Attempt to switch the lexer to the included file before consuming the end
3777 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003778 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003779 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003780 return true;
3781 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003782
3783 return false;
3784}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003785
Jim Grosbach4b905842013-09-20 23:08:21 +00003786/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003787/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003788bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003789 if (getLexer().isNot(AsmToken::String))
3790 return TokError("expected string in '.incbin' directive");
3791
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003792 // Allow the strings to have escaped octal character sequence.
3793 std::string Filename;
3794 if (parseEscapedString(Filename))
3795 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003796 SMLoc IncbinLoc = getLexer().getLoc();
3797 Lex();
3798
3799 if (getLexer().isNot(AsmToken::EndOfStatement))
3800 return TokError("unexpected token in '.incbin' directive");
3801
Kevin Enderby109f25c2011-12-14 21:47:48 +00003802 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003803 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003804 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3805 return true;
3806 }
3807
3808 return false;
3809}
3810
Jim Grosbach4b905842013-09-20 23:08:21 +00003811/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003812/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3813bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003814 TheCondStack.push_back(TheCondState);
3815 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003816 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003817 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003818 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003819 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003820 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003821 return true;
3822
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003823 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003824 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003825
Sean Callanan686ed8d2010-01-19 20:22:31 +00003826 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003827
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003828 switch (DirKind) {
3829 default:
3830 llvm_unreachable("unsupported directive");
3831 case DK_IF:
3832 case DK_IFNE:
3833 break;
3834 case DK_IFEQ:
3835 ExprValue = ExprValue == 0;
3836 break;
3837 case DK_IFGE:
3838 ExprValue = ExprValue >= 0;
3839 break;
3840 case DK_IFGT:
3841 ExprValue = ExprValue > 0;
3842 break;
3843 case DK_IFLE:
3844 ExprValue = ExprValue <= 0;
3845 break;
3846 case DK_IFLT:
3847 ExprValue = ExprValue < 0;
3848 break;
3849 }
3850
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003851 TheCondState.CondMet = ExprValue;
3852 TheCondState.Ignore = !TheCondState.CondMet;
3853 }
3854
3855 return false;
3856}
3857
Jim Grosbach4b905842013-09-20 23:08:21 +00003858/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003859/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003860bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003861 TheCondStack.push_back(TheCondState);
3862 TheCondState.TheCond = AsmCond::IfCond;
3863
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003864 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003865 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003866 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003867 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003868
3869 if (getLexer().isNot(AsmToken::EndOfStatement))
3870 return TokError("unexpected token in '.ifb' directive");
3871
3872 Lex();
3873
3874 TheCondState.CondMet = ExpectBlank == Str.empty();
3875 TheCondState.Ignore = !TheCondState.CondMet;
3876 }
3877
3878 return false;
3879}
3880
Jim Grosbach4b905842013-09-20 23:08:21 +00003881/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003882/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003883/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003884bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003885 TheCondStack.push_back(TheCondState);
3886 TheCondState.TheCond = AsmCond::IfCond;
3887
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003888 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003889 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003890 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003891 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003892
3893 if (getLexer().isNot(AsmToken::Comma))
3894 return TokError("unexpected token in '.ifc' directive");
3895
3896 Lex();
3897
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003898 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003899
3900 if (getLexer().isNot(AsmToken::EndOfStatement))
3901 return TokError("unexpected token in '.ifc' directive");
3902
3903 Lex();
3904
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003905 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003906 TheCondState.Ignore = !TheCondState.CondMet;
3907 }
3908
3909 return false;
3910}
3911
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003912/// parseDirectiveIfeqs
3913/// ::= .ifeqs string1, string2
3914bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3915 if (Lexer.isNot(AsmToken::String)) {
3916 TokError("expected string parameter for '.ifeqs' directive");
3917 eatToEndOfStatement();
3918 return true;
3919 }
3920
3921 StringRef String1 = getTok().getStringContents();
3922 Lex();
3923
3924 if (Lexer.isNot(AsmToken::Comma)) {
3925 TokError("expected comma after first string for '.ifeqs' directive");
3926 eatToEndOfStatement();
3927 return true;
3928 }
3929
3930 Lex();
3931
3932 if (Lexer.isNot(AsmToken::String)) {
3933 TokError("expected string parameter for '.ifeqs' directive");
3934 eatToEndOfStatement();
3935 return true;
3936 }
3937
3938 StringRef String2 = getTok().getStringContents();
3939 Lex();
3940
3941 TheCondStack.push_back(TheCondState);
3942 TheCondState.TheCond = AsmCond::IfCond;
3943 TheCondState.CondMet = String1 == String2;
3944 TheCondState.Ignore = !TheCondState.CondMet;
3945
3946 return false;
3947}
3948
Jim Grosbach4b905842013-09-20 23:08:21 +00003949/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003950/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003951bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003952 StringRef Name;
3953 TheCondStack.push_back(TheCondState);
3954 TheCondState.TheCond = AsmCond::IfCond;
3955
3956 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003957 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003958 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003959 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003960 return TokError("expected identifier after '.ifdef'");
3961
3962 Lex();
3963
3964 MCSymbol *Sym = getContext().LookupSymbol(Name);
3965
3966 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003967 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003968 else
Craig Topper353eda42014-04-24 06:44:33 +00003969 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003970 TheCondState.Ignore = !TheCondState.CondMet;
3971 }
3972
3973 return false;
3974}
3975
Jim Grosbach4b905842013-09-20 23:08:21 +00003976/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003977/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003978bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003979 if (TheCondState.TheCond != AsmCond::IfCond &&
3980 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003981 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3982 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003983 TheCondState.TheCond = AsmCond::ElseIfCond;
3984
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 bool LastIgnoreState = false;
3986 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003987 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003988 if (LastIgnoreState || TheCondState.CondMet) {
3989 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003990 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003991 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003992 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003993 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003994 return true;
3995
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003996 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003997 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003998
Sean Callanan686ed8d2010-01-19 20:22:31 +00003999 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004000 TheCondState.CondMet = ExprValue;
4001 TheCondState.Ignore = !TheCondState.CondMet;
4002 }
4003
4004 return false;
4005}
4006
Jim Grosbach4b905842013-09-20 23:08:21 +00004007/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004009bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004010 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004011 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004012
Sean Callanan686ed8d2010-01-19 20:22:31 +00004013 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014
4015 if (TheCondState.TheCond != AsmCond::IfCond &&
4016 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004017 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4018 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004019 TheCondState.TheCond = AsmCond::ElseCond;
4020 bool LastIgnoreState = false;
4021 if (!TheCondStack.empty())
4022 LastIgnoreState = TheCondStack.back().Ignore;
4023 if (LastIgnoreState || TheCondState.CondMet)
4024 TheCondState.Ignore = true;
4025 else
4026 TheCondState.Ignore = false;
4027
4028 return false;
4029}
4030
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004031/// parseDirectiveEnd
4032/// ::= .end
4033bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4034 if (getLexer().isNot(AsmToken::EndOfStatement))
4035 return TokError("unexpected token in '.end' directive");
4036
4037 Lex();
4038
4039 while (Lexer.isNot(AsmToken::Eof))
4040 Lex();
4041
4042 return false;
4043}
4044
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004045/// parseDirectiveError
4046/// ::= .err
4047/// ::= .error [string]
4048bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4049 if (!TheCondStack.empty()) {
4050 if (TheCondStack.back().Ignore) {
4051 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004052 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004053 }
4054 }
4055
4056 if (!WithMessage)
4057 return Error(L, ".err encountered");
4058
4059 StringRef Message = ".error directive invoked in source file";
4060 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4061 if (Lexer.isNot(AsmToken::String)) {
4062 TokError(".error argument must be a string");
4063 eatToEndOfStatement();
4064 return true;
4065 }
4066
4067 Message = getTok().getStringContents();
4068 Lex();
4069 }
4070
4071 Error(L, Message);
4072 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004073}
4074
Jim Grosbach4b905842013-09-20 23:08:21 +00004075/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004076/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004077bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004078 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004079 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004080
Sean Callanan686ed8d2010-01-19 20:22:31 +00004081 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004082
Jim Grosbach4b905842013-09-20 23:08:21 +00004083 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004084 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4085 ".else");
4086 if (!TheCondStack.empty()) {
4087 TheCondState = TheCondStack.back();
4088 TheCondStack.pop_back();
4089 }
4090
4091 return false;
4092}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004093
Eli Bendersky17233942013-01-15 22:59:42 +00004094void AsmParser::initializeDirectiveKindMap() {
4095 DirectiveKindMap[".set"] = DK_SET;
4096 DirectiveKindMap[".equ"] = DK_EQU;
4097 DirectiveKindMap[".equiv"] = DK_EQUIV;
4098 DirectiveKindMap[".ascii"] = DK_ASCII;
4099 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4100 DirectiveKindMap[".string"] = DK_STRING;
4101 DirectiveKindMap[".byte"] = DK_BYTE;
4102 DirectiveKindMap[".short"] = DK_SHORT;
4103 DirectiveKindMap[".value"] = DK_VALUE;
4104 DirectiveKindMap[".2byte"] = DK_2BYTE;
4105 DirectiveKindMap[".long"] = DK_LONG;
4106 DirectiveKindMap[".int"] = DK_INT;
4107 DirectiveKindMap[".4byte"] = DK_4BYTE;
4108 DirectiveKindMap[".quad"] = DK_QUAD;
4109 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004110 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004111 DirectiveKindMap[".single"] = DK_SINGLE;
4112 DirectiveKindMap[".float"] = DK_FLOAT;
4113 DirectiveKindMap[".double"] = DK_DOUBLE;
4114 DirectiveKindMap[".align"] = DK_ALIGN;
4115 DirectiveKindMap[".align32"] = DK_ALIGN32;
4116 DirectiveKindMap[".balign"] = DK_BALIGN;
4117 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4118 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4119 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4120 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4121 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4122 DirectiveKindMap[".org"] = DK_ORG;
4123 DirectiveKindMap[".fill"] = DK_FILL;
4124 DirectiveKindMap[".zero"] = DK_ZERO;
4125 DirectiveKindMap[".extern"] = DK_EXTERN;
4126 DirectiveKindMap[".globl"] = DK_GLOBL;
4127 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004128 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4129 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4130 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4131 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4132 DirectiveKindMap[".reference"] = DK_REFERENCE;
4133 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4134 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4135 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4136 DirectiveKindMap[".comm"] = DK_COMM;
4137 DirectiveKindMap[".common"] = DK_COMMON;
4138 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4139 DirectiveKindMap[".abort"] = DK_ABORT;
4140 DirectiveKindMap[".include"] = DK_INCLUDE;
4141 DirectiveKindMap[".incbin"] = DK_INCBIN;
4142 DirectiveKindMap[".code16"] = DK_CODE16;
4143 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4144 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004145 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004146 DirectiveKindMap[".irp"] = DK_IRP;
4147 DirectiveKindMap[".irpc"] = DK_IRPC;
4148 DirectiveKindMap[".endr"] = DK_ENDR;
4149 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4150 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4151 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4152 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004153 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4154 DirectiveKindMap[".ifge"] = DK_IFGE;
4155 DirectiveKindMap[".ifgt"] = DK_IFGT;
4156 DirectiveKindMap[".ifle"] = DK_IFLE;
4157 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004158 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004159 DirectiveKindMap[".ifb"] = DK_IFB;
4160 DirectiveKindMap[".ifnb"] = DK_IFNB;
4161 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004162 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004163 DirectiveKindMap[".ifnc"] = DK_IFNC;
4164 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4165 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4166 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4167 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4168 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004169 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004170 DirectiveKindMap[".endif"] = DK_ENDIF;
4171 DirectiveKindMap[".skip"] = DK_SKIP;
4172 DirectiveKindMap[".space"] = DK_SPACE;
4173 DirectiveKindMap[".file"] = DK_FILE;
4174 DirectiveKindMap[".line"] = DK_LINE;
4175 DirectiveKindMap[".loc"] = DK_LOC;
4176 DirectiveKindMap[".stabs"] = DK_STABS;
4177 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4178 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4179 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4180 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4181 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4182 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4183 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4184 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4185 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4186 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4187 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4188 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4189 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4190 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4191 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4192 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4193 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4194 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4195 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4196 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4197 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004198 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004199 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4200 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4201 DirectiveKindMap[".macro"] = DK_MACRO;
4202 DirectiveKindMap[".endm"] = DK_ENDM;
4203 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4204 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004205 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004206 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004207}
4208
Jim Grosbach4b905842013-09-20 23:08:21 +00004209MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004210 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004211
Rafael Espindola34b9c512012-06-03 23:57:14 +00004212 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004213 for (;;) {
4214 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004215 if (getLexer().is(AsmToken::Eof)) {
4216 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004217 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004218 }
4219
Rafael Espindola34b9c512012-06-03 23:57:14 +00004220 if (Lexer.is(AsmToken::Identifier) &&
4221 (getTok().getIdentifier() == ".rept")) {
4222 ++NestLevel;
4223 }
4224
4225 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004226 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004227 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004228 EndToken = getTok();
4229 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004230 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4231 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004232 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004233 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004234 break;
4235 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004236 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004237 }
4238
Rafael Espindola34b9c512012-06-03 23:57:14 +00004239 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004240 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004241 }
4242
4243 const char *BodyStart = StartToken.getLoc().getPointer();
4244 const char *BodyEnd = EndToken.getLoc().getPointer();
4245 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4246
Rafael Espindola34b9c512012-06-03 23:57:14 +00004247 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004248 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004249 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004250}
4251
Jim Grosbach4b905842013-09-20 23:08:21 +00004252void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004253 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004254 OS << ".endr\n";
4255
4256 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004257 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004258
Rafael Espindola34b9c512012-06-03 23:57:14 +00004259 // Create the macro instantiation object and add to the current macro
4260 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004261 MacroInstantiation *MI = new MacroInstantiation(
4262 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004263 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004264
Rafael Espindola34b9c512012-06-03 23:57:14 +00004265 // Jump to the macro instantiation and prime the lexer.
4266 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4267 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4268 Lex();
4269}
4270
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004271/// parseDirectiveRept
4272/// ::= .rep | .rept count
4273bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004274 const MCExpr *CountExpr;
4275 SMLoc CountLoc = getTok().getLoc();
4276 if (parseExpression(CountExpr))
4277 return true;
4278
Rafael Espindola34b9c512012-06-03 23:57:14 +00004279 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004280 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4281 eatToEndOfStatement();
4282 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4283 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004284
4285 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004286 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004287
4288 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004289 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004290
4291 // Eat the end of statement.
4292 Lex();
4293
4294 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004295 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004296 if (!M)
4297 return true;
4298
4299 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4300 // to hold the macro body with substitutions.
4301 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004302 raw_svector_ostream OS(Buf);
4303 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004304 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004305 return true;
4306 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004307 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004308
4309 return false;
4310}
4311
Jim Grosbach4b905842013-09-20 23:08:21 +00004312/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004313/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004314bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004315 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004316
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004317 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004318 return TokError("expected identifier in '.irp' directive");
4319
Rafael Espindola768b41c2012-06-15 14:02:34 +00004320 if (Lexer.isNot(AsmToken::Comma))
4321 return TokError("expected comma in '.irp' directive");
4322
4323 Lex();
4324
Eli Bendersky38274122013-01-14 23:22:36 +00004325 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004326 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004327 return true;
4328
4329 // Eat the end of statement.
4330 Lex();
4331
4332 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004333 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004334 if (!M)
4335 return true;
4336
4337 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4338 // to hold the macro body with substitutions.
4339 SmallString<256> Buf;
4340 raw_svector_ostream OS(Buf);
4341
Eli Bendersky38274122013-01-14 23:22:36 +00004342 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004343 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004344 return true;
4345 }
4346
Jim Grosbach4b905842013-09-20 23:08:21 +00004347 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004348
4349 return false;
4350}
4351
Jim Grosbach4b905842013-09-20 23:08:21 +00004352/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004353/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004354bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004355 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004356
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004357 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004358 return TokError("expected identifier in '.irpc' directive");
4359
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004360 if (Lexer.isNot(AsmToken::Comma))
4361 return TokError("expected comma in '.irpc' directive");
4362
4363 Lex();
4364
Eli Bendersky38274122013-01-14 23:22:36 +00004365 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004366 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004367 return true;
4368
4369 if (A.size() != 1 || A.front().size() != 1)
4370 return TokError("unexpected token in '.irpc' directive");
4371
4372 // Eat the end of statement.
4373 Lex();
4374
4375 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004376 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004377 if (!M)
4378 return true;
4379
4380 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4381 // to hold the macro body with substitutions.
4382 SmallString<256> Buf;
4383 raw_svector_ostream OS(Buf);
4384
4385 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004386 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004387 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004388 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004389
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004390 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004391 return true;
4392 }
4393
Jim Grosbach4b905842013-09-20 23:08:21 +00004394 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004395
4396 return false;
4397}
4398
Jim Grosbach4b905842013-09-20 23:08:21 +00004399bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004400 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004401 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004402
4403 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004404 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004405 assert(getLexer().is(AsmToken::EndOfStatement));
4406
Jim Grosbach4b905842013-09-20 23:08:21 +00004407 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004408 return false;
4409}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004410
Jim Grosbach4b905842013-09-20 23:08:21 +00004411bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004412 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004413 const MCExpr *Value;
4414 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004415 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004416 return true;
4417 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4418 if (!MCE)
4419 return Error(ExprLoc, "unexpected expression in _emit");
4420 uint64_t IntValue = MCE->getValue();
4421 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4422 return Error(ExprLoc, "literal value out of range for directive");
4423
Chad Rosierc7f552c2013-02-12 21:33:51 +00004424 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4425 return false;
4426}
4427
Jim Grosbach4b905842013-09-20 23:08:21 +00004428bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004429 const MCExpr *Value;
4430 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004431 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004432 return true;
4433 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4434 if (!MCE)
4435 return Error(ExprLoc, "unexpected expression in align");
4436 uint64_t IntValue = MCE->getValue();
4437 if (!isPowerOf2_64(IntValue))
4438 return Error(ExprLoc, "literal value not a power of two greater then zero");
4439
Jim Grosbach4b905842013-09-20 23:08:21 +00004440 Info.AsmRewrites->push_back(
4441 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004442 return false;
4443}
4444
Chad Rosierf43fcf52013-02-13 21:27:17 +00004445// We are comparing pointers, but the pointers are relative to a single string.
4446// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004447static int rewritesSort(const AsmRewrite *AsmRewriteA,
4448 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004449 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4450 return -1;
4451 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4452 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004453
Chad Rosierfce4fab2013-04-08 17:43:47 +00004454 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4455 // rewrite to the same location. Make sure the SizeDirective rewrite is
4456 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4457 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004458 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4459 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004460 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004461
Jim Grosbach4b905842013-09-20 23:08:21 +00004462 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4463 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004464 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004465 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004466}
4467
Jim Grosbach4b905842013-09-20 23:08:21 +00004468bool AsmParser::parseMSInlineAsm(
4469 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4470 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4471 SmallVectorImpl<std::string> &Constraints,
4472 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4473 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004474 SmallVector<void *, 4> InputDecls;
4475 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004476 SmallVector<bool, 4> InputDeclsAddressOf;
4477 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004478 SmallVector<std::string, 4> InputConstraints;
4479 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004480 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004481
Benjamin Kramer1a136112013-02-15 20:37:21 +00004482 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004483
4484 // Prime the lexer.
4485 Lex();
4486
4487 // While we have input, parse each statement.
4488 unsigned InputIdx = 0;
4489 unsigned OutputIdx = 0;
4490 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004491 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004492 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004493 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004494
Chad Rosier149e8e02012-12-12 22:45:52 +00004495 if (Info.ParseError)
4496 return true;
4497
Benjamin Kramer1a136112013-02-15 20:37:21 +00004498 if (Info.Opcode == ~0U)
4499 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004500
Benjamin Kramer1a136112013-02-15 20:37:21 +00004501 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004502
Benjamin Kramer1a136112013-02-15 20:37:21 +00004503 // Build the list of clobbers, outputs and inputs.
4504 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004505 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004506
Benjamin Kramer1a136112013-02-15 20:37:21 +00004507 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004508 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004509 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004510
Benjamin Kramer1a136112013-02-15 20:37:21 +00004511 // Register operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00004512 if (Operand.isReg() && !Operand.needAddressOf()) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004513 unsigned NumDefs = Desc.getNumDefs();
4514 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004515 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4516 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004517 continue;
4518 }
4519
4520 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004521 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004522 if (SymName.empty())
4523 continue;
4524
David Blaikie960ea3f2014-06-08 16:18:35 +00004525 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004526 if (!OpDecl)
4527 continue;
4528
4529 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004530 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004531 if (isOutput) {
4532 ++InputIdx;
4533 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004534 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4535 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004536 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004537 } else {
4538 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004539 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4540 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004541 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004542 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004543 }
Reid Kleckneree088972013-12-10 18:27:32 +00004544
4545 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004546 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4547 Desc.getNumImplicitDefs());
4548 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004549 }
4550
4551 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004552 NumOutputs = OutputDecls.size();
4553 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004554
4555 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004556 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4557 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4558 ClobberRegs.end());
4559 Clobbers.assign(ClobberRegs.size(), std::string());
4560 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4561 raw_string_ostream OS(Clobbers[I]);
4562 IP->printRegName(OS, ClobberRegs[I]);
4563 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004564
4565 // Merge the various outputs and inputs. Output are expected first.
4566 if (NumOutputs || NumInputs) {
4567 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004568 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004569 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004570 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004571 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004572 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004573 }
4574 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004575 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004576 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004577 }
4578 }
4579
4580 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004581 std::string AsmStringIR;
4582 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004583 StringRef ASMString =
4584 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4585 const char *AsmStart = ASMString.begin();
4586 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004587 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004588 for (const AsmRewrite &AR : AsmStrRewrites) {
4589 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004590 if (Kind == AOK_Delete)
4591 continue;
4592
David Majnemer8114c1a2014-06-23 02:17:16 +00004593 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004594 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004595
Chad Rosier120eefd2013-03-19 17:32:17 +00004596 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004597 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004598 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004599
Chad Rosier37e755c2012-10-23 17:43:43 +00004600 // Skip the original expression.
4601 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004602 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004603 continue;
4604 }
4605
Chad Rosierff10ed12013-04-12 16:26:42 +00004606 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004607 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004608 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004609 default:
4610 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004611 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004612 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004613 break;
4614 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004616 break;
4617 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004618 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004619 break;
4620 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004621 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004622 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004623 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004624 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004625 default: break;
4626 case 8: OS << "byte ptr "; break;
4627 case 16: OS << "word ptr "; break;
4628 case 32: OS << "dword ptr "; break;
4629 case 64: OS << "qword ptr "; break;
4630 case 80: OS << "xword ptr "; break;
4631 case 128: OS << "xmmword ptr "; break;
4632 case 256: OS << "ymmword ptr "; break;
4633 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004634 break;
4635 case AOK_Emit:
4636 OS << ".byte";
4637 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004638 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004639 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004640 OS << ".align " << Val;
4641
4642 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004643 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004644 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4645 break;
4646 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004647 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004648 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004649 OS.flush();
4650 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004651 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004652 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004653 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004654 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004655
Chad Rosier8bce6642012-10-18 15:49:34 +00004656 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004657 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004658 }
4659
4660 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004661 if (AsmStart != AsmEnd)
4662 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004663
4664 AsmString = OS.str();
4665 return false;
4666}
4667
Daniel Dunbar01e36072010-07-17 02:26:10 +00004668/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004669MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4670 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004671 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004672}