blob: a53f3699e35e33c5bb5a6d30546374cf692755d4 [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;
62
63 MCAsmMacroParameter() : Required(false) { }
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000064};
65
Eli Benderskya313ae62013-01-16 18:56:50 +000066typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
67
68struct MCAsmMacro {
69 StringRef Name;
70 StringRef Body;
71 MCAsmMacroParameters Parameters;
72
73public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000074 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000075 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000076};
77
Daniel Dunbar43235712010-07-18 18:54:11 +000078/// \brief Helper class for storing information about an active macro
79/// instantiation.
80struct MacroInstantiation {
81 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000082 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000083
84 /// The macro instantiation with substitutions.
85 MemoryBuffer *Instantiation;
86
87 /// The location of the instantiation.
88 SMLoc InstantiationLoc;
89
Daniel Dunbar40f1d852012-12-01 01:38:48 +000090 /// The buffer where parsing should resume upon instantiation completion.
91 int ExitBuffer;
92
Daniel Dunbar43235712010-07-18 18:54:11 +000093 /// The location where parsing should resume upon instantiation completion.
94 SMLoc ExitLoc;
95
96public:
Eli Bendersky38274122013-01-14 23:22:36 +000097 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000098 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000099};
100
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
104
Jim Grosbach4b905842013-09-20 23:08:21 +0000105 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 unsigned Opcode;
107
Jim Grosbach4b905842013-09-20 23:08:21 +0000108 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 bool ParseError;
110
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111 SmallVectorImpl<AsmRewrite> *AsmRewrites;
112
Chad Rosier149e8e02012-12-12 22:45:52 +0000113 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000114 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000115 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116
117 ~ParseStatementInfo() {
118 // Free any parsed operands.
119 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
120 delete ParsedOperands[i];
121 ParsedOperands.clear();
122 }
123};
124
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125/// \brief The concrete assembly parser instance.
126class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000127 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
128 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129private:
130 AsmLexer Lexer;
131 MCContext &Ctx;
132 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000133 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000135 SourceMgr::DiagHandlerTy SavedDiagHandler;
136 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000138
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139 /// This is the current buffer index we're lexing from as managed by the
140 /// SourceMgr object.
141 int CurBuffer;
142
143 AsmCond TheCondState;
144 std::vector<AsmCond> TheCondStack;
145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000147 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000148 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000149 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000150
Jim Grosbach4b905842013-09-20 23:08:21 +0000151 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000152 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000153
Jim Grosbach4b905842013-09-20 23:08:21 +0000154 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000155 std::vector<MacroInstantiation*> ActiveMacros;
156
Jim Grosbach4b905842013-09-20 23:08:21 +0000157 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000158 std::deque<MCAsmMacro> MacroLikeBodies;
159
Daniel Dunbar828984f2010-07-18 18:38:02 +0000160 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000161 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000162
Daniel Dunbar43325c42010-09-09 22:42:56 +0000163 /// Flag tracking whether any errors have been encountered.
164 unsigned HadError : 1;
165
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000166 /// The values from the last parsed cpp hash file line comment if any.
167 StringRef CppHashFilename;
168 int64_t CppHashLineNumber;
169 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000170 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000171 /// When generating dwarf for assembly source files we need to calculate the
172 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000173 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000174 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
175 SMLoc LastQueryIDLoc;
176 int LastQueryBuffer;
177 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000178
Devang Patela173ee52012-01-31 18:14:05 +0000179 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
180 unsigned AssemblerDialect;
181
Jim Grosbach4b905842013-09-20 23:08:21 +0000182 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000183 bool IsDarwin;
184
Jim Grosbach4b905842013-09-20 23:08:21 +0000185 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000186 bool ParsingInlineAsm;
187
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000189 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000191 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000192
193 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
194
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000195 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000196 ExtensionDirectiveHandler Handler) {
197 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000198 }
199
200public:
201 /// @name MCAsmParser Interface
202 /// {
203
204 virtual SourceMgr &getSourceManager() { return SrcMgr; }
205 virtual MCAsmLexer &getLexer() { return Lexer; }
206 virtual MCContext &getContext() { return Ctx; }
207 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000208 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000209 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000210 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000211 else
212 return AssemblerDialect;
213 }
214 virtual void setAssemblerDialect(unsigned i) {
215 AssemblerDialect = i;
216 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000218 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000219 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000220 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000221 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000222 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000223
Craig Topper5f96ca52012-08-29 05:48:09 +0000224 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000225
Chad Rosier49963552012-10-13 00:26:04 +0000226 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000227 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000228
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000229 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000231 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000232 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 SmallVectorImpl<std::string> &Clobbers,
234 const MCInstrInfo *MII,
235 const MCInstPrinter *IP,
236 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000237
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000238 bool parseExpression(const MCExpr *&Res);
239 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000240 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
242 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000243
Jim Grosbach4b905842013-09-20 23:08:21 +0000244 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000245 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000246 virtual bool parseIdentifier(StringRef &Res);
247 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000248
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000249 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000250 /// }
251
252private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000253
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool parseStatement(ParseStatementInfo &Info);
255 void eatToEndOfLine();
256 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000257
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000259 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000260 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters,
262 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000263 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000264
Eli Benderskya313ae62013-01-16 18:56:50 +0000265 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Lookup a previously defined macro.
272 /// \param Name Macro name.
273 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000285 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000286 ///
287 /// \param M The macro.
288 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
291 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
David Majnemer91fc4c22014-01-29 18:57:46 +0000294 /// \brief Extract AsmTokens for a macro argument.
295 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
297 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000299
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 void printMacroInstantiations();
301 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000302 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000303 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000305 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 /// \brief Enter the specified file. This returns true on failure.
308 bool enterIncludeFile(const std::string &Filename);
309
310 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000311 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000312 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000314 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000315 /// current token is not set; clients should ensure Lex() is called
316 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000317 ///
318 /// \param InBuffer If not -1, should be the known buffer id that contains the
319 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000320 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000321
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000322 /// \brief Parse up to the end of statement and a return the contents from the
323 /// current token until the end of the statement; the current token on exit
324 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000325 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000326
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000327 /// \brief Parse until the end of a statement or a comma is encountered,
328 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000332 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000333
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
335 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
336 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000337
Jim Grosbach4b905842013-09-20 23:08:21 +0000338 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000339
Eli Bendersky17233942013-01-15 22:59:42 +0000340 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000341 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_NO_DIRECTIVE, // Placeholder
343 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000344 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
345 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000346 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000348 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
350 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
351 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
352 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000353 DK_IF, DK_IFNE, DK_IFB, DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF,
354 DK_IFNDEF, DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000355 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
356 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
357 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
358 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
359 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
360 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000361 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000362 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000363 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000364 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366 };
367
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000369 /// directives parsed by this class.
370 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371
372 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
374 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000375 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
377 bool parseDirectiveFill(); // ".fill"
378 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000379 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
381 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000382 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384
Eli Bendersky17233942013-01-15 22:59:42 +0000385 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveFile(SMLoc DirectiveLoc);
387 bool parseDirectiveLine();
388 bool parseDirectiveLoc();
389 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000390
391 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000393 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFISections();
395 bool parseDirectiveCFIStartProc();
396 bool parseDirectiveCFIEndProc();
397 bool parseDirectiveCFIDefCfaOffset();
398 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIAdjustCfaOffset();
400 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
404 bool parseDirectiveCFIRememberState();
405 bool parseDirectiveCFIRestoreState();
406 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIEscape();
409 bool parseDirectiveCFISignalFrame();
410 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
412 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
414 bool parseDirectiveEndMacro(StringRef Directive);
415 bool parseDirectiveMacro(SMLoc DirectiveLoc);
416 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000417
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424
Eli Bendersky17233942013-01-15 22:59:42 +0000425 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000427
428 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveAbort(); // ".abort"
438 bool parseDirectiveInclude(); // ".include"
439 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000440
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +0000441 // ".if" or ".ifne"
442 bool parseDirectiveIf(SMLoc DirectiveLoc);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000443 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000445 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000447 // ".ifeqs"
448 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000449 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
451 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
452 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
453 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000454 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000455
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000457 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000458
Rafael Espindola34b9c512012-06-03 23:57:14 +0000459 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
461 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000462 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000463 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
465 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
466 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000467
Chad Rosierc7f552c2013-02-12 21:33:51 +0000468 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000470 size_t Len);
471
472 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000473 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000474
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000475 // "end"
476 bool parseDirectiveEnd(SMLoc DirectiveLoc);
477
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000478 // ".err" or ".error"
479 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000480
Eli Bendersky17233942013-01-15 22:59:42 +0000481 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000482};
Daniel Dunbar86033402010-07-12 17:54:38 +0000483}
484
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000485namespace llvm {
486
487extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000488extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000489extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000490
491}
492
Chris Lattnerc35681b2010-01-19 19:46:13 +0000493enum { DEFAULT_ADDRSPACE = 0 };
494
Jim Grosbach4b905842013-09-20 23:08:21 +0000495AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
496 const MCAsmInfo &_MAI)
497 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
498 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
499 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
500 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000501 // Save the old handler.
502 SavedDiagHandler = SrcMgr.getDiagHandler();
503 SavedDiagContext = SrcMgr.getDiagContext();
504 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000505 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000506 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000507
Daniel Dunbarc5011082010-07-12 18:12:02 +0000508 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000509 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
510 case MCObjectFileInfo::IsCOFF:
511 PlatformParser = createCOFFAsmParser();
512 PlatformParser->Initialize(*this);
513 break;
514 case MCObjectFileInfo::IsMachO:
515 PlatformParser = createDarwinAsmParser();
516 PlatformParser->Initialize(*this);
517 IsDarwin = true;
518 break;
519 case MCObjectFileInfo::IsELF:
520 PlatformParser = createELFAsmParser();
521 PlatformParser->Initialize(*this);
522 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000523 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000524
Eli Bendersky17233942013-01-15 22:59:42 +0000525 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000526}
527
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000528AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000529 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
530
531 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000532 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
533 ie = MacroMap.end();
534 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000535 delete it->getValue();
536
Daniel Dunbarc5011082010-07-12 18:12:02 +0000537 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000538}
539
Jim Grosbach4b905842013-09-20 23:08:21 +0000540void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000541 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000542 for (std::vector<MacroInstantiation *>::const_reverse_iterator
543 it = ActiveMacros.rbegin(),
544 ie = ActiveMacros.rend();
545 it != ie; ++it)
546 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000547 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000548}
549
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000550void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
551 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
552 printMacroInstantiations();
553}
554
Chris Lattnera3a06812011-10-16 04:47:35 +0000555bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000556 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000557 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000558 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
559 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000560 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000561}
562
Chris Lattnera3a06812011-10-16 04:47:35 +0000563bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000564 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000565 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
566 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000567 return true;
568}
569
Jim Grosbach4b905842013-09-20 23:08:21 +0000570bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000571 std::string IncludedFile;
572 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 if (NewBuf == -1)
574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000575
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000577
Sean Callanan7a77eae2010-01-21 00:19:58 +0000578 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000579
Sean Callanan7a77eae2010-01-21 00:19:58 +0000580 return false;
581}
Daniel Dunbar43235712010-07-18 18:54:11 +0000582
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000583/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000584/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000586bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 std::string IncludedFile;
588 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
589 if (NewBuf == -1)
590 return true;
591
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000592 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000593 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000594 return false;
595}
596
Jim Grosbach4b905842013-09-20 23:08:21 +0000597void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000598 if (InBuffer != -1) {
599 CurBuffer = InBuffer;
600 } else {
601 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
602 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000603 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
604}
605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606const AsmToken &AsmParser::Lex() {
607 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000608
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 if (tok->is(AsmToken::Eof)) {
610 // If this is the end of an included file, pop the parent file off the
611 // include stack.
612 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
613 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000614 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000615 tok = &Lexer.Lex();
616 }
617 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000620 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000621
Sean Callanan7a77eae2010-01-21 00:19:58 +0000622 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000623}
624
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000625bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000626 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000627 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000628 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000629
Chris Lattner36e02122009-06-21 20:54:55 +0000630 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000631 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000632
633 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000634 AsmCond StartingCondState = TheCondState;
635
Kevin Enderby6469fc22011-11-01 22:27:22 +0000636 // If we are generating dwarf for assembly source files save the initial text
637 // section and generate a .file directive.
638 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000639 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000640 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
641 getStreamer().EmitLabel(SectionStartSym);
642 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000643 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000644 StringRef(),
645 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.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000665 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000666 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000667 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000668 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000670 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000671
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000672 // Check to see that all assembler local symbols were actually defined.
673 // Targets that don't do subsections via symbols may not want this, though,
674 // so conservatively exclude them. Only do this if we're finalizing, though,
675 // as otherwise we won't necessarilly have seen everything yet.
676 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
677 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
678 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000679 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000680 i != e; ++i) {
681 MCSymbol *Sym = i->getValue();
682 // Variable symbols may not be marked as defined, so check those
683 // explicitly. If we know it's a variable, we have a definition for
684 // the purposes of this check.
685 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
686 // FIXME: We would really like to refer back to where the symbol was
687 // first referenced for a source location. We need to add something
688 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000689 printMessage(
690 getLexer().getLoc(), SourceMgr::DK_Error,
691 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000692 }
693 }
694
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000695 // Finalize the output stream if there are no errors and if the client wants
696 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000697 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000698 Out.Finish();
699
Chris Lattner73f36112009-07-02 21:53:43 +0000700 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000701}
702
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000703void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000704 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000705 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000706 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 }
708}
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000712 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000714
Chris Lattnere5074c42009-06-22 01:29:09 +0000715 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000716 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000718}
719
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000720StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 const char *Start = getTok().getLoc().getPointer();
722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000724 Lex();
725
726 const char *End = getTok().getLoc().getPointer();
727 return StringRef(Start, End - Start);
728}
Chris Lattner78db3622009-06-22 05:51:26 +0000729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 const char *Start = getTok().getLoc().getPointer();
732
733 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000734 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 Lex();
736
737 const char *End = getTok().getLoc().getPointer();
738 return StringRef(Start, End - Start);
739}
740
Jim Grosbach4b905842013-09-20 23:08:21 +0000741/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742/// NOTE: This assumes the leading '(' has already been consumed.
743///
744/// parenexpr ::= expr)
745///
Jim Grosbach4b905842013-09-20 23:08:21 +0000746bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
747 if (parseExpression(Res))
748 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000749 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000751 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000752 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753 return false;
754}
Chris Lattner78db3622009-06-22 05:51:26 +0000755
Jim Grosbach4b905842013-09-20 23:08:21 +0000756/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757/// NOTE: This assumes the leading '[' has already been consumed.
758///
759/// bracketexpr ::= expr]
760///
Jim Grosbach4b905842013-09-20 23:08:21 +0000761bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
762 if (parseExpression(Res))
763 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 if (Lexer.isNot(AsmToken::RBrac))
765 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000766 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000767 Lex();
768 return false;
769}
770
Jim Grosbach4b905842013-09-20 23:08:21 +0000771/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= (parenexpr
773/// primaryexpr ::= symbol
774/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000775/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000777bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000778 SMLoc FirstTokenLoc = getLexer().getLoc();
779 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
780 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000781 default:
782 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000783 // If we have an error assume that we've already handled it.
784 case AsmToken::Error:
785 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000786 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000787 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000788 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000789 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000790 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000793 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000794 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000795 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000797 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000798 if (FirstTokenKind == AsmToken::Dollar) {
799 if (Lexer.getMAI().getDollarIsPC()) {
800 // This is a '$' reference, which references the current PC. Emit a
801 // temporary label to the streamer and refer to it.
802 MCSymbol *Sym = Ctx.CreateTempSymbol();
803 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000804 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
805 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 EndLoc = FirstTokenLoc;
807 return false;
808 } else
809 return Error(FirstTokenLoc, "invalid token in expression");
810 return true;
811 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000812 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000813 // Parse symbol variant
814 std::pair<StringRef, StringRef> Split;
815 if (!MAI.useParensForSymbolVariant()) {
816 Split = Identifier.split('@');
817 } else if (Lexer.is(AsmToken::LParen)) {
818 Lexer.Lex(); // eat (
819 StringRef VName;
820 parseIdentifier(VName);
821 if (Lexer.isNot(AsmToken::RParen)) {
822 return Error(Lexer.getTok().getLoc(),
823 "unexpected token in variant, expected ')'");
824 }
825 Lexer.Lex(); // eat )
826 Split = std::make_pair(Identifier, VName);
827 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000828
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000829 EndLoc = SMLoc::getFromPointer(Identifier.end());
830
Daniel Dunbard20cda02009-10-16 01:34:54 +0000831 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000832 StringRef SymbolName = Identifier;
833 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000834
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000835 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000836 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000837 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000838 if (Variant != MCSymbolRefExpr::VK_Invalid) {
839 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000840 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000841 Variant = MCSymbolRefExpr::VK_None;
842 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000843 return Error(SMLoc::getFromPointer(Split.second.begin()),
844 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000845 }
846 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000847
Hans Wennborgce69d772013-10-18 20:46:28 +0000848 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
849
Daniel Dunbard20cda02009-10-16 01:34:54 +0000850 // If this is an absolute variable reference, substitute it now to preserve
851 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000852 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000853 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000854 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000855
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000856 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000857 return false;
858 }
859
860 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000861 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000862 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000863 }
David Woodhousef42a6662014-02-01 16:20:54 +0000864 case AsmToken::BigNum:
865 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000866 case AsmToken::Integer: {
867 SMLoc Loc = getTok().getLoc();
868 int64_t IntVal = getTok().getIntVal();
869 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000870 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000871 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000872 // Look for 'b' or 'f' following an Integer as a directional label
873 if (Lexer.getKind() == AsmToken::Identifier) {
874 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000875 // Lookup the symbol variant if used.
876 std::pair<StringRef, StringRef> Split = IDVal.split('@');
877 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
878 if (Split.first.size() != IDVal.size()) {
879 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
880 if (Variant == MCSymbolRefExpr::VK_Invalid) {
881 Variant = MCSymbolRefExpr::VK_None;
882 return TokError("invalid variant '" + Split.second + "'");
883 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000884 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000886 if (IDVal == "f" || IDVal == "b") {
887 MCSymbol *Sym =
888 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000889 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000890 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000892 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 Lex(); // Eat identifier.
894 }
895 }
Chris Lattner78db3622009-06-22 05:51:26 +0000896 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000898 case AsmToken::Real: {
899 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000900 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000901 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000902 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 Lex(); // Eat token.
904 return false;
905 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000906 case AsmToken::Dot: {
907 // This is a '.' reference, which references the current PC. Emit a
908 // temporary label to the streamer and refer to it.
909 MCSymbol *Sym = Ctx.CreateTempSymbol();
910 Out.EmitLabel(Sym);
911 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000912 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000913 Lex(); // Eat identifier.
914 return false;
915 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000916 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000917 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000918 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000919 case AsmToken::LBrac:
920 if (!PlatformParser->HasBracketExpressions())
921 return TokError("brackets expression not supported on this target");
922 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000928 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000936 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000937 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000938 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000940 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000942 }
943}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000944
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000945bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000946 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000947 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000948}
949
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000951AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000952 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000953 // Ask the target implementation about this expression first.
954 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
955 if (NewE)
956 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 // Recurse over the given expression, rebuilding it to apply the given variant
958 // if there is exactly one symbol.
959 switch (E->getKind()) {
960 case MCExpr::Target:
961 case MCExpr::Constant:
962 return 0;
963
964 case MCExpr::SymbolRef: {
965 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
966
967 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000968 TokError("invalid variant on expression '" + getTok().getIdentifier() +
969 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 return E;
971 }
972
973 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
974 }
975
976 case MCExpr::Unary: {
977 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000978 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979 if (!Sub)
980 return 0;
981 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
982 }
983
984 case MCExpr::Binary: {
985 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
987 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988
989 if (!LHS && !RHS)
990 return 0;
991
Jim Grosbach4b905842013-09-20 23:08:21 +0000992 if (!LHS)
993 LHS = BE->getLHS();
994 if (!RHS)
995 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
997 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
998 }
999 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001000
Craig Toppera2886c22012-02-07 05:05:23 +00001001 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002}
1003
Jim Grosbach4b905842013-09-20 23:08:21 +00001004/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001005///
Jim Grosbachbd164242011-08-20 16:24:13 +00001006/// expr ::= expr &&,|| expr -> lowest.
1007/// expr ::= expr |,^,&,! expr
1008/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1009/// expr ::= expr <<,>> expr
1010/// expr ::= expr +,- expr
1011/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001012/// expr ::= primaryexpr
1013///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001014bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001015 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001016 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001018 return true;
1019
Daniel Dunbar55f16672010-09-17 02:47:07 +00001020 // As a special case, we support 'a op b @ modifier' by rewriting the
1021 // expression to include the modifier. This is inefficient, but in general we
1022 // expect users to use 'a@modifier op b'.
1023 if (Lexer.getKind() == AsmToken::At) {
1024 Lex();
1025
1026 if (Lexer.isNot(AsmToken::Identifier))
1027 return TokError("unexpected symbol modifier following '@'");
1028
1029 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (Variant == MCSymbolRefExpr::VK_Invalid)
1032 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1033
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001035 if (!ModifiedRes) {
1036 return TokError("invalid modifier '" + getTok().getIdentifier() +
1037 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001039
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 Res = ModifiedRes;
1041 Lex();
1042 }
1043
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001044 // Try to constant fold it up front, if possible.
1045 int64_t Value;
1046 if (Res->EvaluateAsAbsolute(Value))
1047 Res = MCConstantExpr::Create(Value, getContext());
1048
1049 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001050}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001051
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001052bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001053 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001054 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001055}
1056
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001058 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001059
Daniel Dunbar75630b32009-06-30 02:10:03 +00001060 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001061 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062 return true;
1063
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001064 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001065 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001066
1067 return false;
1068}
1069
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001071 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001072 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001073 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001074 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075
Jim Grosbach4b905842013-09-20 23:08:21 +00001076 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001079 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001082 return 1;
1083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Low Precedence: |, &, ^
1085 //
1086 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 case AsmToken::EqualEqual:
1099 Kind = MCBinaryExpr::EQ;
1100 return 3;
1101 case AsmToken::ExclaimEqual:
1102 case AsmToken::LessGreater:
1103 Kind = MCBinaryExpr::NE;
1104 return 3;
1105 case AsmToken::Less:
1106 Kind = MCBinaryExpr::LT;
1107 return 3;
1108 case AsmToken::LessEqual:
1109 Kind = MCBinaryExpr::LTE;
1110 return 3;
1111 case AsmToken::Greater:
1112 Kind = MCBinaryExpr::GT;
1113 return 3;
1114 case AsmToken::GreaterEqual:
1115 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001116 return 3;
1117
Jim Grosbach4b905842013-09-20 23:08:21 +00001118 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 case AsmToken::LessLess:
1120 Kind = MCBinaryExpr::Shl;
1121 return 4;
1122 case AsmToken::GreaterGreater:
1123 Kind = MCBinaryExpr::Shr;
1124 return 4;
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127 case AsmToken::Plus:
1128 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130 case AsmToken::Minus:
1131 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001132 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 }
1145}
1146
Jim Grosbach4b905842013-09-20 23:08:21 +00001147/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001149bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001150 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001152 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001153 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001154
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 // If the next token is lower precedence than we are allowed to eat, return
1156 // successfully with what we ate already.
1157 if (TokPrec < Precedence)
1158 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Sean Callanan686ed8d2010-01-19 20:22:31 +00001160 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001163 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 if (parsePrimaryExpr(RHS, EndLoc))
1165 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 // If BinOp binds less tightly with RHS than the operator after RHS, let
1168 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001169 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001171 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1172 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001174 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001175 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001176 }
1177}
1178
Chris Lattner36e02122009-06-21 20:54:55 +00001179/// ParseStatement:
1180/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001181/// ::= Label* Directive ...Operands... EndOfStatement
1182/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001183bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001184 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001185 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001186 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001187 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001188 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001189
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001190 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001191 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001192 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001193 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001194 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001195 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001196 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001197 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001198
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001199 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001200 if (Lexer.is(AsmToken::Integer)) {
1201 LocalLabelVal = getTok().getIntVal();
1202 if (LocalLabelVal < 0) {
1203 if (!TheCondState.Ignore)
1204 return TokError("unexpected token at start of statement");
1205 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001206 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001207 IDVal = getTok().getString();
1208 Lex(); // Consume the integer token to be used as an identifier token.
1209 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001210 if (!TheCondState.Ignore)
1211 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001212 }
1213 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001214 } else if (Lexer.is(AsmToken::Dot)) {
1215 // Treat '.' as a valid identifier in this context.
1216 Lex();
1217 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001218 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001219 if (!TheCondState.Ignore)
1220 return TokError("unexpected token at start of statement");
1221 IDVal = "";
1222 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001223
Chris Lattner926885c2010-04-17 18:14:27 +00001224 // Handle conditional assembly here before checking for skipping. We
1225 // have to do this so that .endif isn't skipped in a ".if 0" block for
1226 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001227 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001228 DirectiveKindMap.find(IDVal);
1229 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1230 ? DK_NO_DIRECTIVE
1231 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001232 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001233 default:
1234 break;
1235 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001236 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001237 return parseDirectiveIf(IDLoc);
1238 case DK_IFB:
1239 return parseDirectiveIfb(IDLoc, true);
1240 case DK_IFNB:
1241 return parseDirectiveIfb(IDLoc, false);
1242 case DK_IFC:
1243 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001244 case DK_IFEQS:
1245 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001246 case DK_IFNC:
1247 return parseDirectiveIfc(IDLoc, false);
1248 case DK_IFDEF:
1249 return parseDirectiveIfdef(IDLoc, true);
1250 case DK_IFNDEF:
1251 case DK_IFNOTDEF:
1252 return parseDirectiveIfdef(IDLoc, false);
1253 case DK_ELSEIF:
1254 return parseDirectiveElseIf(IDLoc);
1255 case DK_ELSE:
1256 return parseDirectiveElse(IDLoc);
1257 case DK_ENDIF:
1258 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001259 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001260
Eli Bendersky88024712013-01-16 19:32:36 +00001261 // Ignore the statement if in the middle of inactive conditional
1262 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001263 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001264 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001265 return false;
1266 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001267
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001268 // FIXME: Recurse on local labels?
1269
1270 // See what kind of statement we have.
1271 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001272 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001273 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001274
Chris Lattner36e02122009-06-21 20:54:55 +00001275 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001276 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001277
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001278 // Diagnose attempt to use '.' as a label.
1279 if (IDVal == ".")
1280 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1281
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001282 // Diagnose attempt to use a variable as a label.
1283 //
1284 // FIXME: Diagnostics. Note the location of the definition as a label.
1285 // FIXME: This doesn't diagnose assignment to a symbol which has been
1286 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001287 MCSymbol *Sym;
1288 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001289 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001290 else
1291 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001292 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001293 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001294
Daniel Dunbare73b2672009-08-26 22:13:22 +00001295 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001296 if (!ParsingInlineAsm)
1297 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001298
Kevin Enderbye7739d42011-12-09 18:09:40 +00001299 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001300 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001301 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001302 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1303 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001304
Tim Northover1744d0a2013-10-25 12:49:50 +00001305 getTargetParser().onLabelParsed(Sym);
1306
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001307 // Consume any end of statement token, if present, to avoid spurious
1308 // AddBlankLine calls().
1309 if (Lexer.is(AsmToken::EndOfStatement)) {
1310 Lex();
1311 if (Lexer.is(AsmToken::Eof))
1312 return false;
1313 }
1314
Eli Friedman0f4871d2012-10-22 23:58:19 +00001315 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001316 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001317
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001318 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001319 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001320 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001321
Jim Grosbach4b905842013-09-20 23:08:21 +00001322 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001323
1324 default: // Normal instruction or directive.
1325 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001326 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001327
1328 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001329 if (areMacrosEnabled())
1330 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1331 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001332 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001333
Michael J. Spencer530ce852010-10-09 11:00:50 +00001334 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001335
Eli Bendersky17233942013-01-15 22:59:42 +00001336 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001337 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001338 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001339 //
Eli Bendersky17233942013-01-15 22:59:42 +00001340 // 1. The target-specific assembly parser. Some directives are target
1341 // specific or may potentially behave differently on certain targets.
1342 // 2. Asm parser extensions. For example, platform-specific parsers
1343 // (like the ELF parser) register themselves as extensions.
1344 // 3. The generic directive parser implemented by this class. These are
1345 // all the directives that behave in a target and platform independent
1346 // manner, or at least have a default behavior that's shared between
1347 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001348
Eli Bendersky17233942013-01-15 22:59:42 +00001349 // First query the target-specific parser. It will return 'true' if it
1350 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001351 if (!getTargetParser().ParseDirective(ID))
1352 return false;
1353
Alp Tokercb402912014-01-24 17:20:08 +00001354 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001355 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001356 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1357 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001358 if (Handler.first)
1359 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1360
1361 // Finally, if no one else is interested in this directive, it must be
1362 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001363 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001364 default:
1365 break;
1366 case DK_SET:
1367 case DK_EQU:
1368 return parseDirectiveSet(IDVal, true);
1369 case DK_EQUIV:
1370 return parseDirectiveSet(IDVal, false);
1371 case DK_ASCII:
1372 return parseDirectiveAscii(IDVal, false);
1373 case DK_ASCIZ:
1374 case DK_STRING:
1375 return parseDirectiveAscii(IDVal, true);
1376 case DK_BYTE:
1377 return parseDirectiveValue(1);
1378 case DK_SHORT:
1379 case DK_VALUE:
1380 case DK_2BYTE:
1381 return parseDirectiveValue(2);
1382 case DK_LONG:
1383 case DK_INT:
1384 case DK_4BYTE:
1385 return parseDirectiveValue(4);
1386 case DK_QUAD:
1387 case DK_8BYTE:
1388 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001389 case DK_OCTA:
1390 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001391 case DK_SINGLE:
1392 case DK_FLOAT:
1393 return parseDirectiveRealValue(APFloat::IEEEsingle);
1394 case DK_DOUBLE:
1395 return parseDirectiveRealValue(APFloat::IEEEdouble);
1396 case DK_ALIGN: {
1397 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1398 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1399 }
1400 case DK_ALIGN32: {
1401 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1402 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1403 }
1404 case DK_BALIGN:
1405 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1406 case DK_BALIGNW:
1407 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1408 case DK_BALIGNL:
1409 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1410 case DK_P2ALIGN:
1411 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1412 case DK_P2ALIGNW:
1413 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1414 case DK_P2ALIGNL:
1415 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1416 case DK_ORG:
1417 return parseDirectiveOrg();
1418 case DK_FILL:
1419 return parseDirectiveFill();
1420 case DK_ZERO:
1421 return parseDirectiveZero();
1422 case DK_EXTERN:
1423 eatToEndOfStatement(); // .extern is the default, ignore it.
1424 return false;
1425 case DK_GLOBL:
1426 case DK_GLOBAL:
1427 return parseDirectiveSymbolAttribute(MCSA_Global);
1428 case DK_LAZY_REFERENCE:
1429 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1430 case DK_NO_DEAD_STRIP:
1431 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1432 case DK_SYMBOL_RESOLVER:
1433 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1434 case DK_PRIVATE_EXTERN:
1435 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1436 case DK_REFERENCE:
1437 return parseDirectiveSymbolAttribute(MCSA_Reference);
1438 case DK_WEAK_DEFINITION:
1439 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1440 case DK_WEAK_REFERENCE:
1441 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1442 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1443 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1444 case DK_COMM:
1445 case DK_COMMON:
1446 return parseDirectiveComm(/*IsLocal=*/false);
1447 case DK_LCOMM:
1448 return parseDirectiveComm(/*IsLocal=*/true);
1449 case DK_ABORT:
1450 return parseDirectiveAbort();
1451 case DK_INCLUDE:
1452 return parseDirectiveInclude();
1453 case DK_INCBIN:
1454 return parseDirectiveIncbin();
1455 case DK_CODE16:
1456 case DK_CODE16GCC:
1457 return TokError(Twine(IDVal) + " not supported yet");
1458 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001459 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001460 case DK_IRP:
1461 return parseDirectiveIrp(IDLoc);
1462 case DK_IRPC:
1463 return parseDirectiveIrpc(IDLoc);
1464 case DK_ENDR:
1465 return parseDirectiveEndr(IDLoc);
1466 case DK_BUNDLE_ALIGN_MODE:
1467 return parseDirectiveBundleAlignMode();
1468 case DK_BUNDLE_LOCK:
1469 return parseDirectiveBundleLock();
1470 case DK_BUNDLE_UNLOCK:
1471 return parseDirectiveBundleUnlock();
1472 case DK_SLEB128:
1473 return parseDirectiveLEB128(true);
1474 case DK_ULEB128:
1475 return parseDirectiveLEB128(false);
1476 case DK_SPACE:
1477 case DK_SKIP:
1478 return parseDirectiveSpace(IDVal);
1479 case DK_FILE:
1480 return parseDirectiveFile(IDLoc);
1481 case DK_LINE:
1482 return parseDirectiveLine();
1483 case DK_LOC:
1484 return parseDirectiveLoc();
1485 case DK_STABS:
1486 return parseDirectiveStabs();
1487 case DK_CFI_SECTIONS:
1488 return parseDirectiveCFISections();
1489 case DK_CFI_STARTPROC:
1490 return parseDirectiveCFIStartProc();
1491 case DK_CFI_ENDPROC:
1492 return parseDirectiveCFIEndProc();
1493 case DK_CFI_DEF_CFA:
1494 return parseDirectiveCFIDefCfa(IDLoc);
1495 case DK_CFI_DEF_CFA_OFFSET:
1496 return parseDirectiveCFIDefCfaOffset();
1497 case DK_CFI_ADJUST_CFA_OFFSET:
1498 return parseDirectiveCFIAdjustCfaOffset();
1499 case DK_CFI_DEF_CFA_REGISTER:
1500 return parseDirectiveCFIDefCfaRegister(IDLoc);
1501 case DK_CFI_OFFSET:
1502 return parseDirectiveCFIOffset(IDLoc);
1503 case DK_CFI_REL_OFFSET:
1504 return parseDirectiveCFIRelOffset(IDLoc);
1505 case DK_CFI_PERSONALITY:
1506 return parseDirectiveCFIPersonalityOrLsda(true);
1507 case DK_CFI_LSDA:
1508 return parseDirectiveCFIPersonalityOrLsda(false);
1509 case DK_CFI_REMEMBER_STATE:
1510 return parseDirectiveCFIRememberState();
1511 case DK_CFI_RESTORE_STATE:
1512 return parseDirectiveCFIRestoreState();
1513 case DK_CFI_SAME_VALUE:
1514 return parseDirectiveCFISameValue(IDLoc);
1515 case DK_CFI_RESTORE:
1516 return parseDirectiveCFIRestore(IDLoc);
1517 case DK_CFI_ESCAPE:
1518 return parseDirectiveCFIEscape();
1519 case DK_CFI_SIGNAL_FRAME:
1520 return parseDirectiveCFISignalFrame();
1521 case DK_CFI_UNDEFINED:
1522 return parseDirectiveCFIUndefined(IDLoc);
1523 case DK_CFI_REGISTER:
1524 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001525 case DK_CFI_WINDOW_SAVE:
1526 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001527 case DK_MACROS_ON:
1528 case DK_MACROS_OFF:
1529 return parseDirectiveMacrosOnOff(IDVal);
1530 case DK_MACRO:
1531 return parseDirectiveMacro(IDLoc);
1532 case DK_ENDM:
1533 case DK_ENDMACRO:
1534 return parseDirectiveEndMacro(IDVal);
1535 case DK_PURGEM:
1536 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001537 case DK_END:
1538 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001539 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001540 return parseDirectiveError(IDLoc, false);
1541 case DK_ERROR:
1542 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001543 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001544
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001545 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001546 }
Chris Lattner36e02122009-06-21 20:54:55 +00001547
Chad Rosierc7f552c2013-02-12 21:33:51 +00001548 // __asm _emit or __asm __emit
1549 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1550 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001551 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001552
1553 // __asm align
1554 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001555 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001556
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001557 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001558
Chris Lattner7cbfa442010-05-19 23:34:33 +00001559 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001560 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001561 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001562 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001563 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001564 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001565
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001566 // Dump the parsed representation, if requested.
1567 if (getShowParsedOperands()) {
1568 SmallString<256> Str;
1569 raw_svector_ostream OS(Str);
1570 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001571 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001572 if (i != 0)
1573 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001574 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001575 }
1576 OS << "]";
1577
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001579 }
1580
Kevin Enderby6469fc22011-11-01 22:27:22 +00001581 // If we are generating dwarf for assembly source files and the current
1582 // section is the initial text section then generate a .loc directive for
1583 // the instruction.
1584 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001585 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001587
Eli Bendersky88024712013-01-16 19:32:36 +00001588 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001589
Eli Bendersky88024712013-01-16 19:32:36 +00001590 // If we previously parsed a cpp hash file line comment then make sure the
1591 // current Dwarf File is for the CppHashFilename if not then emit the
1592 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001593 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001595 if (CppHashFilename.size() != 0) {
1596 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001597 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001598 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001599 getContext().nextGenDwarfFileNumber(), StringRef(),
1600 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001601
Jim Grosbach4b905842013-09-20 23:08:21 +00001602 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1603 // cache with the different Loc from the call above we save the last
1604 // info we queried here with SrcMgr.FindLineNumber().
1605 unsigned CppHashLocLineNo;
1606 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1607 CppHashLocLineNo = LastQueryLine;
1608 else {
1609 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1610 LastQueryLine = CppHashLocLineNo;
1611 LastQueryIDLoc = CppHashLoc;
1612 LastQueryBuffer = CppHashBuf;
1613 }
1614 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001615 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001616
Jim Grosbach4b905842013-09-20 23:08:21 +00001617 getStreamer().EmitDwarfLocDirective(
1618 getContext().getGenDwarfFileNumber(), Line, 0,
1619 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1620 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001621 }
1622
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001623 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001624 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001625 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001626 HadError = getTargetParser().MatchAndEmitInstruction(
1627 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1628 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001629 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001630
Chris Lattnera2a9d162010-09-11 16:18:25 +00001631 // Don't skip the rest of the line, the instruction parser is responsible for
1632 // that.
1633 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001634}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001635
Jim Grosbach4b905842013-09-20 23:08:21 +00001636/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001637/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001638void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001639 if (!Lexer.is(AsmToken::EndOfStatement))
1640 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001641 // Eat EOL.
1642 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001643}
1644
Jim Grosbach4b905842013-09-20 23:08:21 +00001645/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001646/// ::= # number "filename"
1647/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001648bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001649 Lex(); // Eat the hash token.
1650
1651 if (getLexer().isNot(AsmToken::Integer)) {
1652 // Consume the line since in cases it is not a well-formed line directive,
1653 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001654 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001655 return false;
1656 }
1657
1658 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001659 Lex();
1660
1661 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001663 return false;
1664 }
1665
1666 StringRef Filename = getTok().getString();
1667 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001668 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001669
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001670 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1671 CppHashLoc = L;
1672 CppHashFilename = Filename;
1673 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001674 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001675
1676 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001677 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001678 return false;
1679}
1680
Jim Grosbach4b905842013-09-20 23:08:21 +00001681/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001682/// for the Filename and LineNo if any in the diagnostic.
1683void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001684 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 raw_ostream &OS = errs();
1686
1687 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1688 const SMLoc &DiagLoc = Diag.getLoc();
1689 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1690 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1691
Jim Grosbach4b905842013-09-20 23:08:21 +00001692 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 // before printing the message.
1694 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001695 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001696 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1697 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698 }
1699
Eric Christophera7c32732012-12-18 00:30:54 +00001700 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701 // manager changed or buffer changed (like in a nested include) then just
1702 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001703 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001704 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001705 if (Parser->SavedDiagHandler)
1706 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1707 else
1708 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709 return;
1710 }
1711
Eric Christophera7c32732012-12-18 00:30:54 +00001712 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1714 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001715 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001716
1717 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1718 int CppHashLocLineNo =
1719 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001720 int LineNo =
1721 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001722
Jim Grosbach4b905842013-09-20 23:08:21 +00001723 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1724 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001725 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001726
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001727 if (Parser->SavedDiagHandler)
1728 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1729 else
1730 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001731}
1732
Rafael Espindola2c064482012-08-21 18:29:30 +00001733// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1734// difference being that that function accepts '@' as part of identifiers and
1735// we can't do that. AsmLexer.cpp should probably be changed to handle
1736// '@' as a special case when needed.
1737static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001738 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1739 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001740}
1741
Rafael Espindola34b9c512012-06-03 23:57:14 +00001742bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001743 ArrayRef<MCAsmMacroParameter> Parameters,
1744 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001745 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001746 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001747 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001748
Preston Gurd05500642012-09-19 20:36:12 +00001749 // A macro without parameters is handled differently on Darwin:
1750 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001751 while (!Body.empty()) {
1752 // Scan for the next substitution.
1753 std::size_t End = Body.size(), Pos = 0;
1754 for (; Pos != End; ++Pos) {
1755 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001756 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001757 // This macro has no parameters, look for $0, $1, etc.
1758 if (Body[Pos] != '$' || Pos + 1 == End)
1759 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001760
Rafael Espindola1134ab232011-06-05 02:43:45 +00001761 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001762 if (Next == '$' || Next == 'n' ||
1763 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 break;
1765 } else {
1766 // This macro has parameters, look for \foo, \bar, etc.
1767 if (Body[Pos] == '\\' && Pos + 1 != End)
1768 break;
1769 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001770 }
1771
1772 // Add the prefix.
1773 OS << Body.slice(0, Pos);
1774
1775 // Check if we reached the end.
1776 if (Pos == End)
1777 break;
1778
Benjamin Kramer513e7442014-02-20 13:36:32 +00001779 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 switch (Body[Pos + 1]) {
1781 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 case '$':
1783 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001784 break;
1785
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 case 'n':
1788 OS << A.size();
1789 break;
1790
Jim Grosbach4b905842013-09-20 23:08:21 +00001791 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 default: {
1793 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001794 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 if (Index >= A.size())
1796 break;
1797
1798 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001799 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 ie = A[Index].end();
1801 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 OS << it->getString();
1803 break;
1804 }
1805 }
1806 Pos += 2;
1807 } else {
1808 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001809 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001810 ++I;
1811
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 const char *Begin = Body.data() + Pos + 1;
1813 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001814 unsigned Index = 0;
1815 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001816 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001817 break;
1818
Preston Gurd05500642012-09-19 20:36:12 +00001819 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1821 Pos += 3;
1822 else {
1823 OS << '\\' << Argument;
1824 Pos = I;
1825 }
Preston Gurd05500642012-09-19 20:36:12 +00001826 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001827 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001828 ie = A[Index].end();
1829 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001830 if (it->getKind() == AsmToken::String)
1831 OS << it->getStringContents();
1832 else
1833 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834
Preston Gurd05500642012-09-19 20:36:12 +00001835 Pos += 1 + Argument.size();
1836 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001837 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001838 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001839 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001840 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001841
Rafael Espindola1134ab232011-06-05 02:43:45 +00001842 return false;
1843}
Daniel Dunbar43235712010-07-18 18:54:11 +00001844
Jim Grosbach4b905842013-09-20 23:08:21 +00001845MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1846 SMLoc EL, MemoryBuffer *I)
1847 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1848 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001849
Jim Grosbach4b905842013-09-20 23:08:21 +00001850static bool isOperator(AsmToken::TokenKind kind) {
1851 switch (kind) {
1852 default:
1853 return false;
1854 case AsmToken::Plus:
1855 case AsmToken::Minus:
1856 case AsmToken::Tilde:
1857 case AsmToken::Slash:
1858 case AsmToken::Star:
1859 case AsmToken::Dot:
1860 case AsmToken::Equal:
1861 case AsmToken::EqualEqual:
1862 case AsmToken::Pipe:
1863 case AsmToken::PipePipe:
1864 case AsmToken::Caret:
1865 case AsmToken::Amp:
1866 case AsmToken::AmpAmp:
1867 case AsmToken::Exclaim:
1868 case AsmToken::ExclaimEqual:
1869 case AsmToken::Percent:
1870 case AsmToken::Less:
1871 case AsmToken::LessEqual:
1872 case AsmToken::LessLess:
1873 case AsmToken::LessGreater:
1874 case AsmToken::Greater:
1875 case AsmToken::GreaterEqual:
1876 case AsmToken::GreaterGreater:
1877 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001878 }
1879}
1880
David Majnemer16252452014-01-29 00:07:39 +00001881namespace {
1882class AsmLexerSkipSpaceRAII {
1883public:
1884 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1885 Lexer.setSkipSpace(SkipSpace);
1886 }
1887
1888 ~AsmLexerSkipSpaceRAII() {
1889 Lexer.setSkipSpace(true);
1890 }
1891
1892private:
1893 AsmLexer &Lexer;
1894};
1895}
1896
David Majnemer91fc4c22014-01-29 18:57:46 +00001897bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001898 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001899 unsigned AddTokens = 0;
1900
David Majnemer16252452014-01-29 00:07:39 +00001901 // Darwin doesn't use spaces to delmit arguments.
1902 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001903
1904 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001905 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001906 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001907
David Majnemer91fc4c22014-01-29 18:57:46 +00001908 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001909 break;
Preston Gurd05500642012-09-19 20:36:12 +00001910
1911 if (Lexer.is(AsmToken::Space)) {
1912 Lex(); // Eat spaces
1913
1914 // Spaces can delimit parameters, but could also be part an expression.
1915 // If the token after a space is an operator, add the token and the next
1916 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001917 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001918 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001919 // Check to see whether the token is used as an operator,
1920 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001921 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001922 if (*NextChar == ' ')
1923 AddTokens = 2;
1924 }
1925
1926 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001927 break;
1928 }
1929 }
1930 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931
Jim Grosbach4b905842013-09-20 23:08:21 +00001932 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001933 // to be able to fill in the remaining default parameter values
1934 if (Lexer.is(AsmToken::EndOfStatement))
1935 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936
1937 // Adjust the current parentheses level.
1938 if (Lexer.is(AsmToken::LParen))
1939 ++ParenLevel;
1940 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1941 --ParenLevel;
1942
1943 // Append the token to the current argument list.
1944 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001945 if (AddTokens)
1946 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001947 Lex();
1948 }
Preston Gurd05500642012-09-19 20:36:12 +00001949
Rafael Espindola768b41c2012-06-15 14:02:34 +00001950 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001951 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001952 return false;
1953}
1954
1955// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001956bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001957 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001959 bool NamedParametersFound = false;
1960 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001962 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001963 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001964
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965 // Parse two kinds of macro invocations:
1966 // - macros defined without any parameters accept an arbitrary number of them
1967 // - macros defined with parameters accept at most that many of them
1968 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1969 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001970 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001971 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001972
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001973 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001974 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001975 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001976 eatToEndOfStatement();
1977 return true;
1978 }
1979
1980 if (!Lexer.is(AsmToken::Equal)) {
1981 TokError("expected '=' after formal parameter identifier");
1982 eatToEndOfStatement();
1983 return true;
1984 }
1985 Lex();
1986
1987 NamedParametersFound = true;
1988 }
1989
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001990 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001991 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001992 eatToEndOfStatement();
1993 return true;
1994 }
1995
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001996 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997 return true;
1998
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001999 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002000 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002001 unsigned FAI = 0;
2002 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002003 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002004 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002005
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002006 if (FAI >= NParameters) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002007 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002008 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002009 M->Name + "'");
2010 return true;
2011 }
2012 PI = FAI;
2013 }
2014
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002015 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002016 if (A.size() <= PI)
2017 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002018 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002019
2020 if (FALocs.size() <= PI)
2021 FALocs.resize(PI + 1);
2022
2023 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002024 }
Jim Grosbach206661622012-07-30 22:44:17 +00002025
Preston Gurd242ed3152012-09-19 20:29:04 +00002026 // At the end of the statement, fill in remaining arguments that have
2027 // default values. If there aren't any, then the next argument is
2028 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002029 if (Lexer.is(AsmToken::EndOfStatement)) {
2030 bool Failure = false;
2031 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2032 if (A[FAI].empty()) {
2033 if (M->Parameters[FAI].Required) {
2034 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2035 "missing value for required parameter "
2036 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2037 Failure = true;
2038 }
2039
2040 if (!M->Parameters[FAI].Value.empty())
2041 A[FAI] = M->Parameters[FAI].Value;
2042 }
2043 }
2044 return Failure;
2045 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002046
2047 if (Lexer.is(AsmToken::Comma))
2048 Lex();
2049 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002050
2051 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002052}
2053
Jim Grosbach4b905842013-09-20 23:08:21 +00002054const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2055 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002056 return (I == MacroMap.end()) ? NULL : I->getValue();
2057}
2058
Jim Grosbach4b905842013-09-20 23:08:21 +00002059void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002060 MacroMap[Name] = new MCAsmMacro(Macro);
2061}
2062
Jim Grosbach4b905842013-09-20 23:08:21 +00002063void AsmParser::undefineMacro(StringRef Name) {
2064 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002065 if (I != MacroMap.end()) {
2066 delete I->getValue();
2067 MacroMap.erase(I);
2068 }
2069}
2070
Jim Grosbach4b905842013-09-20 23:08:21 +00002071bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002072 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2073 // this, although we should protect against infinite loops.
2074 if (ActiveMacros.size() == 20)
2075 return TokError("macros cannot be nested more than 20 levels deep");
2076
Eli Bendersky38274122013-01-14 23:22:36 +00002077 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002078 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002079 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002080
Rafael Espindola1134ab232011-06-05 02:43:45 +00002081 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2082 // to hold the macro body with substitutions.
2083 SmallString<256> Buf;
2084 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002085 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002086
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002087 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002088 return true;
2089
Eli Bendersky38274122013-01-14 23:22:36 +00002090 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002091 // instantiation.
2092 OS << ".endmacro\n";
2093
Rafael Espindola1134ab232011-06-05 02:43:45 +00002094 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002095 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002096
Daniel Dunbar43235712010-07-18 18:54:11 +00002097 // Create the macro instantiation object and add to the current macro
2098 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002099 MacroInstantiation *MI = new MacroInstantiation(
2100 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002101 ActiveMacros.push_back(MI);
2102
2103 // Jump to the macro instantiation and prime the lexer.
2104 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2105 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2106 Lex();
2107
2108 return false;
2109}
2110
Jim Grosbach4b905842013-09-20 23:08:21 +00002111void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002112 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002113 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002114 Lex();
2115
2116 // Pop the instantiation entry.
2117 delete ActiveMacros.back();
2118 ActiveMacros.pop_back();
2119}
2120
Jim Grosbach4b905842013-09-20 23:08:21 +00002121static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002122 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002123 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002124 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2125 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002126 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002127 case MCExpr::Target:
2128 case MCExpr::Constant:
2129 return false;
2130 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002131 const MCSymbol &S =
2132 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002133 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002134 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002135 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002136 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002137 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002138 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002139 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002140
2141 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002142}
2143
Jim Grosbach4b905842013-09-20 23:08:21 +00002144bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002145 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002146 // FIXME: Use better location, we should use proper tokens.
2147 SMLoc EqualLoc = Lexer.getLoc();
2148
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002149 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002150 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002151 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002152
Rafael Espindola72f5f172012-01-28 05:57:00 +00002153 // Note: we don't count b as used in "a = b". This is to allow
2154 // a = b
2155 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002156
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002157 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002158 return TokError("unexpected token in assignment");
2159
2160 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002161 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002162
Daniel Dunbar5f339242009-10-16 01:57:39 +00002163 // Validate that the LHS is allowed to be a variable (either it has not been
2164 // used as a symbol, or it is an absolute symbol).
2165 MCSymbol *Sym = getContext().LookupSymbol(Name);
2166 if (Sym) {
2167 // Diagnose assignment to a label.
2168 //
2169 // FIXME: Diagnostics. Note the location of the definition as a label.
2170 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002171 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002172 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2173 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002174 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002175 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2176 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002177 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002178 return Error(EqualLoc, "redefinition of '" + Name + "'");
2179 else if (!Sym->isVariable())
2180 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002181 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002182 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002183 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002184
2185 // Don't count these checks as uses.
2186 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002187 } else if (Name == ".") {
2188 if (Out.EmitValueToOffset(Value, 0)) {
2189 Error(EqualLoc, "expected absolute expression");
2190 eatToEndOfStatement();
2191 }
2192 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002193 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002194 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002195
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002196 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002197 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002198 if (NoDeadStrip)
2199 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2200
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002201 return false;
2202}
2203
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002204/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002205/// ::= identifier
2206/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002207bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002208 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002209 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2210 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002211 // handle this as a context dependent token, instead we detect adjacent tokens
2212 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002213 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2214 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002215
Hans Wennborgce69d772013-10-18 20:46:28 +00002216 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002217 Lex();
2218 if (Lexer.isNot(AsmToken::Identifier))
2219 return true;
2220
Hans Wennborgce69d772013-10-18 20:46:28 +00002221 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2222 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002223 return true;
2224
2225 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002226 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002227 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002228 Lex();
2229 return false;
2230 }
2231
Jim Grosbach4b905842013-09-20 23:08:21 +00002232 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002233 return true;
2234
Sean Callanan936b0d32010-01-19 21:44:56 +00002235 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002236
Sean Callanan686ed8d2010-01-19 20:22:31 +00002237 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002238
2239 return false;
2240}
2241
Jim Grosbach4b905842013-09-20 23:08:21 +00002242/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002243/// ::= .equ identifier ',' expression
2244/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002245/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002246bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002247 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002248
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002249 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002250 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002251
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002252 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002253 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002254 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002255
Jim Grosbach4b905842013-09-20 23:08:21 +00002256 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002257}
2258
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002259bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002260 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002261
2262 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002263 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002264 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2265 if (Str[i] != '\\') {
2266 Data += Str[i];
2267 continue;
2268 }
2269
2270 // Recognize escaped characters. Note that this escape semantics currently
2271 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2272 ++i;
2273 if (i == e)
2274 return TokError("unexpected backslash at end of string");
2275
2276 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002277 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002278 // Consume up to three octal characters.
2279 unsigned Value = Str[i] - '0';
2280
Jim Grosbach4b905842013-09-20 23:08:21 +00002281 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002282 ++i;
2283 Value = Value * 8 + (Str[i] - '0');
2284
Jim Grosbach4b905842013-09-20 23:08:21 +00002285 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002286 ++i;
2287 Value = Value * 8 + (Str[i] - '0');
2288 }
2289 }
2290
2291 if (Value > 255)
2292 return TokError("invalid octal escape sequence (out of range)");
2293
Jim Grosbach4b905842013-09-20 23:08:21 +00002294 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002295 continue;
2296 }
2297
2298 // Otherwise recognize individual escapes.
2299 switch (Str[i]) {
2300 default:
2301 // Just reject invalid escape sequences for now.
2302 return TokError("invalid escape sequence (unrecognized character)");
2303
2304 case 'b': Data += '\b'; break;
2305 case 'f': Data += '\f'; break;
2306 case 'n': Data += '\n'; break;
2307 case 'r': Data += '\r'; break;
2308 case 't': Data += '\t'; break;
2309 case '"': Data += '"'; break;
2310 case '\\': Data += '\\'; break;
2311 }
2312 }
2313
2314 return false;
2315}
2316
Jim Grosbach4b905842013-09-20 23:08:21 +00002317/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002318/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002319bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002320 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002321 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002322
Daniel Dunbara10e5192009-06-24 23:30:00 +00002323 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002324 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002325 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002326
Daniel Dunbaref668c12009-08-14 18:19:52 +00002327 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002328 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002329 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002330
Rafael Espindola64e1af82013-07-02 15:49:13 +00002331 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002332 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002333 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002334
Sean Callanan686ed8d2010-01-19 20:22:31 +00002335 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002336
2337 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002338 break;
2339
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002340 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002341 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002342 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002343 }
2344 }
2345
Sean Callanan686ed8d2010-01-19 20:22:31 +00002346 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002347 return false;
2348}
2349
Jim Grosbach4b905842013-09-20 23:08:21 +00002350/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002351/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002352bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002354 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002355
Daniel Dunbara10e5192009-06-24 23:30:00 +00002356 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002357 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002358 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002359 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002360 return true;
2361
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002362 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002363 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2364 assert(Size <= 8 && "Invalid size");
2365 uint64_t IntValue = MCE->getValue();
2366 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2367 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002368 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002369 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002370 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002371
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002372 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002373 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002374
Daniel Dunbara10e5192009-06-24 23:30:00 +00002375 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002377 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002378 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002379 }
2380 }
2381
Sean Callanan686ed8d2010-01-19 20:22:31 +00002382 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 return false;
2384}
2385
David Woodhoused6de0d92014-02-01 16:20:59 +00002386/// ParseDirectiveOctaValue
2387/// ::= .octa [ hexconstant (, hexconstant)* ]
2388bool AsmParser::parseDirectiveOctaValue() {
2389 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2390 checkForValidSection();
2391
2392 for (;;) {
2393 if (Lexer.getKind() == AsmToken::Error)
2394 return true;
2395 if (Lexer.getKind() != AsmToken::Integer &&
2396 Lexer.getKind() != AsmToken::BigNum)
2397 return TokError("unknown token in expression");
2398
2399 SMLoc ExprLoc = getLexer().getLoc();
2400 APInt IntValue = getTok().getAPIntVal();
2401 Lex();
2402
2403 uint64_t hi, lo;
2404 if (IntValue.isIntN(64)) {
2405 hi = 0;
2406 lo = IntValue.getZExtValue();
2407 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002408 // It might actually have more than 128 bits, but the top ones are zero.
2409 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002410 lo = IntValue.getLoBits(64).getZExtValue();
2411 } else
2412 return Error(ExprLoc, "literal value out of range for directive");
2413
2414 if (MAI.isLittleEndian()) {
2415 getStreamer().EmitIntValue(lo, 8);
2416 getStreamer().EmitIntValue(hi, 8);
2417 } else {
2418 getStreamer().EmitIntValue(hi, 8);
2419 getStreamer().EmitIntValue(lo, 8);
2420 }
2421
2422 if (getLexer().is(AsmToken::EndOfStatement))
2423 break;
2424
2425 // FIXME: Improve diagnostic.
2426 if (getLexer().isNot(AsmToken::Comma))
2427 return TokError("unexpected token in directive");
2428 Lex();
2429 }
2430 }
2431
2432 Lex();
2433 return false;
2434}
2435
Jim Grosbach4b905842013-09-20 23:08:21 +00002436/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002437/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002438bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002439 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002440 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002441
2442 for (;;) {
2443 // We don't truly support arithmetic on floating point expressions, so we
2444 // have to manually parse unary prefixes.
2445 bool IsNeg = false;
2446 if (getLexer().is(AsmToken::Minus)) {
2447 Lex();
2448 IsNeg = true;
2449 } else if (getLexer().is(AsmToken::Plus))
2450 Lex();
2451
Michael J. Spencer530ce852010-10-09 11:00:50 +00002452 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002453 getLexer().isNot(AsmToken::Real) &&
2454 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002455 return TokError("unexpected token in directive");
2456
2457 // Convert to an APFloat.
2458 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002459 StringRef IDVal = getTok().getString();
2460 if (getLexer().is(AsmToken::Identifier)) {
2461 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2462 Value = APFloat::getInf(Semantics);
2463 else if (!IDVal.compare_lower("nan"))
2464 Value = APFloat::getNaN(Semantics, false, ~0);
2465 else
2466 return TokError("invalid floating point literal");
2467 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002468 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002469 return TokError("invalid floating point literal");
2470 if (IsNeg)
2471 Value.changeSign();
2472
2473 // Consume the numeric token.
2474 Lex();
2475
2476 // Emit the value as an integer.
2477 APInt AsInt = Value.bitcastToAPInt();
2478 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002479 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002480
2481 if (getLexer().is(AsmToken::EndOfStatement))
2482 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002483
Daniel Dunbar2af16532010-09-24 01:59:56 +00002484 if (getLexer().isNot(AsmToken::Comma))
2485 return TokError("unexpected token in directive");
2486 Lex();
2487 }
2488 }
2489
2490 Lex();
2491 return false;
2492}
2493
Jim Grosbach4b905842013-09-20 23:08:21 +00002494/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002495/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002496bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002497 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002498
2499 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002500 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002501 return true;
2502
Rafael Espindolab91bac62010-10-05 19:42:57 +00002503 int64_t Val = 0;
2504 if (getLexer().is(AsmToken::Comma)) {
2505 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002506 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002507 return true;
2508 }
2509
Rafael Espindola922e3f42010-09-16 15:03:59 +00002510 if (getLexer().isNot(AsmToken::EndOfStatement))
2511 return TokError("unexpected token in '.zero' directive");
2512
2513 Lex();
2514
Rafael Espindola64e1af82013-07-02 15:49:13 +00002515 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002516
2517 return false;
2518}
2519
Jim Grosbach4b905842013-09-20 23:08:21 +00002520/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002521/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002522bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002523 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002524
David Majnemer522d3db2014-02-01 07:19:38 +00002525 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002526 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002528 return true;
2529
David Majnemer522d3db2014-02-01 07:19:38 +00002530 if (NumValues < 0) {
2531 Warning(RepeatLoc,
2532 "'.fill' directive with negative repeat count has no effect");
2533 NumValues = 0;
2534 }
2535
Roman Divackye33098f2013-09-24 17:44:41 +00002536 int64_t FillSize = 1;
2537 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002538
David Majnemer522d3db2014-02-01 07:19:38 +00002539 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002540 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2541 if (getLexer().isNot(AsmToken::Comma))
2542 return TokError("unexpected token in '.fill' directive");
2543 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002544
David Majnemer522d3db2014-02-01 07:19:38 +00002545 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002546 if (parseAbsoluteExpression(FillSize))
2547 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002548
Roman Divackye33098f2013-09-24 17:44:41 +00002549 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2550 if (getLexer().isNot(AsmToken::Comma))
2551 return TokError("unexpected token in '.fill' directive");
2552 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553
David Majnemer522d3db2014-02-01 07:19:38 +00002554 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002555 if (parseAbsoluteExpression(FillExpr))
2556 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002557
Roman Divackye33098f2013-09-24 17:44:41 +00002558 if (getLexer().isNot(AsmToken::EndOfStatement))
2559 return TokError("unexpected token in '.fill' directive");
2560
2561 Lex();
2562 }
2563 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002564
David Majnemer522d3db2014-02-01 07:19:38 +00002565 if (FillSize < 0) {
2566 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2567 NumValues = 0;
2568 }
2569 if (FillSize > 8) {
2570 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2571 FillSize = 8;
2572 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002573
David Majnemer522d3db2014-02-01 07:19:38 +00002574 if (!isUInt<32>(FillExpr) && FillSize > 4)
2575 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2576
2577 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2578 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2579
2580 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2581 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2582 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2583 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002584
2585 return false;
2586}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002587
Jim Grosbach4b905842013-09-20 23:08:21 +00002588/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002589/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002590bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002591 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002592
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002593 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002594 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002595 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002596 return true;
2597
2598 // Parse optional fill expression.
2599 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002600 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2601 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002602 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002603 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002604
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002605 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002606 return true;
2607
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002608 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002609 return TokError("unexpected token in '.org' directive");
2610 }
2611
Sean Callanan686ed8d2010-01-19 20:22:31 +00002612 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002613
Jim Grosbachb5912772012-01-27 00:37:08 +00002614 // Only limited forms of relocatable expressions are accepted here, it
2615 // has to be relative to the current section. The streamer will return
2616 // 'true' if the expression wasn't evaluatable.
2617 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2618 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002619
2620 return false;
2621}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002622
Jim Grosbach4b905842013-09-20 23:08:21 +00002623/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002624/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002625bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002626 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002627
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002628 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002629 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002630 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002631 return true;
2632
2633 SMLoc MaxBytesLoc;
2634 bool HasFillExpr = false;
2635 int64_t FillExpr = 0;
2636 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2638 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002639 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002640 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002641
2642 // The fill expression can be omitted while specifying a maximum number of
2643 // alignment bytes, e.g:
2644 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002645 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002646 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002647 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002648 return true;
2649 }
2650
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002651 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2652 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002654 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002656 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002659
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002660 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661 return TokError("unexpected token in directive");
2662 }
2663 }
2664
Sean Callanan686ed8d2010-01-19 20:22:31 +00002665 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002666
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002667 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669
2670 // Compute alignment in bytes.
2671 if (IsPow2) {
2672 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002673 if (Alignment >= 32) {
2674 Error(AlignmentLoc, "invalid alignment value");
2675 Alignment = 31;
2676 }
2677
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002678 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002679 } else {
2680 // Reject alignments that aren't a power of two, for gas compatibility.
2681 if (!isPowerOf2_64(Alignment))
2682 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 }
2684
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002685 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 if (MaxBytesLoc.isValid()) {
2687 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002688 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002689 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002690 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 }
2692
2693 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002694 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002695 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696 MaxBytesToFill = 0;
2697 }
2698 }
2699
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002700 // Check whether we should use optimal code alignment for this .align
2701 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002702 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002703 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2704 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002705 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002706 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002707 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002708 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2709 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002710 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002711
2712 return false;
2713}
2714
Jim Grosbach4b905842013-09-20 23:08:21 +00002715/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002716/// ::= .file [number] filename
2717/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002718bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002719 // FIXME: I'm not sure what this is.
2720 int64_t FileNumber = -1;
2721 SMLoc FileNumberLoc = getLexer().getLoc();
2722 if (getLexer().is(AsmToken::Integer)) {
2723 FileNumber = getTok().getIntVal();
2724 Lex();
2725
2726 if (FileNumber < 1)
2727 return TokError("file number less than one");
2728 }
2729
2730 if (getLexer().isNot(AsmToken::String))
2731 return TokError("unexpected token in '.file' directive");
2732
2733 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002734 // Allow the strings to have escaped octal character sequence.
2735 std::string Path = getTok().getString();
2736 if (parseEscapedString(Path))
2737 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002738 Lex();
2739
2740 StringRef Directory;
2741 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002742 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002743 if (getLexer().is(AsmToken::String)) {
2744 if (FileNumber == -1)
2745 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002746 if (parseEscapedString(FilenameData))
2747 return true;
2748 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002749 Directory = Path;
2750 Lex();
2751 } else {
2752 Filename = Path;
2753 }
2754
2755 if (getLexer().isNot(AsmToken::EndOfStatement))
2756 return TokError("unexpected token in '.file' directive");
2757
2758 if (FileNumber == -1)
2759 getStreamer().EmitFileDirective(Filename);
2760 else {
2761 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002762 Error(DirectiveLoc,
2763 "input can't have .file dwarf directives when -g is "
2764 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002765
2766 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2767 Error(FileNumberLoc, "file number already allocated");
2768 }
2769
2770 return false;
2771}
2772
Jim Grosbach4b905842013-09-20 23:08:21 +00002773/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002774/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002775bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002776 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2777 if (getLexer().isNot(AsmToken::Integer))
2778 return TokError("unexpected token in '.line' directive");
2779
2780 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002781 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002782 Lex();
2783
2784 // FIXME: Do something with the .line.
2785 }
2786
2787 if (getLexer().isNot(AsmToken::EndOfStatement))
2788 return TokError("unexpected token in '.line' directive");
2789
2790 return false;
2791}
2792
Jim Grosbach4b905842013-09-20 23:08:21 +00002793/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002794/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2795/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2796/// The first number is a file number, must have been previously assigned with
2797/// a .file directive, the second number is the line number and optionally the
2798/// third number is a column position (zero if not specified). The remaining
2799/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002800bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002801 if (getLexer().isNot(AsmToken::Integer))
2802 return TokError("unexpected token in '.loc' directive");
2803 int64_t FileNumber = getTok().getIntVal();
2804 if (FileNumber < 1)
2805 return TokError("file number less than one in '.loc' directive");
2806 if (!getContext().isValidDwarfFileNumber(FileNumber))
2807 return TokError("unassigned file number in '.loc' directive");
2808 Lex();
2809
2810 int64_t LineNumber = 0;
2811 if (getLexer().is(AsmToken::Integer)) {
2812 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002813 if (LineNumber < 0)
2814 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002815 Lex();
2816 }
2817
2818 int64_t ColumnPos = 0;
2819 if (getLexer().is(AsmToken::Integer)) {
2820 ColumnPos = getTok().getIntVal();
2821 if (ColumnPos < 0)
2822 return TokError("column position less than zero in '.loc' directive");
2823 Lex();
2824 }
2825
2826 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2827 unsigned Isa = 0;
2828 int64_t Discriminator = 0;
2829 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2830 for (;;) {
2831 if (getLexer().is(AsmToken::EndOfStatement))
2832 break;
2833
2834 StringRef Name;
2835 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002836 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002837 return TokError("unexpected token in '.loc' directive");
2838
2839 if (Name == "basic_block")
2840 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2841 else if (Name == "prologue_end")
2842 Flags |= DWARF2_FLAG_PROLOGUE_END;
2843 else if (Name == "epilogue_begin")
2844 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2845 else if (Name == "is_stmt") {
2846 Loc = getTok().getLoc();
2847 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002848 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002849 return true;
2850 // The expression must be the constant 0 or 1.
2851 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2852 int Value = MCE->getValue();
2853 if (Value == 0)
2854 Flags &= ~DWARF2_FLAG_IS_STMT;
2855 else if (Value == 1)
2856 Flags |= DWARF2_FLAG_IS_STMT;
2857 else
2858 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002859 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002860 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2861 }
Craig Topperf15655b2013-04-22 04:22:40 +00002862 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002863 Loc = getTok().getLoc();
2864 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002865 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return true;
2867 // The expression must be a constant greater or equal to 0.
2868 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2869 int Value = MCE->getValue();
2870 if (Value < 0)
2871 return Error(Loc, "isa number less than zero");
2872 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002873 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002874 return Error(Loc, "isa number not a constant value");
2875 }
Craig Topperf15655b2013-04-22 04:22:40 +00002876 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002877 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002878 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002879 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002880 return Error(Loc, "unknown sub-directive in '.loc' directive");
2881 }
2882
2883 if (getLexer().is(AsmToken::EndOfStatement))
2884 break;
2885 }
2886 }
2887
2888 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2889 Isa, Discriminator, StringRef());
2890
2891 return false;
2892}
2893
Jim Grosbach4b905842013-09-20 23:08:21 +00002894/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002895/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002896bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002897 return TokError("unsupported directive '.stabs'");
2898}
2899
Jim Grosbach4b905842013-09-20 23:08:21 +00002900/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002901/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002902bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002903 StringRef Name;
2904 bool EH = false;
2905 bool Debug = false;
2906
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002907 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return TokError("Expected an identifier");
2909
2910 if (Name == ".eh_frame")
2911 EH = true;
2912 else if (Name == ".debug_frame")
2913 Debug = true;
2914
2915 if (getLexer().is(AsmToken::Comma)) {
2916 Lex();
2917
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002918 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002919 return TokError("Expected an identifier");
2920
2921 if (Name == ".eh_frame")
2922 EH = true;
2923 else if (Name == ".debug_frame")
2924 Debug = true;
2925 }
2926
2927 getStreamer().EmitCFISections(EH, Debug);
2928 return false;
2929}
2930
Jim Grosbach4b905842013-09-20 23:08:21 +00002931/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002932/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002933bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002934 StringRef Simple;
2935 if (getLexer().isNot(AsmToken::EndOfStatement))
2936 if (parseIdentifier(Simple) || Simple != "simple")
2937 return TokError("unexpected token in .cfi_startproc directive");
2938
2939 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002940 return false;
2941}
2942
Jim Grosbach4b905842013-09-20 23:08:21 +00002943/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002944/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002945bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002946 getStreamer().EmitCFIEndProc();
2947 return false;
2948}
2949
Jim Grosbach4b905842013-09-20 23:08:21 +00002950/// \brief parse register name or number.
2951bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002952 SMLoc DirectiveLoc) {
2953 unsigned RegNo;
2954
2955 if (getLexer().isNot(AsmToken::Integer)) {
2956 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2957 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002958 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002959 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002960 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002961
2962 return false;
2963}
2964
Jim Grosbach4b905842013-09-20 23:08:21 +00002965/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002966/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002967bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002968 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002969 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002970 return true;
2971
2972 if (getLexer().isNot(AsmToken::Comma))
2973 return TokError("unexpected token in directive");
2974 Lex();
2975
2976 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002977 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002978 return true;
2979
2980 getStreamer().EmitCFIDefCfa(Register, Offset);
2981 return false;
2982}
2983
Jim Grosbach4b905842013-09-20 23:08:21 +00002984/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002985/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002986bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002987 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002988 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002989 return true;
2990
2991 getStreamer().EmitCFIDefCfaOffset(Offset);
2992 return false;
2993}
2994
Jim Grosbach4b905842013-09-20 23:08:21 +00002995/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002996/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002997bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002998 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002999 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003000 return true;
3001
3002 if (getLexer().isNot(AsmToken::Comma))
3003 return TokError("unexpected token in directive");
3004 Lex();
3005
3006 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003007 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003008 return true;
3009
3010 getStreamer().EmitCFIRegister(Register1, Register2);
3011 return false;
3012}
3013
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003014/// parseDirectiveCFIWindowSave
3015/// ::= .cfi_window_save
3016bool AsmParser::parseDirectiveCFIWindowSave() {
3017 getStreamer().EmitCFIWindowSave();
3018 return false;
3019}
3020
Jim Grosbach4b905842013-09-20 23:08:21 +00003021/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003022/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003023bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003024 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003025 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003026 return true;
3027
3028 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3029 return false;
3030}
3031
Jim Grosbach4b905842013-09-20 23:08:21 +00003032/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003033/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003034bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003035 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003036 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003037 return true;
3038
3039 getStreamer().EmitCFIDefCfaRegister(Register);
3040 return false;
3041}
3042
Jim Grosbach4b905842013-09-20 23:08:21 +00003043/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003044/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003045bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003046 int64_t Register = 0;
3047 int64_t Offset = 0;
3048
Jim Grosbach4b905842013-09-20 23:08:21 +00003049 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003050 return true;
3051
3052 if (getLexer().isNot(AsmToken::Comma))
3053 return TokError("unexpected token in directive");
3054 Lex();
3055
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003056 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003057 return true;
3058
3059 getStreamer().EmitCFIOffset(Register, Offset);
3060 return false;
3061}
3062
Jim Grosbach4b905842013-09-20 23:08:21 +00003063/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003064/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003065bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003066 int64_t Register = 0;
3067
Jim Grosbach4b905842013-09-20 23:08:21 +00003068 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003069 return true;
3070
3071 if (getLexer().isNot(AsmToken::Comma))
3072 return TokError("unexpected token in directive");
3073 Lex();
3074
3075 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003076 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003077 return true;
3078
3079 getStreamer().EmitCFIRelOffset(Register, Offset);
3080 return false;
3081}
3082
3083static bool isValidEncoding(int64_t Encoding) {
3084 if (Encoding & ~0xff)
3085 return false;
3086
3087 if (Encoding == dwarf::DW_EH_PE_omit)
3088 return true;
3089
3090 const unsigned Format = Encoding & 0xf;
3091 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3092 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3093 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3094 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3095 return false;
3096
3097 const unsigned Application = Encoding & 0x70;
3098 if (Application != dwarf::DW_EH_PE_absptr &&
3099 Application != dwarf::DW_EH_PE_pcrel)
3100 return false;
3101
3102 return true;
3103}
3104
Jim Grosbach4b905842013-09-20 23:08:21 +00003105/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003106/// IsPersonality true for cfi_personality, false for cfi_lsda
3107/// ::= .cfi_personality encoding, [symbol_name]
3108/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003109bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003110 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003111 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003112 return true;
3113 if (Encoding == dwarf::DW_EH_PE_omit)
3114 return false;
3115
3116 if (!isValidEncoding(Encoding))
3117 return TokError("unsupported encoding.");
3118
3119 if (getLexer().isNot(AsmToken::Comma))
3120 return TokError("unexpected token in directive");
3121 Lex();
3122
3123 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003124 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003125 return TokError("expected identifier in directive");
3126
3127 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3128
3129 if (IsPersonality)
3130 getStreamer().EmitCFIPersonality(Sym, Encoding);
3131 else
3132 getStreamer().EmitCFILsda(Sym, Encoding);
3133 return false;
3134}
3135
Jim Grosbach4b905842013-09-20 23:08:21 +00003136/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003137/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003138bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003139 getStreamer().EmitCFIRememberState();
3140 return false;
3141}
3142
Jim Grosbach4b905842013-09-20 23:08:21 +00003143/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003144/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003145bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003146 getStreamer().EmitCFIRestoreState();
3147 return false;
3148}
3149
Jim Grosbach4b905842013-09-20 23:08:21 +00003150/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003151/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003152bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003153 int64_t Register = 0;
3154
Jim Grosbach4b905842013-09-20 23:08:21 +00003155 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003156 return true;
3157
3158 getStreamer().EmitCFISameValue(Register);
3159 return false;
3160}
3161
Jim Grosbach4b905842013-09-20 23:08:21 +00003162/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003163/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003164bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003165 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003166 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003167 return true;
3168
3169 getStreamer().EmitCFIRestore(Register);
3170 return false;
3171}
3172
Jim Grosbach4b905842013-09-20 23:08:21 +00003173/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003174/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003175bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003176 std::string Values;
3177 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003178 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003179 return true;
3180
3181 Values.push_back((uint8_t)CurrValue);
3182
3183 while (getLexer().is(AsmToken::Comma)) {
3184 Lex();
3185
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003186 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003187 return true;
3188
3189 Values.push_back((uint8_t)CurrValue);
3190 }
3191
3192 getStreamer().EmitCFIEscape(Values);
3193 return false;
3194}
3195
Jim Grosbach4b905842013-09-20 23:08:21 +00003196/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003197/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003198bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003199 if (getLexer().isNot(AsmToken::EndOfStatement))
3200 return Error(getLexer().getLoc(),
3201 "unexpected token in '.cfi_signal_frame'");
3202
3203 getStreamer().EmitCFISignalFrame();
3204 return false;
3205}
3206
Jim Grosbach4b905842013-09-20 23:08:21 +00003207/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003208/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003209bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003210 int64_t Register = 0;
3211
Jim Grosbach4b905842013-09-20 23:08:21 +00003212 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003213 return true;
3214
3215 getStreamer().EmitCFIUndefined(Register);
3216 return false;
3217}
3218
Jim Grosbach4b905842013-09-20 23:08:21 +00003219/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003220/// ::= .macros_on
3221/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003222bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003223 if (getLexer().isNot(AsmToken::EndOfStatement))
3224 return Error(getLexer().getLoc(),
3225 "unexpected token in '" + Directive + "' directive");
3226
Jim Grosbach4b905842013-09-20 23:08:21 +00003227 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003228 return false;
3229}
3230
Jim Grosbach4b905842013-09-20 23:08:21 +00003231/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003232/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003233bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003234 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003235 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003236 return TokError("expected identifier in '.macro' directive");
3237
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003238 if (getLexer().is(AsmToken::Comma))
3239 Lex();
3240
Eli Bendersky17233942013-01-15 22:59:42 +00003241 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003242 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3243 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003244 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003245 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003246
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003247 if (Lexer.is(AsmToken::Colon)) {
3248 Lex(); // consume ':'
3249
3250 SMLoc QualLoc;
3251 StringRef Qualifier;
3252
3253 QualLoc = Lexer.getLoc();
3254 if (parseIdentifier(Qualifier))
3255 return Error(QualLoc, "missing parameter qualifier for "
3256 "'" + Parameter.Name + "' in macro '" + Name + "'");
3257
3258 if (Qualifier == "req")
3259 Parameter.Required = true;
3260 else
3261 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3262 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3263 }
3264
David Majnemer91fc4c22014-01-29 18:57:46 +00003265 if (getLexer().is(AsmToken::Equal)) {
3266 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003267
3268 SMLoc ParamLoc;
3269
3270 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003271 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003272 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003273
3274 if (Parameter.Required)
3275 Warning(ParamLoc, "pointless default value for required parameter "
3276 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003277 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003278
3279 Parameters.push_back(Parameter);
3280
3281 if (getLexer().is(AsmToken::Comma))
3282 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003283 }
3284
3285 // Eat the end of statement.
3286 Lex();
3287
3288 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003289 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003290
3291 // Lex the macro definition.
3292 for (;;) {
3293 // Check whether we have reached the end of the file.
3294 if (getLexer().is(AsmToken::Eof))
3295 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3296
3297 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003298 if (getLexer().is(AsmToken::Identifier)) {
3299 if (getTok().getIdentifier() == ".endm" ||
3300 getTok().getIdentifier() == ".endmacro") {
3301 if (MacroDepth == 0) { // Outermost macro.
3302 EndToken = getTok();
3303 Lex();
3304 if (getLexer().isNot(AsmToken::EndOfStatement))
3305 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3306 "' directive");
3307 break;
3308 } else {
3309 // Otherwise we just found the end of an inner macro.
3310 --MacroDepth;
3311 }
3312 } else if (getTok().getIdentifier() == ".macro") {
3313 // We allow nested macros. Those aren't instantiated until the outermost
3314 // macro is expanded so just ignore them for now.
3315 ++MacroDepth;
3316 }
Eli Bendersky17233942013-01-15 22:59:42 +00003317 }
3318
3319 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003320 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003321 }
3322
Jim Grosbach4b905842013-09-20 23:08:21 +00003323 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003324 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3325 }
3326
3327 const char *BodyStart = StartToken.getLoc().getPointer();
3328 const char *BodyEnd = EndToken.getLoc().getPointer();
3329 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003330 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3331 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003332 return false;
3333}
3334
Jim Grosbach4b905842013-09-20 23:08:21 +00003335/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003336///
3337/// With the support added for named parameters there may be code out there that
3338/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003339/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003340/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003341/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003342/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3343/// warning that the positional parameter found in body which have no effect.
3344/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003345/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003346/// intended or change the macro to use the named parameters. It is possible
3347/// this warning will trigger when the none of the named parameters are used
3348/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003349void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003350 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003351 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003352 // If this macro is not defined with named parameters the warning we are
3353 // checking for here doesn't apply.
3354 unsigned NParameters = Parameters.size();
3355 if (NParameters == 0)
3356 return;
3357
3358 bool NamedParametersFound = false;
3359 bool PositionalParametersFound = false;
3360
3361 // Look at the body of the macro for use of both the named parameters and what
3362 // are likely to be positional parameters. This is what expandMacro() is
3363 // doing when it finds the parameters in the body.
3364 while (!Body.empty()) {
3365 // Scan for the next possible parameter.
3366 std::size_t End = Body.size(), Pos = 0;
3367 for (; Pos != End; ++Pos) {
3368 // Check for a substitution or escape.
3369 // This macro is defined with parameters, look for \foo, \bar, etc.
3370 if (Body[Pos] == '\\' && Pos + 1 != End)
3371 break;
3372
3373 // This macro should have parameters, but look for $0, $1, ..., $n too.
3374 if (Body[Pos] != '$' || Pos + 1 == End)
3375 continue;
3376 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003377 if (Next == '$' || Next == 'n' ||
3378 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003379 break;
3380 }
3381
3382 // Check if we reached the end.
3383 if (Pos == End)
3384 break;
3385
3386 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003387 switch (Body[Pos + 1]) {
3388 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389 case '$':
3390 break;
3391
Jim Grosbach4b905842013-09-20 23:08:21 +00003392 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003393 case 'n':
3394 PositionalParametersFound = true;
3395 break;
3396
Jim Grosbach4b905842013-09-20 23:08:21 +00003397 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003398 default: {
3399 PositionalParametersFound = true;
3400 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003401 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003402 }
3403 Pos += 2;
3404 } else {
3405 unsigned I = Pos + 1;
3406 while (isIdentifierChar(Body[I]) && I + 1 != End)
3407 ++I;
3408
Jim Grosbach4b905842013-09-20 23:08:21 +00003409 const char *Begin = Body.data() + Pos + 1;
3410 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003411 unsigned Index = 0;
3412 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003413 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003414 break;
3415
3416 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003417 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3418 Pos += 3;
3419 else {
3420 Pos = I;
3421 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003422 } else {
3423 NamedParametersFound = true;
3424 Pos += 1 + Argument.size();
3425 }
3426 }
3427 // Update the scan point.
3428 Body = Body.substr(Pos);
3429 }
3430
3431 if (!NamedParametersFound && PositionalParametersFound)
3432 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3433 "used in macro body, possible positional parameter "
3434 "found in body which will have no effect");
3435}
3436
Jim Grosbach4b905842013-09-20 23:08:21 +00003437/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003438/// ::= .endm
3439/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003440bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003441 if (getLexer().isNot(AsmToken::EndOfStatement))
3442 return TokError("unexpected token in '" + Directive + "' directive");
3443
3444 // If we are inside a macro instantiation, terminate the current
3445 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003446 if (isInsideMacroInstantiation()) {
3447 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003448 return false;
3449 }
3450
3451 // Otherwise, this .endmacro is a stray entry in the file; well formed
3452 // .endmacro directives are handled during the macro definition parsing.
3453 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003454 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003455}
3456
Jim Grosbach4b905842013-09-20 23:08:21 +00003457/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003458/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003459bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003460 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003461 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003462 return TokError("expected identifier in '.purgem' directive");
3463
3464 if (getLexer().isNot(AsmToken::EndOfStatement))
3465 return TokError("unexpected token in '.purgem' directive");
3466
Jim Grosbach4b905842013-09-20 23:08:21 +00003467 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003468 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3469
Jim Grosbach4b905842013-09-20 23:08:21 +00003470 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003471 return false;
3472}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003473
Jim Grosbach4b905842013-09-20 23:08:21 +00003474/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003475/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003476bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003477 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003478
3479 // Expect a single argument: an expression that evaluates to a constant
3480 // in the inclusive range 0-30.
3481 SMLoc ExprLoc = getLexer().getLoc();
3482 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003483 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003484 return true;
3485 else if (getLexer().isNot(AsmToken::EndOfStatement))
3486 return TokError("unexpected token after expression in"
3487 " '.bundle_align_mode' directive");
3488 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3489 return Error(ExprLoc,
3490 "invalid bundle alignment size (expected between 0 and 30)");
3491
3492 Lex();
3493
3494 // Because of AlignSizePow2's verified range we can safely truncate it to
3495 // unsigned.
3496 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3497 return false;
3498}
3499
Jim Grosbach4b905842013-09-20 23:08:21 +00003500/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003501/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003502bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003504 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003505
Eli Bendersky802b6282013-01-07 21:51:08 +00003506 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3507 StringRef Option;
3508 SMLoc Loc = getTok().getLoc();
3509 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003510 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003511
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003512 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003513 return Error(Loc, kInvalidOptionError);
3514
3515 if (Option != "align_to_end")
3516 return Error(Loc, kInvalidOptionError);
3517 else if (getLexer().isNot(AsmToken::EndOfStatement))
3518 return Error(Loc,
3519 "unexpected token after '.bundle_lock' directive option");
3520 AlignToEnd = true;
3521 }
3522
Eli Benderskyf483ff92012-12-20 19:05:53 +00003523 Lex();
3524
Eli Bendersky802b6282013-01-07 21:51:08 +00003525 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003526 return false;
3527}
3528
Jim Grosbach4b905842013-09-20 23:08:21 +00003529/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003530/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003531bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003532 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003533
3534 if (getLexer().isNot(AsmToken::EndOfStatement))
3535 return TokError("unexpected token in '.bundle_unlock' directive");
3536 Lex();
3537
3538 getStreamer().EmitBundleUnlock();
3539 return false;
3540}
3541
Jim Grosbach4b905842013-09-20 23:08:21 +00003542/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003543/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003544bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003545 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003546
3547 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003548 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003549 return true;
3550
3551 int64_t FillExpr = 0;
3552 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3553 if (getLexer().isNot(AsmToken::Comma))
3554 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3555 Lex();
3556
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003557 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003558 return true;
3559
3560 if (getLexer().isNot(AsmToken::EndOfStatement))
3561 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3562 }
3563
3564 Lex();
3565
3566 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003567 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3568 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003569
3570 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003571 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003572
3573 return false;
3574}
3575
Jim Grosbach4b905842013-09-20 23:08:21 +00003576/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003577/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003578bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003579 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003580 const MCExpr *Value;
3581
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003582 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003583 return true;
3584
3585 if (getLexer().isNot(AsmToken::EndOfStatement))
3586 return TokError("unexpected token in directive");
3587
3588 if (Signed)
3589 getStreamer().EmitSLEB128Value(Value);
3590 else
3591 getStreamer().EmitULEB128Value(Value);
3592
3593 return false;
3594}
3595
Jim Grosbach4b905842013-09-20 23:08:21 +00003596/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003597/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003598bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003599 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003600 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003601 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003602 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003603
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003604 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003605 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003606
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003607 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003608
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003609 // Assembler local symbols don't make any sense here. Complain loudly.
3610 if (Sym->isTemporary())
3611 return Error(Loc, "non-local symbol required in directive");
3612
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003613 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3614 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003615
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003616 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003617 break;
3618
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003619 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003620 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003621 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003622 }
3623 }
3624
Sean Callanan686ed8d2010-01-19 20:22:31 +00003625 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003626 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003627}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003628
Jim Grosbach4b905842013-09-20 23:08:21 +00003629/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003630/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003631bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003632 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003633
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003634 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003635 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003636 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003637 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003638
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003639 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003640 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003641
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003642 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003643 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003644 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003645
3646 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003647 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003648 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003649 return true;
3650
3651 int64_t Pow2Alignment = 0;
3652 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003653 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003654 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003655 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003656 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003657 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003658
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003659 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3660 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003661 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3662
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003663 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003664 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3665 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003666 if (!isPowerOf2_64(Pow2Alignment))
3667 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3668 Pow2Alignment = Log2_64(Pow2Alignment);
3669 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003670 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003671
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003672 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003673 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003674
Sean Callanan686ed8d2010-01-19 20:22:31 +00003675 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003676
Chris Lattner28ad7542009-07-09 17:25:12 +00003677 // NOTE: a size of zero for a .comm should create a undefined symbol
3678 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003679 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003680 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003681 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003682
Eric Christopherbc818852010-05-14 01:38:54 +00003683 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003684 // may internally end up wanting an alignment in bytes.
3685 // FIXME: Diagnose overflow.
3686 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003687 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003688 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003689
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003690 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003691 return Error(IDLoc, "invalid symbol redefinition");
3692
Chris Lattner28ad7542009-07-09 17:25:12 +00003693 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003694 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003695 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003696 return false;
3697 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003698
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003699 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003700 return false;
3701}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003702
Jim Grosbach4b905842013-09-20 23:08:21 +00003703/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003704/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003705bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003706 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003707 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003708
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003709 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003710 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003711 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003712
Sean Callanan686ed8d2010-01-19 20:22:31 +00003713 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003714
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003715 if (Str.empty())
3716 Error(Loc, ".abort detected. Assembly stopping.");
3717 else
3718 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003719 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003720
3721 return false;
3722}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003723
Jim Grosbach4b905842013-09-20 23:08:21 +00003724/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003725/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003726bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003727 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003728 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003729
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003730 // Allow the strings to have escaped octal character sequence.
3731 std::string Filename;
3732 if (parseEscapedString(Filename))
3733 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003734 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003735 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003736
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003737 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003738 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003739
Chris Lattner693fbb82009-07-16 06:14:39 +00003740 // Attempt to switch the lexer to the included file before consuming the end
3741 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003742 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003743 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003744 return true;
3745 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003746
3747 return false;
3748}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003749
Jim Grosbach4b905842013-09-20 23:08:21 +00003750/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003751/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003752bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003753 if (getLexer().isNot(AsmToken::String))
3754 return TokError("expected string in '.incbin' directive");
3755
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003756 // Allow the strings to have escaped octal character sequence.
3757 std::string Filename;
3758 if (parseEscapedString(Filename))
3759 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003760 SMLoc IncbinLoc = getLexer().getLoc();
3761 Lex();
3762
3763 if (getLexer().isNot(AsmToken::EndOfStatement))
3764 return TokError("unexpected token in '.incbin' directive");
3765
Kevin Enderby109f25c2011-12-14 21:47:48 +00003766 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003767 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003768 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3769 return true;
3770 }
3771
3772 return false;
3773}
3774
Jim Grosbach4b905842013-09-20 23:08:21 +00003775/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003776/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003777/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003778bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003779 TheCondStack.push_back(TheCondState);
3780 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003781 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003782 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003783 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003784 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003785 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003786 return true;
3787
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003788 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003789 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003790
Sean Callanan686ed8d2010-01-19 20:22:31 +00003791 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003792
3793 TheCondState.CondMet = ExprValue;
3794 TheCondState.Ignore = !TheCondState.CondMet;
3795 }
3796
3797 return false;
3798}
3799
Jim Grosbach4b905842013-09-20 23:08:21 +00003800/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003801/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003802bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003803 TheCondStack.push_back(TheCondState);
3804 TheCondState.TheCond = AsmCond::IfCond;
3805
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003806 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003807 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003808 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003809 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003810
3811 if (getLexer().isNot(AsmToken::EndOfStatement))
3812 return TokError("unexpected token in '.ifb' directive");
3813
3814 Lex();
3815
3816 TheCondState.CondMet = ExpectBlank == Str.empty();
3817 TheCondState.Ignore = !TheCondState.CondMet;
3818 }
3819
3820 return false;
3821}
3822
Jim Grosbach4b905842013-09-20 23:08:21 +00003823/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003824/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003825/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003826bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +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 Kramere297b9f2012-05-12 11:18:51 +00003832 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003833 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003834
3835 if (getLexer().isNot(AsmToken::Comma))
3836 return TokError("unexpected token in '.ifc' directive");
3837
3838 Lex();
3839
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003840 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003841
3842 if (getLexer().isNot(AsmToken::EndOfStatement))
3843 return TokError("unexpected token in '.ifc' directive");
3844
3845 Lex();
3846
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003847 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003848 TheCondState.Ignore = !TheCondState.CondMet;
3849 }
3850
3851 return false;
3852}
3853
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003854/// parseDirectiveIfeqs
3855/// ::= .ifeqs string1, string2
3856bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3857 if (Lexer.isNot(AsmToken::String)) {
3858 TokError("expected string parameter for '.ifeqs' directive");
3859 eatToEndOfStatement();
3860 return true;
3861 }
3862
3863 StringRef String1 = getTok().getStringContents();
3864 Lex();
3865
3866 if (Lexer.isNot(AsmToken::Comma)) {
3867 TokError("expected comma after first string for '.ifeqs' directive");
3868 eatToEndOfStatement();
3869 return true;
3870 }
3871
3872 Lex();
3873
3874 if (Lexer.isNot(AsmToken::String)) {
3875 TokError("expected string parameter for '.ifeqs' directive");
3876 eatToEndOfStatement();
3877 return true;
3878 }
3879
3880 StringRef String2 = getTok().getStringContents();
3881 Lex();
3882
3883 TheCondStack.push_back(TheCondState);
3884 TheCondState.TheCond = AsmCond::IfCond;
3885 TheCondState.CondMet = String1 == String2;
3886 TheCondState.Ignore = !TheCondState.CondMet;
3887
3888 return false;
3889}
3890
Jim Grosbach4b905842013-09-20 23:08:21 +00003891/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003892/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003893bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003894 StringRef Name;
3895 TheCondStack.push_back(TheCondState);
3896 TheCondState.TheCond = AsmCond::IfCond;
3897
3898 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003899 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003900 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003901 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003902 return TokError("expected identifier after '.ifdef'");
3903
3904 Lex();
3905
3906 MCSymbol *Sym = getContext().LookupSymbol(Name);
3907
3908 if (expect_defined)
3909 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3910 else
3911 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3912 TheCondState.Ignore = !TheCondState.CondMet;
3913 }
3914
3915 return false;
3916}
3917
Jim Grosbach4b905842013-09-20 23:08:21 +00003918/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003919/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003920bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003921 if (TheCondState.TheCond != AsmCond::IfCond &&
3922 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003923 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3924 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003925 TheCondState.TheCond = AsmCond::ElseIfCond;
3926
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003927 bool LastIgnoreState = false;
3928 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003929 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003930 if (LastIgnoreState || TheCondState.CondMet) {
3931 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003932 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003933 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003934 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003935 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003936 return true;
3937
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003938 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003939 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003940
Sean Callanan686ed8d2010-01-19 20:22:31 +00003941 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003942 TheCondState.CondMet = ExprValue;
3943 TheCondState.Ignore = !TheCondState.CondMet;
3944 }
3945
3946 return false;
3947}
3948
Jim Grosbach4b905842013-09-20 23:08:21 +00003949/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003950/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003951bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003952 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003953 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003954
Sean Callanan686ed8d2010-01-19 20:22:31 +00003955 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003956
3957 if (TheCondState.TheCond != AsmCond::IfCond &&
3958 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003959 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3960 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003961 TheCondState.TheCond = AsmCond::ElseCond;
3962 bool LastIgnoreState = false;
3963 if (!TheCondStack.empty())
3964 LastIgnoreState = TheCondStack.back().Ignore;
3965 if (LastIgnoreState || TheCondState.CondMet)
3966 TheCondState.Ignore = true;
3967 else
3968 TheCondState.Ignore = false;
3969
3970 return false;
3971}
3972
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003973/// parseDirectiveEnd
3974/// ::= .end
3975bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3976 if (getLexer().isNot(AsmToken::EndOfStatement))
3977 return TokError("unexpected token in '.end' directive");
3978
3979 Lex();
3980
3981 while (Lexer.isNot(AsmToken::Eof))
3982 Lex();
3983
3984 return false;
3985}
3986
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003987/// parseDirectiveError
3988/// ::= .err
3989/// ::= .error [string]
3990bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
3991 if (!TheCondStack.empty()) {
3992 if (TheCondStack.back().Ignore) {
3993 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00003994 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003995 }
3996 }
3997
3998 if (!WithMessage)
3999 return Error(L, ".err encountered");
4000
4001 StringRef Message = ".error directive invoked in source file";
4002 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4003 if (Lexer.isNot(AsmToken::String)) {
4004 TokError(".error argument must be a string");
4005 eatToEndOfStatement();
4006 return true;
4007 }
4008
4009 Message = getTok().getStringContents();
4010 Lex();
4011 }
4012
4013 Error(L, Message);
4014 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004015}
4016
Jim Grosbach4b905842013-09-20 23:08:21 +00004017/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004018/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004019bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004020 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004021 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004022
Sean Callanan686ed8d2010-01-19 20:22:31 +00004023 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004024
Jim Grosbach4b905842013-09-20 23:08:21 +00004025 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004026 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4027 ".else");
4028 if (!TheCondStack.empty()) {
4029 TheCondState = TheCondStack.back();
4030 TheCondStack.pop_back();
4031 }
4032
4033 return false;
4034}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004035
Eli Bendersky17233942013-01-15 22:59:42 +00004036void AsmParser::initializeDirectiveKindMap() {
4037 DirectiveKindMap[".set"] = DK_SET;
4038 DirectiveKindMap[".equ"] = DK_EQU;
4039 DirectiveKindMap[".equiv"] = DK_EQUIV;
4040 DirectiveKindMap[".ascii"] = DK_ASCII;
4041 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4042 DirectiveKindMap[".string"] = DK_STRING;
4043 DirectiveKindMap[".byte"] = DK_BYTE;
4044 DirectiveKindMap[".short"] = DK_SHORT;
4045 DirectiveKindMap[".value"] = DK_VALUE;
4046 DirectiveKindMap[".2byte"] = DK_2BYTE;
4047 DirectiveKindMap[".long"] = DK_LONG;
4048 DirectiveKindMap[".int"] = DK_INT;
4049 DirectiveKindMap[".4byte"] = DK_4BYTE;
4050 DirectiveKindMap[".quad"] = DK_QUAD;
4051 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004052 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004053 DirectiveKindMap[".single"] = DK_SINGLE;
4054 DirectiveKindMap[".float"] = DK_FLOAT;
4055 DirectiveKindMap[".double"] = DK_DOUBLE;
4056 DirectiveKindMap[".align"] = DK_ALIGN;
4057 DirectiveKindMap[".align32"] = DK_ALIGN32;
4058 DirectiveKindMap[".balign"] = DK_BALIGN;
4059 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4060 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4061 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4062 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4063 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4064 DirectiveKindMap[".org"] = DK_ORG;
4065 DirectiveKindMap[".fill"] = DK_FILL;
4066 DirectiveKindMap[".zero"] = DK_ZERO;
4067 DirectiveKindMap[".extern"] = DK_EXTERN;
4068 DirectiveKindMap[".globl"] = DK_GLOBL;
4069 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004070 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4071 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4072 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4073 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4074 DirectiveKindMap[".reference"] = DK_REFERENCE;
4075 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4076 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4077 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4078 DirectiveKindMap[".comm"] = DK_COMM;
4079 DirectiveKindMap[".common"] = DK_COMMON;
4080 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4081 DirectiveKindMap[".abort"] = DK_ABORT;
4082 DirectiveKindMap[".include"] = DK_INCLUDE;
4083 DirectiveKindMap[".incbin"] = DK_INCBIN;
4084 DirectiveKindMap[".code16"] = DK_CODE16;
4085 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4086 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004087 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004088 DirectiveKindMap[".irp"] = DK_IRP;
4089 DirectiveKindMap[".irpc"] = DK_IRPC;
4090 DirectiveKindMap[".endr"] = DK_ENDR;
4091 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4092 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4093 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4094 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004095 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004096 DirectiveKindMap[".ifb"] = DK_IFB;
4097 DirectiveKindMap[".ifnb"] = DK_IFNB;
4098 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004099 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004100 DirectiveKindMap[".ifnc"] = DK_IFNC;
4101 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4102 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4103 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4104 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4105 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004106 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004107 DirectiveKindMap[".endif"] = DK_ENDIF;
4108 DirectiveKindMap[".skip"] = DK_SKIP;
4109 DirectiveKindMap[".space"] = DK_SPACE;
4110 DirectiveKindMap[".file"] = DK_FILE;
4111 DirectiveKindMap[".line"] = DK_LINE;
4112 DirectiveKindMap[".loc"] = DK_LOC;
4113 DirectiveKindMap[".stabs"] = DK_STABS;
4114 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4115 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4116 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4117 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4118 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4119 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4120 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4121 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4122 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4123 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4124 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4125 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4126 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4127 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4128 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4129 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4130 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4131 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4132 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4133 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4134 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004135 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004136 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4137 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4138 DirectiveKindMap[".macro"] = DK_MACRO;
4139 DirectiveKindMap[".endm"] = DK_ENDM;
4140 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4141 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004142 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004143 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004144}
4145
Jim Grosbach4b905842013-09-20 23:08:21 +00004146MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004147 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004148
Rafael Espindola34b9c512012-06-03 23:57:14 +00004149 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004150 for (;;) {
4151 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004152 if (getLexer().is(AsmToken::Eof)) {
4153 Error(DirectiveLoc, "no matching '.endr' in definition");
4154 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004155 }
4156
Rafael Espindola34b9c512012-06-03 23:57:14 +00004157 if (Lexer.is(AsmToken::Identifier) &&
4158 (getTok().getIdentifier() == ".rept")) {
4159 ++NestLevel;
4160 }
4161
4162 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004163 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004164 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004165 EndToken = getTok();
4166 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004167 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4168 TokError("unexpected token in '.endr' directive");
4169 return 0;
4170 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004171 break;
4172 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004173 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004174 }
4175
Rafael Espindola34b9c512012-06-03 23:57:14 +00004176 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004177 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004178 }
4179
4180 const char *BodyStart = StartToken.getLoc().getPointer();
4181 const char *BodyEnd = EndToken.getLoc().getPointer();
4182 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4183
Rafael Espindola34b9c512012-06-03 23:57:14 +00004184 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004185 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004186 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004187}
4188
Jim Grosbach4b905842013-09-20 23:08:21 +00004189void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004190 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004191 OS << ".endr\n";
4192
4193 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004194 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004195
Rafael Espindola34b9c512012-06-03 23:57:14 +00004196 // Create the macro instantiation object and add to the current macro
4197 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004198 MacroInstantiation *MI = new MacroInstantiation(
4199 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004200 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004201
Rafael Espindola34b9c512012-06-03 23:57:14 +00004202 // Jump to the macro instantiation and prime the lexer.
4203 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4204 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4205 Lex();
4206}
4207
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004208/// parseDirectiveRept
4209/// ::= .rep | .rept count
4210bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004211 const MCExpr *CountExpr;
4212 SMLoc CountLoc = getTok().getLoc();
4213 if (parseExpression(CountExpr))
4214 return true;
4215
Rafael Espindola34b9c512012-06-03 23:57:14 +00004216 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004217 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4218 eatToEndOfStatement();
4219 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4220 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004221
4222 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004223 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004224
4225 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004226 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004227
4228 // Eat the end of statement.
4229 Lex();
4230
4231 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004232 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004233 if (!M)
4234 return true;
4235
4236 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4237 // to hold the macro body with substitutions.
4238 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004239 raw_svector_ostream OS(Buf);
4240 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004241 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004242 return true;
4243 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004244 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004245
4246 return false;
4247}
4248
Jim Grosbach4b905842013-09-20 23:08:21 +00004249/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004250/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004251bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004252 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004253
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004254 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004255 return TokError("expected identifier in '.irp' directive");
4256
Rafael Espindola768b41c2012-06-15 14:02:34 +00004257 if (Lexer.isNot(AsmToken::Comma))
4258 return TokError("expected comma in '.irp' directive");
4259
4260 Lex();
4261
Eli Bendersky38274122013-01-14 23:22:36 +00004262 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004263 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004264 return true;
4265
4266 // Eat the end of statement.
4267 Lex();
4268
4269 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004270 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004271 if (!M)
4272 return true;
4273
4274 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4275 // to hold the macro body with substitutions.
4276 SmallString<256> Buf;
4277 raw_svector_ostream OS(Buf);
4278
Eli Bendersky38274122013-01-14 23:22:36 +00004279 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004280 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004281 return true;
4282 }
4283
Jim Grosbach4b905842013-09-20 23:08:21 +00004284 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004285
4286 return false;
4287}
4288
Jim Grosbach4b905842013-09-20 23:08:21 +00004289/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004290/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004291bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004292 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004293
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004294 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004295 return TokError("expected identifier in '.irpc' directive");
4296
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004297 if (Lexer.isNot(AsmToken::Comma))
4298 return TokError("expected comma in '.irpc' directive");
4299
4300 Lex();
4301
Eli Bendersky38274122013-01-14 23:22:36 +00004302 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004303 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004304 return true;
4305
4306 if (A.size() != 1 || A.front().size() != 1)
4307 return TokError("unexpected token in '.irpc' directive");
4308
4309 // Eat the end of statement.
4310 Lex();
4311
4312 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004313 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004314 if (!M)
4315 return true;
4316
4317 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4318 // to hold the macro body with substitutions.
4319 SmallString<256> Buf;
4320 raw_svector_ostream OS(Buf);
4321
4322 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004323 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004324 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004325 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004326
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004327 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004328 return true;
4329 }
4330
Jim Grosbach4b905842013-09-20 23:08:21 +00004331 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004332
4333 return false;
4334}
4335
Jim Grosbach4b905842013-09-20 23:08:21 +00004336bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004337 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004338 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004339
4340 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004341 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004342 assert(getLexer().is(AsmToken::EndOfStatement));
4343
Jim Grosbach4b905842013-09-20 23:08:21 +00004344 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004345 return false;
4346}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004347
Jim Grosbach4b905842013-09-20 23:08:21 +00004348bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004349 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004350 const MCExpr *Value;
4351 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004352 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004353 return true;
4354 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4355 if (!MCE)
4356 return Error(ExprLoc, "unexpected expression in _emit");
4357 uint64_t IntValue = MCE->getValue();
4358 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4359 return Error(ExprLoc, "literal value out of range for directive");
4360
Chad Rosierc7f552c2013-02-12 21:33:51 +00004361 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4362 return false;
4363}
4364
Jim Grosbach4b905842013-09-20 23:08:21 +00004365bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004366 const MCExpr *Value;
4367 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004368 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004369 return true;
4370 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4371 if (!MCE)
4372 return Error(ExprLoc, "unexpected expression in align");
4373 uint64_t IntValue = MCE->getValue();
4374 if (!isPowerOf2_64(IntValue))
4375 return Error(ExprLoc, "literal value not a power of two greater then zero");
4376
Jim Grosbach4b905842013-09-20 23:08:21 +00004377 Info.AsmRewrites->push_back(
4378 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004379 return false;
4380}
4381
Chad Rosierf43fcf52013-02-13 21:27:17 +00004382// We are comparing pointers, but the pointers are relative to a single string.
4383// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004384static int rewritesSort(const AsmRewrite *AsmRewriteA,
4385 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004386 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4387 return -1;
4388 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4389 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004390
Chad Rosierfce4fab2013-04-08 17:43:47 +00004391 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4392 // rewrite to the same location. Make sure the SizeDirective rewrite is
4393 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4394 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004395 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4396 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004397 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004398
Jim Grosbach4b905842013-09-20 23:08:21 +00004399 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4400 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004401 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004402 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004403}
4404
Jim Grosbach4b905842013-09-20 23:08:21 +00004405bool AsmParser::parseMSInlineAsm(
4406 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4407 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4408 SmallVectorImpl<std::string> &Constraints,
4409 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4410 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004411 SmallVector<void *, 4> InputDecls;
4412 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004413 SmallVector<bool, 4> InputDeclsAddressOf;
4414 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004415 SmallVector<std::string, 4> InputConstraints;
4416 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004417 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004418
Benjamin Kramer1a136112013-02-15 20:37:21 +00004419 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004420
4421 // Prime the lexer.
4422 Lex();
4423
4424 // While we have input, parse each statement.
4425 unsigned InputIdx = 0;
4426 unsigned OutputIdx = 0;
4427 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004428 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004429 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004430 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004431
Chad Rosier149e8e02012-12-12 22:45:52 +00004432 if (Info.ParseError)
4433 return true;
4434
Benjamin Kramer1a136112013-02-15 20:37:21 +00004435 if (Info.Opcode == ~0U)
4436 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004437
Benjamin Kramer1a136112013-02-15 20:37:21 +00004438 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004439
Benjamin Kramer1a136112013-02-15 20:37:21 +00004440 // Build the list of clobbers, outputs and inputs.
4441 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4442 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004443
Benjamin Kramer1a136112013-02-15 20:37:21 +00004444 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004445 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004446 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004447
Benjamin Kramer1a136112013-02-15 20:37:21 +00004448 // Register operand.
4449 if (Operand->isReg() && !Operand->needAddressOf()) {
4450 unsigned NumDefs = Desc.getNumDefs();
4451 // Clobber.
4452 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4453 ClobberRegs.push_back(Operand->getReg());
4454 continue;
4455 }
4456
4457 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004458 StringRef SymName = Operand->getSymName();
4459 if (SymName.empty())
4460 continue;
4461
Chad Rosierdba3fe52013-04-22 22:12:12 +00004462 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004463 if (!OpDecl)
4464 continue;
4465
4466 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004467 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004468 if (isOutput) {
4469 ++InputIdx;
4470 OutputDecls.push_back(OpDecl);
4471 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4472 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004473 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004474 } else {
4475 InputDecls.push_back(OpDecl);
4476 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4477 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004478 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004479 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004480 }
Reid Kleckneree088972013-12-10 18:27:32 +00004481
4482 // Consider implicit defs to be clobbers. Think of cpuid and push.
4483 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4484 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4485 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004486 }
4487
4488 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004489 NumOutputs = OutputDecls.size();
4490 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004491
4492 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004493 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4494 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4495 ClobberRegs.end());
4496 Clobbers.assign(ClobberRegs.size(), std::string());
4497 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4498 raw_string_ostream OS(Clobbers[I]);
4499 IP->printRegName(OS, ClobberRegs[I]);
4500 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004501
4502 // Merge the various outputs and inputs. Output are expected first.
4503 if (NumOutputs || NumInputs) {
4504 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004505 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004506 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004507 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004508 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004509 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004510 }
4511 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004512 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004513 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004514 }
4515 }
4516
4517 // Build the IR assembly string.
4518 std::string AsmStringIR;
4519 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004520 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4521 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004522 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004523 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4524 E = AsmStrRewrites.end();
4525 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004526 AsmRewriteKind Kind = (*I).Kind;
4527 if (Kind == AOK_Delete)
4528 continue;
4529
Chad Rosier8bce6642012-10-18 15:49:34 +00004530 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004531 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004532
Chad Rosier120eefd2013-03-19 17:32:17 +00004533 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004534 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004535 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004536 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004537
Chad Rosier37e755c2012-10-23 17:43:43 +00004538 // Skip the original expression.
4539 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004540 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004541 continue;
4542 }
4543
Chad Rosierff10ed12013-04-12 16:26:42 +00004544 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004545 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004546 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004547 default:
4548 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004549 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004550 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004551 break;
4552 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004553 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004554 break;
4555 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004556 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004557 break;
4558 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004559 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004560 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004561 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004563 default: break;
4564 case 8: OS << "byte ptr "; break;
4565 case 16: OS << "word ptr "; break;
4566 case 32: OS << "dword ptr "; break;
4567 case 64: OS << "qword ptr "; break;
4568 case 80: OS << "xword ptr "; break;
4569 case 128: OS << "xmmword ptr "; break;
4570 case 256: OS << "ymmword ptr "; break;
4571 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004572 break;
4573 case AOK_Emit:
4574 OS << ".byte";
4575 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004576 case AOK_Align: {
4577 unsigned Val = (*I).Val;
4578 OS << ".align " << Val;
4579
4580 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004581 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004582 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4583 break;
4584 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004585 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004586 // Insert the dot if the user omitted it.
4587 OS.flush();
4588 if (AsmStringIR.back() != '.')
4589 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004590 OS << (*I).Val;
4591 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004592 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004593
Chad Rosier8bce6642012-10-18 15:49:34 +00004594 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004595 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004596 }
4597
4598 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004599 if (AsmStart != AsmEnd)
4600 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004601
4602 AsmString = OS.str();
4603 return false;
4604}
4605
Daniel Dunbar01e36072010-07-17 02:26:10 +00004606/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004607MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4608 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004609 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004610}