blob: 50375d37d841e7cda66b7a0cfee728a18eb7148f [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.
136 int CurBuffer;
137
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;
Kevin Enderby27121c12012-11-05 21:55:41 +0000165 int 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;
171 int LastQueryBuffer;
172 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 ///
313 /// \param InBuffer If not -1, should be the known buffer id that contains the
314 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000315 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
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),
Craig Topper353eda42014-04-24 06:44:33 +0000494 PlatformParser(nullptr), CurBuffer(0), MacrosEnabledFlag(true),
Saleem Abdulrasool9dd60cf2014-05-22 06:02:59 +0000495 HadError(false), CppHashLineNumber(0), AssemblerDialect(~0U),
496 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;
569 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 if (NewBuf == -1)
571 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000572
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000574
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000576
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 return false;
578}
Daniel Dunbar43235712010-07-18 18:54:11 +0000579
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000580/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000581/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000582/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000583bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 std::string IncludedFile;
585 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (NewBuf == -1)
587 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
Jim Grosbach4b905842013-09-20 23:08:21 +0000594void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000595 if (InBuffer != -1) {
596 CurBuffer = InBuffer;
597 } else {
598 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
599 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000600 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
601}
602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603const AsmToken &AsmParser::Lex() {
604 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 if (tok->is(AsmToken::Eof)) {
607 // If this is the end of an included file, pop the parent file off the
608 // include stack.
609 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
610 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000611 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 tok = &Lexer.Lex();
613 }
614 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000617 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000620}
621
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000622bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000623 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000624 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000625 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000626
Chris Lattner36e02122009-06-21 20:54:55 +0000627 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000628 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000629
630 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000631 AsmCond StartingCondState = TheCondState;
632
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 // If we are generating dwarf for assembly source files save the initial text
634 // section and generate a .file directive.
635 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000636 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
637 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000638 auto InsertResult = getContext().addGenDwarfSection(
639 getStreamer().getCurrentSection().first);
640 assert(InsertResult.second && ".text section should not have debug info yet");
641 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000642 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
643 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000644 }
645
Chris Lattner73f36112009-07-02 21:53:43 +0000646 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000647 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000648 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000649 if (!parseStatement(Info))
650 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000651
Daniel Dunbar43325c42010-09-09 22:42:56 +0000652 // We had an error, validate that one was emitted and recover by skipping to
653 // the next line.
654 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000655 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000656 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000657
658 if (TheCondState.TheCond != StartingCondState.TheCond ||
659 TheCondState.Ignore != StartingCondState.Ignore)
660 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661
662 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000663 const auto &LineTables = getContext().getMCDwarfLineTables();
664 if (!LineTables.empty()) {
665 unsigned Index = 0;
666 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
667 if (File.Name.empty() && Index != 0)
668 TokError("unassigned file number: " + Twine(Index) +
669 " for .file directives");
670 ++Index;
671 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000672 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000673
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000674 // Check to see that all assembler local symbols were actually defined.
675 // Targets that don't do subsections via symbols may not want this, though,
676 // so conservatively exclude them. Only do this if we're finalizing, though,
677 // as otherwise we won't necessarilly have seen everything yet.
678 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
679 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
680 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000681 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000682 i != e; ++i) {
683 MCSymbol *Sym = i->getValue();
684 // Variable symbols may not be marked as defined, so check those
685 // explicitly. If we know it's a variable, we have a definition for
686 // the purposes of this check.
687 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
688 // FIXME: We would really like to refer back to where the symbol was
689 // first referenced for a source location. We need to add something
690 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000691 printMessage(
692 getLexer().getLoc(), SourceMgr::DK_Error,
693 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000694 }
695 }
696
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000697 // Finalize the output stream if there are no errors and if the client wants
698 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000699 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000700 Out.Finish();
701
Chris Lattner73f36112009-07-02 21:53:43 +0000702 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000703}
704
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000705void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000706 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000708 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000709 }
710}
711
Jim Grosbach4b905842013-09-20 23:08:21 +0000712/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000715 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000716
Chris Lattnere5074c42009-06-22 01:29:09 +0000717 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000718 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000719 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000720}
721
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000722StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 const char *Start = getTok().getLoc().getPointer();
724
Jim Grosbach4b905842013-09-20 23:08:21 +0000725 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000726 Lex();
727
728 const char *End = getTok().getLoc().getPointer();
729 return StringRef(Start, End - Start);
730}
Chris Lattner78db3622009-06-22 05:51:26 +0000731
Jim Grosbach4b905842013-09-20 23:08:21 +0000732StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000733 const char *Start = getTok().getLoc().getPointer();
734
735 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000736 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000737 Lex();
738
739 const char *End = getTok().getLoc().getPointer();
740 return StringRef(Start, End - Start);
741}
742
Jim Grosbach4b905842013-09-20 23:08:21 +0000743/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000744/// NOTE: This assumes the leading '(' has already been consumed.
745///
746/// parenexpr ::= expr)
747///
Jim Grosbach4b905842013-09-20 23:08:21 +0000748bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
749 if (parseExpression(Res))
750 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000751 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000753 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000754 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000755 return false;
756}
Chris Lattner78db3622009-06-22 05:51:26 +0000757
Jim Grosbach4b905842013-09-20 23:08:21 +0000758/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000759/// NOTE: This assumes the leading '[' has already been consumed.
760///
761/// bracketexpr ::= expr]
762///
Jim Grosbach4b905842013-09-20 23:08:21 +0000763bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
764 if (parseExpression(Res))
765 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 if (Lexer.isNot(AsmToken::RBrac))
767 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000768 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000769 Lex();
770 return false;
771}
772
Jim Grosbach4b905842013-09-20 23:08:21 +0000773/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000774/// primaryexpr ::= (parenexpr
775/// primaryexpr ::= symbol
776/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000777/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000778/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000779bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000780 SMLoc FirstTokenLoc = getLexer().getLoc();
781 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
782 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000783 default:
784 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000785 // If we have an error assume that we've already handled it.
786 case AsmToken::Error:
787 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000788 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000789 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000790 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000792 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000793 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000794 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000795 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000796 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000797 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000798 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000799 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000800 if (FirstTokenKind == AsmToken::Dollar) {
801 if (Lexer.getMAI().getDollarIsPC()) {
802 // This is a '$' reference, which references the current PC. Emit a
803 // temporary label to the streamer and refer to it.
804 MCSymbol *Sym = Ctx.CreateTempSymbol();
805 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000806 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
807 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000808 EndLoc = FirstTokenLoc;
809 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000810 }
811 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000812 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000813 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000814 // Parse symbol variant
815 std::pair<StringRef, StringRef> Split;
816 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000817 if (FirstTokenKind == AsmToken::String) {
818 if (Lexer.is(AsmToken::At)) {
819 Lexer.Lex(); // eat @
820 SMLoc AtLoc = getLexer().getLoc();
821 StringRef VName;
822 if (parseIdentifier(VName))
823 return Error(AtLoc, "expected symbol variant after '@'");
824
825 Split = std::make_pair(Identifier, VName);
826 }
827 } else {
828 Split = Identifier.split('@');
829 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000830 } else if (Lexer.is(AsmToken::LParen)) {
831 Lexer.Lex(); // eat (
832 StringRef VName;
833 parseIdentifier(VName);
834 if (Lexer.isNot(AsmToken::RParen)) {
835 return Error(Lexer.getTok().getLoc(),
836 "unexpected token in variant, expected ')'");
837 }
838 Lexer.Lex(); // eat )
839 Split = std::make_pair(Identifier, VName);
840 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000841
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000842 EndLoc = SMLoc::getFromPointer(Identifier.end());
843
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 StringRef SymbolName = Identifier;
846 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000847
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000848 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000849 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000850 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 if (Variant != MCSymbolRefExpr::VK_Invalid) {
852 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000853 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000854 Variant = MCSymbolRefExpr::VK_None;
855 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000856 return Error(SMLoc::getFromPointer(Split.second.begin()),
857 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000858 }
859 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000860
Hans Wennborgce69d772013-10-18 20:46:28 +0000861 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
862
Daniel Dunbard20cda02009-10-16 01:34:54 +0000863 // If this is an absolute variable reference, substitute it now to preserve
864 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000865 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000866 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000867 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000868
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000869 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000870 return false;
871 }
872
873 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000874 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000875 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000876 }
David Woodhousef42a6662014-02-01 16:20:54 +0000877 case AsmToken::BigNum:
878 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000879 case AsmToken::Integer: {
880 SMLoc Loc = getTok().getLoc();
881 int64_t IntVal = getTok().getIntVal();
882 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000883 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000884 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 // Look for 'b' or 'f' following an Integer as a directional label
886 if (Lexer.getKind() == AsmToken::Identifier) {
887 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 // Lookup the symbol variant if used.
889 std::pair<StringRef, StringRef> Split = IDVal.split('@');
890 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
891 if (Split.first.size() != IDVal.size()) {
892 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000893 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000895 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000897 if (IDVal == "f" || IDVal == "b") {
898 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000899 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000900 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000901 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 Lex(); // Eat identifier.
905 }
906 }
Chris Lattner78db3622009-06-22 05:51:26 +0000907 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000908 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 case AsmToken::Real: {
910 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000911 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000912 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000913 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000914 Lex(); // Eat token.
915 return false;
916 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000917 case AsmToken::Dot: {
918 // This is a '.' reference, which references the current PC. Emit a
919 // temporary label to the streamer and refer to it.
920 MCSymbol *Sym = Ctx.CreateTempSymbol();
921 Out.EmitLabel(Sym);
922 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000923 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000924 Lex(); // Eat identifier.
925 return false;
926 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000930 case AsmToken::LBrac:
931 if (!PlatformParser->HasBracketExpressions())
932 return TokError("brackets expression not supported on this target");
933 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000945 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000947 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000948 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000949 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000950 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000951 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000953 }
954}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000955
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000956bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000957 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000958 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000959}
960
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000962AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000964 // Ask the target implementation about this expression first.
965 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
966 if (NewE)
967 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968 // Recurse over the given expression, rebuilding it to apply the given variant
969 // if there is exactly one symbol.
970 switch (E->getKind()) {
971 case MCExpr::Target:
972 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000973 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000974
975 case MCExpr::SymbolRef: {
976 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
977
978 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 TokError("invalid variant on expression '" + getTok().getIdentifier() +
980 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 return E;
982 }
983
984 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
985 }
986
987 case MCExpr::Unary: {
988 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000991 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
993 }
994
995 case MCExpr::Binary: {
996 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
998 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
1000 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001001 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002
Jim Grosbach4b905842013-09-20 23:08:21 +00001003 if (!LHS)
1004 LHS = BE->getLHS();
1005 if (!RHS)
1006 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007
1008 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1009 }
1010 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001011
Craig Toppera2886c22012-02-07 05:05:23 +00001012 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013}
1014
Jim Grosbach4b905842013-09-20 23:08:21 +00001015/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001016///
Jim Grosbachbd164242011-08-20 16:24:13 +00001017/// expr ::= expr &&,|| expr -> lowest.
1018/// expr ::= expr |,^,&,! expr
1019/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1020/// expr ::= expr <<,>> expr
1021/// expr ::= expr +,- expr
1022/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001023/// expr ::= primaryexpr
1024///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001025bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001026 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001027 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001029 return true;
1030
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 // As a special case, we support 'a op b @ modifier' by rewriting the
1032 // expression to include the modifier. This is inefficient, but in general we
1033 // expect users to use 'a@modifier op b'.
1034 if (Lexer.getKind() == AsmToken::At) {
1035 Lex();
1036
1037 if (Lexer.isNot(AsmToken::Identifier))
1038 return TokError("unexpected symbol modifier following '@'");
1039
1040 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042 if (Variant == MCSymbolRefExpr::VK_Invalid)
1043 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1044
Jim Grosbach4b905842013-09-20 23:08:21 +00001045 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 if (!ModifiedRes) {
1047 return TokError("invalid modifier '" + getTok().getIdentifier() +
1048 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001050
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 Res = ModifiedRes;
1052 Lex();
1053 }
1054
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001055 // Try to constant fold it up front, if possible.
1056 int64_t Value;
1057 if (Res->EvaluateAsAbsolute(Value))
1058 Res = MCConstantExpr::Create(Value, getContext());
1059
1060 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001061}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001064 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001065 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001066}
1067
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001068bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001069 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070
Daniel Dunbar75630b32009-06-30 02:10:03 +00001071 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001072 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073 return true;
1074
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001075 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001076 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001077
1078 return false;
1079}
1080
Michael J. Spencer530ce852010-10-09 11:00:50 +00001081static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001083 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001084 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001086
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001090 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001091 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001093 return 1;
1094
Jim Grosbach4b905842013-09-20 23:08:21 +00001095 // Low Precedence: |, &, ^
1096 //
1097 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001104 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001105 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001106 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001107
Jim Grosbach4b905842013-09-20 23:08:21 +00001108 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001109 case AsmToken::EqualEqual:
1110 Kind = MCBinaryExpr::EQ;
1111 return 3;
1112 case AsmToken::ExclaimEqual:
1113 case AsmToken::LessGreater:
1114 Kind = MCBinaryExpr::NE;
1115 return 3;
1116 case AsmToken::Less:
1117 Kind = MCBinaryExpr::LT;
1118 return 3;
1119 case AsmToken::LessEqual:
1120 Kind = MCBinaryExpr::LTE;
1121 return 3;
1122 case AsmToken::Greater:
1123 Kind = MCBinaryExpr::GT;
1124 return 3;
1125 case AsmToken::GreaterEqual:
1126 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001127 return 3;
1128
Jim Grosbach4b905842013-09-20 23:08:21 +00001129 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 case AsmToken::LessLess:
1131 Kind = MCBinaryExpr::Shl;
1132 return 4;
1133 case AsmToken::GreaterGreater:
1134 Kind = MCBinaryExpr::Shr;
1135 return 4;
1136
Jim Grosbach4b905842013-09-20 23:08:21 +00001137 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138 case AsmToken::Plus:
1139 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001141 case AsmToken::Minus:
1142 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001144
Jim Grosbach4b905842013-09-20 23:08:21 +00001145 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001149 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001151 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001152 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001153 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001154 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 }
1156}
1157
Jim Grosbach4b905842013-09-20 23:08:21 +00001158/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001160bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001161 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001163 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001164 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166 // If the next token is lower precedence than we are allowed to eat, return
1167 // successfully with what we ate already.
1168 if (TokPrec < Precedence)
1169 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001170
Sean Callanan686ed8d2010-01-19 20:22:31 +00001171 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001174 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001175 if (parsePrimaryExpr(RHS, EndLoc))
1176 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001177
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178 // If BinOp binds less tightly with RHS than the operator after RHS, let
1179 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001181 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001182 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1183 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001184
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001185 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001186 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001187 }
1188}
1189
Chris Lattner36e02122009-06-21 20:54:55 +00001190/// ParseStatement:
1191/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001192/// ::= Label* Directive ...Operands... EndOfStatement
1193/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001194bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001195 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001196 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001197 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001198 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001199 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001200
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001201 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001202 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001203 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001204 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001206 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001207 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001208 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001209
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001210 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001211 if (Lexer.is(AsmToken::Integer)) {
1212 LocalLabelVal = getTok().getIntVal();
1213 if (LocalLabelVal < 0) {
1214 if (!TheCondState.Ignore)
1215 return TokError("unexpected token at start of statement");
1216 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001217 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001218 IDVal = getTok().getString();
1219 Lex(); // Consume the integer token to be used as an identifier token.
1220 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001221 if (!TheCondState.Ignore)
1222 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001223 }
1224 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001225 } else if (Lexer.is(AsmToken::Dot)) {
1226 // Treat '.' as a valid identifier in this context.
1227 Lex();
1228 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001229 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001230 if (!TheCondState.Ignore)
1231 return TokError("unexpected token at start of statement");
1232 IDVal = "";
1233 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001234
Chris Lattner926885c2010-04-17 18:14:27 +00001235 // Handle conditional assembly here before checking for skipping. We
1236 // have to do this so that .endif isn't skipped in a ".if 0" block for
1237 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001238 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001239 DirectiveKindMap.find(IDVal);
1240 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1241 ? DK_NO_DIRECTIVE
1242 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001243 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 default:
1245 break;
1246 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001247 case DK_IFEQ:
1248 case DK_IFGE:
1249 case DK_IFGT:
1250 case DK_IFLE:
1251 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001252 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001253 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001254 case DK_IFB:
1255 return parseDirectiveIfb(IDLoc, true);
1256 case DK_IFNB:
1257 return parseDirectiveIfb(IDLoc, false);
1258 case DK_IFC:
1259 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001260 case DK_IFEQS:
1261 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001262 case DK_IFNC:
1263 return parseDirectiveIfc(IDLoc, false);
1264 case DK_IFDEF:
1265 return parseDirectiveIfdef(IDLoc, true);
1266 case DK_IFNDEF:
1267 case DK_IFNOTDEF:
1268 return parseDirectiveIfdef(IDLoc, false);
1269 case DK_ELSEIF:
1270 return parseDirectiveElseIf(IDLoc);
1271 case DK_ELSE:
1272 return parseDirectiveElse(IDLoc);
1273 case DK_ENDIF:
1274 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001275 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001276
Eli Bendersky88024712013-01-16 19:32:36 +00001277 // Ignore the statement if in the middle of inactive conditional
1278 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001279 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001280 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001281 return false;
1282 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001283
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001284 // FIXME: Recurse on local labels?
1285
1286 // See what kind of statement we have.
1287 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001288 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001289 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001290
Chris Lattner36e02122009-06-21 20:54:55 +00001291 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001292 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001293
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001294 // Diagnose attempt to use '.' as a label.
1295 if (IDVal == ".")
1296 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1297
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001298 // Diagnose attempt to use a variable as a label.
1299 //
1300 // FIXME: Diagnostics. Note the location of the definition as a label.
1301 // FIXME: This doesn't diagnose assignment to a symbol which has been
1302 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001303 MCSymbol *Sym;
1304 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001305 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001306 else
1307 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001308 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001309 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310
Daniel Dunbare73b2672009-08-26 22:13:22 +00001311 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001312 if (!ParsingInlineAsm)
1313 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001314
Kevin Enderbye7739d42011-12-09 18:09:40 +00001315 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001316 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001318 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1319 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001320
Tim Northover1744d0a2013-10-25 12:49:50 +00001321 getTargetParser().onLabelParsed(Sym);
1322
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001323 // Consume any end of statement token, if present, to avoid spurious
1324 // AddBlankLine calls().
1325 if (Lexer.is(AsmToken::EndOfStatement)) {
1326 Lex();
1327 if (Lexer.is(AsmToken::Eof))
1328 return false;
1329 }
1330
Eli Friedman0f4871d2012-10-22 23:58:19 +00001331 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001332 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001334 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001336 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337
Jim Grosbach4b905842013-09-20 23:08:21 +00001338 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001339
1340 default: // Normal instruction or directive.
1341 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001342 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001343
1344 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001345 if (areMacrosEnabled())
1346 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1347 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001348 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001349
Michael J. Spencer530ce852010-10-09 11:00:50 +00001350 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001351
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001353 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001355 //
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // 1. The target-specific assembly parser. Some directives are target
1357 // specific or may potentially behave differently on certain targets.
1358 // 2. Asm parser extensions. For example, platform-specific parsers
1359 // (like the ELF parser) register themselves as extensions.
1360 // 3. The generic directive parser implemented by this class. These are
1361 // all the directives that behave in a target and platform independent
1362 // manner, or at least have a default behavior that's shared between
1363 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001364
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // First query the target-specific parser. It will return 'true' if it
1366 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001367 if (!getTargetParser().ParseDirective(ID))
1368 return false;
1369
Alp Tokercb402912014-01-24 17:20:08 +00001370 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001371 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001372 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1373 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001374 if (Handler.first)
1375 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1376
1377 // Finally, if no one else is interested in this directive, it must be
1378 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001379 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001380 default:
1381 break;
1382 case DK_SET:
1383 case DK_EQU:
1384 return parseDirectiveSet(IDVal, true);
1385 case DK_EQUIV:
1386 return parseDirectiveSet(IDVal, false);
1387 case DK_ASCII:
1388 return parseDirectiveAscii(IDVal, false);
1389 case DK_ASCIZ:
1390 case DK_STRING:
1391 return parseDirectiveAscii(IDVal, true);
1392 case DK_BYTE:
1393 return parseDirectiveValue(1);
1394 case DK_SHORT:
1395 case DK_VALUE:
1396 case DK_2BYTE:
1397 return parseDirectiveValue(2);
1398 case DK_LONG:
1399 case DK_INT:
1400 case DK_4BYTE:
1401 return parseDirectiveValue(4);
1402 case DK_QUAD:
1403 case DK_8BYTE:
1404 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001405 case DK_OCTA:
1406 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001407 case DK_SINGLE:
1408 case DK_FLOAT:
1409 return parseDirectiveRealValue(APFloat::IEEEsingle);
1410 case DK_DOUBLE:
1411 return parseDirectiveRealValue(APFloat::IEEEdouble);
1412 case DK_ALIGN: {
1413 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1414 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1415 }
1416 case DK_ALIGN32: {
1417 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1418 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1419 }
1420 case DK_BALIGN:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1422 case DK_BALIGNW:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1424 case DK_BALIGNL:
1425 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1426 case DK_P2ALIGN:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1428 case DK_P2ALIGNW:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1430 case DK_P2ALIGNL:
1431 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1432 case DK_ORG:
1433 return parseDirectiveOrg();
1434 case DK_FILL:
1435 return parseDirectiveFill();
1436 case DK_ZERO:
1437 return parseDirectiveZero();
1438 case DK_EXTERN:
1439 eatToEndOfStatement(); // .extern is the default, ignore it.
1440 return false;
1441 case DK_GLOBL:
1442 case DK_GLOBAL:
1443 return parseDirectiveSymbolAttribute(MCSA_Global);
1444 case DK_LAZY_REFERENCE:
1445 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1446 case DK_NO_DEAD_STRIP:
1447 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1448 case DK_SYMBOL_RESOLVER:
1449 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1450 case DK_PRIVATE_EXTERN:
1451 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1452 case DK_REFERENCE:
1453 return parseDirectiveSymbolAttribute(MCSA_Reference);
1454 case DK_WEAK_DEFINITION:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1456 case DK_WEAK_REFERENCE:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1458 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1459 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1460 case DK_COMM:
1461 case DK_COMMON:
1462 return parseDirectiveComm(/*IsLocal=*/false);
1463 case DK_LCOMM:
1464 return parseDirectiveComm(/*IsLocal=*/true);
1465 case DK_ABORT:
1466 return parseDirectiveAbort();
1467 case DK_INCLUDE:
1468 return parseDirectiveInclude();
1469 case DK_INCBIN:
1470 return parseDirectiveIncbin();
1471 case DK_CODE16:
1472 case DK_CODE16GCC:
1473 return TokError(Twine(IDVal) + " not supported yet");
1474 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001475 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001476 case DK_IRP:
1477 return parseDirectiveIrp(IDLoc);
1478 case DK_IRPC:
1479 return parseDirectiveIrpc(IDLoc);
1480 case DK_ENDR:
1481 return parseDirectiveEndr(IDLoc);
1482 case DK_BUNDLE_ALIGN_MODE:
1483 return parseDirectiveBundleAlignMode();
1484 case DK_BUNDLE_LOCK:
1485 return parseDirectiveBundleLock();
1486 case DK_BUNDLE_UNLOCK:
1487 return parseDirectiveBundleUnlock();
1488 case DK_SLEB128:
1489 return parseDirectiveLEB128(true);
1490 case DK_ULEB128:
1491 return parseDirectiveLEB128(false);
1492 case DK_SPACE:
1493 case DK_SKIP:
1494 return parseDirectiveSpace(IDVal);
1495 case DK_FILE:
1496 return parseDirectiveFile(IDLoc);
1497 case DK_LINE:
1498 return parseDirectiveLine();
1499 case DK_LOC:
1500 return parseDirectiveLoc();
1501 case DK_STABS:
1502 return parseDirectiveStabs();
1503 case DK_CFI_SECTIONS:
1504 return parseDirectiveCFISections();
1505 case DK_CFI_STARTPROC:
1506 return parseDirectiveCFIStartProc();
1507 case DK_CFI_ENDPROC:
1508 return parseDirectiveCFIEndProc();
1509 case DK_CFI_DEF_CFA:
1510 return parseDirectiveCFIDefCfa(IDLoc);
1511 case DK_CFI_DEF_CFA_OFFSET:
1512 return parseDirectiveCFIDefCfaOffset();
1513 case DK_CFI_ADJUST_CFA_OFFSET:
1514 return parseDirectiveCFIAdjustCfaOffset();
1515 case DK_CFI_DEF_CFA_REGISTER:
1516 return parseDirectiveCFIDefCfaRegister(IDLoc);
1517 case DK_CFI_OFFSET:
1518 return parseDirectiveCFIOffset(IDLoc);
1519 case DK_CFI_REL_OFFSET:
1520 return parseDirectiveCFIRelOffset(IDLoc);
1521 case DK_CFI_PERSONALITY:
1522 return parseDirectiveCFIPersonalityOrLsda(true);
1523 case DK_CFI_LSDA:
1524 return parseDirectiveCFIPersonalityOrLsda(false);
1525 case DK_CFI_REMEMBER_STATE:
1526 return parseDirectiveCFIRememberState();
1527 case DK_CFI_RESTORE_STATE:
1528 return parseDirectiveCFIRestoreState();
1529 case DK_CFI_SAME_VALUE:
1530 return parseDirectiveCFISameValue(IDLoc);
1531 case DK_CFI_RESTORE:
1532 return parseDirectiveCFIRestore(IDLoc);
1533 case DK_CFI_ESCAPE:
1534 return parseDirectiveCFIEscape();
1535 case DK_CFI_SIGNAL_FRAME:
1536 return parseDirectiveCFISignalFrame();
1537 case DK_CFI_UNDEFINED:
1538 return parseDirectiveCFIUndefined(IDLoc);
1539 case DK_CFI_REGISTER:
1540 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001541 case DK_CFI_WINDOW_SAVE:
1542 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001543 case DK_MACROS_ON:
1544 case DK_MACROS_OFF:
1545 return parseDirectiveMacrosOnOff(IDVal);
1546 case DK_MACRO:
1547 return parseDirectiveMacro(IDLoc);
1548 case DK_ENDM:
1549 case DK_ENDMACRO:
1550 return parseDirectiveEndMacro(IDVal);
1551 case DK_PURGEM:
1552 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001553 case DK_END:
1554 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001555 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001556 return parseDirectiveError(IDLoc, false);
1557 case DK_ERROR:
1558 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001559 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001560
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001561 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001562 }
Chris Lattner36e02122009-06-21 20:54:55 +00001563
Chad Rosierc7f552c2013-02-12 21:33:51 +00001564 // __asm _emit or __asm __emit
1565 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1566 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001568
1569 // __asm align
1570 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001571 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001572
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001573 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001574
Chris Lattner7cbfa442010-05-19 23:34:33 +00001575 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001576 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001577 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001579 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001580 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001581
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001582 // Dump the parsed representation, if requested.
1583 if (getShowParsedOperands()) {
1584 SmallString<256> Str;
1585 raw_svector_ostream OS(Str);
1586 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001587 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001588 if (i != 0)
1589 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001590 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001591 }
1592 OS << "]";
1593
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001595 }
1596
Oliver Stannard8b273082014-06-19 15:52:37 +00001597 // If we are generating dwarf for the current section then generate a .loc
1598 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001599 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001600 getContext().getGenDwarfSectionSyms().count(
1601 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001602
Eli Bendersky88024712013-01-16 19:32:36 +00001603 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001604
Eli Bendersky88024712013-01-16 19:32:36 +00001605 // If we previously parsed a cpp hash file line comment then make sure the
1606 // current Dwarf File is for the CppHashFilename if not then emit the
1607 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001608 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001609 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1610 0, StringRef(), CppHashFilename);
1611 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001612
Jim Grosbach4b905842013-09-20 23:08:21 +00001613 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1614 // cache with the different Loc from the call above we save the last
1615 // info we queried here with SrcMgr.FindLineNumber().
1616 unsigned CppHashLocLineNo;
1617 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1618 CppHashLocLineNo = LastQueryLine;
1619 else {
1620 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1621 LastQueryLine = CppHashLocLineNo;
1622 LastQueryIDLoc = CppHashLoc;
1623 LastQueryBuffer = CppHashBuf;
1624 }
1625 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001626 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001627
Jim Grosbach4b905842013-09-20 23:08:21 +00001628 getStreamer().EmitDwarfLocDirective(
1629 getContext().getGenDwarfFileNumber(), Line, 0,
1630 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1631 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001632 }
1633
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001634 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001635 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001636 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001637 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1638 Info.ParsedOperands, Out,
1639 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001640 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001641
Chris Lattnera2a9d162010-09-11 16:18:25 +00001642 // Don't skip the rest of the line, the instruction parser is responsible for
1643 // that.
1644 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001645}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001646
Jim Grosbach4b905842013-09-20 23:08:21 +00001647/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001648/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001649void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001650 if (!Lexer.is(AsmToken::EndOfStatement))
1651 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001652 // Eat EOL.
1653 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001654}
1655
Jim Grosbach4b905842013-09-20 23:08:21 +00001656/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001657/// ::= # number "filename"
1658/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001659bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001660 Lex(); // Eat the hash token.
1661
1662 if (getLexer().isNot(AsmToken::Integer)) {
1663 // Consume the line since in cases it is not a well-formed line directive,
1664 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001665 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001666 return false;
1667 }
1668
1669 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001670 Lex();
1671
1672 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001673 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001674 return false;
1675 }
1676
1677 StringRef Filename = getTok().getString();
1678 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001679 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001680
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001681 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1682 CppHashLoc = L;
1683 CppHashFilename = Filename;
1684 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001685 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001686
1687 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001689 return false;
1690}
1691
Jim Grosbach4b905842013-09-20 23:08:21 +00001692/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693/// for the Filename and LineNo if any in the diagnostic.
1694void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001695 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 raw_ostream &OS = errs();
1697
1698 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1699 const SMLoc &DiagLoc = Diag.getLoc();
1700 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1701 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1702
Jim Grosbach4b905842013-09-20 23:08:21 +00001703 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001704 // before printing the message.
1705 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001706 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1708 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709 }
1710
Eric Christophera7c32732012-12-18 00:30:54 +00001711 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712 // manager changed or buffer changed (like in a nested include) then just
1713 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001716 if (Parser->SavedDiagHandler)
1717 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1718 else
Craig Topper353eda42014-04-24 06:44:33 +00001719 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 return;
1721 }
1722
Eric Christophera7c32732012-12-18 00:30:54 +00001723 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1725 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001726 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001727
1728 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1729 int CppHashLocLineNo =
1730 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001731 int LineNo =
1732 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001733
Jim Grosbach4b905842013-09-20 23:08:21 +00001734 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1735 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001736 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001737
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001738 if (Parser->SavedDiagHandler)
1739 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1740 else
Craig Topper353eda42014-04-24 06:44:33 +00001741 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001742}
1743
Rafael Espindola2c064482012-08-21 18:29:30 +00001744// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1745// difference being that that function accepts '@' as part of identifiers and
1746// we can't do that. AsmLexer.cpp should probably be changed to handle
1747// '@' as a special case when needed.
1748static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001749 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1750 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001751}
1752
Rafael Espindola34b9c512012-06-03 23:57:14 +00001753bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001754 ArrayRef<MCAsmMacroParameter> Parameters,
1755 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001757 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001758 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001759 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001760
Preston Gurd05500642012-09-19 20:36:12 +00001761 // A macro without parameters is handled differently on Darwin:
1762 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001763 while (!Body.empty()) {
1764 // Scan for the next substitution.
1765 std::size_t End = Body.size(), Pos = 0;
1766 for (; Pos != End; ++Pos) {
1767 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001768 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001769 // This macro has no parameters, look for $0, $1, etc.
1770 if (Body[Pos] != '$' || Pos + 1 == End)
1771 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001772
Rafael Espindola1134ab232011-06-05 02:43:45 +00001773 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001774 if (Next == '$' || Next == 'n' ||
1775 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001776 break;
1777 } else {
1778 // This macro has parameters, look for \foo, \bar, etc.
1779 if (Body[Pos] == '\\' && Pos + 1 != End)
1780 break;
1781 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001782 }
1783
1784 // Add the prefix.
1785 OS << Body.slice(0, Pos);
1786
1787 // Check if we reached the end.
1788 if (Pos == End)
1789 break;
1790
Benjamin Kramer513e7442014-02-20 13:36:32 +00001791 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 switch (Body[Pos + 1]) {
1793 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 case '$':
1795 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001796 break;
1797
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001799 case 'n':
1800 OS << A.size();
1801 break;
1802
Jim Grosbach4b905842013-09-20 23:08:21 +00001803 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001804 default: {
1805 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001807 if (Index >= A.size())
1808 break;
1809
1810 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001811 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 ie = A[Index].end();
1813 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001814 OS << it->getString();
1815 break;
1816 }
1817 }
1818 Pos += 2;
1819 } else {
1820 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001821 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 ++I;
1823
Jim Grosbach4b905842013-09-20 23:08:21 +00001824 const char *Begin = Body.data() + Pos + 1;
1825 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 unsigned Index = 0;
1827 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001828 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 break;
1830
Preston Gurd05500642012-09-19 20:36:12 +00001831 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001832 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1833 Pos += 3;
1834 else {
1835 OS << '\\' << Argument;
1836 Pos = I;
1837 }
Preston Gurd05500642012-09-19 20:36:12 +00001838 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001839 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001840 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001841 ie = A[Index].end();
1842 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001843 // We expect no quotes around the string's contents when
1844 // parsing for varargs.
1845 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001846 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001847 else
1848 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001849
Preston Gurd05500642012-09-19 20:36:12 +00001850 Pos += 1 + Argument.size();
1851 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001853 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001854 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001855 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001856
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857 return false;
1858}
Daniel Dunbar43235712010-07-18 18:54:11 +00001859
Jim Grosbach4b905842013-09-20 23:08:21 +00001860MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1861 SMLoc EL, MemoryBuffer *I)
1862 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1863 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001864
Jim Grosbach4b905842013-09-20 23:08:21 +00001865static bool isOperator(AsmToken::TokenKind kind) {
1866 switch (kind) {
1867 default:
1868 return false;
1869 case AsmToken::Plus:
1870 case AsmToken::Minus:
1871 case AsmToken::Tilde:
1872 case AsmToken::Slash:
1873 case AsmToken::Star:
1874 case AsmToken::Dot:
1875 case AsmToken::Equal:
1876 case AsmToken::EqualEqual:
1877 case AsmToken::Pipe:
1878 case AsmToken::PipePipe:
1879 case AsmToken::Caret:
1880 case AsmToken::Amp:
1881 case AsmToken::AmpAmp:
1882 case AsmToken::Exclaim:
1883 case AsmToken::ExclaimEqual:
1884 case AsmToken::Percent:
1885 case AsmToken::Less:
1886 case AsmToken::LessEqual:
1887 case AsmToken::LessLess:
1888 case AsmToken::LessGreater:
1889 case AsmToken::Greater:
1890 case AsmToken::GreaterEqual:
1891 case AsmToken::GreaterGreater:
1892 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001893 }
1894}
1895
David Majnemer16252452014-01-29 00:07:39 +00001896namespace {
1897class AsmLexerSkipSpaceRAII {
1898public:
1899 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1900 Lexer.setSkipSpace(SkipSpace);
1901 }
1902
1903 ~AsmLexerSkipSpaceRAII() {
1904 Lexer.setSkipSpace(true);
1905 }
1906
1907private:
1908 AsmLexer &Lexer;
1909};
1910}
1911
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001912bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1913
1914 if (Vararg) {
1915 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1916 StringRef Str = parseStringToEndOfStatement();
1917 MA.push_back(AsmToken(AsmToken::String, Str));
1918 }
1919 return false;
1920 }
1921
Rafael Espindola768b41c2012-06-15 14:02:34 +00001922 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001923 unsigned AddTokens = 0;
1924
David Majnemer16252452014-01-29 00:07:39 +00001925 // Darwin doesn't use spaces to delmit arguments.
1926 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927
1928 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001929 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001931
David Majnemer91fc4c22014-01-29 18:57:46 +00001932 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001933 break;
Preston Gurd05500642012-09-19 20:36:12 +00001934
1935 if (Lexer.is(AsmToken::Space)) {
1936 Lex(); // Eat spaces
1937
1938 // Spaces can delimit parameters, but could also be part an expression.
1939 // If the token after a space is an operator, add the token and the next
1940 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001941 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001942 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001943 // Check to see whether the token is used as an operator,
1944 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001945 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001946 if (*NextChar == ' ')
1947 AddTokens = 2;
1948 }
1949
1950 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001951 break;
1952 }
1953 }
1954 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955
Jim Grosbach4b905842013-09-20 23:08:21 +00001956 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 // to be able to fill in the remaining default parameter values
1958 if (Lexer.is(AsmToken::EndOfStatement))
1959 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960
1961 // Adjust the current parentheses level.
1962 if (Lexer.is(AsmToken::LParen))
1963 ++ParenLevel;
1964 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1965 --ParenLevel;
1966
1967 // Append the token to the current argument list.
1968 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001969 if (AddTokens)
1970 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001971 Lex();
1972 }
Preston Gurd05500642012-09-19 20:36:12 +00001973
Rafael Espindola768b41c2012-06-15 14:02:34 +00001974 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001975 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001976 return false;
1977}
1978
1979// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001980bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001981 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001982 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001983 bool NamedParametersFound = false;
1984 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001985
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001987 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001988
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989 // Parse two kinds of macro invocations:
1990 // - macros defined without any parameters accept an arbitrary number of them
1991 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001992 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1994 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001995 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001999 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002000 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002001 eatToEndOfStatement();
2002 return true;
2003 }
2004
2005 if (!Lexer.is(AsmToken::Equal)) {
2006 TokError("expected '=' after formal parameter identifier");
2007 eatToEndOfStatement();
2008 return true;
2009 }
2010 Lex();
2011
2012 NamedParametersFound = true;
2013 }
2014
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002015 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002016 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002017 eatToEndOfStatement();
2018 return true;
2019 }
2020
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002021 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2022 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002023 return true;
2024
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002026 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002027 unsigned FAI = 0;
2028 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002029 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002031
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002032 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002033 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002034 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002035 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002036 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002037 return true;
2038 }
2039 PI = FAI;
2040 }
2041
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002042 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002043 if (A.size() <= PI)
2044 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002045 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002046
2047 if (FALocs.size() <= PI)
2048 FALocs.resize(PI + 1);
2049
2050 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002051 }
Jim Grosbach206661622012-07-30 22:44:17 +00002052
Preston Gurd242ed3152012-09-19 20:29:04 +00002053 // At the end of the statement, fill in remaining arguments that have
2054 // default values. If there aren't any, then the next argument is
2055 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002056 if (Lexer.is(AsmToken::EndOfStatement)) {
2057 bool Failure = false;
2058 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2059 if (A[FAI].empty()) {
2060 if (M->Parameters[FAI].Required) {
2061 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2062 "missing value for required parameter "
2063 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2064 Failure = true;
2065 }
2066
2067 if (!M->Parameters[FAI].Value.empty())
2068 A[FAI] = M->Parameters[FAI].Value;
2069 }
2070 }
2071 return Failure;
2072 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002073
2074 if (Lexer.is(AsmToken::Comma))
2075 Lex();
2076 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002077
2078 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002079}
2080
Jim Grosbach4b905842013-09-20 23:08:21 +00002081const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2082 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002083 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002084}
2085
Jim Grosbach4b905842013-09-20 23:08:21 +00002086void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002087 MacroMap[Name] = new MCAsmMacro(Macro);
2088}
2089
Jim Grosbach4b905842013-09-20 23:08:21 +00002090void AsmParser::undefineMacro(StringRef Name) {
2091 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002092 if (I != MacroMap.end()) {
2093 delete I->getValue();
2094 MacroMap.erase(I);
2095 }
2096}
2097
Jim Grosbach4b905842013-09-20 23:08:21 +00002098bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002099 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2100 // this, although we should protect against infinite loops.
2101 if (ActiveMacros.size() == 20)
2102 return TokError("macros cannot be nested more than 20 levels deep");
2103
Eli Bendersky38274122013-01-14 23:22:36 +00002104 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002105 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002106 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002107
Rafael Espindola1134ab232011-06-05 02:43:45 +00002108 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2109 // to hold the macro body with substitutions.
2110 SmallString<256> Buf;
2111 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002112 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002114 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002115 return true;
2116
Eli Bendersky38274122013-01-14 23:22:36 +00002117 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002118 // instantiation.
2119 OS << ".endmacro\n";
2120
Rafael Espindola1134ab232011-06-05 02:43:45 +00002121 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002122 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002123
Daniel Dunbar43235712010-07-18 18:54:11 +00002124 // Create the macro instantiation object and add to the current macro
2125 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 MacroInstantiation *MI = new MacroInstantiation(
2127 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 ActiveMacros.push_back(MI);
2129
2130 // Jump to the macro instantiation and prime the lexer.
2131 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2132 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2133 Lex();
2134
2135 return false;
2136}
2137
Jim Grosbach4b905842013-09-20 23:08:21 +00002138void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002140 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002141 Lex();
2142
2143 // Pop the instantiation entry.
2144 delete ActiveMacros.back();
2145 ActiveMacros.pop_back();
2146}
2147
Jim Grosbach4b905842013-09-20 23:08:21 +00002148static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002149 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002150 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002151 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2152 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002153 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002154 case MCExpr::Target:
2155 case MCExpr::Constant:
2156 return false;
2157 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002158 const MCSymbol &S =
2159 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002160 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002161 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002162 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002163 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002164 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002165 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002166 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002167
2168 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002169}
2170
Jim Grosbach4b905842013-09-20 23:08:21 +00002171bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002172 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002173 // FIXME: Use better location, we should use proper tokens.
2174 SMLoc EqualLoc = Lexer.getLoc();
2175
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002176 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002177 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002178 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002179
Rafael Espindola72f5f172012-01-28 05:57:00 +00002180 // Note: we don't count b as used in "a = b". This is to allow
2181 // a = b
2182 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002183
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002184 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002185 return TokError("unexpected token in assignment");
2186
2187 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002188 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189
Daniel Dunbar5f339242009-10-16 01:57:39 +00002190 // Validate that the LHS is allowed to be a variable (either it has not been
2191 // used as a symbol, or it is an absolute symbol).
2192 MCSymbol *Sym = getContext().LookupSymbol(Name);
2193 if (Sym) {
2194 // Diagnose assignment to a label.
2195 //
2196 // FIXME: Diagnostics. Note the location of the definition as a label.
2197 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002198 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002199 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2200 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002201 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002202 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2203 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002204 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002205 return Error(EqualLoc, "redefinition of '" + Name + "'");
2206 else if (!Sym->isVariable())
2207 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002208 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002209 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002210 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002211
2212 // Don't count these checks as uses.
2213 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002214 } else if (Name == ".") {
2215 if (Out.EmitValueToOffset(Value, 0)) {
2216 Error(EqualLoc, "expected absolute expression");
2217 eatToEndOfStatement();
2218 }
2219 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002220 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002221 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002223 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002224 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002225 if (NoDeadStrip)
2226 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2227
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002228 return false;
2229}
2230
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002231/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002232/// ::= identifier
2233/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002235 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002236 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2237 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002238 // handle this as a context dependent token, instead we detect adjacent tokens
2239 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2241 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242
Hans Wennborgce69d772013-10-18 20:46:28 +00002243 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 Lex();
2245 if (Lexer.isNot(AsmToken::Identifier))
2246 return true;
2247
Hans Wennborgce69d772013-10-18 20:46:28 +00002248 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2249 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250 return true;
2251
2252 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002253 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002254 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002255 Lex();
2256 return false;
2257 }
2258
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002260 return true;
2261
Sean Callanan936b0d32010-01-19 21:44:56 +00002262 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263
Sean Callanan686ed8d2010-01-19 20:22:31 +00002264 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
2266 return false;
2267}
2268
Jim Grosbach4b905842013-09-20 23:08:21 +00002269/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002270/// ::= .equ identifier ',' expression
2271/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002272/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002273bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002275
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002276 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002277 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002278
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002280 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002281 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002282
Jim Grosbach4b905842013-09-20 23:08:21 +00002283 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284}
2285
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002286bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002288
2289 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002290 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002291 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2292 if (Str[i] != '\\') {
2293 Data += Str[i];
2294 continue;
2295 }
2296
2297 // Recognize escaped characters. Note that this escape semantics currently
2298 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2299 ++i;
2300 if (i == e)
2301 return TokError("unexpected backslash at end of string");
2302
2303 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002304 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002305 // Consume up to three octal characters.
2306 unsigned Value = Str[i] - '0';
2307
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 ++i;
2310 Value = Value * 8 + (Str[i] - '0');
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 ++i;
2314 Value = Value * 8 + (Str[i] - '0');
2315 }
2316 }
2317
2318 if (Value > 255)
2319 return TokError("invalid octal escape sequence (out of range)");
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 continue;
2323 }
2324
2325 // Otherwise recognize individual escapes.
2326 switch (Str[i]) {
2327 default:
2328 // Just reject invalid escape sequences for now.
2329 return TokError("invalid escape sequence (unrecognized character)");
2330
2331 case 'b': Data += '\b'; break;
2332 case 'f': Data += '\f'; break;
2333 case 'n': Data += '\n'; break;
2334 case 'r': Data += '\r'; break;
2335 case 't': Data += '\t'; break;
2336 case '"': Data += '"'; break;
2337 case '\\': Data += '\\'; break;
2338 }
2339 }
2340
2341 return false;
2342}
2343
Jim Grosbach4b905842013-09-20 23:08:21 +00002344/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002345/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002346bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002347 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002348 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002349
Daniel Dunbara10e5192009-06-24 23:30:00 +00002350 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002352 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353
Daniel Dunbaref668c12009-08-14 18:19:52 +00002354 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002355 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357
Rafael Espindola64e1af82013-07-02 15:49:13 +00002358 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Sean Callanan686ed8d2010-01-19 20:22:31 +00002362 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
2364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 break;
2366
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002368 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002369 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 }
2371 }
2372
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 return false;
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002379bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002382
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002384 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002385 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002386 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 return true;
2388
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002389 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002390 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2391 assert(Size <= 8 && "Invalid size");
2392 uint64_t IntValue = MCE->getValue();
2393 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2394 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002395 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002396 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002397 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002399 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002401
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002403 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002405 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 }
2407 }
2408
Sean Callanan686ed8d2010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 return false;
2411}
2412
David Woodhoused6de0d92014-02-01 16:20:59 +00002413/// ParseDirectiveOctaValue
2414/// ::= .octa [ hexconstant (, hexconstant)* ]
2415bool AsmParser::parseDirectiveOctaValue() {
2416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2417 checkForValidSection();
2418
2419 for (;;) {
2420 if (Lexer.getKind() == AsmToken::Error)
2421 return true;
2422 if (Lexer.getKind() != AsmToken::Integer &&
2423 Lexer.getKind() != AsmToken::BigNum)
2424 return TokError("unknown token in expression");
2425
2426 SMLoc ExprLoc = getLexer().getLoc();
2427 APInt IntValue = getTok().getAPIntVal();
2428 Lex();
2429
2430 uint64_t hi, lo;
2431 if (IntValue.isIntN(64)) {
2432 hi = 0;
2433 lo = IntValue.getZExtValue();
2434 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002435 // It might actually have more than 128 bits, but the top ones are zero.
2436 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002437 lo = IntValue.getLoBits(64).getZExtValue();
2438 } else
2439 return Error(ExprLoc, "literal value out of range for directive");
2440
2441 if (MAI.isLittleEndian()) {
2442 getStreamer().EmitIntValue(lo, 8);
2443 getStreamer().EmitIntValue(hi, 8);
2444 } else {
2445 getStreamer().EmitIntValue(hi, 8);
2446 getStreamer().EmitIntValue(lo, 8);
2447 }
2448
2449 if (getLexer().is(AsmToken::EndOfStatement))
2450 break;
2451
2452 // FIXME: Improve diagnostic.
2453 if (getLexer().isNot(AsmToken::Comma))
2454 return TokError("unexpected token in directive");
2455 Lex();
2456 }
2457 }
2458
2459 Lex();
2460 return false;
2461}
2462
Jim Grosbach4b905842013-09-20 23:08:21 +00002463/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002465bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002467 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468
2469 for (;;) {
2470 // We don't truly support arithmetic on floating point expressions, so we
2471 // have to manually parse unary prefixes.
2472 bool IsNeg = false;
2473 if (getLexer().is(AsmToken::Minus)) {
2474 Lex();
2475 IsNeg = true;
2476 } else if (getLexer().is(AsmToken::Plus))
2477 Lex();
2478
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002480 getLexer().isNot(AsmToken::Real) &&
2481 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002482 return TokError("unexpected token in directive");
2483
2484 // Convert to an APFloat.
2485 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002486 StringRef IDVal = getTok().getString();
2487 if (getLexer().is(AsmToken::Identifier)) {
2488 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2489 Value = APFloat::getInf(Semantics);
2490 else if (!IDVal.compare_lower("nan"))
2491 Value = APFloat::getNaN(Semantics, false, ~0);
2492 else
2493 return TokError("invalid floating point literal");
2494 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002495 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002496 return TokError("invalid floating point literal");
2497 if (IsNeg)
2498 Value.changeSign();
2499
2500 // Consume the numeric token.
2501 Lex();
2502
2503 // Emit the value as an integer.
2504 APInt AsInt = Value.bitcastToAPInt();
2505 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002506 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002507
2508 if (getLexer().is(AsmToken::EndOfStatement))
2509 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002510
Daniel Dunbar2af16532010-09-24 01:59:56 +00002511 if (getLexer().isNot(AsmToken::Comma))
2512 return TokError("unexpected token in directive");
2513 Lex();
2514 }
2515 }
2516
2517 Lex();
2518 return false;
2519}
2520
Jim Grosbach4b905842013-09-20 23:08:21 +00002521/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002522/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002523bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002525
2526 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002528 return true;
2529
Rafael Espindolab91bac62010-10-05 19:42:57 +00002530 int64_t Val = 0;
2531 if (getLexer().is(AsmToken::Comma)) {
2532 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002534 return true;
2535 }
2536
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
2538 return TokError("unexpected token in '.zero' directive");
2539
2540 Lex();
2541
Rafael Espindola64e1af82013-07-02 15:49:13 +00002542 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002543
2544 return false;
2545}
2546
Jim Grosbach4b905842013-09-20 23:08:21 +00002547/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002548/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002549bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002550 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002551
David Majnemer522d3db2014-02-01 07:19:38 +00002552 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 return true;
2556
David Majnemer522d3db2014-02-01 07:19:38 +00002557 if (NumValues < 0) {
2558 Warning(RepeatLoc,
2559 "'.fill' directive with negative repeat count has no effect");
2560 NumValues = 0;
2561 }
2562
Roman Divackye33098f2013-09-24 17:44:41 +00002563 int64_t FillSize = 1;
2564 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2568 if (getLexer().isNot(AsmToken::Comma))
2569 return TokError("unexpected token in '.fill' directive");
2570 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002571
David Majnemer522d3db2014-02-01 07:19:38 +00002572 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (parseAbsoluteExpression(FillSize))
2574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002575
Roman Divackye33098f2013-09-24 17:44:41 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2577 if (getLexer().isNot(AsmToken::Comma))
2578 return TokError("unexpected token in '.fill' directive");
2579 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002580
David Majnemer522d3db2014-02-01 07:19:38 +00002581 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillExpr))
2583 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002584
Roman Divackye33098f2013-09-24 17:44:41 +00002585 if (getLexer().isNot(AsmToken::EndOfStatement))
2586 return TokError("unexpected token in '.fill' directive");
2587
2588 Lex();
2589 }
2590 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002591
David Majnemer522d3db2014-02-01 07:19:38 +00002592 if (FillSize < 0) {
2593 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2594 NumValues = 0;
2595 }
2596 if (FillSize > 8) {
2597 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2598 FillSize = 8;
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (!isUInt<32>(FillExpr) && FillSize > 4)
2602 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2603
2604 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2605 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2606
2607 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2608 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2609 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2610 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002611
2612 return false;
2613}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002614
Jim Grosbach4b905842013-09-20 23:08:21 +00002615/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002616/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002617bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002618 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002619
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002620 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002621 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002622 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002623 return true;
2624
2625 // Parse optional fill expression.
2626 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002627 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2628 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002629 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002630 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002631
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002632 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002633 return true;
2634
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002635 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002636 return TokError("unexpected token in '.org' directive");
2637 }
2638
Sean Callanan686ed8d2010-01-19 20:22:31 +00002639 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002640
Jim Grosbachb5912772012-01-27 00:37:08 +00002641 // Only limited forms of relocatable expressions are accepted here, it
2642 // has to be relative to the current section. The streamer will return
2643 // 'true' if the expression wasn't evaluatable.
2644 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2645 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002646
2647 return false;
2648}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002649
Jim Grosbach4b905842013-09-20 23:08:21 +00002650/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002651/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002652bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002653 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002654
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002655 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002656 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658 return true;
2659
2660 SMLoc MaxBytesLoc;
2661 bool HasFillExpr = false;
2662 int64_t FillExpr = 0;
2663 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002664 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2665 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002666 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002667 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668
2669 // The fill expression can be omitted while specifying a maximum number of
2670 // alignment bytes, e.g:
2671 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002672 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002673 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002674 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 return true;
2676 }
2677
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002678 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2679 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002680 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002681 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002683 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002684 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002685 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002686
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002687 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002688 return TokError("unexpected token in directive");
2689 }
2690 }
2691
Sean Callanan686ed8d2010-01-19 20:22:31 +00002692 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002693
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002694 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696
2697 // Compute alignment in bytes.
2698 if (IsPow2) {
2699 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002700 if (Alignment >= 32) {
2701 Error(AlignmentLoc, "invalid alignment value");
2702 Alignment = 31;
2703 }
2704
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002705 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002706 } else {
2707 // Reject alignments that aren't a power of two, for gas compatibility.
2708 if (!isPowerOf2_64(Alignment))
2709 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002710 }
2711
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002712 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002713 if (MaxBytesLoc.isValid()) {
2714 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002715 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002716 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002717 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002718 }
2719
2720 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002721 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002722 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002723 MaxBytesToFill = 0;
2724 }
2725 }
2726
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002727 // Check whether we should use optimal code alignment for this .align
2728 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002729 const MCSection *Section = getStreamer().getCurrentSection().first;
2730 assert(Section && "must have section to emit alignment");
2731 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002732 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2733 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002734 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002735 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002736 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002737 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2738 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002739 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002740
2741 return false;
2742}
2743
Jim Grosbach4b905842013-09-20 23:08:21 +00002744/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002745/// ::= .file [number] filename
2746/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002747bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002748 // FIXME: I'm not sure what this is.
2749 int64_t FileNumber = -1;
2750 SMLoc FileNumberLoc = getLexer().getLoc();
2751 if (getLexer().is(AsmToken::Integer)) {
2752 FileNumber = getTok().getIntVal();
2753 Lex();
2754
2755 if (FileNumber < 1)
2756 return TokError("file number less than one");
2757 }
2758
2759 if (getLexer().isNot(AsmToken::String))
2760 return TokError("unexpected token in '.file' directive");
2761
2762 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002763 // Allow the strings to have escaped octal character sequence.
2764 std::string Path = getTok().getString();
2765 if (parseEscapedString(Path))
2766 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002767 Lex();
2768
2769 StringRef Directory;
2770 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002771 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002772 if (getLexer().is(AsmToken::String)) {
2773 if (FileNumber == -1)
2774 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002775 if (parseEscapedString(FilenameData))
2776 return true;
2777 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002778 Directory = Path;
2779 Lex();
2780 } else {
2781 Filename = Path;
2782 }
2783
2784 if (getLexer().isNot(AsmToken::EndOfStatement))
2785 return TokError("unexpected token in '.file' directive");
2786
2787 if (FileNumber == -1)
2788 getStreamer().EmitFileDirective(Filename);
2789 else {
2790 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002791 Error(DirectiveLoc,
2792 "input can't have .file dwarf directives when -g is "
2793 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002794
David Blaikiec714ef42014-03-17 01:52:11 +00002795 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2796 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002797 Error(FileNumberLoc, "file number already allocated");
2798 }
2799
2800 return false;
2801}
2802
Jim Grosbach4b905842013-09-20 23:08:21 +00002803/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002804/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002805bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002806 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2807 if (getLexer().isNot(AsmToken::Integer))
2808 return TokError("unexpected token in '.line' directive");
2809
2810 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002811 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002812 Lex();
2813
2814 // FIXME: Do something with the .line.
2815 }
2816
2817 if (getLexer().isNot(AsmToken::EndOfStatement))
2818 return TokError("unexpected token in '.line' directive");
2819
2820 return false;
2821}
2822
Jim Grosbach4b905842013-09-20 23:08:21 +00002823/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002824/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2825/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2826/// The first number is a file number, must have been previously assigned with
2827/// a .file directive, the second number is the line number and optionally the
2828/// third number is a column position (zero if not specified). The remaining
2829/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002830bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002831 if (getLexer().isNot(AsmToken::Integer))
2832 return TokError("unexpected token in '.loc' directive");
2833 int64_t FileNumber = getTok().getIntVal();
2834 if (FileNumber < 1)
2835 return TokError("file number less than one in '.loc' directive");
2836 if (!getContext().isValidDwarfFileNumber(FileNumber))
2837 return TokError("unassigned file number in '.loc' directive");
2838 Lex();
2839
2840 int64_t LineNumber = 0;
2841 if (getLexer().is(AsmToken::Integer)) {
2842 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002843 if (LineNumber < 0)
2844 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002845 Lex();
2846 }
2847
2848 int64_t ColumnPos = 0;
2849 if (getLexer().is(AsmToken::Integer)) {
2850 ColumnPos = getTok().getIntVal();
2851 if (ColumnPos < 0)
2852 return TokError("column position less than zero in '.loc' directive");
2853 Lex();
2854 }
2855
2856 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2857 unsigned Isa = 0;
2858 int64_t Discriminator = 0;
2859 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2860 for (;;) {
2861 if (getLexer().is(AsmToken::EndOfStatement))
2862 break;
2863
2864 StringRef Name;
2865 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002866 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002867 return TokError("unexpected token in '.loc' directive");
2868
2869 if (Name == "basic_block")
2870 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2871 else if (Name == "prologue_end")
2872 Flags |= DWARF2_FLAG_PROLOGUE_END;
2873 else if (Name == "epilogue_begin")
2874 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2875 else if (Name == "is_stmt") {
2876 Loc = getTok().getLoc();
2877 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002878 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002879 return true;
2880 // The expression must be the constant 0 or 1.
2881 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2882 int Value = MCE->getValue();
2883 if (Value == 0)
2884 Flags &= ~DWARF2_FLAG_IS_STMT;
2885 else if (Value == 1)
2886 Flags |= DWARF2_FLAG_IS_STMT;
2887 else
2888 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002889 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002890 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2891 }
Craig Topperf15655b2013-04-22 04:22:40 +00002892 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002893 Loc = getTok().getLoc();
2894 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002895 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return true;
2897 // The expression must be a constant greater or equal to 0.
2898 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2899 int Value = MCE->getValue();
2900 if (Value < 0)
2901 return Error(Loc, "isa number less than zero");
2902 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002903 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002904 return Error(Loc, "isa number not a constant value");
2905 }
Craig Topperf15655b2013-04-22 04:22:40 +00002906 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002907 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002909 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002910 return Error(Loc, "unknown sub-directive in '.loc' directive");
2911 }
2912
2913 if (getLexer().is(AsmToken::EndOfStatement))
2914 break;
2915 }
2916 }
2917
2918 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2919 Isa, Discriminator, StringRef());
2920
2921 return false;
2922}
2923
Jim Grosbach4b905842013-09-20 23:08:21 +00002924/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002925/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002926bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002927 return TokError("unsupported directive '.stabs'");
2928}
2929
Jim Grosbach4b905842013-09-20 23:08:21 +00002930/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002931/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002933 StringRef Name;
2934 bool EH = false;
2935 bool Debug = false;
2936
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002937 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002938 return TokError("Expected an identifier");
2939
2940 if (Name == ".eh_frame")
2941 EH = true;
2942 else if (Name == ".debug_frame")
2943 Debug = true;
2944
2945 if (getLexer().is(AsmToken::Comma)) {
2946 Lex();
2947
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002948 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002949 return TokError("Expected an identifier");
2950
2951 if (Name == ".eh_frame")
2952 EH = true;
2953 else if (Name == ".debug_frame")
2954 Debug = true;
2955 }
2956
2957 getStreamer().EmitCFISections(EH, Debug);
2958 return false;
2959}
2960
Jim Grosbach4b905842013-09-20 23:08:21 +00002961/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002962/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002964 StringRef Simple;
2965 if (getLexer().isNot(AsmToken::EndOfStatement))
2966 if (parseIdentifier(Simple) || Simple != "simple")
2967 return TokError("unexpected token in .cfi_startproc directive");
2968
2969 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002970 return false;
2971}
2972
Jim Grosbach4b905842013-09-20 23:08:21 +00002973/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002974/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002975bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002976 getStreamer().EmitCFIEndProc();
2977 return false;
2978}
2979
Jim Grosbach4b905842013-09-20 23:08:21 +00002980/// \brief parse register name or number.
2981bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002982 SMLoc DirectiveLoc) {
2983 unsigned RegNo;
2984
2985 if (getLexer().isNot(AsmToken::Integer)) {
2986 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2987 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002988 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002989 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002990 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002991
2992 return false;
2993}
2994
Jim Grosbach4b905842013-09-20 23:08:21 +00002995/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002996/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002997bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002998 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002999 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003000 return true;
3001
3002 if (getLexer().isNot(AsmToken::Comma))
3003 return TokError("unexpected token in directive");
3004 Lex();
3005
3006 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003007 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003008 return true;
3009
3010 getStreamer().EmitCFIDefCfa(Register, Offset);
3011 return false;
3012}
3013
Jim Grosbach4b905842013-09-20 23:08:21 +00003014/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003015/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003016bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003017 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003018 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003019 return true;
3020
3021 getStreamer().EmitCFIDefCfaOffset(Offset);
3022 return false;
3023}
3024
Jim Grosbach4b905842013-09-20 23:08:21 +00003025/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003026/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003027bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003028 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003029 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003030 return true;
3031
3032 if (getLexer().isNot(AsmToken::Comma))
3033 return TokError("unexpected token in directive");
3034 Lex();
3035
3036 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003037 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003038 return true;
3039
3040 getStreamer().EmitCFIRegister(Register1, Register2);
3041 return false;
3042}
3043
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003044/// parseDirectiveCFIWindowSave
3045/// ::= .cfi_window_save
3046bool AsmParser::parseDirectiveCFIWindowSave() {
3047 getStreamer().EmitCFIWindowSave();
3048 return false;
3049}
3050
Jim Grosbach4b905842013-09-20 23:08:21 +00003051/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003052/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003053bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003054 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003055 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003056 return true;
3057
3058 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3059 return false;
3060}
3061
Jim Grosbach4b905842013-09-20 23:08:21 +00003062/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003063/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003064bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003065 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003066 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003067 return true;
3068
3069 getStreamer().EmitCFIDefCfaRegister(Register);
3070 return false;
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 int64_t Register = 0;
3077 int64_t Offset = 0;
3078
Jim Grosbach4b905842013-09-20 23:08:21 +00003079 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003080 return true;
3081
3082 if (getLexer().isNot(AsmToken::Comma))
3083 return TokError("unexpected token in directive");
3084 Lex();
3085
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003086 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003087 return true;
3088
3089 getStreamer().EmitCFIOffset(Register, Offset);
3090 return false;
3091}
3092
Jim Grosbach4b905842013-09-20 23:08:21 +00003093/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003094/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003095bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003096 int64_t Register = 0;
3097
Jim Grosbach4b905842013-09-20 23:08:21 +00003098 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003099 return true;
3100
3101 if (getLexer().isNot(AsmToken::Comma))
3102 return TokError("unexpected token in directive");
3103 Lex();
3104
3105 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003106 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003107 return true;
3108
3109 getStreamer().EmitCFIRelOffset(Register, Offset);
3110 return false;
3111}
3112
3113static bool isValidEncoding(int64_t Encoding) {
3114 if (Encoding & ~0xff)
3115 return false;
3116
3117 if (Encoding == dwarf::DW_EH_PE_omit)
3118 return true;
3119
3120 const unsigned Format = Encoding & 0xf;
3121 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3122 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3123 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3124 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3125 return false;
3126
3127 const unsigned Application = Encoding & 0x70;
3128 if (Application != dwarf::DW_EH_PE_absptr &&
3129 Application != dwarf::DW_EH_PE_pcrel)
3130 return false;
3131
3132 return true;
3133}
3134
Jim Grosbach4b905842013-09-20 23:08:21 +00003135/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003136/// IsPersonality true for cfi_personality, false for cfi_lsda
3137/// ::= .cfi_personality encoding, [symbol_name]
3138/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003139bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003140 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003141 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003142 return true;
3143 if (Encoding == dwarf::DW_EH_PE_omit)
3144 return false;
3145
3146 if (!isValidEncoding(Encoding))
3147 return TokError("unsupported encoding.");
3148
3149 if (getLexer().isNot(AsmToken::Comma))
3150 return TokError("unexpected token in directive");
3151 Lex();
3152
3153 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003154 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003155 return TokError("expected identifier in directive");
3156
3157 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3158
3159 if (IsPersonality)
3160 getStreamer().EmitCFIPersonality(Sym, Encoding);
3161 else
3162 getStreamer().EmitCFILsda(Sym, Encoding);
3163 return false;
3164}
3165
Jim Grosbach4b905842013-09-20 23:08:21 +00003166/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003167/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003168bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003169 getStreamer().EmitCFIRememberState();
3170 return false;
3171}
3172
Jim Grosbach4b905842013-09-20 23:08:21 +00003173/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003174/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003175bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003176 getStreamer().EmitCFIRestoreState();
3177 return false;
3178}
3179
Jim Grosbach4b905842013-09-20 23:08:21 +00003180/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003181/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003182bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003183 int64_t Register = 0;
3184
Jim Grosbach4b905842013-09-20 23:08:21 +00003185 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003186 return true;
3187
3188 getStreamer().EmitCFISameValue(Register);
3189 return false;
3190}
3191
Jim Grosbach4b905842013-09-20 23:08:21 +00003192/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003193/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003194bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003195 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003196 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003197 return true;
3198
3199 getStreamer().EmitCFIRestore(Register);
3200 return false;
3201}
3202
Jim Grosbach4b905842013-09-20 23:08:21 +00003203/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003204/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003205bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003206 std::string Values;
3207 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003208 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003209 return true;
3210
3211 Values.push_back((uint8_t)CurrValue);
3212
3213 while (getLexer().is(AsmToken::Comma)) {
3214 Lex();
3215
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003216 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003217 return true;
3218
3219 Values.push_back((uint8_t)CurrValue);
3220 }
3221
3222 getStreamer().EmitCFIEscape(Values);
3223 return false;
3224}
3225
Jim Grosbach4b905842013-09-20 23:08:21 +00003226/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003227/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003228bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003229 if (getLexer().isNot(AsmToken::EndOfStatement))
3230 return Error(getLexer().getLoc(),
3231 "unexpected token in '.cfi_signal_frame'");
3232
3233 getStreamer().EmitCFISignalFrame();
3234 return false;
3235}
3236
Jim Grosbach4b905842013-09-20 23:08:21 +00003237/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003238/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003239bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003240 int64_t Register = 0;
3241
Jim Grosbach4b905842013-09-20 23:08:21 +00003242 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003243 return true;
3244
3245 getStreamer().EmitCFIUndefined(Register);
3246 return false;
3247}
3248
Jim Grosbach4b905842013-09-20 23:08:21 +00003249/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003250/// ::= .macros_on
3251/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003252bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003253 if (getLexer().isNot(AsmToken::EndOfStatement))
3254 return Error(getLexer().getLoc(),
3255 "unexpected token in '" + Directive + "' directive");
3256
Jim Grosbach4b905842013-09-20 23:08:21 +00003257 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003258 return false;
3259}
3260
Jim Grosbach4b905842013-09-20 23:08:21 +00003261/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003262/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003263bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003264 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003265 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003266 return TokError("expected identifier in '.macro' directive");
3267
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003268 if (getLexer().is(AsmToken::Comma))
3269 Lex();
3270
Eli Bendersky17233942013-01-15 22:59:42 +00003271 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003272 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003273
3274 if (Parameters.size() && Parameters.back().Vararg)
3275 return Error(Lexer.getLoc(),
3276 "Vararg parameter '" + Parameters.back().Name +
3277 "' should be last one in the list of parameters.");
3278
David Majnemer91fc4c22014-01-29 18:57:46 +00003279 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003280 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003281 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003282
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003283 if (Lexer.is(AsmToken::Colon)) {
3284 Lex(); // consume ':'
3285
3286 SMLoc QualLoc;
3287 StringRef Qualifier;
3288
3289 QualLoc = Lexer.getLoc();
3290 if (parseIdentifier(Qualifier))
3291 return Error(QualLoc, "missing parameter qualifier for "
3292 "'" + Parameter.Name + "' in macro '" + Name + "'");
3293
3294 if (Qualifier == "req")
3295 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003296 else if (Qualifier == "vararg" && !IsDarwin)
3297 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003298 else
3299 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3300 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3301 }
3302
David Majnemer91fc4c22014-01-29 18:57:46 +00003303 if (getLexer().is(AsmToken::Equal)) {
3304 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003305
3306 SMLoc ParamLoc;
3307
3308 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003309 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003310 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003311
3312 if (Parameter.Required)
3313 Warning(ParamLoc, "pointless default value for required parameter "
3314 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003315 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003316
3317 Parameters.push_back(Parameter);
3318
3319 if (getLexer().is(AsmToken::Comma))
3320 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003321 }
3322
3323 // Eat the end of statement.
3324 Lex();
3325
3326 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003327 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003328
3329 // Lex the macro definition.
3330 for (;;) {
3331 // Check whether we have reached the end of the file.
3332 if (getLexer().is(AsmToken::Eof))
3333 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3334
3335 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003336 if (getLexer().is(AsmToken::Identifier)) {
3337 if (getTok().getIdentifier() == ".endm" ||
3338 getTok().getIdentifier() == ".endmacro") {
3339 if (MacroDepth == 0) { // Outermost macro.
3340 EndToken = getTok();
3341 Lex();
3342 if (getLexer().isNot(AsmToken::EndOfStatement))
3343 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3344 "' directive");
3345 break;
3346 } else {
3347 // Otherwise we just found the end of an inner macro.
3348 --MacroDepth;
3349 }
3350 } else if (getTok().getIdentifier() == ".macro") {
3351 // We allow nested macros. Those aren't instantiated until the outermost
3352 // macro is expanded so just ignore them for now.
3353 ++MacroDepth;
3354 }
Eli Bendersky17233942013-01-15 22:59:42 +00003355 }
3356
3357 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003358 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003359 }
3360
Jim Grosbach4b905842013-09-20 23:08:21 +00003361 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003362 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3363 }
3364
3365 const char *BodyStart = StartToken.getLoc().getPointer();
3366 const char *BodyEnd = EndToken.getLoc().getPointer();
3367 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003368 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3369 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003370 return false;
3371}
3372
Jim Grosbach4b905842013-09-20 23:08:21 +00003373/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003374///
3375/// With the support added for named parameters there may be code out there that
3376/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003377/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003379/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003380/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3381/// warning that the positional parameter found in body which have no effect.
3382/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003383/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384/// intended or change the macro to use the named parameters. It is possible
3385/// this warning will trigger when the none of the named parameters are used
3386/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003387void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003389 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390 // If this macro is not defined with named parameters the warning we are
3391 // checking for here doesn't apply.
3392 unsigned NParameters = Parameters.size();
3393 if (NParameters == 0)
3394 return;
3395
3396 bool NamedParametersFound = false;
3397 bool PositionalParametersFound = false;
3398
3399 // Look at the body of the macro for use of both the named parameters and what
3400 // are likely to be positional parameters. This is what expandMacro() is
3401 // doing when it finds the parameters in the body.
3402 while (!Body.empty()) {
3403 // Scan for the next possible parameter.
3404 std::size_t End = Body.size(), Pos = 0;
3405 for (; Pos != End; ++Pos) {
3406 // Check for a substitution or escape.
3407 // This macro is defined with parameters, look for \foo, \bar, etc.
3408 if (Body[Pos] == '\\' && Pos + 1 != End)
3409 break;
3410
3411 // This macro should have parameters, but look for $0, $1, ..., $n too.
3412 if (Body[Pos] != '$' || Pos + 1 == End)
3413 continue;
3414 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003415 if (Next == '$' || Next == 'n' ||
3416 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003417 break;
3418 }
3419
3420 // Check if we reached the end.
3421 if (Pos == End)
3422 break;
3423
3424 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003425 switch (Body[Pos + 1]) {
3426 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003427 case '$':
3428 break;
3429
Jim Grosbach4b905842013-09-20 23:08:21 +00003430 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003431 case 'n':
3432 PositionalParametersFound = true;
3433 break;
3434
Jim Grosbach4b905842013-09-20 23:08:21 +00003435 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003436 default: {
3437 PositionalParametersFound = true;
3438 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003439 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003440 }
3441 Pos += 2;
3442 } else {
3443 unsigned I = Pos + 1;
3444 while (isIdentifierChar(Body[I]) && I + 1 != End)
3445 ++I;
3446
Jim Grosbach4b905842013-09-20 23:08:21 +00003447 const char *Begin = Body.data() + Pos + 1;
3448 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003449 unsigned Index = 0;
3450 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003451 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003452 break;
3453
3454 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003455 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3456 Pos += 3;
3457 else {
3458 Pos = I;
3459 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003460 } else {
3461 NamedParametersFound = true;
3462 Pos += 1 + Argument.size();
3463 }
3464 }
3465 // Update the scan point.
3466 Body = Body.substr(Pos);
3467 }
3468
3469 if (!NamedParametersFound && PositionalParametersFound)
3470 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3471 "used in macro body, possible positional parameter "
3472 "found in body which will have no effect");
3473}
3474
Jim Grosbach4b905842013-09-20 23:08:21 +00003475/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003476/// ::= .endm
3477/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003478bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003479 if (getLexer().isNot(AsmToken::EndOfStatement))
3480 return TokError("unexpected token in '" + Directive + "' directive");
3481
3482 // If we are inside a macro instantiation, terminate the current
3483 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003484 if (isInsideMacroInstantiation()) {
3485 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003486 return false;
3487 }
3488
3489 // Otherwise, this .endmacro is a stray entry in the file; well formed
3490 // .endmacro directives are handled during the macro definition parsing.
3491 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003492 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003493}
3494
Jim Grosbach4b905842013-09-20 23:08:21 +00003495/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003496/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003497bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003498 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003499 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003500 return TokError("expected identifier in '.purgem' directive");
3501
3502 if (getLexer().isNot(AsmToken::EndOfStatement))
3503 return TokError("unexpected token in '.purgem' directive");
3504
Jim Grosbach4b905842013-09-20 23:08:21 +00003505 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003506 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3507
Jim Grosbach4b905842013-09-20 23:08:21 +00003508 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003509 return false;
3510}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003511
Jim Grosbach4b905842013-09-20 23:08:21 +00003512/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003513/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003514bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003515 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003516
3517 // Expect a single argument: an expression that evaluates to a constant
3518 // in the inclusive range 0-30.
3519 SMLoc ExprLoc = getLexer().getLoc();
3520 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003521 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003522 return true;
3523 else if (getLexer().isNot(AsmToken::EndOfStatement))
3524 return TokError("unexpected token after expression in"
3525 " '.bundle_align_mode' directive");
3526 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3527 return Error(ExprLoc,
3528 "invalid bundle alignment size (expected between 0 and 30)");
3529
3530 Lex();
3531
3532 // Because of AlignSizePow2's verified range we can safely truncate it to
3533 // unsigned.
3534 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3535 return false;
3536}
3537
Jim Grosbach4b905842013-09-20 23:08:21 +00003538/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003539/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003540bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003541 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003542 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003543
Eli Bendersky802b6282013-01-07 21:51:08 +00003544 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3545 StringRef Option;
3546 SMLoc Loc = getTok().getLoc();
3547 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003548 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003549
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003550 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003551 return Error(Loc, kInvalidOptionError);
3552
3553 if (Option != "align_to_end")
3554 return Error(Loc, kInvalidOptionError);
3555 else if (getLexer().isNot(AsmToken::EndOfStatement))
3556 return Error(Loc,
3557 "unexpected token after '.bundle_lock' directive option");
3558 AlignToEnd = true;
3559 }
3560
Eli Benderskyf483ff92012-12-20 19:05:53 +00003561 Lex();
3562
Eli Bendersky802b6282013-01-07 21:51:08 +00003563 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003564 return false;
3565}
3566
Jim Grosbach4b905842013-09-20 23:08:21 +00003567/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003568/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003569bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003570 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003571
3572 if (getLexer().isNot(AsmToken::EndOfStatement))
3573 return TokError("unexpected token in '.bundle_unlock' directive");
3574 Lex();
3575
3576 getStreamer().EmitBundleUnlock();
3577 return false;
3578}
3579
Jim Grosbach4b905842013-09-20 23:08:21 +00003580/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003581/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003582bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003583 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003584
3585 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003586 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003587 return true;
3588
3589 int64_t FillExpr = 0;
3590 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3591 if (getLexer().isNot(AsmToken::Comma))
3592 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3593 Lex();
3594
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003595 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003596 return true;
3597
3598 if (getLexer().isNot(AsmToken::EndOfStatement))
3599 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3600 }
3601
3602 Lex();
3603
3604 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003605 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3606 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003607
3608 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003609 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003610
3611 return false;
3612}
3613
Jim Grosbach4b905842013-09-20 23:08:21 +00003614/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003615/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003616bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003617 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003618 const MCExpr *Value;
3619
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003620 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003621 return true;
3622
3623 if (getLexer().isNot(AsmToken::EndOfStatement))
3624 return TokError("unexpected token in directive");
3625
3626 if (Signed)
3627 getStreamer().EmitSLEB128Value(Value);
3628 else
3629 getStreamer().EmitULEB128Value(Value);
3630
3631 return false;
3632}
3633
Jim Grosbach4b905842013-09-20 23:08:21 +00003634/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003635/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003636bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003637 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003638 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003639 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003640 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003641
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003642 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003643 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003644
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003645 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003646
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003647 // Assembler local symbols don't make any sense here. Complain loudly.
3648 if (Sym->isTemporary())
3649 return Error(Loc, "non-local symbol required in directive");
3650
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003651 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3652 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003653
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003654 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003655 break;
3656
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003657 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003658 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003659 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003660 }
3661 }
3662
Sean Callanan686ed8d2010-01-19 20:22:31 +00003663 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003664 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003665}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003666
Jim Grosbach4b905842013-09-20 23:08:21 +00003667/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003668/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003669bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003670 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003671
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003672 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003673 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003675 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003676
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003677 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003678 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003679
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003680 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003681 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003682 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003683
3684 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003685 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003686 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003687 return true;
3688
3689 int64_t Pow2Alignment = 0;
3690 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003691 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003692 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003693 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003694 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003695 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003696
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003697 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3698 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003699 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3700
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003701 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003702 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3703 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003704 if (!isPowerOf2_64(Pow2Alignment))
3705 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3706 Pow2Alignment = Log2_64(Pow2Alignment);
3707 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003709
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003710 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003711 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003712
Sean Callanan686ed8d2010-01-19 20:22:31 +00003713 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003714
Chris Lattner28ad7542009-07-09 17:25:12 +00003715 // NOTE: a size of zero for a .comm should create a undefined symbol
3716 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003717 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003718 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003719 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003720
Eric Christopherbc818852010-05-14 01:38:54 +00003721 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003722 // may internally end up wanting an alignment in bytes.
3723 // FIXME: Diagnose overflow.
3724 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003725 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003726 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003727
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003728 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003729 return Error(IDLoc, "invalid symbol redefinition");
3730
Chris Lattner28ad7542009-07-09 17:25:12 +00003731 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003732 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003733 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003734 return false;
3735 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003737 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003738 return false;
3739}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003740
Jim Grosbach4b905842013-09-20 23:08:21 +00003741/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003742/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003743bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003744 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003745 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003746
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003747 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003748 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003749 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003750
Sean Callanan686ed8d2010-01-19 20:22:31 +00003751 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003752
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003753 if (Str.empty())
3754 Error(Loc, ".abort detected. Assembly stopping.");
3755 else
3756 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003757 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003758
3759 return false;
3760}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003761
Jim Grosbach4b905842013-09-20 23:08:21 +00003762/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003763/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003764bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003765 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003766 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003767
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003768 // Allow the strings to have escaped octal character sequence.
3769 std::string Filename;
3770 if (parseEscapedString(Filename))
3771 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003772 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003773 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003774
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003775 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003776 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003777
Chris Lattner693fbb82009-07-16 06:14:39 +00003778 // Attempt to switch the lexer to the included file before consuming the end
3779 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003780 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003781 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003782 return true;
3783 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003784
3785 return false;
3786}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003787
Jim Grosbach4b905842013-09-20 23:08:21 +00003788/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003789/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003790bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003791 if (getLexer().isNot(AsmToken::String))
3792 return TokError("expected string in '.incbin' directive");
3793
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003794 // Allow the strings to have escaped octal character sequence.
3795 std::string Filename;
3796 if (parseEscapedString(Filename))
3797 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003798 SMLoc IncbinLoc = getLexer().getLoc();
3799 Lex();
3800
3801 if (getLexer().isNot(AsmToken::EndOfStatement))
3802 return TokError("unexpected token in '.incbin' directive");
3803
Kevin Enderby109f25c2011-12-14 21:47:48 +00003804 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003805 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003806 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3807 return true;
3808 }
3809
3810 return false;
3811}
3812
Jim Grosbach4b905842013-09-20 23:08:21 +00003813/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003814/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3815bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003816 TheCondStack.push_back(TheCondState);
3817 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003818 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003819 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003820 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003821 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003822 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003823 return true;
3824
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003825 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003826 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003827
Sean Callanan686ed8d2010-01-19 20:22:31 +00003828 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003829
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003830 switch (DirKind) {
3831 default:
3832 llvm_unreachable("unsupported directive");
3833 case DK_IF:
3834 case DK_IFNE:
3835 break;
3836 case DK_IFEQ:
3837 ExprValue = ExprValue == 0;
3838 break;
3839 case DK_IFGE:
3840 ExprValue = ExprValue >= 0;
3841 break;
3842 case DK_IFGT:
3843 ExprValue = ExprValue > 0;
3844 break;
3845 case DK_IFLE:
3846 ExprValue = ExprValue <= 0;
3847 break;
3848 case DK_IFLT:
3849 ExprValue = ExprValue < 0;
3850 break;
3851 }
3852
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003853 TheCondState.CondMet = ExprValue;
3854 TheCondState.Ignore = !TheCondState.CondMet;
3855 }
3856
3857 return false;
3858}
3859
Jim Grosbach4b905842013-09-20 23:08:21 +00003860/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003861/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003862bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003863 TheCondStack.push_back(TheCondState);
3864 TheCondState.TheCond = AsmCond::IfCond;
3865
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003866 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003867 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003868 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003869 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003870
3871 if (getLexer().isNot(AsmToken::EndOfStatement))
3872 return TokError("unexpected token in '.ifb' directive");
3873
3874 Lex();
3875
3876 TheCondState.CondMet = ExpectBlank == Str.empty();
3877 TheCondState.Ignore = !TheCondState.CondMet;
3878 }
3879
3880 return false;
3881}
3882
Jim Grosbach4b905842013-09-20 23:08:21 +00003883/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003884/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003885/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003886bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003887 TheCondStack.push_back(TheCondState);
3888 TheCondState.TheCond = AsmCond::IfCond;
3889
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003890 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003891 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003892 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003893 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003894
3895 if (getLexer().isNot(AsmToken::Comma))
3896 return TokError("unexpected token in '.ifc' directive");
3897
3898 Lex();
3899
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003900 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003901
3902 if (getLexer().isNot(AsmToken::EndOfStatement))
3903 return TokError("unexpected token in '.ifc' directive");
3904
3905 Lex();
3906
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003907 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003908 TheCondState.Ignore = !TheCondState.CondMet;
3909 }
3910
3911 return false;
3912}
3913
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003914/// parseDirectiveIfeqs
3915/// ::= .ifeqs string1, string2
3916bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3917 if (Lexer.isNot(AsmToken::String)) {
3918 TokError("expected string parameter for '.ifeqs' directive");
3919 eatToEndOfStatement();
3920 return true;
3921 }
3922
3923 StringRef String1 = getTok().getStringContents();
3924 Lex();
3925
3926 if (Lexer.isNot(AsmToken::Comma)) {
3927 TokError("expected comma after first string for '.ifeqs' directive");
3928 eatToEndOfStatement();
3929 return true;
3930 }
3931
3932 Lex();
3933
3934 if (Lexer.isNot(AsmToken::String)) {
3935 TokError("expected string parameter for '.ifeqs' directive");
3936 eatToEndOfStatement();
3937 return true;
3938 }
3939
3940 StringRef String2 = getTok().getStringContents();
3941 Lex();
3942
3943 TheCondStack.push_back(TheCondState);
3944 TheCondState.TheCond = AsmCond::IfCond;
3945 TheCondState.CondMet = String1 == String2;
3946 TheCondState.Ignore = !TheCondState.CondMet;
3947
3948 return false;
3949}
3950
Jim Grosbach4b905842013-09-20 23:08:21 +00003951/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003952/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003953bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003954 StringRef Name;
3955 TheCondStack.push_back(TheCondState);
3956 TheCondState.TheCond = AsmCond::IfCond;
3957
3958 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003959 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003960 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003961 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003962 return TokError("expected identifier after '.ifdef'");
3963
3964 Lex();
3965
3966 MCSymbol *Sym = getContext().LookupSymbol(Name);
3967
3968 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003969 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003970 else
Craig Topper353eda42014-04-24 06:44:33 +00003971 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003972 TheCondState.Ignore = !TheCondState.CondMet;
3973 }
3974
3975 return false;
3976}
3977
Jim Grosbach4b905842013-09-20 23:08:21 +00003978/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003979/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003980bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003981 if (TheCondState.TheCond != AsmCond::IfCond &&
3982 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003983 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3984 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 TheCondState.TheCond = AsmCond::ElseIfCond;
3986
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003987 bool LastIgnoreState = false;
3988 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003989 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003990 if (LastIgnoreState || TheCondState.CondMet) {
3991 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003992 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003993 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003994 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003995 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003996 return true;
3997
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003998 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003999 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004000
Sean Callanan686ed8d2010-01-19 20:22:31 +00004001 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004002 TheCondState.CondMet = ExprValue;
4003 TheCondState.Ignore = !TheCondState.CondMet;
4004 }
4005
4006 return false;
4007}
4008
Jim Grosbach4b905842013-09-20 23:08:21 +00004009/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004010/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004011bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004012 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004013 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004014
Sean Callanan686ed8d2010-01-19 20:22:31 +00004015 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016
4017 if (TheCondState.TheCond != AsmCond::IfCond &&
4018 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004019 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4020 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004021 TheCondState.TheCond = AsmCond::ElseCond;
4022 bool LastIgnoreState = false;
4023 if (!TheCondStack.empty())
4024 LastIgnoreState = TheCondStack.back().Ignore;
4025 if (LastIgnoreState || TheCondState.CondMet)
4026 TheCondState.Ignore = true;
4027 else
4028 TheCondState.Ignore = false;
4029
4030 return false;
4031}
4032
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004033/// parseDirectiveEnd
4034/// ::= .end
4035bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4036 if (getLexer().isNot(AsmToken::EndOfStatement))
4037 return TokError("unexpected token in '.end' directive");
4038
4039 Lex();
4040
4041 while (Lexer.isNot(AsmToken::Eof))
4042 Lex();
4043
4044 return false;
4045}
4046
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004047/// parseDirectiveError
4048/// ::= .err
4049/// ::= .error [string]
4050bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4051 if (!TheCondStack.empty()) {
4052 if (TheCondStack.back().Ignore) {
4053 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004054 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004055 }
4056 }
4057
4058 if (!WithMessage)
4059 return Error(L, ".err encountered");
4060
4061 StringRef Message = ".error directive invoked in source file";
4062 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4063 if (Lexer.isNot(AsmToken::String)) {
4064 TokError(".error argument must be a string");
4065 eatToEndOfStatement();
4066 return true;
4067 }
4068
4069 Message = getTok().getStringContents();
4070 Lex();
4071 }
4072
4073 Error(L, Message);
4074 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004075}
4076
Jim Grosbach4b905842013-09-20 23:08:21 +00004077/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004078/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004079bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004080 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004081 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004082
Sean Callanan686ed8d2010-01-19 20:22:31 +00004083 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004084
Jim Grosbach4b905842013-09-20 23:08:21 +00004085 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004086 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4087 ".else");
4088 if (!TheCondStack.empty()) {
4089 TheCondState = TheCondStack.back();
4090 TheCondStack.pop_back();
4091 }
4092
4093 return false;
4094}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004095
Eli Bendersky17233942013-01-15 22:59:42 +00004096void AsmParser::initializeDirectiveKindMap() {
4097 DirectiveKindMap[".set"] = DK_SET;
4098 DirectiveKindMap[".equ"] = DK_EQU;
4099 DirectiveKindMap[".equiv"] = DK_EQUIV;
4100 DirectiveKindMap[".ascii"] = DK_ASCII;
4101 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4102 DirectiveKindMap[".string"] = DK_STRING;
4103 DirectiveKindMap[".byte"] = DK_BYTE;
4104 DirectiveKindMap[".short"] = DK_SHORT;
4105 DirectiveKindMap[".value"] = DK_VALUE;
4106 DirectiveKindMap[".2byte"] = DK_2BYTE;
4107 DirectiveKindMap[".long"] = DK_LONG;
4108 DirectiveKindMap[".int"] = DK_INT;
4109 DirectiveKindMap[".4byte"] = DK_4BYTE;
4110 DirectiveKindMap[".quad"] = DK_QUAD;
4111 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004112 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004113 DirectiveKindMap[".single"] = DK_SINGLE;
4114 DirectiveKindMap[".float"] = DK_FLOAT;
4115 DirectiveKindMap[".double"] = DK_DOUBLE;
4116 DirectiveKindMap[".align"] = DK_ALIGN;
4117 DirectiveKindMap[".align32"] = DK_ALIGN32;
4118 DirectiveKindMap[".balign"] = DK_BALIGN;
4119 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4120 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4121 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4122 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4123 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4124 DirectiveKindMap[".org"] = DK_ORG;
4125 DirectiveKindMap[".fill"] = DK_FILL;
4126 DirectiveKindMap[".zero"] = DK_ZERO;
4127 DirectiveKindMap[".extern"] = DK_EXTERN;
4128 DirectiveKindMap[".globl"] = DK_GLOBL;
4129 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004130 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4131 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4132 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4133 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4134 DirectiveKindMap[".reference"] = DK_REFERENCE;
4135 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4136 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4137 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4138 DirectiveKindMap[".comm"] = DK_COMM;
4139 DirectiveKindMap[".common"] = DK_COMMON;
4140 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4141 DirectiveKindMap[".abort"] = DK_ABORT;
4142 DirectiveKindMap[".include"] = DK_INCLUDE;
4143 DirectiveKindMap[".incbin"] = DK_INCBIN;
4144 DirectiveKindMap[".code16"] = DK_CODE16;
4145 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4146 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004147 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004148 DirectiveKindMap[".irp"] = DK_IRP;
4149 DirectiveKindMap[".irpc"] = DK_IRPC;
4150 DirectiveKindMap[".endr"] = DK_ENDR;
4151 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4152 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4153 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4154 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004155 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4156 DirectiveKindMap[".ifge"] = DK_IFGE;
4157 DirectiveKindMap[".ifgt"] = DK_IFGT;
4158 DirectiveKindMap[".ifle"] = DK_IFLE;
4159 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004160 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004161 DirectiveKindMap[".ifb"] = DK_IFB;
4162 DirectiveKindMap[".ifnb"] = DK_IFNB;
4163 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004164 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004165 DirectiveKindMap[".ifnc"] = DK_IFNC;
4166 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4167 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4168 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4169 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4170 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004171 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004172 DirectiveKindMap[".endif"] = DK_ENDIF;
4173 DirectiveKindMap[".skip"] = DK_SKIP;
4174 DirectiveKindMap[".space"] = DK_SPACE;
4175 DirectiveKindMap[".file"] = DK_FILE;
4176 DirectiveKindMap[".line"] = DK_LINE;
4177 DirectiveKindMap[".loc"] = DK_LOC;
4178 DirectiveKindMap[".stabs"] = DK_STABS;
4179 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4180 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4181 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4182 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4183 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4184 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4185 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4186 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4187 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4188 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4189 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4190 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4191 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4192 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4193 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4194 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4195 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4196 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4197 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4198 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4199 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004200 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004201 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4202 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4203 DirectiveKindMap[".macro"] = DK_MACRO;
4204 DirectiveKindMap[".endm"] = DK_ENDM;
4205 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4206 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004207 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004208 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004209}
4210
Jim Grosbach4b905842013-09-20 23:08:21 +00004211MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004212 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004213
Rafael Espindola34b9c512012-06-03 23:57:14 +00004214 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004215 for (;;) {
4216 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004217 if (getLexer().is(AsmToken::Eof)) {
4218 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004219 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004220 }
4221
Rafael Espindola34b9c512012-06-03 23:57:14 +00004222 if (Lexer.is(AsmToken::Identifier) &&
4223 (getTok().getIdentifier() == ".rept")) {
4224 ++NestLevel;
4225 }
4226
4227 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004228 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004229 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004230 EndToken = getTok();
4231 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004232 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4233 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004234 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004235 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004236 break;
4237 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004238 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004239 }
4240
Rafael Espindola34b9c512012-06-03 23:57:14 +00004241 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004242 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004243 }
4244
4245 const char *BodyStart = StartToken.getLoc().getPointer();
4246 const char *BodyEnd = EndToken.getLoc().getPointer();
4247 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4248
Rafael Espindola34b9c512012-06-03 23:57:14 +00004249 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004250 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004251 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004252}
4253
Jim Grosbach4b905842013-09-20 23:08:21 +00004254void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004255 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004256 OS << ".endr\n";
4257
4258 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004259 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004260
Rafael Espindola34b9c512012-06-03 23:57:14 +00004261 // Create the macro instantiation object and add to the current macro
4262 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004263 MacroInstantiation *MI = new MacroInstantiation(
4264 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004265 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004266
Rafael Espindola34b9c512012-06-03 23:57:14 +00004267 // Jump to the macro instantiation and prime the lexer.
4268 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4269 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4270 Lex();
4271}
4272
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004273/// parseDirectiveRept
4274/// ::= .rep | .rept count
4275bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004276 const MCExpr *CountExpr;
4277 SMLoc CountLoc = getTok().getLoc();
4278 if (parseExpression(CountExpr))
4279 return true;
4280
Rafael Espindola34b9c512012-06-03 23:57:14 +00004281 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004282 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4283 eatToEndOfStatement();
4284 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4285 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004286
4287 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004288 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004289
4290 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004291 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004292
4293 // Eat the end of statement.
4294 Lex();
4295
4296 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004297 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004298 if (!M)
4299 return true;
4300
4301 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4302 // to hold the macro body with substitutions.
4303 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004304 raw_svector_ostream OS(Buf);
4305 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004306 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004307 return true;
4308 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004309 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004310
4311 return false;
4312}
4313
Jim Grosbach4b905842013-09-20 23:08:21 +00004314/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004315/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004316bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004317 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004318
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004319 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004320 return TokError("expected identifier in '.irp' directive");
4321
Rafael Espindola768b41c2012-06-15 14:02:34 +00004322 if (Lexer.isNot(AsmToken::Comma))
4323 return TokError("expected comma in '.irp' directive");
4324
4325 Lex();
4326
Eli Bendersky38274122013-01-14 23:22:36 +00004327 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004328 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004329 return true;
4330
4331 // Eat the end of statement.
4332 Lex();
4333
4334 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004335 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004336 if (!M)
4337 return true;
4338
4339 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4340 // to hold the macro body with substitutions.
4341 SmallString<256> Buf;
4342 raw_svector_ostream OS(Buf);
4343
Eli Bendersky38274122013-01-14 23:22:36 +00004344 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004345 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004346 return true;
4347 }
4348
Jim Grosbach4b905842013-09-20 23:08:21 +00004349 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004350
4351 return false;
4352}
4353
Jim Grosbach4b905842013-09-20 23:08:21 +00004354/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004355/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004356bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004357 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004358
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004359 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004360 return TokError("expected identifier in '.irpc' directive");
4361
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004362 if (Lexer.isNot(AsmToken::Comma))
4363 return TokError("expected comma in '.irpc' directive");
4364
4365 Lex();
4366
Eli Bendersky38274122013-01-14 23:22:36 +00004367 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004368 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004369 return true;
4370
4371 if (A.size() != 1 || A.front().size() != 1)
4372 return TokError("unexpected token in '.irpc' directive");
4373
4374 // Eat the end of statement.
4375 Lex();
4376
4377 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004378 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004379 if (!M)
4380 return true;
4381
4382 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4383 // to hold the macro body with substitutions.
4384 SmallString<256> Buf;
4385 raw_svector_ostream OS(Buf);
4386
4387 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004388 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004389 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004390 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004391
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004392 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004393 return true;
4394 }
4395
Jim Grosbach4b905842013-09-20 23:08:21 +00004396 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004397
4398 return false;
4399}
4400
Jim Grosbach4b905842013-09-20 23:08:21 +00004401bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004402 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004403 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004404
4405 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004406 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004407 assert(getLexer().is(AsmToken::EndOfStatement));
4408
Jim Grosbach4b905842013-09-20 23:08:21 +00004409 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004410 return false;
4411}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004412
Jim Grosbach4b905842013-09-20 23:08:21 +00004413bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004414 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004415 const MCExpr *Value;
4416 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004417 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004418 return true;
4419 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4420 if (!MCE)
4421 return Error(ExprLoc, "unexpected expression in _emit");
4422 uint64_t IntValue = MCE->getValue();
4423 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4424 return Error(ExprLoc, "literal value out of range for directive");
4425
Chad Rosierc7f552c2013-02-12 21:33:51 +00004426 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4427 return false;
4428}
4429
Jim Grosbach4b905842013-09-20 23:08:21 +00004430bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004431 const MCExpr *Value;
4432 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004433 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004434 return true;
4435 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4436 if (!MCE)
4437 return Error(ExprLoc, "unexpected expression in align");
4438 uint64_t IntValue = MCE->getValue();
4439 if (!isPowerOf2_64(IntValue))
4440 return Error(ExprLoc, "literal value not a power of two greater then zero");
4441
Jim Grosbach4b905842013-09-20 23:08:21 +00004442 Info.AsmRewrites->push_back(
4443 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004444 return false;
4445}
4446
Chad Rosierf43fcf52013-02-13 21:27:17 +00004447// We are comparing pointers, but the pointers are relative to a single string.
4448// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004449static int rewritesSort(const AsmRewrite *AsmRewriteA,
4450 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004451 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4452 return -1;
4453 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4454 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004455
Chad Rosierfce4fab2013-04-08 17:43:47 +00004456 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4457 // rewrite to the same location. Make sure the SizeDirective rewrite is
4458 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4459 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004460 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4461 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004462 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004463
Jim Grosbach4b905842013-09-20 23:08:21 +00004464 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4465 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004466 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004467 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004468}
4469
Jim Grosbach4b905842013-09-20 23:08:21 +00004470bool AsmParser::parseMSInlineAsm(
4471 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4472 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4473 SmallVectorImpl<std::string> &Constraints,
4474 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4475 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004476 SmallVector<void *, 4> InputDecls;
4477 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004478 SmallVector<bool, 4> InputDeclsAddressOf;
4479 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004480 SmallVector<std::string, 4> InputConstraints;
4481 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004482 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004483
Benjamin Kramer1a136112013-02-15 20:37:21 +00004484 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004485
4486 // Prime the lexer.
4487 Lex();
4488
4489 // While we have input, parse each statement.
4490 unsigned InputIdx = 0;
4491 unsigned OutputIdx = 0;
4492 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004493 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004494 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004495 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004496
Chad Rosier149e8e02012-12-12 22:45:52 +00004497 if (Info.ParseError)
4498 return true;
4499
Benjamin Kramer1a136112013-02-15 20:37:21 +00004500 if (Info.Opcode == ~0U)
4501 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004502
Benjamin Kramer1a136112013-02-15 20:37:21 +00004503 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004504
Benjamin Kramer1a136112013-02-15 20:37:21 +00004505 // Build the list of clobbers, outputs and inputs.
4506 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004507 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004508
Benjamin Kramer1a136112013-02-15 20:37:21 +00004509 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004510 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004511 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004512
Benjamin Kramer1a136112013-02-15 20:37:21 +00004513 // Register operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00004514 if (Operand.isReg() && !Operand.needAddressOf()) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004515 unsigned NumDefs = Desc.getNumDefs();
4516 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004517 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4518 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004519 continue;
4520 }
4521
4522 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004523 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004524 if (SymName.empty())
4525 continue;
4526
David Blaikie960ea3f2014-06-08 16:18:35 +00004527 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004528 if (!OpDecl)
4529 continue;
4530
4531 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004532 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004533 if (isOutput) {
4534 ++InputIdx;
4535 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004536 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4537 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004538 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004539 } else {
4540 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004541 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4542 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004543 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004544 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004545 }
Reid Kleckneree088972013-12-10 18:27:32 +00004546
4547 // Consider implicit defs to be clobbers. Think of cpuid and push.
4548 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4549 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4550 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004551 }
4552
4553 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004554 NumOutputs = OutputDecls.size();
4555 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004556
4557 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4559 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4560 ClobberRegs.end());
4561 Clobbers.assign(ClobberRegs.size(), std::string());
4562 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4563 raw_string_ostream OS(Clobbers[I]);
4564 IP->printRegName(OS, ClobberRegs[I]);
4565 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004566
4567 // Merge the various outputs and inputs. Output are expected first.
4568 if (NumOutputs || NumInputs) {
4569 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004570 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004571 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004572 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004573 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004574 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004575 }
4576 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004577 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004578 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004579 }
4580 }
4581
4582 // Build the IR assembly string.
4583 std::string AsmStringIR;
4584 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004585 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4586 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004587 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004588 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4589 E = AsmStrRewrites.end();
4590 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004591 AsmRewriteKind Kind = (*I).Kind;
4592 if (Kind == AOK_Delete)
4593 continue;
4594
Chad Rosier8bce6642012-10-18 15:49:34 +00004595 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004596 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004597
Chad Rosier120eefd2013-03-19 17:32:17 +00004598 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004599 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004600 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004601 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004602
Chad Rosier37e755c2012-10-23 17:43:43 +00004603 // Skip the original expression.
4604 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004605 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004606 continue;
4607 }
4608
Chad Rosierff10ed12013-04-12 16:26:42 +00004609 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004610 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004611 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004612 default:
4613 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004614 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004616 break;
4617 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004618 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004619 break;
4620 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004621 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004622 break;
4623 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004624 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004625 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004626 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004627 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004628 default: break;
4629 case 8: OS << "byte ptr "; break;
4630 case 16: OS << "word ptr "; break;
4631 case 32: OS << "dword ptr "; break;
4632 case 64: OS << "qword ptr "; break;
4633 case 80: OS << "xword ptr "; break;
4634 case 128: OS << "xmmword ptr "; break;
4635 case 256: OS << "ymmword ptr "; break;
4636 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004637 break;
4638 case AOK_Emit:
4639 OS << ".byte";
4640 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004641 case AOK_Align: {
4642 unsigned Val = (*I).Val;
4643 OS << ".align " << Val;
4644
4645 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004646 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004647 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4648 break;
4649 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004650 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004651 // Insert the dot if the user omitted it.
4652 OS.flush();
4653 if (AsmStringIR.back() != '.')
4654 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004655 OS << (*I).Val;
4656 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004657 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004658
Chad Rosier8bce6642012-10-18 15:49:34 +00004659 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004660 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004661 }
4662
4663 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004664 if (AsmStart != AsmEnd)
4665 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004666
4667 AsmString = OS.str();
4668 return false;
4669}
4670
Daniel Dunbar01e36072010-07-17 02:26:10 +00004671/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004672MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4673 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004674 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004675}