blob: db448ae8be2cb0137c3ccfa9297f4f5d7e9c7e49 [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);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000643 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000644 StringRef(),
645 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000646 }
647
Chris Lattner73f36112009-07-02 21:53:43 +0000648 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000649 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000650 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000651 if (!parseStatement(Info))
652 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000653
Daniel Dunbar43325c42010-09-09 22:42:56 +0000654 // We had an error, validate that one was emitted and recover by skipping to
655 // the next line.
656 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000657 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000658 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000659
660 if (TheCondState.TheCond != StartingCondState.TheCond ||
661 TheCondState.Ignore != StartingCondState.Ignore)
662 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000663
664 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000665 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000666 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000667 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000668 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000670 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000671
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000672 // Check to see that all assembler local symbols were actually defined.
673 // Targets that don't do subsections via symbols may not want this, though,
674 // so conservatively exclude them. Only do this if we're finalizing, though,
675 // as otherwise we won't necessarilly have seen everything yet.
676 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
677 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
678 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000679 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000680 i != e; ++i) {
681 MCSymbol *Sym = i->getValue();
682 // Variable symbols may not be marked as defined, so check those
683 // explicitly. If we know it's a variable, we have a definition for
684 // the purposes of this check.
685 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
686 // FIXME: We would really like to refer back to where the symbol was
687 // first referenced for a source location. We need to add something
688 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000689 printMessage(
690 getLexer().getLoc(), SourceMgr::DK_Error,
691 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000692 }
693 }
694
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000695 // Finalize the output stream if there are no errors and if the client wants
696 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000697 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000698 Out.Finish();
699
Chris Lattner73f36112009-07-02 21:53:43 +0000700 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000701}
702
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000703void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000704 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000705 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000706 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000707 }
708}
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000712 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000714
Chris Lattnere5074c42009-06-22 01:29:09 +0000715 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000716 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000718}
719
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000720StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 const char *Start = getTok().getLoc().getPointer();
722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000724 Lex();
725
726 const char *End = getTok().getLoc().getPointer();
727 return StringRef(Start, End - Start);
728}
Chris Lattner78db3622009-06-22 05:51:26 +0000729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 const char *Start = getTok().getLoc().getPointer();
732
733 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000734 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 Lex();
736
737 const char *End = getTok().getLoc().getPointer();
738 return StringRef(Start, End - Start);
739}
740
Jim Grosbach4b905842013-09-20 23:08:21 +0000741/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742/// NOTE: This assumes the leading '(' has already been consumed.
743///
744/// parenexpr ::= expr)
745///
Jim Grosbach4b905842013-09-20 23:08:21 +0000746bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
747 if (parseExpression(Res))
748 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000749 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000751 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000752 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753 return false;
754}
Chris Lattner78db3622009-06-22 05:51:26 +0000755
Jim Grosbach4b905842013-09-20 23:08:21 +0000756/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757/// NOTE: This assumes the leading '[' has already been consumed.
758///
759/// bracketexpr ::= expr]
760///
Jim Grosbach4b905842013-09-20 23:08:21 +0000761bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
762 if (parseExpression(Res))
763 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 if (Lexer.isNot(AsmToken::RBrac))
765 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000766 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000767 Lex();
768 return false;
769}
770
Jim Grosbach4b905842013-09-20 23:08:21 +0000771/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= (parenexpr
773/// primaryexpr ::= symbol
774/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000775/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000777bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000778 SMLoc FirstTokenLoc = getLexer().getLoc();
779 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
780 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000781 default:
782 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000783 // If we have an error assume that we've already handled it.
784 case AsmToken::Error:
785 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000786 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000787 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000788 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000789 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000790 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000791 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000793 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000794 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000795 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000797 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000798 if (FirstTokenKind == AsmToken::Dollar) {
799 if (Lexer.getMAI().getDollarIsPC()) {
800 // This is a '$' reference, which references the current PC. Emit a
801 // temporary label to the streamer and refer to it.
802 MCSymbol *Sym = Ctx.CreateTempSymbol();
803 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000804 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
805 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 EndLoc = FirstTokenLoc;
807 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000808 }
809 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000811 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000812 // Parse symbol variant
813 std::pair<StringRef, StringRef> Split;
814 if (!MAI.useParensForSymbolVariant()) {
815 Split = Identifier.split('@');
816 } else if (Lexer.is(AsmToken::LParen)) {
817 Lexer.Lex(); // eat (
818 StringRef VName;
819 parseIdentifier(VName);
820 if (Lexer.isNot(AsmToken::RParen)) {
821 return Error(Lexer.getTok().getLoc(),
822 "unexpected token in variant, expected ')'");
823 }
824 Lexer.Lex(); // eat )
825 Split = std::make_pair(Identifier, VName);
826 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000827
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000828 EndLoc = SMLoc::getFromPointer(Identifier.end());
829
Daniel Dunbard20cda02009-10-16 01:34:54 +0000830 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000831 StringRef SymbolName = Identifier;
832 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000833
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000834 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000835 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000836 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000837 if (Variant != MCSymbolRefExpr::VK_Invalid) {
838 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000839 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 Variant = MCSymbolRefExpr::VK_None;
841 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000842 return Error(SMLoc::getFromPointer(Split.second.begin()),
843 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000844 }
845 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000846
Hans Wennborgce69d772013-10-18 20:46:28 +0000847 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
848
Daniel Dunbard20cda02009-10-16 01:34:54 +0000849 // If this is an absolute variable reference, substitute it now to preserve
850 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000851 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000852 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000853 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000854
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000855 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000856 return false;
857 }
858
859 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000860 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000861 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000862 }
David Woodhousef42a6662014-02-01 16:20:54 +0000863 case AsmToken::BigNum:
864 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000865 case AsmToken::Integer: {
866 SMLoc Loc = getTok().getLoc();
867 int64_t IntVal = getTok().getIntVal();
868 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000869 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000870 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000871 // Look for 'b' or 'f' following an Integer as a directional label
872 if (Lexer.getKind() == AsmToken::Identifier) {
873 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000874 // Lookup the symbol variant if used.
875 std::pair<StringRef, StringRef> Split = IDVal.split('@');
876 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
877 if (Split.first.size() != IDVal.size()) {
878 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
879 if (Variant == MCSymbolRefExpr::VK_Invalid) {
880 Variant = MCSymbolRefExpr::VK_None;
881 return TokError("invalid variant '" + Split.second + "'");
882 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000883 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000884 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000885 if (IDVal == "f" || IDVal == "b") {
886 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000887 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000889 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000891 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000892 Lex(); // Eat identifier.
893 }
894 }
Chris Lattner78db3622009-06-22 05:51:26 +0000895 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000896 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 case AsmToken::Real: {
898 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000899 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000900 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000901 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000902 Lex(); // Eat token.
903 return false;
904 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000905 case AsmToken::Dot: {
906 // This is a '.' reference, which references the current PC. Emit a
907 // temporary label to the streamer and refer to it.
908 MCSymbol *Sym = Ctx.CreateTempSymbol();
909 Out.EmitLabel(Sym);
910 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000911 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000912 Lex(); // Eat identifier.
913 return false;
914 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000915 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000916 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000918 case AsmToken::LBrac:
919 if (!PlatformParser->HasBracketExpressions())
920 return TokError("brackets expression not supported on this target");
921 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000922 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000927 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000933 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000941 }
942}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000943
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000944bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000945 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000946 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000947}
948
Daniel Dunbar55f16672010-09-17 02:47:07 +0000949const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000950AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000951 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000952 // Ask the target implementation about this expression first.
953 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
954 if (NewE)
955 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000956 // Recurse over the given expression, rebuilding it to apply the given variant
957 // if there is exactly one symbol.
958 switch (E->getKind()) {
959 case MCExpr::Target:
960 case MCExpr::Constant:
961 return 0;
962
963 case MCExpr::SymbolRef: {
964 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
965
966 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000967 TokError("invalid variant on expression '" + getTok().getIdentifier() +
968 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000969 return E;
970 }
971
972 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
973 }
974
975 case MCExpr::Unary: {
976 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000977 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000978 if (!Sub)
979 return 0;
980 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
981 }
982
983 case MCExpr::Binary: {
984 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000985 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
986 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987
988 if (!LHS && !RHS)
989 return 0;
990
Jim Grosbach4b905842013-09-20 23:08:21 +0000991 if (!LHS)
992 LHS = BE->getLHS();
993 if (!RHS)
994 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995
996 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
997 }
998 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000999
Craig Toppera2886c22012-02-07 05:05:23 +00001000 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001}
1002
Jim Grosbach4b905842013-09-20 23:08:21 +00001003/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001004///
Jim Grosbachbd164242011-08-20 16:24:13 +00001005/// expr ::= expr &&,|| expr -> lowest.
1006/// expr ::= expr |,^,&,! expr
1007/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1008/// expr ::= expr <<,>> expr
1009/// expr ::= expr +,- expr
1010/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001011/// expr ::= primaryexpr
1012///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001013bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001014 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001015 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001016 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001017 return true;
1018
Daniel Dunbar55f16672010-09-17 02:47:07 +00001019 // As a special case, we support 'a op b @ modifier' by rewriting the
1020 // expression to include the modifier. This is inefficient, but in general we
1021 // expect users to use 'a@modifier op b'.
1022 if (Lexer.getKind() == AsmToken::At) {
1023 Lex();
1024
1025 if (Lexer.isNot(AsmToken::Identifier))
1026 return TokError("unexpected symbol modifier following '@'");
1027
1028 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001029 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001030 if (Variant == MCSymbolRefExpr::VK_Invalid)
1031 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1032
Jim Grosbach4b905842013-09-20 23:08:21 +00001033 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 if (!ModifiedRes) {
1035 return TokError("invalid modifier '" + getTok().getIdentifier() +
1036 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001038
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039 Res = ModifiedRes;
1040 Lex();
1041 }
1042
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001043 // Try to constant fold it up front, if possible.
1044 int64_t Value;
1045 if (Res->EvaluateAsAbsolute(Value))
1046 Res = MCConstantExpr::Create(Value, getContext());
1047
1048 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001049}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001050
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001051bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001052 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001053 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001054}
1055
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001056bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001057 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001058
Daniel Dunbar75630b32009-06-30 02:10:03 +00001059 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001060 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001061 return true;
1062
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001063 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001064 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001065
1066 return false;
1067}
1068
Michael J. Spencer530ce852010-10-09 11:00:50 +00001069static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001070 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001071 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001072 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001073 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001074
Jim Grosbach4b905842013-09-20 23:08:21 +00001075 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001076 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001078 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001079 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001080 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001081 return 1;
1082
Jim Grosbach4b905842013-09-20 23:08:21 +00001083 // Low Precedence: |, &, ^
1084 //
1085 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001092 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001095
Jim Grosbach4b905842013-09-20 23:08:21 +00001096 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 case AsmToken::EqualEqual:
1098 Kind = MCBinaryExpr::EQ;
1099 return 3;
1100 case AsmToken::ExclaimEqual:
1101 case AsmToken::LessGreater:
1102 Kind = MCBinaryExpr::NE;
1103 return 3;
1104 case AsmToken::Less:
1105 Kind = MCBinaryExpr::LT;
1106 return 3;
1107 case AsmToken::LessEqual:
1108 Kind = MCBinaryExpr::LTE;
1109 return 3;
1110 case AsmToken::Greater:
1111 Kind = MCBinaryExpr::GT;
1112 return 3;
1113 case AsmToken::GreaterEqual:
1114 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001115 return 3;
1116
Jim Grosbach4b905842013-09-20 23:08:21 +00001117 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001118 case AsmToken::LessLess:
1119 Kind = MCBinaryExpr::Shl;
1120 return 4;
1121 case AsmToken::GreaterGreater:
1122 Kind = MCBinaryExpr::Shr;
1123 return 4;
1124
Jim Grosbach4b905842013-09-20 23:08:21 +00001125 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126 case AsmToken::Plus:
1127 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001129 case AsmToken::Minus:
1130 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001132
Jim Grosbach4b905842013-09-20 23:08:21 +00001133 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001137 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001138 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001140 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001141 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001143 }
1144}
1145
Jim Grosbach4b905842013-09-20 23:08:21 +00001146/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001147/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001148bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001149 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001150 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001151 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001152 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154 // If the next token is lower precedence than we are allowed to eat, return
1155 // successfully with what we ate already.
1156 if (TokPrec < Precedence)
1157 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001158
Sean Callanan686ed8d2010-01-19 20:22:31 +00001159 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001162 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001163 if (parsePrimaryExpr(RHS, EndLoc))
1164 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166 // If BinOp binds less tightly with RHS than the operator after RHS, let
1167 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001168 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001169 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001170 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1171 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001173 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001174 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001175 }
1176}
1177
Chris Lattner36e02122009-06-21 20:54:55 +00001178/// ParseStatement:
1179/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001180/// ::= Label* Directive ...Operands... EndOfStatement
1181/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001182bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001183 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001184 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001185 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001186 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001187 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001188
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001189 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001190 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001191 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001192 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001193 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001194 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001195 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001196 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001197
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001198 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001199 if (Lexer.is(AsmToken::Integer)) {
1200 LocalLabelVal = getTok().getIntVal();
1201 if (LocalLabelVal < 0) {
1202 if (!TheCondState.Ignore)
1203 return TokError("unexpected token at start of statement");
1204 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001205 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 IDVal = getTok().getString();
1207 Lex(); // Consume the integer token to be used as an identifier token.
1208 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001209 if (!TheCondState.Ignore)
1210 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001211 }
1212 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001213 } else if (Lexer.is(AsmToken::Dot)) {
1214 // Treat '.' as a valid identifier in this context.
1215 Lex();
1216 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001217 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001218 if (!TheCondState.Ignore)
1219 return TokError("unexpected token at start of statement");
1220 IDVal = "";
1221 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001222
Chris Lattner926885c2010-04-17 18:14:27 +00001223 // Handle conditional assembly here before checking for skipping. We
1224 // have to do this so that .endif isn't skipped in a ".if 0" block for
1225 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001226 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001227 DirectiveKindMap.find(IDVal);
1228 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1229 ? DK_NO_DIRECTIVE
1230 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001231 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001232 default:
1233 break;
1234 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001235 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001236 return parseDirectiveIf(IDLoc);
1237 case DK_IFB:
1238 return parseDirectiveIfb(IDLoc, true);
1239 case DK_IFNB:
1240 return parseDirectiveIfb(IDLoc, false);
1241 case DK_IFC:
1242 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001243 case DK_IFEQS:
1244 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001245 case DK_IFNC:
1246 return parseDirectiveIfc(IDLoc, false);
1247 case DK_IFDEF:
1248 return parseDirectiveIfdef(IDLoc, true);
1249 case DK_IFNDEF:
1250 case DK_IFNOTDEF:
1251 return parseDirectiveIfdef(IDLoc, false);
1252 case DK_ELSEIF:
1253 return parseDirectiveElseIf(IDLoc);
1254 case DK_ELSE:
1255 return parseDirectiveElse(IDLoc);
1256 case DK_ENDIF:
1257 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001258 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001259
Eli Bendersky88024712013-01-16 19:32:36 +00001260 // Ignore the statement if in the middle of inactive conditional
1261 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001262 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001263 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001264 return false;
1265 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001266
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001267 // FIXME: Recurse on local labels?
1268
1269 // See what kind of statement we have.
1270 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001271 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001272 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001273
Chris Lattner36e02122009-06-21 20:54:55 +00001274 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001275 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001276
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001277 // Diagnose attempt to use '.' as a label.
1278 if (IDVal == ".")
1279 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1280
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001281 // Diagnose attempt to use a variable as a label.
1282 //
1283 // FIXME: Diagnostics. Note the location of the definition as a label.
1284 // FIXME: This doesn't diagnose assignment to a symbol which has been
1285 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001286 MCSymbol *Sym;
1287 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001288 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001289 else
1290 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001291 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001292 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001293
Daniel Dunbare73b2672009-08-26 22:13:22 +00001294 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001295 if (!ParsingInlineAsm)
1296 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001297
Kevin Enderbye7739d42011-12-09 18:09:40 +00001298 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001299 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001300 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001301 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1302 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001303
Tim Northover1744d0a2013-10-25 12:49:50 +00001304 getTargetParser().onLabelParsed(Sym);
1305
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001306 // Consume any end of statement token, if present, to avoid spurious
1307 // AddBlankLine calls().
1308 if (Lexer.is(AsmToken::EndOfStatement)) {
1309 Lex();
1310 if (Lexer.is(AsmToken::Eof))
1311 return false;
1312 }
1313
Eli Friedman0f4871d2012-10-22 23:58:19 +00001314 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001315 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001316
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001317 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001318 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001319 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001320
Jim Grosbach4b905842013-09-20 23:08:21 +00001321 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001322
1323 default: // Normal instruction or directive.
1324 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001325 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001326
1327 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001328 if (areMacrosEnabled())
1329 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1330 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001331 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001332
Michael J. Spencer530ce852010-10-09 11:00:50 +00001333 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001334
Eli Bendersky17233942013-01-15 22:59:42 +00001335 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001336 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001337 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001338 //
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // 1. The target-specific assembly parser. Some directives are target
1340 // specific or may potentially behave differently on certain targets.
1341 // 2. Asm parser extensions. For example, platform-specific parsers
1342 // (like the ELF parser) register themselves as extensions.
1343 // 3. The generic directive parser implemented by this class. These are
1344 // all the directives that behave in a target and platform independent
1345 // manner, or at least have a default behavior that's shared between
1346 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001347
Eli Bendersky17233942013-01-15 22:59:42 +00001348 // First query the target-specific parser. It will return 'true' if it
1349 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001350 if (!getTargetParser().ParseDirective(ID))
1351 return false;
1352
Alp Tokercb402912014-01-24 17:20:08 +00001353 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001355 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1356 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001357 if (Handler.first)
1358 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1359
1360 // Finally, if no one else is interested in this directive, it must be
1361 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001362 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001363 default:
1364 break;
1365 case DK_SET:
1366 case DK_EQU:
1367 return parseDirectiveSet(IDVal, true);
1368 case DK_EQUIV:
1369 return parseDirectiveSet(IDVal, false);
1370 case DK_ASCII:
1371 return parseDirectiveAscii(IDVal, false);
1372 case DK_ASCIZ:
1373 case DK_STRING:
1374 return parseDirectiveAscii(IDVal, true);
1375 case DK_BYTE:
1376 return parseDirectiveValue(1);
1377 case DK_SHORT:
1378 case DK_VALUE:
1379 case DK_2BYTE:
1380 return parseDirectiveValue(2);
1381 case DK_LONG:
1382 case DK_INT:
1383 case DK_4BYTE:
1384 return parseDirectiveValue(4);
1385 case DK_QUAD:
1386 case DK_8BYTE:
1387 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001388 case DK_OCTA:
1389 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001390 case DK_SINGLE:
1391 case DK_FLOAT:
1392 return parseDirectiveRealValue(APFloat::IEEEsingle);
1393 case DK_DOUBLE:
1394 return parseDirectiveRealValue(APFloat::IEEEdouble);
1395 case DK_ALIGN: {
1396 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1397 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1398 }
1399 case DK_ALIGN32: {
1400 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1401 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1402 }
1403 case DK_BALIGN:
1404 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1405 case DK_BALIGNW:
1406 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1407 case DK_BALIGNL:
1408 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1409 case DK_P2ALIGN:
1410 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1411 case DK_P2ALIGNW:
1412 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1413 case DK_P2ALIGNL:
1414 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1415 case DK_ORG:
1416 return parseDirectiveOrg();
1417 case DK_FILL:
1418 return parseDirectiveFill();
1419 case DK_ZERO:
1420 return parseDirectiveZero();
1421 case DK_EXTERN:
1422 eatToEndOfStatement(); // .extern is the default, ignore it.
1423 return false;
1424 case DK_GLOBL:
1425 case DK_GLOBAL:
1426 return parseDirectiveSymbolAttribute(MCSA_Global);
1427 case DK_LAZY_REFERENCE:
1428 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1429 case DK_NO_DEAD_STRIP:
1430 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1431 case DK_SYMBOL_RESOLVER:
1432 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1433 case DK_PRIVATE_EXTERN:
1434 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1435 case DK_REFERENCE:
1436 return parseDirectiveSymbolAttribute(MCSA_Reference);
1437 case DK_WEAK_DEFINITION:
1438 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1439 case DK_WEAK_REFERENCE:
1440 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1441 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1442 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1443 case DK_COMM:
1444 case DK_COMMON:
1445 return parseDirectiveComm(/*IsLocal=*/false);
1446 case DK_LCOMM:
1447 return parseDirectiveComm(/*IsLocal=*/true);
1448 case DK_ABORT:
1449 return parseDirectiveAbort();
1450 case DK_INCLUDE:
1451 return parseDirectiveInclude();
1452 case DK_INCBIN:
1453 return parseDirectiveIncbin();
1454 case DK_CODE16:
1455 case DK_CODE16GCC:
1456 return TokError(Twine(IDVal) + " not supported yet");
1457 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001458 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001459 case DK_IRP:
1460 return parseDirectiveIrp(IDLoc);
1461 case DK_IRPC:
1462 return parseDirectiveIrpc(IDLoc);
1463 case DK_ENDR:
1464 return parseDirectiveEndr(IDLoc);
1465 case DK_BUNDLE_ALIGN_MODE:
1466 return parseDirectiveBundleAlignMode();
1467 case DK_BUNDLE_LOCK:
1468 return parseDirectiveBundleLock();
1469 case DK_BUNDLE_UNLOCK:
1470 return parseDirectiveBundleUnlock();
1471 case DK_SLEB128:
1472 return parseDirectiveLEB128(true);
1473 case DK_ULEB128:
1474 return parseDirectiveLEB128(false);
1475 case DK_SPACE:
1476 case DK_SKIP:
1477 return parseDirectiveSpace(IDVal);
1478 case DK_FILE:
1479 return parseDirectiveFile(IDLoc);
1480 case DK_LINE:
1481 return parseDirectiveLine();
1482 case DK_LOC:
1483 return parseDirectiveLoc();
1484 case DK_STABS:
1485 return parseDirectiveStabs();
1486 case DK_CFI_SECTIONS:
1487 return parseDirectiveCFISections();
1488 case DK_CFI_STARTPROC:
1489 return parseDirectiveCFIStartProc();
1490 case DK_CFI_ENDPROC:
1491 return parseDirectiveCFIEndProc();
1492 case DK_CFI_DEF_CFA:
1493 return parseDirectiveCFIDefCfa(IDLoc);
1494 case DK_CFI_DEF_CFA_OFFSET:
1495 return parseDirectiveCFIDefCfaOffset();
1496 case DK_CFI_ADJUST_CFA_OFFSET:
1497 return parseDirectiveCFIAdjustCfaOffset();
1498 case DK_CFI_DEF_CFA_REGISTER:
1499 return parseDirectiveCFIDefCfaRegister(IDLoc);
1500 case DK_CFI_OFFSET:
1501 return parseDirectiveCFIOffset(IDLoc);
1502 case DK_CFI_REL_OFFSET:
1503 return parseDirectiveCFIRelOffset(IDLoc);
1504 case DK_CFI_PERSONALITY:
1505 return parseDirectiveCFIPersonalityOrLsda(true);
1506 case DK_CFI_LSDA:
1507 return parseDirectiveCFIPersonalityOrLsda(false);
1508 case DK_CFI_REMEMBER_STATE:
1509 return parseDirectiveCFIRememberState();
1510 case DK_CFI_RESTORE_STATE:
1511 return parseDirectiveCFIRestoreState();
1512 case DK_CFI_SAME_VALUE:
1513 return parseDirectiveCFISameValue(IDLoc);
1514 case DK_CFI_RESTORE:
1515 return parseDirectiveCFIRestore(IDLoc);
1516 case DK_CFI_ESCAPE:
1517 return parseDirectiveCFIEscape();
1518 case DK_CFI_SIGNAL_FRAME:
1519 return parseDirectiveCFISignalFrame();
1520 case DK_CFI_UNDEFINED:
1521 return parseDirectiveCFIUndefined(IDLoc);
1522 case DK_CFI_REGISTER:
1523 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001524 case DK_CFI_WINDOW_SAVE:
1525 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001526 case DK_MACROS_ON:
1527 case DK_MACROS_OFF:
1528 return parseDirectiveMacrosOnOff(IDVal);
1529 case DK_MACRO:
1530 return parseDirectiveMacro(IDLoc);
1531 case DK_ENDM:
1532 case DK_ENDMACRO:
1533 return parseDirectiveEndMacro(IDVal);
1534 case DK_PURGEM:
1535 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001536 case DK_END:
1537 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001538 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001539 return parseDirectiveError(IDLoc, false);
1540 case DK_ERROR:
1541 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001542 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001543
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001544 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001545 }
Chris Lattner36e02122009-06-21 20:54:55 +00001546
Chad Rosierc7f552c2013-02-12 21:33:51 +00001547 // __asm _emit or __asm __emit
1548 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1549 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001550 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001551
1552 // __asm align
1553 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001554 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001555
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001556 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001557
Chris Lattner7cbfa442010-05-19 23:34:33 +00001558 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001559 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001560 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001561 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001562 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001563 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001564
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001565 // Dump the parsed representation, if requested.
1566 if (getShowParsedOperands()) {
1567 SmallString<256> Str;
1568 raw_svector_ostream OS(Str);
1569 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001570 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001571 if (i != 0)
1572 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001573 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001574 }
1575 OS << "]";
1576
Jim Grosbach4b905842013-09-20 23:08:21 +00001577 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001578 }
1579
Kevin Enderby6469fc22011-11-01 22:27:22 +00001580 // If we are generating dwarf for assembly source files and the current
1581 // section is the initial text section then generate a .loc directive for
1582 // the instruction.
1583 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001584 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001586
Eli Bendersky88024712013-01-16 19:32:36 +00001587 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001588
Eli Bendersky88024712013-01-16 19:32:36 +00001589 // If we previously parsed a cpp hash file line comment then make sure the
1590 // current Dwarf File is for the CppHashFilename if not then emit the
1591 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001592 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001593 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001594 if (CppHashFilename.size() != 0) {
1595 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001596 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001597 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001598 getContext().nextGenDwarfFileNumber(), StringRef(),
1599 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001600
Jim Grosbach4b905842013-09-20 23:08:21 +00001601 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1602 // cache with the different Loc from the call above we save the last
1603 // info we queried here with SrcMgr.FindLineNumber().
1604 unsigned CppHashLocLineNo;
1605 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1606 CppHashLocLineNo = LastQueryLine;
1607 else {
1608 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1609 LastQueryLine = CppHashLocLineNo;
1610 LastQueryIDLoc = CppHashLoc;
1611 LastQueryBuffer = CppHashBuf;
1612 }
1613 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001614 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001615
Jim Grosbach4b905842013-09-20 23:08:21 +00001616 getStreamer().EmitDwarfLocDirective(
1617 getContext().getGenDwarfFileNumber(), Line, 0,
1618 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1619 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001620 }
1621
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001622 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001623 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001624 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001625 HadError = getTargetParser().MatchAndEmitInstruction(
1626 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1627 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001628 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001629
Chris Lattnera2a9d162010-09-11 16:18:25 +00001630 // Don't skip the rest of the line, the instruction parser is responsible for
1631 // that.
1632 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001633}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001634
Jim Grosbach4b905842013-09-20 23:08:21 +00001635/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001636/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001637void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001638 if (!Lexer.is(AsmToken::EndOfStatement))
1639 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001640 // Eat EOL.
1641 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001642}
1643
Jim Grosbach4b905842013-09-20 23:08:21 +00001644/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001645/// ::= # number "filename"
1646/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001647bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001648 Lex(); // Eat the hash token.
1649
1650 if (getLexer().isNot(AsmToken::Integer)) {
1651 // Consume the line since in cases it is not a well-formed line directive,
1652 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001653 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001654 return false;
1655 }
1656
1657 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001658 Lex();
1659
1660 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001661 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001662 return false;
1663 }
1664
1665 StringRef Filename = getTok().getString();
1666 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001667 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001668
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001669 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1670 CppHashLoc = L;
1671 CppHashFilename = Filename;
1672 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001673 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001674
1675 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001677 return false;
1678}
1679
Jim Grosbach4b905842013-09-20 23:08:21 +00001680/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001681/// for the Filename and LineNo if any in the diagnostic.
1682void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001684 raw_ostream &OS = errs();
1685
1686 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1687 const SMLoc &DiagLoc = Diag.getLoc();
1688 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1689 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1690
Jim Grosbach4b905842013-09-20 23:08:21 +00001691 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001692 // before printing the message.
1693 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001694 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001695 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1696 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001697 }
1698
Eric Christophera7c32732012-12-18 00:30:54 +00001699 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700 // manager changed or buffer changed (like in a nested include) then just
1701 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001702 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001704 if (Parser->SavedDiagHandler)
1705 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1706 else
1707 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708 return;
1709 }
1710
Eric Christophera7c32732012-12-18 00:30:54 +00001711 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1713 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001714 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715
1716 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1717 int CppHashLocLineNo =
1718 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001719 int LineNo =
1720 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1723 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001724 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001725
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001726 if (Parser->SavedDiagHandler)
1727 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1728 else
1729 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001730}
1731
Rafael Espindola2c064482012-08-21 18:29:30 +00001732// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1733// difference being that that function accepts '@' as part of identifiers and
1734// we can't do that. AsmLexer.cpp should probably be changed to handle
1735// '@' as a special case when needed.
1736static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001737 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1738 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001739}
1740
Rafael Espindola34b9c512012-06-03 23:57:14 +00001741bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001742 ArrayRef<MCAsmMacroParameter> Parameters,
1743 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001744 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001745 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001746 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001747
Preston Gurd05500642012-09-19 20:36:12 +00001748 // A macro without parameters is handled differently on Darwin:
1749 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001750 while (!Body.empty()) {
1751 // Scan for the next substitution.
1752 std::size_t End = Body.size(), Pos = 0;
1753 for (; Pos != End; ++Pos) {
1754 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001755 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 // This macro has no parameters, look for $0, $1, etc.
1757 if (Body[Pos] != '$' || Pos + 1 == End)
1758 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001759
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001761 if (Next == '$' || Next == 'n' ||
1762 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 break;
1764 } else {
1765 // This macro has parameters, look for \foo, \bar, etc.
1766 if (Body[Pos] == '\\' && Pos + 1 != End)
1767 break;
1768 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001769 }
1770
1771 // Add the prefix.
1772 OS << Body.slice(0, Pos);
1773
1774 // Check if we reached the end.
1775 if (Pos == End)
1776 break;
1777
Benjamin Kramer513e7442014-02-20 13:36:32 +00001778 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001779 switch (Body[Pos + 1]) {
1780 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 case '$':
1782 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001783 break;
1784
Jim Grosbach4b905842013-09-20 23:08:21 +00001785 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001786 case 'n':
1787 OS << A.size();
1788 break;
1789
Jim Grosbach4b905842013-09-20 23:08:21 +00001790 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001791 default: {
1792 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001793 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 if (Index >= A.size())
1795 break;
1796
1797 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001798 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001799 ie = A[Index].end();
1800 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001801 OS << it->getString();
1802 break;
1803 }
1804 }
1805 Pos += 2;
1806 } else {
1807 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001808 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 ++I;
1810
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 const char *Begin = Body.data() + Pos + 1;
1812 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001813 unsigned Index = 0;
1814 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001815 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816 break;
1817
Preston Gurd05500642012-09-19 20:36:12 +00001818 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001819 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1820 Pos += 3;
1821 else {
1822 OS << '\\' << Argument;
1823 Pos = I;
1824 }
Preston Gurd05500642012-09-19 20:36:12 +00001825 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001826 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001827 ie = A[Index].end();
1828 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001829 if (it->getKind() == AsmToken::String)
1830 OS << it->getStringContents();
1831 else
1832 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001833
Preston Gurd05500642012-09-19 20:36:12 +00001834 Pos += 1 + Argument.size();
1835 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001836 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001837 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001838 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001839 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001840
Rafael Espindola1134ab232011-06-05 02:43:45 +00001841 return false;
1842}
Daniel Dunbar43235712010-07-18 18:54:11 +00001843
Jim Grosbach4b905842013-09-20 23:08:21 +00001844MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1845 SMLoc EL, MemoryBuffer *I)
1846 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1847 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001848
Jim Grosbach4b905842013-09-20 23:08:21 +00001849static bool isOperator(AsmToken::TokenKind kind) {
1850 switch (kind) {
1851 default:
1852 return false;
1853 case AsmToken::Plus:
1854 case AsmToken::Minus:
1855 case AsmToken::Tilde:
1856 case AsmToken::Slash:
1857 case AsmToken::Star:
1858 case AsmToken::Dot:
1859 case AsmToken::Equal:
1860 case AsmToken::EqualEqual:
1861 case AsmToken::Pipe:
1862 case AsmToken::PipePipe:
1863 case AsmToken::Caret:
1864 case AsmToken::Amp:
1865 case AsmToken::AmpAmp:
1866 case AsmToken::Exclaim:
1867 case AsmToken::ExclaimEqual:
1868 case AsmToken::Percent:
1869 case AsmToken::Less:
1870 case AsmToken::LessEqual:
1871 case AsmToken::LessLess:
1872 case AsmToken::LessGreater:
1873 case AsmToken::Greater:
1874 case AsmToken::GreaterEqual:
1875 case AsmToken::GreaterGreater:
1876 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001877 }
1878}
1879
David Majnemer16252452014-01-29 00:07:39 +00001880namespace {
1881class AsmLexerSkipSpaceRAII {
1882public:
1883 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1884 Lexer.setSkipSpace(SkipSpace);
1885 }
1886
1887 ~AsmLexerSkipSpaceRAII() {
1888 Lexer.setSkipSpace(true);
1889 }
1890
1891private:
1892 AsmLexer &Lexer;
1893};
1894}
1895
David Majnemer91fc4c22014-01-29 18:57:46 +00001896bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001897 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001898 unsigned AddTokens = 0;
1899
David Majnemer16252452014-01-29 00:07:39 +00001900 // Darwin doesn't use spaces to delmit arguments.
1901 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001902
1903 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001904 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001905 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001906
David Majnemer91fc4c22014-01-29 18:57:46 +00001907 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001908 break;
Preston Gurd05500642012-09-19 20:36:12 +00001909
1910 if (Lexer.is(AsmToken::Space)) {
1911 Lex(); // Eat spaces
1912
1913 // Spaces can delimit parameters, but could also be part an expression.
1914 // If the token after a space is an operator, add the token and the next
1915 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001916 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001917 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001918 // Check to see whether the token is used as an operator,
1919 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001920 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001921 if (*NextChar == ' ')
1922 AddTokens = 2;
1923 }
1924
1925 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001926 break;
1927 }
1928 }
1929 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930
Jim Grosbach4b905842013-09-20 23:08:21 +00001931 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932 // to be able to fill in the remaining default parameter values
1933 if (Lexer.is(AsmToken::EndOfStatement))
1934 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001935
1936 // Adjust the current parentheses level.
1937 if (Lexer.is(AsmToken::LParen))
1938 ++ParenLevel;
1939 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1940 --ParenLevel;
1941
1942 // Append the token to the current argument list.
1943 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001944 if (AddTokens)
1945 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001946 Lex();
1947 }
Preston Gurd05500642012-09-19 20:36:12 +00001948
Rafael Espindola768b41c2012-06-15 14:02:34 +00001949 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001950 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001951 return false;
1952}
1953
1954// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001955bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001956 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001958 bool NamedParametersFound = false;
1959 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001961 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001962 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001963
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964 // Parse two kinds of macro invocations:
1965 // - macros defined without any parameters accept an arbitrary number of them
1966 // - macros defined with parameters accept at most that many of them
1967 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1968 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001969 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001970 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001971
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001972 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001973 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001974 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001975 eatToEndOfStatement();
1976 return true;
1977 }
1978
1979 if (!Lexer.is(AsmToken::Equal)) {
1980 TokError("expected '=' after formal parameter identifier");
1981 eatToEndOfStatement();
1982 return true;
1983 }
1984 Lex();
1985
1986 NamedParametersFound = true;
1987 }
1988
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001989 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001990 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001991 eatToEndOfStatement();
1992 return true;
1993 }
1994
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001995 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001996 return true;
1997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001999 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002000 unsigned FAI = 0;
2001 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002002 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002003 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002004
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002005 if (FAI >= NParameters) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002006 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002007 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool32413392014-03-13 07:02:46 +00002008 (M ? M->Name : "<unnamed>") + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002009 return true;
2010 }
2011 PI = FAI;
2012 }
2013
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002014 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002015 if (A.size() <= PI)
2016 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002017 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002018
2019 if (FALocs.size() <= PI)
2020 FALocs.resize(PI + 1);
2021
2022 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002023 }
Jim Grosbach206661622012-07-30 22:44:17 +00002024
Preston Gurd242ed3152012-09-19 20:29:04 +00002025 // At the end of the statement, fill in remaining arguments that have
2026 // default values. If there aren't any, then the next argument is
2027 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002028 if (Lexer.is(AsmToken::EndOfStatement)) {
2029 bool Failure = false;
2030 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2031 if (A[FAI].empty()) {
2032 if (M->Parameters[FAI].Required) {
2033 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2034 "missing value for required parameter "
2035 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2036 Failure = true;
2037 }
2038
2039 if (!M->Parameters[FAI].Value.empty())
2040 A[FAI] = M->Parameters[FAI].Value;
2041 }
2042 }
2043 return Failure;
2044 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002045
2046 if (Lexer.is(AsmToken::Comma))
2047 Lex();
2048 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002049
2050 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002051}
2052
Jim Grosbach4b905842013-09-20 23:08:21 +00002053const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2054 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002055 return (I == MacroMap.end()) ? NULL : I->getValue();
2056}
2057
Jim Grosbach4b905842013-09-20 23:08:21 +00002058void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002059 MacroMap[Name] = new MCAsmMacro(Macro);
2060}
2061
Jim Grosbach4b905842013-09-20 23:08:21 +00002062void AsmParser::undefineMacro(StringRef Name) {
2063 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002064 if (I != MacroMap.end()) {
2065 delete I->getValue();
2066 MacroMap.erase(I);
2067 }
2068}
2069
Jim Grosbach4b905842013-09-20 23:08:21 +00002070bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002071 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2072 // this, although we should protect against infinite loops.
2073 if (ActiveMacros.size() == 20)
2074 return TokError("macros cannot be nested more than 20 levels deep");
2075
Eli Bendersky38274122013-01-14 23:22:36 +00002076 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002077 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002078 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002079
Rafael Espindola1134ab232011-06-05 02:43:45 +00002080 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2081 // to hold the macro body with substitutions.
2082 SmallString<256> Buf;
2083 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002084 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002085
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002086 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002087 return true;
2088
Eli Bendersky38274122013-01-14 23:22:36 +00002089 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002090 // instantiation.
2091 OS << ".endmacro\n";
2092
Rafael Espindola1134ab232011-06-05 02:43:45 +00002093 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002094 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002095
Daniel Dunbar43235712010-07-18 18:54:11 +00002096 // Create the macro instantiation object and add to the current macro
2097 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002098 MacroInstantiation *MI = new MacroInstantiation(
2099 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002100 ActiveMacros.push_back(MI);
2101
2102 // Jump to the macro instantiation and prime the lexer.
2103 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2104 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2105 Lex();
2106
2107 return false;
2108}
2109
Jim Grosbach4b905842013-09-20 23:08:21 +00002110void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002111 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002112 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002113 Lex();
2114
2115 // Pop the instantiation entry.
2116 delete ActiveMacros.back();
2117 ActiveMacros.pop_back();
2118}
2119
Jim Grosbach4b905842013-09-20 23:08:21 +00002120static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002121 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002122 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002123 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2124 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002125 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002126 case MCExpr::Target:
2127 case MCExpr::Constant:
2128 return false;
2129 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002130 const MCSymbol &S =
2131 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002132 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002133 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002134 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002135 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002136 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002137 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002138 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002139
2140 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002141}
2142
Jim Grosbach4b905842013-09-20 23:08:21 +00002143bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002144 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002145 // FIXME: Use better location, we should use proper tokens.
2146 SMLoc EqualLoc = Lexer.getLoc();
2147
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002148 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002149 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002150 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002151
Rafael Espindola72f5f172012-01-28 05:57:00 +00002152 // Note: we don't count b as used in "a = b". This is to allow
2153 // a = b
2154 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002156 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002157 return TokError("unexpected token in assignment");
2158
2159 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002160 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002161
Daniel Dunbar5f339242009-10-16 01:57:39 +00002162 // Validate that the LHS is allowed to be a variable (either it has not been
2163 // used as a symbol, or it is an absolute symbol).
2164 MCSymbol *Sym = getContext().LookupSymbol(Name);
2165 if (Sym) {
2166 // Diagnose assignment to a label.
2167 //
2168 // FIXME: Diagnostics. Note the location of the definition as a label.
2169 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002170 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002171 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2172 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002173 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002174 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2175 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002176 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002177 return Error(EqualLoc, "redefinition of '" + Name + "'");
2178 else if (!Sym->isVariable())
2179 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002180 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002181 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002182 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002183
2184 // Don't count these checks as uses.
2185 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002186 } else if (Name == ".") {
2187 if (Out.EmitValueToOffset(Value, 0)) {
2188 Error(EqualLoc, "expected absolute expression");
2189 eatToEndOfStatement();
2190 }
2191 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002192 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002193 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002194
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002195 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002196 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002197 if (NoDeadStrip)
2198 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2199
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002200 return false;
2201}
2202
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002203/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002204/// ::= identifier
2205/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002206bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002207 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002208 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2209 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002210 // handle this as a context dependent token, instead we detect adjacent tokens
2211 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002212 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2213 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002214
Hans Wennborgce69d772013-10-18 20:46:28 +00002215 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002216 Lex();
2217 if (Lexer.isNot(AsmToken::Identifier))
2218 return true;
2219
Hans Wennborgce69d772013-10-18 20:46:28 +00002220 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2221 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002222 return true;
2223
2224 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002225 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002226 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002227 Lex();
2228 return false;
2229 }
2230
Jim Grosbach4b905842013-09-20 23:08:21 +00002231 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002232 return true;
2233
Sean Callanan936b0d32010-01-19 21:44:56 +00002234 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002235
Sean Callanan686ed8d2010-01-19 20:22:31 +00002236 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002237
2238 return false;
2239}
2240
Jim Grosbach4b905842013-09-20 23:08:21 +00002241/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002242/// ::= .equ identifier ',' expression
2243/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002244/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002245bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002246 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002247
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002248 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002249 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002250
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002251 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002252 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002253 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002254
Jim Grosbach4b905842013-09-20 23:08:21 +00002255 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002256}
2257
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002258bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002259 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002260
2261 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002262 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002263 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2264 if (Str[i] != '\\') {
2265 Data += Str[i];
2266 continue;
2267 }
2268
2269 // Recognize escaped characters. Note that this escape semantics currently
2270 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2271 ++i;
2272 if (i == e)
2273 return TokError("unexpected backslash at end of string");
2274
2275 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002276 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002277 // Consume up to three octal characters.
2278 unsigned Value = 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
Jim Grosbach4b905842013-09-20 23:08:21 +00002284 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002285 ++i;
2286 Value = Value * 8 + (Str[i] - '0');
2287 }
2288 }
2289
2290 if (Value > 255)
2291 return TokError("invalid octal escape sequence (out of range)");
2292
Jim Grosbach4b905842013-09-20 23:08:21 +00002293 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002294 continue;
2295 }
2296
2297 // Otherwise recognize individual escapes.
2298 switch (Str[i]) {
2299 default:
2300 // Just reject invalid escape sequences for now.
2301 return TokError("invalid escape sequence (unrecognized character)");
2302
2303 case 'b': Data += '\b'; break;
2304 case 'f': Data += '\f'; break;
2305 case 'n': Data += '\n'; break;
2306 case 'r': Data += '\r'; break;
2307 case 't': Data += '\t'; break;
2308 case '"': Data += '"'; break;
2309 case '\\': Data += '\\'; break;
2310 }
2311 }
2312
2313 return false;
2314}
2315
Jim Grosbach4b905842013-09-20 23:08:21 +00002316/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002317/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002318bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002319 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002320 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002321
Daniel Dunbara10e5192009-06-24 23:30:00 +00002322 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002323 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002324 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002325
Daniel Dunbaref668c12009-08-14 18:19:52 +00002326 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002327 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002328 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002329
Rafael Espindola64e1af82013-07-02 15:49:13 +00002330 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002331 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002332 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002333
Sean Callanan686ed8d2010-01-19 20:22:31 +00002334 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002335
2336 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002337 break;
2338
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002339 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002340 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002341 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002342 }
2343 }
2344
Sean Callanan686ed8d2010-01-19 20:22:31 +00002345 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002346 return false;
2347}
2348
Jim Grosbach4b905842013-09-20 23:08:21 +00002349/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002350/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002351bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002352 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002353 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002354
Daniel Dunbara10e5192009-06-24 23:30:00 +00002355 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002356 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002357 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002358 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 return true;
2360
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002361 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002362 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2363 assert(Size <= 8 && "Invalid size");
2364 uint64_t IntValue = MCE->getValue();
2365 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2366 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002367 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002368 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002369 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002371 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002372 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002373
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002375 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002376 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002377 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378 }
2379 }
2380
Sean Callanan686ed8d2010-01-19 20:22:31 +00002381 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002382 return false;
2383}
2384
David Woodhoused6de0d92014-02-01 16:20:59 +00002385/// ParseDirectiveOctaValue
2386/// ::= .octa [ hexconstant (, hexconstant)* ]
2387bool AsmParser::parseDirectiveOctaValue() {
2388 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2389 checkForValidSection();
2390
2391 for (;;) {
2392 if (Lexer.getKind() == AsmToken::Error)
2393 return true;
2394 if (Lexer.getKind() != AsmToken::Integer &&
2395 Lexer.getKind() != AsmToken::BigNum)
2396 return TokError("unknown token in expression");
2397
2398 SMLoc ExprLoc = getLexer().getLoc();
2399 APInt IntValue = getTok().getAPIntVal();
2400 Lex();
2401
2402 uint64_t hi, lo;
2403 if (IntValue.isIntN(64)) {
2404 hi = 0;
2405 lo = IntValue.getZExtValue();
2406 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002407 // It might actually have more than 128 bits, but the top ones are zero.
2408 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002409 lo = IntValue.getLoBits(64).getZExtValue();
2410 } else
2411 return Error(ExprLoc, "literal value out of range for directive");
2412
2413 if (MAI.isLittleEndian()) {
2414 getStreamer().EmitIntValue(lo, 8);
2415 getStreamer().EmitIntValue(hi, 8);
2416 } else {
2417 getStreamer().EmitIntValue(hi, 8);
2418 getStreamer().EmitIntValue(lo, 8);
2419 }
2420
2421 if (getLexer().is(AsmToken::EndOfStatement))
2422 break;
2423
2424 // FIXME: Improve diagnostic.
2425 if (getLexer().isNot(AsmToken::Comma))
2426 return TokError("unexpected token in directive");
2427 Lex();
2428 }
2429 }
2430
2431 Lex();
2432 return false;
2433}
2434
Jim Grosbach4b905842013-09-20 23:08:21 +00002435/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002436/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002437bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002438 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002439 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002440
2441 for (;;) {
2442 // We don't truly support arithmetic on floating point expressions, so we
2443 // have to manually parse unary prefixes.
2444 bool IsNeg = false;
2445 if (getLexer().is(AsmToken::Minus)) {
2446 Lex();
2447 IsNeg = true;
2448 } else if (getLexer().is(AsmToken::Plus))
2449 Lex();
2450
Michael J. Spencer530ce852010-10-09 11:00:50 +00002451 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002452 getLexer().isNot(AsmToken::Real) &&
2453 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002454 return TokError("unexpected token in directive");
2455
2456 // Convert to an APFloat.
2457 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002458 StringRef IDVal = getTok().getString();
2459 if (getLexer().is(AsmToken::Identifier)) {
2460 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2461 Value = APFloat::getInf(Semantics);
2462 else if (!IDVal.compare_lower("nan"))
2463 Value = APFloat::getNaN(Semantics, false, ~0);
2464 else
2465 return TokError("invalid floating point literal");
2466 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002467 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468 return TokError("invalid floating point literal");
2469 if (IsNeg)
2470 Value.changeSign();
2471
2472 // Consume the numeric token.
2473 Lex();
2474
2475 // Emit the value as an integer.
2476 APInt AsInt = Value.bitcastToAPInt();
2477 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002478 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002479
2480 if (getLexer().is(AsmToken::EndOfStatement))
2481 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002482
Daniel Dunbar2af16532010-09-24 01:59:56 +00002483 if (getLexer().isNot(AsmToken::Comma))
2484 return TokError("unexpected token in directive");
2485 Lex();
2486 }
2487 }
2488
2489 Lex();
2490 return false;
2491}
2492
Jim Grosbach4b905842013-09-20 23:08:21 +00002493/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002494/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002495bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002496 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002497
2498 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002499 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002500 return true;
2501
Rafael Espindolab91bac62010-10-05 19:42:57 +00002502 int64_t Val = 0;
2503 if (getLexer().is(AsmToken::Comma)) {
2504 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002505 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002506 return true;
2507 }
2508
Rafael Espindola922e3f42010-09-16 15:03:59 +00002509 if (getLexer().isNot(AsmToken::EndOfStatement))
2510 return TokError("unexpected token in '.zero' directive");
2511
2512 Lex();
2513
Rafael Espindola64e1af82013-07-02 15:49:13 +00002514 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002515
2516 return false;
2517}
2518
Jim Grosbach4b905842013-09-20 23:08:21 +00002519/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002520/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002521bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002522 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002523
David Majnemer522d3db2014-02-01 07:19:38 +00002524 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002525 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002526 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002527 return true;
2528
David Majnemer522d3db2014-02-01 07:19:38 +00002529 if (NumValues < 0) {
2530 Warning(RepeatLoc,
2531 "'.fill' directive with negative repeat count has no effect");
2532 NumValues = 0;
2533 }
2534
Roman Divackye33098f2013-09-24 17:44:41 +00002535 int64_t FillSize = 1;
2536 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002537
David Majnemer522d3db2014-02-01 07:19:38 +00002538 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002539 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2540 if (getLexer().isNot(AsmToken::Comma))
2541 return TokError("unexpected token in '.fill' directive");
2542 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002543
David Majnemer522d3db2014-02-01 07:19:38 +00002544 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002545 if (parseAbsoluteExpression(FillSize))
2546 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002547
Roman Divackye33098f2013-09-24 17:44:41 +00002548 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2549 if (getLexer().isNot(AsmToken::Comma))
2550 return TokError("unexpected token in '.fill' directive");
2551 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002552
David Majnemer522d3db2014-02-01 07:19:38 +00002553 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002554 if (parseAbsoluteExpression(FillExpr))
2555 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002556
Roman Divackye33098f2013-09-24 17:44:41 +00002557 if (getLexer().isNot(AsmToken::EndOfStatement))
2558 return TokError("unexpected token in '.fill' directive");
2559
2560 Lex();
2561 }
2562 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002563
David Majnemer522d3db2014-02-01 07:19:38 +00002564 if (FillSize < 0) {
2565 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2566 NumValues = 0;
2567 }
2568 if (FillSize > 8) {
2569 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2570 FillSize = 8;
2571 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002572
David Majnemer522d3db2014-02-01 07:19:38 +00002573 if (!isUInt<32>(FillExpr) && FillSize > 4)
2574 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2575
2576 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2577 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2578
2579 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2580 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2581 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2582 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002583
2584 return false;
2585}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002586
Jim Grosbach4b905842013-09-20 23:08:21 +00002587/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002588/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002589bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002590 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002591
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002592 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002593 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002594 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002595 return true;
2596
2597 // Parse optional fill expression.
2598 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002599 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2600 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002601 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002602 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002603
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002604 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002605 return true;
2606
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002607 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002608 return TokError("unexpected token in '.org' directive");
2609 }
2610
Sean Callanan686ed8d2010-01-19 20:22:31 +00002611 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002612
Jim Grosbachb5912772012-01-27 00:37:08 +00002613 // Only limited forms of relocatable expressions are accepted here, it
2614 // has to be relative to the current section. The streamer will return
2615 // 'true' if the expression wasn't evaluatable.
2616 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2617 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002618
2619 return false;
2620}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002621
Jim Grosbach4b905842013-09-20 23:08:21 +00002622/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002623/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002624bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002625 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002626
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002627 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002628 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002629 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002630 return true;
2631
2632 SMLoc MaxBytesLoc;
2633 bool HasFillExpr = false;
2634 int64_t FillExpr = 0;
2635 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2637 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002638 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002639 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002640
2641 // The fill expression can be omitted while specifying a maximum number of
2642 // alignment bytes, e.g:
2643 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002644 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002645 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002646 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002647 return true;
2648 }
2649
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002650 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2651 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002653 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002655 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002656 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002657 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002658
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 return TokError("unexpected token in directive");
2661 }
2662 }
2663
Sean Callanan686ed8d2010-01-19 20:22:31 +00002664 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002665
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002666 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668
2669 // Compute alignment in bytes.
2670 if (IsPow2) {
2671 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002672 if (Alignment >= 32) {
2673 Error(AlignmentLoc, "invalid alignment value");
2674 Alignment = 31;
2675 }
2676
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002677 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002678 } else {
2679 // Reject alignments that aren't a power of two, for gas compatibility.
2680 if (!isPowerOf2_64(Alignment))
2681 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 }
2683
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002684 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002685 if (MaxBytesLoc.isValid()) {
2686 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002687 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002688 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002689 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002690 }
2691
2692 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002693 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002694 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695 MaxBytesToFill = 0;
2696 }
2697 }
2698
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002699 // Check whether we should use optimal code alignment for this .align
2700 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002701 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002702 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2703 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002704 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002705 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002706 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002707 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2708 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002709 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002710
2711 return false;
2712}
2713
Jim Grosbach4b905842013-09-20 23:08:21 +00002714/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002715/// ::= .file [number] filename
2716/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002717bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002718 // FIXME: I'm not sure what this is.
2719 int64_t FileNumber = -1;
2720 SMLoc FileNumberLoc = getLexer().getLoc();
2721 if (getLexer().is(AsmToken::Integer)) {
2722 FileNumber = getTok().getIntVal();
2723 Lex();
2724
2725 if (FileNumber < 1)
2726 return TokError("file number less than one");
2727 }
2728
2729 if (getLexer().isNot(AsmToken::String))
2730 return TokError("unexpected token in '.file' directive");
2731
2732 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002733 // Allow the strings to have escaped octal character sequence.
2734 std::string Path = getTok().getString();
2735 if (parseEscapedString(Path))
2736 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002737 Lex();
2738
2739 StringRef Directory;
2740 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002741 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002742 if (getLexer().is(AsmToken::String)) {
2743 if (FileNumber == -1)
2744 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002745 if (parseEscapedString(FilenameData))
2746 return true;
2747 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002748 Directory = Path;
2749 Lex();
2750 } else {
2751 Filename = Path;
2752 }
2753
2754 if (getLexer().isNot(AsmToken::EndOfStatement))
2755 return TokError("unexpected token in '.file' directive");
2756
2757 if (FileNumber == -1)
2758 getStreamer().EmitFileDirective(Filename);
2759 else {
2760 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002761 Error(DirectiveLoc,
2762 "input can't have .file dwarf directives when -g is "
2763 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002764
2765 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2766 Error(FileNumberLoc, "file number already allocated");
2767 }
2768
2769 return false;
2770}
2771
Jim Grosbach4b905842013-09-20 23:08:21 +00002772/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002773/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002774bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002775 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2776 if (getLexer().isNot(AsmToken::Integer))
2777 return TokError("unexpected token in '.line' directive");
2778
2779 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002780 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002781 Lex();
2782
2783 // FIXME: Do something with the .line.
2784 }
2785
2786 if (getLexer().isNot(AsmToken::EndOfStatement))
2787 return TokError("unexpected token in '.line' directive");
2788
2789 return false;
2790}
2791
Jim Grosbach4b905842013-09-20 23:08:21 +00002792/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002793/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2794/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2795/// The first number is a file number, must have been previously assigned with
2796/// a .file directive, the second number is the line number and optionally the
2797/// third number is a column position (zero if not specified). The remaining
2798/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002799bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002800 if (getLexer().isNot(AsmToken::Integer))
2801 return TokError("unexpected token in '.loc' directive");
2802 int64_t FileNumber = getTok().getIntVal();
2803 if (FileNumber < 1)
2804 return TokError("file number less than one in '.loc' directive");
2805 if (!getContext().isValidDwarfFileNumber(FileNumber))
2806 return TokError("unassigned file number in '.loc' directive");
2807 Lex();
2808
2809 int64_t LineNumber = 0;
2810 if (getLexer().is(AsmToken::Integer)) {
2811 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002812 if (LineNumber < 0)
2813 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002814 Lex();
2815 }
2816
2817 int64_t ColumnPos = 0;
2818 if (getLexer().is(AsmToken::Integer)) {
2819 ColumnPos = getTok().getIntVal();
2820 if (ColumnPos < 0)
2821 return TokError("column position less than zero in '.loc' directive");
2822 Lex();
2823 }
2824
2825 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2826 unsigned Isa = 0;
2827 int64_t Discriminator = 0;
2828 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2829 for (;;) {
2830 if (getLexer().is(AsmToken::EndOfStatement))
2831 break;
2832
2833 StringRef Name;
2834 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002835 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002836 return TokError("unexpected token in '.loc' directive");
2837
2838 if (Name == "basic_block")
2839 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2840 else if (Name == "prologue_end")
2841 Flags |= DWARF2_FLAG_PROLOGUE_END;
2842 else if (Name == "epilogue_begin")
2843 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2844 else if (Name == "is_stmt") {
2845 Loc = getTok().getLoc();
2846 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002847 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002848 return true;
2849 // The expression must be the constant 0 or 1.
2850 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2851 int Value = MCE->getValue();
2852 if (Value == 0)
2853 Flags &= ~DWARF2_FLAG_IS_STMT;
2854 else if (Value == 1)
2855 Flags |= DWARF2_FLAG_IS_STMT;
2856 else
2857 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002858 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002859 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2860 }
Craig Topperf15655b2013-04-22 04:22:40 +00002861 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002862 Loc = getTok().getLoc();
2863 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002864 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002865 return true;
2866 // The expression must be a constant greater or equal to 0.
2867 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2868 int Value = MCE->getValue();
2869 if (Value < 0)
2870 return Error(Loc, "isa number less than zero");
2871 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002872 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002873 return Error(Loc, "isa number not a constant value");
2874 }
Craig Topperf15655b2013-04-22 04:22:40 +00002875 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002876 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002878 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002879 return Error(Loc, "unknown sub-directive in '.loc' directive");
2880 }
2881
2882 if (getLexer().is(AsmToken::EndOfStatement))
2883 break;
2884 }
2885 }
2886
2887 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2888 Isa, Discriminator, StringRef());
2889
2890 return false;
2891}
2892
Jim Grosbach4b905842013-09-20 23:08:21 +00002893/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002894/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002895bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return TokError("unsupported directive '.stabs'");
2897}
2898
Jim Grosbach4b905842013-09-20 23:08:21 +00002899/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002900/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002901bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002902 StringRef Name;
2903 bool EH = false;
2904 bool Debug = false;
2905
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002906 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002907 return TokError("Expected an identifier");
2908
2909 if (Name == ".eh_frame")
2910 EH = true;
2911 else if (Name == ".debug_frame")
2912 Debug = true;
2913
2914 if (getLexer().is(AsmToken::Comma)) {
2915 Lex();
2916
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002917 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002918 return TokError("Expected an identifier");
2919
2920 if (Name == ".eh_frame")
2921 EH = true;
2922 else if (Name == ".debug_frame")
2923 Debug = true;
2924 }
2925
2926 getStreamer().EmitCFISections(EH, Debug);
2927 return false;
2928}
2929
Jim Grosbach4b905842013-09-20 23:08:21 +00002930/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002931/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002933 StringRef Simple;
2934 if (getLexer().isNot(AsmToken::EndOfStatement))
2935 if (parseIdentifier(Simple) || Simple != "simple")
2936 return TokError("unexpected token in .cfi_startproc directive");
2937
2938 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002939 return false;
2940}
2941
Jim Grosbach4b905842013-09-20 23:08:21 +00002942/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002943/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002944bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002945 getStreamer().EmitCFIEndProc();
2946 return false;
2947}
2948
Jim Grosbach4b905842013-09-20 23:08:21 +00002949/// \brief parse register name or number.
2950bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002951 SMLoc DirectiveLoc) {
2952 unsigned RegNo;
2953
2954 if (getLexer().isNot(AsmToken::Integer)) {
2955 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2956 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002957 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002958 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002959 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002960
2961 return false;
2962}
2963
Jim Grosbach4b905842013-09-20 23:08:21 +00002964/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002965/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002966bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002967 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002968 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002969 return true;
2970
2971 if (getLexer().isNot(AsmToken::Comma))
2972 return TokError("unexpected token in directive");
2973 Lex();
2974
2975 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002976 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002977 return true;
2978
2979 getStreamer().EmitCFIDefCfa(Register, Offset);
2980 return false;
2981}
2982
Jim Grosbach4b905842013-09-20 23:08:21 +00002983/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002984/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002985bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002986 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002987 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002988 return true;
2989
2990 getStreamer().EmitCFIDefCfaOffset(Offset);
2991 return false;
2992}
2993
Jim Grosbach4b905842013-09-20 23:08:21 +00002994/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002995/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002996bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002997 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002998 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002999 return true;
3000
3001 if (getLexer().isNot(AsmToken::Comma))
3002 return TokError("unexpected token in directive");
3003 Lex();
3004
3005 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003006 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003007 return true;
3008
3009 getStreamer().EmitCFIRegister(Register1, Register2);
3010 return false;
3011}
3012
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003013/// parseDirectiveCFIWindowSave
3014/// ::= .cfi_window_save
3015bool AsmParser::parseDirectiveCFIWindowSave() {
3016 getStreamer().EmitCFIWindowSave();
3017 return false;
3018}
3019
Jim Grosbach4b905842013-09-20 23:08:21 +00003020/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003021/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003022bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003023 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003024 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003025 return true;
3026
3027 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3028 return false;
3029}
3030
Jim Grosbach4b905842013-09-20 23:08:21 +00003031/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003032/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003033bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003034 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003035 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003036 return true;
3037
3038 getStreamer().EmitCFIDefCfaRegister(Register);
3039 return false;
3040}
3041
Jim Grosbach4b905842013-09-20 23:08:21 +00003042/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003043/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003044bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003045 int64_t Register = 0;
3046 int64_t Offset = 0;
3047
Jim Grosbach4b905842013-09-20 23:08:21 +00003048 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003049 return true;
3050
3051 if (getLexer().isNot(AsmToken::Comma))
3052 return TokError("unexpected token in directive");
3053 Lex();
3054
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003055 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003056 return true;
3057
3058 getStreamer().EmitCFIOffset(Register, Offset);
3059 return false;
3060}
3061
Jim Grosbach4b905842013-09-20 23:08:21 +00003062/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003063/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003064bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003065 int64_t Register = 0;
3066
Jim Grosbach4b905842013-09-20 23:08:21 +00003067 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003068 return true;
3069
3070 if (getLexer().isNot(AsmToken::Comma))
3071 return TokError("unexpected token in directive");
3072 Lex();
3073
3074 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003075 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003076 return true;
3077
3078 getStreamer().EmitCFIRelOffset(Register, Offset);
3079 return false;
3080}
3081
3082static bool isValidEncoding(int64_t Encoding) {
3083 if (Encoding & ~0xff)
3084 return false;
3085
3086 if (Encoding == dwarf::DW_EH_PE_omit)
3087 return true;
3088
3089 const unsigned Format = Encoding & 0xf;
3090 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3091 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3092 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3093 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3094 return false;
3095
3096 const unsigned Application = Encoding & 0x70;
3097 if (Application != dwarf::DW_EH_PE_absptr &&
3098 Application != dwarf::DW_EH_PE_pcrel)
3099 return false;
3100
3101 return true;
3102}
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003105/// IsPersonality true for cfi_personality, false for cfi_lsda
3106/// ::= .cfi_personality encoding, [symbol_name]
3107/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003108bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003109 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003110 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003111 return true;
3112 if (Encoding == dwarf::DW_EH_PE_omit)
3113 return false;
3114
3115 if (!isValidEncoding(Encoding))
3116 return TokError("unsupported encoding.");
3117
3118 if (getLexer().isNot(AsmToken::Comma))
3119 return TokError("unexpected token in directive");
3120 Lex();
3121
3122 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003123 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003124 return TokError("expected identifier in directive");
3125
3126 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3127
3128 if (IsPersonality)
3129 getStreamer().EmitCFIPersonality(Sym, Encoding);
3130 else
3131 getStreamer().EmitCFILsda(Sym, Encoding);
3132 return false;
3133}
3134
Jim Grosbach4b905842013-09-20 23:08:21 +00003135/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003136/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003137bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003138 getStreamer().EmitCFIRememberState();
3139 return false;
3140}
3141
Jim Grosbach4b905842013-09-20 23:08:21 +00003142/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003143/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003144bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003145 getStreamer().EmitCFIRestoreState();
3146 return false;
3147}
3148
Jim Grosbach4b905842013-09-20 23:08:21 +00003149/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003150/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003151bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003152 int64_t Register = 0;
3153
Jim Grosbach4b905842013-09-20 23:08:21 +00003154 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003155 return true;
3156
3157 getStreamer().EmitCFISameValue(Register);
3158 return false;
3159}
3160
Jim Grosbach4b905842013-09-20 23:08:21 +00003161/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003162/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003163bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003164 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003165 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003166 return true;
3167
3168 getStreamer().EmitCFIRestore(Register);
3169 return false;
3170}
3171
Jim Grosbach4b905842013-09-20 23:08:21 +00003172/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003173/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003174bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003175 std::string Values;
3176 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003177 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003178 return true;
3179
3180 Values.push_back((uint8_t)CurrValue);
3181
3182 while (getLexer().is(AsmToken::Comma)) {
3183 Lex();
3184
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003185 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003186 return true;
3187
3188 Values.push_back((uint8_t)CurrValue);
3189 }
3190
3191 getStreamer().EmitCFIEscape(Values);
3192 return false;
3193}
3194
Jim Grosbach4b905842013-09-20 23:08:21 +00003195/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003196/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003197bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003198 if (getLexer().isNot(AsmToken::EndOfStatement))
3199 return Error(getLexer().getLoc(),
3200 "unexpected token in '.cfi_signal_frame'");
3201
3202 getStreamer().EmitCFISignalFrame();
3203 return false;
3204}
3205
Jim Grosbach4b905842013-09-20 23:08:21 +00003206/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003207/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003208bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003209 int64_t Register = 0;
3210
Jim Grosbach4b905842013-09-20 23:08:21 +00003211 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003212 return true;
3213
3214 getStreamer().EmitCFIUndefined(Register);
3215 return false;
3216}
3217
Jim Grosbach4b905842013-09-20 23:08:21 +00003218/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003219/// ::= .macros_on
3220/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003221bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003222 if (getLexer().isNot(AsmToken::EndOfStatement))
3223 return Error(getLexer().getLoc(),
3224 "unexpected token in '" + Directive + "' directive");
3225
Jim Grosbach4b905842013-09-20 23:08:21 +00003226 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003227 return false;
3228}
3229
Jim Grosbach4b905842013-09-20 23:08:21 +00003230/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003231/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003232bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003233 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003234 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003235 return TokError("expected identifier in '.macro' directive");
3236
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003237 if (getLexer().is(AsmToken::Comma))
3238 Lex();
3239
Eli Bendersky17233942013-01-15 22:59:42 +00003240 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003241 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3242 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003243 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003244 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003245
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003246 if (Lexer.is(AsmToken::Colon)) {
3247 Lex(); // consume ':'
3248
3249 SMLoc QualLoc;
3250 StringRef Qualifier;
3251
3252 QualLoc = Lexer.getLoc();
3253 if (parseIdentifier(Qualifier))
3254 return Error(QualLoc, "missing parameter qualifier for "
3255 "'" + Parameter.Name + "' in macro '" + Name + "'");
3256
3257 if (Qualifier == "req")
3258 Parameter.Required = true;
3259 else
3260 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3261 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3262 }
3263
David Majnemer91fc4c22014-01-29 18:57:46 +00003264 if (getLexer().is(AsmToken::Equal)) {
3265 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003266
3267 SMLoc ParamLoc;
3268
3269 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003270 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003271 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003272
3273 if (Parameter.Required)
3274 Warning(ParamLoc, "pointless default value for required parameter "
3275 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003276 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003277
3278 Parameters.push_back(Parameter);
3279
3280 if (getLexer().is(AsmToken::Comma))
3281 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003282 }
3283
3284 // Eat the end of statement.
3285 Lex();
3286
3287 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003288 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003289
3290 // Lex the macro definition.
3291 for (;;) {
3292 // Check whether we have reached the end of the file.
3293 if (getLexer().is(AsmToken::Eof))
3294 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3295
3296 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003297 if (getLexer().is(AsmToken::Identifier)) {
3298 if (getTok().getIdentifier() == ".endm" ||
3299 getTok().getIdentifier() == ".endmacro") {
3300 if (MacroDepth == 0) { // Outermost macro.
3301 EndToken = getTok();
3302 Lex();
3303 if (getLexer().isNot(AsmToken::EndOfStatement))
3304 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3305 "' directive");
3306 break;
3307 } else {
3308 // Otherwise we just found the end of an inner macro.
3309 --MacroDepth;
3310 }
3311 } else if (getTok().getIdentifier() == ".macro") {
3312 // We allow nested macros. Those aren't instantiated until the outermost
3313 // macro is expanded so just ignore them for now.
3314 ++MacroDepth;
3315 }
Eli Bendersky17233942013-01-15 22:59:42 +00003316 }
3317
3318 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003319 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003320 }
3321
Jim Grosbach4b905842013-09-20 23:08:21 +00003322 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003323 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3324 }
3325
3326 const char *BodyStart = StartToken.getLoc().getPointer();
3327 const char *BodyEnd = EndToken.getLoc().getPointer();
3328 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003329 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3330 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003331 return false;
3332}
3333
Jim Grosbach4b905842013-09-20 23:08:21 +00003334/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003335///
3336/// With the support added for named parameters there may be code out there that
3337/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003338/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003339/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003340/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003341/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3342/// warning that the positional parameter found in body which have no effect.
3343/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003344/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003345/// intended or change the macro to use the named parameters. It is possible
3346/// this warning will trigger when the none of the named parameters are used
3347/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003348void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003349 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003350 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003351 // If this macro is not defined with named parameters the warning we are
3352 // checking for here doesn't apply.
3353 unsigned NParameters = Parameters.size();
3354 if (NParameters == 0)
3355 return;
3356
3357 bool NamedParametersFound = false;
3358 bool PositionalParametersFound = false;
3359
3360 // Look at the body of the macro for use of both the named parameters and what
3361 // are likely to be positional parameters. This is what expandMacro() is
3362 // doing when it finds the parameters in the body.
3363 while (!Body.empty()) {
3364 // Scan for the next possible parameter.
3365 std::size_t End = Body.size(), Pos = 0;
3366 for (; Pos != End; ++Pos) {
3367 // Check for a substitution or escape.
3368 // This macro is defined with parameters, look for \foo, \bar, etc.
3369 if (Body[Pos] == '\\' && Pos + 1 != End)
3370 break;
3371
3372 // This macro should have parameters, but look for $0, $1, ..., $n too.
3373 if (Body[Pos] != '$' || Pos + 1 == End)
3374 continue;
3375 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003376 if (Next == '$' || Next == 'n' ||
3377 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378 break;
3379 }
3380
3381 // Check if we reached the end.
3382 if (Pos == End)
3383 break;
3384
3385 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003386 switch (Body[Pos + 1]) {
3387 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388 case '$':
3389 break;
3390
Jim Grosbach4b905842013-09-20 23:08:21 +00003391 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003392 case 'n':
3393 PositionalParametersFound = true;
3394 break;
3395
Jim Grosbach4b905842013-09-20 23:08:21 +00003396 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003397 default: {
3398 PositionalParametersFound = true;
3399 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003400 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003401 }
3402 Pos += 2;
3403 } else {
3404 unsigned I = Pos + 1;
3405 while (isIdentifierChar(Body[I]) && I + 1 != End)
3406 ++I;
3407
Jim Grosbach4b905842013-09-20 23:08:21 +00003408 const char *Begin = Body.data() + Pos + 1;
3409 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003410 unsigned Index = 0;
3411 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003412 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003413 break;
3414
3415 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003416 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3417 Pos += 3;
3418 else {
3419 Pos = I;
3420 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003421 } else {
3422 NamedParametersFound = true;
3423 Pos += 1 + Argument.size();
3424 }
3425 }
3426 // Update the scan point.
3427 Body = Body.substr(Pos);
3428 }
3429
3430 if (!NamedParametersFound && PositionalParametersFound)
3431 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3432 "used in macro body, possible positional parameter "
3433 "found in body which will have no effect");
3434}
3435
Jim Grosbach4b905842013-09-20 23:08:21 +00003436/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003437/// ::= .endm
3438/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003439bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003440 if (getLexer().isNot(AsmToken::EndOfStatement))
3441 return TokError("unexpected token in '" + Directive + "' directive");
3442
3443 // If we are inside a macro instantiation, terminate the current
3444 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003445 if (isInsideMacroInstantiation()) {
3446 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003447 return false;
3448 }
3449
3450 // Otherwise, this .endmacro is a stray entry in the file; well formed
3451 // .endmacro directives are handled during the macro definition parsing.
3452 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003453 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003454}
3455
Jim Grosbach4b905842013-09-20 23:08:21 +00003456/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003457/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003458bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003459 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003460 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003461 return TokError("expected identifier in '.purgem' directive");
3462
3463 if (getLexer().isNot(AsmToken::EndOfStatement))
3464 return TokError("unexpected token in '.purgem' directive");
3465
Jim Grosbach4b905842013-09-20 23:08:21 +00003466 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003467 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3468
Jim Grosbach4b905842013-09-20 23:08:21 +00003469 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003470 return false;
3471}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003474/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003475bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003476 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003477
3478 // Expect a single argument: an expression that evaluates to a constant
3479 // in the inclusive range 0-30.
3480 SMLoc ExprLoc = getLexer().getLoc();
3481 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003482 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003483 return true;
3484 else if (getLexer().isNot(AsmToken::EndOfStatement))
3485 return TokError("unexpected token after expression in"
3486 " '.bundle_align_mode' directive");
3487 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3488 return Error(ExprLoc,
3489 "invalid bundle alignment size (expected between 0 and 30)");
3490
3491 Lex();
3492
3493 // Because of AlignSizePow2's verified range we can safely truncate it to
3494 // unsigned.
3495 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3496 return false;
3497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003500/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003502 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003503 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003504
Eli Bendersky802b6282013-01-07 21:51:08 +00003505 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3506 StringRef Option;
3507 SMLoc Loc = getTok().getLoc();
3508 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003509 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003510
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003511 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003512 return Error(Loc, kInvalidOptionError);
3513
3514 if (Option != "align_to_end")
3515 return Error(Loc, kInvalidOptionError);
3516 else if (getLexer().isNot(AsmToken::EndOfStatement))
3517 return Error(Loc,
3518 "unexpected token after '.bundle_lock' directive option");
3519 AlignToEnd = true;
3520 }
3521
Eli Benderskyf483ff92012-12-20 19:05:53 +00003522 Lex();
3523
Eli Bendersky802b6282013-01-07 21:51:08 +00003524 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003525 return false;
3526}
3527
Jim Grosbach4b905842013-09-20 23:08:21 +00003528/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003529/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003530bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003531 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003532
3533 if (getLexer().isNot(AsmToken::EndOfStatement))
3534 return TokError("unexpected token in '.bundle_unlock' directive");
3535 Lex();
3536
3537 getStreamer().EmitBundleUnlock();
3538 return false;
3539}
3540
Jim Grosbach4b905842013-09-20 23:08:21 +00003541/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003542/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003543bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003545
3546 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003547 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003548 return true;
3549
3550 int64_t FillExpr = 0;
3551 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3552 if (getLexer().isNot(AsmToken::Comma))
3553 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3554 Lex();
3555
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003556 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003557 return true;
3558
3559 if (getLexer().isNot(AsmToken::EndOfStatement))
3560 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3561 }
3562
3563 Lex();
3564
3565 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003566 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3567 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003568
3569 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003570 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003571
3572 return false;
3573}
3574
Jim Grosbach4b905842013-09-20 23:08:21 +00003575/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003576/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003577bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003578 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003579 const MCExpr *Value;
3580
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003581 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003582 return true;
3583
3584 if (getLexer().isNot(AsmToken::EndOfStatement))
3585 return TokError("unexpected token in directive");
3586
3587 if (Signed)
3588 getStreamer().EmitSLEB128Value(Value);
3589 else
3590 getStreamer().EmitULEB128Value(Value);
3591
3592 return false;
3593}
3594
Jim Grosbach4b905842013-09-20 23:08:21 +00003595/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003596/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003597bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003598 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003599 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003600 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003601 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003602
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003603 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003604 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003605
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003606 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003607
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003608 // Assembler local symbols don't make any sense here. Complain loudly.
3609 if (Sym->isTemporary())
3610 return Error(Loc, "non-local symbol required in directive");
3611
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003612 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3613 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003614
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003615 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003616 break;
3617
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003618 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003619 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003620 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003621 }
3622 }
3623
Sean Callanan686ed8d2010-01-19 20:22:31 +00003624 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003625 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003626}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003627
Jim Grosbach4b905842013-09-20 23:08:21 +00003628/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003629/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003630bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003631 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003632
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003633 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003634 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003635 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003636 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003637
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003638 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003639 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003640
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003641 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003642 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003643 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003644
3645 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003646 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003647 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003648 return true;
3649
3650 int64_t Pow2Alignment = 0;
3651 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003652 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003653 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003654 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003655 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003656 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003657
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003658 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3659 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003660 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3661
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003662 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003663 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3664 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003665 if (!isPowerOf2_64(Pow2Alignment))
3666 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3667 Pow2Alignment = Log2_64(Pow2Alignment);
3668 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003669 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003670
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003671 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003672 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003673
Sean Callanan686ed8d2010-01-19 20:22:31 +00003674 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003675
Chris Lattner28ad7542009-07-09 17:25:12 +00003676 // NOTE: a size of zero for a .comm should create a undefined symbol
3677 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003678 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003679 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003680 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003681
Eric Christopherbc818852010-05-14 01:38:54 +00003682 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003683 // may internally end up wanting an alignment in bytes.
3684 // FIXME: Diagnose overflow.
3685 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003686 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003687 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003688
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003689 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003690 return Error(IDLoc, "invalid symbol redefinition");
3691
Chris Lattner28ad7542009-07-09 17:25:12 +00003692 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003693 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003694 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003695 return false;
3696 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003697
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003698 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003699 return false;
3700}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003701
Jim Grosbach4b905842013-09-20 23:08:21 +00003702/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003703/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003704bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003705 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003706 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003707
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003708 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003709 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003710 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003711
Sean Callanan686ed8d2010-01-19 20:22:31 +00003712 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003713
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003714 if (Str.empty())
3715 Error(Loc, ".abort detected. Assembly stopping.");
3716 else
3717 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003718 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003719
3720 return false;
3721}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003722
Jim Grosbach4b905842013-09-20 23:08:21 +00003723/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003724/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003725bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003726 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003727 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003728
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003729 // Allow the strings to have escaped octal character sequence.
3730 std::string Filename;
3731 if (parseEscapedString(Filename))
3732 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003734 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003735
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003736 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003737 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003738
Chris Lattner693fbb82009-07-16 06:14:39 +00003739 // Attempt to switch the lexer to the included file before consuming the end
3740 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003741 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003742 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003743 return true;
3744 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003745
3746 return false;
3747}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003748
Jim Grosbach4b905842013-09-20 23:08:21 +00003749/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003750/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003751bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003752 if (getLexer().isNot(AsmToken::String))
3753 return TokError("expected string in '.incbin' directive");
3754
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003755 // Allow the strings to have escaped octal character sequence.
3756 std::string Filename;
3757 if (parseEscapedString(Filename))
3758 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003759 SMLoc IncbinLoc = getLexer().getLoc();
3760 Lex();
3761
3762 if (getLexer().isNot(AsmToken::EndOfStatement))
3763 return TokError("unexpected token in '.incbin' directive");
3764
Kevin Enderby109f25c2011-12-14 21:47:48 +00003765 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003766 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003767 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3768 return true;
3769 }
3770
3771 return false;
3772}
3773
Jim Grosbach4b905842013-09-20 23:08:21 +00003774/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003775/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003776/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003777bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003778 TheCondStack.push_back(TheCondState);
3779 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003780 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003781 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003782 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003783 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003784 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003785 return true;
3786
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003787 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003788 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003789
Sean Callanan686ed8d2010-01-19 20:22:31 +00003790 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003791
3792 TheCondState.CondMet = ExprValue;
3793 TheCondState.Ignore = !TheCondState.CondMet;
3794 }
3795
3796 return false;
3797}
3798
Jim Grosbach4b905842013-09-20 23:08:21 +00003799/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003800/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003801bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003802 TheCondStack.push_back(TheCondState);
3803 TheCondState.TheCond = AsmCond::IfCond;
3804
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003805 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003807 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003808 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003809
3810 if (getLexer().isNot(AsmToken::EndOfStatement))
3811 return TokError("unexpected token in '.ifb' directive");
3812
3813 Lex();
3814
3815 TheCondState.CondMet = ExpectBlank == Str.empty();
3816 TheCondState.Ignore = !TheCondState.CondMet;
3817 }
3818
3819 return false;
3820}
3821
Jim Grosbach4b905842013-09-20 23:08:21 +00003822/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003823/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003824/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003825bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003826 TheCondStack.push_back(TheCondState);
3827 TheCondState.TheCond = AsmCond::IfCond;
3828
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003829 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003830 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003831 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003832 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003833
3834 if (getLexer().isNot(AsmToken::Comma))
3835 return TokError("unexpected token in '.ifc' directive");
3836
3837 Lex();
3838
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003839 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003840
3841 if (getLexer().isNot(AsmToken::EndOfStatement))
3842 return TokError("unexpected token in '.ifc' directive");
3843
3844 Lex();
3845
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003846 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003847 TheCondState.Ignore = !TheCondState.CondMet;
3848 }
3849
3850 return false;
3851}
3852
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003853/// parseDirectiveIfeqs
3854/// ::= .ifeqs string1, string2
3855bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3856 if (Lexer.isNot(AsmToken::String)) {
3857 TokError("expected string parameter for '.ifeqs' directive");
3858 eatToEndOfStatement();
3859 return true;
3860 }
3861
3862 StringRef String1 = getTok().getStringContents();
3863 Lex();
3864
3865 if (Lexer.isNot(AsmToken::Comma)) {
3866 TokError("expected comma after first string for '.ifeqs' directive");
3867 eatToEndOfStatement();
3868 return true;
3869 }
3870
3871 Lex();
3872
3873 if (Lexer.isNot(AsmToken::String)) {
3874 TokError("expected string parameter for '.ifeqs' directive");
3875 eatToEndOfStatement();
3876 return true;
3877 }
3878
3879 StringRef String2 = getTok().getStringContents();
3880 Lex();
3881
3882 TheCondStack.push_back(TheCondState);
3883 TheCondState.TheCond = AsmCond::IfCond;
3884 TheCondState.CondMet = String1 == String2;
3885 TheCondState.Ignore = !TheCondState.CondMet;
3886
3887 return false;
3888}
3889
Jim Grosbach4b905842013-09-20 23:08:21 +00003890/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003891/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003892bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003893 StringRef Name;
3894 TheCondStack.push_back(TheCondState);
3895 TheCondState.TheCond = AsmCond::IfCond;
3896
3897 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003898 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003899 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003900 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003901 return TokError("expected identifier after '.ifdef'");
3902
3903 Lex();
3904
3905 MCSymbol *Sym = getContext().LookupSymbol(Name);
3906
3907 if (expect_defined)
3908 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3909 else
3910 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3911 TheCondState.Ignore = !TheCondState.CondMet;
3912 }
3913
3914 return false;
3915}
3916
Jim Grosbach4b905842013-09-20 23:08:21 +00003917/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003918/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003919bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003920 if (TheCondState.TheCond != AsmCond::IfCond &&
3921 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003922 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3923 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003924 TheCondState.TheCond = AsmCond::ElseIfCond;
3925
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003926 bool LastIgnoreState = false;
3927 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003928 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003929 if (LastIgnoreState || TheCondState.CondMet) {
3930 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003931 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003932 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003933 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003934 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003935 return true;
3936
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003937 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003938 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003939
Sean Callanan686ed8d2010-01-19 20:22:31 +00003940 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003941 TheCondState.CondMet = ExprValue;
3942 TheCondState.Ignore = !TheCondState.CondMet;
3943 }
3944
3945 return false;
3946}
3947
Jim Grosbach4b905842013-09-20 23:08:21 +00003948/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003949/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003950bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003951 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003952 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003953
Sean Callanan686ed8d2010-01-19 20:22:31 +00003954 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003955
3956 if (TheCondState.TheCond != AsmCond::IfCond &&
3957 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003958 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3959 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003960 TheCondState.TheCond = AsmCond::ElseCond;
3961 bool LastIgnoreState = false;
3962 if (!TheCondStack.empty())
3963 LastIgnoreState = TheCondStack.back().Ignore;
3964 if (LastIgnoreState || TheCondState.CondMet)
3965 TheCondState.Ignore = true;
3966 else
3967 TheCondState.Ignore = false;
3968
3969 return false;
3970}
3971
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003972/// parseDirectiveEnd
3973/// ::= .end
3974bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3975 if (getLexer().isNot(AsmToken::EndOfStatement))
3976 return TokError("unexpected token in '.end' directive");
3977
3978 Lex();
3979
3980 while (Lexer.isNot(AsmToken::Eof))
3981 Lex();
3982
3983 return false;
3984}
3985
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003986/// parseDirectiveError
3987/// ::= .err
3988/// ::= .error [string]
3989bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
3990 if (!TheCondStack.empty()) {
3991 if (TheCondStack.back().Ignore) {
3992 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00003993 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003994 }
3995 }
3996
3997 if (!WithMessage)
3998 return Error(L, ".err encountered");
3999
4000 StringRef Message = ".error directive invoked in source file";
4001 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4002 if (Lexer.isNot(AsmToken::String)) {
4003 TokError(".error argument must be a string");
4004 eatToEndOfStatement();
4005 return true;
4006 }
4007
4008 Message = getTok().getStringContents();
4009 Lex();
4010 }
4011
4012 Error(L, Message);
4013 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004014}
4015
Jim Grosbach4b905842013-09-20 23:08:21 +00004016/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004018bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004019 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004021
Sean Callanan686ed8d2010-01-19 20:22:31 +00004022 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004023
Jim Grosbach4b905842013-09-20 23:08:21 +00004024 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004025 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4026 ".else");
4027 if (!TheCondStack.empty()) {
4028 TheCondState = TheCondStack.back();
4029 TheCondStack.pop_back();
4030 }
4031
4032 return false;
4033}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004034
Eli Bendersky17233942013-01-15 22:59:42 +00004035void AsmParser::initializeDirectiveKindMap() {
4036 DirectiveKindMap[".set"] = DK_SET;
4037 DirectiveKindMap[".equ"] = DK_EQU;
4038 DirectiveKindMap[".equiv"] = DK_EQUIV;
4039 DirectiveKindMap[".ascii"] = DK_ASCII;
4040 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4041 DirectiveKindMap[".string"] = DK_STRING;
4042 DirectiveKindMap[".byte"] = DK_BYTE;
4043 DirectiveKindMap[".short"] = DK_SHORT;
4044 DirectiveKindMap[".value"] = DK_VALUE;
4045 DirectiveKindMap[".2byte"] = DK_2BYTE;
4046 DirectiveKindMap[".long"] = DK_LONG;
4047 DirectiveKindMap[".int"] = DK_INT;
4048 DirectiveKindMap[".4byte"] = DK_4BYTE;
4049 DirectiveKindMap[".quad"] = DK_QUAD;
4050 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004051 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004052 DirectiveKindMap[".single"] = DK_SINGLE;
4053 DirectiveKindMap[".float"] = DK_FLOAT;
4054 DirectiveKindMap[".double"] = DK_DOUBLE;
4055 DirectiveKindMap[".align"] = DK_ALIGN;
4056 DirectiveKindMap[".align32"] = DK_ALIGN32;
4057 DirectiveKindMap[".balign"] = DK_BALIGN;
4058 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4059 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4060 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4061 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4062 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4063 DirectiveKindMap[".org"] = DK_ORG;
4064 DirectiveKindMap[".fill"] = DK_FILL;
4065 DirectiveKindMap[".zero"] = DK_ZERO;
4066 DirectiveKindMap[".extern"] = DK_EXTERN;
4067 DirectiveKindMap[".globl"] = DK_GLOBL;
4068 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004069 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4070 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4071 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4072 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4073 DirectiveKindMap[".reference"] = DK_REFERENCE;
4074 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4075 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4076 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4077 DirectiveKindMap[".comm"] = DK_COMM;
4078 DirectiveKindMap[".common"] = DK_COMMON;
4079 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4080 DirectiveKindMap[".abort"] = DK_ABORT;
4081 DirectiveKindMap[".include"] = DK_INCLUDE;
4082 DirectiveKindMap[".incbin"] = DK_INCBIN;
4083 DirectiveKindMap[".code16"] = DK_CODE16;
4084 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4085 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004086 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004087 DirectiveKindMap[".irp"] = DK_IRP;
4088 DirectiveKindMap[".irpc"] = DK_IRPC;
4089 DirectiveKindMap[".endr"] = DK_ENDR;
4090 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4091 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4092 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4093 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004094 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004095 DirectiveKindMap[".ifb"] = DK_IFB;
4096 DirectiveKindMap[".ifnb"] = DK_IFNB;
4097 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004098 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004099 DirectiveKindMap[".ifnc"] = DK_IFNC;
4100 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4101 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4102 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4103 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4104 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004105 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004106 DirectiveKindMap[".endif"] = DK_ENDIF;
4107 DirectiveKindMap[".skip"] = DK_SKIP;
4108 DirectiveKindMap[".space"] = DK_SPACE;
4109 DirectiveKindMap[".file"] = DK_FILE;
4110 DirectiveKindMap[".line"] = DK_LINE;
4111 DirectiveKindMap[".loc"] = DK_LOC;
4112 DirectiveKindMap[".stabs"] = DK_STABS;
4113 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4114 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4115 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4116 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4117 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4118 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4119 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4120 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4121 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4122 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4123 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4124 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4125 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4126 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4127 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4128 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4129 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4130 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4131 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4132 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4133 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004134 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004135 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4136 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4137 DirectiveKindMap[".macro"] = DK_MACRO;
4138 DirectiveKindMap[".endm"] = DK_ENDM;
4139 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4140 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004141 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004142 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004143}
4144
Jim Grosbach4b905842013-09-20 23:08:21 +00004145MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004146 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004147
Rafael Espindola34b9c512012-06-03 23:57:14 +00004148 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004149 for (;;) {
4150 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004151 if (getLexer().is(AsmToken::Eof)) {
4152 Error(DirectiveLoc, "no matching '.endr' in definition");
4153 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004154 }
4155
Rafael Espindola34b9c512012-06-03 23:57:14 +00004156 if (Lexer.is(AsmToken::Identifier) &&
4157 (getTok().getIdentifier() == ".rept")) {
4158 ++NestLevel;
4159 }
4160
4161 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004162 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004163 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004164 EndToken = getTok();
4165 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004166 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4167 TokError("unexpected token in '.endr' directive");
4168 return 0;
4169 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004170 break;
4171 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004172 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004173 }
4174
Rafael Espindola34b9c512012-06-03 23:57:14 +00004175 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004176 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004177 }
4178
4179 const char *BodyStart = StartToken.getLoc().getPointer();
4180 const char *BodyEnd = EndToken.getLoc().getPointer();
4181 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4182
Rafael Espindola34b9c512012-06-03 23:57:14 +00004183 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004184 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004185 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004186}
4187
Jim Grosbach4b905842013-09-20 23:08:21 +00004188void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004189 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004190 OS << ".endr\n";
4191
4192 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004193 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004194
Rafael Espindola34b9c512012-06-03 23:57:14 +00004195 // Create the macro instantiation object and add to the current macro
4196 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004197 MacroInstantiation *MI = new MacroInstantiation(
4198 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004199 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004200
Rafael Espindola34b9c512012-06-03 23:57:14 +00004201 // Jump to the macro instantiation and prime the lexer.
4202 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4203 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4204 Lex();
4205}
4206
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004207/// parseDirectiveRept
4208/// ::= .rep | .rept count
4209bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004210 const MCExpr *CountExpr;
4211 SMLoc CountLoc = getTok().getLoc();
4212 if (parseExpression(CountExpr))
4213 return true;
4214
Rafael Espindola34b9c512012-06-03 23:57:14 +00004215 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004216 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4217 eatToEndOfStatement();
4218 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4219 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004220
4221 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004222 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004223
4224 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004225 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004226
4227 // Eat the end of statement.
4228 Lex();
4229
4230 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004231 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004232 if (!M)
4233 return true;
4234
4235 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4236 // to hold the macro body with substitutions.
4237 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004238 raw_svector_ostream OS(Buf);
4239 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004240 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004241 return true;
4242 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004243 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004244
4245 return false;
4246}
4247
Jim Grosbach4b905842013-09-20 23:08:21 +00004248/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004249/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004250bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004251 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004252
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004253 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004254 return TokError("expected identifier in '.irp' directive");
4255
Rafael Espindola768b41c2012-06-15 14:02:34 +00004256 if (Lexer.isNot(AsmToken::Comma))
4257 return TokError("expected comma in '.irp' directive");
4258
4259 Lex();
4260
Eli Bendersky38274122013-01-14 23:22:36 +00004261 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004262 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004263 return true;
4264
4265 // Eat the end of statement.
4266 Lex();
4267
4268 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004269 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004270 if (!M)
4271 return true;
4272
4273 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4274 // to hold the macro body with substitutions.
4275 SmallString<256> Buf;
4276 raw_svector_ostream OS(Buf);
4277
Eli Bendersky38274122013-01-14 23:22:36 +00004278 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004279 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004280 return true;
4281 }
4282
Jim Grosbach4b905842013-09-20 23:08:21 +00004283 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004284
4285 return false;
4286}
4287
Jim Grosbach4b905842013-09-20 23:08:21 +00004288/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004289/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004290bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004291 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004292
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004293 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004294 return TokError("expected identifier in '.irpc' directive");
4295
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004296 if (Lexer.isNot(AsmToken::Comma))
4297 return TokError("expected comma in '.irpc' directive");
4298
4299 Lex();
4300
Eli Bendersky38274122013-01-14 23:22:36 +00004301 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004302 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004303 return true;
4304
4305 if (A.size() != 1 || A.front().size() != 1)
4306 return TokError("unexpected token in '.irpc' directive");
4307
4308 // Eat the end of statement.
4309 Lex();
4310
4311 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004312 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004313 if (!M)
4314 return true;
4315
4316 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4317 // to hold the macro body with substitutions.
4318 SmallString<256> Buf;
4319 raw_svector_ostream OS(Buf);
4320
4321 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004322 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004323 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004324 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004325
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004326 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004327 return true;
4328 }
4329
Jim Grosbach4b905842013-09-20 23:08:21 +00004330 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004331
4332 return false;
4333}
4334
Jim Grosbach4b905842013-09-20 23:08:21 +00004335bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004336 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004337 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004338
4339 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004340 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004341 assert(getLexer().is(AsmToken::EndOfStatement));
4342
Jim Grosbach4b905842013-09-20 23:08:21 +00004343 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004344 return false;
4345}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004346
Jim Grosbach4b905842013-09-20 23:08:21 +00004347bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004348 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004349 const MCExpr *Value;
4350 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004351 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004352 return true;
4353 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4354 if (!MCE)
4355 return Error(ExprLoc, "unexpected expression in _emit");
4356 uint64_t IntValue = MCE->getValue();
4357 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4358 return Error(ExprLoc, "literal value out of range for directive");
4359
Chad Rosierc7f552c2013-02-12 21:33:51 +00004360 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4361 return false;
4362}
4363
Jim Grosbach4b905842013-09-20 23:08:21 +00004364bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004365 const MCExpr *Value;
4366 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004367 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004368 return true;
4369 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4370 if (!MCE)
4371 return Error(ExprLoc, "unexpected expression in align");
4372 uint64_t IntValue = MCE->getValue();
4373 if (!isPowerOf2_64(IntValue))
4374 return Error(ExprLoc, "literal value not a power of two greater then zero");
4375
Jim Grosbach4b905842013-09-20 23:08:21 +00004376 Info.AsmRewrites->push_back(
4377 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004378 return false;
4379}
4380
Chad Rosierf43fcf52013-02-13 21:27:17 +00004381// We are comparing pointers, but the pointers are relative to a single string.
4382// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004383static int rewritesSort(const AsmRewrite *AsmRewriteA,
4384 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004385 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4386 return -1;
4387 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4388 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004389
Chad Rosierfce4fab2013-04-08 17:43:47 +00004390 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4391 // rewrite to the same location. Make sure the SizeDirective rewrite is
4392 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4393 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004394 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4395 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004396 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004397
Jim Grosbach4b905842013-09-20 23:08:21 +00004398 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4399 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004400 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004401 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004402}
4403
Jim Grosbach4b905842013-09-20 23:08:21 +00004404bool AsmParser::parseMSInlineAsm(
4405 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4406 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4407 SmallVectorImpl<std::string> &Constraints,
4408 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4409 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004410 SmallVector<void *, 4> InputDecls;
4411 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004412 SmallVector<bool, 4> InputDeclsAddressOf;
4413 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004414 SmallVector<std::string, 4> InputConstraints;
4415 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004416 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004417
Benjamin Kramer1a136112013-02-15 20:37:21 +00004418 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004419
4420 // Prime the lexer.
4421 Lex();
4422
4423 // While we have input, parse each statement.
4424 unsigned InputIdx = 0;
4425 unsigned OutputIdx = 0;
4426 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004427 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004428 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004429 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004430
Chad Rosier149e8e02012-12-12 22:45:52 +00004431 if (Info.ParseError)
4432 return true;
4433
Benjamin Kramer1a136112013-02-15 20:37:21 +00004434 if (Info.Opcode == ~0U)
4435 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004436
Benjamin Kramer1a136112013-02-15 20:37:21 +00004437 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004438
Benjamin Kramer1a136112013-02-15 20:37:21 +00004439 // Build the list of clobbers, outputs and inputs.
4440 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4441 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004442
Benjamin Kramer1a136112013-02-15 20:37:21 +00004443 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004444 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004445 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004446
Benjamin Kramer1a136112013-02-15 20:37:21 +00004447 // Register operand.
4448 if (Operand->isReg() && !Operand->needAddressOf()) {
4449 unsigned NumDefs = Desc.getNumDefs();
4450 // Clobber.
4451 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4452 ClobberRegs.push_back(Operand->getReg());
4453 continue;
4454 }
4455
4456 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004457 StringRef SymName = Operand->getSymName();
4458 if (SymName.empty())
4459 continue;
4460
Chad Rosierdba3fe52013-04-22 22:12:12 +00004461 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004462 if (!OpDecl)
4463 continue;
4464
4465 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004466 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004467 if (isOutput) {
4468 ++InputIdx;
4469 OutputDecls.push_back(OpDecl);
4470 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4471 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004472 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004473 } else {
4474 InputDecls.push_back(OpDecl);
4475 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4476 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004477 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004478 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004479 }
Reid Kleckneree088972013-12-10 18:27:32 +00004480
4481 // Consider implicit defs to be clobbers. Think of cpuid and push.
4482 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4483 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4484 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004485 }
4486
4487 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004488 NumOutputs = OutputDecls.size();
4489 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004490
4491 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004492 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4493 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4494 ClobberRegs.end());
4495 Clobbers.assign(ClobberRegs.size(), std::string());
4496 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4497 raw_string_ostream OS(Clobbers[I]);
4498 IP->printRegName(OS, ClobberRegs[I]);
4499 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004500
4501 // Merge the various outputs and inputs. Output are expected first.
4502 if (NumOutputs || NumInputs) {
4503 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004504 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004505 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004506 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004507 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004508 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004509 }
4510 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004511 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004512 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004513 }
4514 }
4515
4516 // Build the IR assembly string.
4517 std::string AsmStringIR;
4518 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004519 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4520 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004521 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004522 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4523 E = AsmStrRewrites.end();
4524 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004525 AsmRewriteKind Kind = (*I).Kind;
4526 if (Kind == AOK_Delete)
4527 continue;
4528
Chad Rosier8bce6642012-10-18 15:49:34 +00004529 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004530 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004531
Chad Rosier120eefd2013-03-19 17:32:17 +00004532 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004533 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004534 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004535 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004536
Chad Rosier37e755c2012-10-23 17:43:43 +00004537 // Skip the original expression.
4538 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004539 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004540 continue;
4541 }
4542
Chad Rosierff10ed12013-04-12 16:26:42 +00004543 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004544 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004545 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004546 default:
4547 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004548 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004549 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004550 break;
4551 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004552 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004553 break;
4554 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004555 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004556 break;
4557 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004559 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004560 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004561 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004562 default: break;
4563 case 8: OS << "byte ptr "; break;
4564 case 16: OS << "word ptr "; break;
4565 case 32: OS << "dword ptr "; break;
4566 case 64: OS << "qword ptr "; break;
4567 case 80: OS << "xword ptr "; break;
4568 case 128: OS << "xmmword ptr "; break;
4569 case 256: OS << "ymmword ptr "; break;
4570 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004571 break;
4572 case AOK_Emit:
4573 OS << ".byte";
4574 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004575 case AOK_Align: {
4576 unsigned Val = (*I).Val;
4577 OS << ".align " << Val;
4578
4579 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004581 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4582 break;
4583 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004584 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004585 // Insert the dot if the user omitted it.
4586 OS.flush();
4587 if (AsmStringIR.back() != '.')
4588 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004589 OS << (*I).Val;
4590 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004591 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004592
Chad Rosier8bce6642012-10-18 15:49:34 +00004593 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004594 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004595 }
4596
4597 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004598 if (AsmStart != AsmEnd)
4599 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004600
4601 AsmString = OS.str();
4602 return false;
4603}
4604
Daniel Dunbar01e36072010-07-17 02:26:10 +00004605/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004606MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4607 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004608 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004609}