blob: 168597fff45af50f176ef93f3034bf5ae7ac6865 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000048static cl::opt<bool>
49FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
51
Eric Christophera7c32732012-12-18 00:30:54 +000052MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000053
Daniel Dunbar86033402010-07-12 17:54:38 +000054namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000058
59struct MCAsmMacroParameter {
60 StringRef Name;
61 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000066};
67
Eli Benderskya313ae62013-01-16 18:56:50 +000068typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
69
70struct MCAsmMacro {
71 StringRef Name;
72 StringRef Body;
73 MCAsmMacroParameters Parameters;
74
75public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000076 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000077 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000078};
79
Daniel Dunbar43235712010-07-18 18:54:11 +000080/// \brief Helper class for storing information about an active macro
81/// instantiation.
82struct MacroInstantiation {
83 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000084 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000085
86 /// The macro instantiation with substitutions.
87 MemoryBuffer *Instantiation;
88
89 /// The location of the instantiation.
90 SMLoc InstantiationLoc;
91
Daniel Dunbar40f1d852012-12-01 01:38:48 +000092 /// The buffer where parsing should resume upon instantiation completion.
93 int ExitBuffer;
94
Daniel Dunbar43235712010-07-18 18:54:11 +000095 /// The location where parsing should resume upon instantiation completion.
96 SMLoc ExitLoc;
97
98public:
Eli Bendersky38274122013-01-14 23:22:36 +000099 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000100 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +0000101};
102
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 unsigned Opcode;
109
Jim Grosbach4b905842013-09-20 23:08:21 +0000110 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 bool ParseError;
112
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
114
Craig Topper353eda42014-04-24 06:44:33 +0000115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000118
119 ~ParseStatementInfo() {
120 // Free any parsed operands.
121 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
122 delete ParsedOperands[i];
123 ParsedOperands.clear();
124 }
125};
126
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127/// \brief The concrete assembly parser instance.
128class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000129 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
130 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131private:
132 AsmLexer Lexer;
133 MCContext &Ctx;
134 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000135 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000136 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000137 SourceMgr::DiagHandlerTy SavedDiagHandler;
138 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000140
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000141 /// This is the current buffer index we're lexing from as managed by the
142 /// SourceMgr object.
143 int CurBuffer;
144
145 AsmCond TheCondState;
146 std::vector<AsmCond> TheCondStack;
147
Jim Grosbach4b905842013-09-20 23:08:21 +0000148 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000149 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000150 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000151 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000154 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000155
Jim Grosbach4b905842013-09-20 23:08:21 +0000156 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000157 std::vector<MacroInstantiation*> ActiveMacros;
158
Jim Grosbach4b905842013-09-20 23:08:21 +0000159 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000160 std::deque<MCAsmMacro> MacroLikeBodies;
161
Daniel Dunbar828984f2010-07-18 18:38:02 +0000162 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000163 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000164
Daniel Dunbar43325c42010-09-09 22:42:56 +0000165 /// Flag tracking whether any errors have been encountered.
166 unsigned HadError : 1;
167
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000168 /// The values from the last parsed cpp hash file line comment if any.
169 StringRef CppHashFilename;
170 int64_t CppHashLineNumber;
171 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000172 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000173 /// When generating dwarf for assembly source files we need to calculate the
174 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000175 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000176 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
177 SMLoc LastQueryIDLoc;
178 int LastQueryBuffer;
179 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000180
Devang Patela173ee52012-01-31 18:14:05 +0000181 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
182 unsigned AssemblerDialect;
183
Jim Grosbach4b905842013-09-20 23:08:21 +0000184 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000185 bool IsDarwin;
186
Jim Grosbach4b905842013-09-20 23:08:21 +0000187 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000188 bool ParsingInlineAsm;
189
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000191 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000192 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000193 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194
Craig Topper59be68f2014-03-08 07:14:16 +0000195 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000196
Craig Topper59be68f2014-03-08 07:14:16 +0000197 void addDirectiveHandler(StringRef Directive,
198 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000199 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000200 }
201
202public:
203 /// @name MCAsmParser Interface
204 /// {
205
Craig Topper59be68f2014-03-08 07:14:16 +0000206 SourceMgr &getSourceManager() override { return SrcMgr; }
207 MCAsmLexer &getLexer() override { return Lexer; }
208 MCContext &getContext() override { return Ctx; }
209 MCStreamer &getStreamer() override { return Out; }
210 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000211 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000212 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000213 else
214 return AssemblerDialect;
215 }
Craig Topper59be68f2014-03-08 07:14:16 +0000216 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000217 AssemblerDialect = i;
218 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 void Note(SMLoc L, const Twine &Msg,
221 ArrayRef<SMRange> Ranges = None) override;
222 bool Warning(SMLoc L, const Twine &Msg,
223 ArrayRef<SMRange> Ranges = None) override;
224 bool Error(SMLoc L, const Twine &Msg,
225 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000226
Craig Topper59be68f2014-03-08 07:14:16 +0000227 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000228
Craig Topper59be68f2014-03-08 07:14:16 +0000229 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
230 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000231
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000234 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000235 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000236 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000237 const MCInstrInfo *MII, const MCInstPrinter *IP,
238 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000239
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000240 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
243 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
244 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000247 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000248 bool parseIdentifier(StringRef &Res) override;
249 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000250
Craig Topper59be68f2014-03-08 07:14:16 +0000251 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252 /// }
253
254private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000255
Jim Grosbach4b905842013-09-20 23:08:21 +0000256 bool parseStatement(ParseStatementInfo &Info);
257 void eatToEndOfLine();
258 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000259
Jim Grosbach4b905842013-09-20 23:08:21 +0000260 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000262 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000263 ArrayRef<MCAsmMacroParameter> Parameters,
264 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000265 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000266
Eli Benderskya313ae62013-01-16 18:56:50 +0000267 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
273 /// \brief Lookup a previously defined macro.
274 /// \param Name Macro name.
275 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
281 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000282 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000283
284 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000287 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000288 ///
289 /// \param M The macro.
290 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000292
293 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000294 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000295
David Majnemer91fc4c22014-01-29 18:57:46 +0000296 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000297 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000298
299 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 void printMacroInstantiations();
303 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000304 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000305 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000307 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000308
Jim Grosbach4b905842013-09-20 23:08:21 +0000309 /// \brief Enter the specified file. This returns true on failure.
310 bool enterIncludeFile(const std::string &Filename);
311
312 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000313 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000314 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000315
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000316 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000317 /// current token is not set; clients should ensure Lex() is called
318 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000319 ///
320 /// \param InBuffer If not -1, should be the known buffer id that contains the
321 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000323
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000324 /// \brief Parse up to the end of statement and a return the contents from the
325 /// current token until the end of the statement; the current token on exit
326 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000327 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000328
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000329 /// \brief Parse until the end of a statement or a comma is encountered,
330 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000334 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
337 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
338 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000339
Jim Grosbach4b905842013-09-20 23:08:21 +0000340 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000341
Eli Bendersky17233942013-01-15 22:59:42 +0000342 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000343 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_NO_DIRECTIVE, // Placeholder
345 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000346 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
347 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000348 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000350 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000351 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
352 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
353 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
354 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000355 DK_IF, DK_IFNE, DK_IFB, DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF,
356 DK_IFNDEF, DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000357 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
358 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
359 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
360 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
361 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
362 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000363 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000364 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000366 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000367 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000368 };
369
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000371 /// directives parsed by this class.
372 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000373
374 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
376 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000377 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
379 bool parseDirectiveFill(); // ".fill"
380 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000381 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
383 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000386
Eli Bendersky17233942013-01-15 22:59:42 +0000387 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveFile(SMLoc DirectiveLoc);
389 bool parseDirectiveLine();
390 bool parseDirectiveLoc();
391 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000392
393 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000395 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000396 bool parseDirectiveCFISections();
397 bool parseDirectiveCFIStartProc();
398 bool parseDirectiveCFIEndProc();
399 bool parseDirectiveCFIDefCfaOffset();
400 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIAdjustCfaOffset();
402 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
406 bool parseDirectiveCFIRememberState();
407 bool parseDirectiveCFIRestoreState();
408 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
409 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
410 bool parseDirectiveCFIEscape();
411 bool parseDirectiveCFISignalFrame();
412 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
414 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
416 bool parseDirectiveEndMacro(StringRef Directive);
417 bool parseDirectiveMacro(SMLoc DirectiveLoc);
418 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000419
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000426
Eli Bendersky17233942013-01-15 22:59:42 +0000427 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000429
430 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveAbort(); // ".abort"
440 bool parseDirectiveInclude(); // ".include"
441 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000442
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +0000443 // ".if" or ".ifne"
444 bool parseDirectiveIf(SMLoc DirectiveLoc);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000445 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000447 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000449 // ".ifeqs"
450 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000451 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
453 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
454 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
455 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000456 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000457
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000459 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000460
Rafael Espindola34b9c512012-06-03 23:57:14 +0000461 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
463 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000464 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000465 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000466 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
467 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
468 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000469
Chad Rosierc7f552c2013-02-12 21:33:51 +0000470 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000472 size_t Len);
473
474 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000475 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000476
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000477 // "end"
478 bool parseDirectiveEnd(SMLoc DirectiveLoc);
479
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000480 // ".err" or ".error"
481 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000482
Eli Bendersky17233942013-01-15 22:59:42 +0000483 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000484};
Daniel Dunbar86033402010-07-12 17:54:38 +0000485}
486
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000487namespace llvm {
488
489extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000490extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000491extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000492
493}
494
Chris Lattnerc35681b2010-01-19 19:46:13 +0000495enum { DEFAULT_ADDRSPACE = 0 };
496
Jim Grosbach4b905842013-09-20 23:08:21 +0000497AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
498 const MCAsmInfo &_MAI)
499 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Craig Topper353eda42014-04-24 06:44:33 +0000500 PlatformParser(nullptr), CurBuffer(0), MacrosEnabledFlag(true),
Saleem Abdulrasool9dd60cf2014-05-22 06:02:59 +0000501 HadError(false), CppHashLineNumber(0), AssemblerDialect(~0U),
502 IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000503 // Save the old handler.
504 SavedDiagHandler = SrcMgr.getDiagHandler();
505 SavedDiagContext = SrcMgr.getDiagContext();
506 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000507 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000508 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000509
Daniel Dunbarc5011082010-07-12 18:12:02 +0000510 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
512 case MCObjectFileInfo::IsCOFF:
513 PlatformParser = createCOFFAsmParser();
514 PlatformParser->Initialize(*this);
515 break;
516 case MCObjectFileInfo::IsMachO:
517 PlatformParser = createDarwinAsmParser();
518 PlatformParser->Initialize(*this);
519 IsDarwin = true;
520 break;
521 case MCObjectFileInfo::IsELF:
522 PlatformParser = createELFAsmParser();
523 PlatformParser->Initialize(*this);
524 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000525 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000526
Eli Bendersky17233942013-01-15 22:59:42 +0000527 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000528}
529
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000530AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000531 assert((HadError || ActiveMacros.empty()) &&
532 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000533
534 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000535 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
536 ie = MacroMap.end();
537 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000538 delete it->getValue();
539
Daniel Dunbarc5011082010-07-12 18:12:02 +0000540 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000541}
542
Jim Grosbach4b905842013-09-20 23:08:21 +0000543void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000544 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000545 for (std::vector<MacroInstantiation *>::const_reverse_iterator
546 it = ActiveMacros.rbegin(),
547 ie = ActiveMacros.rend();
548 it != ie; ++it)
549 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000550 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000551}
552
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000553void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
554 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
555 printMacroInstantiations();
556}
557
Chris Lattnera3a06812011-10-16 04:47:35 +0000558bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000559 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000560 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000561 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
562 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000563 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000564}
565
Chris Lattnera3a06812011-10-16 04:47:35 +0000566bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000567 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000568 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
569 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000570 return true;
571}
572
Jim Grosbach4b905842013-09-20 23:08:21 +0000573bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000574 std::string IncludedFile;
575 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 if (NewBuf == -1)
577 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000578
Sean Callanan7a77eae2010-01-21 00:19:58 +0000579 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000580
Sean Callanan7a77eae2010-01-21 00:19:58 +0000581 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000582
Sean Callanan7a77eae2010-01-21 00:19:58 +0000583 return false;
584}
Daniel Dunbar43235712010-07-18 18:54:11 +0000585
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000586/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000587/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000589bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000590 std::string IncludedFile;
591 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
592 if (NewBuf == -1)
593 return true;
594
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000595 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000596 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000597 return false;
598}
599
Jim Grosbach4b905842013-09-20 23:08:21 +0000600void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000601 if (InBuffer != -1) {
602 CurBuffer = InBuffer;
603 } else {
604 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
605 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000606 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
607}
608
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609const AsmToken &AsmParser::Lex() {
610 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000611
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 if (tok->is(AsmToken::Eof)) {
613 // If this is the end of an included file, pop the parent file off the
614 // include stack.
615 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
616 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000617 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000618 tok = &Lexer.Lex();
619 }
620 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000621
Sean Callanan7a77eae2010-01-21 00:19:58 +0000622 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000623 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000624
Sean Callanan7a77eae2010-01-21 00:19:58 +0000625 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000626}
627
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000628bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000629 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000630 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000631 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000632
Chris Lattner36e02122009-06-21 20:54:55 +0000633 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000634 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000635
636 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000637 AsmCond StartingCondState = TheCondState;
638
Kevin Enderby6469fc22011-11-01 22:27:22 +0000639 // If we are generating dwarf for assembly source files save the initial text
640 // section and generate a .file directive.
641 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000642 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000643 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
644 getStreamer().EmitLabel(SectionStartSym);
645 getContext().setGenDwarfSectionStartSym(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000646 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
647 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000648 }
649
Chris Lattner73f36112009-07-02 21:53:43 +0000650 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000651 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000652 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000653 if (!parseStatement(Info))
654 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000655
Daniel Dunbar43325c42010-09-09 22:42:56 +0000656 // We had an error, validate that one was emitted and recover by skipping to
657 // the next line.
658 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000659 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000660 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000661
662 if (TheCondState.TheCond != StartingCondState.TheCond ||
663 TheCondState.Ignore != StartingCondState.Ignore)
664 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000665
666 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000667 const auto &LineTables = getContext().getMCDwarfLineTables();
668 if (!LineTables.empty()) {
669 unsigned Index = 0;
670 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
671 if (File.Name.empty() && Index != 0)
672 TokError("unassigned file number: " + Twine(Index) +
673 " for .file directives");
674 ++Index;
675 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000676 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000677
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000678 // Check to see that all assembler local symbols were actually defined.
679 // Targets that don't do subsections via symbols may not want this, though,
680 // so conservatively exclude them. Only do this if we're finalizing, though,
681 // as otherwise we won't necessarilly have seen everything yet.
682 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
683 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
684 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000685 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000686 i != e; ++i) {
687 MCSymbol *Sym = i->getValue();
688 // Variable symbols may not be marked as defined, so check those
689 // explicitly. If we know it's a variable, we have a definition for
690 // the purposes of this check.
691 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
692 // FIXME: We would really like to refer back to where the symbol was
693 // first referenced for a source location. We need to add something
694 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000695 printMessage(
696 getLexer().getLoc(), SourceMgr::DK_Error,
697 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000698 }
699 }
700
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000701 // Finalize the output stream if there are no errors and if the client wants
702 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000703 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000704 Out.Finish();
705
Chris Lattner73f36112009-07-02 21:53:43 +0000706 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000707}
708
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000709void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000710 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000711 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000712 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000713 }
714}
715
Jim Grosbach4b905842013-09-20 23:08:21 +0000716/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000717void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000718 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000719 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000720
Chris Lattnere5074c42009-06-22 01:29:09 +0000721 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000722 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000723 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000724}
725
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000726StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000727 const char *Start = getTok().getLoc().getPointer();
728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000730 Lex();
731
732 const char *End = getTok().getLoc().getPointer();
733 return StringRef(Start, End - Start);
734}
Chris Lattner78db3622009-06-22 05:51:26 +0000735
Jim Grosbach4b905842013-09-20 23:08:21 +0000736StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000737 const char *Start = getTok().getLoc().getPointer();
738
739 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000740 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000741 Lex();
742
743 const char *End = getTok().getLoc().getPointer();
744 return StringRef(Start, End - Start);
745}
746
Jim Grosbach4b905842013-09-20 23:08:21 +0000747/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000748/// NOTE: This assumes the leading '(' has already been consumed.
749///
750/// parenexpr ::= expr)
751///
Jim Grosbach4b905842013-09-20 23:08:21 +0000752bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
753 if (parseExpression(Res))
754 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000755 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000756 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000757 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000758 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000759 return false;
760}
Chris Lattner78db3622009-06-22 05:51:26 +0000761
Jim Grosbach4b905842013-09-20 23:08:21 +0000762/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763/// NOTE: This assumes the leading '[' has already been consumed.
764///
765/// bracketexpr ::= expr]
766///
Jim Grosbach4b905842013-09-20 23:08:21 +0000767bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
768 if (parseExpression(Res))
769 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000770 if (Lexer.isNot(AsmToken::RBrac))
771 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000772 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000773 Lex();
774 return false;
775}
776
Jim Grosbach4b905842013-09-20 23:08:21 +0000777/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000778/// primaryexpr ::= (parenexpr
779/// primaryexpr ::= symbol
780/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000781/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000782/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000783bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000784 SMLoc FirstTokenLoc = getLexer().getLoc();
785 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
786 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000787 default:
788 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000789 // If we have an error assume that we've already handled it.
790 case AsmToken::Error:
791 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000792 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000793 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000794 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000795 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000796 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000797 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000798 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000799 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000800 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000801 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000802 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000803 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000804 if (FirstTokenKind == AsmToken::Dollar) {
805 if (Lexer.getMAI().getDollarIsPC()) {
806 // This is a '$' reference, which references the current PC. Emit a
807 // temporary label to the streamer and refer to it.
808 MCSymbol *Sym = Ctx.CreateTempSymbol();
809 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000810 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
811 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000812 EndLoc = FirstTokenLoc;
813 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000814 }
815 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000816 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000817 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000818 // Parse symbol variant
819 std::pair<StringRef, StringRef> Split;
820 if (!MAI.useParensForSymbolVariant()) {
821 Split = Identifier.split('@');
822 } else if (Lexer.is(AsmToken::LParen)) {
823 Lexer.Lex(); // eat (
824 StringRef VName;
825 parseIdentifier(VName);
826 if (Lexer.isNot(AsmToken::RParen)) {
827 return Error(Lexer.getTok().getLoc(),
828 "unexpected token in variant, expected ')'");
829 }
830 Lexer.Lex(); // eat )
831 Split = std::make_pair(Identifier, VName);
832 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000833
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000834 EndLoc = SMLoc::getFromPointer(Identifier.end());
835
Daniel Dunbard20cda02009-10-16 01:34:54 +0000836 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000837 StringRef SymbolName = Identifier;
838 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000839
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000840 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000841 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000842 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000843 if (Variant != MCSymbolRefExpr::VK_Invalid) {
844 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000845 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 Variant = MCSymbolRefExpr::VK_None;
847 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000848 return Error(SMLoc::getFromPointer(Split.second.begin()),
849 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000850 }
851 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000852
Hans Wennborgce69d772013-10-18 20:46:28 +0000853 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
854
Daniel Dunbard20cda02009-10-16 01:34:54 +0000855 // If this is an absolute variable reference, substitute it now to preserve
856 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000857 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000858 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000859 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000860
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000861 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000862 return false;
863 }
864
865 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000866 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000867 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000868 }
David Woodhousef42a6662014-02-01 16:20:54 +0000869 case AsmToken::BigNum:
870 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000871 case AsmToken::Integer: {
872 SMLoc Loc = getTok().getLoc();
873 int64_t IntVal = getTok().getIntVal();
874 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000875 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000876 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000877 // Look for 'b' or 'f' following an Integer as a directional label
878 if (Lexer.getKind() == AsmToken::Identifier) {
879 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000880 // Lookup the symbol variant if used.
881 std::pair<StringRef, StringRef> Split = IDVal.split('@');
882 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
883 if (Split.first.size() != IDVal.size()) {
884 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000885 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000886 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000887 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000889 if (IDVal == "f" || IDVal == "b") {
890 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000891 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000892 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000893 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000894 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000895 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000896 Lex(); // Eat identifier.
897 }
898 }
Chris Lattner78db3622009-06-22 05:51:26 +0000899 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000900 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000901 case AsmToken::Real: {
902 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000903 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000904 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000905 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000906 Lex(); // Eat token.
907 return false;
908 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000909 case AsmToken::Dot: {
910 // This is a '.' reference, which references the current PC. Emit a
911 // temporary label to the streamer and refer to it.
912 MCSymbol *Sym = Ctx.CreateTempSymbol();
913 Out.EmitLabel(Sym);
914 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000915 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000916 Lex(); // Eat identifier.
917 return false;
918 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000919 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000920 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000921 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000922 case AsmToken::LBrac:
923 if (!PlatformParser->HasBracketExpressions())
924 return TokError("brackets expression not supported on this target");
925 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000931 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000933 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000934 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000935 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000937 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000939 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000940 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000941 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000943 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000945 }
946}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000947
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000948bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000949 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000950 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000951}
952
Daniel Dunbar55f16672010-09-17 02:47:07 +0000953const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000954AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000955 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000956 // Ask the target implementation about this expression first.
957 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
958 if (NewE)
959 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000960 // Recurse over the given expression, rebuilding it to apply the given variant
961 // if there is exactly one symbol.
962 switch (E->getKind()) {
963 case MCExpr::Target:
964 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000965 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966
967 case MCExpr::SymbolRef: {
968 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
969
970 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000971 TokError("invalid variant on expression '" + getTok().getIdentifier() +
972 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000973 return E;
974 }
975
976 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
977 }
978
979 case MCExpr::Unary: {
980 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000981 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000982 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000983 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
985 }
986
987 case MCExpr::Binary: {
988 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
990 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000991
992 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000993 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994
Jim Grosbach4b905842013-09-20 23:08:21 +0000995 if (!LHS)
996 LHS = BE->getLHS();
997 if (!RHS)
998 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
1000 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1001 }
1002 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001003
Craig Toppera2886c22012-02-07 05:05:23 +00001004 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001005}
1006
Jim Grosbach4b905842013-09-20 23:08:21 +00001007/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001008///
Jim Grosbachbd164242011-08-20 16:24:13 +00001009/// expr ::= expr &&,|| expr -> lowest.
1010/// expr ::= expr |,^,&,! expr
1011/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1012/// expr ::= expr <<,>> expr
1013/// expr ::= expr +,- expr
1014/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001015/// expr ::= primaryexpr
1016///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001017bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001018 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001019 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001020 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001021 return true;
1022
Daniel Dunbar55f16672010-09-17 02:47:07 +00001023 // As a special case, we support 'a op b @ modifier' by rewriting the
1024 // expression to include the modifier. This is inefficient, but in general we
1025 // expect users to use 'a@modifier op b'.
1026 if (Lexer.getKind() == AsmToken::At) {
1027 Lex();
1028
1029 if (Lexer.isNot(AsmToken::Identifier))
1030 return TokError("unexpected symbol modifier following '@'");
1031
1032 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001033 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 if (Variant == MCSymbolRefExpr::VK_Invalid)
1035 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1036
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 if (!ModifiedRes) {
1039 return TokError("invalid modifier '" + getTok().getIdentifier() +
1040 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001042
Daniel Dunbar55f16672010-09-17 02:47:07 +00001043 Res = ModifiedRes;
1044 Lex();
1045 }
1046
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001047 // Try to constant fold it up front, if possible.
1048 int64_t Value;
1049 if (Res->EvaluateAsAbsolute(Value))
1050 Res = MCConstantExpr::Create(Value, getContext());
1051
1052 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001053}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001054
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001055bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001056 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001057 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001058}
1059
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001060bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001061 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001062
Daniel Dunbar75630b32009-06-30 02:10:03 +00001063 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001064 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001065 return true;
1066
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001067 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001068 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069
1070 return false;
1071}
1072
Michael J. Spencer530ce852010-10-09 11:00:50 +00001073static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001075 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001076 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001077 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001078
Jim Grosbach4b905842013-09-20 23:08:21 +00001079 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001082 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001085 return 1;
1086
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 // Low Precedence: |, &, ^
1088 //
1089 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001096 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001097 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001099
Jim Grosbach4b905842013-09-20 23:08:21 +00001100 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001101 case AsmToken::EqualEqual:
1102 Kind = MCBinaryExpr::EQ;
1103 return 3;
1104 case AsmToken::ExclaimEqual:
1105 case AsmToken::LessGreater:
1106 Kind = MCBinaryExpr::NE;
1107 return 3;
1108 case AsmToken::Less:
1109 Kind = MCBinaryExpr::LT;
1110 return 3;
1111 case AsmToken::LessEqual:
1112 Kind = MCBinaryExpr::LTE;
1113 return 3;
1114 case AsmToken::Greater:
1115 Kind = MCBinaryExpr::GT;
1116 return 3;
1117 case AsmToken::GreaterEqual:
1118 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001119 return 3;
1120
Jim Grosbach4b905842013-09-20 23:08:21 +00001121 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001122 case AsmToken::LessLess:
1123 Kind = MCBinaryExpr::Shl;
1124 return 4;
1125 case AsmToken::GreaterGreater:
1126 Kind = MCBinaryExpr::Shr;
1127 return 4;
1128
Jim Grosbach4b905842013-09-20 23:08:21 +00001129 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130 case AsmToken::Plus:
1131 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001132 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133 case AsmToken::Minus:
1134 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001135 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001136
Jim Grosbach4b905842013-09-20 23:08:21 +00001137 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001144 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001146 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001147 }
1148}
1149
Jim Grosbach4b905842013-09-20 23:08:21 +00001150/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001152bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001153 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001155 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001156 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // If the next token is lower precedence than we are allowed to eat, return
1159 // successfully with what we ate already.
1160 if (TokPrec < Precedence)
1161 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Sean Callanan686ed8d2010-01-19 20:22:31 +00001163 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001164
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001165 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001166 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001167 if (parsePrimaryExpr(RHS, EndLoc))
1168 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001169
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170 // If BinOp binds less tightly with RHS than the operator after RHS, let
1171 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001172 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001173 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001174 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1175 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001176
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001177 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001178 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001179 }
1180}
1181
Chris Lattner36e02122009-06-21 20:54:55 +00001182/// ParseStatement:
1183/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001184/// ::= Label* Directive ...Operands... EndOfStatement
1185/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001186bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001187 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001188 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001189 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001190 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001191 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001192
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001193 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001194 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001195 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001196 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001197 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001198 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001199 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001200 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001201
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001202 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001203 if (Lexer.is(AsmToken::Integer)) {
1204 LocalLabelVal = getTok().getIntVal();
1205 if (LocalLabelVal < 0) {
1206 if (!TheCondState.Ignore)
1207 return TokError("unexpected token at start of statement");
1208 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001209 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001210 IDVal = getTok().getString();
1211 Lex(); // Consume the integer token to be used as an identifier token.
1212 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001213 if (!TheCondState.Ignore)
1214 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001215 }
1216 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001217 } else if (Lexer.is(AsmToken::Dot)) {
1218 // Treat '.' as a valid identifier in this context.
1219 Lex();
1220 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001221 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001222 if (!TheCondState.Ignore)
1223 return TokError("unexpected token at start of statement");
1224 IDVal = "";
1225 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001226
Chris Lattner926885c2010-04-17 18:14:27 +00001227 // Handle conditional assembly here before checking for skipping. We
1228 // have to do this so that .endif isn't skipped in a ".if 0" block for
1229 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001230 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001231 DirectiveKindMap.find(IDVal);
1232 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1233 ? DK_NO_DIRECTIVE
1234 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001235 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001236 default:
1237 break;
1238 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001239 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 return parseDirectiveIf(IDLoc);
1241 case DK_IFB:
1242 return parseDirectiveIfb(IDLoc, true);
1243 case DK_IFNB:
1244 return parseDirectiveIfb(IDLoc, false);
1245 case DK_IFC:
1246 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001247 case DK_IFEQS:
1248 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001249 case DK_IFNC:
1250 return parseDirectiveIfc(IDLoc, false);
1251 case DK_IFDEF:
1252 return parseDirectiveIfdef(IDLoc, true);
1253 case DK_IFNDEF:
1254 case DK_IFNOTDEF:
1255 return parseDirectiveIfdef(IDLoc, false);
1256 case DK_ELSEIF:
1257 return parseDirectiveElseIf(IDLoc);
1258 case DK_ELSE:
1259 return parseDirectiveElse(IDLoc);
1260 case DK_ENDIF:
1261 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001262 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001263
Eli Bendersky88024712013-01-16 19:32:36 +00001264 // Ignore the statement if in the middle of inactive conditional
1265 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001266 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001267 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001268 return false;
1269 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001270
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001271 // FIXME: Recurse on local labels?
1272
1273 // See what kind of statement we have.
1274 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001275 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001276 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001277
Chris Lattner36e02122009-06-21 20:54:55 +00001278 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001279 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001280
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001281 // Diagnose attempt to use '.' as a label.
1282 if (IDVal == ".")
1283 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1284
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001285 // Diagnose attempt to use a variable as a label.
1286 //
1287 // FIXME: Diagnostics. Note the location of the definition as a label.
1288 // FIXME: This doesn't diagnose assignment to a symbol which has been
1289 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001290 MCSymbol *Sym;
1291 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001292 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001293 else
1294 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001295 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001296 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001297
Daniel Dunbare73b2672009-08-26 22:13:22 +00001298 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001299 if (!ParsingInlineAsm)
1300 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001301
Kevin Enderbye7739d42011-12-09 18:09:40 +00001302 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001303 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001304 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001305 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1306 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001307
Tim Northover1744d0a2013-10-25 12:49:50 +00001308 getTargetParser().onLabelParsed(Sym);
1309
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001310 // Consume any end of statement token, if present, to avoid spurious
1311 // AddBlankLine calls().
1312 if (Lexer.is(AsmToken::EndOfStatement)) {
1313 Lex();
1314 if (Lexer.is(AsmToken::Eof))
1315 return false;
1316 }
1317
Eli Friedman0f4871d2012-10-22 23:58:19 +00001318 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001319 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001320
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001321 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001322 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001323 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001324
Jim Grosbach4b905842013-09-20 23:08:21 +00001325 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001326
1327 default: // Normal instruction or directive.
1328 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001329 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001330
1331 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001332 if (areMacrosEnabled())
1333 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1334 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001335 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001336
Michael J. Spencer530ce852010-10-09 11:00:50 +00001337 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001338
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001340 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001341 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001342 //
Eli Bendersky17233942013-01-15 22:59:42 +00001343 // 1. The target-specific assembly parser. Some directives are target
1344 // specific or may potentially behave differently on certain targets.
1345 // 2. Asm parser extensions. For example, platform-specific parsers
1346 // (like the ELF parser) register themselves as extensions.
1347 // 3. The generic directive parser implemented by this class. These are
1348 // all the directives that behave in a target and platform independent
1349 // manner, or at least have a default behavior that's shared between
1350 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001351
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // First query the target-specific parser. It will return 'true' if it
1353 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001354 if (!getTargetParser().ParseDirective(ID))
1355 return false;
1356
Alp Tokercb402912014-01-24 17:20:08 +00001357 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001358 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001359 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1360 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001361 if (Handler.first)
1362 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1363
1364 // Finally, if no one else is interested in this directive, it must be
1365 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001366 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001367 default:
1368 break;
1369 case DK_SET:
1370 case DK_EQU:
1371 return parseDirectiveSet(IDVal, true);
1372 case DK_EQUIV:
1373 return parseDirectiveSet(IDVal, false);
1374 case DK_ASCII:
1375 return parseDirectiveAscii(IDVal, false);
1376 case DK_ASCIZ:
1377 case DK_STRING:
1378 return parseDirectiveAscii(IDVal, true);
1379 case DK_BYTE:
1380 return parseDirectiveValue(1);
1381 case DK_SHORT:
1382 case DK_VALUE:
1383 case DK_2BYTE:
1384 return parseDirectiveValue(2);
1385 case DK_LONG:
1386 case DK_INT:
1387 case DK_4BYTE:
1388 return parseDirectiveValue(4);
1389 case DK_QUAD:
1390 case DK_8BYTE:
1391 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001392 case DK_OCTA:
1393 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001394 case DK_SINGLE:
1395 case DK_FLOAT:
1396 return parseDirectiveRealValue(APFloat::IEEEsingle);
1397 case DK_DOUBLE:
1398 return parseDirectiveRealValue(APFloat::IEEEdouble);
1399 case DK_ALIGN: {
1400 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1401 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1402 }
1403 case DK_ALIGN32: {
1404 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1405 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1406 }
1407 case DK_BALIGN:
1408 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1409 case DK_BALIGNW:
1410 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1411 case DK_BALIGNL:
1412 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1413 case DK_P2ALIGN:
1414 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1415 case DK_P2ALIGNW:
1416 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1417 case DK_P2ALIGNL:
1418 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1419 case DK_ORG:
1420 return parseDirectiveOrg();
1421 case DK_FILL:
1422 return parseDirectiveFill();
1423 case DK_ZERO:
1424 return parseDirectiveZero();
1425 case DK_EXTERN:
1426 eatToEndOfStatement(); // .extern is the default, ignore it.
1427 return false;
1428 case DK_GLOBL:
1429 case DK_GLOBAL:
1430 return parseDirectiveSymbolAttribute(MCSA_Global);
1431 case DK_LAZY_REFERENCE:
1432 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1433 case DK_NO_DEAD_STRIP:
1434 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1435 case DK_SYMBOL_RESOLVER:
1436 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1437 case DK_PRIVATE_EXTERN:
1438 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1439 case DK_REFERENCE:
1440 return parseDirectiveSymbolAttribute(MCSA_Reference);
1441 case DK_WEAK_DEFINITION:
1442 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1443 case DK_WEAK_REFERENCE:
1444 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1445 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1446 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1447 case DK_COMM:
1448 case DK_COMMON:
1449 return parseDirectiveComm(/*IsLocal=*/false);
1450 case DK_LCOMM:
1451 return parseDirectiveComm(/*IsLocal=*/true);
1452 case DK_ABORT:
1453 return parseDirectiveAbort();
1454 case DK_INCLUDE:
1455 return parseDirectiveInclude();
1456 case DK_INCBIN:
1457 return parseDirectiveIncbin();
1458 case DK_CODE16:
1459 case DK_CODE16GCC:
1460 return TokError(Twine(IDVal) + " not supported yet");
1461 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001462 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001463 case DK_IRP:
1464 return parseDirectiveIrp(IDLoc);
1465 case DK_IRPC:
1466 return parseDirectiveIrpc(IDLoc);
1467 case DK_ENDR:
1468 return parseDirectiveEndr(IDLoc);
1469 case DK_BUNDLE_ALIGN_MODE:
1470 return parseDirectiveBundleAlignMode();
1471 case DK_BUNDLE_LOCK:
1472 return parseDirectiveBundleLock();
1473 case DK_BUNDLE_UNLOCK:
1474 return parseDirectiveBundleUnlock();
1475 case DK_SLEB128:
1476 return parseDirectiveLEB128(true);
1477 case DK_ULEB128:
1478 return parseDirectiveLEB128(false);
1479 case DK_SPACE:
1480 case DK_SKIP:
1481 return parseDirectiveSpace(IDVal);
1482 case DK_FILE:
1483 return parseDirectiveFile(IDLoc);
1484 case DK_LINE:
1485 return parseDirectiveLine();
1486 case DK_LOC:
1487 return parseDirectiveLoc();
1488 case DK_STABS:
1489 return parseDirectiveStabs();
1490 case DK_CFI_SECTIONS:
1491 return parseDirectiveCFISections();
1492 case DK_CFI_STARTPROC:
1493 return parseDirectiveCFIStartProc();
1494 case DK_CFI_ENDPROC:
1495 return parseDirectiveCFIEndProc();
1496 case DK_CFI_DEF_CFA:
1497 return parseDirectiveCFIDefCfa(IDLoc);
1498 case DK_CFI_DEF_CFA_OFFSET:
1499 return parseDirectiveCFIDefCfaOffset();
1500 case DK_CFI_ADJUST_CFA_OFFSET:
1501 return parseDirectiveCFIAdjustCfaOffset();
1502 case DK_CFI_DEF_CFA_REGISTER:
1503 return parseDirectiveCFIDefCfaRegister(IDLoc);
1504 case DK_CFI_OFFSET:
1505 return parseDirectiveCFIOffset(IDLoc);
1506 case DK_CFI_REL_OFFSET:
1507 return parseDirectiveCFIRelOffset(IDLoc);
1508 case DK_CFI_PERSONALITY:
1509 return parseDirectiveCFIPersonalityOrLsda(true);
1510 case DK_CFI_LSDA:
1511 return parseDirectiveCFIPersonalityOrLsda(false);
1512 case DK_CFI_REMEMBER_STATE:
1513 return parseDirectiveCFIRememberState();
1514 case DK_CFI_RESTORE_STATE:
1515 return parseDirectiveCFIRestoreState();
1516 case DK_CFI_SAME_VALUE:
1517 return parseDirectiveCFISameValue(IDLoc);
1518 case DK_CFI_RESTORE:
1519 return parseDirectiveCFIRestore(IDLoc);
1520 case DK_CFI_ESCAPE:
1521 return parseDirectiveCFIEscape();
1522 case DK_CFI_SIGNAL_FRAME:
1523 return parseDirectiveCFISignalFrame();
1524 case DK_CFI_UNDEFINED:
1525 return parseDirectiveCFIUndefined(IDLoc);
1526 case DK_CFI_REGISTER:
1527 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001528 case DK_CFI_WINDOW_SAVE:
1529 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001530 case DK_MACROS_ON:
1531 case DK_MACROS_OFF:
1532 return parseDirectiveMacrosOnOff(IDVal);
1533 case DK_MACRO:
1534 return parseDirectiveMacro(IDLoc);
1535 case DK_ENDM:
1536 case DK_ENDMACRO:
1537 return parseDirectiveEndMacro(IDVal);
1538 case DK_PURGEM:
1539 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001540 case DK_END:
1541 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001542 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001543 return parseDirectiveError(IDLoc, false);
1544 case DK_ERROR:
1545 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001546 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001547
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001548 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001549 }
Chris Lattner36e02122009-06-21 20:54:55 +00001550
Chad Rosierc7f552c2013-02-12 21:33:51 +00001551 // __asm _emit or __asm __emit
1552 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1553 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001554 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001555
1556 // __asm align
1557 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001558 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001559
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001560 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001561
Chris Lattner7cbfa442010-05-19 23:34:33 +00001562 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001563 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001564 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001565 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001566 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001567 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001568
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001569 // Dump the parsed representation, if requested.
1570 if (getShowParsedOperands()) {
1571 SmallString<256> Str;
1572 raw_svector_ostream OS(Str);
1573 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001574 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001575 if (i != 0)
1576 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001577 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001578 }
1579 OS << "]";
1580
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001582 }
1583
Kevin Enderby6469fc22011-11-01 22:27:22 +00001584 // If we are generating dwarf for assembly source files and the current
1585 // section is the initial text section then generate a .loc directive for
1586 // the instruction.
1587 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001588 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001589 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001590
Eli Bendersky88024712013-01-16 19:32:36 +00001591 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001592
Eli Bendersky88024712013-01-16 19:32:36 +00001593 // If we previously parsed a cpp hash file line comment then make sure the
1594 // current Dwarf File is for the CppHashFilename if not then emit the
1595 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001596 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001597 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1598 0, StringRef(), CppHashFilename);
1599 getContext().setGenDwarfFileNumber(FileNumber);
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;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001625 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1626 Info.ParsedOperands, Out,
1627 ErrorInfo, 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
Craig Topper353eda42014-04-24 06:44:33 +00001707 Diag.print(nullptr, 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
Craig Topper353eda42014-04-24 06:44:33 +00001729 NewDiag.print(nullptr, 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();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001745 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001746 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001747 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001748
Preston Gurd05500642012-09-19 20:36:12 +00001749 // A macro without parameters is handled differently on Darwin:
1750 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001751 while (!Body.empty()) {
1752 // Scan for the next substitution.
1753 std::size_t End = Body.size(), Pos = 0;
1754 for (; Pos != End; ++Pos) {
1755 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001756 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001757 // This macro has no parameters, look for $0, $1, etc.
1758 if (Body[Pos] != '$' || Pos + 1 == End)
1759 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001760
Rafael Espindola1134ab232011-06-05 02:43:45 +00001761 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001762 if (Next == '$' || Next == 'n' ||
1763 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 break;
1765 } else {
1766 // This macro has parameters, look for \foo, \bar, etc.
1767 if (Body[Pos] == '\\' && Pos + 1 != End)
1768 break;
1769 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001770 }
1771
1772 // Add the prefix.
1773 OS << Body.slice(0, Pos);
1774
1775 // Check if we reached the end.
1776 if (Pos == End)
1777 break;
1778
Benjamin Kramer513e7442014-02-20 13:36:32 +00001779 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 switch (Body[Pos + 1]) {
1781 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 case '$':
1783 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001784 break;
1785
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 case 'n':
1788 OS << A.size();
1789 break;
1790
Jim Grosbach4b905842013-09-20 23:08:21 +00001791 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 default: {
1793 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001794 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 if (Index >= A.size())
1796 break;
1797
1798 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001799 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 ie = A[Index].end();
1801 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 OS << it->getString();
1803 break;
1804 }
1805 }
1806 Pos += 2;
1807 } else {
1808 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001809 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001810 ++I;
1811
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 const char *Begin = Body.data() + Pos + 1;
1813 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001814 unsigned Index = 0;
1815 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001816 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001817 break;
1818
Preston Gurd05500642012-09-19 20:36:12 +00001819 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1821 Pos += 3;
1822 else {
1823 OS << '\\' << Argument;
1824 Pos = I;
1825 }
Preston Gurd05500642012-09-19 20:36:12 +00001826 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001827 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001828 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001829 ie = A[Index].end();
1830 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001831 // We expect no quotes around the string's contents when
1832 // parsing for varargs.
1833 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001834 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001835 else
1836 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001837
Preston Gurd05500642012-09-19 20:36:12 +00001838 Pos += 1 + Argument.size();
1839 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001840 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001841 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001842 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001843 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001844
Rafael Espindola1134ab232011-06-05 02:43:45 +00001845 return false;
1846}
Daniel Dunbar43235712010-07-18 18:54:11 +00001847
Jim Grosbach4b905842013-09-20 23:08:21 +00001848MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1849 SMLoc EL, MemoryBuffer *I)
1850 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1851 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001852
Jim Grosbach4b905842013-09-20 23:08:21 +00001853static bool isOperator(AsmToken::TokenKind kind) {
1854 switch (kind) {
1855 default:
1856 return false;
1857 case AsmToken::Plus:
1858 case AsmToken::Minus:
1859 case AsmToken::Tilde:
1860 case AsmToken::Slash:
1861 case AsmToken::Star:
1862 case AsmToken::Dot:
1863 case AsmToken::Equal:
1864 case AsmToken::EqualEqual:
1865 case AsmToken::Pipe:
1866 case AsmToken::PipePipe:
1867 case AsmToken::Caret:
1868 case AsmToken::Amp:
1869 case AsmToken::AmpAmp:
1870 case AsmToken::Exclaim:
1871 case AsmToken::ExclaimEqual:
1872 case AsmToken::Percent:
1873 case AsmToken::Less:
1874 case AsmToken::LessEqual:
1875 case AsmToken::LessLess:
1876 case AsmToken::LessGreater:
1877 case AsmToken::Greater:
1878 case AsmToken::GreaterEqual:
1879 case AsmToken::GreaterGreater:
1880 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001881 }
1882}
1883
David Majnemer16252452014-01-29 00:07:39 +00001884namespace {
1885class AsmLexerSkipSpaceRAII {
1886public:
1887 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1888 Lexer.setSkipSpace(SkipSpace);
1889 }
1890
1891 ~AsmLexerSkipSpaceRAII() {
1892 Lexer.setSkipSpace(true);
1893 }
1894
1895private:
1896 AsmLexer &Lexer;
1897};
1898}
1899
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001900bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1901
1902 if (Vararg) {
1903 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1904 StringRef Str = parseStringToEndOfStatement();
1905 MA.push_back(AsmToken(AsmToken::String, Str));
1906 }
1907 return false;
1908 }
1909
Rafael Espindola768b41c2012-06-15 14:02:34 +00001910 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001911 unsigned AddTokens = 0;
1912
David Majnemer16252452014-01-29 00:07:39 +00001913 // Darwin doesn't use spaces to delmit arguments.
1914 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001915
1916 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001917 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001918 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001919
David Majnemer91fc4c22014-01-29 18:57:46 +00001920 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001921 break;
Preston Gurd05500642012-09-19 20:36:12 +00001922
1923 if (Lexer.is(AsmToken::Space)) {
1924 Lex(); // Eat spaces
1925
1926 // Spaces can delimit parameters, but could also be part an expression.
1927 // If the token after a space is an operator, add the token and the next
1928 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001929 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001930 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001931 // Check to see whether the token is used as an operator,
1932 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001933 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001934 if (*NextChar == ' ')
1935 AddTokens = 2;
1936 }
1937
1938 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001939 break;
1940 }
1941 }
1942 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001943
Jim Grosbach4b905842013-09-20 23:08:21 +00001944 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001945 // to be able to fill in the remaining default parameter values
1946 if (Lexer.is(AsmToken::EndOfStatement))
1947 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948
1949 // Adjust the current parentheses level.
1950 if (Lexer.is(AsmToken::LParen))
1951 ++ParenLevel;
1952 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1953 --ParenLevel;
1954
1955 // Append the token to the current argument list.
1956 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001957 if (AddTokens)
1958 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959 Lex();
1960 }
Preston Gurd05500642012-09-19 20:36:12 +00001961
Rafael Espindola768b41c2012-06-15 14:02:34 +00001962 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001963 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964 return false;
1965}
1966
1967// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001968bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001969 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001970 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001971 bool NamedParametersFound = false;
1972 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001973
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001974 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001975 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001976
Rafael Espindola768b41c2012-06-15 14:02:34 +00001977 // Parse two kinds of macro invocations:
1978 // - macros defined without any parameters accept an arbitrary number of them
1979 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001980 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001981 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1982 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001983 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001984 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001985
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001987 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001988 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001989 eatToEndOfStatement();
1990 return true;
1991 }
1992
1993 if (!Lexer.is(AsmToken::Equal)) {
1994 TokError("expected '=' after formal parameter identifier");
1995 eatToEndOfStatement();
1996 return true;
1997 }
1998 Lex();
1999
2000 NamedParametersFound = true;
2001 }
2002
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002003 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002004 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002005 eatToEndOfStatement();
2006 return true;
2007 }
2008
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002009 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2010 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002011 return true;
2012
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002013 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002014 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002015 unsigned FAI = 0;
2016 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002017 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002018 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002019
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002020 if (FAI >= NParameters) {
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002021 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002022 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002023 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002024 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 return true;
2026 }
2027 PI = FAI;
2028 }
2029
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002030 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 if (A.size() <= PI)
2032 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002033 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002034
2035 if (FALocs.size() <= PI)
2036 FALocs.resize(PI + 1);
2037
2038 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002039 }
Jim Grosbach206661622012-07-30 22:44:17 +00002040
Preston Gurd242ed3152012-09-19 20:29:04 +00002041 // At the end of the statement, fill in remaining arguments that have
2042 // default values. If there aren't any, then the next argument is
2043 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002044 if (Lexer.is(AsmToken::EndOfStatement)) {
2045 bool Failure = false;
2046 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2047 if (A[FAI].empty()) {
2048 if (M->Parameters[FAI].Required) {
2049 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2050 "missing value for required parameter "
2051 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2052 Failure = true;
2053 }
2054
2055 if (!M->Parameters[FAI].Value.empty())
2056 A[FAI] = M->Parameters[FAI].Value;
2057 }
2058 }
2059 return Failure;
2060 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002061
2062 if (Lexer.is(AsmToken::Comma))
2063 Lex();
2064 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002065
2066 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002067}
2068
Jim Grosbach4b905842013-09-20 23:08:21 +00002069const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2070 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002071 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002072}
2073
Jim Grosbach4b905842013-09-20 23:08:21 +00002074void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002075 MacroMap[Name] = new MCAsmMacro(Macro);
2076}
2077
Jim Grosbach4b905842013-09-20 23:08:21 +00002078void AsmParser::undefineMacro(StringRef Name) {
2079 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002080 if (I != MacroMap.end()) {
2081 delete I->getValue();
2082 MacroMap.erase(I);
2083 }
2084}
2085
Jim Grosbach4b905842013-09-20 23:08:21 +00002086bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002087 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2088 // this, although we should protect against infinite loops.
2089 if (ActiveMacros.size() == 20)
2090 return TokError("macros cannot be nested more than 20 levels deep");
2091
Eli Bendersky38274122013-01-14 23:22:36 +00002092 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002093 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002094 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002095
Rafael Espindola1134ab232011-06-05 02:43:45 +00002096 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2097 // to hold the macro body with substitutions.
2098 SmallString<256> Buf;
2099 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002100 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002101
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002102 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002103 return true;
2104
Eli Bendersky38274122013-01-14 23:22:36 +00002105 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002106 // instantiation.
2107 OS << ".endmacro\n";
2108
Rafael Espindola1134ab232011-06-05 02:43:45 +00002109 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002110 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002111
Daniel Dunbar43235712010-07-18 18:54:11 +00002112 // Create the macro instantiation object and add to the current macro
2113 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002114 MacroInstantiation *MI = new MacroInstantiation(
2115 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002116 ActiveMacros.push_back(MI);
2117
2118 // Jump to the macro instantiation and prime the lexer.
2119 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2120 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2121 Lex();
2122
2123 return false;
2124}
2125
Jim Grosbach4b905842013-09-20 23:08:21 +00002126void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002127 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002128 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002129 Lex();
2130
2131 // Pop the instantiation entry.
2132 delete ActiveMacros.back();
2133 ActiveMacros.pop_back();
2134}
2135
Jim Grosbach4b905842013-09-20 23:08:21 +00002136static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002137 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002138 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002139 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2140 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002141 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002142 case MCExpr::Target:
2143 case MCExpr::Constant:
2144 return false;
2145 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002146 const MCSymbol &S =
2147 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002148 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002150 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002151 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002152 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002153 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002154 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002155
2156 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002157}
2158
Jim Grosbach4b905842013-09-20 23:08:21 +00002159bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002160 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002161 // FIXME: Use better location, we should use proper tokens.
2162 SMLoc EqualLoc = Lexer.getLoc();
2163
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002164 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002165 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002166 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002167
Rafael Espindola72f5f172012-01-28 05:57:00 +00002168 // Note: we don't count b as used in "a = b". This is to allow
2169 // a = b
2170 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002171
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002172 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002173 return TokError("unexpected token in assignment");
2174
2175 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002176 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002177
Daniel Dunbar5f339242009-10-16 01:57:39 +00002178 // Validate that the LHS is allowed to be a variable (either it has not been
2179 // used as a symbol, or it is an absolute symbol).
2180 MCSymbol *Sym = getContext().LookupSymbol(Name);
2181 if (Sym) {
2182 // Diagnose assignment to a label.
2183 //
2184 // FIXME: Diagnostics. Note the location of the definition as a label.
2185 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002186 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002187 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2188 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002189 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002190 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2191 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002192 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002193 return Error(EqualLoc, "redefinition of '" + Name + "'");
2194 else if (!Sym->isVariable())
2195 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002196 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002197 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002198 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002199
2200 // Don't count these checks as uses.
2201 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002202 } else if (Name == ".") {
2203 if (Out.EmitValueToOffset(Value, 0)) {
2204 Error(EqualLoc, "expected absolute expression");
2205 eatToEndOfStatement();
2206 }
2207 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002208 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002209 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002210
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002211 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002212 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002213 if (NoDeadStrip)
2214 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2215
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002216 return false;
2217}
2218
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002219/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002220/// ::= identifier
2221/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002222bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002223 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002224 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2225 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002226 // handle this as a context dependent token, instead we detect adjacent tokens
2227 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002228 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2229 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002230
Hans Wennborgce69d772013-10-18 20:46:28 +00002231 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002232 Lex();
2233 if (Lexer.isNot(AsmToken::Identifier))
2234 return true;
2235
Hans Wennborgce69d772013-10-18 20:46:28 +00002236 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2237 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002238 return true;
2239
2240 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002241 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002242 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002243 Lex();
2244 return false;
2245 }
2246
Jim Grosbach4b905842013-09-20 23:08:21 +00002247 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002248 return true;
2249
Sean Callanan936b0d32010-01-19 21:44:56 +00002250 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002251
Sean Callanan686ed8d2010-01-19 20:22:31 +00002252 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002253
2254 return false;
2255}
2256
Jim Grosbach4b905842013-09-20 23:08:21 +00002257/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002258/// ::= .equ identifier ',' expression
2259/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002260/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002261bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002262 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002263
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002264 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002265 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002266
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002267 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002268 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002269 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002270
Jim Grosbach4b905842013-09-20 23:08:21 +00002271 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002272}
2273
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002274bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002275 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002276
2277 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002278 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002279 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2280 if (Str[i] != '\\') {
2281 Data += Str[i];
2282 continue;
2283 }
2284
2285 // Recognize escaped characters. Note that this escape semantics currently
2286 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2287 ++i;
2288 if (i == e)
2289 return TokError("unexpected backslash at end of string");
2290
2291 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002292 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002293 // Consume up to three octal characters.
2294 unsigned Value = Str[i] - '0';
2295
Jim Grosbach4b905842013-09-20 23:08:21 +00002296 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002297 ++i;
2298 Value = Value * 8 + (Str[i] - '0');
2299
Jim Grosbach4b905842013-09-20 23:08:21 +00002300 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002301 ++i;
2302 Value = Value * 8 + (Str[i] - '0');
2303 }
2304 }
2305
2306 if (Value > 255)
2307 return TokError("invalid octal escape sequence (out of range)");
2308
Jim Grosbach4b905842013-09-20 23:08:21 +00002309 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002310 continue;
2311 }
2312
2313 // Otherwise recognize individual escapes.
2314 switch (Str[i]) {
2315 default:
2316 // Just reject invalid escape sequences for now.
2317 return TokError("invalid escape sequence (unrecognized character)");
2318
2319 case 'b': Data += '\b'; break;
2320 case 'f': Data += '\f'; break;
2321 case 'n': Data += '\n'; break;
2322 case 'r': Data += '\r'; break;
2323 case 't': Data += '\t'; break;
2324 case '"': Data += '"'; break;
2325 case '\\': Data += '\\'; break;
2326 }
2327 }
2328
2329 return false;
2330}
2331
Jim Grosbach4b905842013-09-20 23:08:21 +00002332/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002333/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002334bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002335 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002336 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002337
Daniel Dunbara10e5192009-06-24 23:30:00 +00002338 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002339 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002340 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002341
Daniel Dunbaref668c12009-08-14 18:19:52 +00002342 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002343 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002344 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002345
Rafael Espindola64e1af82013-07-02 15:49:13 +00002346 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002347 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002348 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002349
Sean Callanan686ed8d2010-01-19 20:22:31 +00002350 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351
2352 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002353 break;
2354
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002356 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002357 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002358 }
2359 }
2360
Sean Callanan686ed8d2010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002362 return false;
2363}
2364
Jim Grosbach4b905842013-09-20 23:08:21 +00002365/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002366/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002367bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002368 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002369 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002370
Daniel Dunbara10e5192009-06-24 23:30:00 +00002371 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002372 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002373 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002374 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002375 return true;
2376
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002377 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002378 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2379 assert(Size <= 8 && "Invalid size");
2380 uint64_t IntValue = MCE->getValue();
2381 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2382 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002383 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002384 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002385 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002386
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002387 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002388 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002389
Daniel Dunbara10e5192009-06-24 23:30:00 +00002390 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002391 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002393 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002394 }
2395 }
2396
Sean Callanan686ed8d2010-01-19 20:22:31 +00002397 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398 return false;
2399}
2400
David Woodhoused6de0d92014-02-01 16:20:59 +00002401/// ParseDirectiveOctaValue
2402/// ::= .octa [ hexconstant (, hexconstant)* ]
2403bool AsmParser::parseDirectiveOctaValue() {
2404 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2405 checkForValidSection();
2406
2407 for (;;) {
2408 if (Lexer.getKind() == AsmToken::Error)
2409 return true;
2410 if (Lexer.getKind() != AsmToken::Integer &&
2411 Lexer.getKind() != AsmToken::BigNum)
2412 return TokError("unknown token in expression");
2413
2414 SMLoc ExprLoc = getLexer().getLoc();
2415 APInt IntValue = getTok().getAPIntVal();
2416 Lex();
2417
2418 uint64_t hi, lo;
2419 if (IntValue.isIntN(64)) {
2420 hi = 0;
2421 lo = IntValue.getZExtValue();
2422 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002423 // It might actually have more than 128 bits, but the top ones are zero.
2424 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002425 lo = IntValue.getLoBits(64).getZExtValue();
2426 } else
2427 return Error(ExprLoc, "literal value out of range for directive");
2428
2429 if (MAI.isLittleEndian()) {
2430 getStreamer().EmitIntValue(lo, 8);
2431 getStreamer().EmitIntValue(hi, 8);
2432 } else {
2433 getStreamer().EmitIntValue(hi, 8);
2434 getStreamer().EmitIntValue(lo, 8);
2435 }
2436
2437 if (getLexer().is(AsmToken::EndOfStatement))
2438 break;
2439
2440 // FIXME: Improve diagnostic.
2441 if (getLexer().isNot(AsmToken::Comma))
2442 return TokError("unexpected token in directive");
2443 Lex();
2444 }
2445 }
2446
2447 Lex();
2448 return false;
2449}
2450
Jim Grosbach4b905842013-09-20 23:08:21 +00002451/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002452/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002453bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002454 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002455 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002456
2457 for (;;) {
2458 // We don't truly support arithmetic on floating point expressions, so we
2459 // have to manually parse unary prefixes.
2460 bool IsNeg = false;
2461 if (getLexer().is(AsmToken::Minus)) {
2462 Lex();
2463 IsNeg = true;
2464 } else if (getLexer().is(AsmToken::Plus))
2465 Lex();
2466
Michael J. Spencer530ce852010-10-09 11:00:50 +00002467 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002468 getLexer().isNot(AsmToken::Real) &&
2469 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470 return TokError("unexpected token in directive");
2471
2472 // Convert to an APFloat.
2473 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002474 StringRef IDVal = getTok().getString();
2475 if (getLexer().is(AsmToken::Identifier)) {
2476 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2477 Value = APFloat::getInf(Semantics);
2478 else if (!IDVal.compare_lower("nan"))
2479 Value = APFloat::getNaN(Semantics, false, ~0);
2480 else
2481 return TokError("invalid floating point literal");
2482 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002483 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002484 return TokError("invalid floating point literal");
2485 if (IsNeg)
2486 Value.changeSign();
2487
2488 // Consume the numeric token.
2489 Lex();
2490
2491 // Emit the value as an integer.
2492 APInt AsInt = Value.bitcastToAPInt();
2493 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002494 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002495
2496 if (getLexer().is(AsmToken::EndOfStatement))
2497 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002498
Daniel Dunbar2af16532010-09-24 01:59:56 +00002499 if (getLexer().isNot(AsmToken::Comma))
2500 return TokError("unexpected token in directive");
2501 Lex();
2502 }
2503 }
2504
2505 Lex();
2506 return false;
2507}
2508
Jim Grosbach4b905842013-09-20 23:08:21 +00002509/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002510/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002511bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002512 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002513
2514 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002515 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002516 return true;
2517
Rafael Espindolab91bac62010-10-05 19:42:57 +00002518 int64_t Val = 0;
2519 if (getLexer().is(AsmToken::Comma)) {
2520 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002521 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002522 return true;
2523 }
2524
Rafael Espindola922e3f42010-09-16 15:03:59 +00002525 if (getLexer().isNot(AsmToken::EndOfStatement))
2526 return TokError("unexpected token in '.zero' directive");
2527
2528 Lex();
2529
Rafael Espindola64e1af82013-07-02 15:49:13 +00002530 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002531
2532 return false;
2533}
2534
Jim Grosbach4b905842013-09-20 23:08:21 +00002535/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002536/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002537bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002538 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002539
David Majnemer522d3db2014-02-01 07:19:38 +00002540 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002541 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002542 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002543 return true;
2544
David Majnemer522d3db2014-02-01 07:19:38 +00002545 if (NumValues < 0) {
2546 Warning(RepeatLoc,
2547 "'.fill' directive with negative repeat count has no effect");
2548 NumValues = 0;
2549 }
2550
Roman Divackye33098f2013-09-24 17:44:41 +00002551 int64_t FillSize = 1;
2552 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002553
David Majnemer522d3db2014-02-01 07:19:38 +00002554 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002555 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2556 if (getLexer().isNot(AsmToken::Comma))
2557 return TokError("unexpected token in '.fill' directive");
2558 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002559
David Majnemer522d3db2014-02-01 07:19:38 +00002560 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002561 if (parseAbsoluteExpression(FillSize))
2562 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002563
Roman Divackye33098f2013-09-24 17:44:41 +00002564 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2565 if (getLexer().isNot(AsmToken::Comma))
2566 return TokError("unexpected token in '.fill' directive");
2567 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002568
David Majnemer522d3db2014-02-01 07:19:38 +00002569 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002570 if (parseAbsoluteExpression(FillExpr))
2571 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002572
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (getLexer().isNot(AsmToken::EndOfStatement))
2574 return TokError("unexpected token in '.fill' directive");
2575
2576 Lex();
2577 }
2578 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002579
David Majnemer522d3db2014-02-01 07:19:38 +00002580 if (FillSize < 0) {
2581 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2582 NumValues = 0;
2583 }
2584 if (FillSize > 8) {
2585 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2586 FillSize = 8;
2587 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002588
David Majnemer522d3db2014-02-01 07:19:38 +00002589 if (!isUInt<32>(FillExpr) && FillSize > 4)
2590 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2591
2592 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2593 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2594
2595 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2596 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2597 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2598 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002599
2600 return false;
2601}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002602
Jim Grosbach4b905842013-09-20 23:08:21 +00002603/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002604/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002605bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002606 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002607
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002608 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002609 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002610 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002611 return true;
2612
2613 // Parse optional fill expression.
2614 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002615 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2616 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002617 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002618 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002619
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002620 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002621 return true;
2622
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002623 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002624 return TokError("unexpected token in '.org' directive");
2625 }
2626
Sean Callanan686ed8d2010-01-19 20:22:31 +00002627 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002628
Jim Grosbachb5912772012-01-27 00:37:08 +00002629 // Only limited forms of relocatable expressions are accepted here, it
2630 // has to be relative to the current section. The streamer will return
2631 // 'true' if the expression wasn't evaluatable.
2632 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2633 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002634
2635 return false;
2636}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002637
Jim Grosbach4b905842013-09-20 23:08:21 +00002638/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002639/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002640bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002641 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002642
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002643 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002644 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002645 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002646 return true;
2647
2648 SMLoc MaxBytesLoc;
2649 bool HasFillExpr = false;
2650 int64_t FillExpr = 0;
2651 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002652 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2653 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002655 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002656
2657 // The fill expression can be omitted while specifying a maximum number of
2658 // alignment bytes, e.g:
2659 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002660 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002662 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002663 return true;
2664 }
2665
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002666 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2667 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002669 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002670
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002671 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002672 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002673 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002674
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002675 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002676 return TokError("unexpected token in directive");
2677 }
2678 }
2679
Sean Callanan686ed8d2010-01-19 20:22:31 +00002680 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002682 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684
2685 // Compute alignment in bytes.
2686 if (IsPow2) {
2687 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002688 if (Alignment >= 32) {
2689 Error(AlignmentLoc, "invalid alignment value");
2690 Alignment = 31;
2691 }
2692
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002693 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002694 } else {
2695 // Reject alignments that aren't a power of two, for gas compatibility.
2696 if (!isPowerOf2_64(Alignment))
2697 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698 }
2699
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002700 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002701 if (MaxBytesLoc.isValid()) {
2702 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002703 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002704 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002705 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002706 }
2707
2708 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002709 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002710 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002711 MaxBytesToFill = 0;
2712 }
2713 }
2714
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002715 // Check whether we should use optimal code alignment for this .align
2716 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002717 const MCSection *Section = getStreamer().getCurrentSection().first;
2718 assert(Section && "must have section to emit alignment");
2719 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002720 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2721 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002722 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002723 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002724 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002725 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2726 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002727 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002728
2729 return false;
2730}
2731
Jim Grosbach4b905842013-09-20 23:08:21 +00002732/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002733/// ::= .file [number] filename
2734/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002735bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002736 // FIXME: I'm not sure what this is.
2737 int64_t FileNumber = -1;
2738 SMLoc FileNumberLoc = getLexer().getLoc();
2739 if (getLexer().is(AsmToken::Integer)) {
2740 FileNumber = getTok().getIntVal();
2741 Lex();
2742
2743 if (FileNumber < 1)
2744 return TokError("file number less than one");
2745 }
2746
2747 if (getLexer().isNot(AsmToken::String))
2748 return TokError("unexpected token in '.file' directive");
2749
2750 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002751 // Allow the strings to have escaped octal character sequence.
2752 std::string Path = getTok().getString();
2753 if (parseEscapedString(Path))
2754 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002755 Lex();
2756
2757 StringRef Directory;
2758 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002759 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002760 if (getLexer().is(AsmToken::String)) {
2761 if (FileNumber == -1)
2762 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002763 if (parseEscapedString(FilenameData))
2764 return true;
2765 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002766 Directory = Path;
2767 Lex();
2768 } else {
2769 Filename = Path;
2770 }
2771
2772 if (getLexer().isNot(AsmToken::EndOfStatement))
2773 return TokError("unexpected token in '.file' directive");
2774
2775 if (FileNumber == -1)
2776 getStreamer().EmitFileDirective(Filename);
2777 else {
2778 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002779 Error(DirectiveLoc,
2780 "input can't have .file dwarf directives when -g is "
2781 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002782
David Blaikiec714ef42014-03-17 01:52:11 +00002783 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2784 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002785 Error(FileNumberLoc, "file number already allocated");
2786 }
2787
2788 return false;
2789}
2790
Jim Grosbach4b905842013-09-20 23:08:21 +00002791/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002792/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002793bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002794 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2795 if (getLexer().isNot(AsmToken::Integer))
2796 return TokError("unexpected token in '.line' directive");
2797
2798 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002799 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002800 Lex();
2801
2802 // FIXME: Do something with the .line.
2803 }
2804
2805 if (getLexer().isNot(AsmToken::EndOfStatement))
2806 return TokError("unexpected token in '.line' directive");
2807
2808 return false;
2809}
2810
Jim Grosbach4b905842013-09-20 23:08:21 +00002811/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002812/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2813/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2814/// The first number is a file number, must have been previously assigned with
2815/// a .file directive, the second number is the line number and optionally the
2816/// third number is a column position (zero if not specified). The remaining
2817/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002818bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002819 if (getLexer().isNot(AsmToken::Integer))
2820 return TokError("unexpected token in '.loc' directive");
2821 int64_t FileNumber = getTok().getIntVal();
2822 if (FileNumber < 1)
2823 return TokError("file number less than one in '.loc' directive");
2824 if (!getContext().isValidDwarfFileNumber(FileNumber))
2825 return TokError("unassigned file number in '.loc' directive");
2826 Lex();
2827
2828 int64_t LineNumber = 0;
2829 if (getLexer().is(AsmToken::Integer)) {
2830 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002831 if (LineNumber < 0)
2832 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002833 Lex();
2834 }
2835
2836 int64_t ColumnPos = 0;
2837 if (getLexer().is(AsmToken::Integer)) {
2838 ColumnPos = getTok().getIntVal();
2839 if (ColumnPos < 0)
2840 return TokError("column position less than zero in '.loc' directive");
2841 Lex();
2842 }
2843
2844 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2845 unsigned Isa = 0;
2846 int64_t Discriminator = 0;
2847 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2848 for (;;) {
2849 if (getLexer().is(AsmToken::EndOfStatement))
2850 break;
2851
2852 StringRef Name;
2853 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002854 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002855 return TokError("unexpected token in '.loc' directive");
2856
2857 if (Name == "basic_block")
2858 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2859 else if (Name == "prologue_end")
2860 Flags |= DWARF2_FLAG_PROLOGUE_END;
2861 else if (Name == "epilogue_begin")
2862 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2863 else if (Name == "is_stmt") {
2864 Loc = getTok().getLoc();
2865 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002866 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002867 return true;
2868 // The expression must be the constant 0 or 1.
2869 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2870 int Value = MCE->getValue();
2871 if (Value == 0)
2872 Flags &= ~DWARF2_FLAG_IS_STMT;
2873 else if (Value == 1)
2874 Flags |= DWARF2_FLAG_IS_STMT;
2875 else
2876 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002877 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002878 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2879 }
Craig Topperf15655b2013-04-22 04:22:40 +00002880 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002881 Loc = getTok().getLoc();
2882 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002883 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002884 return true;
2885 // The expression must be a constant greater or equal to 0.
2886 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2887 int Value = MCE->getValue();
2888 if (Value < 0)
2889 return Error(Loc, "isa number less than zero");
2890 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002891 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002892 return Error(Loc, "isa number not a constant value");
2893 }
Craig Topperf15655b2013-04-22 04:22:40 +00002894 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002895 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002897 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002898 return Error(Loc, "unknown sub-directive in '.loc' directive");
2899 }
2900
2901 if (getLexer().is(AsmToken::EndOfStatement))
2902 break;
2903 }
2904 }
2905
2906 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2907 Isa, Discriminator, StringRef());
2908
2909 return false;
2910}
2911
Jim Grosbach4b905842013-09-20 23:08:21 +00002912/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002913/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002914bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002915 return TokError("unsupported directive '.stabs'");
2916}
2917
Jim Grosbach4b905842013-09-20 23:08:21 +00002918/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002919/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002920bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002921 StringRef Name;
2922 bool EH = false;
2923 bool Debug = false;
2924
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002925 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002926 return TokError("Expected an identifier");
2927
2928 if (Name == ".eh_frame")
2929 EH = true;
2930 else if (Name == ".debug_frame")
2931 Debug = true;
2932
2933 if (getLexer().is(AsmToken::Comma)) {
2934 Lex();
2935
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002936 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002937 return TokError("Expected an identifier");
2938
2939 if (Name == ".eh_frame")
2940 EH = true;
2941 else if (Name == ".debug_frame")
2942 Debug = true;
2943 }
2944
2945 getStreamer().EmitCFISections(EH, Debug);
2946 return false;
2947}
2948
Jim Grosbach4b905842013-09-20 23:08:21 +00002949/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002950/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002951bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002952 StringRef Simple;
2953 if (getLexer().isNot(AsmToken::EndOfStatement))
2954 if (parseIdentifier(Simple) || Simple != "simple")
2955 return TokError("unexpected token in .cfi_startproc directive");
2956
2957 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002958 return false;
2959}
2960
Jim Grosbach4b905842013-09-20 23:08:21 +00002961/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002962/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002964 getStreamer().EmitCFIEndProc();
2965 return false;
2966}
2967
Jim Grosbach4b905842013-09-20 23:08:21 +00002968/// \brief parse register name or number.
2969bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002970 SMLoc DirectiveLoc) {
2971 unsigned RegNo;
2972
2973 if (getLexer().isNot(AsmToken::Integer)) {
2974 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2975 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002976 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002977 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002978 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002979
2980 return false;
2981}
2982
Jim Grosbach4b905842013-09-20 23:08:21 +00002983/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002984/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002985bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002986 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002987 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002988 return true;
2989
2990 if (getLexer().isNot(AsmToken::Comma))
2991 return TokError("unexpected token in directive");
2992 Lex();
2993
2994 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002995 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002996 return true;
2997
2998 getStreamer().EmitCFIDefCfa(Register, Offset);
2999 return false;
3000}
3001
Jim Grosbach4b905842013-09-20 23:08:21 +00003002/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003003/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003004bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003005 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003006 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003007 return true;
3008
3009 getStreamer().EmitCFIDefCfaOffset(Offset);
3010 return false;
3011}
3012
Jim Grosbach4b905842013-09-20 23:08:21 +00003013/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003014/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003015bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003016 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003017 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003018 return true;
3019
3020 if (getLexer().isNot(AsmToken::Comma))
3021 return TokError("unexpected token in directive");
3022 Lex();
3023
3024 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003025 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003026 return true;
3027
3028 getStreamer().EmitCFIRegister(Register1, Register2);
3029 return false;
3030}
3031
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003032/// parseDirectiveCFIWindowSave
3033/// ::= .cfi_window_save
3034bool AsmParser::parseDirectiveCFIWindowSave() {
3035 getStreamer().EmitCFIWindowSave();
3036 return false;
3037}
3038
Jim Grosbach4b905842013-09-20 23:08:21 +00003039/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003040/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003041bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003042 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003043 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003044 return true;
3045
3046 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3047 return false;
3048}
3049
Jim Grosbach4b905842013-09-20 23:08:21 +00003050/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003051/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003052bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003053 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003054 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return true;
3056
3057 getStreamer().EmitCFIDefCfaRegister(Register);
3058 return false;
3059}
3060
Jim Grosbach4b905842013-09-20 23:08:21 +00003061/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003062/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003063bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003064 int64_t Register = 0;
3065 int64_t Offset = 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
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003074 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003075 return true;
3076
3077 getStreamer().EmitCFIOffset(Register, Offset);
3078 return false;
3079}
3080
Jim Grosbach4b905842013-09-20 23:08:21 +00003081/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003082/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003083bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003084 int64_t Register = 0;
3085
Jim Grosbach4b905842013-09-20 23:08:21 +00003086 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003087 return true;
3088
3089 if (getLexer().isNot(AsmToken::Comma))
3090 return TokError("unexpected token in directive");
3091 Lex();
3092
3093 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003094 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003095 return true;
3096
3097 getStreamer().EmitCFIRelOffset(Register, Offset);
3098 return false;
3099}
3100
3101static bool isValidEncoding(int64_t Encoding) {
3102 if (Encoding & ~0xff)
3103 return false;
3104
3105 if (Encoding == dwarf::DW_EH_PE_omit)
3106 return true;
3107
3108 const unsigned Format = Encoding & 0xf;
3109 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3110 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3111 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3112 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3113 return false;
3114
3115 const unsigned Application = Encoding & 0x70;
3116 if (Application != dwarf::DW_EH_PE_absptr &&
3117 Application != dwarf::DW_EH_PE_pcrel)
3118 return false;
3119
3120 return true;
3121}
3122
Jim Grosbach4b905842013-09-20 23:08:21 +00003123/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003124/// IsPersonality true for cfi_personality, false for cfi_lsda
3125/// ::= .cfi_personality encoding, [symbol_name]
3126/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003127bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003128 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003129 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003130 return true;
3131 if (Encoding == dwarf::DW_EH_PE_omit)
3132 return false;
3133
3134 if (!isValidEncoding(Encoding))
3135 return TokError("unsupported encoding.");
3136
3137 if (getLexer().isNot(AsmToken::Comma))
3138 return TokError("unexpected token in directive");
3139 Lex();
3140
3141 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003142 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003143 return TokError("expected identifier in directive");
3144
3145 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3146
3147 if (IsPersonality)
3148 getStreamer().EmitCFIPersonality(Sym, Encoding);
3149 else
3150 getStreamer().EmitCFILsda(Sym, Encoding);
3151 return false;
3152}
3153
Jim Grosbach4b905842013-09-20 23:08:21 +00003154/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003155/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003156bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003157 getStreamer().EmitCFIRememberState();
3158 return false;
3159}
3160
Jim Grosbach4b905842013-09-20 23:08:21 +00003161/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003162/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003163bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003164 getStreamer().EmitCFIRestoreState();
3165 return false;
3166}
3167
Jim Grosbach4b905842013-09-20 23:08:21 +00003168/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003169/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003170bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003171 int64_t Register = 0;
3172
Jim Grosbach4b905842013-09-20 23:08:21 +00003173 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003174 return true;
3175
3176 getStreamer().EmitCFISameValue(Register);
3177 return false;
3178}
3179
Jim Grosbach4b905842013-09-20 23:08:21 +00003180/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003181/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003182bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003183 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003184 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003185 return true;
3186
3187 getStreamer().EmitCFIRestore(Register);
3188 return false;
3189}
3190
Jim Grosbach4b905842013-09-20 23:08:21 +00003191/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003192/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003193bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003194 std::string Values;
3195 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003196 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003197 return true;
3198
3199 Values.push_back((uint8_t)CurrValue);
3200
3201 while (getLexer().is(AsmToken::Comma)) {
3202 Lex();
3203
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003204 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003205 return true;
3206
3207 Values.push_back((uint8_t)CurrValue);
3208 }
3209
3210 getStreamer().EmitCFIEscape(Values);
3211 return false;
3212}
3213
Jim Grosbach4b905842013-09-20 23:08:21 +00003214/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003215/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003216bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003217 if (getLexer().isNot(AsmToken::EndOfStatement))
3218 return Error(getLexer().getLoc(),
3219 "unexpected token in '.cfi_signal_frame'");
3220
3221 getStreamer().EmitCFISignalFrame();
3222 return false;
3223}
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003226/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003227bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003228 int64_t Register = 0;
3229
Jim Grosbach4b905842013-09-20 23:08:21 +00003230 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003231 return true;
3232
3233 getStreamer().EmitCFIUndefined(Register);
3234 return false;
3235}
3236
Jim Grosbach4b905842013-09-20 23:08:21 +00003237/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003238/// ::= .macros_on
3239/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003240bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003241 if (getLexer().isNot(AsmToken::EndOfStatement))
3242 return Error(getLexer().getLoc(),
3243 "unexpected token in '" + Directive + "' directive");
3244
Jim Grosbach4b905842013-09-20 23:08:21 +00003245 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003246 return false;
3247}
3248
Jim Grosbach4b905842013-09-20 23:08:21 +00003249/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003250/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003251bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003252 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003253 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003254 return TokError("expected identifier in '.macro' directive");
3255
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003256 if (getLexer().is(AsmToken::Comma))
3257 Lex();
3258
Eli Bendersky17233942013-01-15 22:59:42 +00003259 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003260 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003261
3262 if (Parameters.size() && Parameters.back().Vararg)
3263 return Error(Lexer.getLoc(),
3264 "Vararg parameter '" + Parameters.back().Name +
3265 "' should be last one in the list of parameters.");
3266
David Majnemer91fc4c22014-01-29 18:57:46 +00003267 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003268 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003269 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003270
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003271 if (Lexer.is(AsmToken::Colon)) {
3272 Lex(); // consume ':'
3273
3274 SMLoc QualLoc;
3275 StringRef Qualifier;
3276
3277 QualLoc = Lexer.getLoc();
3278 if (parseIdentifier(Qualifier))
3279 return Error(QualLoc, "missing parameter qualifier for "
3280 "'" + Parameter.Name + "' in macro '" + Name + "'");
3281
3282 if (Qualifier == "req")
3283 Parameter.Required = true;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003284 else if (Qualifier == "vararg" && !IsDarwin)
3285 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003286 else
3287 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3288 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3289 }
3290
David Majnemer91fc4c22014-01-29 18:57:46 +00003291 if (getLexer().is(AsmToken::Equal)) {
3292 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003293
3294 SMLoc ParamLoc;
3295
3296 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003297 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003298 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003299
3300 if (Parameter.Required)
3301 Warning(ParamLoc, "pointless default value for required parameter "
3302 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003303 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003304
3305 Parameters.push_back(Parameter);
3306
3307 if (getLexer().is(AsmToken::Comma))
3308 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003309 }
3310
3311 // Eat the end of statement.
3312 Lex();
3313
3314 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003315 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003316
3317 // Lex the macro definition.
3318 for (;;) {
3319 // Check whether we have reached the end of the file.
3320 if (getLexer().is(AsmToken::Eof))
3321 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3322
3323 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003324 if (getLexer().is(AsmToken::Identifier)) {
3325 if (getTok().getIdentifier() == ".endm" ||
3326 getTok().getIdentifier() == ".endmacro") {
3327 if (MacroDepth == 0) { // Outermost macro.
3328 EndToken = getTok();
3329 Lex();
3330 if (getLexer().isNot(AsmToken::EndOfStatement))
3331 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3332 "' directive");
3333 break;
3334 } else {
3335 // Otherwise we just found the end of an inner macro.
3336 --MacroDepth;
3337 }
3338 } else if (getTok().getIdentifier() == ".macro") {
3339 // We allow nested macros. Those aren't instantiated until the outermost
3340 // macro is expanded so just ignore them for now.
3341 ++MacroDepth;
3342 }
Eli Bendersky17233942013-01-15 22:59:42 +00003343 }
3344
3345 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003346 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003347 }
3348
Jim Grosbach4b905842013-09-20 23:08:21 +00003349 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003350 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3351 }
3352
3353 const char *BodyStart = StartToken.getLoc().getPointer();
3354 const char *BodyEnd = EndToken.getLoc().getPointer();
3355 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003356 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3357 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003358 return false;
3359}
3360
Jim Grosbach4b905842013-09-20 23:08:21 +00003361/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003362///
3363/// With the support added for named parameters there may be code out there that
3364/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003365/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003366/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003367/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003368/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3369/// warning that the positional parameter found in body which have no effect.
3370/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003371/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003372/// intended or change the macro to use the named parameters. It is possible
3373/// this warning will trigger when the none of the named parameters are used
3374/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003375void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003376 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003377 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378 // If this macro is not defined with named parameters the warning we are
3379 // checking for here doesn't apply.
3380 unsigned NParameters = Parameters.size();
3381 if (NParameters == 0)
3382 return;
3383
3384 bool NamedParametersFound = false;
3385 bool PositionalParametersFound = false;
3386
3387 // Look at the body of the macro for use of both the named parameters and what
3388 // are likely to be positional parameters. This is what expandMacro() is
3389 // doing when it finds the parameters in the body.
3390 while (!Body.empty()) {
3391 // Scan for the next possible parameter.
3392 std::size_t End = Body.size(), Pos = 0;
3393 for (; Pos != End; ++Pos) {
3394 // Check for a substitution or escape.
3395 // This macro is defined with parameters, look for \foo, \bar, etc.
3396 if (Body[Pos] == '\\' && Pos + 1 != End)
3397 break;
3398
3399 // This macro should have parameters, but look for $0, $1, ..., $n too.
3400 if (Body[Pos] != '$' || Pos + 1 == End)
3401 continue;
3402 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003403 if (Next == '$' || Next == 'n' ||
3404 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003405 break;
3406 }
3407
3408 // Check if we reached the end.
3409 if (Pos == End)
3410 break;
3411
3412 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003413 switch (Body[Pos + 1]) {
3414 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003415 case '$':
3416 break;
3417
Jim Grosbach4b905842013-09-20 23:08:21 +00003418 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003419 case 'n':
3420 PositionalParametersFound = true;
3421 break;
3422
Jim Grosbach4b905842013-09-20 23:08:21 +00003423 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003424 default: {
3425 PositionalParametersFound = true;
3426 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003427 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003428 }
3429 Pos += 2;
3430 } else {
3431 unsigned I = Pos + 1;
3432 while (isIdentifierChar(Body[I]) && I + 1 != End)
3433 ++I;
3434
Jim Grosbach4b905842013-09-20 23:08:21 +00003435 const char *Begin = Body.data() + Pos + 1;
3436 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003437 unsigned Index = 0;
3438 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003439 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003440 break;
3441
3442 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003443 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3444 Pos += 3;
3445 else {
3446 Pos = I;
3447 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003448 } else {
3449 NamedParametersFound = true;
3450 Pos += 1 + Argument.size();
3451 }
3452 }
3453 // Update the scan point.
3454 Body = Body.substr(Pos);
3455 }
3456
3457 if (!NamedParametersFound && PositionalParametersFound)
3458 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3459 "used in macro body, possible positional parameter "
3460 "found in body which will have no effect");
3461}
3462
Jim Grosbach4b905842013-09-20 23:08:21 +00003463/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003464/// ::= .endm
3465/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003466bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003467 if (getLexer().isNot(AsmToken::EndOfStatement))
3468 return TokError("unexpected token in '" + Directive + "' directive");
3469
3470 // If we are inside a macro instantiation, terminate the current
3471 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003472 if (isInsideMacroInstantiation()) {
3473 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003474 return false;
3475 }
3476
3477 // Otherwise, this .endmacro is a stray entry in the file; well formed
3478 // .endmacro directives are handled during the macro definition parsing.
3479 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003480 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003481}
3482
Jim Grosbach4b905842013-09-20 23:08:21 +00003483/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003484/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003485bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003486 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003487 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003488 return TokError("expected identifier in '.purgem' directive");
3489
3490 if (getLexer().isNot(AsmToken::EndOfStatement))
3491 return TokError("unexpected token in '.purgem' directive");
3492
Jim Grosbach4b905842013-09-20 23:08:21 +00003493 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003494 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3495
Jim Grosbach4b905842013-09-20 23:08:21 +00003496 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003497 return false;
3498}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003499
Jim Grosbach4b905842013-09-20 23:08:21 +00003500/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003501/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003502bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003504
3505 // Expect a single argument: an expression that evaluates to a constant
3506 // in the inclusive range 0-30.
3507 SMLoc ExprLoc = getLexer().getLoc();
3508 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003509 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003510 return true;
3511 else if (getLexer().isNot(AsmToken::EndOfStatement))
3512 return TokError("unexpected token after expression in"
3513 " '.bundle_align_mode' directive");
3514 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3515 return Error(ExprLoc,
3516 "invalid bundle alignment size (expected between 0 and 30)");
3517
3518 Lex();
3519
3520 // Because of AlignSizePow2's verified range we can safely truncate it to
3521 // unsigned.
3522 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3523 return false;
3524}
3525
Jim Grosbach4b905842013-09-20 23:08:21 +00003526/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003527/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003528bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003529 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003530 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003531
Eli Bendersky802b6282013-01-07 21:51:08 +00003532 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3533 StringRef Option;
3534 SMLoc Loc = getTok().getLoc();
3535 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003536 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003537
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003538 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003539 return Error(Loc, kInvalidOptionError);
3540
3541 if (Option != "align_to_end")
3542 return Error(Loc, kInvalidOptionError);
3543 else if (getLexer().isNot(AsmToken::EndOfStatement))
3544 return Error(Loc,
3545 "unexpected token after '.bundle_lock' directive option");
3546 AlignToEnd = true;
3547 }
3548
Eli Benderskyf483ff92012-12-20 19:05:53 +00003549 Lex();
3550
Eli Bendersky802b6282013-01-07 21:51:08 +00003551 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003552 return false;
3553}
3554
Jim Grosbach4b905842013-09-20 23:08:21 +00003555/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003556/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003557bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003558 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003559
3560 if (getLexer().isNot(AsmToken::EndOfStatement))
3561 return TokError("unexpected token in '.bundle_unlock' directive");
3562 Lex();
3563
3564 getStreamer().EmitBundleUnlock();
3565 return false;
3566}
3567
Jim Grosbach4b905842013-09-20 23:08:21 +00003568/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003569/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003570bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003571 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003572
3573 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003574 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003575 return true;
3576
3577 int64_t FillExpr = 0;
3578 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3579 if (getLexer().isNot(AsmToken::Comma))
3580 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3581 Lex();
3582
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003583 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003584 return true;
3585
3586 if (getLexer().isNot(AsmToken::EndOfStatement))
3587 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3588 }
3589
3590 Lex();
3591
3592 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003593 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3594 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003595
3596 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003597 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003598
3599 return false;
3600}
3601
Jim Grosbach4b905842013-09-20 23:08:21 +00003602/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003603/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003604bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003605 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003606 const MCExpr *Value;
3607
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003608 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003609 return true;
3610
3611 if (getLexer().isNot(AsmToken::EndOfStatement))
3612 return TokError("unexpected token in directive");
3613
3614 if (Signed)
3615 getStreamer().EmitSLEB128Value(Value);
3616 else
3617 getStreamer().EmitULEB128Value(Value);
3618
3619 return false;
3620}
3621
Jim Grosbach4b905842013-09-20 23:08:21 +00003622/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003623/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003624bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003625 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003626 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003627 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003628 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003629
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003630 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003631 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003632
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003633 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003634
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003635 // Assembler local symbols don't make any sense here. Complain loudly.
3636 if (Sym->isTemporary())
3637 return Error(Loc, "non-local symbol required in directive");
3638
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003639 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3640 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003641
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003642 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003643 break;
3644
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003645 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003646 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003647 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003648 }
3649 }
3650
Sean Callanan686ed8d2010-01-19 20:22:31 +00003651 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003652 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003653}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003654
Jim Grosbach4b905842013-09-20 23:08:21 +00003655/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003656/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003657bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003658 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003659
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003660 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003661 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003662 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003663 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003664
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003665 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003666 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003667
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003668 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003669 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003670 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003671
3672 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003673 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003675 return true;
3676
3677 int64_t Pow2Alignment = 0;
3678 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003679 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003680 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003681 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003682 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003683 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003684
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003685 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3686 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003687 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3688
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003689 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003690 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3691 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003692 if (!isPowerOf2_64(Pow2Alignment))
3693 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3694 Pow2Alignment = Log2_64(Pow2Alignment);
3695 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003696 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003697
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003698 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003699 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003700
Sean Callanan686ed8d2010-01-19 20:22:31 +00003701 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003702
Chris Lattner28ad7542009-07-09 17:25:12 +00003703 // NOTE: a size of zero for a .comm should create a undefined symbol
3704 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003705 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003706 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003707 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708
Eric Christopherbc818852010-05-14 01:38:54 +00003709 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003710 // may internally end up wanting an alignment in bytes.
3711 // FIXME: Diagnose overflow.
3712 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003713 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003714 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003716 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003717 return Error(IDLoc, "invalid symbol redefinition");
3718
Chris Lattner28ad7542009-07-09 17:25:12 +00003719 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003720 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003721 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003722 return false;
3723 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003724
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003725 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003726 return false;
3727}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003728
Jim Grosbach4b905842013-09-20 23:08:21 +00003729/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003730/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003731bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003732 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003734
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003735 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003736 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003737 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003738
Sean Callanan686ed8d2010-01-19 20:22:31 +00003739 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003740
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003741 if (Str.empty())
3742 Error(Loc, ".abort detected. Assembly stopping.");
3743 else
3744 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003745 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003746
3747 return false;
3748}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003749
Jim Grosbach4b905842013-09-20 23:08:21 +00003750/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003751/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003752bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003753 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003754 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003755
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003756 // Allow the strings to have escaped octal character sequence.
3757 std::string Filename;
3758 if (parseEscapedString(Filename))
3759 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003760 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003761 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003762
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003763 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003764 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003765
Chris Lattner693fbb82009-07-16 06:14:39 +00003766 // Attempt to switch the lexer to the included file before consuming the end
3767 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003768 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003769 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003770 return true;
3771 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003772
3773 return false;
3774}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003775
Jim Grosbach4b905842013-09-20 23:08:21 +00003776/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003777/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003778bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003779 if (getLexer().isNot(AsmToken::String))
3780 return TokError("expected string in '.incbin' directive");
3781
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003782 // Allow the strings to have escaped octal character sequence.
3783 std::string Filename;
3784 if (parseEscapedString(Filename))
3785 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003786 SMLoc IncbinLoc = getLexer().getLoc();
3787 Lex();
3788
3789 if (getLexer().isNot(AsmToken::EndOfStatement))
3790 return TokError("unexpected token in '.incbin' directive");
3791
Kevin Enderby109f25c2011-12-14 21:47:48 +00003792 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003793 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003794 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3795 return true;
3796 }
3797
3798 return false;
3799}
3800
Jim Grosbach4b905842013-09-20 23:08:21 +00003801/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003802/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003803/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003804bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003805 TheCondStack.push_back(TheCondState);
3806 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003807 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003808 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003809 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003810 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003811 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003812 return true;
3813
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003814 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003815 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003816
Sean Callanan686ed8d2010-01-19 20:22:31 +00003817 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003818
3819 TheCondState.CondMet = ExprValue;
3820 TheCondState.Ignore = !TheCondState.CondMet;
3821 }
3822
3823 return false;
3824}
3825
Jim Grosbach4b905842013-09-20 23:08:21 +00003826/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003827/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003828bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003829 TheCondStack.push_back(TheCondState);
3830 TheCondState.TheCond = AsmCond::IfCond;
3831
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003832 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003833 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003834 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003835 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003836
3837 if (getLexer().isNot(AsmToken::EndOfStatement))
3838 return TokError("unexpected token in '.ifb' directive");
3839
3840 Lex();
3841
3842 TheCondState.CondMet = ExpectBlank == Str.empty();
3843 TheCondState.Ignore = !TheCondState.CondMet;
3844 }
3845
3846 return false;
3847}
3848
Jim Grosbach4b905842013-09-20 23:08:21 +00003849/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003850/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003851/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003852bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003853 TheCondStack.push_back(TheCondState);
3854 TheCondState.TheCond = AsmCond::IfCond;
3855
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003856 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003857 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003858 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003859 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003860
3861 if (getLexer().isNot(AsmToken::Comma))
3862 return TokError("unexpected token in '.ifc' directive");
3863
3864 Lex();
3865
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003866 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003867
3868 if (getLexer().isNot(AsmToken::EndOfStatement))
3869 return TokError("unexpected token in '.ifc' directive");
3870
3871 Lex();
3872
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003873 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003874 TheCondState.Ignore = !TheCondState.CondMet;
3875 }
3876
3877 return false;
3878}
3879
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003880/// parseDirectiveIfeqs
3881/// ::= .ifeqs string1, string2
3882bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3883 if (Lexer.isNot(AsmToken::String)) {
3884 TokError("expected string parameter for '.ifeqs' directive");
3885 eatToEndOfStatement();
3886 return true;
3887 }
3888
3889 StringRef String1 = getTok().getStringContents();
3890 Lex();
3891
3892 if (Lexer.isNot(AsmToken::Comma)) {
3893 TokError("expected comma after first string for '.ifeqs' directive");
3894 eatToEndOfStatement();
3895 return true;
3896 }
3897
3898 Lex();
3899
3900 if (Lexer.isNot(AsmToken::String)) {
3901 TokError("expected string parameter for '.ifeqs' directive");
3902 eatToEndOfStatement();
3903 return true;
3904 }
3905
3906 StringRef String2 = getTok().getStringContents();
3907 Lex();
3908
3909 TheCondStack.push_back(TheCondState);
3910 TheCondState.TheCond = AsmCond::IfCond;
3911 TheCondState.CondMet = String1 == String2;
3912 TheCondState.Ignore = !TheCondState.CondMet;
3913
3914 return false;
3915}
3916
Jim Grosbach4b905842013-09-20 23:08:21 +00003917/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003918/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003919bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003920 StringRef Name;
3921 TheCondStack.push_back(TheCondState);
3922 TheCondState.TheCond = AsmCond::IfCond;
3923
3924 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003925 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003926 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003927 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003928 return TokError("expected identifier after '.ifdef'");
3929
3930 Lex();
3931
3932 MCSymbol *Sym = getContext().LookupSymbol(Name);
3933
3934 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003935 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003936 else
Craig Topper353eda42014-04-24 06:44:33 +00003937 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003938 TheCondState.Ignore = !TheCondState.CondMet;
3939 }
3940
3941 return false;
3942}
3943
Jim Grosbach4b905842013-09-20 23:08:21 +00003944/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003945/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003946bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003947 if (TheCondState.TheCond != AsmCond::IfCond &&
3948 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003949 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3950 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003951 TheCondState.TheCond = AsmCond::ElseIfCond;
3952
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003953 bool LastIgnoreState = false;
3954 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003955 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003956 if (LastIgnoreState || TheCondState.CondMet) {
3957 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003958 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003959 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003960 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003961 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003962 return true;
3963
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003964 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003965 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003966
Sean Callanan686ed8d2010-01-19 20:22:31 +00003967 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003968 TheCondState.CondMet = ExprValue;
3969 TheCondState.Ignore = !TheCondState.CondMet;
3970 }
3971
3972 return false;
3973}
3974
Jim Grosbach4b905842013-09-20 23:08:21 +00003975/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003976/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003977bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003978 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003979 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003980
Sean Callanan686ed8d2010-01-19 20:22:31 +00003981 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003982
3983 if (TheCondState.TheCond != AsmCond::IfCond &&
3984 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003985 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3986 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003987 TheCondState.TheCond = AsmCond::ElseCond;
3988 bool LastIgnoreState = false;
3989 if (!TheCondStack.empty())
3990 LastIgnoreState = TheCondStack.back().Ignore;
3991 if (LastIgnoreState || TheCondState.CondMet)
3992 TheCondState.Ignore = true;
3993 else
3994 TheCondState.Ignore = false;
3995
3996 return false;
3997}
3998
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003999/// parseDirectiveEnd
4000/// ::= .end
4001bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4002 if (getLexer().isNot(AsmToken::EndOfStatement))
4003 return TokError("unexpected token in '.end' directive");
4004
4005 Lex();
4006
4007 while (Lexer.isNot(AsmToken::Eof))
4008 Lex();
4009
4010 return false;
4011}
4012
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004013/// parseDirectiveError
4014/// ::= .err
4015/// ::= .error [string]
4016bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4017 if (!TheCondStack.empty()) {
4018 if (TheCondStack.back().Ignore) {
4019 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004020 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004021 }
4022 }
4023
4024 if (!WithMessage)
4025 return Error(L, ".err encountered");
4026
4027 StringRef Message = ".error directive invoked in source file";
4028 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4029 if (Lexer.isNot(AsmToken::String)) {
4030 TokError(".error argument must be a string");
4031 eatToEndOfStatement();
4032 return true;
4033 }
4034
4035 Message = getTok().getStringContents();
4036 Lex();
4037 }
4038
4039 Error(L, Message);
4040 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004041}
4042
Jim Grosbach4b905842013-09-20 23:08:21 +00004043/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004044/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004045bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004046 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004047 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004048
Sean Callanan686ed8d2010-01-19 20:22:31 +00004049 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004050
Jim Grosbach4b905842013-09-20 23:08:21 +00004051 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004052 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4053 ".else");
4054 if (!TheCondStack.empty()) {
4055 TheCondState = TheCondStack.back();
4056 TheCondStack.pop_back();
4057 }
4058
4059 return false;
4060}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004061
Eli Bendersky17233942013-01-15 22:59:42 +00004062void AsmParser::initializeDirectiveKindMap() {
4063 DirectiveKindMap[".set"] = DK_SET;
4064 DirectiveKindMap[".equ"] = DK_EQU;
4065 DirectiveKindMap[".equiv"] = DK_EQUIV;
4066 DirectiveKindMap[".ascii"] = DK_ASCII;
4067 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4068 DirectiveKindMap[".string"] = DK_STRING;
4069 DirectiveKindMap[".byte"] = DK_BYTE;
4070 DirectiveKindMap[".short"] = DK_SHORT;
4071 DirectiveKindMap[".value"] = DK_VALUE;
4072 DirectiveKindMap[".2byte"] = DK_2BYTE;
4073 DirectiveKindMap[".long"] = DK_LONG;
4074 DirectiveKindMap[".int"] = DK_INT;
4075 DirectiveKindMap[".4byte"] = DK_4BYTE;
4076 DirectiveKindMap[".quad"] = DK_QUAD;
4077 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004078 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004079 DirectiveKindMap[".single"] = DK_SINGLE;
4080 DirectiveKindMap[".float"] = DK_FLOAT;
4081 DirectiveKindMap[".double"] = DK_DOUBLE;
4082 DirectiveKindMap[".align"] = DK_ALIGN;
4083 DirectiveKindMap[".align32"] = DK_ALIGN32;
4084 DirectiveKindMap[".balign"] = DK_BALIGN;
4085 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4086 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4087 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4088 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4089 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4090 DirectiveKindMap[".org"] = DK_ORG;
4091 DirectiveKindMap[".fill"] = DK_FILL;
4092 DirectiveKindMap[".zero"] = DK_ZERO;
4093 DirectiveKindMap[".extern"] = DK_EXTERN;
4094 DirectiveKindMap[".globl"] = DK_GLOBL;
4095 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004096 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4097 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4098 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4099 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4100 DirectiveKindMap[".reference"] = DK_REFERENCE;
4101 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4102 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4103 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4104 DirectiveKindMap[".comm"] = DK_COMM;
4105 DirectiveKindMap[".common"] = DK_COMMON;
4106 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4107 DirectiveKindMap[".abort"] = DK_ABORT;
4108 DirectiveKindMap[".include"] = DK_INCLUDE;
4109 DirectiveKindMap[".incbin"] = DK_INCBIN;
4110 DirectiveKindMap[".code16"] = DK_CODE16;
4111 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4112 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004113 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004114 DirectiveKindMap[".irp"] = DK_IRP;
4115 DirectiveKindMap[".irpc"] = DK_IRPC;
4116 DirectiveKindMap[".endr"] = DK_ENDR;
4117 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4118 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4119 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4120 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004121 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004122 DirectiveKindMap[".ifb"] = DK_IFB;
4123 DirectiveKindMap[".ifnb"] = DK_IFNB;
4124 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004125 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004126 DirectiveKindMap[".ifnc"] = DK_IFNC;
4127 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4128 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4129 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4130 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4131 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004132 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004133 DirectiveKindMap[".endif"] = DK_ENDIF;
4134 DirectiveKindMap[".skip"] = DK_SKIP;
4135 DirectiveKindMap[".space"] = DK_SPACE;
4136 DirectiveKindMap[".file"] = DK_FILE;
4137 DirectiveKindMap[".line"] = DK_LINE;
4138 DirectiveKindMap[".loc"] = DK_LOC;
4139 DirectiveKindMap[".stabs"] = DK_STABS;
4140 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4141 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4142 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4143 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4144 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4145 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4146 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4147 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4148 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4149 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4150 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4151 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4152 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4153 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4154 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4155 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4156 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4157 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4158 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4159 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4160 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004161 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004162 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4163 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4164 DirectiveKindMap[".macro"] = DK_MACRO;
4165 DirectiveKindMap[".endm"] = DK_ENDM;
4166 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4167 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004168 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004169 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004170}
4171
Jim Grosbach4b905842013-09-20 23:08:21 +00004172MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004173 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004174
Rafael Espindola34b9c512012-06-03 23:57:14 +00004175 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004176 for (;;) {
4177 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004178 if (getLexer().is(AsmToken::Eof)) {
4179 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004180 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004181 }
4182
Rafael Espindola34b9c512012-06-03 23:57:14 +00004183 if (Lexer.is(AsmToken::Identifier) &&
4184 (getTok().getIdentifier() == ".rept")) {
4185 ++NestLevel;
4186 }
4187
4188 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004189 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004190 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004191 EndToken = getTok();
4192 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004193 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4194 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004195 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004196 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004197 break;
4198 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004199 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004200 }
4201
Rafael Espindola34b9c512012-06-03 23:57:14 +00004202 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004203 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004204 }
4205
4206 const char *BodyStart = StartToken.getLoc().getPointer();
4207 const char *BodyEnd = EndToken.getLoc().getPointer();
4208 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4209
Rafael Espindola34b9c512012-06-03 23:57:14 +00004210 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004211 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004212 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004213}
4214
Jim Grosbach4b905842013-09-20 23:08:21 +00004215void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004216 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004217 OS << ".endr\n";
4218
4219 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004220 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004221
Rafael Espindola34b9c512012-06-03 23:57:14 +00004222 // Create the macro instantiation object and add to the current macro
4223 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004224 MacroInstantiation *MI = new MacroInstantiation(
4225 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004226 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004227
Rafael Espindola34b9c512012-06-03 23:57:14 +00004228 // Jump to the macro instantiation and prime the lexer.
4229 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4230 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4231 Lex();
4232}
4233
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004234/// parseDirectiveRept
4235/// ::= .rep | .rept count
4236bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004237 const MCExpr *CountExpr;
4238 SMLoc CountLoc = getTok().getLoc();
4239 if (parseExpression(CountExpr))
4240 return true;
4241
Rafael Espindola34b9c512012-06-03 23:57:14 +00004242 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004243 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4244 eatToEndOfStatement();
4245 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4246 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004247
4248 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004249 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004250
4251 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004252 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004253
4254 // Eat the end of statement.
4255 Lex();
4256
4257 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004258 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004259 if (!M)
4260 return true;
4261
4262 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4263 // to hold the macro body with substitutions.
4264 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004265 raw_svector_ostream OS(Buf);
4266 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004267 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004268 return true;
4269 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004270 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004271
4272 return false;
4273}
4274
Jim Grosbach4b905842013-09-20 23:08:21 +00004275/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004276/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004277bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004278 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004279
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004280 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004281 return TokError("expected identifier in '.irp' directive");
4282
Rafael Espindola768b41c2012-06-15 14:02:34 +00004283 if (Lexer.isNot(AsmToken::Comma))
4284 return TokError("expected comma in '.irp' directive");
4285
4286 Lex();
4287
Eli Bendersky38274122013-01-14 23:22:36 +00004288 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004289 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004290 return true;
4291
4292 // Eat the end of statement.
4293 Lex();
4294
4295 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004296 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004297 if (!M)
4298 return true;
4299
4300 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4301 // to hold the macro body with substitutions.
4302 SmallString<256> Buf;
4303 raw_svector_ostream OS(Buf);
4304
Eli Bendersky38274122013-01-14 23:22:36 +00004305 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004306 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004307 return true;
4308 }
4309
Jim Grosbach4b905842013-09-20 23:08:21 +00004310 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004311
4312 return false;
4313}
4314
Jim Grosbach4b905842013-09-20 23:08:21 +00004315/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004316/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004317bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004318 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004319
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004320 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004321 return TokError("expected identifier in '.irpc' directive");
4322
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004323 if (Lexer.isNot(AsmToken::Comma))
4324 return TokError("expected comma in '.irpc' directive");
4325
4326 Lex();
4327
Eli Bendersky38274122013-01-14 23:22:36 +00004328 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004329 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004330 return true;
4331
4332 if (A.size() != 1 || A.front().size() != 1)
4333 return TokError("unexpected token in '.irpc' directive");
4334
4335 // Eat the end of statement.
4336 Lex();
4337
4338 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004339 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004340 if (!M)
4341 return true;
4342
4343 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4344 // to hold the macro body with substitutions.
4345 SmallString<256> Buf;
4346 raw_svector_ostream OS(Buf);
4347
4348 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004349 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004350 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004351 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004352
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004353 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004354 return true;
4355 }
4356
Jim Grosbach4b905842013-09-20 23:08:21 +00004357 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004358
4359 return false;
4360}
4361
Jim Grosbach4b905842013-09-20 23:08:21 +00004362bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004363 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004364 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004365
4366 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004367 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004368 assert(getLexer().is(AsmToken::EndOfStatement));
4369
Jim Grosbach4b905842013-09-20 23:08:21 +00004370 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004371 return false;
4372}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004373
Jim Grosbach4b905842013-09-20 23:08:21 +00004374bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004375 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004376 const MCExpr *Value;
4377 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004378 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004379 return true;
4380 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4381 if (!MCE)
4382 return Error(ExprLoc, "unexpected expression in _emit");
4383 uint64_t IntValue = MCE->getValue();
4384 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4385 return Error(ExprLoc, "literal value out of range for directive");
4386
Chad Rosierc7f552c2013-02-12 21:33:51 +00004387 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4388 return false;
4389}
4390
Jim Grosbach4b905842013-09-20 23:08:21 +00004391bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004392 const MCExpr *Value;
4393 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004394 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004395 return true;
4396 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4397 if (!MCE)
4398 return Error(ExprLoc, "unexpected expression in align");
4399 uint64_t IntValue = MCE->getValue();
4400 if (!isPowerOf2_64(IntValue))
4401 return Error(ExprLoc, "literal value not a power of two greater then zero");
4402
Jim Grosbach4b905842013-09-20 23:08:21 +00004403 Info.AsmRewrites->push_back(
4404 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004405 return false;
4406}
4407
Chad Rosierf43fcf52013-02-13 21:27:17 +00004408// We are comparing pointers, but the pointers are relative to a single string.
4409// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004410static int rewritesSort(const AsmRewrite *AsmRewriteA,
4411 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004412 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4413 return -1;
4414 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4415 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004416
Chad Rosierfce4fab2013-04-08 17:43:47 +00004417 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4418 // rewrite to the same location. Make sure the SizeDirective rewrite is
4419 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4420 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004421 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4422 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004423 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004424
Jim Grosbach4b905842013-09-20 23:08:21 +00004425 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4426 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004427 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004428 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004429}
4430
Jim Grosbach4b905842013-09-20 23:08:21 +00004431bool AsmParser::parseMSInlineAsm(
4432 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4433 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4434 SmallVectorImpl<std::string> &Constraints,
4435 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4436 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004437 SmallVector<void *, 4> InputDecls;
4438 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004439 SmallVector<bool, 4> InputDeclsAddressOf;
4440 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004441 SmallVector<std::string, 4> InputConstraints;
4442 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004443 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004444
Benjamin Kramer1a136112013-02-15 20:37:21 +00004445 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004446
4447 // Prime the lexer.
4448 Lex();
4449
4450 // While we have input, parse each statement.
4451 unsigned InputIdx = 0;
4452 unsigned OutputIdx = 0;
4453 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004454 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004455 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004456 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004457
Chad Rosier149e8e02012-12-12 22:45:52 +00004458 if (Info.ParseError)
4459 return true;
4460
Benjamin Kramer1a136112013-02-15 20:37:21 +00004461 if (Info.Opcode == ~0U)
4462 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004463
Benjamin Kramer1a136112013-02-15 20:37:21 +00004464 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004465
Benjamin Kramer1a136112013-02-15 20:37:21 +00004466 // Build the list of clobbers, outputs and inputs.
4467 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4468 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004469
Benjamin Kramer1a136112013-02-15 20:37:21 +00004470 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004471 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004472 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004473
Benjamin Kramer1a136112013-02-15 20:37:21 +00004474 // Register operand.
4475 if (Operand->isReg() && !Operand->needAddressOf()) {
4476 unsigned NumDefs = Desc.getNumDefs();
4477 // Clobber.
4478 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4479 ClobberRegs.push_back(Operand->getReg());
4480 continue;
4481 }
4482
4483 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004484 StringRef SymName = Operand->getSymName();
4485 if (SymName.empty())
4486 continue;
4487
Chad Rosierdba3fe52013-04-22 22:12:12 +00004488 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004489 if (!OpDecl)
4490 continue;
4491
4492 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004493 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004494 if (isOutput) {
4495 ++InputIdx;
4496 OutputDecls.push_back(OpDecl);
4497 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4498 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004499 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004500 } else {
4501 InputDecls.push_back(OpDecl);
4502 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4503 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004504 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004505 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004506 }
Reid Kleckneree088972013-12-10 18:27:32 +00004507
4508 // Consider implicit defs to be clobbers. Think of cpuid and push.
4509 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4510 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4511 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004512 }
4513
4514 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004515 NumOutputs = OutputDecls.size();
4516 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004517
4518 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004519 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4520 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4521 ClobberRegs.end());
4522 Clobbers.assign(ClobberRegs.size(), std::string());
4523 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4524 raw_string_ostream OS(Clobbers[I]);
4525 IP->printRegName(OS, ClobberRegs[I]);
4526 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004527
4528 // Merge the various outputs and inputs. Output are expected first.
4529 if (NumOutputs || NumInputs) {
4530 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004531 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004532 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004533 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004534 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004535 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004536 }
4537 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004538 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004539 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004540 }
4541 }
4542
4543 // Build the IR assembly string.
4544 std::string AsmStringIR;
4545 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004546 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4547 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004548 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004549 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4550 E = AsmStrRewrites.end();
4551 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004552 AsmRewriteKind Kind = (*I).Kind;
4553 if (Kind == AOK_Delete)
4554 continue;
4555
Chad Rosier8bce6642012-10-18 15:49:34 +00004556 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004557 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004558
Chad Rosier120eefd2013-03-19 17:32:17 +00004559 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004560 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004561 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004562 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004563
Chad Rosier37e755c2012-10-23 17:43:43 +00004564 // Skip the original expression.
4565 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004566 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004567 continue;
4568 }
4569
Chad Rosierff10ed12013-04-12 16:26:42 +00004570 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004571 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004572 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004573 default:
4574 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004575 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004576 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004577 break;
4578 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004579 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004580 break;
4581 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004582 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004583 break;
4584 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004585 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004586 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004587 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004588 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004589 default: break;
4590 case 8: OS << "byte ptr "; break;
4591 case 16: OS << "word ptr "; break;
4592 case 32: OS << "dword ptr "; break;
4593 case 64: OS << "qword ptr "; break;
4594 case 80: OS << "xword ptr "; break;
4595 case 128: OS << "xmmword ptr "; break;
4596 case 256: OS << "ymmword ptr "; break;
4597 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004598 break;
4599 case AOK_Emit:
4600 OS << ".byte";
4601 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004602 case AOK_Align: {
4603 unsigned Val = (*I).Val;
4604 OS << ".align " << Val;
4605
4606 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004607 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004608 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4609 break;
4610 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004611 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004612 // Insert the dot if the user omitted it.
4613 OS.flush();
4614 if (AsmStringIR.back() != '.')
4615 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004616 OS << (*I).Val;
4617 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004618 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004619
Chad Rosier8bce6642012-10-18 15:49:34 +00004620 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004621 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004622 }
4623
4624 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004625 if (AsmStart != AsmEnd)
4626 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004627
4628 AsmString = OS.str();
4629 return false;
4630}
4631
Daniel Dunbar01e36072010-07-17 02:26:10 +00004632/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004633MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4634 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004635 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004636}