blob: 633d101248161e24c7b92e44ee3f8e6bf65b9ba8 [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.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
106
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 ~ParseStatementInfo() {
120 // Free any parsed operands.
121 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
122 delete ParsedOperands[i];
123 ParsedOperands.clear();
124 }
125};
126
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127/// \brief The concrete assembly parser instance.
128class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000129 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
130 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131private:
132 AsmLexer Lexer;
133 MCContext &Ctx;
134 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000135 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000136 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000137 SourceMgr::DiagHandlerTy SavedDiagHandler;
138 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000140
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000141 /// This is the current buffer index we're lexing from as managed by the
142 /// SourceMgr object.
143 int CurBuffer;
144
145 AsmCond TheCondState;
146 std::vector<AsmCond> TheCondStack;
147
Jim Grosbach4b905842013-09-20 23:08:21 +0000148 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000149 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000150 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000151 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000154 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000155
Jim Grosbach4b905842013-09-20 23:08:21 +0000156 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000157 std::vector<MacroInstantiation*> ActiveMacros;
158
Jim Grosbach4b905842013-09-20 23:08:21 +0000159 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000160 std::deque<MCAsmMacro> MacroLikeBodies;
161
Daniel Dunbar828984f2010-07-18 18:38:02 +0000162 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000163 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000164
Daniel Dunbar43325c42010-09-09 22:42:56 +0000165 /// Flag tracking whether any errors have been encountered.
166 unsigned HadError : 1;
167
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000168 /// The values from the last parsed cpp hash file line comment if any.
169 StringRef CppHashFilename;
170 int64_t CppHashLineNumber;
171 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000172 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000173 /// When generating dwarf for assembly source files we need to calculate the
174 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000175 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000176 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
177 SMLoc LastQueryIDLoc;
178 int LastQueryBuffer;
179 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000180
Devang Patela173ee52012-01-31 18:14:05 +0000181 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
182 unsigned AssemblerDialect;
183
Jim Grosbach4b905842013-09-20 23:08:21 +0000184 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000185 bool IsDarwin;
186
Jim Grosbach4b905842013-09-20 23:08:21 +0000187 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000188 bool ParsingInlineAsm;
189
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000191 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000192 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000193 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194
Craig Topper59be68f2014-03-08 07:14:16 +0000195 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000196
Craig Topper59be68f2014-03-08 07:14:16 +0000197 void addDirectiveHandler(StringRef Directive,
198 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000199 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000200 }
201
202public:
203 /// @name MCAsmParser Interface
204 /// {
205
Craig Topper59be68f2014-03-08 07:14:16 +0000206 SourceMgr &getSourceManager() override { return SrcMgr; }
207 MCAsmLexer &getLexer() override { return Lexer; }
208 MCContext &getContext() override { return Ctx; }
209 MCStreamer &getStreamer() override { return Out; }
210 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000211 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000212 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000213 else
214 return AssemblerDialect;
215 }
Craig Topper59be68f2014-03-08 07:14:16 +0000216 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000217 AssemblerDialect = i;
218 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 void Note(SMLoc L, const Twine &Msg,
221 ArrayRef<SMRange> Ranges = None) override;
222 bool Warning(SMLoc L, const Twine &Msg,
223 ArrayRef<SMRange> Ranges = None) override;
224 bool Error(SMLoc L, const Twine &Msg,
225 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000226
Craig Topper59be68f2014-03-08 07:14:16 +0000227 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000228
Craig Topper59be68f2014-03-08 07:14:16 +0000229 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
230 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000231
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000234 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000235 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000236 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000237 const MCInstrInfo *MII, const MCInstPrinter *IP,
238 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000239
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000240 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
243 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
244 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000247 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000248 bool parseIdentifier(StringRef &Res) override;
249 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000250
Craig Topper59be68f2014-03-08 07:14:16 +0000251 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252 /// }
253
254private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000255
Jim Grosbach4b905842013-09-20 23:08:21 +0000256 bool parseStatement(ParseStatementInfo &Info);
257 void eatToEndOfLine();
258 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000259
Jim Grosbach4b905842013-09-20 23:08:21 +0000260 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000262 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000263 ArrayRef<MCAsmMacroParameter> Parameters,
264 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000265 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000266
Eli Benderskya313ae62013-01-16 18:56:50 +0000267 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
273 /// \brief Lookup a previously defined macro.
274 /// \param Name Macro name.
275 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
281 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000282 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000283
284 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000287 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000288 ///
289 /// \param M The macro.
290 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000292
293 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000294 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000295
David Majnemer91fc4c22014-01-29 18:57:46 +0000296 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000297 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000298
299 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 void printMacroInstantiations();
303 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000304 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000305 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000307 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000308
Jim Grosbach4b905842013-09-20 23:08:21 +0000309 /// \brief Enter the specified file. This returns true on failure.
310 bool enterIncludeFile(const std::string &Filename);
311
312 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000313 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000314 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000315
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000316 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000317 /// current token is not set; clients should ensure Lex() is called
318 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000319 ///
320 /// \param InBuffer If not -1, should be the known buffer id that contains the
321 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000323
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000324 /// \brief Parse up to the end of statement and a return the contents from the
325 /// current token until the end of the statement; the current token on exit
326 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000327 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000328
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000329 /// \brief Parse until the end of a statement or a comma is encountered,
330 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000334 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
337 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
338 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000339
Jim Grosbach4b905842013-09-20 23:08:21 +0000340 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000341
Eli Bendersky17233942013-01-15 22:59:42 +0000342 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000343 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_NO_DIRECTIVE, // Placeholder
345 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000346 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
347 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000348 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000350 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000351 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
352 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
353 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
354 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000355 DK_IF, DK_IFNE, DK_IFB, DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF,
356 DK_IFNDEF, DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000357 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
358 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
359 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
360 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
361 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
362 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000363 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000364 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000366 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000367 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000368 };
369
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000371 /// directives parsed by this class.
372 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000373
374 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
376 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000377 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
379 bool parseDirectiveFill(); // ".fill"
380 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000381 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
383 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000386
Eli Bendersky17233942013-01-15 22:59:42 +0000387 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveFile(SMLoc DirectiveLoc);
389 bool parseDirectiveLine();
390 bool parseDirectiveLoc();
391 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000392
393 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000395 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000396 bool parseDirectiveCFISections();
397 bool parseDirectiveCFIStartProc();
398 bool parseDirectiveCFIEndProc();
399 bool parseDirectiveCFIDefCfaOffset();
400 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIAdjustCfaOffset();
402 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
406 bool parseDirectiveCFIRememberState();
407 bool parseDirectiveCFIRestoreState();
408 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
409 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
410 bool parseDirectiveCFIEscape();
411 bool parseDirectiveCFISignalFrame();
412 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
414 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
416 bool parseDirectiveEndMacro(StringRef Directive);
417 bool parseDirectiveMacro(SMLoc DirectiveLoc);
418 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000419
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000426
Eli Bendersky17233942013-01-15 22:59:42 +0000427 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000429
430 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveAbort(); // ".abort"
440 bool parseDirectiveInclude(); // ".include"
441 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000442
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +0000443 // ".if" or ".ifne"
444 bool parseDirectiveIf(SMLoc DirectiveLoc);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000445 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000447 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000449 // ".ifeqs"
450 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000451 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
453 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
454 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
455 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000456 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000457
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000459 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000460
Rafael Espindola34b9c512012-06-03 23:57:14 +0000461 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
463 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000464 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000465 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000466 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
467 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
468 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000469
Chad Rosierc7f552c2013-02-12 21:33:51 +0000470 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000472 size_t Len);
473
474 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000475 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000476
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000477 // "end"
478 bool parseDirectiveEnd(SMLoc DirectiveLoc);
479
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000480 // ".err" or ".error"
481 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000482
Eli Bendersky17233942013-01-15 22:59:42 +0000483 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000484};
Daniel Dunbar86033402010-07-12 17:54:38 +0000485}
486
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000487namespace llvm {
488
489extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000490extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000491extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000492
493}
494
Chris Lattnerc35681b2010-01-19 19:46:13 +0000495enum { DEFAULT_ADDRSPACE = 0 };
496
Jim Grosbach4b905842013-09-20 23:08:21 +0000497AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
498 const MCAsmInfo &_MAI)
499 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Craig Topper353eda42014-04-24 06:44:33 +0000500 PlatformParser(nullptr), CurBuffer(0), MacrosEnabledFlag(true),
Jim Grosbach4b905842013-09-20 23:08:21 +0000501 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
502 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000503 // Save the old handler.
504 SavedDiagHandler = SrcMgr.getDiagHandler();
505 SavedDiagContext = SrcMgr.getDiagContext();
506 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000507 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000508 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000509
Daniel Dunbarc5011082010-07-12 18:12:02 +0000510 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
512 case MCObjectFileInfo::IsCOFF:
513 PlatformParser = createCOFFAsmParser();
514 PlatformParser->Initialize(*this);
515 break;
516 case MCObjectFileInfo::IsMachO:
517 PlatformParser = createDarwinAsmParser();
518 PlatformParser->Initialize(*this);
519 IsDarwin = true;
520 break;
521 case MCObjectFileInfo::IsELF:
522 PlatformParser = createELFAsmParser();
523 PlatformParser->Initialize(*this);
524 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000525 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000526
Eli Bendersky17233942013-01-15 22:59:42 +0000527 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000528}
529
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000530AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000531 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
532
533 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000534 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
535 ie = MacroMap.end();
536 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000537 delete it->getValue();
538
Daniel Dunbarc5011082010-07-12 18:12:02 +0000539 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000540}
541
Jim Grosbach4b905842013-09-20 23:08:21 +0000542void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000543 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000544 for (std::vector<MacroInstantiation *>::const_reverse_iterator
545 it = ActiveMacros.rbegin(),
546 ie = ActiveMacros.rend();
547 it != ie; ++it)
548 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000549 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000550}
551
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000552void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
553 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
554 printMacroInstantiations();
555}
556
Chris Lattnera3a06812011-10-16 04:47:35 +0000557bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000558 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000559 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000560 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
561 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000562 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000563}
564
Chris Lattnera3a06812011-10-16 04:47:35 +0000565bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000566 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000567 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
568 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000569 return true;
570}
571
Jim Grosbach4b905842013-09-20 23:08:21 +0000572bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000573 std::string IncludedFile;
574 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 if (NewBuf == -1)
576 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000577
Sean Callanan7a77eae2010-01-21 00:19:58 +0000578 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000579
Sean Callanan7a77eae2010-01-21 00:19:58 +0000580 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000581
Sean Callanan7a77eae2010-01-21 00:19:58 +0000582 return false;
583}
Daniel Dunbar43235712010-07-18 18:54:11 +0000584
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000585/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000586/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000588bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000589 std::string IncludedFile;
590 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
591 if (NewBuf == -1)
592 return true;
593
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000594 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000595 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000596 return false;
597}
598
Jim Grosbach4b905842013-09-20 23:08:21 +0000599void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000600 if (InBuffer != -1) {
601 CurBuffer = InBuffer;
602 } else {
603 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
604 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000605 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
606}
607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608const AsmToken &AsmParser::Lex() {
609 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000610
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Eof)) {
612 // If this is the end of an included file, pop the parent file off the
613 // include stack.
614 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
615 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000616 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000617 tok = &Lexer.Lex();
618 }
619 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000620
Sean Callanan7a77eae2010-01-21 00:19:58 +0000621 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000622 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000623
Sean Callanan7a77eae2010-01-21 00:19:58 +0000624 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000625}
626
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000627bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000628 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000629 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000630 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000631
Chris Lattner36e02122009-06-21 20:54:55 +0000632 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000633 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000634
635 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000636 AsmCond StartingCondState = TheCondState;
637
Kevin Enderby6469fc22011-11-01 22:27:22 +0000638 // If we are generating dwarf for assembly source files save the initial text
639 // section and generate a .file directive.
640 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000641 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000642 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
643 getStreamer().EmitLabel(SectionStartSym);
644 getContext().setGenDwarfSectionStartSym(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000645 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
646 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000647 }
648
Chris Lattner73f36112009-07-02 21:53:43 +0000649 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000650 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000651 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000652 if (!parseStatement(Info))
653 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000654
Daniel Dunbar43325c42010-09-09 22:42:56 +0000655 // We had an error, validate that one was emitted and recover by skipping to
656 // the next line.
657 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000658 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000659 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000660
661 if (TheCondState.TheCond != StartingCondState.TheCond ||
662 TheCondState.Ignore != StartingCondState.Ignore)
663 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000664
665 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000666 const auto &LineTables = getContext().getMCDwarfLineTables();
667 if (!LineTables.empty()) {
668 unsigned Index = 0;
669 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
670 if (File.Name.empty() && Index != 0)
671 TokError("unassigned file number: " + Twine(Index) +
672 " for .file directives");
673 ++Index;
674 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000675 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000676
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000677 // Check to see that all assembler local symbols were actually defined.
678 // Targets that don't do subsections via symbols may not want this, though,
679 // so conservatively exclude them. Only do this if we're finalizing, though,
680 // as otherwise we won't necessarilly have seen everything yet.
681 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
682 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
683 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000684 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000685 i != e; ++i) {
686 MCSymbol *Sym = i->getValue();
687 // Variable symbols may not be marked as defined, so check those
688 // explicitly. If we know it's a variable, we have a definition for
689 // the purposes of this check.
690 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
691 // FIXME: We would really like to refer back to where the symbol was
692 // first referenced for a source location. We need to add something
693 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000694 printMessage(
695 getLexer().getLoc(), SourceMgr::DK_Error,
696 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000697 }
698 }
699
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000700 // Finalize the output stream if there are no errors and if the client wants
701 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000702 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000703 Out.Finish();
704
Chris Lattner73f36112009-07-02 21:53:43 +0000705 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000706}
707
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000708void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000709 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000710 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000711 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000712 }
713}
714
Jim Grosbach4b905842013-09-20 23:08:21 +0000715/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000716void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000717 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000718 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000719
Chris Lattnere5074c42009-06-22 01:29:09 +0000720 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000721 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000722 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000723}
724
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000725StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000726 const char *Start = getTok().getLoc().getPointer();
727
Jim Grosbach4b905842013-09-20 23:08:21 +0000728 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000729 Lex();
730
731 const char *End = getTok().getLoc().getPointer();
732 return StringRef(Start, End - Start);
733}
Chris Lattner78db3622009-06-22 05:51:26 +0000734
Jim Grosbach4b905842013-09-20 23:08:21 +0000735StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000736 const char *Start = getTok().getLoc().getPointer();
737
738 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000739 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000740 Lex();
741
742 const char *End = getTok().getLoc().getPointer();
743 return StringRef(Start, End - Start);
744}
745
Jim Grosbach4b905842013-09-20 23:08:21 +0000746/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000747/// NOTE: This assumes the leading '(' has already been consumed.
748///
749/// parenexpr ::= expr)
750///
Jim Grosbach4b905842013-09-20 23:08:21 +0000751bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
752 if (parseExpression(Res))
753 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000754 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000755 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000756 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000757 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000758 return false;
759}
Chris Lattner78db3622009-06-22 05:51:26 +0000760
Jim Grosbach4b905842013-09-20 23:08:21 +0000761/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000762/// NOTE: This assumes the leading '[' has already been consumed.
763///
764/// bracketexpr ::= expr]
765///
Jim Grosbach4b905842013-09-20 23:08:21 +0000766bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
767 if (parseExpression(Res))
768 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000769 if (Lexer.isNot(AsmToken::RBrac))
770 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000771 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000772 Lex();
773 return false;
774}
775
Jim Grosbach4b905842013-09-20 23:08:21 +0000776/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000777/// primaryexpr ::= (parenexpr
778/// primaryexpr ::= symbol
779/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000780/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000781/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000782bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000783 SMLoc FirstTokenLoc = getLexer().getLoc();
784 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
785 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000786 default:
787 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000788 // If we have an error assume that we've already handled it.
789 case AsmToken::Error:
790 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000791 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000792 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000793 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000794 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000795 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000796 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000797 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000798 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000799 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000800 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000801 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000802 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000803 if (FirstTokenKind == AsmToken::Dollar) {
804 if (Lexer.getMAI().getDollarIsPC()) {
805 // This is a '$' reference, which references the current PC. Emit a
806 // temporary label to the streamer and refer to it.
807 MCSymbol *Sym = Ctx.CreateTempSymbol();
808 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000809 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
810 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000811 EndLoc = FirstTokenLoc;
812 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000813 }
814 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000815 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000816 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000817 // Parse symbol variant
818 std::pair<StringRef, StringRef> Split;
819 if (!MAI.useParensForSymbolVariant()) {
820 Split = Identifier.split('@');
821 } else if (Lexer.is(AsmToken::LParen)) {
822 Lexer.Lex(); // eat (
823 StringRef VName;
824 parseIdentifier(VName);
825 if (Lexer.isNot(AsmToken::RParen)) {
826 return Error(Lexer.getTok().getLoc(),
827 "unexpected token in variant, expected ')'");
828 }
829 Lexer.Lex(); // eat )
830 Split = std::make_pair(Identifier, VName);
831 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000832
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000833 EndLoc = SMLoc::getFromPointer(Identifier.end());
834
Daniel Dunbard20cda02009-10-16 01:34:54 +0000835 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 StringRef SymbolName = Identifier;
837 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000838
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000839 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000840 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000841 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000842 if (Variant != MCSymbolRefExpr::VK_Invalid) {
843 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000844 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 Variant = MCSymbolRefExpr::VK_None;
846 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000847 return Error(SMLoc::getFromPointer(Split.second.begin()),
848 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000849 }
850 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000851
Hans Wennborgce69d772013-10-18 20:46:28 +0000852 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
853
Daniel Dunbard20cda02009-10-16 01:34:54 +0000854 // If this is an absolute variable reference, substitute it now to preserve
855 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000856 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000857 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000858 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000859
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000860 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 return false;
862 }
863
864 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000865 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000866 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000867 }
David Woodhousef42a6662014-02-01 16:20:54 +0000868 case AsmToken::BigNum:
869 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 case AsmToken::Integer: {
871 SMLoc Loc = getTok().getLoc();
872 int64_t IntVal = getTok().getIntVal();
873 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000874 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000875 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000876 // Look for 'b' or 'f' following an Integer as a directional label
877 if (Lexer.getKind() == AsmToken::Identifier) {
878 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 // Lookup the symbol variant if used.
880 std::pair<StringRef, StringRef> Split = IDVal.split('@');
881 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
882 if (Split.first.size() != IDVal.size()) {
883 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000884 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000886 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000887 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000888 if (IDVal == "f" || IDVal == "b") {
889 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000890 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000891 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000892 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000894 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000895 Lex(); // Eat identifier.
896 }
897 }
Chris Lattner78db3622009-06-22 05:51:26 +0000898 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000899 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000900 case AsmToken::Real: {
901 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000902 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000904 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000905 Lex(); // Eat token.
906 return false;
907 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000908 case AsmToken::Dot: {
909 // This is a '.' reference, which references the current PC. Emit a
910 // temporary label to the streamer and refer to it.
911 MCSymbol *Sym = Ctx.CreateTempSymbol();
912 Out.EmitLabel(Sym);
913 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000914 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000915 Lex(); // Eat identifier.
916 return false;
917 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000918 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000919 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000920 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000921 case AsmToken::LBrac:
922 if (!PlatformParser->HasBracketExpressions())
923 return TokError("brackets expression not supported on this target");
924 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000926 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000927 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000930 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000931 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000936 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000938 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000939 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000940 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000942 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000943 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000944 }
945}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000946
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000947bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000948 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000949 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000950}
951
Daniel Dunbar55f16672010-09-17 02:47:07 +0000952const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000953AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000954 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000955 // Ask the target implementation about this expression first.
956 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
957 if (NewE)
958 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959 // Recurse over the given expression, rebuilding it to apply the given variant
960 // if there is exactly one symbol.
961 switch (E->getKind()) {
962 case MCExpr::Target:
963 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000964 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965
966 case MCExpr::SymbolRef: {
967 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
968
969 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000970 TokError("invalid variant on expression '" + getTok().getIdentifier() +
971 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000972 return E;
973 }
974
975 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
976 }
977
978 case MCExpr::Unary: {
979 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000980 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000982 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
984 }
985
986 case MCExpr::Binary: {
987 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
989 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990
991 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000992 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993
Jim Grosbach4b905842013-09-20 23:08:21 +0000994 if (!LHS)
995 LHS = BE->getLHS();
996 if (!RHS)
997 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998
999 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1000 }
1001 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001002
Craig Toppera2886c22012-02-07 05:05:23 +00001003 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004}
1005
Jim Grosbach4b905842013-09-20 23:08:21 +00001006/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001007///
Jim Grosbachbd164242011-08-20 16:24:13 +00001008/// expr ::= expr &&,|| expr -> lowest.
1009/// expr ::= expr |,^,&,! expr
1010/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1011/// expr ::= expr <<,>> expr
1012/// expr ::= expr +,- expr
1013/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001014/// expr ::= primaryexpr
1015///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001016bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001017 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001018 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001019 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001020 return true;
1021
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 // As a special case, we support 'a op b @ modifier' by rewriting the
1023 // expression to include the modifier. This is inefficient, but in general we
1024 // expect users to use 'a@modifier op b'.
1025 if (Lexer.getKind() == AsmToken::At) {
1026 Lex();
1027
1028 if (Lexer.isNot(AsmToken::Identifier))
1029 return TokError("unexpected symbol modifier following '@'");
1030
1031 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001032 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 if (Variant == MCSymbolRefExpr::VK_Invalid)
1034 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1035
Jim Grosbach4b905842013-09-20 23:08:21 +00001036 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 if (!ModifiedRes) {
1038 return TokError("invalid modifier '" + getTok().getIdentifier() +
1039 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001041
Daniel Dunbar55f16672010-09-17 02:47:07 +00001042 Res = ModifiedRes;
1043 Lex();
1044 }
1045
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001046 // Try to constant fold it up front, if possible.
1047 int64_t Value;
1048 if (Res->EvaluateAsAbsolute(Value))
1049 Res = MCConstantExpr::Create(Value, getContext());
1050
1051 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001052}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001053
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001054bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001055 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001056 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001057}
1058
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001059bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001060 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001061
Daniel Dunbar75630b32009-06-30 02:10:03 +00001062 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001064 return true;
1065
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001066 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001067 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068
1069 return false;
1070}
1071
Michael J. Spencer530ce852010-10-09 11:00:50 +00001072static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001073 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001074 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001075 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001076 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001077
Jim Grosbach4b905842013-09-20 23:08:21 +00001078 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001079 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001080 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001081 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001082 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001083 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001084 return 1;
1085
Jim Grosbach4b905842013-09-20 23:08:21 +00001086 // Low Precedence: |, &, ^
1087 //
1088 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001092 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001095 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001098
Jim Grosbach4b905842013-09-20 23:08:21 +00001099 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 case AsmToken::EqualEqual:
1101 Kind = MCBinaryExpr::EQ;
1102 return 3;
1103 case AsmToken::ExclaimEqual:
1104 case AsmToken::LessGreater:
1105 Kind = MCBinaryExpr::NE;
1106 return 3;
1107 case AsmToken::Less:
1108 Kind = MCBinaryExpr::LT;
1109 return 3;
1110 case AsmToken::LessEqual:
1111 Kind = MCBinaryExpr::LTE;
1112 return 3;
1113 case AsmToken::Greater:
1114 Kind = MCBinaryExpr::GT;
1115 return 3;
1116 case AsmToken::GreaterEqual:
1117 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001118 return 3;
1119
Jim Grosbach4b905842013-09-20 23:08:21 +00001120 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001121 case AsmToken::LessLess:
1122 Kind = MCBinaryExpr::Shl;
1123 return 4;
1124 case AsmToken::GreaterGreater:
1125 Kind = MCBinaryExpr::Shr;
1126 return 4;
1127
Jim Grosbach4b905842013-09-20 23:08:21 +00001128 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001129 case AsmToken::Plus:
1130 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001132 case AsmToken::Minus:
1133 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001135
Jim Grosbach4b905842013-09-20 23:08:21 +00001136 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001137 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001138 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001140 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001141 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001143 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001144 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001146 }
1147}
1148
Jim Grosbach4b905842013-09-20 23:08:21 +00001149/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001150/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001151bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001152 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001154 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001155 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001156
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001157 // If the next token is lower precedence than we are allowed to eat, return
1158 // successfully with what we ate already.
1159 if (TokPrec < Precedence)
1160 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Sean Callanan686ed8d2010-01-19 20:22:31 +00001162 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001163
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001165 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001166 if (parsePrimaryExpr(RHS, EndLoc))
1167 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001168
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169 // If BinOp binds less tightly with RHS than the operator after RHS, let
1170 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001171 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001172 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001173 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1174 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001175
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001176 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001177 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178 }
1179}
1180
Chris Lattner36e02122009-06-21 20:54:55 +00001181/// ParseStatement:
1182/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001183/// ::= Label* Directive ...Operands... EndOfStatement
1184/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001185bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001187 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001188 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001189 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001190 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001191
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001193 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001194 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001195 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001196 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001197 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001198 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001199 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001200
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001201 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001202 if (Lexer.is(AsmToken::Integer)) {
1203 LocalLabelVal = getTok().getIntVal();
1204 if (LocalLabelVal < 0) {
1205 if (!TheCondState.Ignore)
1206 return TokError("unexpected token at start of statement");
1207 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001208 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 IDVal = getTok().getString();
1210 Lex(); // Consume the integer token to be used as an identifier token.
1211 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001212 if (!TheCondState.Ignore)
1213 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001214 }
1215 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001216 } else if (Lexer.is(AsmToken::Dot)) {
1217 // Treat '.' as a valid identifier in this context.
1218 Lex();
1219 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001220 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001221 if (!TheCondState.Ignore)
1222 return TokError("unexpected token at start of statement");
1223 IDVal = "";
1224 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001225
Chris Lattner926885c2010-04-17 18:14:27 +00001226 // Handle conditional assembly here before checking for skipping. We
1227 // have to do this so that .endif isn't skipped in a ".if 0" block for
1228 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001229 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 DirectiveKindMap.find(IDVal);
1231 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1232 ? DK_NO_DIRECTIVE
1233 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001234 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001235 default:
1236 break;
1237 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001238 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001239 return parseDirectiveIf(IDLoc);
1240 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
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003801/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003802/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003803bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003804 TheCondStack.push_back(TheCondState);
3805 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003806 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003807 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003808 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003809 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003810 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003811 return true;
3812
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003813 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003814 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003815
Sean Callanan686ed8d2010-01-19 20:22:31 +00003816 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003817
3818 TheCondState.CondMet = ExprValue;
3819 TheCondState.Ignore = !TheCondState.CondMet;
3820 }
3821
3822 return false;
3823}
3824
Jim Grosbach4b905842013-09-20 23:08:21 +00003825/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003826/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003827bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003828 TheCondStack.push_back(TheCondState);
3829 TheCondState.TheCond = AsmCond::IfCond;
3830
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003831 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003832 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003833 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003834 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003835
3836 if (getLexer().isNot(AsmToken::EndOfStatement))
3837 return TokError("unexpected token in '.ifb' directive");
3838
3839 Lex();
3840
3841 TheCondState.CondMet = ExpectBlank == Str.empty();
3842 TheCondState.Ignore = !TheCondState.CondMet;
3843 }
3844
3845 return false;
3846}
3847
Jim Grosbach4b905842013-09-20 23:08:21 +00003848/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003849/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003850/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003851bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003852 TheCondStack.push_back(TheCondState);
3853 TheCondState.TheCond = AsmCond::IfCond;
3854
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003855 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003856 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003857 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003858 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003859
3860 if (getLexer().isNot(AsmToken::Comma))
3861 return TokError("unexpected token in '.ifc' directive");
3862
3863 Lex();
3864
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003865 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003866
3867 if (getLexer().isNot(AsmToken::EndOfStatement))
3868 return TokError("unexpected token in '.ifc' directive");
3869
3870 Lex();
3871
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003872 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003873 TheCondState.Ignore = !TheCondState.CondMet;
3874 }
3875
3876 return false;
3877}
3878
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003879/// parseDirectiveIfeqs
3880/// ::= .ifeqs string1, string2
3881bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3882 if (Lexer.isNot(AsmToken::String)) {
3883 TokError("expected string parameter for '.ifeqs' directive");
3884 eatToEndOfStatement();
3885 return true;
3886 }
3887
3888 StringRef String1 = getTok().getStringContents();
3889 Lex();
3890
3891 if (Lexer.isNot(AsmToken::Comma)) {
3892 TokError("expected comma after first string for '.ifeqs' directive");
3893 eatToEndOfStatement();
3894 return true;
3895 }
3896
3897 Lex();
3898
3899 if (Lexer.isNot(AsmToken::String)) {
3900 TokError("expected string parameter for '.ifeqs' directive");
3901 eatToEndOfStatement();
3902 return true;
3903 }
3904
3905 StringRef String2 = getTok().getStringContents();
3906 Lex();
3907
3908 TheCondStack.push_back(TheCondState);
3909 TheCondState.TheCond = AsmCond::IfCond;
3910 TheCondState.CondMet = String1 == String2;
3911 TheCondState.Ignore = !TheCondState.CondMet;
3912
3913 return false;
3914}
3915
Jim Grosbach4b905842013-09-20 23:08:21 +00003916/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003917/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003918bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003919 StringRef Name;
3920 TheCondStack.push_back(TheCondState);
3921 TheCondState.TheCond = AsmCond::IfCond;
3922
3923 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003924 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003925 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003926 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003927 return TokError("expected identifier after '.ifdef'");
3928
3929 Lex();
3930
3931 MCSymbol *Sym = getContext().LookupSymbol(Name);
3932
3933 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003934 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003935 else
Craig Topper353eda42014-04-24 06:44:33 +00003936 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003937 TheCondState.Ignore = !TheCondState.CondMet;
3938 }
3939
3940 return false;
3941}
3942
Jim Grosbach4b905842013-09-20 23:08:21 +00003943/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003944/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003945bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003946 if (TheCondState.TheCond != AsmCond::IfCond &&
3947 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003948 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3949 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003950 TheCondState.TheCond = AsmCond::ElseIfCond;
3951
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003952 bool LastIgnoreState = false;
3953 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003954 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003955 if (LastIgnoreState || TheCondState.CondMet) {
3956 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003957 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003958 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003959 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003960 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003961 return true;
3962
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003963 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003964 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003965
Sean Callanan686ed8d2010-01-19 20:22:31 +00003966 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003967 TheCondState.CondMet = ExprValue;
3968 TheCondState.Ignore = !TheCondState.CondMet;
3969 }
3970
3971 return false;
3972}
3973
Jim Grosbach4b905842013-09-20 23:08:21 +00003974/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003975/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003976bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003977 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003978 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003979
Sean Callanan686ed8d2010-01-19 20:22:31 +00003980 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003981
3982 if (TheCondState.TheCond != AsmCond::IfCond &&
3983 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003984 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3985 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003986 TheCondState.TheCond = AsmCond::ElseCond;
3987 bool LastIgnoreState = false;
3988 if (!TheCondStack.empty())
3989 LastIgnoreState = TheCondStack.back().Ignore;
3990 if (LastIgnoreState || TheCondState.CondMet)
3991 TheCondState.Ignore = true;
3992 else
3993 TheCondState.Ignore = false;
3994
3995 return false;
3996}
3997
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003998/// parseDirectiveEnd
3999/// ::= .end
4000bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4001 if (getLexer().isNot(AsmToken::EndOfStatement))
4002 return TokError("unexpected token in '.end' directive");
4003
4004 Lex();
4005
4006 while (Lexer.isNot(AsmToken::Eof))
4007 Lex();
4008
4009 return false;
4010}
4011
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004012/// parseDirectiveError
4013/// ::= .err
4014/// ::= .error [string]
4015bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4016 if (!TheCondStack.empty()) {
4017 if (TheCondStack.back().Ignore) {
4018 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004019 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004020 }
4021 }
4022
4023 if (!WithMessage)
4024 return Error(L, ".err encountered");
4025
4026 StringRef Message = ".error directive invoked in source file";
4027 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4028 if (Lexer.isNot(AsmToken::String)) {
4029 TokError(".error argument must be a string");
4030 eatToEndOfStatement();
4031 return true;
4032 }
4033
4034 Message = getTok().getStringContents();
4035 Lex();
4036 }
4037
4038 Error(L, Message);
4039 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004040}
4041
Jim Grosbach4b905842013-09-20 23:08:21 +00004042/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004043/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004044bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004045 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004046 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004047
Sean Callanan686ed8d2010-01-19 20:22:31 +00004048 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004049
Jim Grosbach4b905842013-09-20 23:08:21 +00004050 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004051 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4052 ".else");
4053 if (!TheCondStack.empty()) {
4054 TheCondState = TheCondStack.back();
4055 TheCondStack.pop_back();
4056 }
4057
4058 return false;
4059}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004060
Eli Bendersky17233942013-01-15 22:59:42 +00004061void AsmParser::initializeDirectiveKindMap() {
4062 DirectiveKindMap[".set"] = DK_SET;
4063 DirectiveKindMap[".equ"] = DK_EQU;
4064 DirectiveKindMap[".equiv"] = DK_EQUIV;
4065 DirectiveKindMap[".ascii"] = DK_ASCII;
4066 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4067 DirectiveKindMap[".string"] = DK_STRING;
4068 DirectiveKindMap[".byte"] = DK_BYTE;
4069 DirectiveKindMap[".short"] = DK_SHORT;
4070 DirectiveKindMap[".value"] = DK_VALUE;
4071 DirectiveKindMap[".2byte"] = DK_2BYTE;
4072 DirectiveKindMap[".long"] = DK_LONG;
4073 DirectiveKindMap[".int"] = DK_INT;
4074 DirectiveKindMap[".4byte"] = DK_4BYTE;
4075 DirectiveKindMap[".quad"] = DK_QUAD;
4076 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004077 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004078 DirectiveKindMap[".single"] = DK_SINGLE;
4079 DirectiveKindMap[".float"] = DK_FLOAT;
4080 DirectiveKindMap[".double"] = DK_DOUBLE;
4081 DirectiveKindMap[".align"] = DK_ALIGN;
4082 DirectiveKindMap[".align32"] = DK_ALIGN32;
4083 DirectiveKindMap[".balign"] = DK_BALIGN;
4084 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4085 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4086 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4087 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4088 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4089 DirectiveKindMap[".org"] = DK_ORG;
4090 DirectiveKindMap[".fill"] = DK_FILL;
4091 DirectiveKindMap[".zero"] = DK_ZERO;
4092 DirectiveKindMap[".extern"] = DK_EXTERN;
4093 DirectiveKindMap[".globl"] = DK_GLOBL;
4094 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004095 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4096 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4097 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4098 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4099 DirectiveKindMap[".reference"] = DK_REFERENCE;
4100 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4101 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4102 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4103 DirectiveKindMap[".comm"] = DK_COMM;
4104 DirectiveKindMap[".common"] = DK_COMMON;
4105 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4106 DirectiveKindMap[".abort"] = DK_ABORT;
4107 DirectiveKindMap[".include"] = DK_INCLUDE;
4108 DirectiveKindMap[".incbin"] = DK_INCBIN;
4109 DirectiveKindMap[".code16"] = DK_CODE16;
4110 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4111 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004112 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004113 DirectiveKindMap[".irp"] = DK_IRP;
4114 DirectiveKindMap[".irpc"] = DK_IRPC;
4115 DirectiveKindMap[".endr"] = DK_ENDR;
4116 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4117 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4118 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4119 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004120 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004121 DirectiveKindMap[".ifb"] = DK_IFB;
4122 DirectiveKindMap[".ifnb"] = DK_IFNB;
4123 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004124 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004125 DirectiveKindMap[".ifnc"] = DK_IFNC;
4126 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4127 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4128 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4129 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4130 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004131 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004132 DirectiveKindMap[".endif"] = DK_ENDIF;
4133 DirectiveKindMap[".skip"] = DK_SKIP;
4134 DirectiveKindMap[".space"] = DK_SPACE;
4135 DirectiveKindMap[".file"] = DK_FILE;
4136 DirectiveKindMap[".line"] = DK_LINE;
4137 DirectiveKindMap[".loc"] = DK_LOC;
4138 DirectiveKindMap[".stabs"] = DK_STABS;
4139 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4140 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4141 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4142 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4143 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4144 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4145 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4146 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4147 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4148 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4149 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4150 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4151 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4152 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4153 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4154 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4155 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4156 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4157 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4158 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4159 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004160 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004161 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4162 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4163 DirectiveKindMap[".macro"] = DK_MACRO;
4164 DirectiveKindMap[".endm"] = DK_ENDM;
4165 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4166 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004167 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004168 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004169}
4170
Jim Grosbach4b905842013-09-20 23:08:21 +00004171MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004172 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004173
Rafael Espindola34b9c512012-06-03 23:57:14 +00004174 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004175 for (;;) {
4176 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004177 if (getLexer().is(AsmToken::Eof)) {
4178 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004179 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004180 }
4181
Rafael Espindola34b9c512012-06-03 23:57:14 +00004182 if (Lexer.is(AsmToken::Identifier) &&
4183 (getTok().getIdentifier() == ".rept")) {
4184 ++NestLevel;
4185 }
4186
4187 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004188 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004189 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004190 EndToken = getTok();
4191 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004192 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4193 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004194 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004195 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004196 break;
4197 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004198 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004199 }
4200
Rafael Espindola34b9c512012-06-03 23:57:14 +00004201 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004202 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004203 }
4204
4205 const char *BodyStart = StartToken.getLoc().getPointer();
4206 const char *BodyEnd = EndToken.getLoc().getPointer();
4207 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4208
Rafael Espindola34b9c512012-06-03 23:57:14 +00004209 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004210 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004211 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004212}
4213
Jim Grosbach4b905842013-09-20 23:08:21 +00004214void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004215 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004216 OS << ".endr\n";
4217
4218 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004219 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004220
Rafael Espindola34b9c512012-06-03 23:57:14 +00004221 // Create the macro instantiation object and add to the current macro
4222 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004223 MacroInstantiation *MI = new MacroInstantiation(
4224 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004225 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004226
Rafael Espindola34b9c512012-06-03 23:57:14 +00004227 // Jump to the macro instantiation and prime the lexer.
4228 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4229 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4230 Lex();
4231}
4232
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004233/// parseDirectiveRept
4234/// ::= .rep | .rept count
4235bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004236 const MCExpr *CountExpr;
4237 SMLoc CountLoc = getTok().getLoc();
4238 if (parseExpression(CountExpr))
4239 return true;
4240
Rafael Espindola34b9c512012-06-03 23:57:14 +00004241 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004242 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4243 eatToEndOfStatement();
4244 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4245 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004246
4247 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004248 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004249
4250 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004251 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004252
4253 // Eat the end of statement.
4254 Lex();
4255
4256 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004257 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004258 if (!M)
4259 return true;
4260
4261 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4262 // to hold the macro body with substitutions.
4263 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004264 raw_svector_ostream OS(Buf);
4265 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004266 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004267 return true;
4268 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004269 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004270
4271 return false;
4272}
4273
Jim Grosbach4b905842013-09-20 23:08:21 +00004274/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004275/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004276bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004277 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004278
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004279 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004280 return TokError("expected identifier in '.irp' directive");
4281
Rafael Espindola768b41c2012-06-15 14:02:34 +00004282 if (Lexer.isNot(AsmToken::Comma))
4283 return TokError("expected comma in '.irp' directive");
4284
4285 Lex();
4286
Eli Bendersky38274122013-01-14 23:22:36 +00004287 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004288 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004289 return true;
4290
4291 // Eat the end of statement.
4292 Lex();
4293
4294 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004295 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004296 if (!M)
4297 return true;
4298
4299 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4300 // to hold the macro body with substitutions.
4301 SmallString<256> Buf;
4302 raw_svector_ostream OS(Buf);
4303
Eli Bendersky38274122013-01-14 23:22:36 +00004304 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004305 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004306 return true;
4307 }
4308
Jim Grosbach4b905842013-09-20 23:08:21 +00004309 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004310
4311 return false;
4312}
4313
Jim Grosbach4b905842013-09-20 23:08:21 +00004314/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004315/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004316bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004317 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004318
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004319 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004320 return TokError("expected identifier in '.irpc' directive");
4321
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004322 if (Lexer.isNot(AsmToken::Comma))
4323 return TokError("expected comma in '.irpc' directive");
4324
4325 Lex();
4326
Eli Bendersky38274122013-01-14 23:22:36 +00004327 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004328 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004329 return true;
4330
4331 if (A.size() != 1 || A.front().size() != 1)
4332 return TokError("unexpected token in '.irpc' directive");
4333
4334 // Eat the end of statement.
4335 Lex();
4336
4337 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004338 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004339 if (!M)
4340 return true;
4341
4342 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4343 // to hold the macro body with substitutions.
4344 SmallString<256> Buf;
4345 raw_svector_ostream OS(Buf);
4346
4347 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004348 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004349 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004350 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004351
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004352 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004353 return true;
4354 }
4355
Jim Grosbach4b905842013-09-20 23:08:21 +00004356 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004357
4358 return false;
4359}
4360
Jim Grosbach4b905842013-09-20 23:08:21 +00004361bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004362 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004363 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004364
4365 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004366 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004367 assert(getLexer().is(AsmToken::EndOfStatement));
4368
Jim Grosbach4b905842013-09-20 23:08:21 +00004369 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004370 return false;
4371}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004372
Jim Grosbach4b905842013-09-20 23:08:21 +00004373bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004374 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004375 const MCExpr *Value;
4376 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004377 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004378 return true;
4379 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4380 if (!MCE)
4381 return Error(ExprLoc, "unexpected expression in _emit");
4382 uint64_t IntValue = MCE->getValue();
4383 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4384 return Error(ExprLoc, "literal value out of range for directive");
4385
Chad Rosierc7f552c2013-02-12 21:33:51 +00004386 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4387 return false;
4388}
4389
Jim Grosbach4b905842013-09-20 23:08:21 +00004390bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004391 const MCExpr *Value;
4392 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004393 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004394 return true;
4395 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4396 if (!MCE)
4397 return Error(ExprLoc, "unexpected expression in align");
4398 uint64_t IntValue = MCE->getValue();
4399 if (!isPowerOf2_64(IntValue))
4400 return Error(ExprLoc, "literal value not a power of two greater then zero");
4401
Jim Grosbach4b905842013-09-20 23:08:21 +00004402 Info.AsmRewrites->push_back(
4403 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004404 return false;
4405}
4406
Chad Rosierf43fcf52013-02-13 21:27:17 +00004407// We are comparing pointers, but the pointers are relative to a single string.
4408// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004409static int rewritesSort(const AsmRewrite *AsmRewriteA,
4410 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004411 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4412 return -1;
4413 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4414 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004415
Chad Rosierfce4fab2013-04-08 17:43:47 +00004416 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4417 // rewrite to the same location. Make sure the SizeDirective rewrite is
4418 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4419 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004420 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4421 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004422 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004423
Jim Grosbach4b905842013-09-20 23:08:21 +00004424 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4425 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004426 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004427 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004428}
4429
Jim Grosbach4b905842013-09-20 23:08:21 +00004430bool AsmParser::parseMSInlineAsm(
4431 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4432 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4433 SmallVectorImpl<std::string> &Constraints,
4434 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4435 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004436 SmallVector<void *, 4> InputDecls;
4437 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004438 SmallVector<bool, 4> InputDeclsAddressOf;
4439 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004440 SmallVector<std::string, 4> InputConstraints;
4441 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004442 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004443
Benjamin Kramer1a136112013-02-15 20:37:21 +00004444 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004445
4446 // Prime the lexer.
4447 Lex();
4448
4449 // While we have input, parse each statement.
4450 unsigned InputIdx = 0;
4451 unsigned OutputIdx = 0;
4452 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004453 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004454 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004455 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004456
Chad Rosier149e8e02012-12-12 22:45:52 +00004457 if (Info.ParseError)
4458 return true;
4459
Benjamin Kramer1a136112013-02-15 20:37:21 +00004460 if (Info.Opcode == ~0U)
4461 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004462
Benjamin Kramer1a136112013-02-15 20:37:21 +00004463 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004464
Benjamin Kramer1a136112013-02-15 20:37:21 +00004465 // Build the list of clobbers, outputs and inputs.
4466 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4467 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004468
Benjamin Kramer1a136112013-02-15 20:37:21 +00004469 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004470 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004471 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004472
Benjamin Kramer1a136112013-02-15 20:37:21 +00004473 // Register operand.
4474 if (Operand->isReg() && !Operand->needAddressOf()) {
4475 unsigned NumDefs = Desc.getNumDefs();
4476 // Clobber.
4477 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4478 ClobberRegs.push_back(Operand->getReg());
4479 continue;
4480 }
4481
4482 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004483 StringRef SymName = Operand->getSymName();
4484 if (SymName.empty())
4485 continue;
4486
Chad Rosierdba3fe52013-04-22 22:12:12 +00004487 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004488 if (!OpDecl)
4489 continue;
4490
4491 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004492 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004493 if (isOutput) {
4494 ++InputIdx;
4495 OutputDecls.push_back(OpDecl);
4496 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4497 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004498 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004499 } else {
4500 InputDecls.push_back(OpDecl);
4501 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4502 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004503 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004504 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004505 }
Reid Kleckneree088972013-12-10 18:27:32 +00004506
4507 // Consider implicit defs to be clobbers. Think of cpuid and push.
4508 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4509 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4510 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004511 }
4512
4513 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004514 NumOutputs = OutputDecls.size();
4515 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004516
4517 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004518 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4519 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4520 ClobberRegs.end());
4521 Clobbers.assign(ClobberRegs.size(), std::string());
4522 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4523 raw_string_ostream OS(Clobbers[I]);
4524 IP->printRegName(OS, ClobberRegs[I]);
4525 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004526
4527 // Merge the various outputs and inputs. Output are expected first.
4528 if (NumOutputs || NumInputs) {
4529 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004530 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004531 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004532 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004533 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004534 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004535 }
4536 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004537 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004538 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004539 }
4540 }
4541
4542 // Build the IR assembly string.
4543 std::string AsmStringIR;
4544 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004545 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4546 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004547 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004548 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4549 E = AsmStrRewrites.end();
4550 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004551 AsmRewriteKind Kind = (*I).Kind;
4552 if (Kind == AOK_Delete)
4553 continue;
4554
Chad Rosier8bce6642012-10-18 15:49:34 +00004555 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004556 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004557
Chad Rosier120eefd2013-03-19 17:32:17 +00004558 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004559 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004560 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004561 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004562
Chad Rosier37e755c2012-10-23 17:43:43 +00004563 // Skip the original expression.
4564 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004565 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004566 continue;
4567 }
4568
Chad Rosierff10ed12013-04-12 16:26:42 +00004569 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004570 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004571 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004572 default:
4573 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004574 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004575 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004576 break;
4577 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004578 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004579 break;
4580 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004581 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004582 break;
4583 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004584 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004585 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004586 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004588 default: break;
4589 case 8: OS << "byte ptr "; break;
4590 case 16: OS << "word ptr "; break;
4591 case 32: OS << "dword ptr "; break;
4592 case 64: OS << "qword ptr "; break;
4593 case 80: OS << "xword ptr "; break;
4594 case 128: OS << "xmmword ptr "; break;
4595 case 256: OS << "ymmword ptr "; break;
4596 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004597 break;
4598 case AOK_Emit:
4599 OS << ".byte";
4600 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004601 case AOK_Align: {
4602 unsigned Val = (*I).Val;
4603 OS << ".align " << Val;
4604
4605 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004606 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004607 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4608 break;
4609 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004610 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004611 // Insert the dot if the user omitted it.
4612 OS.flush();
4613 if (AsmStringIR.back() != '.')
4614 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004615 OS << (*I).Val;
4616 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004617 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004618
Chad Rosier8bce6642012-10-18 15:49:34 +00004619 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004620 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004621 }
4622
4623 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004624 if (AsmStart != AsmEnd)
4625 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004626
4627 AsmString = OS.str();
4628 return false;
4629}
4630
Daniel Dunbar01e36072010-07-17 02:26:10 +00004631/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004632MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4633 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004634 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004635}