blob: 62ab4a567a12f922d1b53ec01f9deb8b3fc5aa08 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000048static cl::opt<bool>
49FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
51
Eric Christophera7c32732012-12-18 00:30:54 +000052MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000053
Daniel Dunbar86033402010-07-12 17:54:38 +000054namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000058
59struct MCAsmMacroParameter {
60 StringRef Name;
61 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000066};
67
Eli Benderskya313ae62013-01-16 18:56:50 +000068typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
69
70struct MCAsmMacro {
71 StringRef Name;
72 StringRef Body;
73 MCAsmMacroParameters Parameters;
74
75public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000076 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000077 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000078};
79
Daniel Dunbar43235712010-07-18 18:54:11 +000080/// \brief Helper class for storing information about an active macro
81/// instantiation.
82struct MacroInstantiation {
83 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000084 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000085
86 /// The macro instantiation with substitutions.
87 MemoryBuffer *Instantiation;
88
89 /// The location of the instantiation.
90 SMLoc InstantiationLoc;
91
Daniel Dunbar40f1d852012-12-01 01:38:48 +000092 /// The buffer where parsing should resume upon instantiation completion.
93 int ExitBuffer;
94
Daniel Dunbar43235712010-07-18 18:54:11 +000095 /// The location where parsing should resume upon instantiation completion.
96 SMLoc ExitLoc;
97
98public:
Eli Bendersky38274122013-01-14 23:22:36 +000099 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000100 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +0000101};
102
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000105 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 unsigned Opcode;
109
Jim Grosbach4b905842013-09-20 23:08:21 +0000110 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 bool ParseError;
112
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
114
Craig Topper353eda42014-04-24 06:44:33 +0000115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000118};
119
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000133
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000136 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000165 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000171 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
Craig Topper59be68f2014-03-08 07:14:16 +0000188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
Craig Topper59be68f2014-03-08 07:14:16 +0000199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Craig Topper59be68f2014-03-08 07:14:16 +0000222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseIdentifier(StringRef &Res) override;
242 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Craig Topper59be68f2014-03-08 07:14:16 +0000244 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
257 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000313 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000334
Eli Bendersky17233942013-01-15 22:59:42 +0000335 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000336 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000358 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000360 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000361 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000362 };
363
Jim Grosbach4b905842013-09-20 23:08:21 +0000364 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367
368 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000371 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
373 bool parseDirectiveFill(); // ".fill"
374 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000375 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
377 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000380
Eli Bendersky17233942013-01-15 22:59:42 +0000381 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveFile(SMLoc DirectiveLoc);
383 bool parseDirectiveLine();
384 bool parseDirectiveLoc();
385 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000386
387 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000389 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000390 bool parseDirectiveCFISections();
391 bool parseDirectiveCFIStartProc();
392 bool parseDirectiveCFIEndProc();
393 bool parseDirectiveCFIDefCfaOffset();
394 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIAdjustCfaOffset();
396 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
400 bool parseDirectiveCFIRememberState();
401 bool parseDirectiveCFIRestoreState();
402 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIEscape();
405 bool parseDirectiveCFISignalFrame();
406 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000407
408 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveEndMacro(StringRef Directive);
411 bool parseDirectiveMacro(SMLoc DirectiveLoc);
412 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420
Eli Bendersky17233942013-01-15 22:59:42 +0000421 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000423
424 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveAbort(); // ".abort"
434 bool parseDirectiveInclude(); // ".include"
435 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000437 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
438 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000439 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000441 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000443 // ".ifeqs"
444 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000445 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
447 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
448 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
449 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000450 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000454
Rafael Espindola34b9c512012-06-03 23:57:14 +0000455 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
457 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000458 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000459 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
461 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
462 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000463
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 size_t Len);
467
468 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000470
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000471 // "end"
472 bool parseDirectiveEnd(SMLoc DirectiveLoc);
473
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000474 // ".err" or ".error"
475 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000476
Eli Bendersky17233942013-01-15 22:59:42 +0000477 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000478};
Daniel Dunbar86033402010-07-12 17:54:38 +0000479}
480
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000481namespace llvm {
482
483extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000484extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000485extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486
487}
488
Chris Lattnerc35681b2010-01-19 19:46:13 +0000489enum { DEFAULT_ADDRSPACE = 0 };
490
Jim Grosbach4b905842013-09-20 23:08:21 +0000491AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
492 const MCAsmInfo &_MAI)
493 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000494 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
495 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
496 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000497 // Save the old handler.
498 SavedDiagHandler = SrcMgr.getDiagHandler();
499 SavedDiagContext = SrcMgr.getDiagContext();
500 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000501 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000503
Daniel Dunbarc5011082010-07-12 18:12:02 +0000504 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000505 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
506 case MCObjectFileInfo::IsCOFF:
507 PlatformParser = createCOFFAsmParser();
508 PlatformParser->Initialize(*this);
509 break;
510 case MCObjectFileInfo::IsMachO:
511 PlatformParser = createDarwinAsmParser();
512 PlatformParser->Initialize(*this);
513 IsDarwin = true;
514 break;
515 case MCObjectFileInfo::IsELF:
516 PlatformParser = createELFAsmParser();
517 PlatformParser->Initialize(*this);
518 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000519 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000520
Eli Bendersky17233942013-01-15 22:59:42 +0000521 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000522}
523
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000527
528 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
530 ie = MacroMap.end();
531 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000532 delete it->getValue();
533
Daniel Dunbarc5011082010-07-12 18:12:02 +0000534 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000553 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000569 unsigned NewBuf =
570 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
571 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000573
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 return false;
577}
Daniel Dunbar43235712010-07-18 18:54:11 +0000578
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000579/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000580/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000582bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000584 unsigned NewBuf =
585 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Alp Tokera55b95b2014-07-06 10:33:31 +0000594void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
595 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
597 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000598}
599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600const AsmToken &AsmParser::Lex() {
601 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603 if (tok->is(AsmToken::Eof)) {
604 // If this is the end of an included file, pop the parent file off the
605 // include stack.
606 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
607 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000608 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 tok = &Lexer.Lex();
610 }
611 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000614 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617}
618
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000619bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000620 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000621 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000622 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000623
Chris Lattner36e02122009-06-21 20:54:55 +0000624 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000625 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000626
627 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000628 AsmCond StartingCondState = TheCondState;
629
Kevin Enderby6469fc22011-11-01 22:27:22 +0000630 // If we are generating dwarf for assembly source files save the initial text
631 // section and generate a .file directive.
632 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
634 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000635 auto InsertResult = getContext().addGenDwarfSection(
636 getStreamer().getCurrentSection().first);
637 assert(InsertResult.second && ".text section should not have debug info yet");
638 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000639 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
640 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000641 }
642
Chris Lattner73f36112009-07-02 21:53:43 +0000643 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000644 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000645 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000646 if (!parseStatement(Info))
647 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000648
Daniel Dunbar43325c42010-09-09 22:42:56 +0000649 // We had an error, validate that one was emitted and recover by skipping to
650 // the next line.
651 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000652 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000653 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000654
655 if (TheCondState.TheCond != StartingCondState.TheCond ||
656 TheCondState.Ignore != StartingCondState.Ignore)
657 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000658
659 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000660 const auto &LineTables = getContext().getMCDwarfLineTables();
661 if (!LineTables.empty()) {
662 unsigned Index = 0;
663 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
664 if (File.Name.empty() && Index != 0)
665 TokError("unassigned file number: " + Twine(Index) +
666 " for .file directives");
667 ++Index;
668 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000670
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 // Check to see that all assembler local symbols were actually defined.
672 // Targets that don't do subsections via symbols may not want this, though,
673 // so conservatively exclude them. Only do this if we're finalizing, though,
674 // as otherwise we won't necessarilly have seen everything yet.
675 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
676 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
677 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 i != e; ++i) {
680 MCSymbol *Sym = i->getValue();
681 // Variable symbols may not be marked as defined, so check those
682 // explicitly. If we know it's a variable, we have a definition for
683 // the purposes of this check.
684 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
685 // FIXME: We would really like to refer back to where the symbol was
686 // first referenced for a source location. We need to add something
687 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000688 printMessage(
689 getLexer().getLoc(), SourceMgr::DK_Error,
690 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000691 }
692 }
693
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000694 // Finalize the output stream if there are no errors and if the client wants
695 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000696 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000697 Out.Finish();
698
Chris Lattner73f36112009-07-02 21:53:43 +0000699 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000700}
701
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000703 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000705 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000706 }
707}
708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000711 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000712 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000713
Chris Lattnere5074c42009-06-22 01:29:09 +0000714 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000715 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000716 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000717}
718
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000719StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 const char *Start = getTok().getLoc().getPointer();
721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 Lex();
724
725 const char *End = getTok().getLoc().getPointer();
726 return StringRef(Start, End - Start);
727}
Chris Lattner78db3622009-06-22 05:51:26 +0000728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000730 const char *Start = getTok().getLoc().getPointer();
731
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000733 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
739
Jim Grosbach4b905842013-09-20 23:08:21 +0000740/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741/// NOTE: This assumes the leading '(' has already been consumed.
742///
743/// parenexpr ::= expr)
744///
Jim Grosbach4b905842013-09-20 23:08:21 +0000745bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
746 if (parseExpression(Res))
747 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000748 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000750 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000751 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return false;
753}
Chris Lattner78db3622009-06-22 05:51:26 +0000754
Jim Grosbach4b905842013-09-20 23:08:21 +0000755/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756/// NOTE: This assumes the leading '[' has already been consumed.
757///
758/// bracketexpr ::= expr]
759///
Jim Grosbach4b905842013-09-20 23:08:21 +0000760bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
761 if (parseExpression(Res))
762 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 if (Lexer.isNot(AsmToken::RBrac))
764 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000765 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 Lex();
767 return false;
768}
769
Jim Grosbach4b905842013-09-20 23:08:21 +0000770/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000771/// primaryexpr ::= (parenexpr
772/// primaryexpr ::= symbol
773/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000774/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000775/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000776bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000777 SMLoc FirstTokenLoc = getLexer().getLoc();
778 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
779 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000780 default:
781 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000782 // If we have an error assume that we've already handled it.
783 case AsmToken::Error:
784 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000785 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000786 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000787 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000789 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000790 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000791 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000792 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000793 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000794 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000795 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000796 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 if (FirstTokenKind == AsmToken::Dollar) {
798 if (Lexer.getMAI().getDollarIsPC()) {
799 // This is a '$' reference, which references the current PC. Emit a
800 // temporary label to the streamer and refer to it.
801 MCSymbol *Sym = Ctx.CreateTempSymbol();
802 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000803 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
804 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 EndLoc = FirstTokenLoc;
806 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000807 }
808 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000810 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000811 // Parse symbol variant
812 std::pair<StringRef, StringRef> Split;
813 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000814 if (FirstTokenKind == AsmToken::String) {
815 if (Lexer.is(AsmToken::At)) {
816 Lexer.Lex(); // eat @
817 SMLoc AtLoc = getLexer().getLoc();
818 StringRef VName;
819 if (parseIdentifier(VName))
820 return Error(AtLoc, "expected symbol variant after '@'");
821
822 Split = std::make_pair(Identifier, VName);
823 }
824 } else {
825 Split = Identifier.split('@');
826 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000827 } else if (Lexer.is(AsmToken::LParen)) {
828 Lexer.Lex(); // eat (
829 StringRef VName;
830 parseIdentifier(VName);
831 if (Lexer.isNot(AsmToken::RParen)) {
832 return Error(Lexer.getTok().getLoc(),
833 "unexpected token in variant, expected ')'");
834 }
835 Lexer.Lex(); // eat )
836 Split = std::make_pair(Identifier, VName);
837 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000838
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000839 EndLoc = SMLoc::getFromPointer(Identifier.end());
840
Daniel Dunbard20cda02009-10-16 01:34:54 +0000841 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000842 StringRef SymbolName = Identifier;
843 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000844
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000845 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000846 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000847 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000848 if (Variant != MCSymbolRefExpr::VK_Invalid) {
849 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000850 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 Variant = MCSymbolRefExpr::VK_None;
852 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000853 return Error(SMLoc::getFromPointer(Split.second.begin()),
854 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000855 }
856 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000857
Hans Wennborgce69d772013-10-18 20:46:28 +0000858 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
859
Daniel Dunbard20cda02009-10-16 01:34:54 +0000860 // If this is an absolute variable reference, substitute it now to preserve
861 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000862 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000863 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000864 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000865
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000866 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000867 return false;
868 }
869
870 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000871 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000872 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000873 }
David Woodhousef42a6662014-02-01 16:20:54 +0000874 case AsmToken::BigNum:
875 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000876 case AsmToken::Integer: {
877 SMLoc Loc = getTok().getLoc();
878 int64_t IntVal = getTok().getIntVal();
879 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000880 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000881 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000882 // Look for 'b' or 'f' following an Integer as a directional label
883 if (Lexer.getKind() == AsmToken::Identifier) {
884 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 // Lookup the symbol variant if used.
886 std::pair<StringRef, StringRef> Split = IDVal.split('@');
887 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
888 if (Split.first.size() != IDVal.size()) {
889 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000890 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000891 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000892 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000893 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000894 if (IDVal == "f" || IDVal == "b") {
895 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000896 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000897 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000898 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000899 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000900 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000901 Lex(); // Eat identifier.
902 }
903 }
Chris Lattner78db3622009-06-22 05:51:26 +0000904 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000905 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000906 case AsmToken::Real: {
907 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000908 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000910 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000911 Lex(); // Eat token.
912 return false;
913 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000914 case AsmToken::Dot: {
915 // This is a '.' reference, which references the current PC. Emit a
916 // temporary label to the streamer and refer to it.
917 MCSymbol *Sym = Ctx.CreateTempSymbol();
918 Out.EmitLabel(Sym);
919 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000920 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000921 Lex(); // Eat identifier.
922 return false;
923 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000927 case AsmToken::LBrac:
928 if (!PlatformParser->HasBracketExpressions())
929 return TokError("brackets expression not supported on this target");
930 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000936 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000938 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000939 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000940 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000942 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000943 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000944 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000945 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000946 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000947 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000948 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000949 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000950 }
951}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000952
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000953bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000954 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000955 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000956}
957
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000959AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000960 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000961 // Ask the target implementation about this expression first.
962 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
963 if (NewE)
964 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965 // Recurse over the given expression, rebuilding it to apply the given variant
966 // if there is exactly one symbol.
967 switch (E->getKind()) {
968 case MCExpr::Target:
969 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000970 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000971
972 case MCExpr::SymbolRef: {
973 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
974
975 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000976 TokError("invalid variant on expression '" + getTok().getIdentifier() +
977 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000978 return E;
979 }
980
981 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
982 }
983
984 case MCExpr::Unary: {
985 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000988 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
990 }
991
992 case MCExpr::Binary: {
993 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000994 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
995 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
997 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000998 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
Jim Grosbach4b905842013-09-20 23:08:21 +00001000 if (!LHS)
1001 LHS = BE->getLHS();
1002 if (!RHS)
1003 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004
1005 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1006 }
1007 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001008
Craig Toppera2886c22012-02-07 05:05:23 +00001009 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001010}
1011
Jim Grosbach4b905842013-09-20 23:08:21 +00001012/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001013///
Jim Grosbachbd164242011-08-20 16:24:13 +00001014/// expr ::= expr &&,|| expr -> lowest.
1015/// expr ::= expr |,^,&,! expr
1016/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1017/// expr ::= expr <<,>> expr
1018/// expr ::= expr +,- expr
1019/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001020/// expr ::= primaryexpr
1021///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001022bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001023 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001024 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001025 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001026 return true;
1027
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 // As a special case, we support 'a op b @ modifier' by rewriting the
1029 // expression to include the modifier. This is inefficient, but in general we
1030 // expect users to use 'a@modifier op b'.
1031 if (Lexer.getKind() == AsmToken::At) {
1032 Lex();
1033
1034 if (Lexer.isNot(AsmToken::Identifier))
1035 return TokError("unexpected symbol modifier following '@'");
1036
1037 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001038 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039 if (Variant == MCSymbolRefExpr::VK_Invalid)
1040 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1041
Jim Grosbach4b905842013-09-20 23:08:21 +00001042 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001043 if (!ModifiedRes) {
1044 return TokError("invalid modifier '" + getTok().getIdentifier() +
1045 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001047
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 Res = ModifiedRes;
1049 Lex();
1050 }
1051
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001052 // Try to constant fold it up front, if possible.
1053 int64_t Value;
1054 if (Res->EvaluateAsAbsolute(Value))
1055 Res = MCConstantExpr::Create(Value, getContext());
1056
1057 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001058}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001059
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001060bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001061 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001062 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001063}
1064
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001065bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001066 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001067
Daniel Dunbar75630b32009-06-30 02:10:03 +00001068 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001069 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001070 return true;
1071
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001072 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001073 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001074
1075 return false;
1076}
1077
Michael J. Spencer530ce852010-10-09 11:00:50 +00001078static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001079 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001080 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001081 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001082 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001085 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001087 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001090 return 1;
1091
Jim Grosbach4b905842013-09-20 23:08:21 +00001092 // Low Precedence: |, &, ^
1093 //
1094 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001095 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001104
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001106 case AsmToken::EqualEqual:
1107 Kind = MCBinaryExpr::EQ;
1108 return 3;
1109 case AsmToken::ExclaimEqual:
1110 case AsmToken::LessGreater:
1111 Kind = MCBinaryExpr::NE;
1112 return 3;
1113 case AsmToken::Less:
1114 Kind = MCBinaryExpr::LT;
1115 return 3;
1116 case AsmToken::LessEqual:
1117 Kind = MCBinaryExpr::LTE;
1118 return 3;
1119 case AsmToken::Greater:
1120 Kind = MCBinaryExpr::GT;
1121 return 3;
1122 case AsmToken::GreaterEqual:
1123 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001124 return 3;
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001127 case AsmToken::LessLess:
1128 Kind = MCBinaryExpr::Shl;
1129 return 4;
1130 case AsmToken::GreaterGreater:
1131 Kind = MCBinaryExpr::Shr;
1132 return 4;
1133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001135 case AsmToken::Plus:
1136 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138 case AsmToken::Minus:
1139 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001141
Jim Grosbach4b905842013-09-20 23:08:21 +00001142 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001143 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001144 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001149 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001151 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 }
1153}
1154
Jim Grosbach4b905842013-09-20 23:08:21 +00001155/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001157bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001158 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001160 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // If the next token is lower precedence than we are allowed to eat, return
1164 // successfully with what we ate already.
1165 if (TokPrec < Precedence)
1166 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Sean Callanan686ed8d2010-01-19 20:22:31 +00001168 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001169
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001171 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001172 if (parsePrimaryExpr(RHS, EndLoc))
1173 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001174
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001175 // If BinOp binds less tightly with RHS than the operator after RHS, let
1176 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001177 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001178 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001179 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1180 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001181
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001182 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001183 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001184 }
1185}
1186
Chris Lattner36e02122009-06-21 20:54:55 +00001187/// ParseStatement:
1188/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001189/// ::= Label* Directive ...Operands... EndOfStatement
1190/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001191bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001192 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001193 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001194 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001195 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001196 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001197
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001198 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001199 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001200 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001201 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001202 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001203 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001204 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001205 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001206
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001207 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001208 if (Lexer.is(AsmToken::Integer)) {
1209 LocalLabelVal = getTok().getIntVal();
1210 if (LocalLabelVal < 0) {
1211 if (!TheCondState.Ignore)
1212 return TokError("unexpected token at start of statement");
1213 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001214 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001215 IDVal = getTok().getString();
1216 Lex(); // Consume the integer token to be used as an identifier token.
1217 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001218 if (!TheCondState.Ignore)
1219 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001220 }
1221 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001222 } else if (Lexer.is(AsmToken::Dot)) {
1223 // Treat '.' as a valid identifier in this context.
1224 Lex();
1225 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001226 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001227 if (!TheCondState.Ignore)
1228 return TokError("unexpected token at start of statement");
1229 IDVal = "";
1230 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001231
Chris Lattner926885c2010-04-17 18:14:27 +00001232 // Handle conditional assembly here before checking for skipping. We
1233 // have to do this so that .endif isn't skipped in a ".if 0" block for
1234 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001235 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001236 DirectiveKindMap.find(IDVal);
1237 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1238 ? DK_NO_DIRECTIVE
1239 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001240 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001241 default:
1242 break;
1243 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001244 case DK_IFEQ:
1245 case DK_IFGE:
1246 case DK_IFGT:
1247 case DK_IFLE:
1248 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001249 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001250 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001251 case DK_IFB:
1252 return parseDirectiveIfb(IDLoc, true);
1253 case DK_IFNB:
1254 return parseDirectiveIfb(IDLoc, false);
1255 case DK_IFC:
1256 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001257 case DK_IFEQS:
1258 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001259 case DK_IFNC:
1260 return parseDirectiveIfc(IDLoc, false);
1261 case DK_IFDEF:
1262 return parseDirectiveIfdef(IDLoc, true);
1263 case DK_IFNDEF:
1264 case DK_IFNOTDEF:
1265 return parseDirectiveIfdef(IDLoc, false);
1266 case DK_ELSEIF:
1267 return parseDirectiveElseIf(IDLoc);
1268 case DK_ELSE:
1269 return parseDirectiveElse(IDLoc);
1270 case DK_ENDIF:
1271 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001272 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001273
Eli Bendersky88024712013-01-16 19:32:36 +00001274 // Ignore the statement if in the middle of inactive conditional
1275 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001276 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001277 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001278 return false;
1279 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001280
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001281 // FIXME: Recurse on local labels?
1282
1283 // See what kind of statement we have.
1284 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001285 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001286 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001287
Chris Lattner36e02122009-06-21 20:54:55 +00001288 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001289 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001290
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001291 // Diagnose attempt to use '.' as a label.
1292 if (IDVal == ".")
1293 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1294
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001295 // Diagnose attempt to use a variable as a label.
1296 //
1297 // FIXME: Diagnostics. Note the location of the definition as a label.
1298 // FIXME: This doesn't diagnose assignment to a symbol which has been
1299 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001300 MCSymbol *Sym;
1301 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001302 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001303 else
1304 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001305 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001306 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001307
Daniel Dunbare73b2672009-08-26 22:13:22 +00001308 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001309 if (!ParsingInlineAsm)
1310 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001311
Kevin Enderbye7739d42011-12-09 18:09:40 +00001312 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001313 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001314 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001315 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1316 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317
Tim Northover1744d0a2013-10-25 12:49:50 +00001318 getTargetParser().onLabelParsed(Sym);
1319
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001320 // Consume any end of statement token, if present, to avoid spurious
1321 // AddBlankLine calls().
1322 if (Lexer.is(AsmToken::EndOfStatement)) {
1323 Lex();
1324 if (Lexer.is(AsmToken::Eof))
1325 return false;
1326 }
1327
Eli Friedman0f4871d2012-10-22 23:58:19 +00001328 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001329 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001330
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001331 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001332 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001333 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001334
Jim Grosbach4b905842013-09-20 23:08:21 +00001335 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001336
1337 default: // Normal instruction or directive.
1338 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001339 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001340
1341 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001342 if (areMacrosEnabled())
1343 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1344 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001345 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001346
Michael J. Spencer530ce852010-10-09 11:00:50 +00001347 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001348
Eli Bendersky17233942013-01-15 22:59:42 +00001349 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001350 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001351 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001352 //
Eli Bendersky17233942013-01-15 22:59:42 +00001353 // 1. The target-specific assembly parser. Some directives are target
1354 // specific or may potentially behave differently on certain targets.
1355 // 2. Asm parser extensions. For example, platform-specific parsers
1356 // (like the ELF parser) register themselves as extensions.
1357 // 3. The generic directive parser implemented by this class. These are
1358 // all the directives that behave in a target and platform independent
1359 // manner, or at least have a default behavior that's shared between
1360 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001361
Eli Bendersky17233942013-01-15 22:59:42 +00001362 // First query the target-specific parser. It will return 'true' if it
1363 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001364 if (!getTargetParser().ParseDirective(ID))
1365 return false;
1366
Alp Tokercb402912014-01-24 17:20:08 +00001367 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001368 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001369 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1370 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001371 if (Handler.first)
1372 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1373
1374 // Finally, if no one else is interested in this directive, it must be
1375 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001376 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001377 default:
1378 break;
1379 case DK_SET:
1380 case DK_EQU:
1381 return parseDirectiveSet(IDVal, true);
1382 case DK_EQUIV:
1383 return parseDirectiveSet(IDVal, false);
1384 case DK_ASCII:
1385 return parseDirectiveAscii(IDVal, false);
1386 case DK_ASCIZ:
1387 case DK_STRING:
1388 return parseDirectiveAscii(IDVal, true);
1389 case DK_BYTE:
1390 return parseDirectiveValue(1);
1391 case DK_SHORT:
1392 case DK_VALUE:
1393 case DK_2BYTE:
1394 return parseDirectiveValue(2);
1395 case DK_LONG:
1396 case DK_INT:
1397 case DK_4BYTE:
1398 return parseDirectiveValue(4);
1399 case DK_QUAD:
1400 case DK_8BYTE:
1401 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001402 case DK_OCTA:
1403 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001404 case DK_SINGLE:
1405 case DK_FLOAT:
1406 return parseDirectiveRealValue(APFloat::IEEEsingle);
1407 case DK_DOUBLE:
1408 return parseDirectiveRealValue(APFloat::IEEEdouble);
1409 case DK_ALIGN: {
1410 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1411 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1412 }
1413 case DK_ALIGN32: {
1414 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1415 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1416 }
1417 case DK_BALIGN:
1418 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1419 case DK_BALIGNW:
1420 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1421 case DK_BALIGNL:
1422 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1423 case DK_P2ALIGN:
1424 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1425 case DK_P2ALIGNW:
1426 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1427 case DK_P2ALIGNL:
1428 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1429 case DK_ORG:
1430 return parseDirectiveOrg();
1431 case DK_FILL:
1432 return parseDirectiveFill();
1433 case DK_ZERO:
1434 return parseDirectiveZero();
1435 case DK_EXTERN:
1436 eatToEndOfStatement(); // .extern is the default, ignore it.
1437 return false;
1438 case DK_GLOBL:
1439 case DK_GLOBAL:
1440 return parseDirectiveSymbolAttribute(MCSA_Global);
1441 case DK_LAZY_REFERENCE:
1442 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1443 case DK_NO_DEAD_STRIP:
1444 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1445 case DK_SYMBOL_RESOLVER:
1446 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1447 case DK_PRIVATE_EXTERN:
1448 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1449 case DK_REFERENCE:
1450 return parseDirectiveSymbolAttribute(MCSA_Reference);
1451 case DK_WEAK_DEFINITION:
1452 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1453 case DK_WEAK_REFERENCE:
1454 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1455 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1456 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1457 case DK_COMM:
1458 case DK_COMMON:
1459 return parseDirectiveComm(/*IsLocal=*/false);
1460 case DK_LCOMM:
1461 return parseDirectiveComm(/*IsLocal=*/true);
1462 case DK_ABORT:
1463 return parseDirectiveAbort();
1464 case DK_INCLUDE:
1465 return parseDirectiveInclude();
1466 case DK_INCBIN:
1467 return parseDirectiveIncbin();
1468 case DK_CODE16:
1469 case DK_CODE16GCC:
1470 return TokError(Twine(IDVal) + " not supported yet");
1471 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001472 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001473 case DK_IRP:
1474 return parseDirectiveIrp(IDLoc);
1475 case DK_IRPC:
1476 return parseDirectiveIrpc(IDLoc);
1477 case DK_ENDR:
1478 return parseDirectiveEndr(IDLoc);
1479 case DK_BUNDLE_ALIGN_MODE:
1480 return parseDirectiveBundleAlignMode();
1481 case DK_BUNDLE_LOCK:
1482 return parseDirectiveBundleLock();
1483 case DK_BUNDLE_UNLOCK:
1484 return parseDirectiveBundleUnlock();
1485 case DK_SLEB128:
1486 return parseDirectiveLEB128(true);
1487 case DK_ULEB128:
1488 return parseDirectiveLEB128(false);
1489 case DK_SPACE:
1490 case DK_SKIP:
1491 return parseDirectiveSpace(IDVal);
1492 case DK_FILE:
1493 return parseDirectiveFile(IDLoc);
1494 case DK_LINE:
1495 return parseDirectiveLine();
1496 case DK_LOC:
1497 return parseDirectiveLoc();
1498 case DK_STABS:
1499 return parseDirectiveStabs();
1500 case DK_CFI_SECTIONS:
1501 return parseDirectiveCFISections();
1502 case DK_CFI_STARTPROC:
1503 return parseDirectiveCFIStartProc();
1504 case DK_CFI_ENDPROC:
1505 return parseDirectiveCFIEndProc();
1506 case DK_CFI_DEF_CFA:
1507 return parseDirectiveCFIDefCfa(IDLoc);
1508 case DK_CFI_DEF_CFA_OFFSET:
1509 return parseDirectiveCFIDefCfaOffset();
1510 case DK_CFI_ADJUST_CFA_OFFSET:
1511 return parseDirectiveCFIAdjustCfaOffset();
1512 case DK_CFI_DEF_CFA_REGISTER:
1513 return parseDirectiveCFIDefCfaRegister(IDLoc);
1514 case DK_CFI_OFFSET:
1515 return parseDirectiveCFIOffset(IDLoc);
1516 case DK_CFI_REL_OFFSET:
1517 return parseDirectiveCFIRelOffset(IDLoc);
1518 case DK_CFI_PERSONALITY:
1519 return parseDirectiveCFIPersonalityOrLsda(true);
1520 case DK_CFI_LSDA:
1521 return parseDirectiveCFIPersonalityOrLsda(false);
1522 case DK_CFI_REMEMBER_STATE:
1523 return parseDirectiveCFIRememberState();
1524 case DK_CFI_RESTORE_STATE:
1525 return parseDirectiveCFIRestoreState();
1526 case DK_CFI_SAME_VALUE:
1527 return parseDirectiveCFISameValue(IDLoc);
1528 case DK_CFI_RESTORE:
1529 return parseDirectiveCFIRestore(IDLoc);
1530 case DK_CFI_ESCAPE:
1531 return parseDirectiveCFIEscape();
1532 case DK_CFI_SIGNAL_FRAME:
1533 return parseDirectiveCFISignalFrame();
1534 case DK_CFI_UNDEFINED:
1535 return parseDirectiveCFIUndefined(IDLoc);
1536 case DK_CFI_REGISTER:
1537 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001538 case DK_CFI_WINDOW_SAVE:
1539 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001540 case DK_MACROS_ON:
1541 case DK_MACROS_OFF:
1542 return parseDirectiveMacrosOnOff(IDVal);
1543 case DK_MACRO:
1544 return parseDirectiveMacro(IDLoc);
1545 case DK_ENDM:
1546 case DK_ENDMACRO:
1547 return parseDirectiveEndMacro(IDVal);
1548 case DK_PURGEM:
1549 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001550 case DK_END:
1551 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001552 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001553 return parseDirectiveError(IDLoc, false);
1554 case DK_ERROR:
1555 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001556 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001557
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001558 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001559 }
Chris Lattner36e02122009-06-21 20:54:55 +00001560
Chad Rosierc7f552c2013-02-12 21:33:51 +00001561 // __asm _emit or __asm __emit
1562 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1563 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001564 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001565
1566 // __asm align
1567 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001568 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001569
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001570 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001571
Chris Lattner7cbfa442010-05-19 23:34:33 +00001572 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001573 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001574 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001575 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001576 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001577 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001578
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001579 // Dump the parsed representation, if requested.
1580 if (getShowParsedOperands()) {
1581 SmallString<256> Str;
1582 raw_svector_ostream OS(Str);
1583 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001584 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001585 if (i != 0)
1586 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001587 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001588 }
1589 OS << "]";
1590
Jim Grosbach4b905842013-09-20 23:08:21 +00001591 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001592 }
1593
Oliver Stannard8b273082014-06-19 15:52:37 +00001594 // If we are generating dwarf for the current section then generate a .loc
1595 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001596 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001597 getContext().getGenDwarfSectionSyms().count(
1598 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001599
Eli Bendersky88024712013-01-16 19:32:36 +00001600 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001601
Eli Bendersky88024712013-01-16 19:32:36 +00001602 // If we previously parsed a cpp hash file line comment then make sure the
1603 // current Dwarf File is for the CppHashFilename if not then emit the
1604 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001605 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001606 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1607 0, StringRef(), CppHashFilename);
1608 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001609
Jim Grosbach4b905842013-09-20 23:08:21 +00001610 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1611 // cache with the different Loc from the call above we save the last
1612 // info we queried here with SrcMgr.FindLineNumber().
1613 unsigned CppHashLocLineNo;
1614 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1615 CppHashLocLineNo = LastQueryLine;
1616 else {
1617 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1618 LastQueryLine = CppHashLocLineNo;
1619 LastQueryIDLoc = CppHashLoc;
1620 LastQueryBuffer = CppHashBuf;
1621 }
1622 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001623 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001624
Jim Grosbach4b905842013-09-20 23:08:21 +00001625 getStreamer().EmitDwarfLocDirective(
1626 getContext().getGenDwarfFileNumber(), Line, 0,
1627 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1628 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001629 }
1630
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001631 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001632 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001633 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001634 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1635 Info.ParsedOperands, Out,
1636 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001637 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001638
Chris Lattnera2a9d162010-09-11 16:18:25 +00001639 // Don't skip the rest of the line, the instruction parser is responsible for
1640 // that.
1641 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001642}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001643
Jim Grosbach4b905842013-09-20 23:08:21 +00001644/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001645/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001646void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001647 if (!Lexer.is(AsmToken::EndOfStatement))
1648 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001649 // Eat EOL.
1650 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001651}
1652
Jim Grosbach4b905842013-09-20 23:08:21 +00001653/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001654/// ::= # number "filename"
1655/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001656bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001657 Lex(); // Eat the hash token.
1658
1659 if (getLexer().isNot(AsmToken::Integer)) {
1660 // Consume the line since in cases it is not a well-formed line directive,
1661 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001663 return false;
1664 }
1665
1666 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001667 Lex();
1668
1669 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001671 return false;
1672 }
1673
1674 StringRef Filename = getTok().getString();
1675 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001677
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001678 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1679 CppHashLoc = L;
1680 CppHashFilename = Filename;
1681 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001682 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001683
1684 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001685 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001686 return false;
1687}
1688
Jim Grosbach4b905842013-09-20 23:08:21 +00001689/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690/// for the Filename and LineNo if any in the diagnostic.
1691void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001692 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 raw_ostream &OS = errs();
1694
1695 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1696 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001697 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1698 unsigned CppHashBuf =
1699 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001703 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1704 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1705 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001706 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1707 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708 }
1709
Eric Christophera7c32732012-12-18 00:30:54 +00001710 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711 // manager changed or buffer changed (like in a nested include) then just
1712 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001713 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001715 if (Parser->SavedDiagHandler)
1716 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1717 else
Craig Topper353eda42014-04-24 06:44:33 +00001718 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001719 return;
1720 }
1721
Eric Christophera7c32732012-12-18 00:30:54 +00001722 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1724 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001725 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001726
1727 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1728 int CppHashLocLineNo =
1729 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001730 int LineNo =
1731 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732
Jim Grosbach4b905842013-09-20 23:08:21 +00001733 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1734 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001735 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001736
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001737 if (Parser->SavedDiagHandler)
1738 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1739 else
Craig Topper353eda42014-04-24 06:44:33 +00001740 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741}
1742
Rafael Espindola2c064482012-08-21 18:29:30 +00001743// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1744// difference being that that function accepts '@' as part of identifiers and
1745// we can't do that. AsmLexer.cpp should probably be changed to handle
1746// '@' as a special case when needed.
1747static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001748 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1749 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001750}
1751
Rafael Espindola34b9c512012-06-03 23:57:14 +00001752bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001753 ArrayRef<MCAsmMacroParameter> Parameters,
1754 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001755 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001756 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001757 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001758 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001759
Preston Gurd05500642012-09-19 20:36:12 +00001760 // A macro without parameters is handled differently on Darwin:
1761 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001762 while (!Body.empty()) {
1763 // Scan for the next substitution.
1764 std::size_t End = Body.size(), Pos = 0;
1765 for (; Pos != End; ++Pos) {
1766 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001767 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001768 // This macro has no parameters, look for $0, $1, etc.
1769 if (Body[Pos] != '$' || Pos + 1 == End)
1770 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001771
Rafael Espindola1134ab232011-06-05 02:43:45 +00001772 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001773 if (Next == '$' || Next == 'n' ||
1774 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001775 break;
1776 } else {
1777 // This macro has parameters, look for \foo, \bar, etc.
1778 if (Body[Pos] == '\\' && Pos + 1 != End)
1779 break;
1780 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001781 }
1782
1783 // Add the prefix.
1784 OS << Body.slice(0, Pos);
1785
1786 // Check if we reached the end.
1787 if (Pos == End)
1788 break;
1789
Benjamin Kramer513e7442014-02-20 13:36:32 +00001790 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001791 switch (Body[Pos + 1]) {
1792 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001793 case '$':
1794 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001795 break;
1796
Jim Grosbach4b905842013-09-20 23:08:21 +00001797 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001798 case 'n':
1799 OS << A.size();
1800 break;
1801
Jim Grosbach4b905842013-09-20 23:08:21 +00001802 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001803 default: {
1804 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001805 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001806 if (Index >= A.size())
1807 break;
1808
1809 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001810 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 ie = A[Index].end();
1812 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001813 OS << it->getString();
1814 break;
1815 }
1816 }
1817 Pos += 2;
1818 } else {
1819 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001820 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821 ++I;
1822
Jim Grosbach4b905842013-09-20 23:08:21 +00001823 const char *Begin = Body.data() + Pos + 1;
1824 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001825 unsigned Index = 0;
1826 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001827 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828 break;
1829
Preston Gurd05500642012-09-19 20:36:12 +00001830 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001831 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1832 Pos += 3;
1833 else {
1834 OS << '\\' << Argument;
1835 Pos = I;
1836 }
Preston Gurd05500642012-09-19 20:36:12 +00001837 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001838 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001839 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001840 ie = A[Index].end();
1841 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001842 // We expect no quotes around the string's contents when
1843 // parsing for varargs.
1844 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001845 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001846 else
1847 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001848
Preston Gurd05500642012-09-19 20:36:12 +00001849 Pos += 1 + Argument.size();
1850 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001851 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001852 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001853 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001854 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001855
Rafael Espindola1134ab232011-06-05 02:43:45 +00001856 return false;
1857}
Daniel Dunbar43235712010-07-18 18:54:11 +00001858
Jim Grosbach4b905842013-09-20 23:08:21 +00001859MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1860 SMLoc EL, MemoryBuffer *I)
1861 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1862 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001863
Jim Grosbach4b905842013-09-20 23:08:21 +00001864static bool isOperator(AsmToken::TokenKind kind) {
1865 switch (kind) {
1866 default:
1867 return false;
1868 case AsmToken::Plus:
1869 case AsmToken::Minus:
1870 case AsmToken::Tilde:
1871 case AsmToken::Slash:
1872 case AsmToken::Star:
1873 case AsmToken::Dot:
1874 case AsmToken::Equal:
1875 case AsmToken::EqualEqual:
1876 case AsmToken::Pipe:
1877 case AsmToken::PipePipe:
1878 case AsmToken::Caret:
1879 case AsmToken::Amp:
1880 case AsmToken::AmpAmp:
1881 case AsmToken::Exclaim:
1882 case AsmToken::ExclaimEqual:
1883 case AsmToken::Percent:
1884 case AsmToken::Less:
1885 case AsmToken::LessEqual:
1886 case AsmToken::LessLess:
1887 case AsmToken::LessGreater:
1888 case AsmToken::Greater:
1889 case AsmToken::GreaterEqual:
1890 case AsmToken::GreaterGreater:
1891 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001892 }
1893}
1894
David Majnemer16252452014-01-29 00:07:39 +00001895namespace {
1896class AsmLexerSkipSpaceRAII {
1897public:
1898 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1899 Lexer.setSkipSpace(SkipSpace);
1900 }
1901
1902 ~AsmLexerSkipSpaceRAII() {
1903 Lexer.setSkipSpace(true);
1904 }
1905
1906private:
1907 AsmLexer &Lexer;
1908};
1909}
1910
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001911bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1912
1913 if (Vararg) {
1914 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1915 StringRef Str = parseStringToEndOfStatement();
1916 MA.push_back(AsmToken(AsmToken::String, Str));
1917 }
1918 return false;
1919 }
1920
Rafael Espindola768b41c2012-06-15 14:02:34 +00001921 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001922 unsigned AddTokens = 0;
1923
David Majnemer16252452014-01-29 00:07:39 +00001924 // Darwin doesn't use spaces to delmit arguments.
1925 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001926
1927 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001928 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001929 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001930
David Majnemer91fc4c22014-01-29 18:57:46 +00001931 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001932 break;
Preston Gurd05500642012-09-19 20:36:12 +00001933
1934 if (Lexer.is(AsmToken::Space)) {
1935 Lex(); // Eat spaces
1936
1937 // Spaces can delimit parameters, but could also be part an expression.
1938 // If the token after a space is an operator, add the token and the next
1939 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001940 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001941 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001942 // Check to see whether the token is used as an operator,
1943 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001944 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001945 if (*NextChar == ' ')
1946 AddTokens = 2;
1947 }
1948
1949 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001950 break;
1951 }
1952 }
1953 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001954
Jim Grosbach4b905842013-09-20 23:08:21 +00001955 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001956 // to be able to fill in the remaining default parameter values
1957 if (Lexer.is(AsmToken::EndOfStatement))
1958 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959
1960 // Adjust the current parentheses level.
1961 if (Lexer.is(AsmToken::LParen))
1962 ++ParenLevel;
1963 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1964 --ParenLevel;
1965
1966 // Append the token to the current argument list.
1967 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001968 if (AddTokens)
1969 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001970 Lex();
1971 }
Preston Gurd05500642012-09-19 20:36:12 +00001972
Rafael Espindola768b41c2012-06-15 14:02:34 +00001973 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001974 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001975 return false;
1976}
1977
1978// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001979bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001980 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001981 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001982 bool NamedParametersFound = false;
1983 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001985 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001986 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001987
Rafael Espindola768b41c2012-06-15 14:02:34 +00001988 // Parse two kinds of macro invocations:
1989 // - macros defined without any parameters accept an arbitrary number of them
1990 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001991 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001992 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1993 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001994 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001995 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001996
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001997 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001998 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001999 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002000 eatToEndOfStatement();
2001 return true;
2002 }
2003
2004 if (!Lexer.is(AsmToken::Equal)) {
2005 TokError("expected '=' after formal parameter identifier");
2006 eatToEndOfStatement();
2007 return true;
2008 }
2009 Lex();
2010
2011 NamedParametersFound = true;
2012 }
2013
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002014 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002015 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002016 eatToEndOfStatement();
2017 return true;
2018 }
2019
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002020 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2021 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002022 return true;
2023
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002024 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002025 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002026 unsigned FAI = 0;
2027 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002028 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002029 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002030
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002032 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002033 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002034 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002035 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036 return true;
2037 }
2038 PI = FAI;
2039 }
2040
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002041 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002042 if (A.size() <= PI)
2043 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002044 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002045
2046 if (FALocs.size() <= PI)
2047 FALocs.resize(PI + 1);
2048
2049 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002050 }
Jim Grosbach206661622012-07-30 22:44:17 +00002051
Preston Gurd242ed3152012-09-19 20:29:04 +00002052 // At the end of the statement, fill in remaining arguments that have
2053 // default values. If there aren't any, then the next argument is
2054 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002055 if (Lexer.is(AsmToken::EndOfStatement)) {
2056 bool Failure = false;
2057 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2058 if (A[FAI].empty()) {
2059 if (M->Parameters[FAI].Required) {
2060 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2061 "missing value for required parameter "
2062 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2063 Failure = true;
2064 }
2065
2066 if (!M->Parameters[FAI].Value.empty())
2067 A[FAI] = M->Parameters[FAI].Value;
2068 }
2069 }
2070 return Failure;
2071 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002072
2073 if (Lexer.is(AsmToken::Comma))
2074 Lex();
2075 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002076
2077 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002078}
2079
Jim Grosbach4b905842013-09-20 23:08:21 +00002080const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2081 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002082 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002083}
2084
Jim Grosbach4b905842013-09-20 23:08:21 +00002085void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002086 MacroMap[Name] = new MCAsmMacro(Macro);
2087}
2088
Jim Grosbach4b905842013-09-20 23:08:21 +00002089void AsmParser::undefineMacro(StringRef Name) {
2090 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002091 if (I != MacroMap.end()) {
2092 delete I->getValue();
2093 MacroMap.erase(I);
2094 }
2095}
2096
Jim Grosbach4b905842013-09-20 23:08:21 +00002097bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002098 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2099 // this, although we should protect against infinite loops.
2100 if (ActiveMacros.size() == 20)
2101 return TokError("macros cannot be nested more than 20 levels deep");
2102
Eli Bendersky38274122013-01-14 23:22:36 +00002103 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002104 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002105 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002106
Rafael Espindola1134ab232011-06-05 02:43:45 +00002107 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2108 // to hold the macro body with substitutions.
2109 SmallString<256> Buf;
2110 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002111 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002112
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002113 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002114 return true;
2115
Eli Bendersky38274122013-01-14 23:22:36 +00002116 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002117 // instantiation.
2118 OS << ".endmacro\n";
2119
Rafael Espindola1134ab232011-06-05 02:43:45 +00002120 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002121 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002122
Daniel Dunbar43235712010-07-18 18:54:11 +00002123 // Create the macro instantiation object and add to the current macro
2124 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002125 MacroInstantiation *MI = new MacroInstantiation(
2126 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002127 ActiveMacros.push_back(MI);
2128
2129 // Jump to the macro instantiation and prime the lexer.
2130 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002131 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002132 Lex();
2133
2134 return false;
2135}
2136
Jim Grosbach4b905842013-09-20 23:08:21 +00002137void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002138 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002139 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002140 Lex();
2141
2142 // Pop the instantiation entry.
2143 delete ActiveMacros.back();
2144 ActiveMacros.pop_back();
2145}
2146
Jim Grosbach4b905842013-09-20 23:08:21 +00002147static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002148 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002149 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002150 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2151 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002152 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002153 case MCExpr::Target:
2154 case MCExpr::Constant:
2155 return false;
2156 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002157 const MCSymbol &S =
2158 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002159 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002161 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002162 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002163 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002164 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002165 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002166
2167 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002168}
2169
Jim Grosbach4b905842013-09-20 23:08:21 +00002170bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002171 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002172 // FIXME: Use better location, we should use proper tokens.
2173 SMLoc EqualLoc = Lexer.getLoc();
2174
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002175 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002176 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002177 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002178
Rafael Espindola72f5f172012-01-28 05:57:00 +00002179 // Note: we don't count b as used in "a = b". This is to allow
2180 // a = b
2181 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002182
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002183 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002184 return TokError("unexpected token in assignment");
2185
2186 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002187 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002188
Daniel Dunbar5f339242009-10-16 01:57:39 +00002189 // Validate that the LHS is allowed to be a variable (either it has not been
2190 // used as a symbol, or it is an absolute symbol).
2191 MCSymbol *Sym = getContext().LookupSymbol(Name);
2192 if (Sym) {
2193 // Diagnose assignment to a label.
2194 //
2195 // FIXME: Diagnostics. Note the location of the definition as a label.
2196 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002197 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002198 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2199 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002200 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002201 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2202 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002203 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002204 return Error(EqualLoc, "redefinition of '" + Name + "'");
2205 else if (!Sym->isVariable())
2206 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002207 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002208 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002209 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002210
2211 // Don't count these checks as uses.
2212 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002213 } else if (Name == ".") {
2214 if (Out.EmitValueToOffset(Value, 0)) {
2215 Error(EqualLoc, "expected absolute expression");
2216 eatToEndOfStatement();
2217 }
2218 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002219 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002220 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002221
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002222 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002223 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002224 if (NoDeadStrip)
2225 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2226
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002227 return false;
2228}
2229
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002230/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002231/// ::= identifier
2232/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002233bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002234 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002235 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2236 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002237 // handle this as a context dependent token, instead we detect adjacent tokens
2238 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002239 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2240 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002241
Hans Wennborgce69d772013-10-18 20:46:28 +00002242 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002243 Lex();
2244 if (Lexer.isNot(AsmToken::Identifier))
2245 return true;
2246
Hans Wennborgce69d772013-10-18 20:46:28 +00002247 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2248 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002249 return true;
2250
2251 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002252 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002253 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002254 Lex();
2255 return false;
2256 }
2257
Jim Grosbach4b905842013-09-20 23:08:21 +00002258 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002259 return true;
2260
Sean Callanan936b0d32010-01-19 21:44:56 +00002261 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002262
Sean Callanan686ed8d2010-01-19 20:22:31 +00002263 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002264
2265 return false;
2266}
2267
Jim Grosbach4b905842013-09-20 23:08:21 +00002268/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002269/// ::= .equ identifier ',' expression
2270/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002271/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002272bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002273 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002274
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002275 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002276 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002277
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002278 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002279 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002280 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002281
Jim Grosbach4b905842013-09-20 23:08:21 +00002282 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002283}
2284
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002285bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002286 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002287
2288 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002289 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002290 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2291 if (Str[i] != '\\') {
2292 Data += Str[i];
2293 continue;
2294 }
2295
2296 // Recognize escaped characters. Note that this escape semantics currently
2297 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2298 ++i;
2299 if (i == e)
2300 return TokError("unexpected backslash at end of string");
2301
2302 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002303 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002304 // Consume up to three octal characters.
2305 unsigned Value = Str[i] - '0';
2306
Jim Grosbach4b905842013-09-20 23:08:21 +00002307 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002308 ++i;
2309 Value = Value * 8 + (Str[i] - '0');
2310
Jim Grosbach4b905842013-09-20 23:08:21 +00002311 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002312 ++i;
2313 Value = Value * 8 + (Str[i] - '0');
2314 }
2315 }
2316
2317 if (Value > 255)
2318 return TokError("invalid octal escape sequence (out of range)");
2319
Jim Grosbach4b905842013-09-20 23:08:21 +00002320 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002321 continue;
2322 }
2323
2324 // Otherwise recognize individual escapes.
2325 switch (Str[i]) {
2326 default:
2327 // Just reject invalid escape sequences for now.
2328 return TokError("invalid escape sequence (unrecognized character)");
2329
2330 case 'b': Data += '\b'; break;
2331 case 'f': Data += '\f'; break;
2332 case 'n': Data += '\n'; break;
2333 case 'r': Data += '\r'; break;
2334 case 't': Data += '\t'; break;
2335 case '"': Data += '"'; break;
2336 case '\\': Data += '\\'; break;
2337 }
2338 }
2339
2340 return false;
2341}
2342
Jim Grosbach4b905842013-09-20 23:08:21 +00002343/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002344/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002345bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002347 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002348
Daniel Dunbara10e5192009-06-24 23:30:00 +00002349 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002350 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002351 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002352
Daniel Dunbaref668c12009-08-14 18:19:52 +00002353 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002354 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002355 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002356
Rafael Espindola64e1af82013-07-02 15:49:13 +00002357 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002358 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002359 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002360
Sean Callanan686ed8d2010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002362
2363 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002364 break;
2365
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002366 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002367 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002368 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002369 }
2370 }
2371
Sean Callanan686ed8d2010-01-19 20:22:31 +00002372 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002373 return false;
2374}
2375
Jim Grosbach4b905842013-09-20 23:08:21 +00002376/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002377/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002378bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002379 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002380 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002381
Daniel Dunbara10e5192009-06-24 23:30:00 +00002382 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002383 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002384 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002385 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002386 return true;
2387
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002388 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002389 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2390 assert(Size <= 8 && "Invalid size");
2391 uint64_t IntValue = MCE->getValue();
2392 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2393 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002394 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002395 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002396 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002397
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002398 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002399 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002400
Daniel Dunbara10e5192009-06-24 23:30:00 +00002401 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002402 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002403 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002404 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002405 }
2406 }
2407
Sean Callanan686ed8d2010-01-19 20:22:31 +00002408 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002409 return false;
2410}
2411
David Woodhoused6de0d92014-02-01 16:20:59 +00002412/// ParseDirectiveOctaValue
2413/// ::= .octa [ hexconstant (, hexconstant)* ]
2414bool AsmParser::parseDirectiveOctaValue() {
2415 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2416 checkForValidSection();
2417
2418 for (;;) {
2419 if (Lexer.getKind() == AsmToken::Error)
2420 return true;
2421 if (Lexer.getKind() != AsmToken::Integer &&
2422 Lexer.getKind() != AsmToken::BigNum)
2423 return TokError("unknown token in expression");
2424
2425 SMLoc ExprLoc = getLexer().getLoc();
2426 APInt IntValue = getTok().getAPIntVal();
2427 Lex();
2428
2429 uint64_t hi, lo;
2430 if (IntValue.isIntN(64)) {
2431 hi = 0;
2432 lo = IntValue.getZExtValue();
2433 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002434 // It might actually have more than 128 bits, but the top ones are zero.
2435 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002436 lo = IntValue.getLoBits(64).getZExtValue();
2437 } else
2438 return Error(ExprLoc, "literal value out of range for directive");
2439
2440 if (MAI.isLittleEndian()) {
2441 getStreamer().EmitIntValue(lo, 8);
2442 getStreamer().EmitIntValue(hi, 8);
2443 } else {
2444 getStreamer().EmitIntValue(hi, 8);
2445 getStreamer().EmitIntValue(lo, 8);
2446 }
2447
2448 if (getLexer().is(AsmToken::EndOfStatement))
2449 break;
2450
2451 // FIXME: Improve diagnostic.
2452 if (getLexer().isNot(AsmToken::Comma))
2453 return TokError("unexpected token in directive");
2454 Lex();
2455 }
2456 }
2457
2458 Lex();
2459 return false;
2460}
2461
Jim Grosbach4b905842013-09-20 23:08:21 +00002462/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002463/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002464bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002465 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002466 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002467
2468 for (;;) {
2469 // We don't truly support arithmetic on floating point expressions, so we
2470 // have to manually parse unary prefixes.
2471 bool IsNeg = false;
2472 if (getLexer().is(AsmToken::Minus)) {
2473 Lex();
2474 IsNeg = true;
2475 } else if (getLexer().is(AsmToken::Plus))
2476 Lex();
2477
Michael J. Spencer530ce852010-10-09 11:00:50 +00002478 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002479 getLexer().isNot(AsmToken::Real) &&
2480 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002481 return TokError("unexpected token in directive");
2482
2483 // Convert to an APFloat.
2484 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002485 StringRef IDVal = getTok().getString();
2486 if (getLexer().is(AsmToken::Identifier)) {
2487 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2488 Value = APFloat::getInf(Semantics);
2489 else if (!IDVal.compare_lower("nan"))
2490 Value = APFloat::getNaN(Semantics, false, ~0);
2491 else
2492 return TokError("invalid floating point literal");
2493 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002494 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002495 return TokError("invalid floating point literal");
2496 if (IsNeg)
2497 Value.changeSign();
2498
2499 // Consume the numeric token.
2500 Lex();
2501
2502 // Emit the value as an integer.
2503 APInt AsInt = Value.bitcastToAPInt();
2504 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002505 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002506
2507 if (getLexer().is(AsmToken::EndOfStatement))
2508 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002509
Daniel Dunbar2af16532010-09-24 01:59:56 +00002510 if (getLexer().isNot(AsmToken::Comma))
2511 return TokError("unexpected token in directive");
2512 Lex();
2513 }
2514 }
2515
2516 Lex();
2517 return false;
2518}
2519
Jim Grosbach4b905842013-09-20 23:08:21 +00002520/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002521/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002522bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002523 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002524
2525 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002526 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002527 return true;
2528
Rafael Espindolab91bac62010-10-05 19:42:57 +00002529 int64_t Val = 0;
2530 if (getLexer().is(AsmToken::Comma)) {
2531 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002532 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002533 return true;
2534 }
2535
Rafael Espindola922e3f42010-09-16 15:03:59 +00002536 if (getLexer().isNot(AsmToken::EndOfStatement))
2537 return TokError("unexpected token in '.zero' directive");
2538
2539 Lex();
2540
Rafael Espindola64e1af82013-07-02 15:49:13 +00002541 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002542
2543 return false;
2544}
2545
Jim Grosbach4b905842013-09-20 23:08:21 +00002546/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002547/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002548bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002549 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002550
David Majnemer522d3db2014-02-01 07:19:38 +00002551 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002552 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002553 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002554 return true;
2555
David Majnemer522d3db2014-02-01 07:19:38 +00002556 if (NumValues < 0) {
2557 Warning(RepeatLoc,
2558 "'.fill' directive with negative repeat count has no effect");
2559 NumValues = 0;
2560 }
2561
Roman Divackye33098f2013-09-24 17:44:41 +00002562 int64_t FillSize = 1;
2563 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002564
David Majnemer522d3db2014-02-01 07:19:38 +00002565 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2567 if (getLexer().isNot(AsmToken::Comma))
2568 return TokError("unexpected token in '.fill' directive");
2569 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002570
David Majnemer522d3db2014-02-01 07:19:38 +00002571 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002572 if (parseAbsoluteExpression(FillSize))
2573 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002574
Roman Divackye33098f2013-09-24 17:44:41 +00002575 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2576 if (getLexer().isNot(AsmToken::Comma))
2577 return TokError("unexpected token in '.fill' directive");
2578 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002579
David Majnemer522d3db2014-02-01 07:19:38 +00002580 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002581 if (parseAbsoluteExpression(FillExpr))
2582 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Roman Divackye33098f2013-09-24 17:44:41 +00002584 if (getLexer().isNot(AsmToken::EndOfStatement))
2585 return TokError("unexpected token in '.fill' directive");
2586
2587 Lex();
2588 }
2589 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002590
David Majnemer522d3db2014-02-01 07:19:38 +00002591 if (FillSize < 0) {
2592 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2593 NumValues = 0;
2594 }
2595 if (FillSize > 8) {
2596 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2597 FillSize = 8;
2598 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002599
David Majnemer522d3db2014-02-01 07:19:38 +00002600 if (!isUInt<32>(FillExpr) && FillSize > 4)
2601 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2602
2603 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2604 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2605
2606 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2607 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2608 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2609 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002610
2611 return false;
2612}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002613
Jim Grosbach4b905842013-09-20 23:08:21 +00002614/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002615/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002616bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002617 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002618
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002619 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002620 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002621 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002622 return true;
2623
2624 // Parse optional fill expression.
2625 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2627 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002628 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002629 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002630
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002631 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002632 return true;
2633
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002634 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002635 return TokError("unexpected token in '.org' directive");
2636 }
2637
Sean Callanan686ed8d2010-01-19 20:22:31 +00002638 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002639
Jim Grosbachb5912772012-01-27 00:37:08 +00002640 // Only limited forms of relocatable expressions are accepted here, it
2641 // has to be relative to the current section. The streamer will return
2642 // 'true' if the expression wasn't evaluatable.
2643 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2644 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002645
2646 return false;
2647}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002648
Jim Grosbach4b905842013-09-20 23:08:21 +00002649/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002650/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002651bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002652 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002653
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002654 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002656 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002657 return true;
2658
2659 SMLoc MaxBytesLoc;
2660 bool HasFillExpr = false;
2661 int64_t FillExpr = 0;
2662 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002663 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2664 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002665 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002666 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667
2668 // The fill expression can be omitted while specifying a maximum number of
2669 // alignment bytes, e.g:
2670 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002671 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002673 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002674 return true;
2675 }
2676
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002677 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2678 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002680 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002682 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002683 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002685
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002686 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002687 return TokError("unexpected token in directive");
2688 }
2689 }
2690
Sean Callanan686ed8d2010-01-19 20:22:31 +00002691 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002692
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002693 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002694 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695
2696 // Compute alignment in bytes.
2697 if (IsPow2) {
2698 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002699 if (Alignment >= 32) {
2700 Error(AlignmentLoc, "invalid alignment value");
2701 Alignment = 31;
2702 }
2703
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002704 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002705 } else {
2706 // Reject alignments that aren't a power of two, for gas compatibility.
2707 if (!isPowerOf2_64(Alignment))
2708 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002709 }
2710
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002711 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002712 if (MaxBytesLoc.isValid()) {
2713 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002714 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002715 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002716 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 }
2718
2719 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002720 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002721 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002722 MaxBytesToFill = 0;
2723 }
2724 }
2725
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002726 // Check whether we should use optimal code alignment for this .align
2727 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002728 const MCSection *Section = getStreamer().getCurrentSection().first;
2729 assert(Section && "must have section to emit alignment");
2730 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002731 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2732 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002733 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002734 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002735 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002736 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2737 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002738 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002739
2740 return false;
2741}
2742
Jim Grosbach4b905842013-09-20 23:08:21 +00002743/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002744/// ::= .file [number] filename
2745/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002746bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002747 // FIXME: I'm not sure what this is.
2748 int64_t FileNumber = -1;
2749 SMLoc FileNumberLoc = getLexer().getLoc();
2750 if (getLexer().is(AsmToken::Integer)) {
2751 FileNumber = getTok().getIntVal();
2752 Lex();
2753
2754 if (FileNumber < 1)
2755 return TokError("file number less than one");
2756 }
2757
2758 if (getLexer().isNot(AsmToken::String))
2759 return TokError("unexpected token in '.file' directive");
2760
2761 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002762 // Allow the strings to have escaped octal character sequence.
2763 std::string Path = getTok().getString();
2764 if (parseEscapedString(Path))
2765 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002766 Lex();
2767
2768 StringRef Directory;
2769 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002770 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002771 if (getLexer().is(AsmToken::String)) {
2772 if (FileNumber == -1)
2773 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002774 if (parseEscapedString(FilenameData))
2775 return true;
2776 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002777 Directory = Path;
2778 Lex();
2779 } else {
2780 Filename = Path;
2781 }
2782
2783 if (getLexer().isNot(AsmToken::EndOfStatement))
2784 return TokError("unexpected token in '.file' directive");
2785
2786 if (FileNumber == -1)
2787 getStreamer().EmitFileDirective(Filename);
2788 else {
2789 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002790 Error(DirectiveLoc,
2791 "input can't have .file dwarf directives when -g is "
2792 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002793
David Blaikiec714ef42014-03-17 01:52:11 +00002794 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2795 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002796 Error(FileNumberLoc, "file number already allocated");
2797 }
2798
2799 return false;
2800}
2801
Jim Grosbach4b905842013-09-20 23:08:21 +00002802/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002803/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002804bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002805 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2806 if (getLexer().isNot(AsmToken::Integer))
2807 return TokError("unexpected token in '.line' directive");
2808
2809 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002810 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002811 Lex();
2812
2813 // FIXME: Do something with the .line.
2814 }
2815
2816 if (getLexer().isNot(AsmToken::EndOfStatement))
2817 return TokError("unexpected token in '.line' directive");
2818
2819 return false;
2820}
2821
Jim Grosbach4b905842013-09-20 23:08:21 +00002822/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002823/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2824/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2825/// The first number is a file number, must have been previously assigned with
2826/// a .file directive, the second number is the line number and optionally the
2827/// third number is a column position (zero if not specified). The remaining
2828/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002829bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002830 if (getLexer().isNot(AsmToken::Integer))
2831 return TokError("unexpected token in '.loc' directive");
2832 int64_t FileNumber = getTok().getIntVal();
2833 if (FileNumber < 1)
2834 return TokError("file number less than one in '.loc' directive");
2835 if (!getContext().isValidDwarfFileNumber(FileNumber))
2836 return TokError("unassigned file number in '.loc' directive");
2837 Lex();
2838
2839 int64_t LineNumber = 0;
2840 if (getLexer().is(AsmToken::Integer)) {
2841 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002842 if (LineNumber < 0)
2843 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002844 Lex();
2845 }
2846
2847 int64_t ColumnPos = 0;
2848 if (getLexer().is(AsmToken::Integer)) {
2849 ColumnPos = getTok().getIntVal();
2850 if (ColumnPos < 0)
2851 return TokError("column position less than zero in '.loc' directive");
2852 Lex();
2853 }
2854
2855 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2856 unsigned Isa = 0;
2857 int64_t Discriminator = 0;
2858 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2859 for (;;) {
2860 if (getLexer().is(AsmToken::EndOfStatement))
2861 break;
2862
2863 StringRef Name;
2864 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002865 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return TokError("unexpected token in '.loc' directive");
2867
2868 if (Name == "basic_block")
2869 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2870 else if (Name == "prologue_end")
2871 Flags |= DWARF2_FLAG_PROLOGUE_END;
2872 else if (Name == "epilogue_begin")
2873 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2874 else if (Name == "is_stmt") {
2875 Loc = getTok().getLoc();
2876 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002877 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002878 return true;
2879 // The expression must be the constant 0 or 1.
2880 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2881 int Value = MCE->getValue();
2882 if (Value == 0)
2883 Flags &= ~DWARF2_FLAG_IS_STMT;
2884 else if (Value == 1)
2885 Flags |= DWARF2_FLAG_IS_STMT;
2886 else
2887 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002888 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002889 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2890 }
Craig Topperf15655b2013-04-22 04:22:40 +00002891 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002892 Loc = getTok().getLoc();
2893 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002894 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002895 return true;
2896 // The expression must be a constant greater or equal to 0.
2897 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2898 int Value = MCE->getValue();
2899 if (Value < 0)
2900 return Error(Loc, "isa number less than zero");
2901 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002902 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002903 return Error(Loc, "isa number not a constant value");
2904 }
Craig Topperf15655b2013-04-22 04:22:40 +00002905 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002906 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002907 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002908 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002909 return Error(Loc, "unknown sub-directive in '.loc' directive");
2910 }
2911
2912 if (getLexer().is(AsmToken::EndOfStatement))
2913 break;
2914 }
2915 }
2916
2917 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2918 Isa, Discriminator, StringRef());
2919
2920 return false;
2921}
2922
Jim Grosbach4b905842013-09-20 23:08:21 +00002923/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002924/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002925bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002926 return TokError("unsupported directive '.stabs'");
2927}
2928
Jim Grosbach4b905842013-09-20 23:08:21 +00002929/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002930/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002931bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002932 StringRef Name;
2933 bool EH = false;
2934 bool Debug = false;
2935
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002936 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002937 return TokError("Expected an identifier");
2938
2939 if (Name == ".eh_frame")
2940 EH = true;
2941 else if (Name == ".debug_frame")
2942 Debug = true;
2943
2944 if (getLexer().is(AsmToken::Comma)) {
2945 Lex();
2946
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002947 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002948 return TokError("Expected an identifier");
2949
2950 if (Name == ".eh_frame")
2951 EH = true;
2952 else if (Name == ".debug_frame")
2953 Debug = true;
2954 }
2955
2956 getStreamer().EmitCFISections(EH, Debug);
2957 return false;
2958}
2959
Jim Grosbach4b905842013-09-20 23:08:21 +00002960/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002961/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002962bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002963 StringRef Simple;
2964 if (getLexer().isNot(AsmToken::EndOfStatement))
2965 if (parseIdentifier(Simple) || Simple != "simple")
2966 return TokError("unexpected token in .cfi_startproc directive");
2967
2968 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002969 return false;
2970}
2971
Jim Grosbach4b905842013-09-20 23:08:21 +00002972/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002973/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002974bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002975 getStreamer().EmitCFIEndProc();
2976 return false;
2977}
2978
Jim Grosbach4b905842013-09-20 23:08:21 +00002979/// \brief parse register name or number.
2980bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002981 SMLoc DirectiveLoc) {
2982 unsigned RegNo;
2983
2984 if (getLexer().isNot(AsmToken::Integer)) {
2985 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2986 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002987 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002988 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002989 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002990
2991 return false;
2992}
2993
Jim Grosbach4b905842013-09-20 23:08:21 +00002994/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002995/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002996bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002997 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002998 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002999 return true;
3000
3001 if (getLexer().isNot(AsmToken::Comma))
3002 return TokError("unexpected token in directive");
3003 Lex();
3004
3005 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003006 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003007 return true;
3008
3009 getStreamer().EmitCFIDefCfa(Register, Offset);
3010 return false;
3011}
3012
Jim Grosbach4b905842013-09-20 23:08:21 +00003013/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003014/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003015bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003016 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003017 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003018 return true;
3019
3020 getStreamer().EmitCFIDefCfaOffset(Offset);
3021 return false;
3022}
3023
Jim Grosbach4b905842013-09-20 23:08:21 +00003024/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003025/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003026bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003027 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003028 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003029 return true;
3030
3031 if (getLexer().isNot(AsmToken::Comma))
3032 return TokError("unexpected token in directive");
3033 Lex();
3034
3035 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003036 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003037 return true;
3038
3039 getStreamer().EmitCFIRegister(Register1, Register2);
3040 return false;
3041}
3042
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003043/// parseDirectiveCFIWindowSave
3044/// ::= .cfi_window_save
3045bool AsmParser::parseDirectiveCFIWindowSave() {
3046 getStreamer().EmitCFIWindowSave();
3047 return false;
3048}
3049
Jim Grosbach4b905842013-09-20 23:08:21 +00003050/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003051/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003052bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003053 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003054 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return true;
3056
3057 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3058 return false;
3059}
3060
Jim Grosbach4b905842013-09-20 23:08:21 +00003061/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003062/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003063bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003064 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003065 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003066 return true;
3067
3068 getStreamer().EmitCFIDefCfaRegister(Register);
3069 return false;
3070}
3071
Jim Grosbach4b905842013-09-20 23:08:21 +00003072/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003073/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003074bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003075 int64_t Register = 0;
3076 int64_t Offset = 0;
3077
Jim Grosbach4b905842013-09-20 23:08:21 +00003078 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003079 return true;
3080
3081 if (getLexer().isNot(AsmToken::Comma))
3082 return TokError("unexpected token in directive");
3083 Lex();
3084
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003085 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003086 return true;
3087
3088 getStreamer().EmitCFIOffset(Register, Offset);
3089 return false;
3090}
3091
Jim Grosbach4b905842013-09-20 23:08:21 +00003092/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003093/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003094bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003095 int64_t Register = 0;
3096
Jim Grosbach4b905842013-09-20 23:08:21 +00003097 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return true;
3099
3100 if (getLexer().isNot(AsmToken::Comma))
3101 return TokError("unexpected token in directive");
3102 Lex();
3103
3104 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003105 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003106 return true;
3107
3108 getStreamer().EmitCFIRelOffset(Register, Offset);
3109 return false;
3110}
3111
3112static bool isValidEncoding(int64_t Encoding) {
3113 if (Encoding & ~0xff)
3114 return false;
3115
3116 if (Encoding == dwarf::DW_EH_PE_omit)
3117 return true;
3118
3119 const unsigned Format = Encoding & 0xf;
3120 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3121 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3122 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3123 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3124 return false;
3125
3126 const unsigned Application = Encoding & 0x70;
3127 if (Application != dwarf::DW_EH_PE_absptr &&
3128 Application != dwarf::DW_EH_PE_pcrel)
3129 return false;
3130
3131 return true;
3132}
3133
Jim Grosbach4b905842013-09-20 23:08:21 +00003134/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003135/// IsPersonality true for cfi_personality, false for cfi_lsda
3136/// ::= .cfi_personality encoding, [symbol_name]
3137/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003138bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003139 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003140 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003141 return true;
3142 if (Encoding == dwarf::DW_EH_PE_omit)
3143 return false;
3144
3145 if (!isValidEncoding(Encoding))
3146 return TokError("unsupported encoding.");
3147
3148 if (getLexer().isNot(AsmToken::Comma))
3149 return TokError("unexpected token in directive");
3150 Lex();
3151
3152 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003153 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003154 return TokError("expected identifier in directive");
3155
3156 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3157
3158 if (IsPersonality)
3159 getStreamer().EmitCFIPersonality(Sym, Encoding);
3160 else
3161 getStreamer().EmitCFILsda(Sym, Encoding);
3162 return false;
3163}
3164
Jim Grosbach4b905842013-09-20 23:08:21 +00003165/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003166/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003167bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003168 getStreamer().EmitCFIRememberState();
3169 return false;
3170}
3171
Jim Grosbach4b905842013-09-20 23:08:21 +00003172/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003173/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003174bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003175 getStreamer().EmitCFIRestoreState();
3176 return false;
3177}
3178
Jim Grosbach4b905842013-09-20 23:08:21 +00003179/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003180/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003181bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003182 int64_t Register = 0;
3183
Jim Grosbach4b905842013-09-20 23:08:21 +00003184 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003185 return true;
3186
3187 getStreamer().EmitCFISameValue(Register);
3188 return false;
3189}
3190
Jim Grosbach4b905842013-09-20 23:08:21 +00003191/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003192/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003193bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003194 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003195 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003196 return true;
3197
3198 getStreamer().EmitCFIRestore(Register);
3199 return false;
3200}
3201
Jim Grosbach4b905842013-09-20 23:08:21 +00003202/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003203/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003204bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003205 std::string Values;
3206 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003207 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003208 return true;
3209
3210 Values.push_back((uint8_t)CurrValue);
3211
3212 while (getLexer().is(AsmToken::Comma)) {
3213 Lex();
3214
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003215 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003216 return true;
3217
3218 Values.push_back((uint8_t)CurrValue);
3219 }
3220
3221 getStreamer().EmitCFIEscape(Values);
3222 return false;
3223}
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003226/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003227bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003228 if (getLexer().isNot(AsmToken::EndOfStatement))
3229 return Error(getLexer().getLoc(),
3230 "unexpected token in '.cfi_signal_frame'");
3231
3232 getStreamer().EmitCFISignalFrame();
3233 return false;
3234}
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003237/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003238bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 int64_t Register = 0;
3240
Jim Grosbach4b905842013-09-20 23:08:21 +00003241 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003242 return true;
3243
3244 getStreamer().EmitCFIUndefined(Register);
3245 return false;
3246}
3247
Jim Grosbach4b905842013-09-20 23:08:21 +00003248/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003249/// ::= .macros_on
3250/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003251bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003252 if (getLexer().isNot(AsmToken::EndOfStatement))
3253 return Error(getLexer().getLoc(),
3254 "unexpected token in '" + Directive + "' directive");
3255
Jim Grosbach4b905842013-09-20 23:08:21 +00003256 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003257 return false;
3258}
3259
Jim Grosbach4b905842013-09-20 23:08:21 +00003260/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003261/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003262bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003263 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003264 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003265 return TokError("expected identifier in '.macro' directive");
3266
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003267 if (getLexer().is(AsmToken::Comma))
3268 Lex();
3269
Eli Bendersky17233942013-01-15 22:59:42 +00003270 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003271 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003272
3273 if (Parameters.size() && Parameters.back().Vararg)
3274 return Error(Lexer.getLoc(),
3275 "Vararg parameter '" + Parameters.back().Name +
3276 "' should be last one in the list of parameters.");
3277
David Majnemer91fc4c22014-01-29 18:57:46 +00003278 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003279 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003280 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003281
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003282 if (Lexer.is(AsmToken::Colon)) {
3283 Lex(); // consume ':'
3284
3285 SMLoc QualLoc;
3286 StringRef Qualifier;
3287
3288 QualLoc = Lexer.getLoc();
3289 if (parseIdentifier(Qualifier))
3290 return Error(QualLoc, "missing parameter qualifier for "
3291 "'" + Parameter.Name + "' in macro '" + Name + "'");
3292
3293 if (Qualifier == "req")
3294 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003295 else if (Qualifier == "vararg" && !IsDarwin)
3296 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003297 else
3298 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3299 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3300 }
3301
David Majnemer91fc4c22014-01-29 18:57:46 +00003302 if (getLexer().is(AsmToken::Equal)) {
3303 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003304
3305 SMLoc ParamLoc;
3306
3307 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003308 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003309 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003310
3311 if (Parameter.Required)
3312 Warning(ParamLoc, "pointless default value for required parameter "
3313 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003314 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003315
3316 Parameters.push_back(Parameter);
3317
3318 if (getLexer().is(AsmToken::Comma))
3319 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003320 }
3321
3322 // Eat the end of statement.
3323 Lex();
3324
3325 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003326 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003327
3328 // Lex the macro definition.
3329 for (;;) {
3330 // Check whether we have reached the end of the file.
3331 if (getLexer().is(AsmToken::Eof))
3332 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3333
3334 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003335 if (getLexer().is(AsmToken::Identifier)) {
3336 if (getTok().getIdentifier() == ".endm" ||
3337 getTok().getIdentifier() == ".endmacro") {
3338 if (MacroDepth == 0) { // Outermost macro.
3339 EndToken = getTok();
3340 Lex();
3341 if (getLexer().isNot(AsmToken::EndOfStatement))
3342 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3343 "' directive");
3344 break;
3345 } else {
3346 // Otherwise we just found the end of an inner macro.
3347 --MacroDepth;
3348 }
3349 } else if (getTok().getIdentifier() == ".macro") {
3350 // We allow nested macros. Those aren't instantiated until the outermost
3351 // macro is expanded so just ignore them for now.
3352 ++MacroDepth;
3353 }
Eli Bendersky17233942013-01-15 22:59:42 +00003354 }
3355
3356 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003357 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003358 }
3359
Jim Grosbach4b905842013-09-20 23:08:21 +00003360 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003361 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3362 }
3363
3364 const char *BodyStart = StartToken.getLoc().getPointer();
3365 const char *BodyEnd = EndToken.getLoc().getPointer();
3366 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003367 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3368 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003369 return false;
3370}
3371
Jim Grosbach4b905842013-09-20 23:08:21 +00003372/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003373///
3374/// With the support added for named parameters there may be code out there that
3375/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003376/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003377/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003378/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003379/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3380/// warning that the positional parameter found in body which have no effect.
3381/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003382/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003383/// intended or change the macro to use the named parameters. It is possible
3384/// this warning will trigger when the none of the named parameters are used
3385/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003386void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003387 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003388 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389 // If this macro is not defined with named parameters the warning we are
3390 // checking for here doesn't apply.
3391 unsigned NParameters = Parameters.size();
3392 if (NParameters == 0)
3393 return;
3394
3395 bool NamedParametersFound = false;
3396 bool PositionalParametersFound = false;
3397
3398 // Look at the body of the macro for use of both the named parameters and what
3399 // are likely to be positional parameters. This is what expandMacro() is
3400 // doing when it finds the parameters in the body.
3401 while (!Body.empty()) {
3402 // Scan for the next possible parameter.
3403 std::size_t End = Body.size(), Pos = 0;
3404 for (; Pos != End; ++Pos) {
3405 // Check for a substitution or escape.
3406 // This macro is defined with parameters, look for \foo, \bar, etc.
3407 if (Body[Pos] == '\\' && Pos + 1 != End)
3408 break;
3409
3410 // This macro should have parameters, but look for $0, $1, ..., $n too.
3411 if (Body[Pos] != '$' || Pos + 1 == End)
3412 continue;
3413 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003414 if (Next == '$' || Next == 'n' ||
3415 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003416 break;
3417 }
3418
3419 // Check if we reached the end.
3420 if (Pos == End)
3421 break;
3422
3423 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003424 switch (Body[Pos + 1]) {
3425 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003426 case '$':
3427 break;
3428
Jim Grosbach4b905842013-09-20 23:08:21 +00003429 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003430 case 'n':
3431 PositionalParametersFound = true;
3432 break;
3433
Jim Grosbach4b905842013-09-20 23:08:21 +00003434 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003435 default: {
3436 PositionalParametersFound = true;
3437 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003438 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003439 }
3440 Pos += 2;
3441 } else {
3442 unsigned I = Pos + 1;
3443 while (isIdentifierChar(Body[I]) && I + 1 != End)
3444 ++I;
3445
Jim Grosbach4b905842013-09-20 23:08:21 +00003446 const char *Begin = Body.data() + Pos + 1;
3447 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003448 unsigned Index = 0;
3449 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003450 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003451 break;
3452
3453 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003454 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3455 Pos += 3;
3456 else {
3457 Pos = I;
3458 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003459 } else {
3460 NamedParametersFound = true;
3461 Pos += 1 + Argument.size();
3462 }
3463 }
3464 // Update the scan point.
3465 Body = Body.substr(Pos);
3466 }
3467
3468 if (!NamedParametersFound && PositionalParametersFound)
3469 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3470 "used in macro body, possible positional parameter "
3471 "found in body which will have no effect");
3472}
3473
Jim Grosbach4b905842013-09-20 23:08:21 +00003474/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003475/// ::= .endm
3476/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003477bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003478 if (getLexer().isNot(AsmToken::EndOfStatement))
3479 return TokError("unexpected token in '" + Directive + "' directive");
3480
3481 // If we are inside a macro instantiation, terminate the current
3482 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003483 if (isInsideMacroInstantiation()) {
3484 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003485 return false;
3486 }
3487
3488 // Otherwise, this .endmacro is a stray entry in the file; well formed
3489 // .endmacro directives are handled during the macro definition parsing.
3490 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003491 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003492}
3493
Jim Grosbach4b905842013-09-20 23:08:21 +00003494/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003495/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003496bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003497 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003498 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003499 return TokError("expected identifier in '.purgem' directive");
3500
3501 if (getLexer().isNot(AsmToken::EndOfStatement))
3502 return TokError("unexpected token in '.purgem' directive");
3503
Jim Grosbach4b905842013-09-20 23:08:21 +00003504 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003505 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3506
Jim Grosbach4b905842013-09-20 23:08:21 +00003507 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003508 return false;
3509}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003510
Jim Grosbach4b905842013-09-20 23:08:21 +00003511/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003512/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003513bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003514 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003515
3516 // Expect a single argument: an expression that evaluates to a constant
3517 // in the inclusive range 0-30.
3518 SMLoc ExprLoc = getLexer().getLoc();
3519 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003520 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003521 return true;
3522 else if (getLexer().isNot(AsmToken::EndOfStatement))
3523 return TokError("unexpected token after expression in"
3524 " '.bundle_align_mode' directive");
3525 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3526 return Error(ExprLoc,
3527 "invalid bundle alignment size (expected between 0 and 30)");
3528
3529 Lex();
3530
3531 // Because of AlignSizePow2's verified range we can safely truncate it to
3532 // unsigned.
3533 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3534 return false;
3535}
3536
Jim Grosbach4b905842013-09-20 23:08:21 +00003537/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003538/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003539bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003540 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003541 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003542
Eli Bendersky802b6282013-01-07 21:51:08 +00003543 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3544 StringRef Option;
3545 SMLoc Loc = getTok().getLoc();
3546 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003547 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003548
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003549 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003550 return Error(Loc, kInvalidOptionError);
3551
3552 if (Option != "align_to_end")
3553 return Error(Loc, kInvalidOptionError);
3554 else if (getLexer().isNot(AsmToken::EndOfStatement))
3555 return Error(Loc,
3556 "unexpected token after '.bundle_lock' directive option");
3557 AlignToEnd = true;
3558 }
3559
Eli Benderskyf483ff92012-12-20 19:05:53 +00003560 Lex();
3561
Eli Bendersky802b6282013-01-07 21:51:08 +00003562 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003563 return false;
3564}
3565
Jim Grosbach4b905842013-09-20 23:08:21 +00003566/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003567/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003568bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003569 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003570
3571 if (getLexer().isNot(AsmToken::EndOfStatement))
3572 return TokError("unexpected token in '.bundle_unlock' directive");
3573 Lex();
3574
3575 getStreamer().EmitBundleUnlock();
3576 return false;
3577}
3578
Jim Grosbach4b905842013-09-20 23:08:21 +00003579/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003580/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003581bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003582 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003583
3584 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003585 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003586 return true;
3587
3588 int64_t FillExpr = 0;
3589 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3590 if (getLexer().isNot(AsmToken::Comma))
3591 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3592 Lex();
3593
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003594 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003595 return true;
3596
3597 if (getLexer().isNot(AsmToken::EndOfStatement))
3598 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3599 }
3600
3601 Lex();
3602
3603 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003604 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3605 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003606
3607 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003608 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003609
3610 return false;
3611}
3612
Jim Grosbach4b905842013-09-20 23:08:21 +00003613/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003614/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003615bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003616 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003617 const MCExpr *Value;
3618
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003619 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003620 return true;
3621
3622 if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token in directive");
3624
3625 if (Signed)
3626 getStreamer().EmitSLEB128Value(Value);
3627 else
3628 getStreamer().EmitULEB128Value(Value);
3629
3630 return false;
3631}
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003634/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003635bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003637 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003638 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003639 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003640
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003642 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003643
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003644 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003645
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003646 // Assembler local symbols don't make any sense here. Complain loudly.
3647 if (Sym->isTemporary())
3648 return Error(Loc, "non-local symbol required in directive");
3649
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003650 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3651 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003652
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003653 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003654 break;
3655
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003656 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003657 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003658 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003659 }
3660 }
3661
Sean Callanan686ed8d2010-01-19 20:22:31 +00003662 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003663 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003664}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003667/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003668bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003669 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003670
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003671 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003672 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003674 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003675
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003676 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003677 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003678
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003679 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003680 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003681 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003682
3683 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003684 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003685 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003686 return true;
3687
3688 int64_t Pow2Alignment = 0;
3689 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003690 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003691 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003692 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003693 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003694 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003695
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003696 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3697 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003698 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3699
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003700 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003701 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3702 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003703 if (!isPowerOf2_64(Pow2Alignment))
3704 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3705 Pow2Alignment = Log2_64(Pow2Alignment);
3706 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003707 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003708
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003709 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003710 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003711
Sean Callanan686ed8d2010-01-19 20:22:31 +00003712 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003713
Chris Lattner28ad7542009-07-09 17:25:12 +00003714 // NOTE: a size of zero for a .comm should create a undefined symbol
3715 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003716 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003717 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003718 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003719
Eric Christopherbc818852010-05-14 01:38:54 +00003720 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003721 // may internally end up wanting an alignment in bytes.
3722 // FIXME: Diagnose overflow.
3723 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003724 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003725 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003726
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003727 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003728 return Error(IDLoc, "invalid symbol redefinition");
3729
Chris Lattner28ad7542009-07-09 17:25:12 +00003730 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003731 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003732 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003733 return false;
3734 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003735
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003736 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003737 return false;
3738}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003739
Jim Grosbach4b905842013-09-20 23:08:21 +00003740/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003741/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003742bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003743 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003744 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003745
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003746 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003747 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003748 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003749
Sean Callanan686ed8d2010-01-19 20:22:31 +00003750 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003751
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003752 if (Str.empty())
3753 Error(Loc, ".abort detected. Assembly stopping.");
3754 else
3755 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003756 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003757
3758 return false;
3759}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003760
Jim Grosbach4b905842013-09-20 23:08:21 +00003761/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003762/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003763bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003764 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003765 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003766
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003767 // Allow the strings to have escaped octal character sequence.
3768 std::string Filename;
3769 if (parseEscapedString(Filename))
3770 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003771 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003772 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003773
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003774 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003775 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003776
Chris Lattner693fbb82009-07-16 06:14:39 +00003777 // Attempt to switch the lexer to the included file before consuming the end
3778 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003779 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003780 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003781 return true;
3782 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003783
3784 return false;
3785}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003786
Jim Grosbach4b905842013-09-20 23:08:21 +00003787/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003788/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003789bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003790 if (getLexer().isNot(AsmToken::String))
3791 return TokError("expected string in '.incbin' directive");
3792
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003793 // Allow the strings to have escaped octal character sequence.
3794 std::string Filename;
3795 if (parseEscapedString(Filename))
3796 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003797 SMLoc IncbinLoc = getLexer().getLoc();
3798 Lex();
3799
3800 if (getLexer().isNot(AsmToken::EndOfStatement))
3801 return TokError("unexpected token in '.incbin' directive");
3802
Kevin Enderby109f25c2011-12-14 21:47:48 +00003803 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003804 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003805 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3806 return true;
3807 }
3808
3809 return false;
3810}
3811
Jim Grosbach4b905842013-09-20 23:08:21 +00003812/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003813/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3814bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003815 TheCondStack.push_back(TheCondState);
3816 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003817 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003818 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003819 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003820 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003821 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003822 return true;
3823
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003824 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003825 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003826
Sean Callanan686ed8d2010-01-19 20:22:31 +00003827 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003828
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003829 switch (DirKind) {
3830 default:
3831 llvm_unreachable("unsupported directive");
3832 case DK_IF:
3833 case DK_IFNE:
3834 break;
3835 case DK_IFEQ:
3836 ExprValue = ExprValue == 0;
3837 break;
3838 case DK_IFGE:
3839 ExprValue = ExprValue >= 0;
3840 break;
3841 case DK_IFGT:
3842 ExprValue = ExprValue > 0;
3843 break;
3844 case DK_IFLE:
3845 ExprValue = ExprValue <= 0;
3846 break;
3847 case DK_IFLT:
3848 ExprValue = ExprValue < 0;
3849 break;
3850 }
3851
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852 TheCondState.CondMet = ExprValue;
3853 TheCondState.Ignore = !TheCondState.CondMet;
3854 }
3855
3856 return false;
3857}
3858
Jim Grosbach4b905842013-09-20 23:08:21 +00003859/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003860/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003861bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003862 TheCondStack.push_back(TheCondState);
3863 TheCondState.TheCond = AsmCond::IfCond;
3864
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003865 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003866 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003867 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003868 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003869
3870 if (getLexer().isNot(AsmToken::EndOfStatement))
3871 return TokError("unexpected token in '.ifb' directive");
3872
3873 Lex();
3874
3875 TheCondState.CondMet = ExpectBlank == Str.empty();
3876 TheCondState.Ignore = !TheCondState.CondMet;
3877 }
3878
3879 return false;
3880}
3881
Jim Grosbach4b905842013-09-20 23:08:21 +00003882/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003883/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003884/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003885bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003886 TheCondStack.push_back(TheCondState);
3887 TheCondState.TheCond = AsmCond::IfCond;
3888
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003889 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003890 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003891 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003892 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003893
3894 if (getLexer().isNot(AsmToken::Comma))
3895 return TokError("unexpected token in '.ifc' directive");
3896
3897 Lex();
3898
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003899 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003900
3901 if (getLexer().isNot(AsmToken::EndOfStatement))
3902 return TokError("unexpected token in '.ifc' directive");
3903
3904 Lex();
3905
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003906 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003907 TheCondState.Ignore = !TheCondState.CondMet;
3908 }
3909
3910 return false;
3911}
3912
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003913/// parseDirectiveIfeqs
3914/// ::= .ifeqs string1, string2
3915bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3916 if (Lexer.isNot(AsmToken::String)) {
3917 TokError("expected string parameter for '.ifeqs' directive");
3918 eatToEndOfStatement();
3919 return true;
3920 }
3921
3922 StringRef String1 = getTok().getStringContents();
3923 Lex();
3924
3925 if (Lexer.isNot(AsmToken::Comma)) {
3926 TokError("expected comma after first string for '.ifeqs' directive");
3927 eatToEndOfStatement();
3928 return true;
3929 }
3930
3931 Lex();
3932
3933 if (Lexer.isNot(AsmToken::String)) {
3934 TokError("expected string parameter for '.ifeqs' directive");
3935 eatToEndOfStatement();
3936 return true;
3937 }
3938
3939 StringRef String2 = getTok().getStringContents();
3940 Lex();
3941
3942 TheCondStack.push_back(TheCondState);
3943 TheCondState.TheCond = AsmCond::IfCond;
3944 TheCondState.CondMet = String1 == String2;
3945 TheCondState.Ignore = !TheCondState.CondMet;
3946
3947 return false;
3948}
3949
Jim Grosbach4b905842013-09-20 23:08:21 +00003950/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003951/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003952bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003953 StringRef Name;
3954 TheCondStack.push_back(TheCondState);
3955 TheCondState.TheCond = AsmCond::IfCond;
3956
3957 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003958 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003959 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003960 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003961 return TokError("expected identifier after '.ifdef'");
3962
3963 Lex();
3964
3965 MCSymbol *Sym = getContext().LookupSymbol(Name);
3966
3967 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003968 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003969 else
Craig Topper353eda42014-04-24 06:44:33 +00003970 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003971 TheCondState.Ignore = !TheCondState.CondMet;
3972 }
3973
3974 return false;
3975}
3976
Jim Grosbach4b905842013-09-20 23:08:21 +00003977/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003978/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003979bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003980 if (TheCondState.TheCond != AsmCond::IfCond &&
3981 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003982 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3983 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003984 TheCondState.TheCond = AsmCond::ElseIfCond;
3985
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003986 bool LastIgnoreState = false;
3987 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003988 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003989 if (LastIgnoreState || TheCondState.CondMet) {
3990 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003991 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003992 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003993 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003994 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003995 return true;
3996
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003997 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003998 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003999
Sean Callanan686ed8d2010-01-19 20:22:31 +00004000 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004001 TheCondState.CondMet = ExprValue;
4002 TheCondState.Ignore = !TheCondState.CondMet;
4003 }
4004
4005 return false;
4006}
4007
Jim Grosbach4b905842013-09-20 23:08:21 +00004008/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004009/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004010bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004011 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004012 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004013
Sean Callanan686ed8d2010-01-19 20:22:31 +00004014 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004015
4016 if (TheCondState.TheCond != AsmCond::IfCond &&
4017 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004018 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4019 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020 TheCondState.TheCond = AsmCond::ElseCond;
4021 bool LastIgnoreState = false;
4022 if (!TheCondStack.empty())
4023 LastIgnoreState = TheCondStack.back().Ignore;
4024 if (LastIgnoreState || TheCondState.CondMet)
4025 TheCondState.Ignore = true;
4026 else
4027 TheCondState.Ignore = false;
4028
4029 return false;
4030}
4031
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004032/// parseDirectiveEnd
4033/// ::= .end
4034bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4035 if (getLexer().isNot(AsmToken::EndOfStatement))
4036 return TokError("unexpected token in '.end' directive");
4037
4038 Lex();
4039
4040 while (Lexer.isNot(AsmToken::Eof))
4041 Lex();
4042
4043 return false;
4044}
4045
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004046/// parseDirectiveError
4047/// ::= .err
4048/// ::= .error [string]
4049bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4050 if (!TheCondStack.empty()) {
4051 if (TheCondStack.back().Ignore) {
4052 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004053 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004054 }
4055 }
4056
4057 if (!WithMessage)
4058 return Error(L, ".err encountered");
4059
4060 StringRef Message = ".error directive invoked in source file";
4061 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4062 if (Lexer.isNot(AsmToken::String)) {
4063 TokError(".error argument must be a string");
4064 eatToEndOfStatement();
4065 return true;
4066 }
4067
4068 Message = getTok().getStringContents();
4069 Lex();
4070 }
4071
4072 Error(L, Message);
4073 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004074}
4075
Jim Grosbach4b905842013-09-20 23:08:21 +00004076/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004077/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004078bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004079 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004080 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004081
Sean Callanan686ed8d2010-01-19 20:22:31 +00004082 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004083
Jim Grosbach4b905842013-09-20 23:08:21 +00004084 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004085 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4086 ".else");
4087 if (!TheCondStack.empty()) {
4088 TheCondState = TheCondStack.back();
4089 TheCondStack.pop_back();
4090 }
4091
4092 return false;
4093}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004094
Eli Bendersky17233942013-01-15 22:59:42 +00004095void AsmParser::initializeDirectiveKindMap() {
4096 DirectiveKindMap[".set"] = DK_SET;
4097 DirectiveKindMap[".equ"] = DK_EQU;
4098 DirectiveKindMap[".equiv"] = DK_EQUIV;
4099 DirectiveKindMap[".ascii"] = DK_ASCII;
4100 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4101 DirectiveKindMap[".string"] = DK_STRING;
4102 DirectiveKindMap[".byte"] = DK_BYTE;
4103 DirectiveKindMap[".short"] = DK_SHORT;
4104 DirectiveKindMap[".value"] = DK_VALUE;
4105 DirectiveKindMap[".2byte"] = DK_2BYTE;
4106 DirectiveKindMap[".long"] = DK_LONG;
4107 DirectiveKindMap[".int"] = DK_INT;
4108 DirectiveKindMap[".4byte"] = DK_4BYTE;
4109 DirectiveKindMap[".quad"] = DK_QUAD;
4110 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004111 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004112 DirectiveKindMap[".single"] = DK_SINGLE;
4113 DirectiveKindMap[".float"] = DK_FLOAT;
4114 DirectiveKindMap[".double"] = DK_DOUBLE;
4115 DirectiveKindMap[".align"] = DK_ALIGN;
4116 DirectiveKindMap[".align32"] = DK_ALIGN32;
4117 DirectiveKindMap[".balign"] = DK_BALIGN;
4118 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4119 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4120 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4121 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4122 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4123 DirectiveKindMap[".org"] = DK_ORG;
4124 DirectiveKindMap[".fill"] = DK_FILL;
4125 DirectiveKindMap[".zero"] = DK_ZERO;
4126 DirectiveKindMap[".extern"] = DK_EXTERN;
4127 DirectiveKindMap[".globl"] = DK_GLOBL;
4128 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004129 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4130 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4131 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4132 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4133 DirectiveKindMap[".reference"] = DK_REFERENCE;
4134 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4135 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4136 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4137 DirectiveKindMap[".comm"] = DK_COMM;
4138 DirectiveKindMap[".common"] = DK_COMMON;
4139 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4140 DirectiveKindMap[".abort"] = DK_ABORT;
4141 DirectiveKindMap[".include"] = DK_INCLUDE;
4142 DirectiveKindMap[".incbin"] = DK_INCBIN;
4143 DirectiveKindMap[".code16"] = DK_CODE16;
4144 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4145 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004146 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004147 DirectiveKindMap[".irp"] = DK_IRP;
4148 DirectiveKindMap[".irpc"] = DK_IRPC;
4149 DirectiveKindMap[".endr"] = DK_ENDR;
4150 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4151 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4152 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4153 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004154 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4155 DirectiveKindMap[".ifge"] = DK_IFGE;
4156 DirectiveKindMap[".ifgt"] = DK_IFGT;
4157 DirectiveKindMap[".ifle"] = DK_IFLE;
4158 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004159 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004160 DirectiveKindMap[".ifb"] = DK_IFB;
4161 DirectiveKindMap[".ifnb"] = DK_IFNB;
4162 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004163 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004164 DirectiveKindMap[".ifnc"] = DK_IFNC;
4165 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4166 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4167 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4168 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4169 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004170 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004171 DirectiveKindMap[".endif"] = DK_ENDIF;
4172 DirectiveKindMap[".skip"] = DK_SKIP;
4173 DirectiveKindMap[".space"] = DK_SPACE;
4174 DirectiveKindMap[".file"] = DK_FILE;
4175 DirectiveKindMap[".line"] = DK_LINE;
4176 DirectiveKindMap[".loc"] = DK_LOC;
4177 DirectiveKindMap[".stabs"] = DK_STABS;
4178 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4179 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4180 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4181 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4182 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4183 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4184 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4185 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4186 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4187 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4188 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4189 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4190 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4191 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4192 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4193 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4194 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4195 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4196 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4197 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4198 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004199 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004200 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4201 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4202 DirectiveKindMap[".macro"] = DK_MACRO;
4203 DirectiveKindMap[".endm"] = DK_ENDM;
4204 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4205 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004206 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004207 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004208}
4209
Jim Grosbach4b905842013-09-20 23:08:21 +00004210MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004211 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004212
Rafael Espindola34b9c512012-06-03 23:57:14 +00004213 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004214 for (;;) {
4215 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004216 if (getLexer().is(AsmToken::Eof)) {
4217 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004218 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004219 }
4220
Rafael Espindola34b9c512012-06-03 23:57:14 +00004221 if (Lexer.is(AsmToken::Identifier) &&
4222 (getTok().getIdentifier() == ".rept")) {
4223 ++NestLevel;
4224 }
4225
4226 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004227 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004228 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004229 EndToken = getTok();
4230 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004231 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4232 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004233 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004234 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004235 break;
4236 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004237 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004238 }
4239
Rafael Espindola34b9c512012-06-03 23:57:14 +00004240 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004241 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004242 }
4243
4244 const char *BodyStart = StartToken.getLoc().getPointer();
4245 const char *BodyEnd = EndToken.getLoc().getPointer();
4246 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4247
Rafael Espindola34b9c512012-06-03 23:57:14 +00004248 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004249 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004250 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004251}
4252
Jim Grosbach4b905842013-09-20 23:08:21 +00004253void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004254 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004255 OS << ".endr\n";
4256
4257 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004258 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004259
Rafael Espindola34b9c512012-06-03 23:57:14 +00004260 // Create the macro instantiation object and add to the current macro
4261 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004262 MacroInstantiation *MI = new MacroInstantiation(
4263 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004264 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004265
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 // Jump to the macro instantiation and prime the lexer.
4267 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004268 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004269 Lex();
4270}
4271
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004272/// parseDirectiveRept
4273/// ::= .rep | .rept count
4274bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004275 const MCExpr *CountExpr;
4276 SMLoc CountLoc = getTok().getLoc();
4277 if (parseExpression(CountExpr))
4278 return true;
4279
Rafael Espindola34b9c512012-06-03 23:57:14 +00004280 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004281 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4282 eatToEndOfStatement();
4283 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4284 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004285
4286 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004287 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004288
4289 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004290 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004291
4292 // Eat the end of statement.
4293 Lex();
4294
4295 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004296 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004297 if (!M)
4298 return true;
4299
4300 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4301 // to hold the macro body with substitutions.
4302 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004303 raw_svector_ostream OS(Buf);
4304 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004305 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004306 return true;
4307 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004308 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309
4310 return false;
4311}
4312
Jim Grosbach4b905842013-09-20 23:08:21 +00004313/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004314/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004315bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004316 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004317
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004318 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004319 return TokError("expected identifier in '.irp' directive");
4320
Rafael Espindola768b41c2012-06-15 14:02:34 +00004321 if (Lexer.isNot(AsmToken::Comma))
4322 return TokError("expected comma in '.irp' directive");
4323
4324 Lex();
4325
Eli Bendersky38274122013-01-14 23:22:36 +00004326 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004327 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004328 return true;
4329
4330 // Eat the end of statement.
4331 Lex();
4332
4333 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004334 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004335 if (!M)
4336 return true;
4337
4338 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4339 // to hold the macro body with substitutions.
4340 SmallString<256> Buf;
4341 raw_svector_ostream OS(Buf);
4342
Eli Bendersky38274122013-01-14 23:22:36 +00004343 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004344 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004345 return true;
4346 }
4347
Jim Grosbach4b905842013-09-20 23:08:21 +00004348 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004349
4350 return false;
4351}
4352
Jim Grosbach4b905842013-09-20 23:08:21 +00004353/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004354/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004355bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004356 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004357
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004358 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004359 return TokError("expected identifier in '.irpc' directive");
4360
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004361 if (Lexer.isNot(AsmToken::Comma))
4362 return TokError("expected comma in '.irpc' directive");
4363
4364 Lex();
4365
Eli Bendersky38274122013-01-14 23:22:36 +00004366 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004367 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004368 return true;
4369
4370 if (A.size() != 1 || A.front().size() != 1)
4371 return TokError("unexpected token in '.irpc' directive");
4372
4373 // Eat the end of statement.
4374 Lex();
4375
4376 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004377 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004378 if (!M)
4379 return true;
4380
4381 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4382 // to hold the macro body with substitutions.
4383 SmallString<256> Buf;
4384 raw_svector_ostream OS(Buf);
4385
4386 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004387 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004388 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004389 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004390
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004391 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004392 return true;
4393 }
4394
Jim Grosbach4b905842013-09-20 23:08:21 +00004395 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004396
4397 return false;
4398}
4399
Jim Grosbach4b905842013-09-20 23:08:21 +00004400bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004401 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004402 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004403
4404 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004405 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004406 assert(getLexer().is(AsmToken::EndOfStatement));
4407
Jim Grosbach4b905842013-09-20 23:08:21 +00004408 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004409 return false;
4410}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004411
Jim Grosbach4b905842013-09-20 23:08:21 +00004412bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004413 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004414 const MCExpr *Value;
4415 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004416 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004417 return true;
4418 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4419 if (!MCE)
4420 return Error(ExprLoc, "unexpected expression in _emit");
4421 uint64_t IntValue = MCE->getValue();
4422 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4423 return Error(ExprLoc, "literal value out of range for directive");
4424
Chad Rosierc7f552c2013-02-12 21:33:51 +00004425 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4426 return false;
4427}
4428
Jim Grosbach4b905842013-09-20 23:08:21 +00004429bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004430 const MCExpr *Value;
4431 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004432 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004433 return true;
4434 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4435 if (!MCE)
4436 return Error(ExprLoc, "unexpected expression in align");
4437 uint64_t IntValue = MCE->getValue();
4438 if (!isPowerOf2_64(IntValue))
4439 return Error(ExprLoc, "literal value not a power of two greater then zero");
4440
Jim Grosbach4b905842013-09-20 23:08:21 +00004441 Info.AsmRewrites->push_back(
4442 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004443 return false;
4444}
4445
Chad Rosierf43fcf52013-02-13 21:27:17 +00004446// We are comparing pointers, but the pointers are relative to a single string.
4447// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004448static int rewritesSort(const AsmRewrite *AsmRewriteA,
4449 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004450 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4451 return -1;
4452 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4453 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004454
Chad Rosierfce4fab2013-04-08 17:43:47 +00004455 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4456 // rewrite to the same location. Make sure the SizeDirective rewrite is
4457 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4458 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004459 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4460 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004461 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004462
Jim Grosbach4b905842013-09-20 23:08:21 +00004463 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4464 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004465 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004466 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004467}
4468
Jim Grosbach4b905842013-09-20 23:08:21 +00004469bool AsmParser::parseMSInlineAsm(
4470 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4471 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4472 SmallVectorImpl<std::string> &Constraints,
4473 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4474 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004475 SmallVector<void *, 4> InputDecls;
4476 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004477 SmallVector<bool, 4> InputDeclsAddressOf;
4478 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004479 SmallVector<std::string, 4> InputConstraints;
4480 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004481 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004482
Benjamin Kramer1a136112013-02-15 20:37:21 +00004483 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004484
4485 // Prime the lexer.
4486 Lex();
4487
4488 // While we have input, parse each statement.
4489 unsigned InputIdx = 0;
4490 unsigned OutputIdx = 0;
4491 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004492 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004493 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004494 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004495
Chad Rosier149e8e02012-12-12 22:45:52 +00004496 if (Info.ParseError)
4497 return true;
4498
Benjamin Kramer1a136112013-02-15 20:37:21 +00004499 if (Info.Opcode == ~0U)
4500 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004501
Benjamin Kramer1a136112013-02-15 20:37:21 +00004502 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004503
Benjamin Kramer1a136112013-02-15 20:37:21 +00004504 // Build the list of clobbers, outputs and inputs.
4505 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004506 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004507
Benjamin Kramer1a136112013-02-15 20:37:21 +00004508 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004509 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004510 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004511
Benjamin Kramer1a136112013-02-15 20:37:21 +00004512 // Register operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00004513 if (Operand.isReg() && !Operand.needAddressOf()) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004514 unsigned NumDefs = Desc.getNumDefs();
4515 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004516 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4517 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004518 continue;
4519 }
4520
4521 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004522 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004523 if (SymName.empty())
4524 continue;
4525
David Blaikie960ea3f2014-06-08 16:18:35 +00004526 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004527 if (!OpDecl)
4528 continue;
4529
4530 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004531 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004532 if (isOutput) {
4533 ++InputIdx;
4534 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004535 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4536 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004537 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004538 } else {
4539 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004540 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4541 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004542 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004543 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004544 }
Reid Kleckneree088972013-12-10 18:27:32 +00004545
4546 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004547 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4548 Desc.getNumImplicitDefs());
4549 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004550 }
4551
4552 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004553 NumOutputs = OutputDecls.size();
4554 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004555
4556 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004557 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4558 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4559 ClobberRegs.end());
4560 Clobbers.assign(ClobberRegs.size(), std::string());
4561 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4562 raw_string_ostream OS(Clobbers[I]);
4563 IP->printRegName(OS, ClobberRegs[I]);
4564 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004565
4566 // Merge the various outputs and inputs. Output are expected first.
4567 if (NumOutputs || NumInputs) {
4568 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004569 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004570 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004571 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004572 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004573 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004574 }
4575 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004576 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004577 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004578 }
4579 }
4580
4581 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004582 std::string AsmStringIR;
4583 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004584 StringRef ASMString =
4585 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4586 const char *AsmStart = ASMString.begin();
4587 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004588 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004589 for (const AsmRewrite &AR : AsmStrRewrites) {
4590 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004591 if (Kind == AOK_Delete)
4592 continue;
4593
David Majnemer8114c1a2014-06-23 02:17:16 +00004594 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004595 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004596
Chad Rosier120eefd2013-03-19 17:32:17 +00004597 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004598 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004599 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004600
Chad Rosier37e755c2012-10-23 17:43:43 +00004601 // Skip the original expression.
4602 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004603 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004604 continue;
4605 }
4606
Chad Rosierff10ed12013-04-12 16:26:42 +00004607 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004608 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004609 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004610 default:
4611 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004612 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004613 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004614 break;
4615 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004616 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004617 break;
4618 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004619 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004620 break;
4621 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004622 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004623 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004624 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004625 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004626 default: break;
4627 case 8: OS << "byte ptr "; break;
4628 case 16: OS << "word ptr "; break;
4629 case 32: OS << "dword ptr "; break;
4630 case 64: OS << "qword ptr "; break;
4631 case 80: OS << "xword ptr "; break;
4632 case 128: OS << "xmmword ptr "; break;
4633 case 256: OS << "ymmword ptr "; break;
4634 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004635 break;
4636 case AOK_Emit:
4637 OS << ".byte";
4638 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004639 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004640 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004641 OS << ".align " << Val;
4642
4643 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004644 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004645 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4646 break;
4647 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004648 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004649 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004650 OS.flush();
4651 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004652 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004653 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004654 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004655 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004656
Chad Rosier8bce6642012-10-18 15:49:34 +00004657 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004658 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004659 }
4660
4661 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004662 if (AsmStart != AsmEnd)
4663 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004664
4665 AsmString = OS.str();
4666 return false;
4667}
4668
Daniel Dunbar01e36072010-07-17 02:26:10 +00004669/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004670MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4671 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004672 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004673}