blob: c34fe8a4133fde7d5196bec6cc023fb164766174 [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>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000054/// \brief Helper types for tracking macro definitions.
55typedef std::vector<AsmToken> MCAsmMacroArgument;
56typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000057
58struct MCAsmMacroParameter {
59 StringRef Name;
60 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000061 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000062 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000063
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000064 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000065};
66
Eli Benderskya313ae62013-01-16 18:56:50 +000067typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
68
69struct MCAsmMacro {
70 StringRef Name;
71 StringRef Body;
72 MCAsmMacroParameters Parameters;
73
74public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000075 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000076 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000077};
78
Daniel Dunbar43235712010-07-18 18:54:11 +000079/// \brief Helper class for storing information about an active macro
80/// instantiation.
81struct MacroInstantiation {
82 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000083 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000084
85 /// The macro instantiation with substitutions.
86 MemoryBuffer *Instantiation;
87
88 /// The location of the instantiation.
89 SMLoc InstantiationLoc;
90
Daniel Dunbar40f1d852012-12-01 01:38:48 +000091 /// The buffer where parsing should resume upon instantiation completion.
92 int ExitBuffer;
93
Daniel Dunbar43235712010-07-18 18:54:11 +000094 /// The location where parsing should resume upon instantiation completion.
95 SMLoc ExitLoc;
96
97public:
Eli Bendersky38274122013-01-14 23:22:36 +000098 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000099 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +0000100};
101
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000103 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000104 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
105
Jim Grosbach4b905842013-09-20 23:08:21 +0000106 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 unsigned Opcode;
108
Jim Grosbach4b905842013-09-20 23:08:21 +0000109 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000110 bool ParseError;
111
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112 SmallVectorImpl<AsmRewrite> *AsmRewrites;
113
Chad Rosier149e8e02012-12-12 22:45:52 +0000114 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000115 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000116 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000117
118 ~ParseStatementInfo() {
119 // Free any parsed operands.
120 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
121 delete ParsedOperands[i];
122 ParsedOperands.clear();
123 }
124};
125
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126/// \brief The concrete assembly parser instance.
127class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000128 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
129 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130private:
131 AsmLexer Lexer;
132 MCContext &Ctx;
133 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000134 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000136 SourceMgr::DiagHandlerTy SavedDiagHandler;
137 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000138 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000139
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000140 /// This is the current buffer index we're lexing from as managed by the
141 /// SourceMgr object.
142 int CurBuffer;
143
144 AsmCond TheCondState;
145 std::vector<AsmCond> TheCondStack;
146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000148 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000149 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000150 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000153 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000154
Jim Grosbach4b905842013-09-20 23:08:21 +0000155 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000156 std::vector<MacroInstantiation*> ActiveMacros;
157
Jim Grosbach4b905842013-09-20 23:08:21 +0000158 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000159 std::deque<MCAsmMacro> MacroLikeBodies;
160
Daniel Dunbar828984f2010-07-18 18:38:02 +0000161 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000162 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000163
Daniel Dunbar43325c42010-09-09 22:42:56 +0000164 /// Flag tracking whether any errors have been encountered.
165 unsigned HadError : 1;
166
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000167 /// The values from the last parsed cpp hash file line comment if any.
168 StringRef CppHashFilename;
169 int64_t CppHashLineNumber;
170 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000171 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000172 /// When generating dwarf for assembly source files we need to calculate the
173 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000174 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000175 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
176 SMLoc LastQueryIDLoc;
177 int LastQueryBuffer;
178 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000179
Devang Patela173ee52012-01-31 18:14:05 +0000180 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
181 unsigned AssemblerDialect;
182
Jim Grosbach4b905842013-09-20 23:08:21 +0000183 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000184 bool IsDarwin;
185
Jim Grosbach4b905842013-09-20 23:08:21 +0000186 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000187 bool ParsingInlineAsm;
188
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000190 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000192 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193
Craig Topper59be68f2014-03-08 07:14:16 +0000194 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000195
Craig Topper59be68f2014-03-08 07:14:16 +0000196 void addDirectiveHandler(StringRef Directive,
197 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000198 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000199 }
200
201public:
202 /// @name MCAsmParser Interface
203 /// {
204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 SourceMgr &getSourceManager() override { return SrcMgr; }
206 MCAsmLexer &getLexer() override { return Lexer; }
207 MCContext &getContext() override { return Ctx; }
208 MCStreamer &getStreamer() override { return Out; }
209 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000211 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000212 else
213 return AssemblerDialect;
214 }
Craig Topper59be68f2014-03-08 07:14:16 +0000215 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000216 AssemblerDialect = i;
217 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper59be68f2014-03-08 07:14:16 +0000219 void Note(SMLoc L, const Twine &Msg,
220 ArrayRef<SMRange> Ranges = None) override;
221 bool Warning(SMLoc L, const Twine &Msg,
222 ArrayRef<SMRange> Ranges = None) override;
223 bool Error(SMLoc L, const Twine &Msg,
224 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000225
Craig Topper59be68f2014-03-08 07:14:16 +0000226 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000227
Craig Topper59be68f2014-03-08 07:14:16 +0000228 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
229 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000230
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000231 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000232 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000233 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000234 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000235 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000236 const MCInstrInfo *MII, const MCInstPrinter *IP,
237 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000238
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000239 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
241 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
243 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244
Jim Grosbach4b905842013-09-20 23:08:21 +0000245 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000246 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000247 bool parseIdentifier(StringRef &Res) override;
248 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000249
Craig Topper59be68f2014-03-08 07:14:16 +0000250 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000251 /// }
252
253private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000254
Jim Grosbach4b905842013-09-20 23:08:21 +0000255 bool parseStatement(ParseStatementInfo &Info);
256 void eatToEndOfLine();
257 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000258
Jim Grosbach4b905842013-09-20 23:08:21 +0000259 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000260 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000261 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000262 ArrayRef<MCAsmMacroParameter> Parameters,
263 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000264 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000265
Eli Benderskya313ae62013-01-16 18:56:50 +0000266 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000267 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000268
269 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
272 /// \brief Lookup a previously defined macro.
273 /// \param Name Macro name.
274 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
280 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
283 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000286 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000287 ///
288 /// \param M The macro.
289 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000290 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
David Majnemer91fc4c22014-01-29 18:57:46 +0000295 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000296 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
298 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000300
Jim Grosbach4b905842013-09-20 23:08:21 +0000301 void printMacroInstantiations();
302 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000303 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000304 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000305 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000306 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000307
Jim Grosbach4b905842013-09-20 23:08:21 +0000308 /// \brief Enter the specified file. This returns true on failure.
309 bool enterIncludeFile(const std::string &Filename);
310
311 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000312 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000313 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000314
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000315 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000316 /// current token is not set; clients should ensure Lex() is called
317 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000318 ///
319 /// \param InBuffer If not -1, should be the known buffer id that contains the
320 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000321 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000322
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000323 /// \brief Parse up to the end of statement and a return the contents from the
324 /// current token until the end of the statement; the current token on exit
325 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000326 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000327
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000328 /// \brief Parse until the end of a statement or a comma is encountered,
329 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000331
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000333 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000334
Jim Grosbach4b905842013-09-20 23:08:21 +0000335 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
336 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
337 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000338
Jim Grosbach4b905842013-09-20 23:08:21 +0000339 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000340
Eli Bendersky17233942013-01-15 22:59:42 +0000341 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000342 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000343 DK_NO_DIRECTIVE, // Placeholder
344 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000345 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
346 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000347 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000348 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000349 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000350 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
351 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
352 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
353 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000354 DK_IF, DK_IFNE, DK_IFB, DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF,
355 DK_IFNDEF, DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000356 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
357 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
358 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
359 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
360 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
361 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000362 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000363 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000364 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000365 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000366 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367 };
368
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000370 /// directives parsed by this class.
371 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000372
373 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
375 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000376 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
378 bool parseDirectiveFill(); // ".fill"
379 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000380 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000381 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
382 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000383 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000385
Eli Bendersky17233942013-01-15 22:59:42 +0000386 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000387 bool parseDirectiveFile(SMLoc DirectiveLoc);
388 bool parseDirectiveLine();
389 bool parseDirectiveLoc();
390 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000391
392 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000393 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000394 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000395 bool parseDirectiveCFISections();
396 bool parseDirectiveCFIStartProc();
397 bool parseDirectiveCFIEndProc();
398 bool parseDirectiveCFIDefCfaOffset();
399 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIAdjustCfaOffset();
401 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
405 bool parseDirectiveCFIRememberState();
406 bool parseDirectiveCFIRestoreState();
407 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
409 bool parseDirectiveCFIEscape();
410 bool parseDirectiveCFISignalFrame();
411 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000412
413 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
415 bool parseDirectiveEndMacro(StringRef Directive);
416 bool parseDirectiveMacro(SMLoc DirectiveLoc);
417 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000421 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000423 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000425
Eli Bendersky17233942013-01-15 22:59:42 +0000426 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000428
429 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000433 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000437
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveAbort(); // ".abort"
439 bool parseDirectiveInclude(); // ".include"
440 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000441
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +0000442 // ".if" or ".ifne"
443 bool parseDirectiveIf(SMLoc DirectiveLoc);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000444 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000446 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000448 // ".ifeqs"
449 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000450 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
452 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
453 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
454 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000455 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000456
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000458 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000459
Rafael Espindola34b9c512012-06-03 23:57:14 +0000460 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
462 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000463 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000464 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
466 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
467 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000468
Chad Rosierc7f552c2013-02-12 21:33:51 +0000469 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000471 size_t Len);
472
473 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000474 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000475
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000476 // "end"
477 bool parseDirectiveEnd(SMLoc DirectiveLoc);
478
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000479 // ".err" or ".error"
480 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000481
Eli Bendersky17233942013-01-15 22:59:42 +0000482 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000483};
Daniel Dunbar86033402010-07-12 17:54:38 +0000484}
485
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486namespace llvm {
487
488extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000489extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000490extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000491
492}
493
Chris Lattnerc35681b2010-01-19 19:46:13 +0000494enum { DEFAULT_ADDRSPACE = 0 };
495
Jim Grosbach4b905842013-09-20 23:08:21 +0000496AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
497 const MCAsmInfo &_MAI)
498 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
499 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
500 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
501 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000502 // Save the old handler.
503 SavedDiagHandler = SrcMgr.getDiagHandler();
504 SavedDiagContext = SrcMgr.getDiagContext();
505 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000506 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000507 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000508
Daniel Dunbarc5011082010-07-12 18:12:02 +0000509 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000510 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
511 case MCObjectFileInfo::IsCOFF:
512 PlatformParser = createCOFFAsmParser();
513 PlatformParser->Initialize(*this);
514 break;
515 case MCObjectFileInfo::IsMachO:
516 PlatformParser = createDarwinAsmParser();
517 PlatformParser->Initialize(*this);
518 IsDarwin = true;
519 break;
520 case MCObjectFileInfo::IsELF:
521 PlatformParser = createELFAsmParser();
522 PlatformParser->Initialize(*this);
523 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000524 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000525
Eli Bendersky17233942013-01-15 22:59:42 +0000526 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000527}
528
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000529AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000530 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
531
532 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000533 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
534 ie = MacroMap.end();
535 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000536 delete it->getValue();
537
Daniel Dunbarc5011082010-07-12 18:12:02 +0000538 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000539}
540
Jim Grosbach4b905842013-09-20 23:08:21 +0000541void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000542 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000543 for (std::vector<MacroInstantiation *>::const_reverse_iterator
544 it = ActiveMacros.rbegin(),
545 ie = ActiveMacros.rend();
546 it != ie; ++it)
547 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000548 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000549}
550
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000551void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
552 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
553 printMacroInstantiations();
554}
555
Chris Lattnera3a06812011-10-16 04:47:35 +0000556bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000558 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000559 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
560 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000561 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000562}
563
Chris Lattnera3a06812011-10-16 04:47:35 +0000564bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000565 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000566 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
567 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000568 return true;
569}
570
Jim Grosbach4b905842013-09-20 23:08:21 +0000571bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000572 std::string IncludedFile;
573 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 if (NewBuf == -1)
575 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000576
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000578
Sean Callanan7a77eae2010-01-21 00:19:58 +0000579 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000580
Sean Callanan7a77eae2010-01-21 00:19:58 +0000581 return false;
582}
Daniel Dunbar43235712010-07-18 18:54:11 +0000583
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000584/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000585/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000586/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000587bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588 std::string IncludedFile;
589 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
590 if (NewBuf == -1)
591 return true;
592
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000593 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000594 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000595 return false;
596}
597
Jim Grosbach4b905842013-09-20 23:08:21 +0000598void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000599 if (InBuffer != -1) {
600 CurBuffer = InBuffer;
601 } else {
602 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
603 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000604 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
605}
606
Sean Callanan7a77eae2010-01-21 00:19:58 +0000607const AsmToken &AsmParser::Lex() {
608 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000609
Sean Callanan7a77eae2010-01-21 00:19:58 +0000610 if (tok->is(AsmToken::Eof)) {
611 // If this is the end of an included file, pop the parent file off the
612 // include stack.
613 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
614 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000615 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 tok = &Lexer.Lex();
617 }
618 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000619
Sean Callanan7a77eae2010-01-21 00:19:58 +0000620 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000621 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000622
Sean Callanan7a77eae2010-01-21 00:19:58 +0000623 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000624}
625
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000626bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000627 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000628 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000629 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000630
Chris Lattner36e02122009-06-21 20:54:55 +0000631 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000632 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000633
634 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000635 AsmCond StartingCondState = TheCondState;
636
Kevin Enderby6469fc22011-11-01 22:27:22 +0000637 // If we are generating dwarf for assembly source files save the initial text
638 // section and generate a .file directive.
639 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000640 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000641 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
642 getStreamer().EmitLabel(SectionStartSym);
643 getContext().setGenDwarfSectionStartSym(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000644 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
645 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000646 }
647
Chris Lattner73f36112009-07-02 21:53:43 +0000648 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000649 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000650 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000651 if (!parseStatement(Info))
652 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000653
Daniel Dunbar43325c42010-09-09 22:42:56 +0000654 // We had an error, validate that one was emitted and recover by skipping to
655 // the next line.
656 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000657 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000658 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000659
660 if (TheCondState.TheCond != StartingCondState.TheCond ||
661 TheCondState.Ignore != StartingCondState.Ignore)
662 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000663
664 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000665 const auto &LineTables = getContext().getMCDwarfLineTables();
666 if (!LineTables.empty()) {
667 unsigned Index = 0;
668 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
669 if (File.Name.empty() && Index != 0)
670 TokError("unassigned file number: " + Twine(Index) +
671 " for .file directives");
672 ++Index;
673 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000674 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000675
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000676 // Check to see that all assembler local symbols were actually defined.
677 // Targets that don't do subsections via symbols may not want this, though,
678 // so conservatively exclude them. Only do this if we're finalizing, though,
679 // as otherwise we won't necessarilly have seen everything yet.
680 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
681 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
682 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000683 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000684 i != e; ++i) {
685 MCSymbol *Sym = i->getValue();
686 // Variable symbols may not be marked as defined, so check those
687 // explicitly. If we know it's a variable, we have a definition for
688 // the purposes of this check.
689 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
690 // FIXME: We would really like to refer back to where the symbol was
691 // first referenced for a source location. We need to add something
692 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000693 printMessage(
694 getLexer().getLoc(), SourceMgr::DK_Error,
695 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000696 }
697 }
698
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000699 // Finalize the output stream if there are no errors and if the client wants
700 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000701 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000702 Out.Finish();
703
Chris Lattner73f36112009-07-02 21:53:43 +0000704 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000705}
706
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000708 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000709 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000710 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000711 }
712}
713
Jim Grosbach4b905842013-09-20 23:08:21 +0000714/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000715void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000716 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000718
Chris Lattnere5074c42009-06-22 01:29:09 +0000719 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000720 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000721 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000722}
723
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000724StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000725 const char *Start = getTok().getLoc().getPointer();
726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000728 Lex();
729
730 const char *End = getTok().getLoc().getPointer();
731 return StringRef(Start, End - Start);
732}
Chris Lattner78db3622009-06-22 05:51:26 +0000733
Jim Grosbach4b905842013-09-20 23:08:21 +0000734StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 const char *Start = getTok().getLoc().getPointer();
736
737 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000738 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000739 Lex();
740
741 const char *End = getTok().getLoc().getPointer();
742 return StringRef(Start, End - Start);
743}
744
Jim Grosbach4b905842013-09-20 23:08:21 +0000745/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746/// NOTE: This assumes the leading '(' has already been consumed.
747///
748/// parenexpr ::= expr)
749///
Jim Grosbach4b905842013-09-20 23:08:21 +0000750bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
751 if (parseExpression(Res))
752 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000753 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000754 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000755 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000756 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000757 return false;
758}
Chris Lattner78db3622009-06-22 05:51:26 +0000759
Jim Grosbach4b905842013-09-20 23:08:21 +0000760/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000761/// NOTE: This assumes the leading '[' has already been consumed.
762///
763/// bracketexpr ::= expr]
764///
Jim Grosbach4b905842013-09-20 23:08:21 +0000765bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
766 if (parseExpression(Res))
767 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000768 if (Lexer.isNot(AsmToken::RBrac))
769 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000770 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000771 Lex();
772 return false;
773}
774
Jim Grosbach4b905842013-09-20 23:08:21 +0000775/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= (parenexpr
777/// primaryexpr ::= symbol
778/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000779/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000780/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000781bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000782 SMLoc FirstTokenLoc = getLexer().getLoc();
783 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
784 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000785 default:
786 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000787 // If we have an error assume that we've already handled it.
788 case AsmToken::Error:
789 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000790 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000791 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000792 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000793 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000794 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000795 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000797 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000798 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000799 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000800 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000801 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000802 if (FirstTokenKind == AsmToken::Dollar) {
803 if (Lexer.getMAI().getDollarIsPC()) {
804 // This is a '$' reference, which references the current PC. Emit a
805 // temporary label to the streamer and refer to it.
806 MCSymbol *Sym = Ctx.CreateTempSymbol();
807 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000808 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
809 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 EndLoc = FirstTokenLoc;
811 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000812 }
813 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000814 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000815 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000816 // Parse symbol variant
817 std::pair<StringRef, StringRef> Split;
818 if (!MAI.useParensForSymbolVariant()) {
819 Split = Identifier.split('@');
820 } else if (Lexer.is(AsmToken::LParen)) {
821 Lexer.Lex(); // eat (
822 StringRef VName;
823 parseIdentifier(VName);
824 if (Lexer.isNot(AsmToken::RParen)) {
825 return Error(Lexer.getTok().getLoc(),
826 "unexpected token in variant, expected ')'");
827 }
828 Lexer.Lex(); // eat )
829 Split = std::make_pair(Identifier, VName);
830 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000831
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000832 EndLoc = SMLoc::getFromPointer(Identifier.end());
833
Daniel Dunbard20cda02009-10-16 01:34:54 +0000834 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000835 StringRef SymbolName = Identifier;
836 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000837
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000838 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000839 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000840 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000841 if (Variant != MCSymbolRefExpr::VK_Invalid) {
842 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000843 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000844 Variant = MCSymbolRefExpr::VK_None;
845 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000846 return Error(SMLoc::getFromPointer(Split.second.begin()),
847 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000848 }
849 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000850
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
852
Daniel Dunbard20cda02009-10-16 01:34:54 +0000853 // If this is an absolute variable reference, substitute it now to preserve
854 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000855 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000856 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000857 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000858
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000859 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000860 return false;
861 }
862
863 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000864 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000865 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000866 }
David Woodhousef42a6662014-02-01 16:20:54 +0000867 case AsmToken::BigNum:
868 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000869 case AsmToken::Integer: {
870 SMLoc Loc = getTok().getLoc();
871 int64_t IntVal = getTok().getIntVal();
872 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000873 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000874 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000875 // Look for 'b' or 'f' following an Integer as a directional label
876 if (Lexer.getKind() == AsmToken::Identifier) {
877 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000878 // Lookup the symbol variant if used.
879 std::pair<StringRef, StringRef> Split = IDVal.split('@');
880 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
881 if (Split.first.size() != IDVal.size()) {
882 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000883 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000884 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000885 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000886 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000887 if (IDVal == "f" || IDVal == "b") {
888 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000889 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000891 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000892 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000893 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000894 Lex(); // Eat identifier.
895 }
896 }
Chris Lattner78db3622009-06-22 05:51:26 +0000897 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000898 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000899 case AsmToken::Real: {
900 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000901 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000902 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000904 Lex(); // Eat token.
905 return false;
906 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000907 case AsmToken::Dot: {
908 // This is a '.' reference, which references the current PC. Emit a
909 // temporary label to the streamer and refer to it.
910 MCSymbol *Sym = Ctx.CreateTempSymbol();
911 Out.EmitLabel(Sym);
912 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000913 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000914 Lex(); // Eat identifier.
915 return false;
916 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000917 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000918 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000920 case AsmToken::LBrac:
921 if (!PlatformParser->HasBracketExpressions())
922 return TokError("brackets expression not supported on this target");
923 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000924 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000925 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000926 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000927 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000929 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000931 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000932 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000933 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000935 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000937 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000938 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000939 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000941 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000943 }
944}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000945
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000946bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000947 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000948 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000949}
950
Daniel Dunbar55f16672010-09-17 02:47:07 +0000951const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000952AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000953 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000954 // Ask the target implementation about this expression first.
955 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
956 if (NewE)
957 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958 // Recurse over the given expression, rebuilding it to apply the given variant
959 // if there is exactly one symbol.
960 switch (E->getKind()) {
961 case MCExpr::Target:
962 case MCExpr::Constant:
963 return 0;
964
965 case MCExpr::SymbolRef: {
966 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
967
968 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000969 TokError("invalid variant on expression '" + getTok().getIdentifier() +
970 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000971 return E;
972 }
973
974 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
975 }
976
977 case MCExpr::Unary: {
978 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000980 if (!Sub)
981 return 0;
982 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
983 }
984
985 case MCExpr::Binary: {
986 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000987 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
988 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989
990 if (!LHS && !RHS)
991 return 0;
992
Jim Grosbach4b905842013-09-20 23:08:21 +0000993 if (!LHS)
994 LHS = BE->getLHS();
995 if (!RHS)
996 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000997
998 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
999 }
1000 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001001
Craig Toppera2886c22012-02-07 05:05:23 +00001002 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001003}
1004
Jim Grosbach4b905842013-09-20 23:08:21 +00001005/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001006///
Jim Grosbachbd164242011-08-20 16:24:13 +00001007/// expr ::= expr &&,|| expr -> lowest.
1008/// expr ::= expr |,^,&,! expr
1009/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1010/// expr ::= expr <<,>> expr
1011/// expr ::= expr +,- expr
1012/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001013/// expr ::= primaryexpr
1014///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001015bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001016 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001017 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001018 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001019 return true;
1020
Daniel Dunbar55f16672010-09-17 02:47:07 +00001021 // As a special case, we support 'a op b @ modifier' by rewriting the
1022 // expression to include the modifier. This is inefficient, but in general we
1023 // expect users to use 'a@modifier op b'.
1024 if (Lexer.getKind() == AsmToken::At) {
1025 Lex();
1026
1027 if (Lexer.isNot(AsmToken::Identifier))
1028 return TokError("unexpected symbol modifier following '@'");
1029
1030 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001031 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001032 if (Variant == MCSymbolRefExpr::VK_Invalid)
1033 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1034
Jim Grosbach4b905842013-09-20 23:08:21 +00001035 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 if (!ModifiedRes) {
1037 return TokError("invalid modifier '" + getTok().getIdentifier() +
1038 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001040
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 Res = ModifiedRes;
1042 Lex();
1043 }
1044
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001045 // Try to constant fold it up front, if possible.
1046 int64_t Value;
1047 if (Res->EvaluateAsAbsolute(Value))
1048 Res = MCConstantExpr::Create(Value, getContext());
1049
1050 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001051}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001053bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001054 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001055 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001056}
1057
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001059 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001060
Daniel Dunbar75630b32009-06-30 02:10:03 +00001061 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001062 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001063 return true;
1064
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001065 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001066 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001067
1068 return false;
1069}
1070
Michael J. Spencer530ce852010-10-09 11:00:50 +00001071static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001072 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001073 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001074 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001075 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001076
Jim Grosbach4b905842013-09-20 23:08:21 +00001077 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001078 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001079 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001080 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001081 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001083 return 1;
1084
Jim Grosbach4b905842013-09-20 23:08:21 +00001085 // Low Precedence: |, &, ^
1086 //
1087 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001090 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001091 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001093 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001094 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001095 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001096 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001097
Jim Grosbach4b905842013-09-20 23:08:21 +00001098 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001099 case AsmToken::EqualEqual:
1100 Kind = MCBinaryExpr::EQ;
1101 return 3;
1102 case AsmToken::ExclaimEqual:
1103 case AsmToken::LessGreater:
1104 Kind = MCBinaryExpr::NE;
1105 return 3;
1106 case AsmToken::Less:
1107 Kind = MCBinaryExpr::LT;
1108 return 3;
1109 case AsmToken::LessEqual:
1110 Kind = MCBinaryExpr::LTE;
1111 return 3;
1112 case AsmToken::Greater:
1113 Kind = MCBinaryExpr::GT;
1114 return 3;
1115 case AsmToken::GreaterEqual:
1116 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001117 return 3;
1118
Jim Grosbach4b905842013-09-20 23:08:21 +00001119 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001120 case AsmToken::LessLess:
1121 Kind = MCBinaryExpr::Shl;
1122 return 4;
1123 case AsmToken::GreaterGreater:
1124 Kind = MCBinaryExpr::Shr;
1125 return 4;
1126
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001128 case AsmToken::Plus:
1129 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001131 case AsmToken::Minus:
1132 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001134
Jim Grosbach4b905842013-09-20 23:08:21 +00001135 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001136 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001137 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001138 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001139 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001140 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001141 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001142 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001143 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001144 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145 }
1146}
1147
Jim Grosbach4b905842013-09-20 23:08:21 +00001148/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001150bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001151 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001153 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001154 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156 // If the next token is lower precedence than we are allowed to eat, return
1157 // successfully with what we ate already.
1158 if (TokPrec < Precedence)
1159 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Sean Callanan686ed8d2010-01-19 20:22:31 +00001161 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001164 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001165 if (parsePrimaryExpr(RHS, EndLoc))
1166 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001168 // If BinOp binds less tightly with RHS than the operator after RHS, let
1169 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001170 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001171 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001172 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1173 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001174
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001175 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001176 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001177 }
1178}
1179
Chris Lattner36e02122009-06-21 20:54:55 +00001180/// ParseStatement:
1181/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001182/// ::= Label* Directive ...Operands... EndOfStatement
1183/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001184bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001185 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001186 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001187 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001188 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001189 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001191 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001192 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001193 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001195 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001197 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001198 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001199
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 if (Lexer.is(AsmToken::Integer)) {
1202 LocalLabelVal = getTok().getIntVal();
1203 if (LocalLabelVal < 0) {
1204 if (!TheCondState.Ignore)
1205 return TokError("unexpected token at start of statement");
1206 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001207 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001208 IDVal = getTok().getString();
1209 Lex(); // Consume the integer token to be used as an identifier token.
1210 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001211 if (!TheCondState.Ignore)
1212 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 }
1214 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001215 } else if (Lexer.is(AsmToken::Dot)) {
1216 // Treat '.' as a valid identifier in this context.
1217 Lex();
1218 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001219 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001220 if (!TheCondState.Ignore)
1221 return TokError("unexpected token at start of statement");
1222 IDVal = "";
1223 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001224
Chris Lattner926885c2010-04-17 18:14:27 +00001225 // Handle conditional assembly here before checking for skipping. We
1226 // have to do this so that .endif isn't skipped in a ".if 0" block for
1227 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001228 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001229 DirectiveKindMap.find(IDVal);
1230 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1231 ? DK_NO_DIRECTIVE
1232 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001233 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001234 default:
1235 break;
1236 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001237 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001238 return parseDirectiveIf(IDLoc);
1239 case DK_IFB:
1240 return parseDirectiveIfb(IDLoc, true);
1241 case DK_IFNB:
1242 return parseDirectiveIfb(IDLoc, false);
1243 case DK_IFC:
1244 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001245 case DK_IFEQS:
1246 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001247 case DK_IFNC:
1248 return parseDirectiveIfc(IDLoc, false);
1249 case DK_IFDEF:
1250 return parseDirectiveIfdef(IDLoc, true);
1251 case DK_IFNDEF:
1252 case DK_IFNOTDEF:
1253 return parseDirectiveIfdef(IDLoc, false);
1254 case DK_ELSEIF:
1255 return parseDirectiveElseIf(IDLoc);
1256 case DK_ELSE:
1257 return parseDirectiveElse(IDLoc);
1258 case DK_ENDIF:
1259 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001260 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001261
Eli Bendersky88024712013-01-16 19:32:36 +00001262 // Ignore the statement if in the middle of inactive conditional
1263 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001264 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001265 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001266 return false;
1267 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001268
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001269 // FIXME: Recurse on local labels?
1270
1271 // See what kind of statement we have.
1272 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001273 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001274 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001275
Chris Lattner36e02122009-06-21 20:54:55 +00001276 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001277 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001278
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001279 // Diagnose attempt to use '.' as a label.
1280 if (IDVal == ".")
1281 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1282
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001283 // Diagnose attempt to use a variable as a label.
1284 //
1285 // FIXME: Diagnostics. Note the location of the definition as a label.
1286 // FIXME: This doesn't diagnose assignment to a symbol which has been
1287 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001288 MCSymbol *Sym;
1289 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001290 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001291 else
1292 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001293 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001294 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001295
Daniel Dunbare73b2672009-08-26 22:13:22 +00001296 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001297 if (!ParsingInlineAsm)
1298 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001299
Kevin Enderbye7739d42011-12-09 18:09:40 +00001300 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001301 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001302 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001303 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1304 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001305
Tim Northover1744d0a2013-10-25 12:49:50 +00001306 getTargetParser().onLabelParsed(Sym);
1307
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001308 // Consume any end of statement token, if present, to avoid spurious
1309 // AddBlankLine calls().
1310 if (Lexer.is(AsmToken::EndOfStatement)) {
1311 Lex();
1312 if (Lexer.is(AsmToken::Eof))
1313 return false;
1314 }
1315
Eli Friedman0f4871d2012-10-22 23:58:19 +00001316 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001317 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001318
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001319 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001320 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001321 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001322
Jim Grosbach4b905842013-09-20 23:08:21 +00001323 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001324
1325 default: // Normal instruction or directive.
1326 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001327 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001328
1329 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001330 if (areMacrosEnabled())
1331 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1332 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001333 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001334
Michael J. Spencer530ce852010-10-09 11:00:50 +00001335 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001336
Eli Bendersky17233942013-01-15 22:59:42 +00001337 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001338 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001340 //
Eli Bendersky17233942013-01-15 22:59:42 +00001341 // 1. The target-specific assembly parser. Some directives are target
1342 // specific or may potentially behave differently on certain targets.
1343 // 2. Asm parser extensions. For example, platform-specific parsers
1344 // (like the ELF parser) register themselves as extensions.
1345 // 3. The generic directive parser implemented by this class. These are
1346 // all the directives that behave in a target and platform independent
1347 // manner, or at least have a default behavior that's shared between
1348 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001349
Eli Bendersky17233942013-01-15 22:59:42 +00001350 // First query the target-specific parser. It will return 'true' if it
1351 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001352 if (!getTargetParser().ParseDirective(ID))
1353 return false;
1354
Alp Tokercb402912014-01-24 17:20:08 +00001355 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001357 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1358 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001359 if (Handler.first)
1360 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1361
1362 // Finally, if no one else is interested in this directive, it must be
1363 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001364 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001365 default:
1366 break;
1367 case DK_SET:
1368 case DK_EQU:
1369 return parseDirectiveSet(IDVal, true);
1370 case DK_EQUIV:
1371 return parseDirectiveSet(IDVal, false);
1372 case DK_ASCII:
1373 return parseDirectiveAscii(IDVal, false);
1374 case DK_ASCIZ:
1375 case DK_STRING:
1376 return parseDirectiveAscii(IDVal, true);
1377 case DK_BYTE:
1378 return parseDirectiveValue(1);
1379 case DK_SHORT:
1380 case DK_VALUE:
1381 case DK_2BYTE:
1382 return parseDirectiveValue(2);
1383 case DK_LONG:
1384 case DK_INT:
1385 case DK_4BYTE:
1386 return parseDirectiveValue(4);
1387 case DK_QUAD:
1388 case DK_8BYTE:
1389 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001390 case DK_OCTA:
1391 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001392 case DK_SINGLE:
1393 case DK_FLOAT:
1394 return parseDirectiveRealValue(APFloat::IEEEsingle);
1395 case DK_DOUBLE:
1396 return parseDirectiveRealValue(APFloat::IEEEdouble);
1397 case DK_ALIGN: {
1398 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1399 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1400 }
1401 case DK_ALIGN32: {
1402 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1403 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1404 }
1405 case DK_BALIGN:
1406 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1407 case DK_BALIGNW:
1408 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1409 case DK_BALIGNL:
1410 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1411 case DK_P2ALIGN:
1412 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1413 case DK_P2ALIGNW:
1414 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1415 case DK_P2ALIGNL:
1416 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1417 case DK_ORG:
1418 return parseDirectiveOrg();
1419 case DK_FILL:
1420 return parseDirectiveFill();
1421 case DK_ZERO:
1422 return parseDirectiveZero();
1423 case DK_EXTERN:
1424 eatToEndOfStatement(); // .extern is the default, ignore it.
1425 return false;
1426 case DK_GLOBL:
1427 case DK_GLOBAL:
1428 return parseDirectiveSymbolAttribute(MCSA_Global);
1429 case DK_LAZY_REFERENCE:
1430 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1431 case DK_NO_DEAD_STRIP:
1432 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1433 case DK_SYMBOL_RESOLVER:
1434 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1435 case DK_PRIVATE_EXTERN:
1436 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1437 case DK_REFERENCE:
1438 return parseDirectiveSymbolAttribute(MCSA_Reference);
1439 case DK_WEAK_DEFINITION:
1440 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1441 case DK_WEAK_REFERENCE:
1442 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1443 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1444 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1445 case DK_COMM:
1446 case DK_COMMON:
1447 return parseDirectiveComm(/*IsLocal=*/false);
1448 case DK_LCOMM:
1449 return parseDirectiveComm(/*IsLocal=*/true);
1450 case DK_ABORT:
1451 return parseDirectiveAbort();
1452 case DK_INCLUDE:
1453 return parseDirectiveInclude();
1454 case DK_INCBIN:
1455 return parseDirectiveIncbin();
1456 case DK_CODE16:
1457 case DK_CODE16GCC:
1458 return TokError(Twine(IDVal) + " not supported yet");
1459 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001460 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001461 case DK_IRP:
1462 return parseDirectiveIrp(IDLoc);
1463 case DK_IRPC:
1464 return parseDirectiveIrpc(IDLoc);
1465 case DK_ENDR:
1466 return parseDirectiveEndr(IDLoc);
1467 case DK_BUNDLE_ALIGN_MODE:
1468 return parseDirectiveBundleAlignMode();
1469 case DK_BUNDLE_LOCK:
1470 return parseDirectiveBundleLock();
1471 case DK_BUNDLE_UNLOCK:
1472 return parseDirectiveBundleUnlock();
1473 case DK_SLEB128:
1474 return parseDirectiveLEB128(true);
1475 case DK_ULEB128:
1476 return parseDirectiveLEB128(false);
1477 case DK_SPACE:
1478 case DK_SKIP:
1479 return parseDirectiveSpace(IDVal);
1480 case DK_FILE:
1481 return parseDirectiveFile(IDLoc);
1482 case DK_LINE:
1483 return parseDirectiveLine();
1484 case DK_LOC:
1485 return parseDirectiveLoc();
1486 case DK_STABS:
1487 return parseDirectiveStabs();
1488 case DK_CFI_SECTIONS:
1489 return parseDirectiveCFISections();
1490 case DK_CFI_STARTPROC:
1491 return parseDirectiveCFIStartProc();
1492 case DK_CFI_ENDPROC:
1493 return parseDirectiveCFIEndProc();
1494 case DK_CFI_DEF_CFA:
1495 return parseDirectiveCFIDefCfa(IDLoc);
1496 case DK_CFI_DEF_CFA_OFFSET:
1497 return parseDirectiveCFIDefCfaOffset();
1498 case DK_CFI_ADJUST_CFA_OFFSET:
1499 return parseDirectiveCFIAdjustCfaOffset();
1500 case DK_CFI_DEF_CFA_REGISTER:
1501 return parseDirectiveCFIDefCfaRegister(IDLoc);
1502 case DK_CFI_OFFSET:
1503 return parseDirectiveCFIOffset(IDLoc);
1504 case DK_CFI_REL_OFFSET:
1505 return parseDirectiveCFIRelOffset(IDLoc);
1506 case DK_CFI_PERSONALITY:
1507 return parseDirectiveCFIPersonalityOrLsda(true);
1508 case DK_CFI_LSDA:
1509 return parseDirectiveCFIPersonalityOrLsda(false);
1510 case DK_CFI_REMEMBER_STATE:
1511 return parseDirectiveCFIRememberState();
1512 case DK_CFI_RESTORE_STATE:
1513 return parseDirectiveCFIRestoreState();
1514 case DK_CFI_SAME_VALUE:
1515 return parseDirectiveCFISameValue(IDLoc);
1516 case DK_CFI_RESTORE:
1517 return parseDirectiveCFIRestore(IDLoc);
1518 case DK_CFI_ESCAPE:
1519 return parseDirectiveCFIEscape();
1520 case DK_CFI_SIGNAL_FRAME:
1521 return parseDirectiveCFISignalFrame();
1522 case DK_CFI_UNDEFINED:
1523 return parseDirectiveCFIUndefined(IDLoc);
1524 case DK_CFI_REGISTER:
1525 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001526 case DK_CFI_WINDOW_SAVE:
1527 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001528 case DK_MACROS_ON:
1529 case DK_MACROS_OFF:
1530 return parseDirectiveMacrosOnOff(IDVal);
1531 case DK_MACRO:
1532 return parseDirectiveMacro(IDLoc);
1533 case DK_ENDM:
1534 case DK_ENDMACRO:
1535 return parseDirectiveEndMacro(IDVal);
1536 case DK_PURGEM:
1537 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001538 case DK_END:
1539 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001540 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001541 return parseDirectiveError(IDLoc, false);
1542 case DK_ERROR:
1543 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001544 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001545
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001546 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001547 }
Chris Lattner36e02122009-06-21 20:54:55 +00001548
Chad Rosierc7f552c2013-02-12 21:33:51 +00001549 // __asm _emit or __asm __emit
1550 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1551 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001552 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001553
1554 // __asm align
1555 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001556 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001557
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001558 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001559
Chris Lattner7cbfa442010-05-19 23:34:33 +00001560 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001561 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001562 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001563 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001564 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001565 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001566
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001567 // Dump the parsed representation, if requested.
1568 if (getShowParsedOperands()) {
1569 SmallString<256> Str;
1570 raw_svector_ostream OS(Str);
1571 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001572 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001573 if (i != 0)
1574 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001575 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001576 }
1577 OS << "]";
1578
Jim Grosbach4b905842013-09-20 23:08:21 +00001579 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001580 }
1581
Kevin Enderby6469fc22011-11-01 22:27:22 +00001582 // If we are generating dwarf for assembly source files and the current
1583 // section is the initial text section then generate a .loc directive for
1584 // the instruction.
1585 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001586 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001587 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001588
Eli Bendersky88024712013-01-16 19:32:36 +00001589 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001590
Eli Bendersky88024712013-01-16 19:32:36 +00001591 // If we previously parsed a cpp hash file line comment then make sure the
1592 // current Dwarf File is for the CppHashFilename if not then emit the
1593 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001594 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001595 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1596 0, StringRef(), CppHashFilename);
1597 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001598
Jim Grosbach4b905842013-09-20 23:08:21 +00001599 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1600 // cache with the different Loc from the call above we save the last
1601 // info we queried here with SrcMgr.FindLineNumber().
1602 unsigned CppHashLocLineNo;
1603 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1604 CppHashLocLineNo = LastQueryLine;
1605 else {
1606 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1607 LastQueryLine = CppHashLocLineNo;
1608 LastQueryIDLoc = CppHashLoc;
1609 LastQueryBuffer = CppHashBuf;
1610 }
1611 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001612 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001613
Jim Grosbach4b905842013-09-20 23:08:21 +00001614 getStreamer().EmitDwarfLocDirective(
1615 getContext().getGenDwarfFileNumber(), Line, 0,
1616 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1617 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001618 }
1619
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001620 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001621 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001622 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001623 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1624 Info.ParsedOperands, Out,
1625 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001626 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001627
Chris Lattnera2a9d162010-09-11 16:18:25 +00001628 // Don't skip the rest of the line, the instruction parser is responsible for
1629 // that.
1630 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001631}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001632
Jim Grosbach4b905842013-09-20 23:08:21 +00001633/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001634/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001635void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001636 if (!Lexer.is(AsmToken::EndOfStatement))
1637 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001638 // Eat EOL.
1639 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001640}
1641
Jim Grosbach4b905842013-09-20 23:08:21 +00001642/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001643/// ::= # number "filename"
1644/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001645bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001646 Lex(); // Eat the hash token.
1647
1648 if (getLexer().isNot(AsmToken::Integer)) {
1649 // Consume the line since in cases it is not a well-formed line directive,
1650 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001651 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001652 return false;
1653 }
1654
1655 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001656 Lex();
1657
1658 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001659 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001660 return false;
1661 }
1662
1663 StringRef Filename = getTok().getString();
1664 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001665 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001666
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001667 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1668 CppHashLoc = L;
1669 CppHashFilename = Filename;
1670 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001671 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001672
1673 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001674 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001675 return false;
1676}
1677
Jim Grosbach4b905842013-09-20 23:08:21 +00001678/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001679/// for the Filename and LineNo if any in the diagnostic.
1680void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001681 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001682 raw_ostream &OS = errs();
1683
1684 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1685 const SMLoc &DiagLoc = Diag.getLoc();
1686 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1687 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1688
Jim Grosbach4b905842013-09-20 23:08:21 +00001689 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690 // before printing the message.
1691 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001692 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001693 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1694 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695 }
1696
Eric Christophera7c32732012-12-18 00:30:54 +00001697 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698 // manager changed or buffer changed (like in a nested include) then just
1699 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001700 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001702 if (Parser->SavedDiagHandler)
1703 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1704 else
1705 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706 return;
1707 }
1708
Eric Christophera7c32732012-12-18 00:30:54 +00001709 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1711 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001712 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713
1714 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1715 int CppHashLocLineNo =
1716 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001717 int LineNo =
1718 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001719
Jim Grosbach4b905842013-09-20 23:08:21 +00001720 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1721 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001722 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001724 if (Parser->SavedDiagHandler)
1725 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1726 else
1727 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001728}
1729
Rafael Espindola2c064482012-08-21 18:29:30 +00001730// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1731// difference being that that function accepts '@' as part of identifiers and
1732// we can't do that. AsmLexer.cpp should probably be changed to handle
1733// '@' as a special case when needed.
1734static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001735 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1736 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001737}
1738
Rafael Espindola34b9c512012-06-03 23:57:14 +00001739bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001740 ArrayRef<MCAsmMacroParameter> Parameters,
1741 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001742 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001743 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001744 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001745 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001746
Preston Gurd05500642012-09-19 20:36:12 +00001747 // A macro without parameters is handled differently on Darwin:
1748 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001749 while (!Body.empty()) {
1750 // Scan for the next substitution.
1751 std::size_t End = Body.size(), Pos = 0;
1752 for (; Pos != End; ++Pos) {
1753 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001754 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001755 // This macro has no parameters, look for $0, $1, etc.
1756 if (Body[Pos] != '$' || Pos + 1 == End)
1757 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001758
Rafael Espindola1134ab232011-06-05 02:43:45 +00001759 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001760 if (Next == '$' || Next == 'n' ||
1761 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001762 break;
1763 } else {
1764 // This macro has parameters, look for \foo, \bar, etc.
1765 if (Body[Pos] == '\\' && Pos + 1 != End)
1766 break;
1767 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768 }
1769
1770 // Add the prefix.
1771 OS << Body.slice(0, Pos);
1772
1773 // Check if we reached the end.
1774 if (Pos == End)
1775 break;
1776
Benjamin Kramer513e7442014-02-20 13:36:32 +00001777 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 switch (Body[Pos + 1]) {
1779 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 case '$':
1781 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001782 break;
1783
Jim Grosbach4b905842013-09-20 23:08:21 +00001784 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001785 case 'n':
1786 OS << A.size();
1787 break;
1788
Jim Grosbach4b905842013-09-20 23:08:21 +00001789 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001790 default: {
1791 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001793 if (Index >= A.size())
1794 break;
1795
1796 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001797 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 ie = A[Index].end();
1799 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001800 OS << it->getString();
1801 break;
1802 }
1803 }
1804 Pos += 2;
1805 } else {
1806 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001807 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001808 ++I;
1809
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 const char *Begin = Body.data() + Pos + 1;
1811 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 unsigned Index = 0;
1813 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001814 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 break;
1816
Preston Gurd05500642012-09-19 20:36:12 +00001817 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001818 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1819 Pos += 3;
1820 else {
1821 OS << '\\' << Argument;
1822 Pos = I;
1823 }
Preston Gurd05500642012-09-19 20:36:12 +00001824 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001825 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001826 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001827 ie = A[Index].end();
1828 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001829 // We expect no quotes around the string's contents when
1830 // parsing for varargs.
1831 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001832 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001833 else
1834 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001835
Preston Gurd05500642012-09-19 20:36:12 +00001836 Pos += 1 + Argument.size();
1837 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001838 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001839 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001840 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001841 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001842
Rafael Espindola1134ab232011-06-05 02:43:45 +00001843 return false;
1844}
Daniel Dunbar43235712010-07-18 18:54:11 +00001845
Jim Grosbach4b905842013-09-20 23:08:21 +00001846MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1847 SMLoc EL, MemoryBuffer *I)
1848 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1849 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001850
Jim Grosbach4b905842013-09-20 23:08:21 +00001851static bool isOperator(AsmToken::TokenKind kind) {
1852 switch (kind) {
1853 default:
1854 return false;
1855 case AsmToken::Plus:
1856 case AsmToken::Minus:
1857 case AsmToken::Tilde:
1858 case AsmToken::Slash:
1859 case AsmToken::Star:
1860 case AsmToken::Dot:
1861 case AsmToken::Equal:
1862 case AsmToken::EqualEqual:
1863 case AsmToken::Pipe:
1864 case AsmToken::PipePipe:
1865 case AsmToken::Caret:
1866 case AsmToken::Amp:
1867 case AsmToken::AmpAmp:
1868 case AsmToken::Exclaim:
1869 case AsmToken::ExclaimEqual:
1870 case AsmToken::Percent:
1871 case AsmToken::Less:
1872 case AsmToken::LessEqual:
1873 case AsmToken::LessLess:
1874 case AsmToken::LessGreater:
1875 case AsmToken::Greater:
1876 case AsmToken::GreaterEqual:
1877 case AsmToken::GreaterGreater:
1878 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001879 }
1880}
1881
David Majnemer16252452014-01-29 00:07:39 +00001882namespace {
1883class AsmLexerSkipSpaceRAII {
1884public:
1885 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1886 Lexer.setSkipSpace(SkipSpace);
1887 }
1888
1889 ~AsmLexerSkipSpaceRAII() {
1890 Lexer.setSkipSpace(true);
1891 }
1892
1893private:
1894 AsmLexer &Lexer;
1895};
1896}
1897
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001898bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1899
1900 if (Vararg) {
1901 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1902 StringRef Str = parseStringToEndOfStatement();
1903 MA.push_back(AsmToken(AsmToken::String, Str));
1904 }
1905 return false;
1906 }
1907
Rafael Espindola768b41c2012-06-15 14:02:34 +00001908 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001909 unsigned AddTokens = 0;
1910
David Majnemer16252452014-01-29 00:07:39 +00001911 // Darwin doesn't use spaces to delmit arguments.
1912 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001913
1914 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001915 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001916 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001917
David Majnemer91fc4c22014-01-29 18:57:46 +00001918 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001919 break;
Preston Gurd05500642012-09-19 20:36:12 +00001920
1921 if (Lexer.is(AsmToken::Space)) {
1922 Lex(); // Eat spaces
1923
1924 // Spaces can delimit parameters, but could also be part an expression.
1925 // If the token after a space is an operator, add the token and the next
1926 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001927 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001928 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001929 // Check to see whether the token is used as an operator,
1930 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001931 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001932 if (*NextChar == ' ')
1933 AddTokens = 2;
1934 }
1935
1936 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001937 break;
1938 }
1939 }
1940 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001941
Jim Grosbach4b905842013-09-20 23:08:21 +00001942 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001943 // to be able to fill in the remaining default parameter values
1944 if (Lexer.is(AsmToken::EndOfStatement))
1945 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001946
1947 // Adjust the current parentheses level.
1948 if (Lexer.is(AsmToken::LParen))
1949 ++ParenLevel;
1950 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1951 --ParenLevel;
1952
1953 // Append the token to the current argument list.
1954 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001955 if (AddTokens)
1956 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 Lex();
1958 }
Preston Gurd05500642012-09-19 20:36:12 +00001959
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001961 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001962 return false;
1963}
1964
1965// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001966bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001967 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001968 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001969 bool NamedParametersFound = false;
1970 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001971
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001972 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001973 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001974
Rafael Espindola768b41c2012-06-15 14:02:34 +00001975 // Parse two kinds of macro invocations:
1976 // - macros defined without any parameters accept an arbitrary number of them
1977 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001978 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001979 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1980 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001981 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001982 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001984 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001985 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001986 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001987 eatToEndOfStatement();
1988 return true;
1989 }
1990
1991 if (!Lexer.is(AsmToken::Equal)) {
1992 TokError("expected '=' after formal parameter identifier");
1993 eatToEndOfStatement();
1994 return true;
1995 }
1996 Lex();
1997
1998 NamedParametersFound = true;
1999 }
2000
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002001 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002002 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002003 eatToEndOfStatement();
2004 return true;
2005 }
2006
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002007 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2008 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002009 return true;
2010
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002011 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002012 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002013 unsigned FAI = 0;
2014 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002015 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002016 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002017
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002018 if (FAI >= NParameters) {
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002019 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002020 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002021 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002022 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002023 return true;
2024 }
2025 PI = FAI;
2026 }
2027
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002028 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002029 if (A.size() <= PI)
2030 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002031 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002032
2033 if (FALocs.size() <= PI)
2034 FALocs.resize(PI + 1);
2035
2036 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002037 }
Jim Grosbach206661622012-07-30 22:44:17 +00002038
Preston Gurd242ed3152012-09-19 20:29:04 +00002039 // At the end of the statement, fill in remaining arguments that have
2040 // default values. If there aren't any, then the next argument is
2041 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002042 if (Lexer.is(AsmToken::EndOfStatement)) {
2043 bool Failure = false;
2044 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2045 if (A[FAI].empty()) {
2046 if (M->Parameters[FAI].Required) {
2047 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2048 "missing value for required parameter "
2049 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2050 Failure = true;
2051 }
2052
2053 if (!M->Parameters[FAI].Value.empty())
2054 A[FAI] = M->Parameters[FAI].Value;
2055 }
2056 }
2057 return Failure;
2058 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002059
2060 if (Lexer.is(AsmToken::Comma))
2061 Lex();
2062 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002063
2064 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002065}
2066
Jim Grosbach4b905842013-09-20 23:08:21 +00002067const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2068 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002069 return (I == MacroMap.end()) ? NULL : I->getValue();
2070}
2071
Jim Grosbach4b905842013-09-20 23:08:21 +00002072void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002073 MacroMap[Name] = new MCAsmMacro(Macro);
2074}
2075
Jim Grosbach4b905842013-09-20 23:08:21 +00002076void AsmParser::undefineMacro(StringRef Name) {
2077 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002078 if (I != MacroMap.end()) {
2079 delete I->getValue();
2080 MacroMap.erase(I);
2081 }
2082}
2083
Jim Grosbach4b905842013-09-20 23:08:21 +00002084bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002085 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2086 // this, although we should protect against infinite loops.
2087 if (ActiveMacros.size() == 20)
2088 return TokError("macros cannot be nested more than 20 levels deep");
2089
Eli Bendersky38274122013-01-14 23:22:36 +00002090 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002091 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002092 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002093
Rafael Espindola1134ab232011-06-05 02:43:45 +00002094 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2095 // to hold the macro body with substitutions.
2096 SmallString<256> Buf;
2097 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002098 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002099
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002100 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002101 return true;
2102
Eli Bendersky38274122013-01-14 23:22:36 +00002103 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002104 // instantiation.
2105 OS << ".endmacro\n";
2106
Rafael Espindola1134ab232011-06-05 02:43:45 +00002107 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002108 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002109
Daniel Dunbar43235712010-07-18 18:54:11 +00002110 // Create the macro instantiation object and add to the current macro
2111 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002112 MacroInstantiation *MI = new MacroInstantiation(
2113 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002114 ActiveMacros.push_back(MI);
2115
2116 // Jump to the macro instantiation and prime the lexer.
2117 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2118 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2119 Lex();
2120
2121 return false;
2122}
2123
Jim Grosbach4b905842013-09-20 23:08:21 +00002124void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002125 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002127 Lex();
2128
2129 // Pop the instantiation entry.
2130 delete ActiveMacros.back();
2131 ActiveMacros.pop_back();
2132}
2133
Jim Grosbach4b905842013-09-20 23:08:21 +00002134static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002135 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002136 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002137 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2138 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002139 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002140 case MCExpr::Target:
2141 case MCExpr::Constant:
2142 return false;
2143 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002144 const MCSymbol &S =
2145 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002146 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002147 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002148 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002149 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002150 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002151 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002152 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002153
2154 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155}
2156
Jim Grosbach4b905842013-09-20 23:08:21 +00002157bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002158 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002159 // FIXME: Use better location, we should use proper tokens.
2160 SMLoc EqualLoc = Lexer.getLoc();
2161
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002162 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002163 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002164 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002165
Rafael Espindola72f5f172012-01-28 05:57:00 +00002166 // Note: we don't count b as used in "a = b". This is to allow
2167 // a = b
2168 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002169
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002170 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002171 return TokError("unexpected token in assignment");
2172
2173 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002174 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002175
Daniel Dunbar5f339242009-10-16 01:57:39 +00002176 // Validate that the LHS is allowed to be a variable (either it has not been
2177 // used as a symbol, or it is an absolute symbol).
2178 MCSymbol *Sym = getContext().LookupSymbol(Name);
2179 if (Sym) {
2180 // Diagnose assignment to a label.
2181 //
2182 // FIXME: Diagnostics. Note the location of the definition as a label.
2183 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002184 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002185 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2186 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002187 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002188 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2189 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002190 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002191 return Error(EqualLoc, "redefinition of '" + Name + "'");
2192 else if (!Sym->isVariable())
2193 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002194 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002195 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002196 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002197
2198 // Don't count these checks as uses.
2199 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002200 } else if (Name == ".") {
2201 if (Out.EmitValueToOffset(Value, 0)) {
2202 Error(EqualLoc, "expected absolute expression");
2203 eatToEndOfStatement();
2204 }
2205 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002206 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002207 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002208
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002209 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002210 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002211 if (NoDeadStrip)
2212 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2213
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002214 return false;
2215}
2216
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002217/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002218/// ::= identifier
2219/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002220bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002221 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002222 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2223 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002224 // handle this as a context dependent token, instead we detect adjacent tokens
2225 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002226 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2227 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002228
Hans Wennborgce69d772013-10-18 20:46:28 +00002229 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002230 Lex();
2231 if (Lexer.isNot(AsmToken::Identifier))
2232 return true;
2233
Hans Wennborgce69d772013-10-18 20:46:28 +00002234 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2235 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002236 return true;
2237
2238 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002239 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002241 Lex();
2242 return false;
2243 }
2244
Jim Grosbach4b905842013-09-20 23:08:21 +00002245 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002246 return true;
2247
Sean Callanan936b0d32010-01-19 21:44:56 +00002248 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002249
Sean Callanan686ed8d2010-01-19 20:22:31 +00002250 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002251
2252 return false;
2253}
2254
Jim Grosbach4b905842013-09-20 23:08:21 +00002255/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002256/// ::= .equ identifier ',' expression
2257/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002258/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002259bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002260 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002261
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002262 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002263 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002264
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002265 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002266 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002267 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002268
Jim Grosbach4b905842013-09-20 23:08:21 +00002269 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002270}
2271
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002272bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002273 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002274
2275 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002276 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002277 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2278 if (Str[i] != '\\') {
2279 Data += Str[i];
2280 continue;
2281 }
2282
2283 // Recognize escaped characters. Note that this escape semantics currently
2284 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2285 ++i;
2286 if (i == e)
2287 return TokError("unexpected backslash at end of string");
2288
2289 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002290 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002291 // Consume up to three octal characters.
2292 unsigned Value = Str[i] - '0';
2293
Jim Grosbach4b905842013-09-20 23:08:21 +00002294 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002295 ++i;
2296 Value = Value * 8 + (Str[i] - '0');
2297
Jim Grosbach4b905842013-09-20 23:08:21 +00002298 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002299 ++i;
2300 Value = Value * 8 + (Str[i] - '0');
2301 }
2302 }
2303
2304 if (Value > 255)
2305 return TokError("invalid octal escape sequence (out of range)");
2306
Jim Grosbach4b905842013-09-20 23:08:21 +00002307 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002308 continue;
2309 }
2310
2311 // Otherwise recognize individual escapes.
2312 switch (Str[i]) {
2313 default:
2314 // Just reject invalid escape sequences for now.
2315 return TokError("invalid escape sequence (unrecognized character)");
2316
2317 case 'b': Data += '\b'; break;
2318 case 'f': Data += '\f'; break;
2319 case 'n': Data += '\n'; break;
2320 case 'r': Data += '\r'; break;
2321 case 't': Data += '\t'; break;
2322 case '"': Data += '"'; break;
2323 case '\\': Data += '\\'; break;
2324 }
2325 }
2326
2327 return false;
2328}
2329
Jim Grosbach4b905842013-09-20 23:08:21 +00002330/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002331/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002332bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002333 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002334 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002335
Daniel Dunbara10e5192009-06-24 23:30:00 +00002336 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002337 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002338 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002339
Daniel Dunbaref668c12009-08-14 18:19:52 +00002340 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002341 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002342 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002343
Rafael Espindola64e1af82013-07-02 15:49:13 +00002344 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002345 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002346 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002347
Sean Callanan686ed8d2010-01-19 20:22:31 +00002348 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002349
2350 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002351 break;
2352
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002354 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002355 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002356 }
2357 }
2358
Sean Callanan686ed8d2010-01-19 20:22:31 +00002359 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002360 return false;
2361}
2362
Jim Grosbach4b905842013-09-20 23:08:21 +00002363/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002364/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002365bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002366 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002367 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002368
Daniel Dunbara10e5192009-06-24 23:30:00 +00002369 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002370 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002371 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002372 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002373 return true;
2374
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002375 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002376 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2377 assert(Size <= 8 && "Invalid size");
2378 uint64_t IntValue = MCE->getValue();
2379 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2380 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002381 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002382 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002383 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002384
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002385 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002386 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002387
Daniel Dunbara10e5192009-06-24 23:30:00 +00002388 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002389 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002390 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002391 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 }
2393 }
2394
Sean Callanan686ed8d2010-01-19 20:22:31 +00002395 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002396 return false;
2397}
2398
David Woodhoused6de0d92014-02-01 16:20:59 +00002399/// ParseDirectiveOctaValue
2400/// ::= .octa [ hexconstant (, hexconstant)* ]
2401bool AsmParser::parseDirectiveOctaValue() {
2402 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2403 checkForValidSection();
2404
2405 for (;;) {
2406 if (Lexer.getKind() == AsmToken::Error)
2407 return true;
2408 if (Lexer.getKind() != AsmToken::Integer &&
2409 Lexer.getKind() != AsmToken::BigNum)
2410 return TokError("unknown token in expression");
2411
2412 SMLoc ExprLoc = getLexer().getLoc();
2413 APInt IntValue = getTok().getAPIntVal();
2414 Lex();
2415
2416 uint64_t hi, lo;
2417 if (IntValue.isIntN(64)) {
2418 hi = 0;
2419 lo = IntValue.getZExtValue();
2420 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002421 // It might actually have more than 128 bits, but the top ones are zero.
2422 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002423 lo = IntValue.getLoBits(64).getZExtValue();
2424 } else
2425 return Error(ExprLoc, "literal value out of range for directive");
2426
2427 if (MAI.isLittleEndian()) {
2428 getStreamer().EmitIntValue(lo, 8);
2429 getStreamer().EmitIntValue(hi, 8);
2430 } else {
2431 getStreamer().EmitIntValue(hi, 8);
2432 getStreamer().EmitIntValue(lo, 8);
2433 }
2434
2435 if (getLexer().is(AsmToken::EndOfStatement))
2436 break;
2437
2438 // FIXME: Improve diagnostic.
2439 if (getLexer().isNot(AsmToken::Comma))
2440 return TokError("unexpected token in directive");
2441 Lex();
2442 }
2443 }
2444
2445 Lex();
2446 return false;
2447}
2448
Jim Grosbach4b905842013-09-20 23:08:21 +00002449/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002450/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002451bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002452 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002453 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002454
2455 for (;;) {
2456 // We don't truly support arithmetic on floating point expressions, so we
2457 // have to manually parse unary prefixes.
2458 bool IsNeg = false;
2459 if (getLexer().is(AsmToken::Minus)) {
2460 Lex();
2461 IsNeg = true;
2462 } else if (getLexer().is(AsmToken::Plus))
2463 Lex();
2464
Michael J. Spencer530ce852010-10-09 11:00:50 +00002465 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002466 getLexer().isNot(AsmToken::Real) &&
2467 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468 return TokError("unexpected token in directive");
2469
2470 // Convert to an APFloat.
2471 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002472 StringRef IDVal = getTok().getString();
2473 if (getLexer().is(AsmToken::Identifier)) {
2474 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2475 Value = APFloat::getInf(Semantics);
2476 else if (!IDVal.compare_lower("nan"))
2477 Value = APFloat::getNaN(Semantics, false, ~0);
2478 else
2479 return TokError("invalid floating point literal");
2480 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002481 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002482 return TokError("invalid floating point literal");
2483 if (IsNeg)
2484 Value.changeSign();
2485
2486 // Consume the numeric token.
2487 Lex();
2488
2489 // Emit the value as an integer.
2490 APInt AsInt = Value.bitcastToAPInt();
2491 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002492 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002493
2494 if (getLexer().is(AsmToken::EndOfStatement))
2495 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002496
Daniel Dunbar2af16532010-09-24 01:59:56 +00002497 if (getLexer().isNot(AsmToken::Comma))
2498 return TokError("unexpected token in directive");
2499 Lex();
2500 }
2501 }
2502
2503 Lex();
2504 return false;
2505}
2506
Jim Grosbach4b905842013-09-20 23:08:21 +00002507/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002508/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002509bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002510 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002511
2512 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002513 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002514 return true;
2515
Rafael Espindolab91bac62010-10-05 19:42:57 +00002516 int64_t Val = 0;
2517 if (getLexer().is(AsmToken::Comma)) {
2518 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002519 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002520 return true;
2521 }
2522
Rafael Espindola922e3f42010-09-16 15:03:59 +00002523 if (getLexer().isNot(AsmToken::EndOfStatement))
2524 return TokError("unexpected token in '.zero' directive");
2525
2526 Lex();
2527
Rafael Espindola64e1af82013-07-02 15:49:13 +00002528 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002529
2530 return false;
2531}
2532
Jim Grosbach4b905842013-09-20 23:08:21 +00002533/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002534/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002535bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002536 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002537
David Majnemer522d3db2014-02-01 07:19:38 +00002538 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002540 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002541 return true;
2542
David Majnemer522d3db2014-02-01 07:19:38 +00002543 if (NumValues < 0) {
2544 Warning(RepeatLoc,
2545 "'.fill' directive with negative repeat count has no effect");
2546 NumValues = 0;
2547 }
2548
Roman Divackye33098f2013-09-24 17:44:41 +00002549 int64_t FillSize = 1;
2550 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002551
David Majnemer522d3db2014-02-01 07:19:38 +00002552 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002553 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2554 if (getLexer().isNot(AsmToken::Comma))
2555 return TokError("unexpected token in '.fill' directive");
2556 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002557
David Majnemer522d3db2014-02-01 07:19:38 +00002558 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002559 if (parseAbsoluteExpression(FillSize))
2560 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002561
Roman Divackye33098f2013-09-24 17:44:41 +00002562 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2563 if (getLexer().isNot(AsmToken::Comma))
2564 return TokError("unexpected token in '.fill' directive");
2565 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002566
David Majnemer522d3db2014-02-01 07:19:38 +00002567 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002568 if (parseAbsoluteExpression(FillExpr))
2569 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002570
Roman Divackye33098f2013-09-24 17:44:41 +00002571 if (getLexer().isNot(AsmToken::EndOfStatement))
2572 return TokError("unexpected token in '.fill' directive");
2573
2574 Lex();
2575 }
2576 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002577
David Majnemer522d3db2014-02-01 07:19:38 +00002578 if (FillSize < 0) {
2579 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2580 NumValues = 0;
2581 }
2582 if (FillSize > 8) {
2583 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2584 FillSize = 8;
2585 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002586
David Majnemer522d3db2014-02-01 07:19:38 +00002587 if (!isUInt<32>(FillExpr) && FillSize > 4)
2588 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2589
2590 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2591 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2592
2593 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2594 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2595 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2596 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002597
2598 return false;
2599}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002600
Jim Grosbach4b905842013-09-20 23:08:21 +00002601/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002602/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002603bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002604 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002605
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002606 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002607 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002608 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002609 return true;
2610
2611 // Parse optional fill expression.
2612 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002613 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2614 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002615 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002616 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002617
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002618 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002619 return true;
2620
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002621 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002622 return TokError("unexpected token in '.org' directive");
2623 }
2624
Sean Callanan686ed8d2010-01-19 20:22:31 +00002625 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002626
Jim Grosbachb5912772012-01-27 00:37:08 +00002627 // Only limited forms of relocatable expressions are accepted here, it
2628 // has to be relative to the current section. The streamer will return
2629 // 'true' if the expression wasn't evaluatable.
2630 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2631 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002632
2633 return false;
2634}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002635
Jim Grosbach4b905842013-09-20 23:08:21 +00002636/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002637/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002638bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002639 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002640
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002641 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002642 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002643 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002644 return true;
2645
2646 SMLoc MaxBytesLoc;
2647 bool HasFillExpr = false;
2648 int64_t FillExpr = 0;
2649 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002650 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2651 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002653 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654
2655 // The fill expression can be omitted while specifying a maximum number of
2656 // alignment bytes, e.g:
2657 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002658 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002659 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002660 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661 return true;
2662 }
2663
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002664 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2665 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002666 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002667 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002669 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002670 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002671 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002672
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002673 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002674 return TokError("unexpected token in directive");
2675 }
2676 }
2677
Sean Callanan686ed8d2010-01-19 20:22:31 +00002678 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002680 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682
2683 // Compute alignment in bytes.
2684 if (IsPow2) {
2685 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002686 if (Alignment >= 32) {
2687 Error(AlignmentLoc, "invalid alignment value");
2688 Alignment = 31;
2689 }
2690
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002691 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002692 } else {
2693 // Reject alignments that aren't a power of two, for gas compatibility.
2694 if (!isPowerOf2_64(Alignment))
2695 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696 }
2697
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002698 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699 if (MaxBytesLoc.isValid()) {
2700 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002701 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002702 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002703 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002704 }
2705
2706 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002707 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002708 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002709 MaxBytesToFill = 0;
2710 }
2711 }
2712
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002713 // Check whether we should use optimal code alignment for this .align
2714 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002715 const MCSection *Section = getStreamer().getCurrentSection().first;
2716 assert(Section && "must have section to emit alignment");
2717 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002718 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2719 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002720 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002721 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002722 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002723 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2724 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002725 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002726
2727 return false;
2728}
2729
Jim Grosbach4b905842013-09-20 23:08:21 +00002730/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002731/// ::= .file [number] filename
2732/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002733bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002734 // FIXME: I'm not sure what this is.
2735 int64_t FileNumber = -1;
2736 SMLoc FileNumberLoc = getLexer().getLoc();
2737 if (getLexer().is(AsmToken::Integer)) {
2738 FileNumber = getTok().getIntVal();
2739 Lex();
2740
2741 if (FileNumber < 1)
2742 return TokError("file number less than one");
2743 }
2744
2745 if (getLexer().isNot(AsmToken::String))
2746 return TokError("unexpected token in '.file' directive");
2747
2748 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002749 // Allow the strings to have escaped octal character sequence.
2750 std::string Path = getTok().getString();
2751 if (parseEscapedString(Path))
2752 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002753 Lex();
2754
2755 StringRef Directory;
2756 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002757 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002758 if (getLexer().is(AsmToken::String)) {
2759 if (FileNumber == -1)
2760 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002761 if (parseEscapedString(FilenameData))
2762 return true;
2763 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002764 Directory = Path;
2765 Lex();
2766 } else {
2767 Filename = Path;
2768 }
2769
2770 if (getLexer().isNot(AsmToken::EndOfStatement))
2771 return TokError("unexpected token in '.file' directive");
2772
2773 if (FileNumber == -1)
2774 getStreamer().EmitFileDirective(Filename);
2775 else {
2776 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002777 Error(DirectiveLoc,
2778 "input can't have .file dwarf directives when -g is "
2779 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002780
David Blaikiec714ef42014-03-17 01:52:11 +00002781 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2782 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002783 Error(FileNumberLoc, "file number already allocated");
2784 }
2785
2786 return false;
2787}
2788
Jim Grosbach4b905842013-09-20 23:08:21 +00002789/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002790/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002791bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002792 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2793 if (getLexer().isNot(AsmToken::Integer))
2794 return TokError("unexpected token in '.line' directive");
2795
2796 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002797 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002798 Lex();
2799
2800 // FIXME: Do something with the .line.
2801 }
2802
2803 if (getLexer().isNot(AsmToken::EndOfStatement))
2804 return TokError("unexpected token in '.line' directive");
2805
2806 return false;
2807}
2808
Jim Grosbach4b905842013-09-20 23:08:21 +00002809/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002810/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2811/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2812/// The first number is a file number, must have been previously assigned with
2813/// a .file directive, the second number is the line number and optionally the
2814/// third number is a column position (zero if not specified). The remaining
2815/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002816bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002817 if (getLexer().isNot(AsmToken::Integer))
2818 return TokError("unexpected token in '.loc' directive");
2819 int64_t FileNumber = getTok().getIntVal();
2820 if (FileNumber < 1)
2821 return TokError("file number less than one in '.loc' directive");
2822 if (!getContext().isValidDwarfFileNumber(FileNumber))
2823 return TokError("unassigned file number in '.loc' directive");
2824 Lex();
2825
2826 int64_t LineNumber = 0;
2827 if (getLexer().is(AsmToken::Integer)) {
2828 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002829 if (LineNumber < 0)
2830 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002831 Lex();
2832 }
2833
2834 int64_t ColumnPos = 0;
2835 if (getLexer().is(AsmToken::Integer)) {
2836 ColumnPos = getTok().getIntVal();
2837 if (ColumnPos < 0)
2838 return TokError("column position less than zero in '.loc' directive");
2839 Lex();
2840 }
2841
2842 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2843 unsigned Isa = 0;
2844 int64_t Discriminator = 0;
2845 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2846 for (;;) {
2847 if (getLexer().is(AsmToken::EndOfStatement))
2848 break;
2849
2850 StringRef Name;
2851 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002852 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002853 return TokError("unexpected token in '.loc' directive");
2854
2855 if (Name == "basic_block")
2856 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2857 else if (Name == "prologue_end")
2858 Flags |= DWARF2_FLAG_PROLOGUE_END;
2859 else if (Name == "epilogue_begin")
2860 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2861 else if (Name == "is_stmt") {
2862 Loc = getTok().getLoc();
2863 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002864 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002865 return true;
2866 // The expression must be the constant 0 or 1.
2867 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2868 int Value = MCE->getValue();
2869 if (Value == 0)
2870 Flags &= ~DWARF2_FLAG_IS_STMT;
2871 else if (Value == 1)
2872 Flags |= DWARF2_FLAG_IS_STMT;
2873 else
2874 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002875 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002876 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2877 }
Craig Topperf15655b2013-04-22 04:22:40 +00002878 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002879 Loc = getTok().getLoc();
2880 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002881 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002882 return true;
2883 // The expression must be a constant greater or equal to 0.
2884 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2885 int Value = MCE->getValue();
2886 if (Value < 0)
2887 return Error(Loc, "isa number less than zero");
2888 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002889 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002890 return Error(Loc, "isa number not a constant value");
2891 }
Craig Topperf15655b2013-04-22 04:22:40 +00002892 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002893 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002894 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002895 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return Error(Loc, "unknown sub-directive in '.loc' directive");
2897 }
2898
2899 if (getLexer().is(AsmToken::EndOfStatement))
2900 break;
2901 }
2902 }
2903
2904 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2905 Isa, Discriminator, StringRef());
2906
2907 return false;
2908}
2909
Jim Grosbach4b905842013-09-20 23:08:21 +00002910/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002911/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002912bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002913 return TokError("unsupported directive '.stabs'");
2914}
2915
Jim Grosbach4b905842013-09-20 23:08:21 +00002916/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002917/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002918bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002919 StringRef Name;
2920 bool EH = false;
2921 bool Debug = false;
2922
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002923 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002924 return TokError("Expected an identifier");
2925
2926 if (Name == ".eh_frame")
2927 EH = true;
2928 else if (Name == ".debug_frame")
2929 Debug = true;
2930
2931 if (getLexer().is(AsmToken::Comma)) {
2932 Lex();
2933
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002934 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002935 return TokError("Expected an identifier");
2936
2937 if (Name == ".eh_frame")
2938 EH = true;
2939 else if (Name == ".debug_frame")
2940 Debug = true;
2941 }
2942
2943 getStreamer().EmitCFISections(EH, Debug);
2944 return false;
2945}
2946
Jim Grosbach4b905842013-09-20 23:08:21 +00002947/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002948/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002949bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002950 StringRef Simple;
2951 if (getLexer().isNot(AsmToken::EndOfStatement))
2952 if (parseIdentifier(Simple) || Simple != "simple")
2953 return TokError("unexpected token in .cfi_startproc directive");
2954
2955 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002956 return false;
2957}
2958
Jim Grosbach4b905842013-09-20 23:08:21 +00002959/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002960/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002961bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002962 getStreamer().EmitCFIEndProc();
2963 return false;
2964}
2965
Jim Grosbach4b905842013-09-20 23:08:21 +00002966/// \brief parse register name or number.
2967bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002968 SMLoc DirectiveLoc) {
2969 unsigned RegNo;
2970
2971 if (getLexer().isNot(AsmToken::Integer)) {
2972 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2973 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002974 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002975 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002976 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002977
2978 return false;
2979}
2980
Jim Grosbach4b905842013-09-20 23:08:21 +00002981/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002982/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002983bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002984 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002985 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002986 return true;
2987
2988 if (getLexer().isNot(AsmToken::Comma))
2989 return TokError("unexpected token in directive");
2990 Lex();
2991
2992 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002993 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002994 return true;
2995
2996 getStreamer().EmitCFIDefCfa(Register, Offset);
2997 return false;
2998}
2999
Jim Grosbach4b905842013-09-20 23:08:21 +00003000/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003001/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003002bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003003 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003004 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003005 return true;
3006
3007 getStreamer().EmitCFIDefCfaOffset(Offset);
3008 return false;
3009}
3010
Jim Grosbach4b905842013-09-20 23:08:21 +00003011/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003012/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003013bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003014 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003015 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003016 return true;
3017
3018 if (getLexer().isNot(AsmToken::Comma))
3019 return TokError("unexpected token in directive");
3020 Lex();
3021
3022 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003023 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003024 return true;
3025
3026 getStreamer().EmitCFIRegister(Register1, Register2);
3027 return false;
3028}
3029
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003030/// parseDirectiveCFIWindowSave
3031/// ::= .cfi_window_save
3032bool AsmParser::parseDirectiveCFIWindowSave() {
3033 getStreamer().EmitCFIWindowSave();
3034 return false;
3035}
3036
Jim Grosbach4b905842013-09-20 23:08:21 +00003037/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003038/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003039bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003040 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003041 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3045 return false;
3046}
3047
Jim Grosbach4b905842013-09-20 23:08:21 +00003048/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003049/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003050bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003051 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003052 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003053 return true;
3054
3055 getStreamer().EmitCFIDefCfaRegister(Register);
3056 return false;
3057}
3058
Jim Grosbach4b905842013-09-20 23:08:21 +00003059/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003060/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003061bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003062 int64_t Register = 0;
3063 int64_t Offset = 0;
3064
Jim Grosbach4b905842013-09-20 23:08:21 +00003065 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003066 return true;
3067
3068 if (getLexer().isNot(AsmToken::Comma))
3069 return TokError("unexpected token in directive");
3070 Lex();
3071
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return true;
3074
3075 getStreamer().EmitCFIOffset(Register, Offset);
3076 return false;
3077}
3078
Jim Grosbach4b905842013-09-20 23:08:21 +00003079/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003080/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003081bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003082 int64_t Register = 0;
3083
Jim Grosbach4b905842013-09-20 23:08:21 +00003084 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003085 return true;
3086
3087 if (getLexer().isNot(AsmToken::Comma))
3088 return TokError("unexpected token in directive");
3089 Lex();
3090
3091 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003092 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003093 return true;
3094
3095 getStreamer().EmitCFIRelOffset(Register, Offset);
3096 return false;
3097}
3098
3099static bool isValidEncoding(int64_t Encoding) {
3100 if (Encoding & ~0xff)
3101 return false;
3102
3103 if (Encoding == dwarf::DW_EH_PE_omit)
3104 return true;
3105
3106 const unsigned Format = Encoding & 0xf;
3107 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3108 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3109 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3110 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3111 return false;
3112
3113 const unsigned Application = Encoding & 0x70;
3114 if (Application != dwarf::DW_EH_PE_absptr &&
3115 Application != dwarf::DW_EH_PE_pcrel)
3116 return false;
3117
3118 return true;
3119}
3120
Jim Grosbach4b905842013-09-20 23:08:21 +00003121/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003122/// IsPersonality true for cfi_personality, false for cfi_lsda
3123/// ::= .cfi_personality encoding, [symbol_name]
3124/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003125bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003126 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003127 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003128 return true;
3129 if (Encoding == dwarf::DW_EH_PE_omit)
3130 return false;
3131
3132 if (!isValidEncoding(Encoding))
3133 return TokError("unsupported encoding.");
3134
3135 if (getLexer().isNot(AsmToken::Comma))
3136 return TokError("unexpected token in directive");
3137 Lex();
3138
3139 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003140 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003141 return TokError("expected identifier in directive");
3142
3143 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3144
3145 if (IsPersonality)
3146 getStreamer().EmitCFIPersonality(Sym, Encoding);
3147 else
3148 getStreamer().EmitCFILsda(Sym, Encoding);
3149 return false;
3150}
3151
Jim Grosbach4b905842013-09-20 23:08:21 +00003152/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003153/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003154bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003155 getStreamer().EmitCFIRememberState();
3156 return false;
3157}
3158
Jim Grosbach4b905842013-09-20 23:08:21 +00003159/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003160/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003161bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003162 getStreamer().EmitCFIRestoreState();
3163 return false;
3164}
3165
Jim Grosbach4b905842013-09-20 23:08:21 +00003166/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003167/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003168bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003169 int64_t Register = 0;
3170
Jim Grosbach4b905842013-09-20 23:08:21 +00003171 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003172 return true;
3173
3174 getStreamer().EmitCFISameValue(Register);
3175 return false;
3176}
3177
Jim Grosbach4b905842013-09-20 23:08:21 +00003178/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003179/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003180bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003181 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003182 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003183 return true;
3184
3185 getStreamer().EmitCFIRestore(Register);
3186 return false;
3187}
3188
Jim Grosbach4b905842013-09-20 23:08:21 +00003189/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003190/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003191bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003192 std::string Values;
3193 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003194 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003195 return true;
3196
3197 Values.push_back((uint8_t)CurrValue);
3198
3199 while (getLexer().is(AsmToken::Comma)) {
3200 Lex();
3201
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003202 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003203 return true;
3204
3205 Values.push_back((uint8_t)CurrValue);
3206 }
3207
3208 getStreamer().EmitCFIEscape(Values);
3209 return false;
3210}
3211
Jim Grosbach4b905842013-09-20 23:08:21 +00003212/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003213/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003214bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003215 if (getLexer().isNot(AsmToken::EndOfStatement))
3216 return Error(getLexer().getLoc(),
3217 "unexpected token in '.cfi_signal_frame'");
3218
3219 getStreamer().EmitCFISignalFrame();
3220 return false;
3221}
3222
Jim Grosbach4b905842013-09-20 23:08:21 +00003223/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003224/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003225bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003226 int64_t Register = 0;
3227
Jim Grosbach4b905842013-09-20 23:08:21 +00003228 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003229 return true;
3230
3231 getStreamer().EmitCFIUndefined(Register);
3232 return false;
3233}
3234
Jim Grosbach4b905842013-09-20 23:08:21 +00003235/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003236/// ::= .macros_on
3237/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003238bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 if (getLexer().isNot(AsmToken::EndOfStatement))
3240 return Error(getLexer().getLoc(),
3241 "unexpected token in '" + Directive + "' directive");
3242
Jim Grosbach4b905842013-09-20 23:08:21 +00003243 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003244 return false;
3245}
3246
Jim Grosbach4b905842013-09-20 23:08:21 +00003247/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003248/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003249bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003250 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003251 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003252 return TokError("expected identifier in '.macro' directive");
3253
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003254 if (getLexer().is(AsmToken::Comma))
3255 Lex();
3256
Eli Bendersky17233942013-01-15 22:59:42 +00003257 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003258 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003259
3260 if (Parameters.size() && Parameters.back().Vararg)
3261 return Error(Lexer.getLoc(),
3262 "Vararg parameter '" + Parameters.back().Name +
3263 "' should be last one in the list of parameters.");
3264
David Majnemer91fc4c22014-01-29 18:57:46 +00003265 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003266 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003267 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003268
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003269 if (Lexer.is(AsmToken::Colon)) {
3270 Lex(); // consume ':'
3271
3272 SMLoc QualLoc;
3273 StringRef Qualifier;
3274
3275 QualLoc = Lexer.getLoc();
3276 if (parseIdentifier(Qualifier))
3277 return Error(QualLoc, "missing parameter qualifier for "
3278 "'" + Parameter.Name + "' in macro '" + Name + "'");
3279
3280 if (Qualifier == "req")
3281 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003282 else if (Qualifier == "vararg" && !IsDarwin)
3283 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003284 else
3285 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3286 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3287 }
3288
David Majnemer91fc4c22014-01-29 18:57:46 +00003289 if (getLexer().is(AsmToken::Equal)) {
3290 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003291
3292 SMLoc ParamLoc;
3293
3294 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003295 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003296 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003297
3298 if (Parameter.Required)
3299 Warning(ParamLoc, "pointless default value for required parameter "
3300 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003301 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003302
3303 Parameters.push_back(Parameter);
3304
3305 if (getLexer().is(AsmToken::Comma))
3306 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003307 }
3308
3309 // Eat the end of statement.
3310 Lex();
3311
3312 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003313 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003314
3315 // Lex the macro definition.
3316 for (;;) {
3317 // Check whether we have reached the end of the file.
3318 if (getLexer().is(AsmToken::Eof))
3319 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3320
3321 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003322 if (getLexer().is(AsmToken::Identifier)) {
3323 if (getTok().getIdentifier() == ".endm" ||
3324 getTok().getIdentifier() == ".endmacro") {
3325 if (MacroDepth == 0) { // Outermost macro.
3326 EndToken = getTok();
3327 Lex();
3328 if (getLexer().isNot(AsmToken::EndOfStatement))
3329 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3330 "' directive");
3331 break;
3332 } else {
3333 // Otherwise we just found the end of an inner macro.
3334 --MacroDepth;
3335 }
3336 } else if (getTok().getIdentifier() == ".macro") {
3337 // We allow nested macros. Those aren't instantiated until the outermost
3338 // macro is expanded so just ignore them for now.
3339 ++MacroDepth;
3340 }
Eli Bendersky17233942013-01-15 22:59:42 +00003341 }
3342
3343 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003344 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003345 }
3346
Jim Grosbach4b905842013-09-20 23:08:21 +00003347 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003348 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3349 }
3350
3351 const char *BodyStart = StartToken.getLoc().getPointer();
3352 const char *BodyEnd = EndToken.getLoc().getPointer();
3353 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003354 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3355 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003356 return false;
3357}
3358
Jim Grosbach4b905842013-09-20 23:08:21 +00003359/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003360///
3361/// With the support added for named parameters there may be code out there that
3362/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003363/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003364/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003365/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003366/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3367/// warning that the positional parameter found in body which have no effect.
3368/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003369/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003370/// intended or change the macro to use the named parameters. It is possible
3371/// this warning will trigger when the none of the named parameters are used
3372/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003373void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003374 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003375 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003376 // If this macro is not defined with named parameters the warning we are
3377 // checking for here doesn't apply.
3378 unsigned NParameters = Parameters.size();
3379 if (NParameters == 0)
3380 return;
3381
3382 bool NamedParametersFound = false;
3383 bool PositionalParametersFound = false;
3384
3385 // Look at the body of the macro for use of both the named parameters and what
3386 // are likely to be positional parameters. This is what expandMacro() is
3387 // doing when it finds the parameters in the body.
3388 while (!Body.empty()) {
3389 // Scan for the next possible parameter.
3390 std::size_t End = Body.size(), Pos = 0;
3391 for (; Pos != End; ++Pos) {
3392 // Check for a substitution or escape.
3393 // This macro is defined with parameters, look for \foo, \bar, etc.
3394 if (Body[Pos] == '\\' && Pos + 1 != End)
3395 break;
3396
3397 // This macro should have parameters, but look for $0, $1, ..., $n too.
3398 if (Body[Pos] != '$' || Pos + 1 == End)
3399 continue;
3400 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003401 if (Next == '$' || Next == 'n' ||
3402 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003403 break;
3404 }
3405
3406 // Check if we reached the end.
3407 if (Pos == End)
3408 break;
3409
3410 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003411 switch (Body[Pos + 1]) {
3412 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003413 case '$':
3414 break;
3415
Jim Grosbach4b905842013-09-20 23:08:21 +00003416 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003417 case 'n':
3418 PositionalParametersFound = true;
3419 break;
3420
Jim Grosbach4b905842013-09-20 23:08:21 +00003421 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003422 default: {
3423 PositionalParametersFound = true;
3424 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003425 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003426 }
3427 Pos += 2;
3428 } else {
3429 unsigned I = Pos + 1;
3430 while (isIdentifierChar(Body[I]) && I + 1 != End)
3431 ++I;
3432
Jim Grosbach4b905842013-09-20 23:08:21 +00003433 const char *Begin = Body.data() + Pos + 1;
3434 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003435 unsigned Index = 0;
3436 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003437 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 break;
3439
3440 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003441 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3442 Pos += 3;
3443 else {
3444 Pos = I;
3445 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003446 } else {
3447 NamedParametersFound = true;
3448 Pos += 1 + Argument.size();
3449 }
3450 }
3451 // Update the scan point.
3452 Body = Body.substr(Pos);
3453 }
3454
3455 if (!NamedParametersFound && PositionalParametersFound)
3456 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3457 "used in macro body, possible positional parameter "
3458 "found in body which will have no effect");
3459}
3460
Jim Grosbach4b905842013-09-20 23:08:21 +00003461/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003462/// ::= .endm
3463/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003464bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003465 if (getLexer().isNot(AsmToken::EndOfStatement))
3466 return TokError("unexpected token in '" + Directive + "' directive");
3467
3468 // If we are inside a macro instantiation, terminate the current
3469 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003470 if (isInsideMacroInstantiation()) {
3471 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003472 return false;
3473 }
3474
3475 // Otherwise, this .endmacro is a stray entry in the file; well formed
3476 // .endmacro directives are handled during the macro definition parsing.
3477 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003478 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003479}
3480
Jim Grosbach4b905842013-09-20 23:08:21 +00003481/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003482/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003483bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003484 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003485 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003486 return TokError("expected identifier in '.purgem' directive");
3487
3488 if (getLexer().isNot(AsmToken::EndOfStatement))
3489 return TokError("unexpected token in '.purgem' directive");
3490
Jim Grosbach4b905842013-09-20 23:08:21 +00003491 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003492 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3493
Jim Grosbach4b905842013-09-20 23:08:21 +00003494 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003495 return false;
3496}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003497
Jim Grosbach4b905842013-09-20 23:08:21 +00003498/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003499/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003500bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003501 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003502
3503 // Expect a single argument: an expression that evaluates to a constant
3504 // in the inclusive range 0-30.
3505 SMLoc ExprLoc = getLexer().getLoc();
3506 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003507 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003508 return true;
3509 else if (getLexer().isNot(AsmToken::EndOfStatement))
3510 return TokError("unexpected token after expression in"
3511 " '.bundle_align_mode' directive");
3512 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3513 return Error(ExprLoc,
3514 "invalid bundle alignment size (expected between 0 and 30)");
3515
3516 Lex();
3517
3518 // Because of AlignSizePow2's verified range we can safely truncate it to
3519 // unsigned.
3520 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3521 return false;
3522}
3523
Jim Grosbach4b905842013-09-20 23:08:21 +00003524/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003525/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003526bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003527 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003528 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003529
Eli Bendersky802b6282013-01-07 21:51:08 +00003530 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3531 StringRef Option;
3532 SMLoc Loc = getTok().getLoc();
3533 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003534 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003535
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003536 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003537 return Error(Loc, kInvalidOptionError);
3538
3539 if (Option != "align_to_end")
3540 return Error(Loc, kInvalidOptionError);
3541 else if (getLexer().isNot(AsmToken::EndOfStatement))
3542 return Error(Loc,
3543 "unexpected token after '.bundle_lock' directive option");
3544 AlignToEnd = true;
3545 }
3546
Eli Benderskyf483ff92012-12-20 19:05:53 +00003547 Lex();
3548
Eli Bendersky802b6282013-01-07 21:51:08 +00003549 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003550 return false;
3551}
3552
Jim Grosbach4b905842013-09-20 23:08:21 +00003553/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003554/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003555bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003556 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003557
3558 if (getLexer().isNot(AsmToken::EndOfStatement))
3559 return TokError("unexpected token in '.bundle_unlock' directive");
3560 Lex();
3561
3562 getStreamer().EmitBundleUnlock();
3563 return false;
3564}
3565
Jim Grosbach4b905842013-09-20 23:08:21 +00003566/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003567/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003568bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003569 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003570
3571 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003572 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003573 return true;
3574
3575 int64_t FillExpr = 0;
3576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3577 if (getLexer().isNot(AsmToken::Comma))
3578 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3579 Lex();
3580
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003581 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003582 return true;
3583
3584 if (getLexer().isNot(AsmToken::EndOfStatement))
3585 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3586 }
3587
3588 Lex();
3589
3590 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003591 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3592 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003593
3594 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003595 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003596
3597 return false;
3598}
3599
Jim Grosbach4b905842013-09-20 23:08:21 +00003600/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003601/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003602bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003603 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003604 const MCExpr *Value;
3605
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003606 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003607 return true;
3608
3609 if (getLexer().isNot(AsmToken::EndOfStatement))
3610 return TokError("unexpected token in directive");
3611
3612 if (Signed)
3613 getStreamer().EmitSLEB128Value(Value);
3614 else
3615 getStreamer().EmitULEB128Value(Value);
3616
3617 return false;
3618}
3619
Jim Grosbach4b905842013-09-20 23:08:21 +00003620/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003621/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003622bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003623 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003624 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003625 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003626 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003627
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003628 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003629 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003630
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003631 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003632
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003633 // Assembler local symbols don't make any sense here. Complain loudly.
3634 if (Sym->isTemporary())
3635 return Error(Loc, "non-local symbol required in directive");
3636
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003637 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3638 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003639
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003640 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003641 break;
3642
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003643 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003644 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003645 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003646 }
3647 }
3648
Sean Callanan686ed8d2010-01-19 20:22:31 +00003649 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003650 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003651}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003652
Jim Grosbach4b905842013-09-20 23:08:21 +00003653/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003654/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003655bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003656 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003657
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003658 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003659 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003660 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003661 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003662
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003663 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003664 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003665
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003666 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003667 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003668 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003669
3670 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003671 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003672 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003673 return true;
3674
3675 int64_t Pow2Alignment = 0;
3676 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003677 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003678 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003679 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003680 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003681 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003682
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003683 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3684 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003685 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3686
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003687 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003688 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3689 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003690 if (!isPowerOf2_64(Pow2Alignment))
3691 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3692 Pow2Alignment = Log2_64(Pow2Alignment);
3693 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003694 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003695
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003696 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003697 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003698
Sean Callanan686ed8d2010-01-19 20:22:31 +00003699 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003700
Chris Lattner28ad7542009-07-09 17:25:12 +00003701 // NOTE: a size of zero for a .comm should create a undefined symbol
3702 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003703 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003704 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003705 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003706
Eric Christopherbc818852010-05-14 01:38:54 +00003707 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708 // may internally end up wanting an alignment in bytes.
3709 // FIXME: Diagnose overflow.
3710 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003711 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003712 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003713
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003714 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715 return Error(IDLoc, "invalid symbol redefinition");
3716
Chris Lattner28ad7542009-07-09 17:25:12 +00003717 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003718 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003719 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003720 return false;
3721 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003722
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003723 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003724 return false;
3725}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003726
Jim Grosbach4b905842013-09-20 23:08:21 +00003727/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003728/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003729bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003730 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003731 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003732
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003733 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003734 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003735 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003736
Sean Callanan686ed8d2010-01-19 20:22:31 +00003737 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003738
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003739 if (Str.empty())
3740 Error(Loc, ".abort detected. Assembly stopping.");
3741 else
3742 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003743 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003744
3745 return false;
3746}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003747
Jim Grosbach4b905842013-09-20 23:08:21 +00003748/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003749/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003750bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003751 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003752 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003753
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003754 // Allow the strings to have escaped octal character sequence.
3755 std::string Filename;
3756 if (parseEscapedString(Filename))
3757 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003758 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003759 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003760
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003761 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003762 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003763
Chris Lattner693fbb82009-07-16 06:14:39 +00003764 // Attempt to switch the lexer to the included file before consuming the end
3765 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003766 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003767 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003768 return true;
3769 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003770
3771 return false;
3772}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003773
Jim Grosbach4b905842013-09-20 23:08:21 +00003774/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003775/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003776bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003777 if (getLexer().isNot(AsmToken::String))
3778 return TokError("expected string in '.incbin' directive");
3779
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003780 // Allow the strings to have escaped octal character sequence.
3781 std::string Filename;
3782 if (parseEscapedString(Filename))
3783 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003784 SMLoc IncbinLoc = getLexer().getLoc();
3785 Lex();
3786
3787 if (getLexer().isNot(AsmToken::EndOfStatement))
3788 return TokError("unexpected token in '.incbin' directive");
3789
Kevin Enderby109f25c2011-12-14 21:47:48 +00003790 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003791 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003792 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3793 return true;
3794 }
3795
3796 return false;
3797}
3798
Jim Grosbach4b905842013-09-20 23:08:21 +00003799/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003800/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003801/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003802bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003803 TheCondStack.push_back(TheCondState);
3804 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003805 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003807 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003808 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003809 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003810 return true;
3811
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003812 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003813 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003814
Sean Callanan686ed8d2010-01-19 20:22:31 +00003815 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003816
3817 TheCondState.CondMet = ExprValue;
3818 TheCondState.Ignore = !TheCondState.CondMet;
3819 }
3820
3821 return false;
3822}
3823
Jim Grosbach4b905842013-09-20 23:08:21 +00003824/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003825/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003826bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003827 TheCondStack.push_back(TheCondState);
3828 TheCondState.TheCond = AsmCond::IfCond;
3829
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003830 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003831 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003832 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003833 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003834
3835 if (getLexer().isNot(AsmToken::EndOfStatement))
3836 return TokError("unexpected token in '.ifb' directive");
3837
3838 Lex();
3839
3840 TheCondState.CondMet = ExpectBlank == Str.empty();
3841 TheCondState.Ignore = !TheCondState.CondMet;
3842 }
3843
3844 return false;
3845}
3846
Jim Grosbach4b905842013-09-20 23:08:21 +00003847/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003848/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003849/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003850bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003851 TheCondStack.push_back(TheCondState);
3852 TheCondState.TheCond = AsmCond::IfCond;
3853
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003854 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003855 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003856 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003857 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003858
3859 if (getLexer().isNot(AsmToken::Comma))
3860 return TokError("unexpected token in '.ifc' directive");
3861
3862 Lex();
3863
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003864 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003865
3866 if (getLexer().isNot(AsmToken::EndOfStatement))
3867 return TokError("unexpected token in '.ifc' directive");
3868
3869 Lex();
3870
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003871 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003872 TheCondState.Ignore = !TheCondState.CondMet;
3873 }
3874
3875 return false;
3876}
3877
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003878/// parseDirectiveIfeqs
3879/// ::= .ifeqs string1, string2
3880bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3881 if (Lexer.isNot(AsmToken::String)) {
3882 TokError("expected string parameter for '.ifeqs' directive");
3883 eatToEndOfStatement();
3884 return true;
3885 }
3886
3887 StringRef String1 = getTok().getStringContents();
3888 Lex();
3889
3890 if (Lexer.isNot(AsmToken::Comma)) {
3891 TokError("expected comma after first string for '.ifeqs' directive");
3892 eatToEndOfStatement();
3893 return true;
3894 }
3895
3896 Lex();
3897
3898 if (Lexer.isNot(AsmToken::String)) {
3899 TokError("expected string parameter for '.ifeqs' directive");
3900 eatToEndOfStatement();
3901 return true;
3902 }
3903
3904 StringRef String2 = getTok().getStringContents();
3905 Lex();
3906
3907 TheCondStack.push_back(TheCondState);
3908 TheCondState.TheCond = AsmCond::IfCond;
3909 TheCondState.CondMet = String1 == String2;
3910 TheCondState.Ignore = !TheCondState.CondMet;
3911
3912 return false;
3913}
3914
Jim Grosbach4b905842013-09-20 23:08:21 +00003915/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003916/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003917bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003918 StringRef Name;
3919 TheCondStack.push_back(TheCondState);
3920 TheCondState.TheCond = AsmCond::IfCond;
3921
3922 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003923 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003924 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003925 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003926 return TokError("expected identifier after '.ifdef'");
3927
3928 Lex();
3929
3930 MCSymbol *Sym = getContext().LookupSymbol(Name);
3931
3932 if (expect_defined)
3933 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3934 else
3935 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3936 TheCondState.Ignore = !TheCondState.CondMet;
3937 }
3938
3939 return false;
3940}
3941
Jim Grosbach4b905842013-09-20 23:08:21 +00003942/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003943/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003944bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003945 if (TheCondState.TheCond != AsmCond::IfCond &&
3946 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003947 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3948 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003949 TheCondState.TheCond = AsmCond::ElseIfCond;
3950
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003951 bool LastIgnoreState = false;
3952 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003953 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003954 if (LastIgnoreState || TheCondState.CondMet) {
3955 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003956 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003957 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003958 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003959 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003960 return true;
3961
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003962 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003963 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003964
Sean Callanan686ed8d2010-01-19 20:22:31 +00003965 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003966 TheCondState.CondMet = ExprValue;
3967 TheCondState.Ignore = !TheCondState.CondMet;
3968 }
3969
3970 return false;
3971}
3972
Jim Grosbach4b905842013-09-20 23:08:21 +00003973/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003974/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003975bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003976 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003977 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003978
Sean Callanan686ed8d2010-01-19 20:22:31 +00003979 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003980
3981 if (TheCondState.TheCond != AsmCond::IfCond &&
3982 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003983 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3984 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003985 TheCondState.TheCond = AsmCond::ElseCond;
3986 bool LastIgnoreState = false;
3987 if (!TheCondStack.empty())
3988 LastIgnoreState = TheCondStack.back().Ignore;
3989 if (LastIgnoreState || TheCondState.CondMet)
3990 TheCondState.Ignore = true;
3991 else
3992 TheCondState.Ignore = false;
3993
3994 return false;
3995}
3996
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003997/// parseDirectiveEnd
3998/// ::= .end
3999bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4000 if (getLexer().isNot(AsmToken::EndOfStatement))
4001 return TokError("unexpected token in '.end' directive");
4002
4003 Lex();
4004
4005 while (Lexer.isNot(AsmToken::Eof))
4006 Lex();
4007
4008 return false;
4009}
4010
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004011/// parseDirectiveError
4012/// ::= .err
4013/// ::= .error [string]
4014bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4015 if (!TheCondStack.empty()) {
4016 if (TheCondStack.back().Ignore) {
4017 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004018 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004019 }
4020 }
4021
4022 if (!WithMessage)
4023 return Error(L, ".err encountered");
4024
4025 StringRef Message = ".error directive invoked in source file";
4026 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4027 if (Lexer.isNot(AsmToken::String)) {
4028 TokError(".error argument must be a string");
4029 eatToEndOfStatement();
4030 return true;
4031 }
4032
4033 Message = getTok().getStringContents();
4034 Lex();
4035 }
4036
4037 Error(L, Message);
4038 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004039}
4040
Jim Grosbach4b905842013-09-20 23:08:21 +00004041/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004042/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004043bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004044 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004045 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004046
Sean Callanan686ed8d2010-01-19 20:22:31 +00004047 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004048
Jim Grosbach4b905842013-09-20 23:08:21 +00004049 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004050 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4051 ".else");
4052 if (!TheCondStack.empty()) {
4053 TheCondState = TheCondStack.back();
4054 TheCondStack.pop_back();
4055 }
4056
4057 return false;
4058}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004059
Eli Bendersky17233942013-01-15 22:59:42 +00004060void AsmParser::initializeDirectiveKindMap() {
4061 DirectiveKindMap[".set"] = DK_SET;
4062 DirectiveKindMap[".equ"] = DK_EQU;
4063 DirectiveKindMap[".equiv"] = DK_EQUIV;
4064 DirectiveKindMap[".ascii"] = DK_ASCII;
4065 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4066 DirectiveKindMap[".string"] = DK_STRING;
4067 DirectiveKindMap[".byte"] = DK_BYTE;
4068 DirectiveKindMap[".short"] = DK_SHORT;
4069 DirectiveKindMap[".value"] = DK_VALUE;
4070 DirectiveKindMap[".2byte"] = DK_2BYTE;
4071 DirectiveKindMap[".long"] = DK_LONG;
4072 DirectiveKindMap[".int"] = DK_INT;
4073 DirectiveKindMap[".4byte"] = DK_4BYTE;
4074 DirectiveKindMap[".quad"] = DK_QUAD;
4075 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004076 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004077 DirectiveKindMap[".single"] = DK_SINGLE;
4078 DirectiveKindMap[".float"] = DK_FLOAT;
4079 DirectiveKindMap[".double"] = DK_DOUBLE;
4080 DirectiveKindMap[".align"] = DK_ALIGN;
4081 DirectiveKindMap[".align32"] = DK_ALIGN32;
4082 DirectiveKindMap[".balign"] = DK_BALIGN;
4083 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4084 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4085 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4086 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4087 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4088 DirectiveKindMap[".org"] = DK_ORG;
4089 DirectiveKindMap[".fill"] = DK_FILL;
4090 DirectiveKindMap[".zero"] = DK_ZERO;
4091 DirectiveKindMap[".extern"] = DK_EXTERN;
4092 DirectiveKindMap[".globl"] = DK_GLOBL;
4093 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004094 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4095 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4096 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4097 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4098 DirectiveKindMap[".reference"] = DK_REFERENCE;
4099 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4100 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4101 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4102 DirectiveKindMap[".comm"] = DK_COMM;
4103 DirectiveKindMap[".common"] = DK_COMMON;
4104 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4105 DirectiveKindMap[".abort"] = DK_ABORT;
4106 DirectiveKindMap[".include"] = DK_INCLUDE;
4107 DirectiveKindMap[".incbin"] = DK_INCBIN;
4108 DirectiveKindMap[".code16"] = DK_CODE16;
4109 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4110 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004111 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004112 DirectiveKindMap[".irp"] = DK_IRP;
4113 DirectiveKindMap[".irpc"] = DK_IRPC;
4114 DirectiveKindMap[".endr"] = DK_ENDR;
4115 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4116 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4117 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4118 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004119 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004120 DirectiveKindMap[".ifb"] = DK_IFB;
4121 DirectiveKindMap[".ifnb"] = DK_IFNB;
4122 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004123 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004124 DirectiveKindMap[".ifnc"] = DK_IFNC;
4125 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4126 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4127 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4128 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4129 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004130 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004131 DirectiveKindMap[".endif"] = DK_ENDIF;
4132 DirectiveKindMap[".skip"] = DK_SKIP;
4133 DirectiveKindMap[".space"] = DK_SPACE;
4134 DirectiveKindMap[".file"] = DK_FILE;
4135 DirectiveKindMap[".line"] = DK_LINE;
4136 DirectiveKindMap[".loc"] = DK_LOC;
4137 DirectiveKindMap[".stabs"] = DK_STABS;
4138 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4139 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4140 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4141 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4142 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4143 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4144 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4145 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4146 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4147 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4148 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4149 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4150 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4151 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4152 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4153 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4154 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4155 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4156 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4157 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4158 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004159 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004160 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4161 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4162 DirectiveKindMap[".macro"] = DK_MACRO;
4163 DirectiveKindMap[".endm"] = DK_ENDM;
4164 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4165 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004166 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004167 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004171 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004172
Rafael Espindola34b9c512012-06-03 23:57:14 +00004173 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004174 for (;;) {
4175 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004176 if (getLexer().is(AsmToken::Eof)) {
4177 Error(DirectiveLoc, "no matching '.endr' in definition");
4178 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004179 }
4180
Rafael Espindola34b9c512012-06-03 23:57:14 +00004181 if (Lexer.is(AsmToken::Identifier) &&
4182 (getTok().getIdentifier() == ".rept")) {
4183 ++NestLevel;
4184 }
4185
4186 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004187 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004188 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004189 EndToken = getTok();
4190 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004191 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4192 TokError("unexpected token in '.endr' directive");
4193 return 0;
4194 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004195 break;
4196 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004197 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004198 }
4199
Rafael Espindola34b9c512012-06-03 23:57:14 +00004200 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004201 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004202 }
4203
4204 const char *BodyStart = StartToken.getLoc().getPointer();
4205 const char *BodyEnd = EndToken.getLoc().getPointer();
4206 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4207
Rafael Espindola34b9c512012-06-03 23:57:14 +00004208 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004209 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004210 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004211}
4212
Jim Grosbach4b905842013-09-20 23:08:21 +00004213void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004214 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004215 OS << ".endr\n";
4216
4217 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004218 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004219
Rafael Espindola34b9c512012-06-03 23:57:14 +00004220 // Create the macro instantiation object and add to the current macro
4221 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004222 MacroInstantiation *MI = new MacroInstantiation(
4223 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004224 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004225
Rafael Espindola34b9c512012-06-03 23:57:14 +00004226 // Jump to the macro instantiation and prime the lexer.
4227 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4228 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4229 Lex();
4230}
4231
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004232/// parseDirectiveRept
4233/// ::= .rep | .rept count
4234bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004235 const MCExpr *CountExpr;
4236 SMLoc CountLoc = getTok().getLoc();
4237 if (parseExpression(CountExpr))
4238 return true;
4239
Rafael Espindola34b9c512012-06-03 23:57:14 +00004240 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004241 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4242 eatToEndOfStatement();
4243 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4244 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004245
4246 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004247 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004248
4249 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004250 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004251
4252 // Eat the end of statement.
4253 Lex();
4254
4255 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004256 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004257 if (!M)
4258 return true;
4259
4260 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4261 // to hold the macro body with substitutions.
4262 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004263 raw_svector_ostream OS(Buf);
4264 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004265 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 return true;
4267 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004268 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004269
4270 return false;
4271}
4272
Jim Grosbach4b905842013-09-20 23:08:21 +00004273/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004274/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004275bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004276 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004277
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004278 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004279 return TokError("expected identifier in '.irp' directive");
4280
Rafael Espindola768b41c2012-06-15 14:02:34 +00004281 if (Lexer.isNot(AsmToken::Comma))
4282 return TokError("expected comma in '.irp' directive");
4283
4284 Lex();
4285
Eli Bendersky38274122013-01-14 23:22:36 +00004286 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004287 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004288 return true;
4289
4290 // Eat the end of statement.
4291 Lex();
4292
4293 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004294 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004295 if (!M)
4296 return true;
4297
4298 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4299 // to hold the macro body with substitutions.
4300 SmallString<256> Buf;
4301 raw_svector_ostream OS(Buf);
4302
Eli Bendersky38274122013-01-14 23:22:36 +00004303 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004304 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004305 return true;
4306 }
4307
Jim Grosbach4b905842013-09-20 23:08:21 +00004308 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004309
4310 return false;
4311}
4312
Jim Grosbach4b905842013-09-20 23:08:21 +00004313/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004314/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004315bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004316 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004317
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004318 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004319 return TokError("expected identifier in '.irpc' directive");
4320
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004321 if (Lexer.isNot(AsmToken::Comma))
4322 return TokError("expected comma in '.irpc' directive");
4323
4324 Lex();
4325
Eli Bendersky38274122013-01-14 23:22:36 +00004326 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004327 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004328 return true;
4329
4330 if (A.size() != 1 || A.front().size() != 1)
4331 return TokError("unexpected token in '.irpc' directive");
4332
4333 // Eat the end of statement.
4334 Lex();
4335
4336 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004337 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004338 if (!M)
4339 return true;
4340
4341 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4342 // to hold the macro body with substitutions.
4343 SmallString<256> Buf;
4344 raw_svector_ostream OS(Buf);
4345
4346 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004347 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004348 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004349 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004350
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004351 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004352 return true;
4353 }
4354
Jim Grosbach4b905842013-09-20 23:08:21 +00004355 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004356
4357 return false;
4358}
4359
Jim Grosbach4b905842013-09-20 23:08:21 +00004360bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004361 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004362 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004363
4364 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004365 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004366 assert(getLexer().is(AsmToken::EndOfStatement));
4367
Jim Grosbach4b905842013-09-20 23:08:21 +00004368 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004369 return false;
4370}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004371
Jim Grosbach4b905842013-09-20 23:08:21 +00004372bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004373 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004374 const MCExpr *Value;
4375 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004376 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004377 return true;
4378 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4379 if (!MCE)
4380 return Error(ExprLoc, "unexpected expression in _emit");
4381 uint64_t IntValue = MCE->getValue();
4382 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4383 return Error(ExprLoc, "literal value out of range for directive");
4384
Chad Rosierc7f552c2013-02-12 21:33:51 +00004385 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4386 return false;
4387}
4388
Jim Grosbach4b905842013-09-20 23:08:21 +00004389bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004390 const MCExpr *Value;
4391 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004392 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004393 return true;
4394 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4395 if (!MCE)
4396 return Error(ExprLoc, "unexpected expression in align");
4397 uint64_t IntValue = MCE->getValue();
4398 if (!isPowerOf2_64(IntValue))
4399 return Error(ExprLoc, "literal value not a power of two greater then zero");
4400
Jim Grosbach4b905842013-09-20 23:08:21 +00004401 Info.AsmRewrites->push_back(
4402 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004403 return false;
4404}
4405
Chad Rosierf43fcf52013-02-13 21:27:17 +00004406// We are comparing pointers, but the pointers are relative to a single string.
4407// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004408static int rewritesSort(const AsmRewrite *AsmRewriteA,
4409 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004410 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4411 return -1;
4412 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4413 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004414
Chad Rosierfce4fab2013-04-08 17:43:47 +00004415 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4416 // rewrite to the same location. Make sure the SizeDirective rewrite is
4417 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4418 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004419 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4420 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004421 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004422
Jim Grosbach4b905842013-09-20 23:08:21 +00004423 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4424 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004425 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004426 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004427}
4428
Jim Grosbach4b905842013-09-20 23:08:21 +00004429bool AsmParser::parseMSInlineAsm(
4430 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4431 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4432 SmallVectorImpl<std::string> &Constraints,
4433 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4434 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004435 SmallVector<void *, 4> InputDecls;
4436 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004437 SmallVector<bool, 4> InputDeclsAddressOf;
4438 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004439 SmallVector<std::string, 4> InputConstraints;
4440 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004441 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004442
Benjamin Kramer1a136112013-02-15 20:37:21 +00004443 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004444
4445 // Prime the lexer.
4446 Lex();
4447
4448 // While we have input, parse each statement.
4449 unsigned InputIdx = 0;
4450 unsigned OutputIdx = 0;
4451 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004452 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004453 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004454 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004455
Chad Rosier149e8e02012-12-12 22:45:52 +00004456 if (Info.ParseError)
4457 return true;
4458
Benjamin Kramer1a136112013-02-15 20:37:21 +00004459 if (Info.Opcode == ~0U)
4460 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004461
Benjamin Kramer1a136112013-02-15 20:37:21 +00004462 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004463
Benjamin Kramer1a136112013-02-15 20:37:21 +00004464 // Build the list of clobbers, outputs and inputs.
4465 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4466 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004467
Benjamin Kramer1a136112013-02-15 20:37:21 +00004468 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004469 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004470 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004471
Benjamin Kramer1a136112013-02-15 20:37:21 +00004472 // Register operand.
4473 if (Operand->isReg() && !Operand->needAddressOf()) {
4474 unsigned NumDefs = Desc.getNumDefs();
4475 // Clobber.
4476 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4477 ClobberRegs.push_back(Operand->getReg());
4478 continue;
4479 }
4480
4481 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004482 StringRef SymName = Operand->getSymName();
4483 if (SymName.empty())
4484 continue;
4485
Chad Rosierdba3fe52013-04-22 22:12:12 +00004486 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004487 if (!OpDecl)
4488 continue;
4489
4490 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004491 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004492 if (isOutput) {
4493 ++InputIdx;
4494 OutputDecls.push_back(OpDecl);
4495 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4496 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004497 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004498 } else {
4499 InputDecls.push_back(OpDecl);
4500 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4501 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004502 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004503 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004504 }
Reid Kleckneree088972013-12-10 18:27:32 +00004505
4506 // Consider implicit defs to be clobbers. Think of cpuid and push.
4507 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4508 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4509 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004510 }
4511
4512 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004513 NumOutputs = OutputDecls.size();
4514 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004515
4516 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004517 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4518 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4519 ClobberRegs.end());
4520 Clobbers.assign(ClobberRegs.size(), std::string());
4521 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4522 raw_string_ostream OS(Clobbers[I]);
4523 IP->printRegName(OS, ClobberRegs[I]);
4524 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004525
4526 // Merge the various outputs and inputs. Output are expected first.
4527 if (NumOutputs || NumInputs) {
4528 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004529 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004530 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004531 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004532 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004533 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004534 }
4535 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004536 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004537 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004538 }
4539 }
4540
4541 // Build the IR assembly string.
4542 std::string AsmStringIR;
4543 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004544 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4545 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004546 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004547 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4548 E = AsmStrRewrites.end();
4549 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004550 AsmRewriteKind Kind = (*I).Kind;
4551 if (Kind == AOK_Delete)
4552 continue;
4553
Chad Rosier8bce6642012-10-18 15:49:34 +00004554 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004555 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004556
Chad Rosier120eefd2013-03-19 17:32:17 +00004557 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004558 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004559 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004560 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004561
Chad Rosier37e755c2012-10-23 17:43:43 +00004562 // Skip the original expression.
4563 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004564 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004565 continue;
4566 }
4567
Chad Rosierff10ed12013-04-12 16:26:42 +00004568 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004569 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004570 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004571 default:
4572 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004573 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004574 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004575 break;
4576 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004577 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004578 break;
4579 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004581 break;
4582 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004583 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004584 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004585 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004586 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004587 default: break;
4588 case 8: OS << "byte ptr "; break;
4589 case 16: OS << "word ptr "; break;
4590 case 32: OS << "dword ptr "; break;
4591 case 64: OS << "qword ptr "; break;
4592 case 80: OS << "xword ptr "; break;
4593 case 128: OS << "xmmword ptr "; break;
4594 case 256: OS << "ymmword ptr "; break;
4595 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004596 break;
4597 case AOK_Emit:
4598 OS << ".byte";
4599 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004600 case AOK_Align: {
4601 unsigned Val = (*I).Val;
4602 OS << ".align " << Val;
4603
4604 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004605 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004606 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4607 break;
4608 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004609 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004610 // Insert the dot if the user omitted it.
4611 OS.flush();
4612 if (AsmStringIR.back() != '.')
4613 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004614 OS << (*I).Val;
4615 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004616 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004617
Chad Rosier8bce6642012-10-18 15:49:34 +00004618 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004619 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004620 }
4621
4622 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004623 if (AsmStart != AsmEnd)
4624 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004625
4626 AsmString = OS.str();
4627 return false;
4628}
4629
Daniel Dunbar01e36072010-07-17 02:26:10 +00004630/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004631MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4632 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004633 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004634}