blob: 932d0f3318ec463652c195cc188f3eaa08e5e436 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000048static cl::opt<bool>
49FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
51
Eric Christophera7c32732012-12-18 00:30:54 +000052MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000053
Daniel Dunbar86033402010-07-12 17:54:38 +000054namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000058
59struct MCAsmMacroParameter {
60 StringRef Name;
61 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000066};
67
Eli Benderskya313ae62013-01-16 18:56:50 +000068typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
69
70struct MCAsmMacro {
71 StringRef Name;
72 StringRef Body;
73 MCAsmMacroParameters Parameters;
74
75public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000076 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000077 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000078};
79
Daniel Dunbar43235712010-07-18 18:54:11 +000080/// \brief Helper class for storing information about an active macro
81/// instantiation.
82struct MacroInstantiation {
83 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000084 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000085
86 /// The macro instantiation with substitutions.
87 MemoryBuffer *Instantiation;
88
89 /// The location of the instantiation.
90 SMLoc InstantiationLoc;
91
Daniel Dunbar40f1d852012-12-01 01:38:48 +000092 /// The buffer where parsing should resume upon instantiation completion.
93 int ExitBuffer;
94
Daniel Dunbar43235712010-07-18 18:54:11 +000095 /// The location where parsing should resume upon instantiation completion.
96 SMLoc ExitLoc;
97
98public:
Eli Bendersky38274122013-01-14 23:22:36 +000099 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000100 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +0000101};
102
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000105 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 unsigned Opcode;
109
Jim Grosbach4b905842013-09-20 23:08:21 +0000110 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 bool ParseError;
112
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
114
Craig Topper353eda42014-04-24 06:44:33 +0000115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000118};
119
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000133
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000165 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
171 int LastQueryBuffer;
172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
Craig Topper59be68f2014-03-08 07:14:16 +0000188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
Craig Topper59be68f2014-03-08 07:14:16 +0000199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Craig Topper59be68f2014-03-08 07:14:16 +0000222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseIdentifier(StringRef &Res) override;
242 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Craig Topper59be68f2014-03-08 07:14:16 +0000244 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
257 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
313 /// \param InBuffer If not -1, should be the known buffer id that contains the
314 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000315 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000334
Eli Bendersky17233942013-01-15 22:59:42 +0000335 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000336 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000358 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000360 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000361 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000362 };
363
Jim Grosbach4b905842013-09-20 23:08:21 +0000364 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367
368 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000371 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
373 bool parseDirectiveFill(); // ".fill"
374 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000375 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
377 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000380
Eli Bendersky17233942013-01-15 22:59:42 +0000381 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveFile(SMLoc DirectiveLoc);
383 bool parseDirectiveLine();
384 bool parseDirectiveLoc();
385 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000386
387 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000389 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000390 bool parseDirectiveCFISections();
391 bool parseDirectiveCFIStartProc();
392 bool parseDirectiveCFIEndProc();
393 bool parseDirectiveCFIDefCfaOffset();
394 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIAdjustCfaOffset();
396 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
400 bool parseDirectiveCFIRememberState();
401 bool parseDirectiveCFIRestoreState();
402 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIEscape();
405 bool parseDirectiveCFISignalFrame();
406 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000407
408 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveEndMacro(StringRef Directive);
411 bool parseDirectiveMacro(SMLoc DirectiveLoc);
412 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420
Eli Bendersky17233942013-01-15 22:59:42 +0000421 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000423
424 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveAbort(); // ".abort"
434 bool parseDirectiveInclude(); // ".include"
435 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000437 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
438 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000439 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000441 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000443 // ".ifeqs"
444 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000445 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
447 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
448 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
449 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000450 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000454
Rafael Espindola34b9c512012-06-03 23:57:14 +0000455 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
457 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000458 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000459 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
461 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
462 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000463
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 size_t Len);
467
468 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000470
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000471 // "end"
472 bool parseDirectiveEnd(SMLoc DirectiveLoc);
473
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000474 // ".err" or ".error"
475 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000476
Eli Bendersky17233942013-01-15 22:59:42 +0000477 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000478};
Daniel Dunbar86033402010-07-12 17:54:38 +0000479}
480
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000481namespace llvm {
482
483extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000484extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000485extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486
487}
488
Chris Lattnerc35681b2010-01-19 19:46:13 +0000489enum { DEFAULT_ADDRSPACE = 0 };
490
Jim Grosbach4b905842013-09-20 23:08:21 +0000491AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
492 const MCAsmInfo &_MAI)
493 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Craig Topper353eda42014-04-24 06:44:33 +0000494 PlatformParser(nullptr), CurBuffer(0), MacrosEnabledFlag(true),
Saleem Abdulrasool9dd60cf2014-05-22 06:02:59 +0000495 HadError(false), CppHashLineNumber(0), AssemblerDialect(~0U),
496 IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000497 // Save the old handler.
498 SavedDiagHandler = SrcMgr.getDiagHandler();
499 SavedDiagContext = SrcMgr.getDiagContext();
500 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000501 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000503
Daniel Dunbarc5011082010-07-12 18:12:02 +0000504 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000505 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
506 case MCObjectFileInfo::IsCOFF:
507 PlatformParser = createCOFFAsmParser();
508 PlatformParser->Initialize(*this);
509 break;
510 case MCObjectFileInfo::IsMachO:
511 PlatformParser = createDarwinAsmParser();
512 PlatformParser->Initialize(*this);
513 IsDarwin = true;
514 break;
515 case MCObjectFileInfo::IsELF:
516 PlatformParser = createELFAsmParser();
517 PlatformParser->Initialize(*this);
518 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000519 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000520
Eli Bendersky17233942013-01-15 22:59:42 +0000521 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000522}
523
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000527
528 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
530 ie = MacroMap.end();
531 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000532 delete it->getValue();
533
Daniel Dunbarc5011082010-07-12 18:12:02 +0000534 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000553 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
569 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 if (NewBuf == -1)
571 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000572
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000574
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000576
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 return false;
578}
Daniel Dunbar43235712010-07-18 18:54:11 +0000579
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000580/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000581/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000582/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000583bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 std::string IncludedFile;
585 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (NewBuf == -1)
587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Jim Grosbach4b905842013-09-20 23:08:21 +0000594void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000595 if (InBuffer != -1) {
596 CurBuffer = InBuffer;
597 } else {
598 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
599 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000600 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
601}
602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603const AsmToken &AsmParser::Lex() {
604 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 if (tok->is(AsmToken::Eof)) {
607 // If this is the end of an included file, pop the parent file off the
608 // include stack.
609 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
610 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000611 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 tok = &Lexer.Lex();
613 }
614 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000617 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000620}
621
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000622bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000623 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000624 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000625 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000626
Chris Lattner36e02122009-06-21 20:54:55 +0000627 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000628 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000629
630 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000631 AsmCond StartingCondState = TheCondState;
632
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 // If we are generating dwarf for assembly source files save the initial text
634 // section and generate a .file directive.
635 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000636 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000637 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
638 getStreamer().EmitLabel(SectionStartSym);
639 getContext().setGenDwarfSectionStartSym(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000640 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
641 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000642 }
643
Chris Lattner73f36112009-07-02 21:53:43 +0000644 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000645 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000646 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000647 if (!parseStatement(Info))
648 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000649
Daniel Dunbar43325c42010-09-09 22:42:56 +0000650 // We had an error, validate that one was emitted and recover by skipping to
651 // the next line.
652 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000653 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000654 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000655
656 if (TheCondState.TheCond != StartingCondState.TheCond ||
657 TheCondState.Ignore != StartingCondState.Ignore)
658 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000659
660 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000661 const auto &LineTables = getContext().getMCDwarfLineTables();
662 if (!LineTables.empty()) {
663 unsigned Index = 0;
664 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
665 if (File.Name.empty() && Index != 0)
666 TokError("unassigned file number: " + Twine(Index) +
667 " for .file directives");
668 ++Index;
669 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000670 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000671
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000672 // Check to see that all assembler local symbols were actually defined.
673 // Targets that don't do subsections via symbols may not want this, though,
674 // so conservatively exclude them. Only do this if we're finalizing, though,
675 // as otherwise we won't necessarilly have seen everything yet.
676 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
677 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
678 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000679 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000680 i != e; ++i) {
681 MCSymbol *Sym = i->getValue();
682 // Variable symbols may not be marked as defined, so check those
683 // explicitly. If we know it's a variable, we have a definition for
684 // the purposes of this check.
685 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
686 // FIXME: We would really like to refer back to where the symbol was
687 // first referenced for a source location. We need to add something
688 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000689 printMessage(
690 getLexer().getLoc(), SourceMgr::DK_Error,
691 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000692 }
693 }
694
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000695 // Finalize the output stream if there are no errors and if the client wants
696 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000697 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000698 Out.Finish();
699
Chris Lattner73f36112009-07-02 21:53:43 +0000700 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000701}
702
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000703void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000704 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000705 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000706 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 }
708}
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000712 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000714
Chris Lattnere5074c42009-06-22 01:29:09 +0000715 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000716 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000718}
719
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000720StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 const char *Start = getTok().getLoc().getPointer();
722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000724 Lex();
725
726 const char *End = getTok().getLoc().getPointer();
727 return StringRef(Start, End - Start);
728}
Chris Lattner78db3622009-06-22 05:51:26 +0000729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 const char *Start = getTok().getLoc().getPointer();
732
733 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000734 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 Lex();
736
737 const char *End = getTok().getLoc().getPointer();
738 return StringRef(Start, End - Start);
739}
740
Jim Grosbach4b905842013-09-20 23:08:21 +0000741/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742/// NOTE: This assumes the leading '(' has already been consumed.
743///
744/// parenexpr ::= expr)
745///
Jim Grosbach4b905842013-09-20 23:08:21 +0000746bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
747 if (parseExpression(Res))
748 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000749 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000751 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000752 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753 return false;
754}
Chris Lattner78db3622009-06-22 05:51:26 +0000755
Jim Grosbach4b905842013-09-20 23:08:21 +0000756/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757/// NOTE: This assumes the leading '[' has already been consumed.
758///
759/// bracketexpr ::= expr]
760///
Jim Grosbach4b905842013-09-20 23:08:21 +0000761bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
762 if (parseExpression(Res))
763 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 if (Lexer.isNot(AsmToken::RBrac))
765 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000766 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000767 Lex();
768 return false;
769}
770
Jim Grosbach4b905842013-09-20 23:08:21 +0000771/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= (parenexpr
773/// primaryexpr ::= symbol
774/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000775/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000777bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000778 SMLoc FirstTokenLoc = getLexer().getLoc();
779 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
780 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000781 default:
782 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000783 // If we have an error assume that we've already handled it.
784 case AsmToken::Error:
785 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000786 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000787 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000788 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000789 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000790 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000793 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000794 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000795 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000797 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000798 if (FirstTokenKind == AsmToken::Dollar) {
799 if (Lexer.getMAI().getDollarIsPC()) {
800 // This is a '$' reference, which references the current PC. Emit a
801 // temporary label to the streamer and refer to it.
802 MCSymbol *Sym = Ctx.CreateTempSymbol();
803 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000804 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
805 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 EndLoc = FirstTokenLoc;
807 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000808 }
809 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000811 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000812 // Parse symbol variant
813 std::pair<StringRef, StringRef> Split;
814 if (!MAI.useParensForSymbolVariant()) {
815 Split = Identifier.split('@');
816 } else if (Lexer.is(AsmToken::LParen)) {
817 Lexer.Lex(); // eat (
818 StringRef VName;
819 parseIdentifier(VName);
820 if (Lexer.isNot(AsmToken::RParen)) {
821 return Error(Lexer.getTok().getLoc(),
822 "unexpected token in variant, expected ')'");
823 }
824 Lexer.Lex(); // eat )
825 Split = std::make_pair(Identifier, VName);
826 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000827
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000828 EndLoc = SMLoc::getFromPointer(Identifier.end());
829
Daniel Dunbard20cda02009-10-16 01:34:54 +0000830 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000831 StringRef SymbolName = Identifier;
832 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000833
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000834 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000835 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000836 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000837 if (Variant != MCSymbolRefExpr::VK_Invalid) {
838 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000839 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 Variant = MCSymbolRefExpr::VK_None;
841 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000842 return Error(SMLoc::getFromPointer(Split.second.begin()),
843 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000844 }
845 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000846
Hans Wennborgce69d772013-10-18 20:46:28 +0000847 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
848
Daniel Dunbard20cda02009-10-16 01:34:54 +0000849 // If this is an absolute variable reference, substitute it now to preserve
850 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000851 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000852 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000853 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000854
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000855 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000856 return false;
857 }
858
859 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000860 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000861 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000862 }
David Woodhousef42a6662014-02-01 16:20:54 +0000863 case AsmToken::BigNum:
864 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000865 case AsmToken::Integer: {
866 SMLoc Loc = getTok().getLoc();
867 int64_t IntVal = getTok().getIntVal();
868 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000869 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000870 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000871 // Look for 'b' or 'f' following an Integer as a directional label
872 if (Lexer.getKind() == AsmToken::Identifier) {
873 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000874 // Lookup the symbol variant if used.
875 std::pair<StringRef, StringRef> Split = IDVal.split('@');
876 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
877 if (Split.first.size() != IDVal.size()) {
878 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000879 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000880 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000881 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000883 if (IDVal == "f" || IDVal == "b") {
884 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000885 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000886 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000887 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000888 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000889 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 Lex(); // Eat identifier.
891 }
892 }
Chris Lattner78db3622009-06-22 05:51:26 +0000893 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000894 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000895 case AsmToken::Real: {
896 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000897 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000898 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000900 Lex(); // Eat token.
901 return false;
902 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000903 case AsmToken::Dot: {
904 // This is a '.' reference, which references the current PC. Emit a
905 // temporary label to the streamer and refer to it.
906 MCSymbol *Sym = Ctx.CreateTempSymbol();
907 Out.EmitLabel(Sym);
908 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000909 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000910 Lex(); // Eat identifier.
911 return false;
912 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000913 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000914 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000915 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000916 case AsmToken::LBrac:
917 if (!PlatformParser->HasBracketExpressions())
918 return TokError("brackets expression not supported on this target");
919 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000920 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000924 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000925 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000931 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000933 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000934 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000935 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000937 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000939 }
940}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000941
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000942bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000943 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000944 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000945}
946
Daniel Dunbar55f16672010-09-17 02:47:07 +0000947const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000948AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000949 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000950 // Ask the target implementation about this expression first.
951 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
952 if (NewE)
953 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000954 // Recurse over the given expression, rebuilding it to apply the given variant
955 // if there is exactly one symbol.
956 switch (E->getKind()) {
957 case MCExpr::Target:
958 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000959 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000960
961 case MCExpr::SymbolRef: {
962 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
963
964 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000965 TokError("invalid variant on expression '" + getTok().getIdentifier() +
966 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000967 return E;
968 }
969
970 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
971 }
972
973 case MCExpr::Unary: {
974 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000975 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000977 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000978 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
979 }
980
981 case MCExpr::Binary: {
982 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
984 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000985
986 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000987 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 if (!LHS)
990 LHS = BE->getLHS();
991 if (!RHS)
992 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993
994 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
995 }
996 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000997
Craig Toppera2886c22012-02-07 05:05:23 +0000998 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999}
1000
Jim Grosbach4b905842013-09-20 23:08:21 +00001001/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001002///
Jim Grosbachbd164242011-08-20 16:24:13 +00001003/// expr ::= expr &&,|| expr -> lowest.
1004/// expr ::= expr |,^,&,! expr
1005/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1006/// expr ::= expr <<,>> expr
1007/// expr ::= expr +,- expr
1008/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001009/// expr ::= primaryexpr
1010///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001011bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001012 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001013 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001014 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001015 return true;
1016
Daniel Dunbar55f16672010-09-17 02:47:07 +00001017 // As a special case, we support 'a op b @ modifier' by rewriting the
1018 // expression to include the modifier. This is inefficient, but in general we
1019 // expect users to use 'a@modifier op b'.
1020 if (Lexer.getKind() == AsmToken::At) {
1021 Lex();
1022
1023 if (Lexer.isNot(AsmToken::Identifier))
1024 return TokError("unexpected symbol modifier following '@'");
1025
1026 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001027 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 if (Variant == MCSymbolRefExpr::VK_Invalid)
1029 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1030
Jim Grosbach4b905842013-09-20 23:08:21 +00001031 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001032 if (!ModifiedRes) {
1033 return TokError("invalid modifier '" + getTok().getIdentifier() +
1034 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001035 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001036
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 Res = ModifiedRes;
1038 Lex();
1039 }
1040
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001041 // Try to constant fold it up front, if possible.
1042 int64_t Value;
1043 if (Res->EvaluateAsAbsolute(Value))
1044 Res = MCConstantExpr::Create(Value, getContext());
1045
1046 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001047}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001048
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001049bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001050 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001051 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001052}
1053
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001054bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001055 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001056
Daniel Dunbar75630b32009-06-30 02:10:03 +00001057 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001059 return true;
1060
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001061 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001062 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001063
1064 return false;
1065}
1066
Michael J. Spencer530ce852010-10-09 11:00:50 +00001067static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001068 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001069 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001070 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001071 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001072
Jim Grosbach4b905842013-09-20 23:08:21 +00001073 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001076 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001079 return 1;
1080
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 // Low Precedence: |, &, ^
1082 //
1083 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001093
Jim Grosbach4b905842013-09-20 23:08:21 +00001094 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 case AsmToken::EqualEqual:
1096 Kind = MCBinaryExpr::EQ;
1097 return 3;
1098 case AsmToken::ExclaimEqual:
1099 case AsmToken::LessGreater:
1100 Kind = MCBinaryExpr::NE;
1101 return 3;
1102 case AsmToken::Less:
1103 Kind = MCBinaryExpr::LT;
1104 return 3;
1105 case AsmToken::LessEqual:
1106 Kind = MCBinaryExpr::LTE;
1107 return 3;
1108 case AsmToken::Greater:
1109 Kind = MCBinaryExpr::GT;
1110 return 3;
1111 case AsmToken::GreaterEqual:
1112 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001113 return 3;
1114
Jim Grosbach4b905842013-09-20 23:08:21 +00001115 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001116 case AsmToken::LessLess:
1117 Kind = MCBinaryExpr::Shl;
1118 return 4;
1119 case AsmToken::GreaterGreater:
1120 Kind = MCBinaryExpr::Shr;
1121 return 4;
1122
Jim Grosbach4b905842013-09-20 23:08:21 +00001123 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124 case AsmToken::Plus:
1125 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001126 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127 case AsmToken::Minus:
1128 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130
Jim Grosbach4b905842013-09-20 23:08:21 +00001131 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001141 }
1142}
1143
Jim Grosbach4b905842013-09-20 23:08:21 +00001144/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001146bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001147 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001149 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001150 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001151
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 // If the next token is lower precedence than we are allowed to eat, return
1153 // successfully with what we ate already.
1154 if (TokPrec < Precedence)
1155 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001156
Sean Callanan686ed8d2010-01-19 20:22:31 +00001157 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001158
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001160 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001161 if (parsePrimaryExpr(RHS, EndLoc))
1162 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001163
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 // If BinOp binds less tightly with RHS than the operator after RHS, let
1165 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001166 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001168 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1169 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001171 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001172 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 }
1174}
1175
Chris Lattner36e02122009-06-21 20:54:55 +00001176/// ParseStatement:
1177/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001178/// ::= Label* Directive ...Operands... EndOfStatement
1179/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001180bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001181 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001182 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001183 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001184 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001185 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001187 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001188 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001189 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001193 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001194 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001197 if (Lexer.is(AsmToken::Integer)) {
1198 LocalLabelVal = getTok().getIntVal();
1199 if (LocalLabelVal < 0) {
1200 if (!TheCondState.Ignore)
1201 return TokError("unexpected token at start of statement");
1202 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001203 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 IDVal = getTok().getString();
1205 Lex(); // Consume the integer token to be used as an identifier token.
1206 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001207 if (!TheCondState.Ignore)
1208 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 }
1210 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001211 } else if (Lexer.is(AsmToken::Dot)) {
1212 // Treat '.' as a valid identifier in this context.
1213 Lex();
1214 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001215 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
1218 IDVal = "";
1219 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001220
Chris Lattner926885c2010-04-17 18:14:27 +00001221 // Handle conditional assembly here before checking for skipping. We
1222 // have to do this so that .endif isn't skipped in a ".if 0" block for
1223 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001224 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001225 DirectiveKindMap.find(IDVal);
1226 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1227 ? DK_NO_DIRECTIVE
1228 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001229 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 default:
1231 break;
1232 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001233 case DK_IFEQ:
1234 case DK_IFGE:
1235 case DK_IFGT:
1236 case DK_IFLE:
1237 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001238 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001239 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 case DK_IFB:
1241 return parseDirectiveIfb(IDLoc, true);
1242 case DK_IFNB:
1243 return parseDirectiveIfb(IDLoc, false);
1244 case DK_IFC:
1245 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001246 case DK_IFEQS:
1247 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001248 case DK_IFNC:
1249 return parseDirectiveIfc(IDLoc, false);
1250 case DK_IFDEF:
1251 return parseDirectiveIfdef(IDLoc, true);
1252 case DK_IFNDEF:
1253 case DK_IFNOTDEF:
1254 return parseDirectiveIfdef(IDLoc, false);
1255 case DK_ELSEIF:
1256 return parseDirectiveElseIf(IDLoc);
1257 case DK_ELSE:
1258 return parseDirectiveElse(IDLoc);
1259 case DK_ENDIF:
1260 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001261 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001262
Eli Bendersky88024712013-01-16 19:32:36 +00001263 // Ignore the statement if in the middle of inactive conditional
1264 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001265 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001266 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001267 return false;
1268 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001269
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001270 // FIXME: Recurse on local labels?
1271
1272 // See what kind of statement we have.
1273 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001274 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001275 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001276
Chris Lattner36e02122009-06-21 20:54:55 +00001277 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001278 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001279
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001280 // Diagnose attempt to use '.' as a label.
1281 if (IDVal == ".")
1282 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1283
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001284 // Diagnose attempt to use a variable as a label.
1285 //
1286 // FIXME: Diagnostics. Note the location of the definition as a label.
1287 // FIXME: This doesn't diagnose assignment to a symbol which has been
1288 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001289 MCSymbol *Sym;
1290 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001291 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001292 else
1293 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001294 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001295 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001296
Daniel Dunbare73b2672009-08-26 22:13:22 +00001297 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001298 if (!ParsingInlineAsm)
1299 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001300
Kevin Enderbye7739d42011-12-09 18:09:40 +00001301 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001302 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001303 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001304 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1305 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001306
Tim Northover1744d0a2013-10-25 12:49:50 +00001307 getTargetParser().onLabelParsed(Sym);
1308
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001309 // Consume any end of statement token, if present, to avoid spurious
1310 // AddBlankLine calls().
1311 if (Lexer.is(AsmToken::EndOfStatement)) {
1312 Lex();
1313 if (Lexer.is(AsmToken::Eof))
1314 return false;
1315 }
1316
Eli Friedman0f4871d2012-10-22 23:58:19 +00001317 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001318 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001319
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001320 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001321 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001322 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001323
Jim Grosbach4b905842013-09-20 23:08:21 +00001324 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001325
1326 default: // Normal instruction or directive.
1327 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001328 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001329
1330 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001331 if (areMacrosEnabled())
1332 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1333 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001334 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001335
Michael J. Spencer530ce852010-10-09 11:00:50 +00001336 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001337
Eli Bendersky17233942013-01-15 22:59:42 +00001338 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001339 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001340 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001341 //
Eli Bendersky17233942013-01-15 22:59:42 +00001342 // 1. The target-specific assembly parser. Some directives are target
1343 // specific or may potentially behave differently on certain targets.
1344 // 2. Asm parser extensions. For example, platform-specific parsers
1345 // (like the ELF parser) register themselves as extensions.
1346 // 3. The generic directive parser implemented by this class. These are
1347 // all the directives that behave in a target and platform independent
1348 // manner, or at least have a default behavior that's shared between
1349 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001350
Eli Bendersky17233942013-01-15 22:59:42 +00001351 // First query the target-specific parser. It will return 'true' if it
1352 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001353 if (!getTargetParser().ParseDirective(ID))
1354 return false;
1355
Alp Tokercb402912014-01-24 17:20:08 +00001356 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001357 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001358 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1359 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001360 if (Handler.first)
1361 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1362
1363 // Finally, if no one else is interested in this directive, it must be
1364 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001365 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001366 default:
1367 break;
1368 case DK_SET:
1369 case DK_EQU:
1370 return parseDirectiveSet(IDVal, true);
1371 case DK_EQUIV:
1372 return parseDirectiveSet(IDVal, false);
1373 case DK_ASCII:
1374 return parseDirectiveAscii(IDVal, false);
1375 case DK_ASCIZ:
1376 case DK_STRING:
1377 return parseDirectiveAscii(IDVal, true);
1378 case DK_BYTE:
1379 return parseDirectiveValue(1);
1380 case DK_SHORT:
1381 case DK_VALUE:
1382 case DK_2BYTE:
1383 return parseDirectiveValue(2);
1384 case DK_LONG:
1385 case DK_INT:
1386 case DK_4BYTE:
1387 return parseDirectiveValue(4);
1388 case DK_QUAD:
1389 case DK_8BYTE:
1390 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001391 case DK_OCTA:
1392 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001393 case DK_SINGLE:
1394 case DK_FLOAT:
1395 return parseDirectiveRealValue(APFloat::IEEEsingle);
1396 case DK_DOUBLE:
1397 return parseDirectiveRealValue(APFloat::IEEEdouble);
1398 case DK_ALIGN: {
1399 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1400 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1401 }
1402 case DK_ALIGN32: {
1403 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1404 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1405 }
1406 case DK_BALIGN:
1407 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1408 case DK_BALIGNW:
1409 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1410 case DK_BALIGNL:
1411 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1412 case DK_P2ALIGN:
1413 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1414 case DK_P2ALIGNW:
1415 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1416 case DK_P2ALIGNL:
1417 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1418 case DK_ORG:
1419 return parseDirectiveOrg();
1420 case DK_FILL:
1421 return parseDirectiveFill();
1422 case DK_ZERO:
1423 return parseDirectiveZero();
1424 case DK_EXTERN:
1425 eatToEndOfStatement(); // .extern is the default, ignore it.
1426 return false;
1427 case DK_GLOBL:
1428 case DK_GLOBAL:
1429 return parseDirectiveSymbolAttribute(MCSA_Global);
1430 case DK_LAZY_REFERENCE:
1431 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1432 case DK_NO_DEAD_STRIP:
1433 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1434 case DK_SYMBOL_RESOLVER:
1435 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1436 case DK_PRIVATE_EXTERN:
1437 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1438 case DK_REFERENCE:
1439 return parseDirectiveSymbolAttribute(MCSA_Reference);
1440 case DK_WEAK_DEFINITION:
1441 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1442 case DK_WEAK_REFERENCE:
1443 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1444 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1445 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1446 case DK_COMM:
1447 case DK_COMMON:
1448 return parseDirectiveComm(/*IsLocal=*/false);
1449 case DK_LCOMM:
1450 return parseDirectiveComm(/*IsLocal=*/true);
1451 case DK_ABORT:
1452 return parseDirectiveAbort();
1453 case DK_INCLUDE:
1454 return parseDirectiveInclude();
1455 case DK_INCBIN:
1456 return parseDirectiveIncbin();
1457 case DK_CODE16:
1458 case DK_CODE16GCC:
1459 return TokError(Twine(IDVal) + " not supported yet");
1460 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001461 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001462 case DK_IRP:
1463 return parseDirectiveIrp(IDLoc);
1464 case DK_IRPC:
1465 return parseDirectiveIrpc(IDLoc);
1466 case DK_ENDR:
1467 return parseDirectiveEndr(IDLoc);
1468 case DK_BUNDLE_ALIGN_MODE:
1469 return parseDirectiveBundleAlignMode();
1470 case DK_BUNDLE_LOCK:
1471 return parseDirectiveBundleLock();
1472 case DK_BUNDLE_UNLOCK:
1473 return parseDirectiveBundleUnlock();
1474 case DK_SLEB128:
1475 return parseDirectiveLEB128(true);
1476 case DK_ULEB128:
1477 return parseDirectiveLEB128(false);
1478 case DK_SPACE:
1479 case DK_SKIP:
1480 return parseDirectiveSpace(IDVal);
1481 case DK_FILE:
1482 return parseDirectiveFile(IDLoc);
1483 case DK_LINE:
1484 return parseDirectiveLine();
1485 case DK_LOC:
1486 return parseDirectiveLoc();
1487 case DK_STABS:
1488 return parseDirectiveStabs();
1489 case DK_CFI_SECTIONS:
1490 return parseDirectiveCFISections();
1491 case DK_CFI_STARTPROC:
1492 return parseDirectiveCFIStartProc();
1493 case DK_CFI_ENDPROC:
1494 return parseDirectiveCFIEndProc();
1495 case DK_CFI_DEF_CFA:
1496 return parseDirectiveCFIDefCfa(IDLoc);
1497 case DK_CFI_DEF_CFA_OFFSET:
1498 return parseDirectiveCFIDefCfaOffset();
1499 case DK_CFI_ADJUST_CFA_OFFSET:
1500 return parseDirectiveCFIAdjustCfaOffset();
1501 case DK_CFI_DEF_CFA_REGISTER:
1502 return parseDirectiveCFIDefCfaRegister(IDLoc);
1503 case DK_CFI_OFFSET:
1504 return parseDirectiveCFIOffset(IDLoc);
1505 case DK_CFI_REL_OFFSET:
1506 return parseDirectiveCFIRelOffset(IDLoc);
1507 case DK_CFI_PERSONALITY:
1508 return parseDirectiveCFIPersonalityOrLsda(true);
1509 case DK_CFI_LSDA:
1510 return parseDirectiveCFIPersonalityOrLsda(false);
1511 case DK_CFI_REMEMBER_STATE:
1512 return parseDirectiveCFIRememberState();
1513 case DK_CFI_RESTORE_STATE:
1514 return parseDirectiveCFIRestoreState();
1515 case DK_CFI_SAME_VALUE:
1516 return parseDirectiveCFISameValue(IDLoc);
1517 case DK_CFI_RESTORE:
1518 return parseDirectiveCFIRestore(IDLoc);
1519 case DK_CFI_ESCAPE:
1520 return parseDirectiveCFIEscape();
1521 case DK_CFI_SIGNAL_FRAME:
1522 return parseDirectiveCFISignalFrame();
1523 case DK_CFI_UNDEFINED:
1524 return parseDirectiveCFIUndefined(IDLoc);
1525 case DK_CFI_REGISTER:
1526 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001527 case DK_CFI_WINDOW_SAVE:
1528 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001529 case DK_MACROS_ON:
1530 case DK_MACROS_OFF:
1531 return parseDirectiveMacrosOnOff(IDVal);
1532 case DK_MACRO:
1533 return parseDirectiveMacro(IDLoc);
1534 case DK_ENDM:
1535 case DK_ENDMACRO:
1536 return parseDirectiveEndMacro(IDVal);
1537 case DK_PURGEM:
1538 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001539 case DK_END:
1540 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001541 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001542 return parseDirectiveError(IDLoc, false);
1543 case DK_ERROR:
1544 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001545 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001546
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001547 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001548 }
Chris Lattner36e02122009-06-21 20:54:55 +00001549
Chad Rosierc7f552c2013-02-12 21:33:51 +00001550 // __asm _emit or __asm __emit
1551 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1552 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001553 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001554
1555 // __asm align
1556 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001557 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001558
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001559 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001560
Chris Lattner7cbfa442010-05-19 23:34:33 +00001561 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001562 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001563 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001564 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001565 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001566 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001567
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001568 // Dump the parsed representation, if requested.
1569 if (getShowParsedOperands()) {
1570 SmallString<256> Str;
1571 raw_svector_ostream OS(Str);
1572 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001573 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001574 if (i != 0)
1575 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001576 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001577 }
1578 OS << "]";
1579
Jim Grosbach4b905842013-09-20 23:08:21 +00001580 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001581 }
1582
Kevin Enderby6469fc22011-11-01 22:27:22 +00001583 // If we are generating dwarf for assembly source files and the current
1584 // section is the initial text section then generate a .loc directive for
1585 // the instruction.
1586 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001587 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001588 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001589
Eli Bendersky88024712013-01-16 19:32:36 +00001590 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001591
Eli Bendersky88024712013-01-16 19:32:36 +00001592 // If we previously parsed a cpp hash file line comment then make sure the
1593 // current Dwarf File is for the CppHashFilename if not then emit the
1594 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001595 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001596 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1597 0, StringRef(), CppHashFilename);
1598 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001599
Jim Grosbach4b905842013-09-20 23:08:21 +00001600 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1601 // cache with the different Loc from the call above we save the last
1602 // info we queried here with SrcMgr.FindLineNumber().
1603 unsigned CppHashLocLineNo;
1604 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1605 CppHashLocLineNo = LastQueryLine;
1606 else {
1607 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1608 LastQueryLine = CppHashLocLineNo;
1609 LastQueryIDLoc = CppHashLoc;
1610 LastQueryBuffer = CppHashBuf;
1611 }
1612 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001613 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001614
Jim Grosbach4b905842013-09-20 23:08:21 +00001615 getStreamer().EmitDwarfLocDirective(
1616 getContext().getGenDwarfFileNumber(), Line, 0,
1617 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1618 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001619 }
1620
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001621 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001622 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001623 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001624 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1625 Info.ParsedOperands, Out,
1626 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001627 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001628
Chris Lattnera2a9d162010-09-11 16:18:25 +00001629 // Don't skip the rest of the line, the instruction parser is responsible for
1630 // that.
1631 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001632}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001633
Jim Grosbach4b905842013-09-20 23:08:21 +00001634/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001635/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001636void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001637 if (!Lexer.is(AsmToken::EndOfStatement))
1638 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001639 // Eat EOL.
1640 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001641}
1642
Jim Grosbach4b905842013-09-20 23:08:21 +00001643/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001644/// ::= # number "filename"
1645/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001646bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001647 Lex(); // Eat the hash token.
1648
1649 if (getLexer().isNot(AsmToken::Integer)) {
1650 // Consume the line since in cases it is not a well-formed line directive,
1651 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001652 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001653 return false;
1654 }
1655
1656 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001657 Lex();
1658
1659 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001660 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001661 return false;
1662 }
1663
1664 StringRef Filename = getTok().getString();
1665 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001666 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001667
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001668 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1669 CppHashLoc = L;
1670 CppHashFilename = Filename;
1671 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001672 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001673
1674 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001675 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001676 return false;
1677}
1678
Jim Grosbach4b905842013-09-20 23:08:21 +00001679/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001680/// for the Filename and LineNo if any in the diagnostic.
1681void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001682 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001683 raw_ostream &OS = errs();
1684
1685 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1686 const SMLoc &DiagLoc = Diag.getLoc();
1687 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1688 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1689
Jim Grosbach4b905842013-09-20 23:08:21 +00001690 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691 // before printing the message.
1692 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001693 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1695 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 }
1697
Eric Christophera7c32732012-12-18 00:30:54 +00001698 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699 // manager changed or buffer changed (like in a nested include) then just
1700 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001703 if (Parser->SavedDiagHandler)
1704 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1705 else
Craig Topper353eda42014-04-24 06:44:33 +00001706 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707 return;
1708 }
1709
Eric Christophera7c32732012-12-18 00:30:54 +00001710 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1712 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001713 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714
1715 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1716 int CppHashLocLineNo =
1717 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001718 int LineNo =
1719 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720
Jim Grosbach4b905842013-09-20 23:08:21 +00001721 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1722 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001723 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001725 if (Parser->SavedDiagHandler)
1726 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1727 else
Craig Topper353eda42014-04-24 06:44:33 +00001728 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001729}
1730
Rafael Espindola2c064482012-08-21 18:29:30 +00001731// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1732// difference being that that function accepts '@' as part of identifiers and
1733// we can't do that. AsmLexer.cpp should probably be changed to handle
1734// '@' as a special case when needed.
1735static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001736 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1737 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001738}
1739
Rafael Espindola34b9c512012-06-03 23:57:14 +00001740bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001741 ArrayRef<MCAsmMacroParameter> Parameters,
1742 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001743 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001744 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001745 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001746 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001747
Preston Gurd05500642012-09-19 20:36:12 +00001748 // A macro without parameters is handled differently on Darwin:
1749 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001750 while (!Body.empty()) {
1751 // Scan for the next substitution.
1752 std::size_t End = Body.size(), Pos = 0;
1753 for (; Pos != End; ++Pos) {
1754 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001755 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 // This macro has no parameters, look for $0, $1, etc.
1757 if (Body[Pos] != '$' || Pos + 1 == End)
1758 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001759
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001761 if (Next == '$' || Next == 'n' ||
1762 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 break;
1764 } else {
1765 // This macro has parameters, look for \foo, \bar, etc.
1766 if (Body[Pos] == '\\' && Pos + 1 != End)
1767 break;
1768 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001769 }
1770
1771 // Add the prefix.
1772 OS << Body.slice(0, Pos);
1773
1774 // Check if we reached the end.
1775 if (Pos == End)
1776 break;
1777
Benjamin Kramer513e7442014-02-20 13:36:32 +00001778 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001779 switch (Body[Pos + 1]) {
1780 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 case '$':
1782 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001783 break;
1784
Jim Grosbach4b905842013-09-20 23:08:21 +00001785 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001786 case 'n':
1787 OS << A.size();
1788 break;
1789
Jim Grosbach4b905842013-09-20 23:08:21 +00001790 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001791 default: {
1792 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001793 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 if (Index >= A.size())
1795 break;
1796
1797 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001798 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001799 ie = A[Index].end();
1800 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001801 OS << it->getString();
1802 break;
1803 }
1804 }
1805 Pos += 2;
1806 } else {
1807 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001808 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 ++I;
1810
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 const char *Begin = Body.data() + Pos + 1;
1812 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001813 unsigned Index = 0;
1814 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001815 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816 break;
1817
Preston Gurd05500642012-09-19 20:36:12 +00001818 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001819 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1820 Pos += 3;
1821 else {
1822 OS << '\\' << Argument;
1823 Pos = I;
1824 }
Preston Gurd05500642012-09-19 20:36:12 +00001825 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001826 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001827 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001828 ie = A[Index].end();
1829 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001830 // We expect no quotes around the string's contents when
1831 // parsing for varargs.
1832 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001833 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001834 else
1835 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001836
Preston Gurd05500642012-09-19 20:36:12 +00001837 Pos += 1 + Argument.size();
1838 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001839 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001840 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001841 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001842 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001843
Rafael Espindola1134ab232011-06-05 02:43:45 +00001844 return false;
1845}
Daniel Dunbar43235712010-07-18 18:54:11 +00001846
Jim Grosbach4b905842013-09-20 23:08:21 +00001847MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1848 SMLoc EL, MemoryBuffer *I)
1849 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1850 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001851
Jim Grosbach4b905842013-09-20 23:08:21 +00001852static bool isOperator(AsmToken::TokenKind kind) {
1853 switch (kind) {
1854 default:
1855 return false;
1856 case AsmToken::Plus:
1857 case AsmToken::Minus:
1858 case AsmToken::Tilde:
1859 case AsmToken::Slash:
1860 case AsmToken::Star:
1861 case AsmToken::Dot:
1862 case AsmToken::Equal:
1863 case AsmToken::EqualEqual:
1864 case AsmToken::Pipe:
1865 case AsmToken::PipePipe:
1866 case AsmToken::Caret:
1867 case AsmToken::Amp:
1868 case AsmToken::AmpAmp:
1869 case AsmToken::Exclaim:
1870 case AsmToken::ExclaimEqual:
1871 case AsmToken::Percent:
1872 case AsmToken::Less:
1873 case AsmToken::LessEqual:
1874 case AsmToken::LessLess:
1875 case AsmToken::LessGreater:
1876 case AsmToken::Greater:
1877 case AsmToken::GreaterEqual:
1878 case AsmToken::GreaterGreater:
1879 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001880 }
1881}
1882
David Majnemer16252452014-01-29 00:07:39 +00001883namespace {
1884class AsmLexerSkipSpaceRAII {
1885public:
1886 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1887 Lexer.setSkipSpace(SkipSpace);
1888 }
1889
1890 ~AsmLexerSkipSpaceRAII() {
1891 Lexer.setSkipSpace(true);
1892 }
1893
1894private:
1895 AsmLexer &Lexer;
1896};
1897}
1898
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001899bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1900
1901 if (Vararg) {
1902 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1903 StringRef Str = parseStringToEndOfStatement();
1904 MA.push_back(AsmToken(AsmToken::String, Str));
1905 }
1906 return false;
1907 }
1908
Rafael Espindola768b41c2012-06-15 14:02:34 +00001909 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001910 unsigned AddTokens = 0;
1911
David Majnemer16252452014-01-29 00:07:39 +00001912 // Darwin doesn't use spaces to delmit arguments.
1913 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001914
1915 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001916 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001917 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001918
David Majnemer91fc4c22014-01-29 18:57:46 +00001919 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001920 break;
Preston Gurd05500642012-09-19 20:36:12 +00001921
1922 if (Lexer.is(AsmToken::Space)) {
1923 Lex(); // Eat spaces
1924
1925 // Spaces can delimit parameters, but could also be part an expression.
1926 // If the token after a space is an operator, add the token and the next
1927 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001928 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001929 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001930 // Check to see whether the token is used as an operator,
1931 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001932 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001933 if (*NextChar == ' ')
1934 AddTokens = 2;
1935 }
1936
1937 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001938 break;
1939 }
1940 }
1941 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001942
Jim Grosbach4b905842013-09-20 23:08:21 +00001943 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 // to be able to fill in the remaining default parameter values
1945 if (Lexer.is(AsmToken::EndOfStatement))
1946 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001947
1948 // Adjust the current parentheses level.
1949 if (Lexer.is(AsmToken::LParen))
1950 ++ParenLevel;
1951 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1952 --ParenLevel;
1953
1954 // Append the token to the current argument list.
1955 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001956 if (AddTokens)
1957 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958 Lex();
1959 }
Preston Gurd05500642012-09-19 20:36:12 +00001960
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001962 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963 return false;
1964}
1965
1966// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001967bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001968 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001969 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001970 bool NamedParametersFound = false;
1971 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001972
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001973 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001974 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001975
Rafael Espindola768b41c2012-06-15 14:02:34 +00001976 // Parse two kinds of macro invocations:
1977 // - macros defined without any parameters accept an arbitrary number of them
1978 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001979 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1981 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001982 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001983 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001985 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001986 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001987 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001988 eatToEndOfStatement();
1989 return true;
1990 }
1991
1992 if (!Lexer.is(AsmToken::Equal)) {
1993 TokError("expected '=' after formal parameter identifier");
1994 eatToEndOfStatement();
1995 return true;
1996 }
1997 Lex();
1998
1999 NamedParametersFound = true;
2000 }
2001
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002002 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002003 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002004 eatToEndOfStatement();
2005 return true;
2006 }
2007
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002008 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2009 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002010 return true;
2011
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002012 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002013 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002014 unsigned FAI = 0;
2015 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002016 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002017 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002018
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002019 if (FAI >= NParameters) {
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002020 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002021 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002022 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002023 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002024 return true;
2025 }
2026 PI = FAI;
2027 }
2028
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002029 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 if (A.size() <= PI)
2031 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002032 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002033
2034 if (FALocs.size() <= PI)
2035 FALocs.resize(PI + 1);
2036
2037 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002038 }
Jim Grosbach206661622012-07-30 22:44:17 +00002039
Preston Gurd242ed3152012-09-19 20:29:04 +00002040 // At the end of the statement, fill in remaining arguments that have
2041 // default values. If there aren't any, then the next argument is
2042 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002043 if (Lexer.is(AsmToken::EndOfStatement)) {
2044 bool Failure = false;
2045 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2046 if (A[FAI].empty()) {
2047 if (M->Parameters[FAI].Required) {
2048 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2049 "missing value for required parameter "
2050 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2051 Failure = true;
2052 }
2053
2054 if (!M->Parameters[FAI].Value.empty())
2055 A[FAI] = M->Parameters[FAI].Value;
2056 }
2057 }
2058 return Failure;
2059 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002060
2061 if (Lexer.is(AsmToken::Comma))
2062 Lex();
2063 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002064
2065 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002066}
2067
Jim Grosbach4b905842013-09-20 23:08:21 +00002068const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2069 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002070 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002071}
2072
Jim Grosbach4b905842013-09-20 23:08:21 +00002073void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002074 MacroMap[Name] = new MCAsmMacro(Macro);
2075}
2076
Jim Grosbach4b905842013-09-20 23:08:21 +00002077void AsmParser::undefineMacro(StringRef Name) {
2078 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002079 if (I != MacroMap.end()) {
2080 delete I->getValue();
2081 MacroMap.erase(I);
2082 }
2083}
2084
Jim Grosbach4b905842013-09-20 23:08:21 +00002085bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002086 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2087 // this, although we should protect against infinite loops.
2088 if (ActiveMacros.size() == 20)
2089 return TokError("macros cannot be nested more than 20 levels deep");
2090
Eli Bendersky38274122013-01-14 23:22:36 +00002091 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002092 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002093 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002094
Rafael Espindola1134ab232011-06-05 02:43:45 +00002095 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2096 // to hold the macro body with substitutions.
2097 SmallString<256> Buf;
2098 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002099 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002100
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002101 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002102 return true;
2103
Eli Bendersky38274122013-01-14 23:22:36 +00002104 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002105 // instantiation.
2106 OS << ".endmacro\n";
2107
Rafael Espindola1134ab232011-06-05 02:43:45 +00002108 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002109 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002110
Daniel Dunbar43235712010-07-18 18:54:11 +00002111 // Create the macro instantiation object and add to the current macro
2112 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002113 MacroInstantiation *MI = new MacroInstantiation(
2114 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002115 ActiveMacros.push_back(MI);
2116
2117 // Jump to the macro instantiation and prime the lexer.
2118 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2119 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2120 Lex();
2121
2122 return false;
2123}
2124
Jim Grosbach4b905842013-09-20 23:08:21 +00002125void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002126 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002127 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 Lex();
2129
2130 // Pop the instantiation entry.
2131 delete ActiveMacros.back();
2132 ActiveMacros.pop_back();
2133}
2134
Jim Grosbach4b905842013-09-20 23:08:21 +00002135static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002136 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002137 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002138 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2139 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002140 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002141 case MCExpr::Target:
2142 case MCExpr::Constant:
2143 return false;
2144 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002145 const MCSymbol &S =
2146 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002147 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002148 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002149 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002150 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002151 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002152 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002153 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002154
2155 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002156}
2157
Jim Grosbach4b905842013-09-20 23:08:21 +00002158bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002159 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002160 // FIXME: Use better location, we should use proper tokens.
2161 SMLoc EqualLoc = Lexer.getLoc();
2162
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002163 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002164 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002165 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002166
Rafael Espindola72f5f172012-01-28 05:57:00 +00002167 // Note: we don't count b as used in "a = b". This is to allow
2168 // a = b
2169 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002170
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002171 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002172 return TokError("unexpected token in assignment");
2173
2174 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002175 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002176
Daniel Dunbar5f339242009-10-16 01:57:39 +00002177 // Validate that the LHS is allowed to be a variable (either it has not been
2178 // used as a symbol, or it is an absolute symbol).
2179 MCSymbol *Sym = getContext().LookupSymbol(Name);
2180 if (Sym) {
2181 // Diagnose assignment to a label.
2182 //
2183 // FIXME: Diagnostics. Note the location of the definition as a label.
2184 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002185 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002186 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2187 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002188 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002189 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2190 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002191 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002192 return Error(EqualLoc, "redefinition of '" + Name + "'");
2193 else if (!Sym->isVariable())
2194 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002195 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002196 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002197 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002198
2199 // Don't count these checks as uses.
2200 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002201 } else if (Name == ".") {
2202 if (Out.EmitValueToOffset(Value, 0)) {
2203 Error(EqualLoc, "expected absolute expression");
2204 eatToEndOfStatement();
2205 }
2206 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002207 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002208 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002209
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002210 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002211 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002212 if (NoDeadStrip)
2213 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2214
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002215 return false;
2216}
2217
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002218/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002219/// ::= identifier
2220/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002221bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002222 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002223 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2224 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002225 // handle this as a context dependent token, instead we detect adjacent tokens
2226 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002227 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2228 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002229
Hans Wennborgce69d772013-10-18 20:46:28 +00002230 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002231 Lex();
2232 if (Lexer.isNot(AsmToken::Identifier))
2233 return true;
2234
Hans Wennborgce69d772013-10-18 20:46:28 +00002235 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2236 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002237 return true;
2238
2239 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002240 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002241 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242 Lex();
2243 return false;
2244 }
2245
Jim Grosbach4b905842013-09-20 23:08:21 +00002246 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002247 return true;
2248
Sean Callanan936b0d32010-01-19 21:44:56 +00002249 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002250
Sean Callanan686ed8d2010-01-19 20:22:31 +00002251 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002252
2253 return false;
2254}
2255
Jim Grosbach4b905842013-09-20 23:08:21 +00002256/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002257/// ::= .equ identifier ',' expression
2258/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002259/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002260bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002261 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002262
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002263 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002264 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002265
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002266 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002267 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002268 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002269
Jim Grosbach4b905842013-09-20 23:08:21 +00002270 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002271}
2272
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002273bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002274 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002275
2276 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002277 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002278 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2279 if (Str[i] != '\\') {
2280 Data += Str[i];
2281 continue;
2282 }
2283
2284 // Recognize escaped characters. Note that this escape semantics currently
2285 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2286 ++i;
2287 if (i == e)
2288 return TokError("unexpected backslash at end of string");
2289
2290 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002291 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002292 // Consume up to three octal characters.
2293 unsigned Value = Str[i] - '0';
2294
Jim Grosbach4b905842013-09-20 23:08:21 +00002295 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002296 ++i;
2297 Value = Value * 8 + (Str[i] - '0');
2298
Jim Grosbach4b905842013-09-20 23:08:21 +00002299 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002300 ++i;
2301 Value = Value * 8 + (Str[i] - '0');
2302 }
2303 }
2304
2305 if (Value > 255)
2306 return TokError("invalid octal escape sequence (out of range)");
2307
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 continue;
2310 }
2311
2312 // Otherwise recognize individual escapes.
2313 switch (Str[i]) {
2314 default:
2315 // Just reject invalid escape sequences for now.
2316 return TokError("invalid escape sequence (unrecognized character)");
2317
2318 case 'b': Data += '\b'; break;
2319 case 'f': Data += '\f'; break;
2320 case 'n': Data += '\n'; break;
2321 case 'r': Data += '\r'; break;
2322 case 't': Data += '\t'; break;
2323 case '"': Data += '"'; break;
2324 case '\\': Data += '\\'; break;
2325 }
2326 }
2327
2328 return false;
2329}
2330
Jim Grosbach4b905842013-09-20 23:08:21 +00002331/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002332/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002333bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002334 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002335 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002336
Daniel Dunbara10e5192009-06-24 23:30:00 +00002337 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002338 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002339 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002340
Daniel Dunbaref668c12009-08-14 18:19:52 +00002341 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002342 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002343 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002344
Rafael Espindola64e1af82013-07-02 15:49:13 +00002345 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002346 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002347 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002348
Sean Callanan686ed8d2010-01-19 20:22:31 +00002349 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002350
2351 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002352 break;
2353
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002354 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002355 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002356 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002357 }
2358 }
2359
Sean Callanan686ed8d2010-01-19 20:22:31 +00002360 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002361 return false;
2362}
2363
Jim Grosbach4b905842013-09-20 23:08:21 +00002364/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002366bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002368 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002369
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002371 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002372 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002373 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 return true;
2375
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002376 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002377 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2378 assert(Size <= 8 && "Invalid size");
2379 uint64_t IntValue = MCE->getValue();
2380 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2381 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002382 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002383 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002384 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002385
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002386 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002388
Daniel Dunbara10e5192009-06-24 23:30:00 +00002389 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002390 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002391 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002392 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002393 }
2394 }
2395
Sean Callanan686ed8d2010-01-19 20:22:31 +00002396 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002397 return false;
2398}
2399
David Woodhoused6de0d92014-02-01 16:20:59 +00002400/// ParseDirectiveOctaValue
2401/// ::= .octa [ hexconstant (, hexconstant)* ]
2402bool AsmParser::parseDirectiveOctaValue() {
2403 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2404 checkForValidSection();
2405
2406 for (;;) {
2407 if (Lexer.getKind() == AsmToken::Error)
2408 return true;
2409 if (Lexer.getKind() != AsmToken::Integer &&
2410 Lexer.getKind() != AsmToken::BigNum)
2411 return TokError("unknown token in expression");
2412
2413 SMLoc ExprLoc = getLexer().getLoc();
2414 APInt IntValue = getTok().getAPIntVal();
2415 Lex();
2416
2417 uint64_t hi, lo;
2418 if (IntValue.isIntN(64)) {
2419 hi = 0;
2420 lo = IntValue.getZExtValue();
2421 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002422 // It might actually have more than 128 bits, but the top ones are zero.
2423 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002424 lo = IntValue.getLoBits(64).getZExtValue();
2425 } else
2426 return Error(ExprLoc, "literal value out of range for directive");
2427
2428 if (MAI.isLittleEndian()) {
2429 getStreamer().EmitIntValue(lo, 8);
2430 getStreamer().EmitIntValue(hi, 8);
2431 } else {
2432 getStreamer().EmitIntValue(hi, 8);
2433 getStreamer().EmitIntValue(lo, 8);
2434 }
2435
2436 if (getLexer().is(AsmToken::EndOfStatement))
2437 break;
2438
2439 // FIXME: Improve diagnostic.
2440 if (getLexer().isNot(AsmToken::Comma))
2441 return TokError("unexpected token in directive");
2442 Lex();
2443 }
2444 }
2445
2446 Lex();
2447 return false;
2448}
2449
Jim Grosbach4b905842013-09-20 23:08:21 +00002450/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002451/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002452bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002453 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002454 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002455
2456 for (;;) {
2457 // We don't truly support arithmetic on floating point expressions, so we
2458 // have to manually parse unary prefixes.
2459 bool IsNeg = false;
2460 if (getLexer().is(AsmToken::Minus)) {
2461 Lex();
2462 IsNeg = true;
2463 } else if (getLexer().is(AsmToken::Plus))
2464 Lex();
2465
Michael J. Spencer530ce852010-10-09 11:00:50 +00002466 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002467 getLexer().isNot(AsmToken::Real) &&
2468 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002469 return TokError("unexpected token in directive");
2470
2471 // Convert to an APFloat.
2472 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002473 StringRef IDVal = getTok().getString();
2474 if (getLexer().is(AsmToken::Identifier)) {
2475 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2476 Value = APFloat::getInf(Semantics);
2477 else if (!IDVal.compare_lower("nan"))
2478 Value = APFloat::getNaN(Semantics, false, ~0);
2479 else
2480 return TokError("invalid floating point literal");
2481 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002482 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002483 return TokError("invalid floating point literal");
2484 if (IsNeg)
2485 Value.changeSign();
2486
2487 // Consume the numeric token.
2488 Lex();
2489
2490 // Emit the value as an integer.
2491 APInt AsInt = Value.bitcastToAPInt();
2492 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002493 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002494
2495 if (getLexer().is(AsmToken::EndOfStatement))
2496 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002497
Daniel Dunbar2af16532010-09-24 01:59:56 +00002498 if (getLexer().isNot(AsmToken::Comma))
2499 return TokError("unexpected token in directive");
2500 Lex();
2501 }
2502 }
2503
2504 Lex();
2505 return false;
2506}
2507
Jim Grosbach4b905842013-09-20 23:08:21 +00002508/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002509/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002510bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002511 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002512
2513 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002514 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002515 return true;
2516
Rafael Espindolab91bac62010-10-05 19:42:57 +00002517 int64_t Val = 0;
2518 if (getLexer().is(AsmToken::Comma)) {
2519 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002520 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002521 return true;
2522 }
2523
Rafael Espindola922e3f42010-09-16 15:03:59 +00002524 if (getLexer().isNot(AsmToken::EndOfStatement))
2525 return TokError("unexpected token in '.zero' directive");
2526
2527 Lex();
2528
Rafael Espindola64e1af82013-07-02 15:49:13 +00002529 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002530
2531 return false;
2532}
2533
Jim Grosbach4b905842013-09-20 23:08:21 +00002534/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002535/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002536bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002537 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002538
David Majnemer522d3db2014-02-01 07:19:38 +00002539 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002540 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002541 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002542 return true;
2543
David Majnemer522d3db2014-02-01 07:19:38 +00002544 if (NumValues < 0) {
2545 Warning(RepeatLoc,
2546 "'.fill' directive with negative repeat count has no effect");
2547 NumValues = 0;
2548 }
2549
Roman Divackye33098f2013-09-24 17:44:41 +00002550 int64_t FillSize = 1;
2551 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002552
David Majnemer522d3db2014-02-01 07:19:38 +00002553 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002554 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2555 if (getLexer().isNot(AsmToken::Comma))
2556 return TokError("unexpected token in '.fill' directive");
2557 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002558
David Majnemer522d3db2014-02-01 07:19:38 +00002559 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002560 if (parseAbsoluteExpression(FillSize))
2561 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002562
Roman Divackye33098f2013-09-24 17:44:41 +00002563 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2564 if (getLexer().isNot(AsmToken::Comma))
2565 return TokError("unexpected token in '.fill' directive");
2566 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002567
David Majnemer522d3db2014-02-01 07:19:38 +00002568 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002569 if (parseAbsoluteExpression(FillExpr))
2570 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002571
Roman Divackye33098f2013-09-24 17:44:41 +00002572 if (getLexer().isNot(AsmToken::EndOfStatement))
2573 return TokError("unexpected token in '.fill' directive");
2574
2575 Lex();
2576 }
2577 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002578
David Majnemer522d3db2014-02-01 07:19:38 +00002579 if (FillSize < 0) {
2580 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2581 NumValues = 0;
2582 }
2583 if (FillSize > 8) {
2584 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2585 FillSize = 8;
2586 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002587
David Majnemer522d3db2014-02-01 07:19:38 +00002588 if (!isUInt<32>(FillExpr) && FillSize > 4)
2589 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2590
2591 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2592 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2593
2594 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2595 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2596 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2597 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002598
2599 return false;
2600}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002601
Jim Grosbach4b905842013-09-20 23:08:21 +00002602/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002603/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002604bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002605 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002606
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002607 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002608 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002609 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002610 return true;
2611
2612 // Parse optional fill expression.
2613 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002614 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2615 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002616 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002617 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002618
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002619 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002620 return true;
2621
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002622 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002623 return TokError("unexpected token in '.org' directive");
2624 }
2625
Sean Callanan686ed8d2010-01-19 20:22:31 +00002626 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002627
Jim Grosbachb5912772012-01-27 00:37:08 +00002628 // Only limited forms of relocatable expressions are accepted here, it
2629 // has to be relative to the current section. The streamer will return
2630 // 'true' if the expression wasn't evaluatable.
2631 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2632 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002633
2634 return false;
2635}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002636
Jim Grosbach4b905842013-09-20 23:08:21 +00002637/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002638/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002639bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002640 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002641
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002642 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002643 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002644 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002645 return true;
2646
2647 SMLoc MaxBytesLoc;
2648 bool HasFillExpr = false;
2649 int64_t FillExpr = 0;
2650 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002651 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2652 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002654 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655
2656 // The fill expression can be omitted while specifying a maximum number of
2657 // alignment bytes, e.g:
2658 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002661 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662 return true;
2663 }
2664
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002665 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2666 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002668 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002670 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002671 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002673
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002674 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 return TokError("unexpected token in directive");
2676 }
2677 }
2678
Sean Callanan686ed8d2010-01-19 20:22:31 +00002679 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002680
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002681 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683
2684 // Compute alignment in bytes.
2685 if (IsPow2) {
2686 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002687 if (Alignment >= 32) {
2688 Error(AlignmentLoc, "invalid alignment value");
2689 Alignment = 31;
2690 }
2691
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002692 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002693 } else {
2694 // Reject alignments that aren't a power of two, for gas compatibility.
2695 if (!isPowerOf2_64(Alignment))
2696 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697 }
2698
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002699 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002700 if (MaxBytesLoc.isValid()) {
2701 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002702 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002703 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002704 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002705 }
2706
2707 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002708 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002709 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002710 MaxBytesToFill = 0;
2711 }
2712 }
2713
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002714 // Check whether we should use optimal code alignment for this .align
2715 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002716 const MCSection *Section = getStreamer().getCurrentSection().first;
2717 assert(Section && "must have section to emit alignment");
2718 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002719 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2720 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002721 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002722 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002723 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002724 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2725 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002726 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002727
2728 return false;
2729}
2730
Jim Grosbach4b905842013-09-20 23:08:21 +00002731/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002732/// ::= .file [number] filename
2733/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002734bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002735 // FIXME: I'm not sure what this is.
2736 int64_t FileNumber = -1;
2737 SMLoc FileNumberLoc = getLexer().getLoc();
2738 if (getLexer().is(AsmToken::Integer)) {
2739 FileNumber = getTok().getIntVal();
2740 Lex();
2741
2742 if (FileNumber < 1)
2743 return TokError("file number less than one");
2744 }
2745
2746 if (getLexer().isNot(AsmToken::String))
2747 return TokError("unexpected token in '.file' directive");
2748
2749 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002750 // Allow the strings to have escaped octal character sequence.
2751 std::string Path = getTok().getString();
2752 if (parseEscapedString(Path))
2753 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002754 Lex();
2755
2756 StringRef Directory;
2757 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002758 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002759 if (getLexer().is(AsmToken::String)) {
2760 if (FileNumber == -1)
2761 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002762 if (parseEscapedString(FilenameData))
2763 return true;
2764 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002765 Directory = Path;
2766 Lex();
2767 } else {
2768 Filename = Path;
2769 }
2770
2771 if (getLexer().isNot(AsmToken::EndOfStatement))
2772 return TokError("unexpected token in '.file' directive");
2773
2774 if (FileNumber == -1)
2775 getStreamer().EmitFileDirective(Filename);
2776 else {
2777 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002778 Error(DirectiveLoc,
2779 "input can't have .file dwarf directives when -g is "
2780 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002781
David Blaikiec714ef42014-03-17 01:52:11 +00002782 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2783 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002784 Error(FileNumberLoc, "file number already allocated");
2785 }
2786
2787 return false;
2788}
2789
Jim Grosbach4b905842013-09-20 23:08:21 +00002790/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002791/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002792bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002793 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2794 if (getLexer().isNot(AsmToken::Integer))
2795 return TokError("unexpected token in '.line' directive");
2796
2797 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002798 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002799 Lex();
2800
2801 // FIXME: Do something with the .line.
2802 }
2803
2804 if (getLexer().isNot(AsmToken::EndOfStatement))
2805 return TokError("unexpected token in '.line' directive");
2806
2807 return false;
2808}
2809
Jim Grosbach4b905842013-09-20 23:08:21 +00002810/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002811/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2812/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2813/// The first number is a file number, must have been previously assigned with
2814/// a .file directive, the second number is the line number and optionally the
2815/// third number is a column position (zero if not specified). The remaining
2816/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002817bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002818 if (getLexer().isNot(AsmToken::Integer))
2819 return TokError("unexpected token in '.loc' directive");
2820 int64_t FileNumber = getTok().getIntVal();
2821 if (FileNumber < 1)
2822 return TokError("file number less than one in '.loc' directive");
2823 if (!getContext().isValidDwarfFileNumber(FileNumber))
2824 return TokError("unassigned file number in '.loc' directive");
2825 Lex();
2826
2827 int64_t LineNumber = 0;
2828 if (getLexer().is(AsmToken::Integer)) {
2829 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002830 if (LineNumber < 0)
2831 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002832 Lex();
2833 }
2834
2835 int64_t ColumnPos = 0;
2836 if (getLexer().is(AsmToken::Integer)) {
2837 ColumnPos = getTok().getIntVal();
2838 if (ColumnPos < 0)
2839 return TokError("column position less than zero in '.loc' directive");
2840 Lex();
2841 }
2842
2843 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2844 unsigned Isa = 0;
2845 int64_t Discriminator = 0;
2846 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2847 for (;;) {
2848 if (getLexer().is(AsmToken::EndOfStatement))
2849 break;
2850
2851 StringRef Name;
2852 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002853 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002854 return TokError("unexpected token in '.loc' directive");
2855
2856 if (Name == "basic_block")
2857 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2858 else if (Name == "prologue_end")
2859 Flags |= DWARF2_FLAG_PROLOGUE_END;
2860 else if (Name == "epilogue_begin")
2861 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2862 else if (Name == "is_stmt") {
2863 Loc = getTok().getLoc();
2864 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002865 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return true;
2867 // The expression must be the constant 0 or 1.
2868 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2869 int Value = MCE->getValue();
2870 if (Value == 0)
2871 Flags &= ~DWARF2_FLAG_IS_STMT;
2872 else if (Value == 1)
2873 Flags |= DWARF2_FLAG_IS_STMT;
2874 else
2875 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002876 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2878 }
Craig Topperf15655b2013-04-22 04:22:40 +00002879 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002880 Loc = getTok().getLoc();
2881 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002882 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002883 return true;
2884 // The expression must be a constant greater or equal to 0.
2885 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2886 int Value = MCE->getValue();
2887 if (Value < 0)
2888 return Error(Loc, "isa number less than zero");
2889 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002890 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 return Error(Loc, "isa number not a constant value");
2892 }
Craig Topperf15655b2013-04-22 04:22:40 +00002893 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002894 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002895 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002896 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002897 return Error(Loc, "unknown sub-directive in '.loc' directive");
2898 }
2899
2900 if (getLexer().is(AsmToken::EndOfStatement))
2901 break;
2902 }
2903 }
2904
2905 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2906 Isa, Discriminator, StringRef());
2907
2908 return false;
2909}
2910
Jim Grosbach4b905842013-09-20 23:08:21 +00002911/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002912/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002913bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return TokError("unsupported directive '.stabs'");
2915}
2916
Jim Grosbach4b905842013-09-20 23:08:21 +00002917/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002918/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002919bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002920 StringRef Name;
2921 bool EH = false;
2922 bool Debug = false;
2923
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002924 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002925 return TokError("Expected an identifier");
2926
2927 if (Name == ".eh_frame")
2928 EH = true;
2929 else if (Name == ".debug_frame")
2930 Debug = true;
2931
2932 if (getLexer().is(AsmToken::Comma)) {
2933 Lex();
2934
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002935 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return TokError("Expected an identifier");
2937
2938 if (Name == ".eh_frame")
2939 EH = true;
2940 else if (Name == ".debug_frame")
2941 Debug = true;
2942 }
2943
2944 getStreamer().EmitCFISections(EH, Debug);
2945 return false;
2946}
2947
Jim Grosbach4b905842013-09-20 23:08:21 +00002948/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002949/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002950bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002951 StringRef Simple;
2952 if (getLexer().isNot(AsmToken::EndOfStatement))
2953 if (parseIdentifier(Simple) || Simple != "simple")
2954 return TokError("unexpected token in .cfi_startproc directive");
2955
2956 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002957 return false;
2958}
2959
Jim Grosbach4b905842013-09-20 23:08:21 +00002960/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002961/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002962bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002963 getStreamer().EmitCFIEndProc();
2964 return false;
2965}
2966
Jim Grosbach4b905842013-09-20 23:08:21 +00002967/// \brief parse register name or number.
2968bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002969 SMLoc DirectiveLoc) {
2970 unsigned RegNo;
2971
2972 if (getLexer().isNot(AsmToken::Integer)) {
2973 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2974 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002975 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002976 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002977 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002978
2979 return false;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002983/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002984bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002985 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002986 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002987 return true;
2988
2989 if (getLexer().isNot(AsmToken::Comma))
2990 return TokError("unexpected token in directive");
2991 Lex();
2992
2993 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002994 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002995 return true;
2996
2997 getStreamer().EmitCFIDefCfa(Register, Offset);
2998 return false;
2999}
3000
Jim Grosbach4b905842013-09-20 23:08:21 +00003001/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003002/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003003bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003004 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003005 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return true;
3007
3008 getStreamer().EmitCFIDefCfaOffset(Offset);
3009 return false;
3010}
3011
Jim Grosbach4b905842013-09-20 23:08:21 +00003012/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003013/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003014bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003015 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003016 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return true;
3018
3019 if (getLexer().isNot(AsmToken::Comma))
3020 return TokError("unexpected token in directive");
3021 Lex();
3022
3023 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003024 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003025 return true;
3026
3027 getStreamer().EmitCFIRegister(Register1, Register2);
3028 return false;
3029}
3030
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003031/// parseDirectiveCFIWindowSave
3032/// ::= .cfi_window_save
3033bool AsmParser::parseDirectiveCFIWindowSave() {
3034 getStreamer().EmitCFIWindowSave();
3035 return false;
3036}
3037
Jim Grosbach4b905842013-09-20 23:08:21 +00003038/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003039/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003040bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003041 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003042 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003043 return true;
3044
3045 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3046 return false;
3047}
3048
Jim Grosbach4b905842013-09-20 23:08:21 +00003049/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003050/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003051bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003052 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003053 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003054 return true;
3055
3056 getStreamer().EmitCFIDefCfaRegister(Register);
3057 return false;
3058}
3059
Jim Grosbach4b905842013-09-20 23:08:21 +00003060/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003061/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003062bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003063 int64_t Register = 0;
3064 int64_t Offset = 0;
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003067 return true;
3068
3069 if (getLexer().isNot(AsmToken::Comma))
3070 return TokError("unexpected token in directive");
3071 Lex();
3072
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003073 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003074 return true;
3075
3076 getStreamer().EmitCFIOffset(Register, Offset);
3077 return false;
3078}
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003081/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003082bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003083 int64_t Register = 0;
3084
Jim Grosbach4b905842013-09-20 23:08:21 +00003085 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003086 return true;
3087
3088 if (getLexer().isNot(AsmToken::Comma))
3089 return TokError("unexpected token in directive");
3090 Lex();
3091
3092 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003093 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003094 return true;
3095
3096 getStreamer().EmitCFIRelOffset(Register, Offset);
3097 return false;
3098}
3099
3100static bool isValidEncoding(int64_t Encoding) {
3101 if (Encoding & ~0xff)
3102 return false;
3103
3104 if (Encoding == dwarf::DW_EH_PE_omit)
3105 return true;
3106
3107 const unsigned Format = Encoding & 0xf;
3108 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3109 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3110 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3111 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3112 return false;
3113
3114 const unsigned Application = Encoding & 0x70;
3115 if (Application != dwarf::DW_EH_PE_absptr &&
3116 Application != dwarf::DW_EH_PE_pcrel)
3117 return false;
3118
3119 return true;
3120}
3121
Jim Grosbach4b905842013-09-20 23:08:21 +00003122/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003123/// IsPersonality true for cfi_personality, false for cfi_lsda
3124/// ::= .cfi_personality encoding, [symbol_name]
3125/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003126bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003127 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003128 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003129 return true;
3130 if (Encoding == dwarf::DW_EH_PE_omit)
3131 return false;
3132
3133 if (!isValidEncoding(Encoding))
3134 return TokError("unsupported encoding.");
3135
3136 if (getLexer().isNot(AsmToken::Comma))
3137 return TokError("unexpected token in directive");
3138 Lex();
3139
3140 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003141 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003142 return TokError("expected identifier in directive");
3143
3144 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3145
3146 if (IsPersonality)
3147 getStreamer().EmitCFIPersonality(Sym, Encoding);
3148 else
3149 getStreamer().EmitCFILsda(Sym, Encoding);
3150 return false;
3151}
3152
Jim Grosbach4b905842013-09-20 23:08:21 +00003153/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003154/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003155bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003156 getStreamer().EmitCFIRememberState();
3157 return false;
3158}
3159
Jim Grosbach4b905842013-09-20 23:08:21 +00003160/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003161/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003162bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003163 getStreamer().EmitCFIRestoreState();
3164 return false;
3165}
3166
Jim Grosbach4b905842013-09-20 23:08:21 +00003167/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003168/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003169bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003170 int64_t Register = 0;
3171
Jim Grosbach4b905842013-09-20 23:08:21 +00003172 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003173 return true;
3174
3175 getStreamer().EmitCFISameValue(Register);
3176 return false;
3177}
3178
Jim Grosbach4b905842013-09-20 23:08:21 +00003179/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003180/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003181bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003182 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003183 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003184 return true;
3185
3186 getStreamer().EmitCFIRestore(Register);
3187 return false;
3188}
3189
Jim Grosbach4b905842013-09-20 23:08:21 +00003190/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003191/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003192bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003193 std::string Values;
3194 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003195 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003196 return true;
3197
3198 Values.push_back((uint8_t)CurrValue);
3199
3200 while (getLexer().is(AsmToken::Comma)) {
3201 Lex();
3202
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003203 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003204 return true;
3205
3206 Values.push_back((uint8_t)CurrValue);
3207 }
3208
3209 getStreamer().EmitCFIEscape(Values);
3210 return false;
3211}
3212
Jim Grosbach4b905842013-09-20 23:08:21 +00003213/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003214/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003215bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003216 if (getLexer().isNot(AsmToken::EndOfStatement))
3217 return Error(getLexer().getLoc(),
3218 "unexpected token in '.cfi_signal_frame'");
3219
3220 getStreamer().EmitCFISignalFrame();
3221 return false;
3222}
3223
Jim Grosbach4b905842013-09-20 23:08:21 +00003224/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003225/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003226bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003227 int64_t Register = 0;
3228
Jim Grosbach4b905842013-09-20 23:08:21 +00003229 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003230 return true;
3231
3232 getStreamer().EmitCFIUndefined(Register);
3233 return false;
3234}
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003237/// ::= .macros_on
3238/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003239bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003240 if (getLexer().isNot(AsmToken::EndOfStatement))
3241 return Error(getLexer().getLoc(),
3242 "unexpected token in '" + Directive + "' directive");
3243
Jim Grosbach4b905842013-09-20 23:08:21 +00003244 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003245 return false;
3246}
3247
Jim Grosbach4b905842013-09-20 23:08:21 +00003248/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003249/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003250bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003251 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003252 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003253 return TokError("expected identifier in '.macro' directive");
3254
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003255 if (getLexer().is(AsmToken::Comma))
3256 Lex();
3257
Eli Bendersky17233942013-01-15 22:59:42 +00003258 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003259 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003260
3261 if (Parameters.size() && Parameters.back().Vararg)
3262 return Error(Lexer.getLoc(),
3263 "Vararg parameter '" + Parameters.back().Name +
3264 "' should be last one in the list of parameters.");
3265
David Majnemer91fc4c22014-01-29 18:57:46 +00003266 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003267 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003268 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003269
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003270 if (Lexer.is(AsmToken::Colon)) {
3271 Lex(); // consume ':'
3272
3273 SMLoc QualLoc;
3274 StringRef Qualifier;
3275
3276 QualLoc = Lexer.getLoc();
3277 if (parseIdentifier(Qualifier))
3278 return Error(QualLoc, "missing parameter qualifier for "
3279 "'" + Parameter.Name + "' in macro '" + Name + "'");
3280
3281 if (Qualifier == "req")
3282 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003283 else if (Qualifier == "vararg" && !IsDarwin)
3284 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003285 else
3286 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3287 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3288 }
3289
David Majnemer91fc4c22014-01-29 18:57:46 +00003290 if (getLexer().is(AsmToken::Equal)) {
3291 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003292
3293 SMLoc ParamLoc;
3294
3295 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003296 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003297 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003298
3299 if (Parameter.Required)
3300 Warning(ParamLoc, "pointless default value for required parameter "
3301 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003302 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003303
3304 Parameters.push_back(Parameter);
3305
3306 if (getLexer().is(AsmToken::Comma))
3307 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003308 }
3309
3310 // Eat the end of statement.
3311 Lex();
3312
3313 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003314 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003315
3316 // Lex the macro definition.
3317 for (;;) {
3318 // Check whether we have reached the end of the file.
3319 if (getLexer().is(AsmToken::Eof))
3320 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3321
3322 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003323 if (getLexer().is(AsmToken::Identifier)) {
3324 if (getTok().getIdentifier() == ".endm" ||
3325 getTok().getIdentifier() == ".endmacro") {
3326 if (MacroDepth == 0) { // Outermost macro.
3327 EndToken = getTok();
3328 Lex();
3329 if (getLexer().isNot(AsmToken::EndOfStatement))
3330 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3331 "' directive");
3332 break;
3333 } else {
3334 // Otherwise we just found the end of an inner macro.
3335 --MacroDepth;
3336 }
3337 } else if (getTok().getIdentifier() == ".macro") {
3338 // We allow nested macros. Those aren't instantiated until the outermost
3339 // macro is expanded so just ignore them for now.
3340 ++MacroDepth;
3341 }
Eli Bendersky17233942013-01-15 22:59:42 +00003342 }
3343
3344 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003345 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003346 }
3347
Jim Grosbach4b905842013-09-20 23:08:21 +00003348 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003349 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3350 }
3351
3352 const char *BodyStart = StartToken.getLoc().getPointer();
3353 const char *BodyEnd = EndToken.getLoc().getPointer();
3354 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003355 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3356 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003357 return false;
3358}
3359
Jim Grosbach4b905842013-09-20 23:08:21 +00003360/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003361///
3362/// With the support added for named parameters there may be code out there that
3363/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003364/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003365/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003366/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003367/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3368/// warning that the positional parameter found in body which have no effect.
3369/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003370/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003371/// intended or change the macro to use the named parameters. It is possible
3372/// this warning will trigger when the none of the named parameters are used
3373/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003374void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003375 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003376 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003377 // If this macro is not defined with named parameters the warning we are
3378 // checking for here doesn't apply.
3379 unsigned NParameters = Parameters.size();
3380 if (NParameters == 0)
3381 return;
3382
3383 bool NamedParametersFound = false;
3384 bool PositionalParametersFound = false;
3385
3386 // Look at the body of the macro for use of both the named parameters and what
3387 // are likely to be positional parameters. This is what expandMacro() is
3388 // doing when it finds the parameters in the body.
3389 while (!Body.empty()) {
3390 // Scan for the next possible parameter.
3391 std::size_t End = Body.size(), Pos = 0;
3392 for (; Pos != End; ++Pos) {
3393 // Check for a substitution or escape.
3394 // This macro is defined with parameters, look for \foo, \bar, etc.
3395 if (Body[Pos] == '\\' && Pos + 1 != End)
3396 break;
3397
3398 // This macro should have parameters, but look for $0, $1, ..., $n too.
3399 if (Body[Pos] != '$' || Pos + 1 == End)
3400 continue;
3401 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003402 if (Next == '$' || Next == 'n' ||
3403 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003404 break;
3405 }
3406
3407 // Check if we reached the end.
3408 if (Pos == End)
3409 break;
3410
3411 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003412 switch (Body[Pos + 1]) {
3413 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003414 case '$':
3415 break;
3416
Jim Grosbach4b905842013-09-20 23:08:21 +00003417 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003418 case 'n':
3419 PositionalParametersFound = true;
3420 break;
3421
Jim Grosbach4b905842013-09-20 23:08:21 +00003422 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003423 default: {
3424 PositionalParametersFound = true;
3425 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003426 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003427 }
3428 Pos += 2;
3429 } else {
3430 unsigned I = Pos + 1;
3431 while (isIdentifierChar(Body[I]) && I + 1 != End)
3432 ++I;
3433
Jim Grosbach4b905842013-09-20 23:08:21 +00003434 const char *Begin = Body.data() + Pos + 1;
3435 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003436 unsigned Index = 0;
3437 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003438 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003439 break;
3440
3441 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003442 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3443 Pos += 3;
3444 else {
3445 Pos = I;
3446 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003447 } else {
3448 NamedParametersFound = true;
3449 Pos += 1 + Argument.size();
3450 }
3451 }
3452 // Update the scan point.
3453 Body = Body.substr(Pos);
3454 }
3455
3456 if (!NamedParametersFound && PositionalParametersFound)
3457 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3458 "used in macro body, possible positional parameter "
3459 "found in body which will have no effect");
3460}
3461
Jim Grosbach4b905842013-09-20 23:08:21 +00003462/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003463/// ::= .endm
3464/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003465bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003466 if (getLexer().isNot(AsmToken::EndOfStatement))
3467 return TokError("unexpected token in '" + Directive + "' directive");
3468
3469 // If we are inside a macro instantiation, terminate the current
3470 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003471 if (isInsideMacroInstantiation()) {
3472 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003473 return false;
3474 }
3475
3476 // Otherwise, this .endmacro is a stray entry in the file; well formed
3477 // .endmacro directives are handled during the macro definition parsing.
3478 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003479 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003480}
3481
Jim Grosbach4b905842013-09-20 23:08:21 +00003482/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003483/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003484bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003485 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003486 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003487 return TokError("expected identifier in '.purgem' directive");
3488
3489 if (getLexer().isNot(AsmToken::EndOfStatement))
3490 return TokError("unexpected token in '.purgem' directive");
3491
Jim Grosbach4b905842013-09-20 23:08:21 +00003492 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003493 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3494
Jim Grosbach4b905842013-09-20 23:08:21 +00003495 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003496 return false;
3497}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003500/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003502 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003503
3504 // Expect a single argument: an expression that evaluates to a constant
3505 // in the inclusive range 0-30.
3506 SMLoc ExprLoc = getLexer().getLoc();
3507 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003508 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003509 return true;
3510 else if (getLexer().isNot(AsmToken::EndOfStatement))
3511 return TokError("unexpected token after expression in"
3512 " '.bundle_align_mode' directive");
3513 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3514 return Error(ExprLoc,
3515 "invalid bundle alignment size (expected between 0 and 30)");
3516
3517 Lex();
3518
3519 // Because of AlignSizePow2's verified range we can safely truncate it to
3520 // unsigned.
3521 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3522 return false;
3523}
3524
Jim Grosbach4b905842013-09-20 23:08:21 +00003525/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003526/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003528 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003529 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003530
Eli Bendersky802b6282013-01-07 21:51:08 +00003531 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3532 StringRef Option;
3533 SMLoc Loc = getTok().getLoc();
3534 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003535 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003536
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003537 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003538 return Error(Loc, kInvalidOptionError);
3539
3540 if (Option != "align_to_end")
3541 return Error(Loc, kInvalidOptionError);
3542 else if (getLexer().isNot(AsmToken::EndOfStatement))
3543 return Error(Loc,
3544 "unexpected token after '.bundle_lock' directive option");
3545 AlignToEnd = true;
3546 }
3547
Eli Benderskyf483ff92012-12-20 19:05:53 +00003548 Lex();
3549
Eli Bendersky802b6282013-01-07 21:51:08 +00003550 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003551 return false;
3552}
3553
Jim Grosbach4b905842013-09-20 23:08:21 +00003554/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003555/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003556bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003557 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003558
3559 if (getLexer().isNot(AsmToken::EndOfStatement))
3560 return TokError("unexpected token in '.bundle_unlock' directive");
3561 Lex();
3562
3563 getStreamer().EmitBundleUnlock();
3564 return false;
3565}
3566
Jim Grosbach4b905842013-09-20 23:08:21 +00003567/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003568/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003569bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003570 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003571
3572 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003573 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003574 return true;
3575
3576 int64_t FillExpr = 0;
3577 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3578 if (getLexer().isNot(AsmToken::Comma))
3579 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3580 Lex();
3581
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003582 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003583 return true;
3584
3585 if (getLexer().isNot(AsmToken::EndOfStatement))
3586 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3587 }
3588
3589 Lex();
3590
3591 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003592 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3593 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003594
3595 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003596 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003597
3598 return false;
3599}
3600
Jim Grosbach4b905842013-09-20 23:08:21 +00003601/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003602/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003603bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003604 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003605 const MCExpr *Value;
3606
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003607 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003608 return true;
3609
3610 if (getLexer().isNot(AsmToken::EndOfStatement))
3611 return TokError("unexpected token in directive");
3612
3613 if (Signed)
3614 getStreamer().EmitSLEB128Value(Value);
3615 else
3616 getStreamer().EmitULEB128Value(Value);
3617
3618 return false;
3619}
3620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003622/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003624 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003625 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003626 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003627 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003628
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003629 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003630 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003631
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003632 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003633
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003634 // Assembler local symbols don't make any sense here. Complain loudly.
3635 if (Sym->isTemporary())
3636 return Error(Loc, "non-local symbol required in directive");
3637
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003638 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3639 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003640
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003641 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003642 break;
3643
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003644 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003645 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003646 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003647 }
3648 }
3649
Sean Callanan686ed8d2010-01-19 20:22:31 +00003650 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003651 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003652}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003653
Jim Grosbach4b905842013-09-20 23:08:21 +00003654/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003655/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003656bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003657 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003658
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003659 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003660 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003661 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003662 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003663
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003664 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003665 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003666
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003667 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003668 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003669 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003670
3671 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003672 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003674 return true;
3675
3676 int64_t Pow2Alignment = 0;
3677 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003678 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003679 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003680 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003681 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003682 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003683
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003684 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3685 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003686 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3687
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003688 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003689 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3690 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003691 if (!isPowerOf2_64(Pow2Alignment))
3692 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3693 Pow2Alignment = Log2_64(Pow2Alignment);
3694 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003695 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003696
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003697 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003698 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003699
Sean Callanan686ed8d2010-01-19 20:22:31 +00003700 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003701
Chris Lattner28ad7542009-07-09 17:25:12 +00003702 // NOTE: a size of zero for a .comm should create a undefined symbol
3703 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003704 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003705 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003706 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003707
Eric Christopherbc818852010-05-14 01:38:54 +00003708 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003709 // may internally end up wanting an alignment in bytes.
3710 // FIXME: Diagnose overflow.
3711 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003712 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003713 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003714
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003715 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003716 return Error(IDLoc, "invalid symbol redefinition");
3717
Chris Lattner28ad7542009-07-09 17:25:12 +00003718 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003719 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003720 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003721 return false;
3722 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003723
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003724 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003725 return false;
3726}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003727
Jim Grosbach4b905842013-09-20 23:08:21 +00003728/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003729/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003730bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003731 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003732 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003733
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003734 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003735 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003736 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003737
Sean Callanan686ed8d2010-01-19 20:22:31 +00003738 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003739
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003740 if (Str.empty())
3741 Error(Loc, ".abort detected. Assembly stopping.");
3742 else
3743 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003744 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003745
3746 return false;
3747}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003748
Jim Grosbach4b905842013-09-20 23:08:21 +00003749/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003750/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003751bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003752 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003753 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003754
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003755 // Allow the strings to have escaped octal character sequence.
3756 std::string Filename;
3757 if (parseEscapedString(Filename))
3758 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003759 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003760 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003761
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003762 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003763 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003764
Chris Lattner693fbb82009-07-16 06:14:39 +00003765 // Attempt to switch the lexer to the included file before consuming the end
3766 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003767 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003768 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003769 return true;
3770 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003771
3772 return false;
3773}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003774
Jim Grosbach4b905842013-09-20 23:08:21 +00003775/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003776/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003777bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003778 if (getLexer().isNot(AsmToken::String))
3779 return TokError("expected string in '.incbin' directive");
3780
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003781 // Allow the strings to have escaped octal character sequence.
3782 std::string Filename;
3783 if (parseEscapedString(Filename))
3784 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003785 SMLoc IncbinLoc = getLexer().getLoc();
3786 Lex();
3787
3788 if (getLexer().isNot(AsmToken::EndOfStatement))
3789 return TokError("unexpected token in '.incbin' directive");
3790
Kevin Enderby109f25c2011-12-14 21:47:48 +00003791 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003792 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003793 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3794 return true;
3795 }
3796
3797 return false;
3798}
3799
Jim Grosbach4b905842013-09-20 23:08:21 +00003800/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003801/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3802bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003803 TheCondStack.push_back(TheCondState);
3804 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003805 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003807 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003808 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003809 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003810 return true;
3811
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003812 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003813 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003814
Sean Callanan686ed8d2010-01-19 20:22:31 +00003815 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003816
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003817 switch (DirKind) {
3818 default:
3819 llvm_unreachable("unsupported directive");
3820 case DK_IF:
3821 case DK_IFNE:
3822 break;
3823 case DK_IFEQ:
3824 ExprValue = ExprValue == 0;
3825 break;
3826 case DK_IFGE:
3827 ExprValue = ExprValue >= 0;
3828 break;
3829 case DK_IFGT:
3830 ExprValue = ExprValue > 0;
3831 break;
3832 case DK_IFLE:
3833 ExprValue = ExprValue <= 0;
3834 break;
3835 case DK_IFLT:
3836 ExprValue = ExprValue < 0;
3837 break;
3838 }
3839
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003840 TheCondState.CondMet = ExprValue;
3841 TheCondState.Ignore = !TheCondState.CondMet;
3842 }
3843
3844 return false;
3845}
3846
Jim Grosbach4b905842013-09-20 23:08:21 +00003847/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003848/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003849bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003850 TheCondStack.push_back(TheCondState);
3851 TheCondState.TheCond = AsmCond::IfCond;
3852
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003853 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003854 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003855 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003856 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003857
3858 if (getLexer().isNot(AsmToken::EndOfStatement))
3859 return TokError("unexpected token in '.ifb' directive");
3860
3861 Lex();
3862
3863 TheCondState.CondMet = ExpectBlank == Str.empty();
3864 TheCondState.Ignore = !TheCondState.CondMet;
3865 }
3866
3867 return false;
3868}
3869
Jim Grosbach4b905842013-09-20 23:08:21 +00003870/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003871/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003872/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003873bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003874 TheCondStack.push_back(TheCondState);
3875 TheCondState.TheCond = AsmCond::IfCond;
3876
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003877 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003878 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003879 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003880 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003881
3882 if (getLexer().isNot(AsmToken::Comma))
3883 return TokError("unexpected token in '.ifc' directive");
3884
3885 Lex();
3886
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003887 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003888
3889 if (getLexer().isNot(AsmToken::EndOfStatement))
3890 return TokError("unexpected token in '.ifc' directive");
3891
3892 Lex();
3893
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003894 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003895 TheCondState.Ignore = !TheCondState.CondMet;
3896 }
3897
3898 return false;
3899}
3900
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003901/// parseDirectiveIfeqs
3902/// ::= .ifeqs string1, string2
3903bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3904 if (Lexer.isNot(AsmToken::String)) {
3905 TokError("expected string parameter for '.ifeqs' directive");
3906 eatToEndOfStatement();
3907 return true;
3908 }
3909
3910 StringRef String1 = getTok().getStringContents();
3911 Lex();
3912
3913 if (Lexer.isNot(AsmToken::Comma)) {
3914 TokError("expected comma after first string for '.ifeqs' directive");
3915 eatToEndOfStatement();
3916 return true;
3917 }
3918
3919 Lex();
3920
3921 if (Lexer.isNot(AsmToken::String)) {
3922 TokError("expected string parameter for '.ifeqs' directive");
3923 eatToEndOfStatement();
3924 return true;
3925 }
3926
3927 StringRef String2 = getTok().getStringContents();
3928 Lex();
3929
3930 TheCondStack.push_back(TheCondState);
3931 TheCondState.TheCond = AsmCond::IfCond;
3932 TheCondState.CondMet = String1 == String2;
3933 TheCondState.Ignore = !TheCondState.CondMet;
3934
3935 return false;
3936}
3937
Jim Grosbach4b905842013-09-20 23:08:21 +00003938/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003939/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003940bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003941 StringRef Name;
3942 TheCondStack.push_back(TheCondState);
3943 TheCondState.TheCond = AsmCond::IfCond;
3944
3945 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003946 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003947 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003948 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003949 return TokError("expected identifier after '.ifdef'");
3950
3951 Lex();
3952
3953 MCSymbol *Sym = getContext().LookupSymbol(Name);
3954
3955 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003956 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003957 else
Craig Topper353eda42014-04-24 06:44:33 +00003958 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003959 TheCondState.Ignore = !TheCondState.CondMet;
3960 }
3961
3962 return false;
3963}
3964
Jim Grosbach4b905842013-09-20 23:08:21 +00003965/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003966/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003967bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003968 if (TheCondState.TheCond != AsmCond::IfCond &&
3969 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003970 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3971 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003972 TheCondState.TheCond = AsmCond::ElseIfCond;
3973
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003974 bool LastIgnoreState = false;
3975 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003976 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003977 if (LastIgnoreState || TheCondState.CondMet) {
3978 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003979 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003980 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003981 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003982 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003983 return true;
3984
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003985 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003986 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003987
Sean Callanan686ed8d2010-01-19 20:22:31 +00003988 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003989 TheCondState.CondMet = ExprValue;
3990 TheCondState.Ignore = !TheCondState.CondMet;
3991 }
3992
3993 return false;
3994}
3995
Jim Grosbach4b905842013-09-20 23:08:21 +00003996/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003997/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003998bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003999 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004000 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004001
Sean Callanan686ed8d2010-01-19 20:22:31 +00004002 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004003
4004 if (TheCondState.TheCond != AsmCond::IfCond &&
4005 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004006 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4007 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008 TheCondState.TheCond = AsmCond::ElseCond;
4009 bool LastIgnoreState = false;
4010 if (!TheCondStack.empty())
4011 LastIgnoreState = TheCondStack.back().Ignore;
4012 if (LastIgnoreState || TheCondState.CondMet)
4013 TheCondState.Ignore = true;
4014 else
4015 TheCondState.Ignore = false;
4016
4017 return false;
4018}
4019
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004020/// parseDirectiveEnd
4021/// ::= .end
4022bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4023 if (getLexer().isNot(AsmToken::EndOfStatement))
4024 return TokError("unexpected token in '.end' directive");
4025
4026 Lex();
4027
4028 while (Lexer.isNot(AsmToken::Eof))
4029 Lex();
4030
4031 return false;
4032}
4033
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004034/// parseDirectiveError
4035/// ::= .err
4036/// ::= .error [string]
4037bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4038 if (!TheCondStack.empty()) {
4039 if (TheCondStack.back().Ignore) {
4040 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004041 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004042 }
4043 }
4044
4045 if (!WithMessage)
4046 return Error(L, ".err encountered");
4047
4048 StringRef Message = ".error directive invoked in source file";
4049 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4050 if (Lexer.isNot(AsmToken::String)) {
4051 TokError(".error argument must be a string");
4052 eatToEndOfStatement();
4053 return true;
4054 }
4055
4056 Message = getTok().getStringContents();
4057 Lex();
4058 }
4059
4060 Error(L, Message);
4061 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004062}
4063
Jim Grosbach4b905842013-09-20 23:08:21 +00004064/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004065/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004066bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004067 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004068 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004069
Sean Callanan686ed8d2010-01-19 20:22:31 +00004070 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004071
Jim Grosbach4b905842013-09-20 23:08:21 +00004072 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004073 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4074 ".else");
4075 if (!TheCondStack.empty()) {
4076 TheCondState = TheCondStack.back();
4077 TheCondStack.pop_back();
4078 }
4079
4080 return false;
4081}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004082
Eli Bendersky17233942013-01-15 22:59:42 +00004083void AsmParser::initializeDirectiveKindMap() {
4084 DirectiveKindMap[".set"] = DK_SET;
4085 DirectiveKindMap[".equ"] = DK_EQU;
4086 DirectiveKindMap[".equiv"] = DK_EQUIV;
4087 DirectiveKindMap[".ascii"] = DK_ASCII;
4088 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4089 DirectiveKindMap[".string"] = DK_STRING;
4090 DirectiveKindMap[".byte"] = DK_BYTE;
4091 DirectiveKindMap[".short"] = DK_SHORT;
4092 DirectiveKindMap[".value"] = DK_VALUE;
4093 DirectiveKindMap[".2byte"] = DK_2BYTE;
4094 DirectiveKindMap[".long"] = DK_LONG;
4095 DirectiveKindMap[".int"] = DK_INT;
4096 DirectiveKindMap[".4byte"] = DK_4BYTE;
4097 DirectiveKindMap[".quad"] = DK_QUAD;
4098 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004099 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004100 DirectiveKindMap[".single"] = DK_SINGLE;
4101 DirectiveKindMap[".float"] = DK_FLOAT;
4102 DirectiveKindMap[".double"] = DK_DOUBLE;
4103 DirectiveKindMap[".align"] = DK_ALIGN;
4104 DirectiveKindMap[".align32"] = DK_ALIGN32;
4105 DirectiveKindMap[".balign"] = DK_BALIGN;
4106 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4107 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4108 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4109 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4110 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4111 DirectiveKindMap[".org"] = DK_ORG;
4112 DirectiveKindMap[".fill"] = DK_FILL;
4113 DirectiveKindMap[".zero"] = DK_ZERO;
4114 DirectiveKindMap[".extern"] = DK_EXTERN;
4115 DirectiveKindMap[".globl"] = DK_GLOBL;
4116 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004117 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4118 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4119 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4120 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4121 DirectiveKindMap[".reference"] = DK_REFERENCE;
4122 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4123 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4124 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4125 DirectiveKindMap[".comm"] = DK_COMM;
4126 DirectiveKindMap[".common"] = DK_COMMON;
4127 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4128 DirectiveKindMap[".abort"] = DK_ABORT;
4129 DirectiveKindMap[".include"] = DK_INCLUDE;
4130 DirectiveKindMap[".incbin"] = DK_INCBIN;
4131 DirectiveKindMap[".code16"] = DK_CODE16;
4132 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4133 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004134 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004135 DirectiveKindMap[".irp"] = DK_IRP;
4136 DirectiveKindMap[".irpc"] = DK_IRPC;
4137 DirectiveKindMap[".endr"] = DK_ENDR;
4138 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4139 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4140 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4141 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004142 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4143 DirectiveKindMap[".ifge"] = DK_IFGE;
4144 DirectiveKindMap[".ifgt"] = DK_IFGT;
4145 DirectiveKindMap[".ifle"] = DK_IFLE;
4146 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004147 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004148 DirectiveKindMap[".ifb"] = DK_IFB;
4149 DirectiveKindMap[".ifnb"] = DK_IFNB;
4150 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004151 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004152 DirectiveKindMap[".ifnc"] = DK_IFNC;
4153 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4154 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4155 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4156 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4157 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004158 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004159 DirectiveKindMap[".endif"] = DK_ENDIF;
4160 DirectiveKindMap[".skip"] = DK_SKIP;
4161 DirectiveKindMap[".space"] = DK_SPACE;
4162 DirectiveKindMap[".file"] = DK_FILE;
4163 DirectiveKindMap[".line"] = DK_LINE;
4164 DirectiveKindMap[".loc"] = DK_LOC;
4165 DirectiveKindMap[".stabs"] = DK_STABS;
4166 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4167 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4168 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4169 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4170 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4171 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4172 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4173 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4174 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4175 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4176 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4177 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4178 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4179 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4180 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4181 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4182 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4183 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4184 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4185 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4186 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004187 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004188 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4189 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4190 DirectiveKindMap[".macro"] = DK_MACRO;
4191 DirectiveKindMap[".endm"] = DK_ENDM;
4192 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4193 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004194 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004195 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004196}
4197
Jim Grosbach4b905842013-09-20 23:08:21 +00004198MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004199 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004200
Rafael Espindola34b9c512012-06-03 23:57:14 +00004201 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004202 for (;;) {
4203 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004204 if (getLexer().is(AsmToken::Eof)) {
4205 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004206 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004207 }
4208
Rafael Espindola34b9c512012-06-03 23:57:14 +00004209 if (Lexer.is(AsmToken::Identifier) &&
4210 (getTok().getIdentifier() == ".rept")) {
4211 ++NestLevel;
4212 }
4213
4214 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004215 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004216 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004217 EndToken = getTok();
4218 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004219 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4220 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004221 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004222 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004223 break;
4224 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004225 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004226 }
4227
Rafael Espindola34b9c512012-06-03 23:57:14 +00004228 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004229 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004230 }
4231
4232 const char *BodyStart = StartToken.getLoc().getPointer();
4233 const char *BodyEnd = EndToken.getLoc().getPointer();
4234 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4235
Rafael Espindola34b9c512012-06-03 23:57:14 +00004236 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004237 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004238 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004239}
4240
Jim Grosbach4b905842013-09-20 23:08:21 +00004241void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004242 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004243 OS << ".endr\n";
4244
4245 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004246 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004247
Rafael Espindola34b9c512012-06-03 23:57:14 +00004248 // Create the macro instantiation object and add to the current macro
4249 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004250 MacroInstantiation *MI = new MacroInstantiation(
4251 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004252 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004253
Rafael Espindola34b9c512012-06-03 23:57:14 +00004254 // Jump to the macro instantiation and prime the lexer.
4255 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4256 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4257 Lex();
4258}
4259
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004260/// parseDirectiveRept
4261/// ::= .rep | .rept count
4262bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004263 const MCExpr *CountExpr;
4264 SMLoc CountLoc = getTok().getLoc();
4265 if (parseExpression(CountExpr))
4266 return true;
4267
Rafael Espindola34b9c512012-06-03 23:57:14 +00004268 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004269 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4270 eatToEndOfStatement();
4271 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4272 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004273
4274 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004275 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004276
4277 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004278 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004279
4280 // Eat the end of statement.
4281 Lex();
4282
4283 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004284 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004285 if (!M)
4286 return true;
4287
4288 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4289 // to hold the macro body with substitutions.
4290 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004291 raw_svector_ostream OS(Buf);
4292 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004293 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004294 return true;
4295 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004296 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004297
4298 return false;
4299}
4300
Jim Grosbach4b905842013-09-20 23:08:21 +00004301/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004302/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004303bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004304 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004305
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004306 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004307 return TokError("expected identifier in '.irp' directive");
4308
Rafael Espindola768b41c2012-06-15 14:02:34 +00004309 if (Lexer.isNot(AsmToken::Comma))
4310 return TokError("expected comma in '.irp' directive");
4311
4312 Lex();
4313
Eli Bendersky38274122013-01-14 23:22:36 +00004314 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004315 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004316 return true;
4317
4318 // Eat the end of statement.
4319 Lex();
4320
4321 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004322 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004323 if (!M)
4324 return true;
4325
4326 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4327 // to hold the macro body with substitutions.
4328 SmallString<256> Buf;
4329 raw_svector_ostream OS(Buf);
4330
Eli Bendersky38274122013-01-14 23:22:36 +00004331 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004332 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004333 return true;
4334 }
4335
Jim Grosbach4b905842013-09-20 23:08:21 +00004336 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004337
4338 return false;
4339}
4340
Jim Grosbach4b905842013-09-20 23:08:21 +00004341/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004342/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004343bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004344 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004345
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004346 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004347 return TokError("expected identifier in '.irpc' directive");
4348
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004349 if (Lexer.isNot(AsmToken::Comma))
4350 return TokError("expected comma in '.irpc' directive");
4351
4352 Lex();
4353
Eli Bendersky38274122013-01-14 23:22:36 +00004354 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004355 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004356 return true;
4357
4358 if (A.size() != 1 || A.front().size() != 1)
4359 return TokError("unexpected token in '.irpc' directive");
4360
4361 // Eat the end of statement.
4362 Lex();
4363
4364 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004365 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004366 if (!M)
4367 return true;
4368
4369 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4370 // to hold the macro body with substitutions.
4371 SmallString<256> Buf;
4372 raw_svector_ostream OS(Buf);
4373
4374 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004375 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004376 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004377 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004378
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004379 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004380 return true;
4381 }
4382
Jim Grosbach4b905842013-09-20 23:08:21 +00004383 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004384
4385 return false;
4386}
4387
Jim Grosbach4b905842013-09-20 23:08:21 +00004388bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004389 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004390 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004391
4392 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004393 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004394 assert(getLexer().is(AsmToken::EndOfStatement));
4395
Jim Grosbach4b905842013-09-20 23:08:21 +00004396 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004397 return false;
4398}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004399
Jim Grosbach4b905842013-09-20 23:08:21 +00004400bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004401 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004402 const MCExpr *Value;
4403 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004404 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004405 return true;
4406 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4407 if (!MCE)
4408 return Error(ExprLoc, "unexpected expression in _emit");
4409 uint64_t IntValue = MCE->getValue();
4410 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4411 return Error(ExprLoc, "literal value out of range for directive");
4412
Chad Rosierc7f552c2013-02-12 21:33:51 +00004413 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4414 return false;
4415}
4416
Jim Grosbach4b905842013-09-20 23:08:21 +00004417bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004418 const MCExpr *Value;
4419 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004420 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004421 return true;
4422 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4423 if (!MCE)
4424 return Error(ExprLoc, "unexpected expression in align");
4425 uint64_t IntValue = MCE->getValue();
4426 if (!isPowerOf2_64(IntValue))
4427 return Error(ExprLoc, "literal value not a power of two greater then zero");
4428
Jim Grosbach4b905842013-09-20 23:08:21 +00004429 Info.AsmRewrites->push_back(
4430 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004431 return false;
4432}
4433
Chad Rosierf43fcf52013-02-13 21:27:17 +00004434// We are comparing pointers, but the pointers are relative to a single string.
4435// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004436static int rewritesSort(const AsmRewrite *AsmRewriteA,
4437 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004438 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4439 return -1;
4440 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4441 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004442
Chad Rosierfce4fab2013-04-08 17:43:47 +00004443 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4444 // rewrite to the same location. Make sure the SizeDirective rewrite is
4445 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4446 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004447 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4448 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004449 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004450
Jim Grosbach4b905842013-09-20 23:08:21 +00004451 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4452 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004453 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004454 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004455}
4456
Jim Grosbach4b905842013-09-20 23:08:21 +00004457bool AsmParser::parseMSInlineAsm(
4458 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4459 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4460 SmallVectorImpl<std::string> &Constraints,
4461 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4462 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004463 SmallVector<void *, 4> InputDecls;
4464 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004465 SmallVector<bool, 4> InputDeclsAddressOf;
4466 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004467 SmallVector<std::string, 4> InputConstraints;
4468 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004469 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004470
Benjamin Kramer1a136112013-02-15 20:37:21 +00004471 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004472
4473 // Prime the lexer.
4474 Lex();
4475
4476 // While we have input, parse each statement.
4477 unsigned InputIdx = 0;
4478 unsigned OutputIdx = 0;
4479 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004480 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004481 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004482 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004483
Chad Rosier149e8e02012-12-12 22:45:52 +00004484 if (Info.ParseError)
4485 return true;
4486
Benjamin Kramer1a136112013-02-15 20:37:21 +00004487 if (Info.Opcode == ~0U)
4488 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004489
Benjamin Kramer1a136112013-02-15 20:37:21 +00004490 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004491
Benjamin Kramer1a136112013-02-15 20:37:21 +00004492 // Build the list of clobbers, outputs and inputs.
4493 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004494 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004495
Benjamin Kramer1a136112013-02-15 20:37:21 +00004496 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004497 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004498 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004499
Benjamin Kramer1a136112013-02-15 20:37:21 +00004500 // Register operand.
David Blaikie960ea3f2014-06-08 16:18:35 +00004501 if (Operand.isReg() && !Operand.needAddressOf()) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004502 unsigned NumDefs = Desc.getNumDefs();
4503 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004504 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4505 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004506 continue;
4507 }
4508
4509 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004510 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004511 if (SymName.empty())
4512 continue;
4513
David Blaikie960ea3f2014-06-08 16:18:35 +00004514 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004515 if (!OpDecl)
4516 continue;
4517
4518 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004519 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004520 if (isOutput) {
4521 ++InputIdx;
4522 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004523 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4524 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004525 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004526 } else {
4527 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004528 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4529 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004530 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004531 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004532 }
Reid Kleckneree088972013-12-10 18:27:32 +00004533
4534 // Consider implicit defs to be clobbers. Think of cpuid and push.
4535 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4536 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4537 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004538 }
4539
4540 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004541 NumOutputs = OutputDecls.size();
4542 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004543
4544 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004545 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4546 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4547 ClobberRegs.end());
4548 Clobbers.assign(ClobberRegs.size(), std::string());
4549 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4550 raw_string_ostream OS(Clobbers[I]);
4551 IP->printRegName(OS, ClobberRegs[I]);
4552 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004553
4554 // Merge the various outputs and inputs. Output are expected first.
4555 if (NumOutputs || NumInputs) {
4556 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004557 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004558 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004559 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004560 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004561 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004562 }
4563 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004564 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004565 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004566 }
4567 }
4568
4569 // Build the IR assembly string.
4570 std::string AsmStringIR;
4571 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004572 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4573 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004574 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004575 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4576 E = AsmStrRewrites.end();
4577 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004578 AsmRewriteKind Kind = (*I).Kind;
4579 if (Kind == AOK_Delete)
4580 continue;
4581
Chad Rosier8bce6642012-10-18 15:49:34 +00004582 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004583 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004584
Chad Rosier120eefd2013-03-19 17:32:17 +00004585 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004586 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004587 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004588 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004589
Chad Rosier37e755c2012-10-23 17:43:43 +00004590 // Skip the original expression.
4591 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004592 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004593 continue;
4594 }
4595
Chad Rosierff10ed12013-04-12 16:26:42 +00004596 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004597 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004598 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004599 default:
4600 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004601 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004602 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004603 break;
4604 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004605 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004606 break;
4607 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004608 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004609 break;
4610 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004611 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004612 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004613 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004614 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004615 default: break;
4616 case 8: OS << "byte ptr "; break;
4617 case 16: OS << "word ptr "; break;
4618 case 32: OS << "dword ptr "; break;
4619 case 64: OS << "qword ptr "; break;
4620 case 80: OS << "xword ptr "; break;
4621 case 128: OS << "xmmword ptr "; break;
4622 case 256: OS << "ymmword ptr "; break;
4623 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004624 break;
4625 case AOK_Emit:
4626 OS << ".byte";
4627 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004628 case AOK_Align: {
4629 unsigned Val = (*I).Val;
4630 OS << ".align " << Val;
4631
4632 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004633 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004634 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4635 break;
4636 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004637 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004638 // Insert the dot if the user omitted it.
4639 OS.flush();
4640 if (AsmStringIR.back() != '.')
4641 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004642 OS << (*I).Val;
4643 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004644 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004645
Chad Rosier8bce6642012-10-18 15:49:34 +00004646 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004647 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004648 }
4649
4650 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004651 if (AsmStart != AsmEnd)
4652 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004653
4654 AsmString = OS.str();
4655 return false;
4656}
4657
Daniel Dunbar01e36072010-07-17 02:26:10 +00004658/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004659MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4660 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004661 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004662}