blob: ca603cf613068807a24bba3300a627eaab9bef8b [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
Craig Topper59be68f2014-03-08 07:14:16 +0000193 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194
Craig Topper59be68f2014-03-08 07:14:16 +0000195 void addDirectiveHandler(StringRef Directive,
196 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000197 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000198 }
199
200public:
201 /// @name MCAsmParser Interface
202 /// {
203
Craig Topper59be68f2014-03-08 07:14:16 +0000204 SourceMgr &getSourceManager() override { return SrcMgr; }
205 MCAsmLexer &getLexer() override { return Lexer; }
206 MCContext &getContext() override { return Ctx; }
207 MCStreamer &getStreamer() override { return Out; }
208 unsigned getAssemblerDialect() override {
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 }
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000215 AssemblerDialect = i;
216 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Craig Topper59be68f2014-03-08 07:14:16 +0000218 void Note(SMLoc L, const Twine &Msg,
219 ArrayRef<SMRange> Ranges = None) override;
220 bool Warning(SMLoc L, const Twine &Msg,
221 ArrayRef<SMRange> Ranges = None) override;
222 bool Error(SMLoc L, const Twine &Msg,
223 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000224
Craig Topper59be68f2014-03-08 07:14:16 +0000225 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000226
Craig Topper59be68f2014-03-08 07:14:16 +0000227 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
228 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000229
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000230 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000231 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000232 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000234 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000235 const MCInstrInfo *MII, const MCInstPrinter *IP,
236 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000237
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000238 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000239 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
240 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
241 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parseAbsoluteExpression(int64_t &Res) override;
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.
Craig Topper59be68f2014-03-08 07:14:16 +0000246 bool parseIdentifier(StringRef &Res) override;
247 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000248
Craig Topper59be68f2014-03-08 07:14:16 +0000249 void checkForValidSection() override;
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.
Craig Topper59be68f2014-03-08 07:14:16 +0000325 StringRef parseStringToEndOfStatement() override;
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
Craig Topper59be68f2014-03-08 07:14:16 +0000454 bool parseEscapedString(std::string &Data) override;
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);
David Blaikiec714ef42014-03-17 01:52:11 +0000643 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
644 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000645 }
646
Chris Lattner73f36112009-07-02 21:53:43 +0000647 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000648 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000649 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000650 if (!parseStatement(Info))
651 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000652
Daniel Dunbar43325c42010-09-09 22:42:56 +0000653 // We had an error, validate that one was emitted and recover by skipping to
654 // the next line.
655 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000656 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000657 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000658
659 if (TheCondState.TheCond != StartingCondState.TheCond ||
660 TheCondState.Ignore != StartingCondState.Ignore)
661 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000662
663 // Check to see there are no empty DwarfFile slots.
David Blaikiea55ddad2014-03-13 18:55:04 +0000664 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000665 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000667 if (MCDwarfFiles[i].Name.empty())
Kevin Enderbye5930f12010-07-28 20:55:35 +0000668 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000670
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 // Check to see that all assembler local symbols were actually defined.
672 // Targets that don't do subsections via symbols may not want this, though,
673 // so conservatively exclude them. Only do this if we're finalizing, though,
674 // as otherwise we won't necessarilly have seen everything yet.
675 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
676 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
677 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 i != e; ++i) {
680 MCSymbol *Sym = i->getValue();
681 // Variable symbols may not be marked as defined, so check those
682 // explicitly. If we know it's a variable, we have a definition for
683 // the purposes of this check.
684 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
685 // FIXME: We would really like to refer back to where the symbol was
686 // first referenced for a source location. We need to add something
687 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000688 printMessage(
689 getLexer().getLoc(), SourceMgr::DK_Error,
690 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000691 }
692 }
693
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000694 // Finalize the output stream if there are no errors and if the client wants
695 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000696 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000697 Out.Finish();
698
Chris Lattner73f36112009-07-02 21:53:43 +0000699 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000700}
701
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000703 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000705 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000706 }
707}
708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000711 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000712 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000713
Chris Lattnere5074c42009-06-22 01:29:09 +0000714 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000715 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000716 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000717}
718
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000719StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 const char *Start = getTok().getLoc().getPointer();
721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 Lex();
724
725 const char *End = getTok().getLoc().getPointer();
726 return StringRef(Start, End - Start);
727}
Chris Lattner78db3622009-06-22 05:51:26 +0000728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000730 const char *Start = getTok().getLoc().getPointer();
731
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000733 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
739
Jim Grosbach4b905842013-09-20 23:08:21 +0000740/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741/// NOTE: This assumes the leading '(' has already been consumed.
742///
743/// parenexpr ::= expr)
744///
Jim Grosbach4b905842013-09-20 23:08:21 +0000745bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
746 if (parseExpression(Res))
747 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000748 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000750 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000751 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return false;
753}
Chris Lattner78db3622009-06-22 05:51:26 +0000754
Jim Grosbach4b905842013-09-20 23:08:21 +0000755/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756/// NOTE: This assumes the leading '[' has already been consumed.
757///
758/// bracketexpr ::= expr]
759///
Jim Grosbach4b905842013-09-20 23:08:21 +0000760bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
761 if (parseExpression(Res))
762 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 if (Lexer.isNot(AsmToken::RBrac))
764 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000765 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 Lex();
767 return false;
768}
769
Jim Grosbach4b905842013-09-20 23:08:21 +0000770/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000771/// primaryexpr ::= (parenexpr
772/// primaryexpr ::= symbol
773/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000774/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000775/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000776bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000777 SMLoc FirstTokenLoc = getLexer().getLoc();
778 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
779 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000780 default:
781 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000782 // If we have an error assume that we've already handled it.
783 case AsmToken::Error:
784 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000785 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000786 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000787 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000789 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000790 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000791 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000792 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000793 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000794 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000795 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000796 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 if (FirstTokenKind == AsmToken::Dollar) {
798 if (Lexer.getMAI().getDollarIsPC()) {
799 // This is a '$' reference, which references the current PC. Emit a
800 // temporary label to the streamer and refer to it.
801 MCSymbol *Sym = Ctx.CreateTempSymbol();
802 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000803 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
804 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 EndLoc = FirstTokenLoc;
806 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000807 }
808 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000810 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000811 // Parse symbol variant
812 std::pair<StringRef, StringRef> Split;
813 if (!MAI.useParensForSymbolVariant()) {
814 Split = Identifier.split('@');
815 } else if (Lexer.is(AsmToken::LParen)) {
816 Lexer.Lex(); // eat (
817 StringRef VName;
818 parseIdentifier(VName);
819 if (Lexer.isNot(AsmToken::RParen)) {
820 return Error(Lexer.getTok().getLoc(),
821 "unexpected token in variant, expected ')'");
822 }
823 Lexer.Lex(); // eat )
824 Split = std::make_pair(Identifier, VName);
825 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000826
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000827 EndLoc = SMLoc::getFromPointer(Identifier.end());
828
Daniel Dunbard20cda02009-10-16 01:34:54 +0000829 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 StringRef SymbolName = Identifier;
831 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000832
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000833 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000834 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000835 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 if (Variant != MCSymbolRefExpr::VK_Invalid) {
837 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 Variant = MCSymbolRefExpr::VK_None;
840 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000841 return Error(SMLoc::getFromPointer(Split.second.begin()),
842 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000843 }
844 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000845
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
847
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 // If this is an absolute variable reference, substitute it now to preserve
849 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000850 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000851 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000852 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000853
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000855 return false;
856 }
857
858 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000859 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000860 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 }
David Woodhousef42a6662014-02-01 16:20:54 +0000862 case AsmToken::BigNum:
863 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000864 case AsmToken::Integer: {
865 SMLoc Loc = getTok().getLoc();
866 int64_t IntVal = getTok().getIntVal();
867 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000868 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000869 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 // Look for 'b' or 'f' following an Integer as a directional label
871 if (Lexer.getKind() == AsmToken::Identifier) {
872 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000873 // Lookup the symbol variant if used.
874 std::pair<StringRef, StringRef> Split = IDVal.split('@');
875 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
876 if (Split.first.size() != IDVal.size()) {
877 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
878 if (Variant == MCSymbolRefExpr::VK_Invalid) {
879 Variant = MCSymbolRefExpr::VK_None;
880 return TokError("invalid variant '" + Split.second + "'");
881 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000882 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000884 if (IDVal == "f" || IDVal == "b") {
885 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000886 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000887 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000888 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000889 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000890 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 Lex(); // Eat identifier.
892 }
893 }
Chris Lattner78db3622009-06-22 05:51:26 +0000894 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000895 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000896 case AsmToken::Real: {
897 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000898 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000899 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000900 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000901 Lex(); // Eat token.
902 return false;
903 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000904 case AsmToken::Dot: {
905 // This is a '.' reference, which references the current PC. Emit a
906 // temporary label to the streamer and refer to it.
907 MCSymbol *Sym = Ctx.CreateTempSymbol();
908 Out.EmitLabel(Sym);
909 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000910 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000911 Lex(); // Eat identifier.
912 return false;
913 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000914 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000915 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000916 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000917 case AsmToken::LBrac:
918 if (!PlatformParser->HasBracketExpressions())
919 return TokError("brackets expression not supported on this target");
920 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000921 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000922 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000923 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000924 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000925 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000926 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000928 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000929 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000930 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000931 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000932 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000934 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000935 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000936 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000938 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000940 }
941}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000942
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000943bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000944 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000945 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000946}
947
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000949AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000951 // Ask the target implementation about this expression first.
952 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
953 if (NewE)
954 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000955 // Recurse over the given expression, rebuilding it to apply the given variant
956 // if there is exactly one symbol.
957 switch (E->getKind()) {
958 case MCExpr::Target:
959 case MCExpr::Constant:
960 return 0;
961
962 case MCExpr::SymbolRef: {
963 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
964
965 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000966 TokError("invalid variant on expression '" + getTok().getIdentifier() +
967 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968 return E;
969 }
970
971 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
972 }
973
974 case MCExpr::Unary: {
975 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000976 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000977 if (!Sub)
978 return 0;
979 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
980 }
981
982 case MCExpr::Binary: {
983 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000984 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
985 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986
987 if (!LHS && !RHS)
988 return 0;
989
Jim Grosbach4b905842013-09-20 23:08:21 +0000990 if (!LHS)
991 LHS = BE->getLHS();
992 if (!RHS)
993 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994
995 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
996 }
997 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000998
Craig Toppera2886c22012-02-07 05:05:23 +0000999 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001000}
1001
Jim Grosbach4b905842013-09-20 23:08:21 +00001002/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001003///
Jim Grosbachbd164242011-08-20 16:24:13 +00001004/// expr ::= expr &&,|| expr -> lowest.
1005/// expr ::= expr |,^,&,! expr
1006/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1007/// expr ::= expr <<,>> expr
1008/// expr ::= expr +,- expr
1009/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001010/// expr ::= primaryexpr
1011///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001012bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001013 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001014 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001015 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001016 return true;
1017
Daniel Dunbar55f16672010-09-17 02:47:07 +00001018 // As a special case, we support 'a op b @ modifier' by rewriting the
1019 // expression to include the modifier. This is inefficient, but in general we
1020 // expect users to use 'a@modifier op b'.
1021 if (Lexer.getKind() == AsmToken::At) {
1022 Lex();
1023
1024 if (Lexer.isNot(AsmToken::Identifier))
1025 return TokError("unexpected symbol modifier following '@'");
1026
1027 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 if (Variant == MCSymbolRefExpr::VK_Invalid)
1030 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1031
Jim Grosbach4b905842013-09-20 23:08:21 +00001032 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 if (!ModifiedRes) {
1034 return TokError("invalid modifier '" + getTok().getIdentifier() +
1035 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001037
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 Res = ModifiedRes;
1039 Lex();
1040 }
1041
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001042 // Try to constant fold it up front, if possible.
1043 int64_t Value;
1044 if (Res->EvaluateAsAbsolute(Value))
1045 Res = MCConstantExpr::Create(Value, getContext());
1046
1047 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001048}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001049
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001050bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001051 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001052 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001053}
1054
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001055bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001056 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001057
Daniel Dunbar75630b32009-06-30 02:10:03 +00001058 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001059 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001060 return true;
1061
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001062 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001063 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001064
1065 return false;
1066}
1067
Michael J. Spencer530ce852010-10-09 11:00:50 +00001068static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001069 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001070 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001071 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001072 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073
Jim Grosbach4b905842013-09-20 23:08:21 +00001074 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001075 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001076 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001077 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001078 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001079 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001080 return 1;
1081
Jim Grosbach4b905842013-09-20 23:08:21 +00001082 // Low Precedence: |, &, ^
1083 //
1084 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001085 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001087 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001090 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001091 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001093 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001094
Jim Grosbach4b905842013-09-20 23:08:21 +00001095 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001096 case AsmToken::EqualEqual:
1097 Kind = MCBinaryExpr::EQ;
1098 return 3;
1099 case AsmToken::ExclaimEqual:
1100 case AsmToken::LessGreater:
1101 Kind = MCBinaryExpr::NE;
1102 return 3;
1103 case AsmToken::Less:
1104 Kind = MCBinaryExpr::LT;
1105 return 3;
1106 case AsmToken::LessEqual:
1107 Kind = MCBinaryExpr::LTE;
1108 return 3;
1109 case AsmToken::Greater:
1110 Kind = MCBinaryExpr::GT;
1111 return 3;
1112 case AsmToken::GreaterEqual:
1113 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001114 return 3;
1115
Jim Grosbach4b905842013-09-20 23:08:21 +00001116 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001117 case AsmToken::LessLess:
1118 Kind = MCBinaryExpr::Shl;
1119 return 4;
1120 case AsmToken::GreaterGreater:
1121 Kind = MCBinaryExpr::Shr;
1122 return 4;
1123
Jim Grosbach4b905842013-09-20 23:08:21 +00001124 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001125 case AsmToken::Plus:
1126 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001127 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001128 case AsmToken::Minus:
1129 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001131
Jim Grosbach4b905842013-09-20 23:08:21 +00001132 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001133 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001134 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001135 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001136 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001137 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001138 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001139 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001140 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001141 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142 }
1143}
1144
Jim Grosbach4b905842013-09-20 23:08:21 +00001145/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001146/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001147bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001148 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001151 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001152
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153 // If the next token is lower precedence than we are allowed to eat, return
1154 // successfully with what we ate already.
1155 if (TokPrec < Precedence)
1156 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Sean Callanan686ed8d2010-01-19 20:22:31 +00001158 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001161 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001162 if (parsePrimaryExpr(RHS, EndLoc))
1163 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001164
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001165 // If BinOp binds less tightly with RHS than the operator after RHS, let
1166 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001167 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001168 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001169 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1170 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001171
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001172 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001173 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001174 }
1175}
1176
Chris Lattner36e02122009-06-21 20:54:55 +00001177/// ParseStatement:
1178/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001179/// ::= Label* Directive ...Operands... EndOfStatement
1180/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001181bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001182 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001183 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001184 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001185 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001186 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001187
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001188 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001189 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001190 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001191 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001192 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001193 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001194 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001195 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001196
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001197 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001198 if (Lexer.is(AsmToken::Integer)) {
1199 LocalLabelVal = getTok().getIntVal();
1200 if (LocalLabelVal < 0) {
1201 if (!TheCondState.Ignore)
1202 return TokError("unexpected token at start of statement");
1203 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001204 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 IDVal = getTok().getString();
1206 Lex(); // Consume the integer token to be used as an identifier token.
1207 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001208 if (!TheCondState.Ignore)
1209 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001210 }
1211 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001212 } else if (Lexer.is(AsmToken::Dot)) {
1213 // Treat '.' as a valid identifier in this context.
1214 Lex();
1215 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001216 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001217 if (!TheCondState.Ignore)
1218 return TokError("unexpected token at start of statement");
1219 IDVal = "";
1220 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001221
Chris Lattner926885c2010-04-17 18:14:27 +00001222 // Handle conditional assembly here before checking for skipping. We
1223 // have to do this so that .endif isn't skipped in a ".if 0" block for
1224 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001225 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001226 DirectiveKindMap.find(IDVal);
1227 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1228 ? DK_NO_DIRECTIVE
1229 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001230 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001231 default:
1232 break;
1233 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001234 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001235 return parseDirectiveIf(IDLoc);
1236 case DK_IFB:
1237 return parseDirectiveIfb(IDLoc, true);
1238 case DK_IFNB:
1239 return parseDirectiveIfb(IDLoc, false);
1240 case DK_IFC:
1241 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001242 case DK_IFEQS:
1243 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 case DK_IFNC:
1245 return parseDirectiveIfc(IDLoc, false);
1246 case DK_IFDEF:
1247 return parseDirectiveIfdef(IDLoc, true);
1248 case DK_IFNDEF:
1249 case DK_IFNOTDEF:
1250 return parseDirectiveIfdef(IDLoc, false);
1251 case DK_ELSEIF:
1252 return parseDirectiveElseIf(IDLoc);
1253 case DK_ELSE:
1254 return parseDirectiveElse(IDLoc);
1255 case DK_ENDIF:
1256 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001257 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001258
Eli Bendersky88024712013-01-16 19:32:36 +00001259 // Ignore the statement if in the middle of inactive conditional
1260 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001261 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001262 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001263 return false;
1264 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001265
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001266 // FIXME: Recurse on local labels?
1267
1268 // See what kind of statement we have.
1269 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001270 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001271 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001272
Chris Lattner36e02122009-06-21 20:54:55 +00001273 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001274 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001275
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001276 // Diagnose attempt to use '.' as a label.
1277 if (IDVal == ".")
1278 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1279
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001280 // Diagnose attempt to use a variable as a label.
1281 //
1282 // FIXME: Diagnostics. Note the location of the definition as a label.
1283 // FIXME: This doesn't diagnose assignment to a symbol which has been
1284 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001285 MCSymbol *Sym;
1286 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001287 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001288 else
1289 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001290 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001291 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001292
Daniel Dunbare73b2672009-08-26 22:13:22 +00001293 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001294 if (!ParsingInlineAsm)
1295 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001296
Kevin Enderbye7739d42011-12-09 18:09:40 +00001297 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001298 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001299 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001300 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1301 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001302
Tim Northover1744d0a2013-10-25 12:49:50 +00001303 getTargetParser().onLabelParsed(Sym);
1304
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001305 // Consume any end of statement token, if present, to avoid spurious
1306 // AddBlankLine calls().
1307 if (Lexer.is(AsmToken::EndOfStatement)) {
1308 Lex();
1309 if (Lexer.is(AsmToken::Eof))
1310 return false;
1311 }
1312
Eli Friedman0f4871d2012-10-22 23:58:19 +00001313 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001314 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001315
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001316 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001317 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001318 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001319
Jim Grosbach4b905842013-09-20 23:08:21 +00001320 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001321
1322 default: // Normal instruction or directive.
1323 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001324 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001325
1326 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001327 if (areMacrosEnabled())
1328 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1329 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001330 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001331
Michael J. Spencer530ce852010-10-09 11:00:50 +00001332 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001333
Eli Bendersky17233942013-01-15 22:59:42 +00001334 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001335 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001336 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001337 //
Eli Bendersky17233942013-01-15 22:59:42 +00001338 // 1. The target-specific assembly parser. Some directives are target
1339 // specific or may potentially behave differently on certain targets.
1340 // 2. Asm parser extensions. For example, platform-specific parsers
1341 // (like the ELF parser) register themselves as extensions.
1342 // 3. The generic directive parser implemented by this class. These are
1343 // all the directives that behave in a target and platform independent
1344 // manner, or at least have a default behavior that's shared between
1345 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001346
Eli Bendersky17233942013-01-15 22:59:42 +00001347 // First query the target-specific parser. It will return 'true' if it
1348 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001349 if (!getTargetParser().ParseDirective(ID))
1350 return false;
1351
Alp Tokercb402912014-01-24 17:20:08 +00001352 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001353 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001354 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1355 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001356 if (Handler.first)
1357 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1358
1359 // Finally, if no one else is interested in this directive, it must be
1360 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001361 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001362 default:
1363 break;
1364 case DK_SET:
1365 case DK_EQU:
1366 return parseDirectiveSet(IDVal, true);
1367 case DK_EQUIV:
1368 return parseDirectiveSet(IDVal, false);
1369 case DK_ASCII:
1370 return parseDirectiveAscii(IDVal, false);
1371 case DK_ASCIZ:
1372 case DK_STRING:
1373 return parseDirectiveAscii(IDVal, true);
1374 case DK_BYTE:
1375 return parseDirectiveValue(1);
1376 case DK_SHORT:
1377 case DK_VALUE:
1378 case DK_2BYTE:
1379 return parseDirectiveValue(2);
1380 case DK_LONG:
1381 case DK_INT:
1382 case DK_4BYTE:
1383 return parseDirectiveValue(4);
1384 case DK_QUAD:
1385 case DK_8BYTE:
1386 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001387 case DK_OCTA:
1388 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001389 case DK_SINGLE:
1390 case DK_FLOAT:
1391 return parseDirectiveRealValue(APFloat::IEEEsingle);
1392 case DK_DOUBLE:
1393 return parseDirectiveRealValue(APFloat::IEEEdouble);
1394 case DK_ALIGN: {
1395 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1396 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1397 }
1398 case DK_ALIGN32: {
1399 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1400 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1401 }
1402 case DK_BALIGN:
1403 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1404 case DK_BALIGNW:
1405 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1406 case DK_BALIGNL:
1407 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1408 case DK_P2ALIGN:
1409 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1410 case DK_P2ALIGNW:
1411 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1412 case DK_P2ALIGNL:
1413 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1414 case DK_ORG:
1415 return parseDirectiveOrg();
1416 case DK_FILL:
1417 return parseDirectiveFill();
1418 case DK_ZERO:
1419 return parseDirectiveZero();
1420 case DK_EXTERN:
1421 eatToEndOfStatement(); // .extern is the default, ignore it.
1422 return false;
1423 case DK_GLOBL:
1424 case DK_GLOBAL:
1425 return parseDirectiveSymbolAttribute(MCSA_Global);
1426 case DK_LAZY_REFERENCE:
1427 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1428 case DK_NO_DEAD_STRIP:
1429 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1430 case DK_SYMBOL_RESOLVER:
1431 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1432 case DK_PRIVATE_EXTERN:
1433 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1434 case DK_REFERENCE:
1435 return parseDirectiveSymbolAttribute(MCSA_Reference);
1436 case DK_WEAK_DEFINITION:
1437 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1438 case DK_WEAK_REFERENCE:
1439 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1440 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1441 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1442 case DK_COMM:
1443 case DK_COMMON:
1444 return parseDirectiveComm(/*IsLocal=*/false);
1445 case DK_LCOMM:
1446 return parseDirectiveComm(/*IsLocal=*/true);
1447 case DK_ABORT:
1448 return parseDirectiveAbort();
1449 case DK_INCLUDE:
1450 return parseDirectiveInclude();
1451 case DK_INCBIN:
1452 return parseDirectiveIncbin();
1453 case DK_CODE16:
1454 case DK_CODE16GCC:
1455 return TokError(Twine(IDVal) + " not supported yet");
1456 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001457 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001458 case DK_IRP:
1459 return parseDirectiveIrp(IDLoc);
1460 case DK_IRPC:
1461 return parseDirectiveIrpc(IDLoc);
1462 case DK_ENDR:
1463 return parseDirectiveEndr(IDLoc);
1464 case DK_BUNDLE_ALIGN_MODE:
1465 return parseDirectiveBundleAlignMode();
1466 case DK_BUNDLE_LOCK:
1467 return parseDirectiveBundleLock();
1468 case DK_BUNDLE_UNLOCK:
1469 return parseDirectiveBundleUnlock();
1470 case DK_SLEB128:
1471 return parseDirectiveLEB128(true);
1472 case DK_ULEB128:
1473 return parseDirectiveLEB128(false);
1474 case DK_SPACE:
1475 case DK_SKIP:
1476 return parseDirectiveSpace(IDVal);
1477 case DK_FILE:
1478 return parseDirectiveFile(IDLoc);
1479 case DK_LINE:
1480 return parseDirectiveLine();
1481 case DK_LOC:
1482 return parseDirectiveLoc();
1483 case DK_STABS:
1484 return parseDirectiveStabs();
1485 case DK_CFI_SECTIONS:
1486 return parseDirectiveCFISections();
1487 case DK_CFI_STARTPROC:
1488 return parseDirectiveCFIStartProc();
1489 case DK_CFI_ENDPROC:
1490 return parseDirectiveCFIEndProc();
1491 case DK_CFI_DEF_CFA:
1492 return parseDirectiveCFIDefCfa(IDLoc);
1493 case DK_CFI_DEF_CFA_OFFSET:
1494 return parseDirectiveCFIDefCfaOffset();
1495 case DK_CFI_ADJUST_CFA_OFFSET:
1496 return parseDirectiveCFIAdjustCfaOffset();
1497 case DK_CFI_DEF_CFA_REGISTER:
1498 return parseDirectiveCFIDefCfaRegister(IDLoc);
1499 case DK_CFI_OFFSET:
1500 return parseDirectiveCFIOffset(IDLoc);
1501 case DK_CFI_REL_OFFSET:
1502 return parseDirectiveCFIRelOffset(IDLoc);
1503 case DK_CFI_PERSONALITY:
1504 return parseDirectiveCFIPersonalityOrLsda(true);
1505 case DK_CFI_LSDA:
1506 return parseDirectiveCFIPersonalityOrLsda(false);
1507 case DK_CFI_REMEMBER_STATE:
1508 return parseDirectiveCFIRememberState();
1509 case DK_CFI_RESTORE_STATE:
1510 return parseDirectiveCFIRestoreState();
1511 case DK_CFI_SAME_VALUE:
1512 return parseDirectiveCFISameValue(IDLoc);
1513 case DK_CFI_RESTORE:
1514 return parseDirectiveCFIRestore(IDLoc);
1515 case DK_CFI_ESCAPE:
1516 return parseDirectiveCFIEscape();
1517 case DK_CFI_SIGNAL_FRAME:
1518 return parseDirectiveCFISignalFrame();
1519 case DK_CFI_UNDEFINED:
1520 return parseDirectiveCFIUndefined(IDLoc);
1521 case DK_CFI_REGISTER:
1522 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001523 case DK_CFI_WINDOW_SAVE:
1524 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001525 case DK_MACROS_ON:
1526 case DK_MACROS_OFF:
1527 return parseDirectiveMacrosOnOff(IDVal);
1528 case DK_MACRO:
1529 return parseDirectiveMacro(IDLoc);
1530 case DK_ENDM:
1531 case DK_ENDMACRO:
1532 return parseDirectiveEndMacro(IDVal);
1533 case DK_PURGEM:
1534 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001535 case DK_END:
1536 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001537 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001538 return parseDirectiveError(IDLoc, false);
1539 case DK_ERROR:
1540 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001541 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001542
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001543 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001544 }
Chris Lattner36e02122009-06-21 20:54:55 +00001545
Chad Rosierc7f552c2013-02-12 21:33:51 +00001546 // __asm _emit or __asm __emit
1547 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1548 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001549 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001550
1551 // __asm align
1552 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001553 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001554
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001555 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001556
Chris Lattner7cbfa442010-05-19 23:34:33 +00001557 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001558 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001559 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001560 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001561 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001562 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001563
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001564 // Dump the parsed representation, if requested.
1565 if (getShowParsedOperands()) {
1566 SmallString<256> Str;
1567 raw_svector_ostream OS(Str);
1568 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001569 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001570 if (i != 0)
1571 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001572 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001573 }
1574 OS << "]";
1575
Jim Grosbach4b905842013-09-20 23:08:21 +00001576 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001577 }
1578
Kevin Enderby6469fc22011-11-01 22:27:22 +00001579 // If we are generating dwarf for assembly source files and the current
1580 // section is the initial text section then generate a .loc directive for
1581 // the instruction.
1582 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001583 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001585
Eli Bendersky88024712013-01-16 19:32:36 +00001586 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001587
Eli Bendersky88024712013-01-16 19:32:36 +00001588 // If we previously parsed a cpp hash file line comment then make sure the
1589 // current Dwarf File is for the CppHashFilename if not then emit the
1590 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001591 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001592 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1593 0, StringRef(), CppHashFilename);
1594 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001595
Jim Grosbach4b905842013-09-20 23:08:21 +00001596 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1597 // cache with the different Loc from the call above we save the last
1598 // info we queried here with SrcMgr.FindLineNumber().
1599 unsigned CppHashLocLineNo;
1600 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1601 CppHashLocLineNo = LastQueryLine;
1602 else {
1603 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1604 LastQueryLine = CppHashLocLineNo;
1605 LastQueryIDLoc = CppHashLoc;
1606 LastQueryBuffer = CppHashBuf;
1607 }
1608 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001609 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001610
Jim Grosbach4b905842013-09-20 23:08:21 +00001611 getStreamer().EmitDwarfLocDirective(
1612 getContext().getGenDwarfFileNumber(), Line, 0,
1613 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1614 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001615 }
1616
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001617 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001618 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001619 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001620 HadError = getTargetParser().MatchAndEmitInstruction(
1621 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1622 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001623 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001624
Chris Lattnera2a9d162010-09-11 16:18:25 +00001625 // Don't skip the rest of the line, the instruction parser is responsible for
1626 // that.
1627 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001628}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001629
Jim Grosbach4b905842013-09-20 23:08:21 +00001630/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001631/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001632void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001633 if (!Lexer.is(AsmToken::EndOfStatement))
1634 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001635 // Eat EOL.
1636 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001637}
1638
Jim Grosbach4b905842013-09-20 23:08:21 +00001639/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001640/// ::= # number "filename"
1641/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001642bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001643 Lex(); // Eat the hash token.
1644
1645 if (getLexer().isNot(AsmToken::Integer)) {
1646 // Consume the line since in cases it is not a well-formed line directive,
1647 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001648 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001649 return false;
1650 }
1651
1652 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001653 Lex();
1654
1655 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001656 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001657 return false;
1658 }
1659
1660 StringRef Filename = getTok().getString();
1661 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001663
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001664 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1665 CppHashLoc = L;
1666 CppHashFilename = Filename;
1667 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001668 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001669
1670 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001672 return false;
1673}
1674
Jim Grosbach4b905842013-09-20 23:08:21 +00001675/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001676/// for the Filename and LineNo if any in the diagnostic.
1677void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001678 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001679 raw_ostream &OS = errs();
1680
1681 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1682 const SMLoc &DiagLoc = Diag.getLoc();
1683 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1684 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1685
Jim Grosbach4b905842013-09-20 23:08:21 +00001686 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001687 // before printing the message.
1688 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001689 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001690 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1691 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001692 }
1693
Eric Christophera7c32732012-12-18 00:30:54 +00001694 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695 // manager changed or buffer changed (like in a nested include) then just
1696 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001697 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001699 if (Parser->SavedDiagHandler)
1700 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1701 else
1702 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703 return;
1704 }
1705
Eric Christophera7c32732012-12-18 00:30:54 +00001706 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1708 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001709 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710
1711 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1712 int CppHashLocLineNo =
1713 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 int LineNo =
1715 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001716
Jim Grosbach4b905842013-09-20 23:08:21 +00001717 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1718 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001719 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001721 if (Parser->SavedDiagHandler)
1722 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1723 else
1724 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001725}
1726
Rafael Espindola2c064482012-08-21 18:29:30 +00001727// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1728// difference being that that function accepts '@' as part of identifiers and
1729// we can't do that. AsmLexer.cpp should probably be changed to handle
1730// '@' as a special case when needed.
1731static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001732 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1733 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001734}
1735
Rafael Espindola34b9c512012-06-03 23:57:14 +00001736bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001737 ArrayRef<MCAsmMacroParameter> Parameters,
1738 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001739 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001740 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001741 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001742
Preston Gurd05500642012-09-19 20:36:12 +00001743 // A macro without parameters is handled differently on Darwin:
1744 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001745 while (!Body.empty()) {
1746 // Scan for the next substitution.
1747 std::size_t End = Body.size(), Pos = 0;
1748 for (; Pos != End; ++Pos) {
1749 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001750 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001751 // This macro has no parameters, look for $0, $1, etc.
1752 if (Body[Pos] != '$' || Pos + 1 == End)
1753 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001754
Rafael Espindola1134ab232011-06-05 02:43:45 +00001755 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001756 if (Next == '$' || Next == 'n' ||
1757 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001758 break;
1759 } else {
1760 // This macro has parameters, look for \foo, \bar, etc.
1761 if (Body[Pos] == '\\' && Pos + 1 != End)
1762 break;
1763 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001764 }
1765
1766 // Add the prefix.
1767 OS << Body.slice(0, Pos);
1768
1769 // Check if we reached the end.
1770 if (Pos == End)
1771 break;
1772
Benjamin Kramer513e7442014-02-20 13:36:32 +00001773 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001774 switch (Body[Pos + 1]) {
1775 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001776 case '$':
1777 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001778 break;
1779
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 case 'n':
1782 OS << A.size();
1783 break;
1784
Jim Grosbach4b905842013-09-20 23:08:21 +00001785 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001786 default: {
1787 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001788 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001789 if (Index >= A.size())
1790 break;
1791
1792 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001793 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001794 ie = A[Index].end();
1795 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001796 OS << it->getString();
1797 break;
1798 }
1799 }
1800 Pos += 2;
1801 } else {
1802 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001803 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001804 ++I;
1805
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 const char *Begin = Body.data() + Pos + 1;
1807 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001808 unsigned Index = 0;
1809 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001810 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001811 break;
1812
Preston Gurd05500642012-09-19 20:36:12 +00001813 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001814 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1815 Pos += 3;
1816 else {
1817 OS << '\\' << Argument;
1818 Pos = I;
1819 }
Preston Gurd05500642012-09-19 20:36:12 +00001820 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001821 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001822 ie = A[Index].end();
1823 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001824 if (it->getKind() == AsmToken::String)
1825 OS << it->getStringContents();
1826 else
1827 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828
Preston Gurd05500642012-09-19 20:36:12 +00001829 Pos += 1 + Argument.size();
1830 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001832 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001833 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001834 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001835
Rafael Espindola1134ab232011-06-05 02:43:45 +00001836 return false;
1837}
Daniel Dunbar43235712010-07-18 18:54:11 +00001838
Jim Grosbach4b905842013-09-20 23:08:21 +00001839MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1840 SMLoc EL, MemoryBuffer *I)
1841 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1842 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001843
Jim Grosbach4b905842013-09-20 23:08:21 +00001844static bool isOperator(AsmToken::TokenKind kind) {
1845 switch (kind) {
1846 default:
1847 return false;
1848 case AsmToken::Plus:
1849 case AsmToken::Minus:
1850 case AsmToken::Tilde:
1851 case AsmToken::Slash:
1852 case AsmToken::Star:
1853 case AsmToken::Dot:
1854 case AsmToken::Equal:
1855 case AsmToken::EqualEqual:
1856 case AsmToken::Pipe:
1857 case AsmToken::PipePipe:
1858 case AsmToken::Caret:
1859 case AsmToken::Amp:
1860 case AsmToken::AmpAmp:
1861 case AsmToken::Exclaim:
1862 case AsmToken::ExclaimEqual:
1863 case AsmToken::Percent:
1864 case AsmToken::Less:
1865 case AsmToken::LessEqual:
1866 case AsmToken::LessLess:
1867 case AsmToken::LessGreater:
1868 case AsmToken::Greater:
1869 case AsmToken::GreaterEqual:
1870 case AsmToken::GreaterGreater:
1871 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001872 }
1873}
1874
David Majnemer16252452014-01-29 00:07:39 +00001875namespace {
1876class AsmLexerSkipSpaceRAII {
1877public:
1878 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1879 Lexer.setSkipSpace(SkipSpace);
1880 }
1881
1882 ~AsmLexerSkipSpaceRAII() {
1883 Lexer.setSkipSpace(true);
1884 }
1885
1886private:
1887 AsmLexer &Lexer;
1888};
1889}
1890
David Majnemer91fc4c22014-01-29 18:57:46 +00001891bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001892 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001893 unsigned AddTokens = 0;
1894
David Majnemer16252452014-01-29 00:07:39 +00001895 // Darwin doesn't use spaces to delmit arguments.
1896 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001897
1898 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001899 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001900 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001901
David Majnemer91fc4c22014-01-29 18:57:46 +00001902 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001903 break;
Preston Gurd05500642012-09-19 20:36:12 +00001904
1905 if (Lexer.is(AsmToken::Space)) {
1906 Lex(); // Eat spaces
1907
1908 // Spaces can delimit parameters, but could also be part an expression.
1909 // If the token after a space is an operator, add the token and the next
1910 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001911 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001912 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001913 // Check to see whether the token is used as an operator,
1914 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001915 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001916 if (*NextChar == ' ')
1917 AddTokens = 2;
1918 }
1919
1920 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001921 break;
1922 }
1923 }
1924 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001925
Jim Grosbach4b905842013-09-20 23:08:21 +00001926 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927 // to be able to fill in the remaining default parameter values
1928 if (Lexer.is(AsmToken::EndOfStatement))
1929 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930
1931 // Adjust the current parentheses level.
1932 if (Lexer.is(AsmToken::LParen))
1933 ++ParenLevel;
1934 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1935 --ParenLevel;
1936
1937 // Append the token to the current argument list.
1938 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001939 if (AddTokens)
1940 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001941 Lex();
1942 }
Preston Gurd05500642012-09-19 20:36:12 +00001943
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001945 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001946 return false;
1947}
1948
1949// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001950bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001951 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001952 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001953 bool NamedParametersFound = false;
1954 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001956 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001957 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001958
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959 // Parse two kinds of macro invocations:
1960 // - macros defined without any parameters accept an arbitrary number of them
1961 // - macros defined with parameters accept at most that many of them
1962 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1963 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001964 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001965 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001966
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001967 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001968 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001969 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001970 eatToEndOfStatement();
1971 return true;
1972 }
1973
1974 if (!Lexer.is(AsmToken::Equal)) {
1975 TokError("expected '=' after formal parameter identifier");
1976 eatToEndOfStatement();
1977 return true;
1978 }
1979 Lex();
1980
1981 NamedParametersFound = true;
1982 }
1983
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001984 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001985 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986 eatToEndOfStatement();
1987 return true;
1988 }
1989
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001990 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001991 return true;
1992
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001993 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001994 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001995 unsigned FAI = 0;
1996 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001997 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001999
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002000 if (FAI >= NParameters) {
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002001 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002002 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002003 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002004 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002005 return true;
2006 }
2007 PI = FAI;
2008 }
2009
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002010 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002011 if (A.size() <= PI)
2012 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002013 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002014
2015 if (FALocs.size() <= PI)
2016 FALocs.resize(PI + 1);
2017
2018 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002019 }
Jim Grosbach206661622012-07-30 22:44:17 +00002020
Preston Gurd242ed3152012-09-19 20:29:04 +00002021 // At the end of the statement, fill in remaining arguments that have
2022 // default values. If there aren't any, then the next argument is
2023 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002024 if (Lexer.is(AsmToken::EndOfStatement)) {
2025 bool Failure = false;
2026 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2027 if (A[FAI].empty()) {
2028 if (M->Parameters[FAI].Required) {
2029 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2030 "missing value for required parameter "
2031 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2032 Failure = true;
2033 }
2034
2035 if (!M->Parameters[FAI].Value.empty())
2036 A[FAI] = M->Parameters[FAI].Value;
2037 }
2038 }
2039 return Failure;
2040 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002041
2042 if (Lexer.is(AsmToken::Comma))
2043 Lex();
2044 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002045
2046 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002047}
2048
Jim Grosbach4b905842013-09-20 23:08:21 +00002049const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2050 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002051 return (I == MacroMap.end()) ? NULL : I->getValue();
2052}
2053
Jim Grosbach4b905842013-09-20 23:08:21 +00002054void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002055 MacroMap[Name] = new MCAsmMacro(Macro);
2056}
2057
Jim Grosbach4b905842013-09-20 23:08:21 +00002058void AsmParser::undefineMacro(StringRef Name) {
2059 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002060 if (I != MacroMap.end()) {
2061 delete I->getValue();
2062 MacroMap.erase(I);
2063 }
2064}
2065
Jim Grosbach4b905842013-09-20 23:08:21 +00002066bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002067 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2068 // this, although we should protect against infinite loops.
2069 if (ActiveMacros.size() == 20)
2070 return TokError("macros cannot be nested more than 20 levels deep");
2071
Eli Bendersky38274122013-01-14 23:22:36 +00002072 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002073 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002074 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002075
Rafael Espindola1134ab232011-06-05 02:43:45 +00002076 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2077 // to hold the macro body with substitutions.
2078 SmallString<256> Buf;
2079 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002080 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002081
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002082 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002083 return true;
2084
Eli Bendersky38274122013-01-14 23:22:36 +00002085 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002086 // instantiation.
2087 OS << ".endmacro\n";
2088
Rafael Espindola1134ab232011-06-05 02:43:45 +00002089 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002090 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002091
Daniel Dunbar43235712010-07-18 18:54:11 +00002092 // Create the macro instantiation object and add to the current macro
2093 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002094 MacroInstantiation *MI = new MacroInstantiation(
2095 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002096 ActiveMacros.push_back(MI);
2097
2098 // Jump to the macro instantiation and prime the lexer.
2099 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2100 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2101 Lex();
2102
2103 return false;
2104}
2105
Jim Grosbach4b905842013-09-20 23:08:21 +00002106void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002107 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002108 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002109 Lex();
2110
2111 // Pop the instantiation entry.
2112 delete ActiveMacros.back();
2113 ActiveMacros.pop_back();
2114}
2115
Jim Grosbach4b905842013-09-20 23:08:21 +00002116static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002117 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002118 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002119 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2120 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002121 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002122 case MCExpr::Target:
2123 case MCExpr::Constant:
2124 return false;
2125 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 const MCSymbol &S =
2127 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002128 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002129 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002130 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002131 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002132 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002133 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002134 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002135
2136 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002137}
2138
Jim Grosbach4b905842013-09-20 23:08:21 +00002139bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002140 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002141 // FIXME: Use better location, we should use proper tokens.
2142 SMLoc EqualLoc = Lexer.getLoc();
2143
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002144 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002145 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002146 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002147
Rafael Espindola72f5f172012-01-28 05:57:00 +00002148 // Note: we don't count b as used in "a = b". This is to allow
2149 // a = b
2150 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002151
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002152 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002153 return TokError("unexpected token in assignment");
2154
2155 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002156 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002157
Daniel Dunbar5f339242009-10-16 01:57:39 +00002158 // Validate that the LHS is allowed to be a variable (either it has not been
2159 // used as a symbol, or it is an absolute symbol).
2160 MCSymbol *Sym = getContext().LookupSymbol(Name);
2161 if (Sym) {
2162 // Diagnose assignment to a label.
2163 //
2164 // FIXME: Diagnostics. Note the location of the definition as a label.
2165 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002166 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002167 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2168 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002169 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002170 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2171 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002172 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002173 return Error(EqualLoc, "redefinition of '" + Name + "'");
2174 else if (!Sym->isVariable())
2175 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002176 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002177 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002178 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002179
2180 // Don't count these checks as uses.
2181 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002182 } else if (Name == ".") {
2183 if (Out.EmitValueToOffset(Value, 0)) {
2184 Error(EqualLoc, "expected absolute expression");
2185 eatToEndOfStatement();
2186 }
2187 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002188 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002189 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002190
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002191 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002192 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002193 if (NoDeadStrip)
2194 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2195
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002196 return false;
2197}
2198
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002199/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002200/// ::= identifier
2201/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002202bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002203 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002204 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2205 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002206 // handle this as a context dependent token, instead we detect adjacent tokens
2207 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002208 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2209 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002210
Hans Wennborgce69d772013-10-18 20:46:28 +00002211 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002212 Lex();
2213 if (Lexer.isNot(AsmToken::Identifier))
2214 return true;
2215
Hans Wennborgce69d772013-10-18 20:46:28 +00002216 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2217 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002218 return true;
2219
2220 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002221 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002222 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002223 Lex();
2224 return false;
2225 }
2226
Jim Grosbach4b905842013-09-20 23:08:21 +00002227 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002228 return true;
2229
Sean Callanan936b0d32010-01-19 21:44:56 +00002230 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002231
Sean Callanan686ed8d2010-01-19 20:22:31 +00002232 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002233
2234 return false;
2235}
2236
Jim Grosbach4b905842013-09-20 23:08:21 +00002237/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002238/// ::= .equ identifier ',' expression
2239/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002240/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002241bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002242 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002243
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002244 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002245 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002246
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002247 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002248 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002249 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002250
Jim Grosbach4b905842013-09-20 23:08:21 +00002251 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002252}
2253
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002254bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002255 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002256
2257 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002258 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002259 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2260 if (Str[i] != '\\') {
2261 Data += Str[i];
2262 continue;
2263 }
2264
2265 // Recognize escaped characters. Note that this escape semantics currently
2266 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2267 ++i;
2268 if (i == e)
2269 return TokError("unexpected backslash at end of string");
2270
2271 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002272 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002273 // Consume up to three octal characters.
2274 unsigned Value = Str[i] - '0';
2275
Jim Grosbach4b905842013-09-20 23:08:21 +00002276 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002277 ++i;
2278 Value = Value * 8 + (Str[i] - '0');
2279
Jim Grosbach4b905842013-09-20 23:08:21 +00002280 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002281 ++i;
2282 Value = Value * 8 + (Str[i] - '0');
2283 }
2284 }
2285
2286 if (Value > 255)
2287 return TokError("invalid octal escape sequence (out of range)");
2288
Jim Grosbach4b905842013-09-20 23:08:21 +00002289 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002290 continue;
2291 }
2292
2293 // Otherwise recognize individual escapes.
2294 switch (Str[i]) {
2295 default:
2296 // Just reject invalid escape sequences for now.
2297 return TokError("invalid escape sequence (unrecognized character)");
2298
2299 case 'b': Data += '\b'; break;
2300 case 'f': Data += '\f'; break;
2301 case 'n': Data += '\n'; break;
2302 case 'r': Data += '\r'; break;
2303 case 't': Data += '\t'; break;
2304 case '"': Data += '"'; break;
2305 case '\\': Data += '\\'; break;
2306 }
2307 }
2308
2309 return false;
2310}
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002313/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002314bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002315 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002316 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002317
Daniel Dunbara10e5192009-06-24 23:30:00 +00002318 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002319 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002320 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002321
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002323 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002324 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002325
Rafael Espindola64e1af82013-07-02 15:49:13 +00002326 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002327 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002328 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002329
Sean Callanan686ed8d2010-01-19 20:22:31 +00002330 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002331
2332 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002333 break;
2334
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002335 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002336 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002337 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002338 }
2339 }
2340
Sean Callanan686ed8d2010-01-19 20:22:31 +00002341 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002342 return false;
2343}
2344
Jim Grosbach4b905842013-09-20 23:08:21 +00002345/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002346/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002347bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002348 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002349 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002350
Daniel Dunbara10e5192009-06-24 23:30:00 +00002351 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002352 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002353 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002354 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002355 return true;
2356
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002357 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002358 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2359 assert(Size <= 8 && "Invalid size");
2360 uint64_t IntValue = MCE->getValue();
2361 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2362 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002363 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002364 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002365 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002366
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002368 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002369
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002371 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002372 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 }
2375 }
2376
Sean Callanan686ed8d2010-01-19 20:22:31 +00002377 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378 return false;
2379}
2380
David Woodhoused6de0d92014-02-01 16:20:59 +00002381/// ParseDirectiveOctaValue
2382/// ::= .octa [ hexconstant (, hexconstant)* ]
2383bool AsmParser::parseDirectiveOctaValue() {
2384 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2385 checkForValidSection();
2386
2387 for (;;) {
2388 if (Lexer.getKind() == AsmToken::Error)
2389 return true;
2390 if (Lexer.getKind() != AsmToken::Integer &&
2391 Lexer.getKind() != AsmToken::BigNum)
2392 return TokError("unknown token in expression");
2393
2394 SMLoc ExprLoc = getLexer().getLoc();
2395 APInt IntValue = getTok().getAPIntVal();
2396 Lex();
2397
2398 uint64_t hi, lo;
2399 if (IntValue.isIntN(64)) {
2400 hi = 0;
2401 lo = IntValue.getZExtValue();
2402 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002403 // It might actually have more than 128 bits, but the top ones are zero.
2404 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002405 lo = IntValue.getLoBits(64).getZExtValue();
2406 } else
2407 return Error(ExprLoc, "literal value out of range for directive");
2408
2409 if (MAI.isLittleEndian()) {
2410 getStreamer().EmitIntValue(lo, 8);
2411 getStreamer().EmitIntValue(hi, 8);
2412 } else {
2413 getStreamer().EmitIntValue(hi, 8);
2414 getStreamer().EmitIntValue(lo, 8);
2415 }
2416
2417 if (getLexer().is(AsmToken::EndOfStatement))
2418 break;
2419
2420 // FIXME: Improve diagnostic.
2421 if (getLexer().isNot(AsmToken::Comma))
2422 return TokError("unexpected token in directive");
2423 Lex();
2424 }
2425 }
2426
2427 Lex();
2428 return false;
2429}
2430
Jim Grosbach4b905842013-09-20 23:08:21 +00002431/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002432/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002433bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002434 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002435 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002436
2437 for (;;) {
2438 // We don't truly support arithmetic on floating point expressions, so we
2439 // have to manually parse unary prefixes.
2440 bool IsNeg = false;
2441 if (getLexer().is(AsmToken::Minus)) {
2442 Lex();
2443 IsNeg = true;
2444 } else if (getLexer().is(AsmToken::Plus))
2445 Lex();
2446
Michael J. Spencer530ce852010-10-09 11:00:50 +00002447 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002448 getLexer().isNot(AsmToken::Real) &&
2449 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002450 return TokError("unexpected token in directive");
2451
2452 // Convert to an APFloat.
2453 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002454 StringRef IDVal = getTok().getString();
2455 if (getLexer().is(AsmToken::Identifier)) {
2456 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2457 Value = APFloat::getInf(Semantics);
2458 else if (!IDVal.compare_lower("nan"))
2459 Value = APFloat::getNaN(Semantics, false, ~0);
2460 else
2461 return TokError("invalid floating point literal");
2462 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002463 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464 return TokError("invalid floating point literal");
2465 if (IsNeg)
2466 Value.changeSign();
2467
2468 // Consume the numeric token.
2469 Lex();
2470
2471 // Emit the value as an integer.
2472 APInt AsInt = Value.bitcastToAPInt();
2473 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002474 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002475
2476 if (getLexer().is(AsmToken::EndOfStatement))
2477 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002478
Daniel Dunbar2af16532010-09-24 01:59:56 +00002479 if (getLexer().isNot(AsmToken::Comma))
2480 return TokError("unexpected token in directive");
2481 Lex();
2482 }
2483 }
2484
2485 Lex();
2486 return false;
2487}
2488
Jim Grosbach4b905842013-09-20 23:08:21 +00002489/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002490/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002491bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002492 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002493
2494 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002495 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002496 return true;
2497
Rafael Espindolab91bac62010-10-05 19:42:57 +00002498 int64_t Val = 0;
2499 if (getLexer().is(AsmToken::Comma)) {
2500 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002501 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002502 return true;
2503 }
2504
Rafael Espindola922e3f42010-09-16 15:03:59 +00002505 if (getLexer().isNot(AsmToken::EndOfStatement))
2506 return TokError("unexpected token in '.zero' directive");
2507
2508 Lex();
2509
Rafael Espindola64e1af82013-07-02 15:49:13 +00002510 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002511
2512 return false;
2513}
2514
Jim Grosbach4b905842013-09-20 23:08:21 +00002515/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002516/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002517bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002518 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002519
David Majnemer522d3db2014-02-01 07:19:38 +00002520 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002521 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002522 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002523 return true;
2524
David Majnemer522d3db2014-02-01 07:19:38 +00002525 if (NumValues < 0) {
2526 Warning(RepeatLoc,
2527 "'.fill' directive with negative repeat count has no effect");
2528 NumValues = 0;
2529 }
2530
Roman Divackye33098f2013-09-24 17:44:41 +00002531 int64_t FillSize = 1;
2532 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002533
David Majnemer522d3db2014-02-01 07:19:38 +00002534 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2536 if (getLexer().isNot(AsmToken::Comma))
2537 return TokError("unexpected token in '.fill' directive");
2538 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539
David Majnemer522d3db2014-02-01 07:19:38 +00002540 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002541 if (parseAbsoluteExpression(FillSize))
2542 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002543
Roman Divackye33098f2013-09-24 17:44:41 +00002544 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2545 if (getLexer().isNot(AsmToken::Comma))
2546 return TokError("unexpected token in '.fill' directive");
2547 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002548
David Majnemer522d3db2014-02-01 07:19:38 +00002549 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002550 if (parseAbsoluteExpression(FillExpr))
2551 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002552
Roman Divackye33098f2013-09-24 17:44:41 +00002553 if (getLexer().isNot(AsmToken::EndOfStatement))
2554 return TokError("unexpected token in '.fill' directive");
2555
2556 Lex();
2557 }
2558 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002559
David Majnemer522d3db2014-02-01 07:19:38 +00002560 if (FillSize < 0) {
2561 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2562 NumValues = 0;
2563 }
2564 if (FillSize > 8) {
2565 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2566 FillSize = 8;
2567 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002568
David Majnemer522d3db2014-02-01 07:19:38 +00002569 if (!isUInt<32>(FillExpr) && FillSize > 4)
2570 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2571
2572 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2573 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2574
2575 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2576 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2577 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2578 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002579
2580 return false;
2581}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002582
Jim Grosbach4b905842013-09-20 23:08:21 +00002583/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002584/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002585bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002586 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002587
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002588 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002589 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002590 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002591 return true;
2592
2593 // Parse optional fill expression.
2594 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002595 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2596 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002597 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002598 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002599
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002600 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002601 return true;
2602
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002603 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002604 return TokError("unexpected token in '.org' directive");
2605 }
2606
Sean Callanan686ed8d2010-01-19 20:22:31 +00002607 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002608
Jim Grosbachb5912772012-01-27 00:37:08 +00002609 // Only limited forms of relocatable expressions are accepted here, it
2610 // has to be relative to the current section. The streamer will return
2611 // 'true' if the expression wasn't evaluatable.
2612 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2613 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002614
2615 return false;
2616}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002617
Jim Grosbach4b905842013-09-20 23:08:21 +00002618/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002619/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002620bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002621 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002622
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002623 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002624 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002625 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002626 return true;
2627
2628 SMLoc MaxBytesLoc;
2629 bool HasFillExpr = false;
2630 int64_t FillExpr = 0;
2631 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2633 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002634 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002635 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002636
2637 // The fill expression can be omitted while specifying a maximum number of
2638 // alignment bytes, e.g:
2639 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002640 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002641 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002642 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002643 return true;
2644 }
2645
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002646 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2647 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002648 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002649 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002650
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002651 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002652 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002654
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002655 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002656 return TokError("unexpected token in directive");
2657 }
2658 }
2659
Sean Callanan686ed8d2010-01-19 20:22:31 +00002660 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002662 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002663 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002664
2665 // Compute alignment in bytes.
2666 if (IsPow2) {
2667 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002668 if (Alignment >= 32) {
2669 Error(AlignmentLoc, "invalid alignment value");
2670 Alignment = 31;
2671 }
2672
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002673 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002674 } else {
2675 // Reject alignments that aren't a power of two, for gas compatibility.
2676 if (!isPowerOf2_64(Alignment))
2677 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678 }
2679
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002680 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681 if (MaxBytesLoc.isValid()) {
2682 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002683 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002684 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002685 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 }
2687
2688 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002689 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002690 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 MaxBytesToFill = 0;
2692 }
2693 }
2694
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002695 // Check whether we should use optimal code alignment for this .align
2696 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002697 const MCSection *Section = getStreamer().getCurrentSection().first;
2698 assert(Section && "must have section to emit alignment");
2699 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002700 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2701 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002702 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002703 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002704 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002705 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2706 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002707 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002708
2709 return false;
2710}
2711
Jim Grosbach4b905842013-09-20 23:08:21 +00002712/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002713/// ::= .file [number] filename
2714/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002715bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002716 // FIXME: I'm not sure what this is.
2717 int64_t FileNumber = -1;
2718 SMLoc FileNumberLoc = getLexer().getLoc();
2719 if (getLexer().is(AsmToken::Integer)) {
2720 FileNumber = getTok().getIntVal();
2721 Lex();
2722
2723 if (FileNumber < 1)
2724 return TokError("file number less than one");
2725 }
2726
2727 if (getLexer().isNot(AsmToken::String))
2728 return TokError("unexpected token in '.file' directive");
2729
2730 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002731 // Allow the strings to have escaped octal character sequence.
2732 std::string Path = getTok().getString();
2733 if (parseEscapedString(Path))
2734 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002735 Lex();
2736
2737 StringRef Directory;
2738 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002739 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002740 if (getLexer().is(AsmToken::String)) {
2741 if (FileNumber == -1)
2742 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002743 if (parseEscapedString(FilenameData))
2744 return true;
2745 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002746 Directory = Path;
2747 Lex();
2748 } else {
2749 Filename = Path;
2750 }
2751
2752 if (getLexer().isNot(AsmToken::EndOfStatement))
2753 return TokError("unexpected token in '.file' directive");
2754
2755 if (FileNumber == -1)
2756 getStreamer().EmitFileDirective(Filename);
2757 else {
2758 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002759 Error(DirectiveLoc,
2760 "input can't have .file dwarf directives when -g is "
2761 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002762
David Blaikiec714ef42014-03-17 01:52:11 +00002763 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2764 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002765 Error(FileNumberLoc, "file number already allocated");
2766 }
2767
2768 return false;
2769}
2770
Jim Grosbach4b905842013-09-20 23:08:21 +00002771/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002772/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002773bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002774 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2775 if (getLexer().isNot(AsmToken::Integer))
2776 return TokError("unexpected token in '.line' directive");
2777
2778 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002779 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002780 Lex();
2781
2782 // FIXME: Do something with the .line.
2783 }
2784
2785 if (getLexer().isNot(AsmToken::EndOfStatement))
2786 return TokError("unexpected token in '.line' directive");
2787
2788 return false;
2789}
2790
Jim Grosbach4b905842013-09-20 23:08:21 +00002791/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002792/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2793/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2794/// The first number is a file number, must have been previously assigned with
2795/// a .file directive, the second number is the line number and optionally the
2796/// third number is a column position (zero if not specified). The remaining
2797/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002798bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002799 if (getLexer().isNot(AsmToken::Integer))
2800 return TokError("unexpected token in '.loc' directive");
2801 int64_t FileNumber = getTok().getIntVal();
2802 if (FileNumber < 1)
2803 return TokError("file number less than one in '.loc' directive");
2804 if (!getContext().isValidDwarfFileNumber(FileNumber))
2805 return TokError("unassigned file number in '.loc' directive");
2806 Lex();
2807
2808 int64_t LineNumber = 0;
2809 if (getLexer().is(AsmToken::Integer)) {
2810 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002811 if (LineNumber < 0)
2812 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002813 Lex();
2814 }
2815
2816 int64_t ColumnPos = 0;
2817 if (getLexer().is(AsmToken::Integer)) {
2818 ColumnPos = getTok().getIntVal();
2819 if (ColumnPos < 0)
2820 return TokError("column position less than zero in '.loc' directive");
2821 Lex();
2822 }
2823
2824 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2825 unsigned Isa = 0;
2826 int64_t Discriminator = 0;
2827 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2828 for (;;) {
2829 if (getLexer().is(AsmToken::EndOfStatement))
2830 break;
2831
2832 StringRef Name;
2833 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002834 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002835 return TokError("unexpected token in '.loc' directive");
2836
2837 if (Name == "basic_block")
2838 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2839 else if (Name == "prologue_end")
2840 Flags |= DWARF2_FLAG_PROLOGUE_END;
2841 else if (Name == "epilogue_begin")
2842 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2843 else if (Name == "is_stmt") {
2844 Loc = getTok().getLoc();
2845 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002846 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002847 return true;
2848 // The expression must be the constant 0 or 1.
2849 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2850 int Value = MCE->getValue();
2851 if (Value == 0)
2852 Flags &= ~DWARF2_FLAG_IS_STMT;
2853 else if (Value == 1)
2854 Flags |= DWARF2_FLAG_IS_STMT;
2855 else
2856 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002857 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002858 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2859 }
Craig Topperf15655b2013-04-22 04:22:40 +00002860 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002861 Loc = getTok().getLoc();
2862 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002863 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002864 return true;
2865 // The expression must be a constant greater or equal to 0.
2866 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2867 int Value = MCE->getValue();
2868 if (Value < 0)
2869 return Error(Loc, "isa number less than zero");
2870 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002871 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002872 return Error(Loc, "isa number not a constant value");
2873 }
Craig Topperf15655b2013-04-22 04:22:40 +00002874 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002875 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002876 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002877 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002878 return Error(Loc, "unknown sub-directive in '.loc' directive");
2879 }
2880
2881 if (getLexer().is(AsmToken::EndOfStatement))
2882 break;
2883 }
2884 }
2885
2886 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2887 Isa, Discriminator, StringRef());
2888
2889 return false;
2890}
2891
Jim Grosbach4b905842013-09-20 23:08:21 +00002892/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002893/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002894bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002895 return TokError("unsupported directive '.stabs'");
2896}
2897
Jim Grosbach4b905842013-09-20 23:08:21 +00002898/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002899/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002900bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002901 StringRef Name;
2902 bool EH = false;
2903 bool Debug = false;
2904
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002905 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return TokError("Expected an identifier");
2907
2908 if (Name == ".eh_frame")
2909 EH = true;
2910 else if (Name == ".debug_frame")
2911 Debug = true;
2912
2913 if (getLexer().is(AsmToken::Comma)) {
2914 Lex();
2915
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002916 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002917 return TokError("Expected an identifier");
2918
2919 if (Name == ".eh_frame")
2920 EH = true;
2921 else if (Name == ".debug_frame")
2922 Debug = true;
2923 }
2924
2925 getStreamer().EmitCFISections(EH, Debug);
2926 return false;
2927}
2928
Jim Grosbach4b905842013-09-20 23:08:21 +00002929/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002930/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002931bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002932 StringRef Simple;
2933 if (getLexer().isNot(AsmToken::EndOfStatement))
2934 if (parseIdentifier(Simple) || Simple != "simple")
2935 return TokError("unexpected token in .cfi_startproc directive");
2936
2937 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002938 return false;
2939}
2940
Jim Grosbach4b905842013-09-20 23:08:21 +00002941/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002942/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002943bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002944 getStreamer().EmitCFIEndProc();
2945 return false;
2946}
2947
Jim Grosbach4b905842013-09-20 23:08:21 +00002948/// \brief parse register name or number.
2949bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002950 SMLoc DirectiveLoc) {
2951 unsigned RegNo;
2952
2953 if (getLexer().isNot(AsmToken::Integer)) {
2954 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2955 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002956 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002957 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002958 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002959
2960 return false;
2961}
2962
Jim Grosbach4b905842013-09-20 23:08:21 +00002963/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002964/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002965bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002966 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002967 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002968 return true;
2969
2970 if (getLexer().isNot(AsmToken::Comma))
2971 return TokError("unexpected token in directive");
2972 Lex();
2973
2974 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002975 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002976 return true;
2977
2978 getStreamer().EmitCFIDefCfa(Register, Offset);
2979 return false;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002983/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002984bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002985 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002986 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002987 return true;
2988
2989 getStreamer().EmitCFIDefCfaOffset(Offset);
2990 return false;
2991}
2992
Jim Grosbach4b905842013-09-20 23:08:21 +00002993/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002994/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002995bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002996 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002997 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002998 return true;
2999
3000 if (getLexer().isNot(AsmToken::Comma))
3001 return TokError("unexpected token in directive");
3002 Lex();
3003
3004 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003005 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return true;
3007
3008 getStreamer().EmitCFIRegister(Register1, Register2);
3009 return false;
3010}
3011
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003012/// parseDirectiveCFIWindowSave
3013/// ::= .cfi_window_save
3014bool AsmParser::parseDirectiveCFIWindowSave() {
3015 getStreamer().EmitCFIWindowSave();
3016 return false;
3017}
3018
Jim Grosbach4b905842013-09-20 23:08:21 +00003019/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003020/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003021bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003022 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003023 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003024 return true;
3025
3026 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3027 return false;
3028}
3029
Jim Grosbach4b905842013-09-20 23:08:21 +00003030/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003031/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003032bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003033 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003034 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003035 return true;
3036
3037 getStreamer().EmitCFIDefCfaRegister(Register);
3038 return false;
3039}
3040
Jim Grosbach4b905842013-09-20 23:08:21 +00003041/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003042/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003043bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003044 int64_t Register = 0;
3045 int64_t Offset = 0;
3046
Jim Grosbach4b905842013-09-20 23:08:21 +00003047 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return true;
3049
3050 if (getLexer().isNot(AsmToken::Comma))
3051 return TokError("unexpected token in directive");
3052 Lex();
3053
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003054 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return true;
3056
3057 getStreamer().EmitCFIOffset(Register, Offset);
3058 return false;
3059}
3060
Jim Grosbach4b905842013-09-20 23:08:21 +00003061/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003062/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003063bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003064 int64_t Register = 0;
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003067 return true;
3068
3069 if (getLexer().isNot(AsmToken::Comma))
3070 return TokError("unexpected token in directive");
3071 Lex();
3072
3073 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003074 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003075 return true;
3076
3077 getStreamer().EmitCFIRelOffset(Register, Offset);
3078 return false;
3079}
3080
3081static bool isValidEncoding(int64_t Encoding) {
3082 if (Encoding & ~0xff)
3083 return false;
3084
3085 if (Encoding == dwarf::DW_EH_PE_omit)
3086 return true;
3087
3088 const unsigned Format = Encoding & 0xf;
3089 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3090 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3091 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3092 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3093 return false;
3094
3095 const unsigned Application = Encoding & 0x70;
3096 if (Application != dwarf::DW_EH_PE_absptr &&
3097 Application != dwarf::DW_EH_PE_pcrel)
3098 return false;
3099
3100 return true;
3101}
3102
Jim Grosbach4b905842013-09-20 23:08:21 +00003103/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003104/// IsPersonality true for cfi_personality, false for cfi_lsda
3105/// ::= .cfi_personality encoding, [symbol_name]
3106/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003107bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003108 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003109 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return true;
3111 if (Encoding == dwarf::DW_EH_PE_omit)
3112 return false;
3113
3114 if (!isValidEncoding(Encoding))
3115 return TokError("unsupported encoding.");
3116
3117 if (getLexer().isNot(AsmToken::Comma))
3118 return TokError("unexpected token in directive");
3119 Lex();
3120
3121 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003122 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003123 return TokError("expected identifier in directive");
3124
3125 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3126
3127 if (IsPersonality)
3128 getStreamer().EmitCFIPersonality(Sym, Encoding);
3129 else
3130 getStreamer().EmitCFILsda(Sym, Encoding);
3131 return false;
3132}
3133
Jim Grosbach4b905842013-09-20 23:08:21 +00003134/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003135/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003136bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003137 getStreamer().EmitCFIRememberState();
3138 return false;
3139}
3140
Jim Grosbach4b905842013-09-20 23:08:21 +00003141/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003142/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003143bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003144 getStreamer().EmitCFIRestoreState();
3145 return false;
3146}
3147
Jim Grosbach4b905842013-09-20 23:08:21 +00003148/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003149/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003150bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003151 int64_t Register = 0;
3152
Jim Grosbach4b905842013-09-20 23:08:21 +00003153 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003154 return true;
3155
3156 getStreamer().EmitCFISameValue(Register);
3157 return false;
3158}
3159
Jim Grosbach4b905842013-09-20 23:08:21 +00003160/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003161/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003162bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003163 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003164 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003165 return true;
3166
3167 getStreamer().EmitCFIRestore(Register);
3168 return false;
3169}
3170
Jim Grosbach4b905842013-09-20 23:08:21 +00003171/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003172/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003173bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003174 std::string Values;
3175 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003176 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003177 return true;
3178
3179 Values.push_back((uint8_t)CurrValue);
3180
3181 while (getLexer().is(AsmToken::Comma)) {
3182 Lex();
3183
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003184 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003185 return true;
3186
3187 Values.push_back((uint8_t)CurrValue);
3188 }
3189
3190 getStreamer().EmitCFIEscape(Values);
3191 return false;
3192}
3193
Jim Grosbach4b905842013-09-20 23:08:21 +00003194/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003195/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003196bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003197 if (getLexer().isNot(AsmToken::EndOfStatement))
3198 return Error(getLexer().getLoc(),
3199 "unexpected token in '.cfi_signal_frame'");
3200
3201 getStreamer().EmitCFISignalFrame();
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003207bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003208 int64_t Register = 0;
3209
Jim Grosbach4b905842013-09-20 23:08:21 +00003210 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003211 return true;
3212
3213 getStreamer().EmitCFIUndefined(Register);
3214 return false;
3215}
3216
Jim Grosbach4b905842013-09-20 23:08:21 +00003217/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003218/// ::= .macros_on
3219/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003220bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003221 if (getLexer().isNot(AsmToken::EndOfStatement))
3222 return Error(getLexer().getLoc(),
3223 "unexpected token in '" + Directive + "' directive");
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003226 return false;
3227}
3228
Jim Grosbach4b905842013-09-20 23:08:21 +00003229/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003230/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003231bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003232 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003233 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003234 return TokError("expected identifier in '.macro' directive");
3235
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003236 if (getLexer().is(AsmToken::Comma))
3237 Lex();
3238
Eli Bendersky17233942013-01-15 22:59:42 +00003239 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003240 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3241 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003242 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003243 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003244
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003245 if (Lexer.is(AsmToken::Colon)) {
3246 Lex(); // consume ':'
3247
3248 SMLoc QualLoc;
3249 StringRef Qualifier;
3250
3251 QualLoc = Lexer.getLoc();
3252 if (parseIdentifier(Qualifier))
3253 return Error(QualLoc, "missing parameter qualifier for "
3254 "'" + Parameter.Name + "' in macro '" + Name + "'");
3255
3256 if (Qualifier == "req")
3257 Parameter.Required = true;
3258 else
3259 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3260 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3261 }
3262
David Majnemer91fc4c22014-01-29 18:57:46 +00003263 if (getLexer().is(AsmToken::Equal)) {
3264 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003265
3266 SMLoc ParamLoc;
3267
3268 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003269 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003270 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003271
3272 if (Parameter.Required)
3273 Warning(ParamLoc, "pointless default value for required parameter "
3274 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003275 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003276
3277 Parameters.push_back(Parameter);
3278
3279 if (getLexer().is(AsmToken::Comma))
3280 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003281 }
3282
3283 // Eat the end of statement.
3284 Lex();
3285
3286 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003287 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003288
3289 // Lex the macro definition.
3290 for (;;) {
3291 // Check whether we have reached the end of the file.
3292 if (getLexer().is(AsmToken::Eof))
3293 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3294
3295 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003296 if (getLexer().is(AsmToken::Identifier)) {
3297 if (getTok().getIdentifier() == ".endm" ||
3298 getTok().getIdentifier() == ".endmacro") {
3299 if (MacroDepth == 0) { // Outermost macro.
3300 EndToken = getTok();
3301 Lex();
3302 if (getLexer().isNot(AsmToken::EndOfStatement))
3303 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3304 "' directive");
3305 break;
3306 } else {
3307 // Otherwise we just found the end of an inner macro.
3308 --MacroDepth;
3309 }
3310 } else if (getTok().getIdentifier() == ".macro") {
3311 // We allow nested macros. Those aren't instantiated until the outermost
3312 // macro is expanded so just ignore them for now.
3313 ++MacroDepth;
3314 }
Eli Bendersky17233942013-01-15 22:59:42 +00003315 }
3316
3317 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003318 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003319 }
3320
Jim Grosbach4b905842013-09-20 23:08:21 +00003321 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003322 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3323 }
3324
3325 const char *BodyStart = StartToken.getLoc().getPointer();
3326 const char *BodyEnd = EndToken.getLoc().getPointer();
3327 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003328 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3329 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003330 return false;
3331}
3332
Jim Grosbach4b905842013-09-20 23:08:21 +00003333/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003334///
3335/// With the support added for named parameters there may be code out there that
3336/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003337/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003338/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003339/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003340/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3341/// warning that the positional parameter found in body which have no effect.
3342/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003343/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003344/// intended or change the macro to use the named parameters. It is possible
3345/// this warning will trigger when the none of the named parameters are used
3346/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003347void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003348 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003349 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003350 // If this macro is not defined with named parameters the warning we are
3351 // checking for here doesn't apply.
3352 unsigned NParameters = Parameters.size();
3353 if (NParameters == 0)
3354 return;
3355
3356 bool NamedParametersFound = false;
3357 bool PositionalParametersFound = false;
3358
3359 // Look at the body of the macro for use of both the named parameters and what
3360 // are likely to be positional parameters. This is what expandMacro() is
3361 // doing when it finds the parameters in the body.
3362 while (!Body.empty()) {
3363 // Scan for the next possible parameter.
3364 std::size_t End = Body.size(), Pos = 0;
3365 for (; Pos != End; ++Pos) {
3366 // Check for a substitution or escape.
3367 // This macro is defined with parameters, look for \foo, \bar, etc.
3368 if (Body[Pos] == '\\' && Pos + 1 != End)
3369 break;
3370
3371 // This macro should have parameters, but look for $0, $1, ..., $n too.
3372 if (Body[Pos] != '$' || Pos + 1 == End)
3373 continue;
3374 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003375 if (Next == '$' || Next == 'n' ||
3376 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003377 break;
3378 }
3379
3380 // Check if we reached the end.
3381 if (Pos == End)
3382 break;
3383
3384 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003385 switch (Body[Pos + 1]) {
3386 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003387 case '$':
3388 break;
3389
Jim Grosbach4b905842013-09-20 23:08:21 +00003390 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003391 case 'n':
3392 PositionalParametersFound = true;
3393 break;
3394
Jim Grosbach4b905842013-09-20 23:08:21 +00003395 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003396 default: {
3397 PositionalParametersFound = true;
3398 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003399 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003400 }
3401 Pos += 2;
3402 } else {
3403 unsigned I = Pos + 1;
3404 while (isIdentifierChar(Body[I]) && I + 1 != End)
3405 ++I;
3406
Jim Grosbach4b905842013-09-20 23:08:21 +00003407 const char *Begin = Body.data() + Pos + 1;
3408 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003409 unsigned Index = 0;
3410 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003411 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003412 break;
3413
3414 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003415 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3416 Pos += 3;
3417 else {
3418 Pos = I;
3419 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003420 } else {
3421 NamedParametersFound = true;
3422 Pos += 1 + Argument.size();
3423 }
3424 }
3425 // Update the scan point.
3426 Body = Body.substr(Pos);
3427 }
3428
3429 if (!NamedParametersFound && PositionalParametersFound)
3430 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3431 "used in macro body, possible positional parameter "
3432 "found in body which will have no effect");
3433}
3434
Jim Grosbach4b905842013-09-20 23:08:21 +00003435/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003436/// ::= .endm
3437/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003438bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003439 if (getLexer().isNot(AsmToken::EndOfStatement))
3440 return TokError("unexpected token in '" + Directive + "' directive");
3441
3442 // If we are inside a macro instantiation, terminate the current
3443 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003444 if (isInsideMacroInstantiation()) {
3445 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003446 return false;
3447 }
3448
3449 // Otherwise, this .endmacro is a stray entry in the file; well formed
3450 // .endmacro directives are handled during the macro definition parsing.
3451 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003452 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003453}
3454
Jim Grosbach4b905842013-09-20 23:08:21 +00003455/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003456/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003457bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003458 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003459 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003460 return TokError("expected identifier in '.purgem' directive");
3461
3462 if (getLexer().isNot(AsmToken::EndOfStatement))
3463 return TokError("unexpected token in '.purgem' directive");
3464
Jim Grosbach4b905842013-09-20 23:08:21 +00003465 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003466 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3467
Jim Grosbach4b905842013-09-20 23:08:21 +00003468 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003469 return false;
3470}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003471
Jim Grosbach4b905842013-09-20 23:08:21 +00003472/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003473/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003474bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003475 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003476
3477 // Expect a single argument: an expression that evaluates to a constant
3478 // in the inclusive range 0-30.
3479 SMLoc ExprLoc = getLexer().getLoc();
3480 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003481 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003482 return true;
3483 else if (getLexer().isNot(AsmToken::EndOfStatement))
3484 return TokError("unexpected token after expression in"
3485 " '.bundle_align_mode' directive");
3486 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3487 return Error(ExprLoc,
3488 "invalid bundle alignment size (expected between 0 and 30)");
3489
3490 Lex();
3491
3492 // Because of AlignSizePow2's verified range we can safely truncate it to
3493 // unsigned.
3494 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3495 return false;
3496}
3497
Jim Grosbach4b905842013-09-20 23:08:21 +00003498/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003499/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003500bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003501 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003502 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003503
Eli Bendersky802b6282013-01-07 21:51:08 +00003504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3505 StringRef Option;
3506 SMLoc Loc = getTok().getLoc();
3507 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003508 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003509
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003510 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003511 return Error(Loc, kInvalidOptionError);
3512
3513 if (Option != "align_to_end")
3514 return Error(Loc, kInvalidOptionError);
3515 else if (getLexer().isNot(AsmToken::EndOfStatement))
3516 return Error(Loc,
3517 "unexpected token after '.bundle_lock' directive option");
3518 AlignToEnd = true;
3519 }
3520
Eli Benderskyf483ff92012-12-20 19:05:53 +00003521 Lex();
3522
Eli Bendersky802b6282013-01-07 21:51:08 +00003523 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003524 return false;
3525}
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003528/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003529bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003530 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003531
3532 if (getLexer().isNot(AsmToken::EndOfStatement))
3533 return TokError("unexpected token in '.bundle_unlock' directive");
3534 Lex();
3535
3536 getStreamer().EmitBundleUnlock();
3537 return false;
3538}
3539
Jim Grosbach4b905842013-09-20 23:08:21 +00003540/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003541/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003542bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003543 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003544
3545 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003546 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003547 return true;
3548
3549 int64_t FillExpr = 0;
3550 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3551 if (getLexer().isNot(AsmToken::Comma))
3552 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3553 Lex();
3554
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003555 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003556 return true;
3557
3558 if (getLexer().isNot(AsmToken::EndOfStatement))
3559 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3560 }
3561
3562 Lex();
3563
3564 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003565 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3566 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003567
3568 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003569 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003570
3571 return false;
3572}
3573
Jim Grosbach4b905842013-09-20 23:08:21 +00003574/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003575/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003576bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003577 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003578 const MCExpr *Value;
3579
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003580 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003581 return true;
3582
3583 if (getLexer().isNot(AsmToken::EndOfStatement))
3584 return TokError("unexpected token in directive");
3585
3586 if (Signed)
3587 getStreamer().EmitSLEB128Value(Value);
3588 else
3589 getStreamer().EmitULEB128Value(Value);
3590
3591 return false;
3592}
3593
Jim Grosbach4b905842013-09-20 23:08:21 +00003594/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003595/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003596bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003597 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003598 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003599 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003600 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003601
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003602 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003603 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003604
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003605 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003606
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003607 // Assembler local symbols don't make any sense here. Complain loudly.
3608 if (Sym->isTemporary())
3609 return Error(Loc, "non-local symbol required in directive");
3610
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003611 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3612 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003613
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003614 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003615 break;
3616
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003617 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003618 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003619 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003620 }
3621 }
3622
Sean Callanan686ed8d2010-01-19 20:22:31 +00003623 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003624 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003625}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003626
Jim Grosbach4b905842013-09-20 23:08:21 +00003627/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003628/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003629bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003630 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003631
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003632 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003633 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003634 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003635 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003636
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003637 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003638 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003639
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003640 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003641 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003642 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003643
3644 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003645 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003646 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003647 return true;
3648
3649 int64_t Pow2Alignment = 0;
3650 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003651 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003652 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003653 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003654 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003655 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003656
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003657 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3658 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003659 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3660
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003661 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003662 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3663 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003664 if (!isPowerOf2_64(Pow2Alignment))
3665 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3666 Pow2Alignment = Log2_64(Pow2Alignment);
3667 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003668 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003669
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003670 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003671 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003672
Sean Callanan686ed8d2010-01-19 20:22:31 +00003673 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003674
Chris Lattner28ad7542009-07-09 17:25:12 +00003675 // NOTE: a size of zero for a .comm should create a undefined symbol
3676 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003677 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003678 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003679 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003680
Eric Christopherbc818852010-05-14 01:38:54 +00003681 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003682 // may internally end up wanting an alignment in bytes.
3683 // FIXME: Diagnose overflow.
3684 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003685 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003686 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003687
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003688 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003689 return Error(IDLoc, "invalid symbol redefinition");
3690
Chris Lattner28ad7542009-07-09 17:25:12 +00003691 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003692 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003693 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003694 return false;
3695 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003696
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003697 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003698 return false;
3699}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003700
Jim Grosbach4b905842013-09-20 23:08:21 +00003701/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003702/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003703bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003704 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003705 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003706
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003707 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003708 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003709 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003710
Sean Callanan686ed8d2010-01-19 20:22:31 +00003711 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003712
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003713 if (Str.empty())
3714 Error(Loc, ".abort detected. Assembly stopping.");
3715 else
3716 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003717 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003718
3719 return false;
3720}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003721
Jim Grosbach4b905842013-09-20 23:08:21 +00003722/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003723/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003724bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003725 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003726 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003727
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003728 // Allow the strings to have escaped octal character sequence.
3729 std::string Filename;
3730 if (parseEscapedString(Filename))
3731 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003732 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003733 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003734
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003735 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003736 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003737
Chris Lattner693fbb82009-07-16 06:14:39 +00003738 // Attempt to switch the lexer to the included file before consuming the end
3739 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003740 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003741 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003742 return true;
3743 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003744
3745 return false;
3746}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003747
Jim Grosbach4b905842013-09-20 23:08:21 +00003748/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003749/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003750bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003751 if (getLexer().isNot(AsmToken::String))
3752 return TokError("expected string in '.incbin' directive");
3753
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003754 // Allow the strings to have escaped octal character sequence.
3755 std::string Filename;
3756 if (parseEscapedString(Filename))
3757 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003758 SMLoc IncbinLoc = getLexer().getLoc();
3759 Lex();
3760
3761 if (getLexer().isNot(AsmToken::EndOfStatement))
3762 return TokError("unexpected token in '.incbin' directive");
3763
Kevin Enderby109f25c2011-12-14 21:47:48 +00003764 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003765 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003766 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3767 return true;
3768 }
3769
3770 return false;
3771}
3772
Jim Grosbach4b905842013-09-20 23:08:21 +00003773/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003774/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003775/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003776bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777 TheCondStack.push_back(TheCondState);
3778 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003779 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003780 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003781 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003782 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003783 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003784 return true;
3785
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003786 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003787 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003788
Sean Callanan686ed8d2010-01-19 20:22:31 +00003789 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003790
3791 TheCondState.CondMet = ExprValue;
3792 TheCondState.Ignore = !TheCondState.CondMet;
3793 }
3794
3795 return false;
3796}
3797
Jim Grosbach4b905842013-09-20 23:08:21 +00003798/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003799/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003800bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003801 TheCondStack.push_back(TheCondState);
3802 TheCondState.TheCond = AsmCond::IfCond;
3803
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003804 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003805 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003806 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003807 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003808
3809 if (getLexer().isNot(AsmToken::EndOfStatement))
3810 return TokError("unexpected token in '.ifb' directive");
3811
3812 Lex();
3813
3814 TheCondState.CondMet = ExpectBlank == Str.empty();
3815 TheCondState.Ignore = !TheCondState.CondMet;
3816 }
3817
3818 return false;
3819}
3820
Jim Grosbach4b905842013-09-20 23:08:21 +00003821/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003822/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003823/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003824bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003825 TheCondStack.push_back(TheCondState);
3826 TheCondState.TheCond = AsmCond::IfCond;
3827
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003828 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003829 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003830 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003831 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003832
3833 if (getLexer().isNot(AsmToken::Comma))
3834 return TokError("unexpected token in '.ifc' directive");
3835
3836 Lex();
3837
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003838 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003839
3840 if (getLexer().isNot(AsmToken::EndOfStatement))
3841 return TokError("unexpected token in '.ifc' directive");
3842
3843 Lex();
3844
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003845 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003846 TheCondState.Ignore = !TheCondState.CondMet;
3847 }
3848
3849 return false;
3850}
3851
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003852/// parseDirectiveIfeqs
3853/// ::= .ifeqs string1, string2
3854bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3855 if (Lexer.isNot(AsmToken::String)) {
3856 TokError("expected string parameter for '.ifeqs' directive");
3857 eatToEndOfStatement();
3858 return true;
3859 }
3860
3861 StringRef String1 = getTok().getStringContents();
3862 Lex();
3863
3864 if (Lexer.isNot(AsmToken::Comma)) {
3865 TokError("expected comma after first string for '.ifeqs' directive");
3866 eatToEndOfStatement();
3867 return true;
3868 }
3869
3870 Lex();
3871
3872 if (Lexer.isNot(AsmToken::String)) {
3873 TokError("expected string parameter for '.ifeqs' directive");
3874 eatToEndOfStatement();
3875 return true;
3876 }
3877
3878 StringRef String2 = getTok().getStringContents();
3879 Lex();
3880
3881 TheCondStack.push_back(TheCondState);
3882 TheCondState.TheCond = AsmCond::IfCond;
3883 TheCondState.CondMet = String1 == String2;
3884 TheCondState.Ignore = !TheCondState.CondMet;
3885
3886 return false;
3887}
3888
Jim Grosbach4b905842013-09-20 23:08:21 +00003889/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003890/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003891bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003892 StringRef Name;
3893 TheCondStack.push_back(TheCondState);
3894 TheCondState.TheCond = AsmCond::IfCond;
3895
3896 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003897 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003898 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003899 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003900 return TokError("expected identifier after '.ifdef'");
3901
3902 Lex();
3903
3904 MCSymbol *Sym = getContext().LookupSymbol(Name);
3905
3906 if (expect_defined)
3907 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3908 else
3909 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3910 TheCondState.Ignore = !TheCondState.CondMet;
3911 }
3912
3913 return false;
3914}
3915
Jim Grosbach4b905842013-09-20 23:08:21 +00003916/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003917/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003918bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003919 if (TheCondState.TheCond != AsmCond::IfCond &&
3920 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003921 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3922 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003923 TheCondState.TheCond = AsmCond::ElseIfCond;
3924
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003925 bool LastIgnoreState = false;
3926 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003927 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003928 if (LastIgnoreState || TheCondState.CondMet) {
3929 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003930 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003931 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003932 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003933 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003934 return true;
3935
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003936 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003937 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003938
Sean Callanan686ed8d2010-01-19 20:22:31 +00003939 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003940 TheCondState.CondMet = ExprValue;
3941 TheCondState.Ignore = !TheCondState.CondMet;
3942 }
3943
3944 return false;
3945}
3946
Jim Grosbach4b905842013-09-20 23:08:21 +00003947/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003948/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003949bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003950 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003951 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003952
Sean Callanan686ed8d2010-01-19 20:22:31 +00003953 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003954
3955 if (TheCondState.TheCond != AsmCond::IfCond &&
3956 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003957 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3958 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003959 TheCondState.TheCond = AsmCond::ElseCond;
3960 bool LastIgnoreState = false;
3961 if (!TheCondStack.empty())
3962 LastIgnoreState = TheCondStack.back().Ignore;
3963 if (LastIgnoreState || TheCondState.CondMet)
3964 TheCondState.Ignore = true;
3965 else
3966 TheCondState.Ignore = false;
3967
3968 return false;
3969}
3970
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003971/// parseDirectiveEnd
3972/// ::= .end
3973bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3974 if (getLexer().isNot(AsmToken::EndOfStatement))
3975 return TokError("unexpected token in '.end' directive");
3976
3977 Lex();
3978
3979 while (Lexer.isNot(AsmToken::Eof))
3980 Lex();
3981
3982 return false;
3983}
3984
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003985/// parseDirectiveError
3986/// ::= .err
3987/// ::= .error [string]
3988bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
3989 if (!TheCondStack.empty()) {
3990 if (TheCondStack.back().Ignore) {
3991 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00003992 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003993 }
3994 }
3995
3996 if (!WithMessage)
3997 return Error(L, ".err encountered");
3998
3999 StringRef Message = ".error directive invoked in source file";
4000 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4001 if (Lexer.isNot(AsmToken::String)) {
4002 TokError(".error argument must be a string");
4003 eatToEndOfStatement();
4004 return true;
4005 }
4006
4007 Message = getTok().getStringContents();
4008 Lex();
4009 }
4010
4011 Error(L, Message);
4012 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004013}
4014
Jim Grosbach4b905842013-09-20 23:08:21 +00004015/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004017bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004018 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004019 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004020
Sean Callanan686ed8d2010-01-19 20:22:31 +00004021 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022
Jim Grosbach4b905842013-09-20 23:08:21 +00004023 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004024 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4025 ".else");
4026 if (!TheCondStack.empty()) {
4027 TheCondState = TheCondStack.back();
4028 TheCondStack.pop_back();
4029 }
4030
4031 return false;
4032}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004033
Eli Bendersky17233942013-01-15 22:59:42 +00004034void AsmParser::initializeDirectiveKindMap() {
4035 DirectiveKindMap[".set"] = DK_SET;
4036 DirectiveKindMap[".equ"] = DK_EQU;
4037 DirectiveKindMap[".equiv"] = DK_EQUIV;
4038 DirectiveKindMap[".ascii"] = DK_ASCII;
4039 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4040 DirectiveKindMap[".string"] = DK_STRING;
4041 DirectiveKindMap[".byte"] = DK_BYTE;
4042 DirectiveKindMap[".short"] = DK_SHORT;
4043 DirectiveKindMap[".value"] = DK_VALUE;
4044 DirectiveKindMap[".2byte"] = DK_2BYTE;
4045 DirectiveKindMap[".long"] = DK_LONG;
4046 DirectiveKindMap[".int"] = DK_INT;
4047 DirectiveKindMap[".4byte"] = DK_4BYTE;
4048 DirectiveKindMap[".quad"] = DK_QUAD;
4049 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004050 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004051 DirectiveKindMap[".single"] = DK_SINGLE;
4052 DirectiveKindMap[".float"] = DK_FLOAT;
4053 DirectiveKindMap[".double"] = DK_DOUBLE;
4054 DirectiveKindMap[".align"] = DK_ALIGN;
4055 DirectiveKindMap[".align32"] = DK_ALIGN32;
4056 DirectiveKindMap[".balign"] = DK_BALIGN;
4057 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4058 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4059 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4060 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4061 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4062 DirectiveKindMap[".org"] = DK_ORG;
4063 DirectiveKindMap[".fill"] = DK_FILL;
4064 DirectiveKindMap[".zero"] = DK_ZERO;
4065 DirectiveKindMap[".extern"] = DK_EXTERN;
4066 DirectiveKindMap[".globl"] = DK_GLOBL;
4067 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004068 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4069 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4070 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4071 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4072 DirectiveKindMap[".reference"] = DK_REFERENCE;
4073 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4074 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4075 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4076 DirectiveKindMap[".comm"] = DK_COMM;
4077 DirectiveKindMap[".common"] = DK_COMMON;
4078 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4079 DirectiveKindMap[".abort"] = DK_ABORT;
4080 DirectiveKindMap[".include"] = DK_INCLUDE;
4081 DirectiveKindMap[".incbin"] = DK_INCBIN;
4082 DirectiveKindMap[".code16"] = DK_CODE16;
4083 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4084 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004085 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004086 DirectiveKindMap[".irp"] = DK_IRP;
4087 DirectiveKindMap[".irpc"] = DK_IRPC;
4088 DirectiveKindMap[".endr"] = DK_ENDR;
4089 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4090 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4091 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4092 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004093 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004094 DirectiveKindMap[".ifb"] = DK_IFB;
4095 DirectiveKindMap[".ifnb"] = DK_IFNB;
4096 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004097 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004098 DirectiveKindMap[".ifnc"] = DK_IFNC;
4099 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4100 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4101 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4102 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4103 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004104 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004105 DirectiveKindMap[".endif"] = DK_ENDIF;
4106 DirectiveKindMap[".skip"] = DK_SKIP;
4107 DirectiveKindMap[".space"] = DK_SPACE;
4108 DirectiveKindMap[".file"] = DK_FILE;
4109 DirectiveKindMap[".line"] = DK_LINE;
4110 DirectiveKindMap[".loc"] = DK_LOC;
4111 DirectiveKindMap[".stabs"] = DK_STABS;
4112 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4113 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4114 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4115 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4116 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4117 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4118 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4119 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4120 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4121 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4122 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4123 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4124 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4125 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4126 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4127 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4128 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4129 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4130 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4131 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4132 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004133 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004134 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4135 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4136 DirectiveKindMap[".macro"] = DK_MACRO;
4137 DirectiveKindMap[".endm"] = DK_ENDM;
4138 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4139 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004140 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004141 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004142}
4143
Jim Grosbach4b905842013-09-20 23:08:21 +00004144MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004145 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004146
Rafael Espindola34b9c512012-06-03 23:57:14 +00004147 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004148 for (;;) {
4149 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004150 if (getLexer().is(AsmToken::Eof)) {
4151 Error(DirectiveLoc, "no matching '.endr' in definition");
4152 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004153 }
4154
Rafael Espindola34b9c512012-06-03 23:57:14 +00004155 if (Lexer.is(AsmToken::Identifier) &&
4156 (getTok().getIdentifier() == ".rept")) {
4157 ++NestLevel;
4158 }
4159
4160 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004161 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004162 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004163 EndToken = getTok();
4164 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004165 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4166 TokError("unexpected token in '.endr' directive");
4167 return 0;
4168 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004169 break;
4170 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004171 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004172 }
4173
Rafael Espindola34b9c512012-06-03 23:57:14 +00004174 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004175 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004176 }
4177
4178 const char *BodyStart = StartToken.getLoc().getPointer();
4179 const char *BodyEnd = EndToken.getLoc().getPointer();
4180 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4181
Rafael Espindola34b9c512012-06-03 23:57:14 +00004182 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004183 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004184 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004185}
4186
Jim Grosbach4b905842013-09-20 23:08:21 +00004187void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004188 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004189 OS << ".endr\n";
4190
4191 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004192 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004193
Rafael Espindola34b9c512012-06-03 23:57:14 +00004194 // Create the macro instantiation object and add to the current macro
4195 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004196 MacroInstantiation *MI = new MacroInstantiation(
4197 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004198 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004199
Rafael Espindola34b9c512012-06-03 23:57:14 +00004200 // Jump to the macro instantiation and prime the lexer.
4201 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4202 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4203 Lex();
4204}
4205
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004206/// parseDirectiveRept
4207/// ::= .rep | .rept count
4208bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004209 const MCExpr *CountExpr;
4210 SMLoc CountLoc = getTok().getLoc();
4211 if (parseExpression(CountExpr))
4212 return true;
4213
Rafael Espindola34b9c512012-06-03 23:57:14 +00004214 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004215 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4216 eatToEndOfStatement();
4217 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4218 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004219
4220 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004221 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004222
4223 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004224 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004225
4226 // Eat the end of statement.
4227 Lex();
4228
4229 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004230 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004231 if (!M)
4232 return true;
4233
4234 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4235 // to hold the macro body with substitutions.
4236 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004237 raw_svector_ostream OS(Buf);
4238 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004239 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004240 return true;
4241 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004242 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004243
4244 return false;
4245}
4246
Jim Grosbach4b905842013-09-20 23:08:21 +00004247/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004248/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004249bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004250 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004251
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004252 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004253 return TokError("expected identifier in '.irp' directive");
4254
Rafael Espindola768b41c2012-06-15 14:02:34 +00004255 if (Lexer.isNot(AsmToken::Comma))
4256 return TokError("expected comma in '.irp' directive");
4257
4258 Lex();
4259
Eli Bendersky38274122013-01-14 23:22:36 +00004260 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004261 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004262 return true;
4263
4264 // Eat the end of statement.
4265 Lex();
4266
4267 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004268 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004269 if (!M)
4270 return true;
4271
4272 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4273 // to hold the macro body with substitutions.
4274 SmallString<256> Buf;
4275 raw_svector_ostream OS(Buf);
4276
Eli Bendersky38274122013-01-14 23:22:36 +00004277 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004278 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004279 return true;
4280 }
4281
Jim Grosbach4b905842013-09-20 23:08:21 +00004282 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004283
4284 return false;
4285}
4286
Jim Grosbach4b905842013-09-20 23:08:21 +00004287/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004288/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004289bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004290 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004291
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004292 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004293 return TokError("expected identifier in '.irpc' directive");
4294
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004295 if (Lexer.isNot(AsmToken::Comma))
4296 return TokError("expected comma in '.irpc' directive");
4297
4298 Lex();
4299
Eli Bendersky38274122013-01-14 23:22:36 +00004300 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004301 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004302 return true;
4303
4304 if (A.size() != 1 || A.front().size() != 1)
4305 return TokError("unexpected token in '.irpc' directive");
4306
4307 // Eat the end of statement.
4308 Lex();
4309
4310 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004311 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004312 if (!M)
4313 return true;
4314
4315 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4316 // to hold the macro body with substitutions.
4317 SmallString<256> Buf;
4318 raw_svector_ostream OS(Buf);
4319
4320 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004321 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004322 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004323 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004324
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004325 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004326 return true;
4327 }
4328
Jim Grosbach4b905842013-09-20 23:08:21 +00004329 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004330
4331 return false;
4332}
4333
Jim Grosbach4b905842013-09-20 23:08:21 +00004334bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004335 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004336 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004337
4338 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004339 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004340 assert(getLexer().is(AsmToken::EndOfStatement));
4341
Jim Grosbach4b905842013-09-20 23:08:21 +00004342 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004343 return false;
4344}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004345
Jim Grosbach4b905842013-09-20 23:08:21 +00004346bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004347 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004348 const MCExpr *Value;
4349 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004350 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004351 return true;
4352 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4353 if (!MCE)
4354 return Error(ExprLoc, "unexpected expression in _emit");
4355 uint64_t IntValue = MCE->getValue();
4356 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4357 return Error(ExprLoc, "literal value out of range for directive");
4358
Chad Rosierc7f552c2013-02-12 21:33:51 +00004359 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4360 return false;
4361}
4362
Jim Grosbach4b905842013-09-20 23:08:21 +00004363bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004364 const MCExpr *Value;
4365 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004366 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004367 return true;
4368 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4369 if (!MCE)
4370 return Error(ExprLoc, "unexpected expression in align");
4371 uint64_t IntValue = MCE->getValue();
4372 if (!isPowerOf2_64(IntValue))
4373 return Error(ExprLoc, "literal value not a power of two greater then zero");
4374
Jim Grosbach4b905842013-09-20 23:08:21 +00004375 Info.AsmRewrites->push_back(
4376 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004377 return false;
4378}
4379
Chad Rosierf43fcf52013-02-13 21:27:17 +00004380// We are comparing pointers, but the pointers are relative to a single string.
4381// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004382static int rewritesSort(const AsmRewrite *AsmRewriteA,
4383 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004384 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4385 return -1;
4386 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4387 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004388
Chad Rosierfce4fab2013-04-08 17:43:47 +00004389 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4390 // rewrite to the same location. Make sure the SizeDirective rewrite is
4391 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4392 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004393 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4394 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004395 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004396
Jim Grosbach4b905842013-09-20 23:08:21 +00004397 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4398 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004399 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004400 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004401}
4402
Jim Grosbach4b905842013-09-20 23:08:21 +00004403bool AsmParser::parseMSInlineAsm(
4404 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4405 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4406 SmallVectorImpl<std::string> &Constraints,
4407 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4408 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004409 SmallVector<void *, 4> InputDecls;
4410 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004411 SmallVector<bool, 4> InputDeclsAddressOf;
4412 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004413 SmallVector<std::string, 4> InputConstraints;
4414 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004415 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004416
Benjamin Kramer1a136112013-02-15 20:37:21 +00004417 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004418
4419 // Prime the lexer.
4420 Lex();
4421
4422 // While we have input, parse each statement.
4423 unsigned InputIdx = 0;
4424 unsigned OutputIdx = 0;
4425 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004426 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004427 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004428 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004429
Chad Rosier149e8e02012-12-12 22:45:52 +00004430 if (Info.ParseError)
4431 return true;
4432
Benjamin Kramer1a136112013-02-15 20:37:21 +00004433 if (Info.Opcode == ~0U)
4434 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004435
Benjamin Kramer1a136112013-02-15 20:37:21 +00004436 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004437
Benjamin Kramer1a136112013-02-15 20:37:21 +00004438 // Build the list of clobbers, outputs and inputs.
4439 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4440 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004441
Benjamin Kramer1a136112013-02-15 20:37:21 +00004442 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004443 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004444 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004445
Benjamin Kramer1a136112013-02-15 20:37:21 +00004446 // Register operand.
4447 if (Operand->isReg() && !Operand->needAddressOf()) {
4448 unsigned NumDefs = Desc.getNumDefs();
4449 // Clobber.
4450 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4451 ClobberRegs.push_back(Operand->getReg());
4452 continue;
4453 }
4454
4455 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004456 StringRef SymName = Operand->getSymName();
4457 if (SymName.empty())
4458 continue;
4459
Chad Rosierdba3fe52013-04-22 22:12:12 +00004460 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004461 if (!OpDecl)
4462 continue;
4463
4464 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004465 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004466 if (isOutput) {
4467 ++InputIdx;
4468 OutputDecls.push_back(OpDecl);
4469 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4470 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004471 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004472 } else {
4473 InputDecls.push_back(OpDecl);
4474 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4475 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004476 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004477 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004478 }
Reid Kleckneree088972013-12-10 18:27:32 +00004479
4480 // Consider implicit defs to be clobbers. Think of cpuid and push.
4481 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4482 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4483 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004484 }
4485
4486 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004487 NumOutputs = OutputDecls.size();
4488 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004489
4490 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004491 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4492 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4493 ClobberRegs.end());
4494 Clobbers.assign(ClobberRegs.size(), std::string());
4495 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4496 raw_string_ostream OS(Clobbers[I]);
4497 IP->printRegName(OS, ClobberRegs[I]);
4498 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004499
4500 // Merge the various outputs and inputs. Output are expected first.
4501 if (NumOutputs || NumInputs) {
4502 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004503 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004504 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004505 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004506 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004507 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004508 }
4509 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004510 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004511 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004512 }
4513 }
4514
4515 // Build the IR assembly string.
4516 std::string AsmStringIR;
4517 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004518 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4519 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004520 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004521 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4522 E = AsmStrRewrites.end();
4523 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004524 AsmRewriteKind Kind = (*I).Kind;
4525 if (Kind == AOK_Delete)
4526 continue;
4527
Chad Rosier8bce6642012-10-18 15:49:34 +00004528 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004529 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004530
Chad Rosier120eefd2013-03-19 17:32:17 +00004531 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004532 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004533 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004534 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004535
Chad Rosier37e755c2012-10-23 17:43:43 +00004536 // Skip the original expression.
4537 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004538 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004539 continue;
4540 }
4541
Chad Rosierff10ed12013-04-12 16:26:42 +00004542 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004543 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004544 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004545 default:
4546 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004547 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004548 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004549 break;
4550 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004551 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004552 break;
4553 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004554 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004555 break;
4556 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004557 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004558 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004559 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004560 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004561 default: break;
4562 case 8: OS << "byte ptr "; break;
4563 case 16: OS << "word ptr "; break;
4564 case 32: OS << "dword ptr "; break;
4565 case 64: OS << "qword ptr "; break;
4566 case 80: OS << "xword ptr "; break;
4567 case 128: OS << "xmmword ptr "; break;
4568 case 256: OS << "ymmword ptr "; break;
4569 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004570 break;
4571 case AOK_Emit:
4572 OS << ".byte";
4573 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004574 case AOK_Align: {
4575 unsigned Val = (*I).Val;
4576 OS << ".align " << Val;
4577
4578 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004579 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004580 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4581 break;
4582 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004583 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004584 // Insert the dot if the user omitted it.
4585 OS.flush();
4586 if (AsmStringIR.back() != '.')
4587 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004588 OS << (*I).Val;
4589 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004590 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004591
Chad Rosier8bce6642012-10-18 15:49:34 +00004592 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004593 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004594 }
4595
4596 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004597 if (AsmStart != AsmEnd)
4598 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004599
4600 AsmString = OS.str();
4601 return false;
4602}
4603
Daniel Dunbar01e36072010-07-17 02:26:10 +00004604/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004605MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4606 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004607 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004608}