blob: edbfefa370f0aee0a5d7c2295bc60eb1d4156e9a [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
Eric Christophera7c32732012-12-18 00:30:54 +000048MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000049
Daniel Dunbar86033402010-07-12 17:54:38 +000050namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000051/// \brief Helper types for tracking macro definitions.
52typedef std::vector<AsmToken> MCAsmMacroArgument;
53typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000054
55struct MCAsmMacroParameter {
56 StringRef Name;
57 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000058 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000059 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000062};
63
Eli Benderskya313ae62013-01-16 18:56:50 +000064typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
65
66struct MCAsmMacro {
67 StringRef Name;
68 StringRef Body;
69 MCAsmMacroParameters Parameters;
70
71public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000072 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000073 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000074};
75
Daniel Dunbar43235712010-07-18 18:54:11 +000076/// \brief Helper class for storing information about an active macro
77/// instantiation.
78struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000079 /// The location of the instantiation.
80 SMLoc InstantiationLoc;
81
Daniel Dunbar40f1d852012-12-01 01:38:48 +000082 /// The buffer where parsing should resume upon instantiation completion.
83 int ExitBuffer;
84
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location where parsing should resume upon instantiation completion.
86 SMLoc ExitLoc;
87
Nico Weber155dccd12014-07-24 17:08:39 +000088 /// The depth of TheCondStack at the start of the instantiation.
89 size_t CondStackDepth;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000092 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000093};
94
Eli Friedman0f4871d2012-10-22 23:58:19 +000095struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000096 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000097 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000098
Jim Grosbach4b905842013-09-20 23:08:21 +000099 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100 unsigned Opcode;
101
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000103 bool ParseError;
104
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVectorImpl<AsmRewrite> *AsmRewrites;
106
Craig Topper353eda42014-04-24 06:44:33 +0000107 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110};
111
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000112/// \brief The concrete assembly parser instance.
113class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000114 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
115 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000116private:
117 AsmLexer Lexer;
118 MCContext &Ctx;
119 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000120 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000122 SourceMgr::DiagHandlerTy SavedDiagHandler;
123 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000125
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126 /// This is the current buffer index we're lexing from as managed by the
127 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000128 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129
130 AsmCond TheCondState;
131 std::vector<AsmCond> TheCondStack;
132
Jim Grosbach4b905842013-09-20 23:08:21 +0000133 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000134 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000135 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000136 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000137
Jim Grosbach4b905842013-09-20 23:08:21 +0000138 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000139 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000142 std::vector<MacroInstantiation*> ActiveMacros;
143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000145 std::deque<MCAsmMacro> MacroLikeBodies;
146
Daniel Dunbar828984f2010-07-18 18:38:02 +0000147 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000148 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149
Daniel Dunbar43325c42010-09-09 22:42:56 +0000150 /// Flag tracking whether any errors have been encountered.
151 unsigned HadError : 1;
152
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000153 /// The values from the last parsed cpp hash file line comment if any.
154 StringRef CppHashFilename;
155 int64_t CppHashLineNumber;
156 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000157 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000158 /// When generating dwarf for assembly source files we need to calculate the
159 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000160 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
162 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000163 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000165
Devang Patela173ee52012-01-31 18:14:05 +0000166 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
167 unsigned AssemblerDialect;
168
Jim Grosbach4b905842013-09-20 23:08:21 +0000169 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000170 bool IsDarwin;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000173 bool ParsingInlineAsm;
174
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000175public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000176 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000177 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000178 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179
Craig Topper59be68f2014-03-08 07:14:16 +0000180 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181
Craig Topper59be68f2014-03-08 07:14:16 +0000182 void addDirectiveHandler(StringRef Directive,
183 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000184 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
Craig Topper59be68f2014-03-08 07:14:16 +0000191 SourceMgr &getSourceManager() override { return SrcMgr; }
192 MCAsmLexer &getLexer() override { return Lexer; }
193 MCContext &getContext() override { return Ctx; }
194 MCStreamer &getStreamer() override { return Out; }
195 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
Craig Topper59be68f2014-03-08 07:14:16 +0000201 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000202 AssemblerDialect = i;
203 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 void Note(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = None) override;
207 bool Warning(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = None) override;
209 bool Error(SMLoc L, const Twine &Msg,
210 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
215 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000216
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000217 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000218 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000219 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000220 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000222 const MCInstrInfo *MII, const MCInstPrinter *IP,
223 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000226 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
227 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
228 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
229 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000230
Jim Grosbach4b905842013-09-20 23:08:21 +0000231 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000232 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseIdentifier(StringRef &Res) override;
234 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000235
Craig Topper59be68f2014-03-08 07:14:16 +0000236 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000240
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000241 bool parseStatement(ParseStatementInfo &Info,
242 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000243 void eatToEndOfLine();
244 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000247 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000248 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000249 ArrayRef<MCAsmMacroParameter> Parameters,
250 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000251 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000252
Eli Benderskya313ae62013-01-16 18:56:50 +0000253 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000255
256 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000258
259 /// \brief Lookup a previously defined macro.
260 /// \param Name Macro name.
261 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000273 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000274 ///
275 /// \param M The macro.
276 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
David Majnemer91fc4c22014-01-29 18:57:46 +0000282 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000283 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void printMacroInstantiations();
289 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000290 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000291 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000292 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000293 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 /// \brief Enter the specified file. This returns true on failure.
296 bool enterIncludeFile(const std::string &Filename);
297
298 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000299 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000301
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000302 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000303 /// current token is not set; clients should ensure Lex() is called
304 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000305 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000306 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000307 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000308 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000309
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000310 /// \brief Parse up to the end of statement and a return the contents from the
311 /// current token until the end of the statement; the current token on exit
312 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000313 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000315 /// \brief Parse until the end of a statement or a comma is encountered,
316 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000317 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000318
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000320 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000321
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
323 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
324 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000327
Eli Bendersky17233942013-01-15 22:59:42 +0000328 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000329 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000330 DK_NO_DIRECTIVE, // Placeholder
331 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000332 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
333 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000334 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000335 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000336 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
338 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
339 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
340 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000341 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
342 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
343 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000344 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
345 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
346 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
347 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
348 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
349 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000350 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000351 DK_MACROS_ON, DK_MACROS_OFF,
352 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000353 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000354 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000355 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000356 };
357
Jim Grosbach4b905842013-09-20 23:08:21 +0000358 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000359 /// directives parsed by this class.
360 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361
362 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
364 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000365 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
367 bool parseDirectiveFill(); // ".fill"
368 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000369 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
371 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000372 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000374
Eli Bendersky17233942013-01-15 22:59:42 +0000375 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveFile(SMLoc DirectiveLoc);
377 bool parseDirectiveLine();
378 bool parseDirectiveLoc();
379 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000380
381 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000383 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveCFISections();
385 bool parseDirectiveCFIStartProc();
386 bool parseDirectiveCFIEndProc();
387 bool parseDirectiveCFIDefCfaOffset();
388 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
389 bool parseDirectiveCFIAdjustCfaOffset();
390 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
394 bool parseDirectiveCFIRememberState();
395 bool parseDirectiveCFIRestoreState();
396 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIEscape();
399 bool parseDirectiveCFISignalFrame();
400 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000401
402 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000403 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000404 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000405 bool parseDirectiveEndMacro(StringRef Directive);
406 bool parseDirectiveMacro(SMLoc DirectiveLoc);
407 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
Eli Benderskyf483ff92012-12-20 19:05:53 +0000409 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000411 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415
Eli Bendersky17233942013-01-15 22:59:42 +0000416 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
419 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000423 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveAbort(); // ".abort"
429 bool parseDirectiveInclude(); // ".include"
430 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000432 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
433 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000434 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000436 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000438 // ".ifeqs"
439 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000445 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000466 // "end"
467 bool parseDirectiveEnd(SMLoc DirectiveLoc);
468
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000469 // ".err" or ".error"
470 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000471
Nico Weber404012b2014-07-24 16:26:06 +0000472 // ".warning"
473 bool parseDirectiveWarning(SMLoc DirectiveLoc);
474
Eli Bendersky17233942013-01-15 22:59:42 +0000475 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000476};
Daniel Dunbar86033402010-07-12 17:54:38 +0000477}
478
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000479namespace llvm {
480
481extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000482extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000483extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484
485}
486
Chris Lattnerc35681b2010-01-19 19:46:13 +0000487enum { DEFAULT_ADDRSPACE = 0 };
488
Jim Grosbach4b905842013-09-20 23:08:21 +0000489AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
490 const MCAsmInfo &_MAI)
491 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000492 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
493 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
494 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000495 // Save the old handler.
496 SavedDiagHandler = SrcMgr.getDiagHandler();
497 SavedDiagContext = SrcMgr.getDiagContext();
498 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000499 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000500 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000501
Daniel Dunbarc5011082010-07-12 18:12:02 +0000502 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000503 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
504 case MCObjectFileInfo::IsCOFF:
505 PlatformParser = createCOFFAsmParser();
506 PlatformParser->Initialize(*this);
507 break;
508 case MCObjectFileInfo::IsMachO:
509 PlatformParser = createDarwinAsmParser();
510 PlatformParser->Initialize(*this);
511 IsDarwin = true;
512 break;
513 case MCObjectFileInfo::IsELF:
514 PlatformParser = createELFAsmParser();
515 PlatformParser->Initialize(*this);
516 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000517 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000518
Eli Bendersky17233942013-01-15 22:59:42 +0000519 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000520}
521
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000522AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000523 assert((HadError || ActiveMacros.empty()) &&
524 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000525
526 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000527 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
528 ie = MacroMap.end();
529 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000530 delete it->getValue();
531
Daniel Dunbarc5011082010-07-12 18:12:02 +0000532 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000533}
534
Jim Grosbach4b905842013-09-20 23:08:21 +0000535void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000536 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000537 for (std::vector<MacroInstantiation *>::const_reverse_iterator
538 it = ActiveMacros.rbegin(),
539 ie = ActiveMacros.rend();
540 it != ie; ++it)
541 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000542 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000543}
544
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000545void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
546 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
547 printMacroInstantiations();
548}
549
Chris Lattnera3a06812011-10-16 04:47:35 +0000550bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000551 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000552 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000553 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
554 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000555 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000556}
557
Chris Lattnera3a06812011-10-16 04:47:35 +0000558bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000559 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000560 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
561 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000562 return true;
563}
564
Jim Grosbach4b905842013-09-20 23:08:21 +0000565bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000566 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000567 unsigned NewBuf =
568 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
569 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000571
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000573 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 return false;
575}
Daniel Dunbar43235712010-07-18 18:54:11 +0000576
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000577/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000578/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000580bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000582 unsigned NewBuf =
583 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
584 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585 return true;
586
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000587 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000588 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000589 return false;
590}
591
Alp Tokera55b95b2014-07-06 10:33:31 +0000592void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
593 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000594 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
595 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000596}
597
Sean Callanan7a77eae2010-01-21 00:19:58 +0000598const AsmToken &AsmParser::Lex() {
599 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000600
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 if (tok->is(AsmToken::Eof)) {
602 // If this is the end of an included file, pop the parent file off the
603 // include stack.
604 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
605 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000606 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000607 tok = &Lexer.Lex();
608 }
609 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000610
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000612 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000613
Sean Callanan7a77eae2010-01-21 00:19:58 +0000614 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000615}
616
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000617bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000618 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000619 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000620 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000621
Chris Lattner36e02122009-06-21 20:54:55 +0000622 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000623 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000624
625 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000626 AsmCond StartingCondState = TheCondState;
627
Kevin Enderby6469fc22011-11-01 22:27:22 +0000628 // If we are generating dwarf for assembly source files save the initial text
629 // section and generate a .file directive.
630 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000631 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
632 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000633 auto InsertResult = getContext().addGenDwarfSection(
634 getStreamer().getCurrentSection().first);
635 assert(InsertResult.second && ".text section should not have debug info yet");
636 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000637 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
638 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000639 }
640
Chris Lattner73f36112009-07-02 21:53:43 +0000641 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000642 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000643 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000644 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000645 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000646
Daniel Dunbar43325c42010-09-09 22:42:56 +0000647 // We had an error, validate that one was emitted and recover by skipping to
648 // the next line.
649 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000650 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000651 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000652
653 if (TheCondState.TheCond != StartingCondState.TheCond ||
654 TheCondState.Ignore != StartingCondState.Ignore)
655 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656
657 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000658 const auto &LineTables = getContext().getMCDwarfLineTables();
659 if (!LineTables.empty()) {
660 unsigned Index = 0;
661 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
662 if (File.Name.empty() && Index != 0)
663 TokError("unassigned file number: " + Twine(Index) +
664 " for .file directives");
665 ++Index;
666 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000667 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000668
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000669 // Check to see that all assembler local symbols were actually defined.
670 // Targets that don't do subsections via symbols may not want this, though,
671 // so conservatively exclude them. Only do this if we're finalizing, though,
672 // as otherwise we won't necessarilly have seen everything yet.
673 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
674 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
675 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000676 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000677 i != e; ++i) {
678 MCSymbol *Sym = i->getValue();
679 // Variable symbols may not be marked as defined, so check those
680 // explicitly. If we know it's a variable, we have a definition for
681 // the purposes of this check.
682 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
683 // FIXME: We would really like to refer back to where the symbol was
684 // first referenced for a source location. We need to add something
685 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000686 printMessage(
687 getLexer().getLoc(), SourceMgr::DK_Error,
688 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000689 }
690 }
691
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000692 // Finalize the output stream if there are no errors and if the client wants
693 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000694 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000695 Out.Finish();
696
Chris Lattner73f36112009-07-02 21:53:43 +0000697 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000698}
699
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000700void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000701 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000702 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000703 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 }
705}
706
Jim Grosbach4b905842013-09-20 23:08:21 +0000707/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000708void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000709 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000710 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000711
Chris Lattnere5074c42009-06-22 01:29:09 +0000712 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000713 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000714 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000715}
716
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000717StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000718 const char *Start = getTok().getLoc().getPointer();
719
Jim Grosbach4b905842013-09-20 23:08:21 +0000720 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 Lex();
722
723 const char *End = getTok().getLoc().getPointer();
724 return StringRef(Start, End - Start);
725}
Chris Lattner78db3622009-06-22 05:51:26 +0000726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000728 const char *Start = getTok().getLoc().getPointer();
729
730 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000731 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000732 Lex();
733
734 const char *End = getTok().getLoc().getPointer();
735 return StringRef(Start, End - Start);
736}
737
Jim Grosbach4b905842013-09-20 23:08:21 +0000738/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000739/// NOTE: This assumes the leading '(' has already been consumed.
740///
741/// parenexpr ::= expr)
742///
Jim Grosbach4b905842013-09-20 23:08:21 +0000743bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
744 if (parseExpression(Res))
745 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000746 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000747 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000748 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000749 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return false;
751}
Chris Lattner78db3622009-06-22 05:51:26 +0000752
Jim Grosbach4b905842013-09-20 23:08:21 +0000753/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754/// NOTE: This assumes the leading '[' has already been consumed.
755///
756/// bracketexpr ::= expr]
757///
Jim Grosbach4b905842013-09-20 23:08:21 +0000758bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
759 if (parseExpression(Res))
760 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000761 if (Lexer.isNot(AsmToken::RBrac))
762 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000763 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 Lex();
765 return false;
766}
767
Jim Grosbach4b905842013-09-20 23:08:21 +0000768/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769/// primaryexpr ::= (parenexpr
770/// primaryexpr ::= symbol
771/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000772/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000773/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000774bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000775 SMLoc FirstTokenLoc = getLexer().getLoc();
776 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
777 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000778 default:
779 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000780 // If we have an error assume that we've already handled it.
781 case AsmToken::Error:
782 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000783 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000784 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000785 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000786 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000787 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000789 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000790 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000791 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000792 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000793 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000794 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000795 if (FirstTokenKind == AsmToken::Dollar) {
796 if (Lexer.getMAI().getDollarIsPC()) {
797 // This is a '$' reference, which references the current PC. Emit a
798 // temporary label to the streamer and refer to it.
799 MCSymbol *Sym = Ctx.CreateTempSymbol();
800 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000801 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
802 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000803 EndLoc = FirstTokenLoc;
804 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000805 }
806 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000807 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000808 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000809 // Parse symbol variant
810 std::pair<StringRef, StringRef> Split;
811 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000812 if (FirstTokenKind == AsmToken::String) {
813 if (Lexer.is(AsmToken::At)) {
814 Lexer.Lex(); // eat @
815 SMLoc AtLoc = getLexer().getLoc();
816 StringRef VName;
817 if (parseIdentifier(VName))
818 return Error(AtLoc, "expected symbol variant after '@'");
819
820 Split = std::make_pair(Identifier, VName);
821 }
822 } else {
823 Split = Identifier.split('@');
824 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000825 } else if (Lexer.is(AsmToken::LParen)) {
826 Lexer.Lex(); // eat (
827 StringRef VName;
828 parseIdentifier(VName);
829 if (Lexer.isNot(AsmToken::RParen)) {
830 return Error(Lexer.getTok().getLoc(),
831 "unexpected token in variant, expected ')'");
832 }
833 Lexer.Lex(); // eat )
834 Split = std::make_pair(Identifier, VName);
835 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000836
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000837 EndLoc = SMLoc::getFromPointer(Identifier.end());
838
Daniel Dunbard20cda02009-10-16 01:34:54 +0000839 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 StringRef SymbolName = Identifier;
841 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000842
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000843 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000844 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000845 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 if (Variant != MCSymbolRefExpr::VK_Invalid) {
847 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000848 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000849 Variant = MCSymbolRefExpr::VK_None;
850 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000851 return Error(SMLoc::getFromPointer(Split.second.begin()),
852 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000853 }
854 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000855
Hans Wennborgce69d772013-10-18 20:46:28 +0000856 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
857
Daniel Dunbard20cda02009-10-16 01:34:54 +0000858 // If this is an absolute variable reference, substitute it now to preserve
859 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000860 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000861 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000862 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000863
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000864 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000865 return false;
866 }
867
868 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000869 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000870 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000871 }
David Woodhousef42a6662014-02-01 16:20:54 +0000872 case AsmToken::BigNum:
873 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000874 case AsmToken::Integer: {
875 SMLoc Loc = getTok().getLoc();
876 int64_t IntVal = getTok().getIntVal();
877 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000878 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000879 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000880 // Look for 'b' or 'f' following an Integer as a directional label
881 if (Lexer.getKind() == AsmToken::Identifier) {
882 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 // Lookup the symbol variant if used.
884 std::pair<StringRef, StringRef> Split = IDVal.split('@');
885 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
886 if (Split.first.size() != IDVal.size()) {
887 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000888 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000889 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000890 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000891 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000892 if (IDVal == "f" || IDVal == "b") {
893 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000894 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000895 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000896 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000898 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000899 Lex(); // Eat identifier.
900 }
901 }
Chris Lattner78db3622009-06-22 05:51:26 +0000902 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000903 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000904 case AsmToken::Real: {
905 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000906 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000907 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 Lex(); // Eat token.
910 return false;
911 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000912 case AsmToken::Dot: {
913 // This is a '.' reference, which references the current PC. Emit a
914 // temporary label to the streamer and refer to it.
915 MCSymbol *Sym = Ctx.CreateTempSymbol();
916 Out.EmitLabel(Sym);
917 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000918 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000919 Lex(); // Eat identifier.
920 return false;
921 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000922 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000923 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000924 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000925 case AsmToken::LBrac:
926 if (!PlatformParser->HasBracketExpressions())
927 return TokError("brackets expression not supported on this target");
928 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000936 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000937 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000938 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000940 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000942 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000943 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000944 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000945 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000946 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000947 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000948 }
949}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000950
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000951bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000952 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000953 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000954}
955
Daniel Dunbar55f16672010-09-17 02:47:07 +0000956const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000957AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000959 // Ask the target implementation about this expression first.
960 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
961 if (NewE)
962 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 // Recurse over the given expression, rebuilding it to apply the given variant
964 // if there is exactly one symbol.
965 switch (E->getKind()) {
966 case MCExpr::Target:
967 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000968 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000969
970 case MCExpr::SymbolRef: {
971 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
972
973 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 TokError("invalid variant on expression '" + getTok().getIdentifier() +
975 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976 return E;
977 }
978
979 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
980 }
981
982 case MCExpr::Unary: {
983 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000984 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000985 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000986 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
988 }
989
990 case MCExpr::Binary: {
991 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000992 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
993 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994
995 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000996 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000997
Jim Grosbach4b905842013-09-20 23:08:21 +0000998 if (!LHS)
999 LHS = BE->getLHS();
1000 if (!RHS)
1001 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002
1003 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1004 }
1005 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001006
Craig Toppera2886c22012-02-07 05:05:23 +00001007 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001008}
1009
Jim Grosbach4b905842013-09-20 23:08:21 +00001010/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001011///
Jim Grosbachbd164242011-08-20 16:24:13 +00001012/// expr ::= expr &&,|| expr -> lowest.
1013/// expr ::= expr |,^,&,! expr
1014/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1015/// expr ::= expr <<,>> expr
1016/// expr ::= expr +,- expr
1017/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001018/// expr ::= primaryexpr
1019///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001020bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001021 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001022 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001023 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001024 return true;
1025
Daniel Dunbar55f16672010-09-17 02:47:07 +00001026 // As a special case, we support 'a op b @ modifier' by rewriting the
1027 // expression to include the modifier. This is inefficient, but in general we
1028 // expect users to use 'a@modifier op b'.
1029 if (Lexer.getKind() == AsmToken::At) {
1030 Lex();
1031
1032 if (Lexer.isNot(AsmToken::Identifier))
1033 return TokError("unexpected symbol modifier following '@'");
1034
1035 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001036 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 if (Variant == MCSymbolRefExpr::VK_Invalid)
1038 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1039
Jim Grosbach4b905842013-09-20 23:08:21 +00001040 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 if (!ModifiedRes) {
1042 return TokError("invalid modifier '" + getTok().getIdentifier() +
1043 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001045
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 Res = ModifiedRes;
1047 Lex();
1048 }
1049
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001050 // Try to constant fold it up front, if possible.
1051 int64_t Value;
1052 if (Res->EvaluateAsAbsolute(Value))
1053 Res = MCConstantExpr::Create(Value, getContext());
1054
1055 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001056}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001057
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001059 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001060 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001061}
1062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001064 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001065
Daniel Dunbar75630b32009-06-30 02:10:03 +00001066 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001067 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068 return true;
1069
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001070 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001071 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001072
1073 return false;
1074}
1075
Michael J. Spencer530ce852010-10-09 11:00:50 +00001076static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001078 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001079 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001080 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001081
Jim Grosbach4b905842013-09-20 23:08:21 +00001082 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001085 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001088 return 1;
1089
Jim Grosbach4b905842013-09-20 23:08:21 +00001090 // Low Precedence: |, &, ^
1091 //
1092 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001096 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001097 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001099 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001100 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001101 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001102
Jim Grosbach4b905842013-09-20 23:08:21 +00001103 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001104 case AsmToken::EqualEqual:
1105 Kind = MCBinaryExpr::EQ;
1106 return 3;
1107 case AsmToken::ExclaimEqual:
1108 case AsmToken::LessGreater:
1109 Kind = MCBinaryExpr::NE;
1110 return 3;
1111 case AsmToken::Less:
1112 Kind = MCBinaryExpr::LT;
1113 return 3;
1114 case AsmToken::LessEqual:
1115 Kind = MCBinaryExpr::LTE;
1116 return 3;
1117 case AsmToken::Greater:
1118 Kind = MCBinaryExpr::GT;
1119 return 3;
1120 case AsmToken::GreaterEqual:
1121 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001122 return 3;
1123
Jim Grosbach4b905842013-09-20 23:08:21 +00001124 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 case AsmToken::LessLess:
1126 Kind = MCBinaryExpr::Shl;
1127 return 4;
1128 case AsmToken::GreaterGreater:
1129 Kind = MCBinaryExpr::Shr;
1130 return 4;
1131
Jim Grosbach4b905842013-09-20 23:08:21 +00001132 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133 case AsmToken::Plus:
1134 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001135 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001136 case AsmToken::Minus:
1137 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001138 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001139
Jim Grosbach4b905842013-09-20 23:08:21 +00001140 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001144 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001146 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001147 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001149 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001150 }
1151}
1152
Jim Grosbach4b905842013-09-20 23:08:21 +00001153/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001155bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001156 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001157 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001158 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001159 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 // If the next token is lower precedence than we are allowed to eat, return
1162 // successfully with what we ate already.
1163 if (TokPrec < Precedence)
1164 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Sean Callanan686ed8d2010-01-19 20:22:31 +00001166 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001168 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001169 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001170 if (parsePrimaryExpr(RHS, EndLoc))
1171 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 // If BinOp binds less tightly with RHS than the operator after RHS, let
1174 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001175 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001176 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001177 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1178 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001179
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001180 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001181 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001182 }
1183}
1184
Chris Lattner36e02122009-06-21 20:54:55 +00001185/// ParseStatement:
1186/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001187/// ::= Label* Directive ...Operands... EndOfStatement
1188/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001189bool AsmParser::parseStatement(ParseStatementInfo &Info,
1190 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001191 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001192 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001193 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001194 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001195 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001196
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001197 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001198 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001199 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001200 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001202 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001203 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001204 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001205
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001206 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001207 if (Lexer.is(AsmToken::Integer)) {
1208 LocalLabelVal = getTok().getIntVal();
1209 if (LocalLabelVal < 0) {
1210 if (!TheCondState.Ignore)
1211 return TokError("unexpected token at start of statement");
1212 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001213 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001214 IDVal = getTok().getString();
1215 Lex(); // Consume the integer token to be used as an identifier token.
1216 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001217 if (!TheCondState.Ignore)
1218 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001219 }
1220 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001221 } else if (Lexer.is(AsmToken::Dot)) {
1222 // Treat '.' as a valid identifier in this context.
1223 Lex();
1224 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001225 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001226 if (!TheCondState.Ignore)
1227 return TokError("unexpected token at start of statement");
1228 IDVal = "";
1229 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001230
Chris Lattner926885c2010-04-17 18:14:27 +00001231 // Handle conditional assembly here before checking for skipping. We
1232 // have to do this so that .endif isn't skipped in a ".if 0" block for
1233 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001234 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001235 DirectiveKindMap.find(IDVal);
1236 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1237 ? DK_NO_DIRECTIVE
1238 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001239 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 default:
1241 break;
1242 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001243 case DK_IFEQ:
1244 case DK_IFGE:
1245 case DK_IFGT:
1246 case DK_IFLE:
1247 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001248 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001249 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001250 case DK_IFB:
1251 return parseDirectiveIfb(IDLoc, true);
1252 case DK_IFNB:
1253 return parseDirectiveIfb(IDLoc, false);
1254 case DK_IFC:
1255 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001256 case DK_IFEQS:
1257 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001258 case DK_IFNC:
1259 return parseDirectiveIfc(IDLoc, false);
1260 case DK_IFDEF:
1261 return parseDirectiveIfdef(IDLoc, true);
1262 case DK_IFNDEF:
1263 case DK_IFNOTDEF:
1264 return parseDirectiveIfdef(IDLoc, false);
1265 case DK_ELSEIF:
1266 return parseDirectiveElseIf(IDLoc);
1267 case DK_ELSE:
1268 return parseDirectiveElse(IDLoc);
1269 case DK_ENDIF:
1270 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001271 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001272
Eli Bendersky88024712013-01-16 19:32:36 +00001273 // Ignore the statement if in the middle of inactive conditional
1274 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001275 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001276 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001277 return false;
1278 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001279
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001280 // FIXME: Recurse on local labels?
1281
1282 // See what kind of statement we have.
1283 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001284 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001285 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001286
Chris Lattner36e02122009-06-21 20:54:55 +00001287 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001288 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001289
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001290 // Diagnose attempt to use '.' as a label.
1291 if (IDVal == ".")
1292 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1293
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001294 // Diagnose attempt to use a variable as a label.
1295 //
1296 // FIXME: Diagnostics. Note the location of the definition as a label.
1297 // FIXME: This doesn't diagnose assignment to a symbol which has been
1298 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001299 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001300 if (LocalLabelVal == -1) {
1301 if (ParsingInlineAsm && SI) {
1302 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1303 assert(RewrittenLabel.size() && "We should have an internal name here.");
1304 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1305 IDVal.size(), RewrittenLabel));
1306 IDVal = RewrittenLabel;
1307 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001308 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001309 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001310 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001311 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001312 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001313
Daniel Dunbare73b2672009-08-26 22:13:22 +00001314 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001315 if (!ParsingInlineAsm)
1316 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001317
Kevin Enderbye7739d42011-12-09 18:09:40 +00001318 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001319 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001320 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001321 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1322 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001323
Tim Northover1744d0a2013-10-25 12:49:50 +00001324 getTargetParser().onLabelParsed(Sym);
1325
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001326 // Consume any end of statement token, if present, to avoid spurious
1327 // AddBlankLine calls().
1328 if (Lexer.is(AsmToken::EndOfStatement)) {
1329 Lex();
1330 if (Lexer.is(AsmToken::Eof))
1331 return false;
1332 }
1333
Eli Friedman0f4871d2012-10-22 23:58:19 +00001334 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001335 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001336
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001337 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001338 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001339 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001340
Jim Grosbach4b905842013-09-20 23:08:21 +00001341 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001342
1343 default: // Normal instruction or directive.
1344 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001345 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001346
1347 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001348 if (areMacrosEnabled())
1349 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1350 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001351 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001352
Michael J. Spencer530ce852010-10-09 11:00:50 +00001353 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001354
Eli Bendersky17233942013-01-15 22:59:42 +00001355 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001356 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001357 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001358 //
Eli Bendersky17233942013-01-15 22:59:42 +00001359 // 1. The target-specific assembly parser. Some directives are target
1360 // specific or may potentially behave differently on certain targets.
1361 // 2. Asm parser extensions. For example, platform-specific parsers
1362 // (like the ELF parser) register themselves as extensions.
1363 // 3. The generic directive parser implemented by this class. These are
1364 // all the directives that behave in a target and platform independent
1365 // manner, or at least have a default behavior that's shared between
1366 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001367
Eli Bendersky17233942013-01-15 22:59:42 +00001368 // First query the target-specific parser. It will return 'true' if it
1369 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001370 if (!getTargetParser().ParseDirective(ID))
1371 return false;
1372
Alp Tokercb402912014-01-24 17:20:08 +00001373 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001374 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001375 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1376 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001377 if (Handler.first)
1378 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1379
1380 // Finally, if no one else is interested in this directive, it must be
1381 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001382 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001383 default:
1384 break;
1385 case DK_SET:
1386 case DK_EQU:
1387 return parseDirectiveSet(IDVal, true);
1388 case DK_EQUIV:
1389 return parseDirectiveSet(IDVal, false);
1390 case DK_ASCII:
1391 return parseDirectiveAscii(IDVal, false);
1392 case DK_ASCIZ:
1393 case DK_STRING:
1394 return parseDirectiveAscii(IDVal, true);
1395 case DK_BYTE:
1396 return parseDirectiveValue(1);
1397 case DK_SHORT:
1398 case DK_VALUE:
1399 case DK_2BYTE:
1400 return parseDirectiveValue(2);
1401 case DK_LONG:
1402 case DK_INT:
1403 case DK_4BYTE:
1404 return parseDirectiveValue(4);
1405 case DK_QUAD:
1406 case DK_8BYTE:
1407 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001408 case DK_OCTA:
1409 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001410 case DK_SINGLE:
1411 case DK_FLOAT:
1412 return parseDirectiveRealValue(APFloat::IEEEsingle);
1413 case DK_DOUBLE:
1414 return parseDirectiveRealValue(APFloat::IEEEdouble);
1415 case DK_ALIGN: {
1416 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1417 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1418 }
1419 case DK_ALIGN32: {
1420 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1421 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1422 }
1423 case DK_BALIGN:
1424 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1425 case DK_BALIGNW:
1426 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1427 case DK_BALIGNL:
1428 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1429 case DK_P2ALIGN:
1430 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1431 case DK_P2ALIGNW:
1432 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1433 case DK_P2ALIGNL:
1434 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1435 case DK_ORG:
1436 return parseDirectiveOrg();
1437 case DK_FILL:
1438 return parseDirectiveFill();
1439 case DK_ZERO:
1440 return parseDirectiveZero();
1441 case DK_EXTERN:
1442 eatToEndOfStatement(); // .extern is the default, ignore it.
1443 return false;
1444 case DK_GLOBL:
1445 case DK_GLOBAL:
1446 return parseDirectiveSymbolAttribute(MCSA_Global);
1447 case DK_LAZY_REFERENCE:
1448 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1449 case DK_NO_DEAD_STRIP:
1450 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1451 case DK_SYMBOL_RESOLVER:
1452 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1453 case DK_PRIVATE_EXTERN:
1454 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1455 case DK_REFERENCE:
1456 return parseDirectiveSymbolAttribute(MCSA_Reference);
1457 case DK_WEAK_DEFINITION:
1458 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1459 case DK_WEAK_REFERENCE:
1460 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1461 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1462 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1463 case DK_COMM:
1464 case DK_COMMON:
1465 return parseDirectiveComm(/*IsLocal=*/false);
1466 case DK_LCOMM:
1467 return parseDirectiveComm(/*IsLocal=*/true);
1468 case DK_ABORT:
1469 return parseDirectiveAbort();
1470 case DK_INCLUDE:
1471 return parseDirectiveInclude();
1472 case DK_INCBIN:
1473 return parseDirectiveIncbin();
1474 case DK_CODE16:
1475 case DK_CODE16GCC:
1476 return TokError(Twine(IDVal) + " not supported yet");
1477 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001478 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001479 case DK_IRP:
1480 return parseDirectiveIrp(IDLoc);
1481 case DK_IRPC:
1482 return parseDirectiveIrpc(IDLoc);
1483 case DK_ENDR:
1484 return parseDirectiveEndr(IDLoc);
1485 case DK_BUNDLE_ALIGN_MODE:
1486 return parseDirectiveBundleAlignMode();
1487 case DK_BUNDLE_LOCK:
1488 return parseDirectiveBundleLock();
1489 case DK_BUNDLE_UNLOCK:
1490 return parseDirectiveBundleUnlock();
1491 case DK_SLEB128:
1492 return parseDirectiveLEB128(true);
1493 case DK_ULEB128:
1494 return parseDirectiveLEB128(false);
1495 case DK_SPACE:
1496 case DK_SKIP:
1497 return parseDirectiveSpace(IDVal);
1498 case DK_FILE:
1499 return parseDirectiveFile(IDLoc);
1500 case DK_LINE:
1501 return parseDirectiveLine();
1502 case DK_LOC:
1503 return parseDirectiveLoc();
1504 case DK_STABS:
1505 return parseDirectiveStabs();
1506 case DK_CFI_SECTIONS:
1507 return parseDirectiveCFISections();
1508 case DK_CFI_STARTPROC:
1509 return parseDirectiveCFIStartProc();
1510 case DK_CFI_ENDPROC:
1511 return parseDirectiveCFIEndProc();
1512 case DK_CFI_DEF_CFA:
1513 return parseDirectiveCFIDefCfa(IDLoc);
1514 case DK_CFI_DEF_CFA_OFFSET:
1515 return parseDirectiveCFIDefCfaOffset();
1516 case DK_CFI_ADJUST_CFA_OFFSET:
1517 return parseDirectiveCFIAdjustCfaOffset();
1518 case DK_CFI_DEF_CFA_REGISTER:
1519 return parseDirectiveCFIDefCfaRegister(IDLoc);
1520 case DK_CFI_OFFSET:
1521 return parseDirectiveCFIOffset(IDLoc);
1522 case DK_CFI_REL_OFFSET:
1523 return parseDirectiveCFIRelOffset(IDLoc);
1524 case DK_CFI_PERSONALITY:
1525 return parseDirectiveCFIPersonalityOrLsda(true);
1526 case DK_CFI_LSDA:
1527 return parseDirectiveCFIPersonalityOrLsda(false);
1528 case DK_CFI_REMEMBER_STATE:
1529 return parseDirectiveCFIRememberState();
1530 case DK_CFI_RESTORE_STATE:
1531 return parseDirectiveCFIRestoreState();
1532 case DK_CFI_SAME_VALUE:
1533 return parseDirectiveCFISameValue(IDLoc);
1534 case DK_CFI_RESTORE:
1535 return parseDirectiveCFIRestore(IDLoc);
1536 case DK_CFI_ESCAPE:
1537 return parseDirectiveCFIEscape();
1538 case DK_CFI_SIGNAL_FRAME:
1539 return parseDirectiveCFISignalFrame();
1540 case DK_CFI_UNDEFINED:
1541 return parseDirectiveCFIUndefined(IDLoc);
1542 case DK_CFI_REGISTER:
1543 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001544 case DK_CFI_WINDOW_SAVE:
1545 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 case DK_MACROS_ON:
1547 case DK_MACROS_OFF:
1548 return parseDirectiveMacrosOnOff(IDVal);
1549 case DK_MACRO:
1550 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001551 case DK_EXITM:
1552 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001553 case DK_ENDM:
1554 case DK_ENDMACRO:
1555 return parseDirectiveEndMacro(IDVal);
1556 case DK_PURGEM:
1557 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001558 case DK_END:
1559 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001560 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001561 return parseDirectiveError(IDLoc, false);
1562 case DK_ERROR:
1563 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001564 case DK_WARNING:
1565 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001566 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001567
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001568 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001569 }
Chris Lattner36e02122009-06-21 20:54:55 +00001570
Chad Rosierc7f552c2013-02-12 21:33:51 +00001571 // __asm _emit or __asm __emit
1572 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1573 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001575
1576 // __asm align
1577 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001579
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001580 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001581
Chris Lattner7cbfa442010-05-19 23:34:33 +00001582 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001583 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001584 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001586 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001587 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001588
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001589 // Dump the parsed representation, if requested.
1590 if (getShowParsedOperands()) {
1591 SmallString<256> Str;
1592 raw_svector_ostream OS(Str);
1593 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001594 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001595 if (i != 0)
1596 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001597 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001598 }
1599 OS << "]";
1600
Jim Grosbach4b905842013-09-20 23:08:21 +00001601 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001602 }
1603
Oliver Stannard8b273082014-06-19 15:52:37 +00001604 // If we are generating dwarf for the current section then generate a .loc
1605 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001606 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001607 getContext().getGenDwarfSectionSyms().count(
1608 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001609
Eli Bendersky88024712013-01-16 19:32:36 +00001610 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001611
Eli Bendersky88024712013-01-16 19:32:36 +00001612 // If we previously parsed a cpp hash file line comment then make sure the
1613 // current Dwarf File is for the CppHashFilename if not then emit the
1614 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001615 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001616 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1617 0, StringRef(), CppHashFilename);
1618 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001619
Jim Grosbach4b905842013-09-20 23:08:21 +00001620 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1621 // cache with the different Loc from the call above we save the last
1622 // info we queried here with SrcMgr.FindLineNumber().
1623 unsigned CppHashLocLineNo;
1624 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1625 CppHashLocLineNo = LastQueryLine;
1626 else {
1627 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1628 LastQueryLine = CppHashLocLineNo;
1629 LastQueryIDLoc = CppHashLoc;
1630 LastQueryBuffer = CppHashBuf;
1631 }
1632 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001633 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001634
Jim Grosbach4b905842013-09-20 23:08:21 +00001635 getStreamer().EmitDwarfLocDirective(
1636 getContext().getGenDwarfFileNumber(), Line, 0,
1637 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1638 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001639 }
1640
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001641 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001642 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001643 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001644 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1645 Info.ParsedOperands, Out,
1646 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001647 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001648
Chris Lattnera2a9d162010-09-11 16:18:25 +00001649 // Don't skip the rest of the line, the instruction parser is responsible for
1650 // that.
1651 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001652}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001653
Jim Grosbach4b905842013-09-20 23:08:21 +00001654/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001655/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001656void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001657 if (!Lexer.is(AsmToken::EndOfStatement))
1658 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001659 // Eat EOL.
1660 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001661}
1662
Jim Grosbach4b905842013-09-20 23:08:21 +00001663/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001664/// ::= # number "filename"
1665/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001666bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001667 Lex(); // Eat the hash token.
1668
1669 if (getLexer().isNot(AsmToken::Integer)) {
1670 // Consume the line since in cases it is not a well-formed line directive,
1671 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001672 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001673 return false;
1674 }
1675
1676 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001677 Lex();
1678
1679 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001680 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001681 return false;
1682 }
1683
1684 StringRef Filename = getTok().getString();
1685 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001686 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001687
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001688 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1689 CppHashLoc = L;
1690 CppHashFilename = Filename;
1691 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001692 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001693
1694 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001695 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001696 return false;
1697}
1698
Jim Grosbach4b905842013-09-20 23:08:21 +00001699/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700/// for the Filename and LineNo if any in the diagnostic.
1701void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001702 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703 raw_ostream &OS = errs();
1704
1705 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1706 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001707 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1708 unsigned CppHashBuf =
1709 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710
Jim Grosbach4b905842013-09-20 23:08:21 +00001711 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001713 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1714 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1715 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001716 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1717 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718 }
1719
Eric Christophera7c32732012-12-18 00:30:54 +00001720 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721 // manager changed or buffer changed (like in a nested include) then just
1722 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001723 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001725 if (Parser->SavedDiagHandler)
1726 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1727 else
Craig Topper353eda42014-04-24 06:44:33 +00001728 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001729 return;
1730 }
1731
Eric Christophera7c32732012-12-18 00:30:54 +00001732 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001733 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1734 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001735 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001736
1737 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1738 int CppHashLocLineNo =
1739 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001740 int LineNo =
1741 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001742
Jim Grosbach4b905842013-09-20 23:08:21 +00001743 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1744 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001745 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001746
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001747 if (Parser->SavedDiagHandler)
1748 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1749 else
Craig Topper353eda42014-04-24 06:44:33 +00001750 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001751}
1752
Rafael Espindola2c064482012-08-21 18:29:30 +00001753// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1754// difference being that that function accepts '@' as part of identifiers and
1755// we can't do that. AsmLexer.cpp should probably be changed to handle
1756// '@' as a special case when needed.
1757static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001758 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1759 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001760}
1761
Rafael Espindola34b9c512012-06-03 23:57:14 +00001762bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001763 ArrayRef<MCAsmMacroParameter> Parameters,
1764 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001765 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001766 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001767 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001768 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001769
Preston Gurd05500642012-09-19 20:36:12 +00001770 // A macro without parameters is handled differently on Darwin:
1771 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001772 while (!Body.empty()) {
1773 // Scan for the next substitution.
1774 std::size_t End = Body.size(), Pos = 0;
1775 for (; Pos != End; ++Pos) {
1776 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001777 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001778 // This macro has no parameters, look for $0, $1, etc.
1779 if (Body[Pos] != '$' || Pos + 1 == End)
1780 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001781
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001783 if (Next == '$' || Next == 'n' ||
1784 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001785 break;
1786 } else {
1787 // This macro has parameters, look for \foo, \bar, etc.
1788 if (Body[Pos] == '\\' && Pos + 1 != End)
1789 break;
1790 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001791 }
1792
1793 // Add the prefix.
1794 OS << Body.slice(0, Pos);
1795
1796 // Check if we reached the end.
1797 if (Pos == End)
1798 break;
1799
Benjamin Kramer513e7442014-02-20 13:36:32 +00001800 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001801 switch (Body[Pos + 1]) {
1802 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001803 case '$':
1804 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001805 break;
1806
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001808 case 'n':
1809 OS << A.size();
1810 break;
1811
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001813 default: {
1814 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001815 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816 if (Index >= A.size())
1817 break;
1818
1819 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001820 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001821 ie = A[Index].end();
1822 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 OS << it->getString();
1824 break;
1825 }
1826 }
1827 Pos += 2;
1828 } else {
1829 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001830 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 ++I;
1832
Jim Grosbach4b905842013-09-20 23:08:21 +00001833 const char *Begin = Body.data() + Pos + 1;
1834 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001835 unsigned Index = 0;
1836 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001837 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001838 break;
1839
Preston Gurd05500642012-09-19 20:36:12 +00001840 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001841 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1842 Pos += 3;
1843 else {
1844 OS << '\\' << Argument;
1845 Pos = I;
1846 }
Preston Gurd05500642012-09-19 20:36:12 +00001847 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001848 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001849 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001850 ie = A[Index].end();
1851 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001852 // We expect no quotes around the string's contents when
1853 // parsing for varargs.
1854 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001855 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001856 else
1857 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001858
Preston Gurd05500642012-09-19 20:36:12 +00001859 Pos += 1 + Argument.size();
1860 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001861 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001862 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001863 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001864 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001865
Rafael Espindola1134ab232011-06-05 02:43:45 +00001866 return false;
1867}
Daniel Dunbar43235712010-07-18 18:54:11 +00001868
Nico Weber2a8f9222014-07-24 16:29:04 +00001869MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001870 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001871 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001872 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001873
Jim Grosbach4b905842013-09-20 23:08:21 +00001874static bool isOperator(AsmToken::TokenKind kind) {
1875 switch (kind) {
1876 default:
1877 return false;
1878 case AsmToken::Plus:
1879 case AsmToken::Minus:
1880 case AsmToken::Tilde:
1881 case AsmToken::Slash:
1882 case AsmToken::Star:
1883 case AsmToken::Dot:
1884 case AsmToken::Equal:
1885 case AsmToken::EqualEqual:
1886 case AsmToken::Pipe:
1887 case AsmToken::PipePipe:
1888 case AsmToken::Caret:
1889 case AsmToken::Amp:
1890 case AsmToken::AmpAmp:
1891 case AsmToken::Exclaim:
1892 case AsmToken::ExclaimEqual:
1893 case AsmToken::Percent:
1894 case AsmToken::Less:
1895 case AsmToken::LessEqual:
1896 case AsmToken::LessLess:
1897 case AsmToken::LessGreater:
1898 case AsmToken::Greater:
1899 case AsmToken::GreaterEqual:
1900 case AsmToken::GreaterGreater:
1901 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001902 }
1903}
1904
David Majnemer16252452014-01-29 00:07:39 +00001905namespace {
1906class AsmLexerSkipSpaceRAII {
1907public:
1908 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1909 Lexer.setSkipSpace(SkipSpace);
1910 }
1911
1912 ~AsmLexerSkipSpaceRAII() {
1913 Lexer.setSkipSpace(true);
1914 }
1915
1916private:
1917 AsmLexer &Lexer;
1918};
1919}
1920
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001921bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1922
1923 if (Vararg) {
1924 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1925 StringRef Str = parseStringToEndOfStatement();
1926 MA.push_back(AsmToken(AsmToken::String, Str));
1927 }
1928 return false;
1929 }
1930
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001932 unsigned AddTokens = 0;
1933
David Majnemer16252452014-01-29 00:07:39 +00001934 // Darwin doesn't use spaces to delmit arguments.
1935 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936
1937 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001938 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001939 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001940
David Majnemer91fc4c22014-01-29 18:57:46 +00001941 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001942 break;
Preston Gurd05500642012-09-19 20:36:12 +00001943
1944 if (Lexer.is(AsmToken::Space)) {
1945 Lex(); // Eat spaces
1946
1947 // Spaces can delimit parameters, but could also be part an expression.
1948 // If the token after a space is an operator, add the token and the next
1949 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001950 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001951 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001952 // Check to see whether the token is used as an operator,
1953 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001954 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001955 if (*NextChar == ' ')
1956 AddTokens = 2;
1957 }
1958
1959 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001960 break;
1961 }
1962 }
1963 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964
Jim Grosbach4b905842013-09-20 23:08:21 +00001965 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001966 // to be able to fill in the remaining default parameter values
1967 if (Lexer.is(AsmToken::EndOfStatement))
1968 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001969
1970 // Adjust the current parentheses level.
1971 if (Lexer.is(AsmToken::LParen))
1972 ++ParenLevel;
1973 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1974 --ParenLevel;
1975
1976 // Append the token to the current argument list.
1977 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001978 if (AddTokens)
1979 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980 Lex();
1981 }
Preston Gurd05500642012-09-19 20:36:12 +00001982
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001984 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001985 return false;
1986}
1987
1988// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001989bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001990 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001991 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001992 bool NamedParametersFound = false;
1993 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001994
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001995 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001996 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001997
Rafael Espindola768b41c2012-06-15 14:02:34 +00001998 // Parse two kinds of macro invocations:
1999 // - macros defined without any parameters accept an arbitrary number of them
2000 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002001 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002002 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2003 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002004 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002005 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002006
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002007 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002008 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002009 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002010 eatToEndOfStatement();
2011 return true;
2012 }
2013
2014 if (!Lexer.is(AsmToken::Equal)) {
2015 TokError("expected '=' after formal parameter identifier");
2016 eatToEndOfStatement();
2017 return true;
2018 }
2019 Lex();
2020
2021 NamedParametersFound = true;
2022 }
2023
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002024 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002025 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002026 eatToEndOfStatement();
2027 return true;
2028 }
2029
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002030 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2031 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002032 return true;
2033
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002034 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002035 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036 unsigned FAI = 0;
2037 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002038 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002039 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002040
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002041 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002042 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002043 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002044 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002045 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002046 return true;
2047 }
2048 PI = FAI;
2049 }
2050
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002051 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002052 if (A.size() <= PI)
2053 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002054 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002055
2056 if (FALocs.size() <= PI)
2057 FALocs.resize(PI + 1);
2058
2059 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002060 }
Jim Grosbach206661622012-07-30 22:44:17 +00002061
Preston Gurd242ed3152012-09-19 20:29:04 +00002062 // At the end of the statement, fill in remaining arguments that have
2063 // default values. If there aren't any, then the next argument is
2064 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002065 if (Lexer.is(AsmToken::EndOfStatement)) {
2066 bool Failure = false;
2067 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2068 if (A[FAI].empty()) {
2069 if (M->Parameters[FAI].Required) {
2070 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2071 "missing value for required parameter "
2072 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2073 Failure = true;
2074 }
2075
2076 if (!M->Parameters[FAI].Value.empty())
2077 A[FAI] = M->Parameters[FAI].Value;
2078 }
2079 }
2080 return Failure;
2081 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002082
2083 if (Lexer.is(AsmToken::Comma))
2084 Lex();
2085 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002086
2087 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002088}
2089
Jim Grosbach4b905842013-09-20 23:08:21 +00002090const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2091 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002092 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002093}
2094
Jim Grosbach4b905842013-09-20 23:08:21 +00002095void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002096 MacroMap[Name] = new MCAsmMacro(Macro);
2097}
2098
Jim Grosbach4b905842013-09-20 23:08:21 +00002099void AsmParser::undefineMacro(StringRef Name) {
2100 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002101 if (I != MacroMap.end()) {
2102 delete I->getValue();
2103 MacroMap.erase(I);
2104 }
2105}
2106
Jim Grosbach4b905842013-09-20 23:08:21 +00002107bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002108 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2109 // this, although we should protect against infinite loops.
2110 if (ActiveMacros.size() == 20)
2111 return TokError("macros cannot be nested more than 20 levels deep");
2112
Eli Bendersky38274122013-01-14 23:22:36 +00002113 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002114 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002115 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002116
Rafael Espindola1134ab232011-06-05 02:43:45 +00002117 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2118 // to hold the macro body with substitutions.
2119 SmallString<256> Buf;
2120 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002121 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002122
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002123 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002124 return true;
2125
Eli Bendersky38274122013-01-14 23:22:36 +00002126 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002127 // instantiation.
2128 OS << ".endmacro\n";
2129
Rafael Espindola3560ff22014-08-27 20:03:13 +00002130 std::unique_ptr<MemoryBuffer> Instantiation =
2131 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002132
Daniel Dunbar43235712010-07-18 18:54:11 +00002133 // Create the macro instantiation object and add to the current macro
2134 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002135 MacroInstantiation *MI = new MacroInstantiation(
2136 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002137 ActiveMacros.push_back(MI);
2138
2139 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002140 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002141 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002142 Lex();
2143
2144 return false;
2145}
2146
Jim Grosbach4b905842013-09-20 23:08:21 +00002147void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002148 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002150 Lex();
2151
2152 // Pop the instantiation entry.
2153 delete ActiveMacros.back();
2154 ActiveMacros.pop_back();
2155}
2156
Jim Grosbach4b905842013-09-20 23:08:21 +00002157static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002158 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002159 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2161 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002162 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002163 case MCExpr::Target:
2164 case MCExpr::Constant:
2165 return false;
2166 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 const MCSymbol &S =
2168 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002169 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002170 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002171 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002172 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002173 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002174 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002175 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002176
2177 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002178}
2179
Jim Grosbach4b905842013-09-20 23:08:21 +00002180bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002181 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002182 // FIXME: Use better location, we should use proper tokens.
2183 SMLoc EqualLoc = Lexer.getLoc();
2184
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002185 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002186 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002188
Rafael Espindola72f5f172012-01-28 05:57:00 +00002189 // Note: we don't count b as used in "a = b". This is to allow
2190 // a = b
2191 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002192
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002193 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002194 return TokError("unexpected token in assignment");
2195
2196 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002197 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002198
Daniel Dunbar5f339242009-10-16 01:57:39 +00002199 // Validate that the LHS is allowed to be a variable (either it has not been
2200 // used as a symbol, or it is an absolute symbol).
2201 MCSymbol *Sym = getContext().LookupSymbol(Name);
2202 if (Sym) {
2203 // Diagnose assignment to a label.
2204 //
2205 // FIXME: Diagnostics. Note the location of the definition as a label.
2206 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002207 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002208 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2209 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002210 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002211 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2212 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002213 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002214 return Error(EqualLoc, "redefinition of '" + Name + "'");
2215 else if (!Sym->isVariable())
2216 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002217 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002218 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002219 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002220
2221 // Don't count these checks as uses.
2222 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002223 } else if (Name == ".") {
2224 if (Out.EmitValueToOffset(Value, 0)) {
2225 Error(EqualLoc, "expected absolute expression");
2226 eatToEndOfStatement();
2227 }
2228 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002229 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002230 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002231
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002232 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002233 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002234 if (NoDeadStrip)
2235 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2236
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002237 return false;
2238}
2239
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002240/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002241/// ::= identifier
2242/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002243bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002245 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2246 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002247 // handle this as a context dependent token, instead we detect adjacent tokens
2248 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002249 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2250 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002251
Hans Wennborgce69d772013-10-18 20:46:28 +00002252 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002253 Lex();
2254 if (Lexer.isNot(AsmToken::Identifier))
2255 return true;
2256
Hans Wennborgce69d772013-10-18 20:46:28 +00002257 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2258 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002259 return true;
2260
2261 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002262 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002263 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002264 Lex();
2265 return false;
2266 }
2267
Jim Grosbach4b905842013-09-20 23:08:21 +00002268 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002269 return true;
2270
Sean Callanan936b0d32010-01-19 21:44:56 +00002271 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002272
Sean Callanan686ed8d2010-01-19 20:22:31 +00002273 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274
2275 return false;
2276}
2277
Jim Grosbach4b905842013-09-20 23:08:21 +00002278/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002279/// ::= .equ identifier ',' expression
2280/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002281/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002282bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002283 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002285 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002286 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002287
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002288 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002289 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002290 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002293}
2294
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002295bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002296 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002297
2298 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002299 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002300 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2301 if (Str[i] != '\\') {
2302 Data += Str[i];
2303 continue;
2304 }
2305
2306 // Recognize escaped characters. Note that this escape semantics currently
2307 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2308 ++i;
2309 if (i == e)
2310 return TokError("unexpected backslash at end of string");
2311
2312 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002313 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002314 // Consume up to three octal characters.
2315 unsigned Value = Str[i] - '0';
2316
Jim Grosbach4b905842013-09-20 23:08:21 +00002317 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002318 ++i;
2319 Value = Value * 8 + (Str[i] - '0');
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 ++i;
2323 Value = Value * 8 + (Str[i] - '0');
2324 }
2325 }
2326
2327 if (Value > 255)
2328 return TokError("invalid octal escape sequence (out of range)");
2329
Jim Grosbach4b905842013-09-20 23:08:21 +00002330 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002331 continue;
2332 }
2333
2334 // Otherwise recognize individual escapes.
2335 switch (Str[i]) {
2336 default:
2337 // Just reject invalid escape sequences for now.
2338 return TokError("invalid escape sequence (unrecognized character)");
2339
2340 case 'b': Data += '\b'; break;
2341 case 'f': Data += '\f'; break;
2342 case 'n': Data += '\n'; break;
2343 case 'r': Data += '\r'; break;
2344 case 't': Data += '\t'; break;
2345 case '"': Data += '"'; break;
2346 case '\\': Data += '\\'; break;
2347 }
2348 }
2349
2350 return false;
2351}
2352
Jim Grosbach4b905842013-09-20 23:08:21 +00002353/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002354/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002355bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002357 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002358
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002360 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002361 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002362
Daniel Dunbaref668c12009-08-14 18:19:52 +00002363 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002364 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002365 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002366
Rafael Espindola64e1af82013-07-02 15:49:13 +00002367 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002368 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002369 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002370
Sean Callanan686ed8d2010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002372
2373 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 break;
2375
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002377 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002378 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002379 }
2380 }
2381
Sean Callanan686ed8d2010-01-19 20:22:31 +00002382 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 return false;
2384}
2385
Jim Grosbach4b905842013-09-20 23:08:21 +00002386/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002388bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002389 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002391
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002393 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002394 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002395 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002396 return true;
2397
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002398 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002399 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2400 assert(Size <= 8 && "Invalid size");
2401 uint64_t IntValue = MCE->getValue();
2402 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2403 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002404 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002405 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002406 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002407
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002408 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002409 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002410
Daniel Dunbara10e5192009-06-24 23:30:00 +00002411 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002412 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002413 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002414 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002415 }
2416 }
2417
Sean Callanan686ed8d2010-01-19 20:22:31 +00002418 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002419 return false;
2420}
2421
David Woodhoused6de0d92014-02-01 16:20:59 +00002422/// ParseDirectiveOctaValue
2423/// ::= .octa [ hexconstant (, hexconstant)* ]
2424bool AsmParser::parseDirectiveOctaValue() {
2425 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2426 checkForValidSection();
2427
2428 for (;;) {
2429 if (Lexer.getKind() == AsmToken::Error)
2430 return true;
2431 if (Lexer.getKind() != AsmToken::Integer &&
2432 Lexer.getKind() != AsmToken::BigNum)
2433 return TokError("unknown token in expression");
2434
2435 SMLoc ExprLoc = getLexer().getLoc();
2436 APInt IntValue = getTok().getAPIntVal();
2437 Lex();
2438
2439 uint64_t hi, lo;
2440 if (IntValue.isIntN(64)) {
2441 hi = 0;
2442 lo = IntValue.getZExtValue();
2443 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002444 // It might actually have more than 128 bits, but the top ones are zero.
2445 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002446 lo = IntValue.getLoBits(64).getZExtValue();
2447 } else
2448 return Error(ExprLoc, "literal value out of range for directive");
2449
2450 if (MAI.isLittleEndian()) {
2451 getStreamer().EmitIntValue(lo, 8);
2452 getStreamer().EmitIntValue(hi, 8);
2453 } else {
2454 getStreamer().EmitIntValue(hi, 8);
2455 getStreamer().EmitIntValue(lo, 8);
2456 }
2457
2458 if (getLexer().is(AsmToken::EndOfStatement))
2459 break;
2460
2461 // FIXME: Improve diagnostic.
2462 if (getLexer().isNot(AsmToken::Comma))
2463 return TokError("unexpected token in directive");
2464 Lex();
2465 }
2466 }
2467
2468 Lex();
2469 return false;
2470}
2471
Jim Grosbach4b905842013-09-20 23:08:21 +00002472/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002473/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002474bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002475 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002476 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002477
2478 for (;;) {
2479 // We don't truly support arithmetic on floating point expressions, so we
2480 // have to manually parse unary prefixes.
2481 bool IsNeg = false;
2482 if (getLexer().is(AsmToken::Minus)) {
2483 Lex();
2484 IsNeg = true;
2485 } else if (getLexer().is(AsmToken::Plus))
2486 Lex();
2487
Michael J. Spencer530ce852010-10-09 11:00:50 +00002488 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002489 getLexer().isNot(AsmToken::Real) &&
2490 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002491 return TokError("unexpected token in directive");
2492
2493 // Convert to an APFloat.
2494 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002495 StringRef IDVal = getTok().getString();
2496 if (getLexer().is(AsmToken::Identifier)) {
2497 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2498 Value = APFloat::getInf(Semantics);
2499 else if (!IDVal.compare_lower("nan"))
2500 Value = APFloat::getNaN(Semantics, false, ~0);
2501 else
2502 return TokError("invalid floating point literal");
2503 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002504 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002505 return TokError("invalid floating point literal");
2506 if (IsNeg)
2507 Value.changeSign();
2508
2509 // Consume the numeric token.
2510 Lex();
2511
2512 // Emit the value as an integer.
2513 APInt AsInt = Value.bitcastToAPInt();
2514 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002515 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002516
2517 if (getLexer().is(AsmToken::EndOfStatement))
2518 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002519
Daniel Dunbar2af16532010-09-24 01:59:56 +00002520 if (getLexer().isNot(AsmToken::Comma))
2521 return TokError("unexpected token in directive");
2522 Lex();
2523 }
2524 }
2525
2526 Lex();
2527 return false;
2528}
2529
Jim Grosbach4b905842013-09-20 23:08:21 +00002530/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002531/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002532bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002534
2535 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002536 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 return true;
2538
Rafael Espindolab91bac62010-10-05 19:42:57 +00002539 int64_t Val = 0;
2540 if (getLexer().is(AsmToken::Comma)) {
2541 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002542 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002543 return true;
2544 }
2545
Rafael Espindola922e3f42010-09-16 15:03:59 +00002546 if (getLexer().isNot(AsmToken::EndOfStatement))
2547 return TokError("unexpected token in '.zero' directive");
2548
2549 Lex();
2550
Rafael Espindola64e1af82013-07-02 15:49:13 +00002551 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002552
2553 return false;
2554}
2555
Jim Grosbach4b905842013-09-20 23:08:21 +00002556/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002557/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002558bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002559 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002560
David Majnemer522d3db2014-02-01 07:19:38 +00002561 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002562 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002563 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002564 return true;
2565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 if (NumValues < 0) {
2567 Warning(RepeatLoc,
2568 "'.fill' directive with negative repeat count has no effect");
2569 NumValues = 0;
2570 }
2571
Roman Divackye33098f2013-09-24 17:44:41 +00002572 int64_t FillSize = 1;
2573 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002574
David Majnemer522d3db2014-02-01 07:19:38 +00002575 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2577 if (getLexer().isNot(AsmToken::Comma))
2578 return TokError("unexpected token in '.fill' directive");
2579 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002580
David Majnemer522d3db2014-02-01 07:19:38 +00002581 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillSize))
2583 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002584
Roman Divackye33098f2013-09-24 17:44:41 +00002585 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2586 if (getLexer().isNot(AsmToken::Comma))
2587 return TokError("unexpected token in '.fill' directive");
2588 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002589
David Majnemer522d3db2014-02-01 07:19:38 +00002590 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002591 if (parseAbsoluteExpression(FillExpr))
2592 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002593
Roman Divackye33098f2013-09-24 17:44:41 +00002594 if (getLexer().isNot(AsmToken::EndOfStatement))
2595 return TokError("unexpected token in '.fill' directive");
2596
2597 Lex();
2598 }
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (FillSize < 0) {
2602 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2603 NumValues = 0;
2604 }
2605 if (FillSize > 8) {
2606 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2607 FillSize = 8;
2608 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002609
David Majnemer522d3db2014-02-01 07:19:38 +00002610 if (!isUInt<32>(FillExpr) && FillSize > 4)
2611 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2612
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002613 if (NumValues > 0) {
2614 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2615 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2616 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2617 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2618 if (NonZeroFillSize < FillSize)
2619 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2620 }
David Majnemer522d3db2014-02-01 07:19:38 +00002621 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002622
2623 return false;
2624}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002625
Jim Grosbach4b905842013-09-20 23:08:21 +00002626/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002627/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002628bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002629 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002630
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002631 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002632 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002633 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002634 return true;
2635
2636 // Parse optional fill expression.
2637 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002638 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2639 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002640 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002641 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002642
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002643 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002644 return true;
2645
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002646 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002647 return TokError("unexpected token in '.org' directive");
2648 }
2649
Sean Callanan686ed8d2010-01-19 20:22:31 +00002650 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002651
Jim Grosbachb5912772012-01-27 00:37:08 +00002652 // Only limited forms of relocatable expressions are accepted here, it
2653 // has to be relative to the current section. The streamer will return
2654 // 'true' if the expression wasn't evaluatable.
2655 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2656 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002657
2658 return false;
2659}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660
Jim Grosbach4b905842013-09-20 23:08:21 +00002661/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002663bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002664 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002665
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002666 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002668 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669 return true;
2670
2671 SMLoc MaxBytesLoc;
2672 bool HasFillExpr = false;
2673 int64_t FillExpr = 0;
2674 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002675 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2676 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002678 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679
2680 // The fill expression can be omitted while specifying a maximum number of
2681 // alignment bytes, e.g:
2682 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002683 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002685 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 return true;
2687 }
2688
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2690 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002692 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002693
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002694 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002695 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002697
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002698 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699 return TokError("unexpected token in directive");
2700 }
2701 }
2702
Sean Callanan686ed8d2010-01-19 20:22:31 +00002703 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002704
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002705 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002706 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002707
2708 // Compute alignment in bytes.
2709 if (IsPow2) {
2710 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002711 if (Alignment >= 32) {
2712 Error(AlignmentLoc, "invalid alignment value");
2713 Alignment = 31;
2714 }
2715
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002716 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002717 } else {
2718 // Reject alignments that aren't a power of two, for gas compatibility.
2719 if (!isPowerOf2_64(Alignment))
2720 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002721 }
2722
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002723 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002724 if (MaxBytesLoc.isValid()) {
2725 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002726 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002727 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002728 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002729 }
2730
2731 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002732 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002733 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002734 MaxBytesToFill = 0;
2735 }
2736 }
2737
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002738 // Check whether we should use optimal code alignment for this .align
2739 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002740 const MCSection *Section = getStreamer().getCurrentSection().first;
2741 assert(Section && "must have section to emit alignment");
2742 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002743 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2744 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002745 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002746 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002747 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002748 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2749 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002750 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002751
2752 return false;
2753}
2754
Jim Grosbach4b905842013-09-20 23:08:21 +00002755/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002756/// ::= .file [number] filename
2757/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002758bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002759 // FIXME: I'm not sure what this is.
2760 int64_t FileNumber = -1;
2761 SMLoc FileNumberLoc = getLexer().getLoc();
2762 if (getLexer().is(AsmToken::Integer)) {
2763 FileNumber = getTok().getIntVal();
2764 Lex();
2765
2766 if (FileNumber < 1)
2767 return TokError("file number less than one");
2768 }
2769
2770 if (getLexer().isNot(AsmToken::String))
2771 return TokError("unexpected token in '.file' directive");
2772
2773 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002774 // Allow the strings to have escaped octal character sequence.
2775 std::string Path = getTok().getString();
2776 if (parseEscapedString(Path))
2777 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002778 Lex();
2779
2780 StringRef Directory;
2781 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002782 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002783 if (getLexer().is(AsmToken::String)) {
2784 if (FileNumber == -1)
2785 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002786 if (parseEscapedString(FilenameData))
2787 return true;
2788 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002789 Directory = Path;
2790 Lex();
2791 } else {
2792 Filename = Path;
2793 }
2794
2795 if (getLexer().isNot(AsmToken::EndOfStatement))
2796 return TokError("unexpected token in '.file' directive");
2797
2798 if (FileNumber == -1)
2799 getStreamer().EmitFileDirective(Filename);
2800 else {
2801 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002802 Error(DirectiveLoc,
2803 "input can't have .file dwarf directives when -g is "
2804 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002805
David Blaikiec714ef42014-03-17 01:52:11 +00002806 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2807 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002808 Error(FileNumberLoc, "file number already allocated");
2809 }
2810
2811 return false;
2812}
2813
Jim Grosbach4b905842013-09-20 23:08:21 +00002814/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002815/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002816bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002817 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2818 if (getLexer().isNot(AsmToken::Integer))
2819 return TokError("unexpected token in '.line' directive");
2820
2821 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002822 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002823 Lex();
2824
2825 // FIXME: Do something with the .line.
2826 }
2827
2828 if (getLexer().isNot(AsmToken::EndOfStatement))
2829 return TokError("unexpected token in '.line' directive");
2830
2831 return false;
2832}
2833
Jim Grosbach4b905842013-09-20 23:08:21 +00002834/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002835/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2836/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2837/// The first number is a file number, must have been previously assigned with
2838/// a .file directive, the second number is the line number and optionally the
2839/// third number is a column position (zero if not specified). The remaining
2840/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002841bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002842 if (getLexer().isNot(AsmToken::Integer))
2843 return TokError("unexpected token in '.loc' directive");
2844 int64_t FileNumber = getTok().getIntVal();
2845 if (FileNumber < 1)
2846 return TokError("file number less than one in '.loc' directive");
2847 if (!getContext().isValidDwarfFileNumber(FileNumber))
2848 return TokError("unassigned file number in '.loc' directive");
2849 Lex();
2850
2851 int64_t LineNumber = 0;
2852 if (getLexer().is(AsmToken::Integer)) {
2853 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002854 if (LineNumber < 0)
2855 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002856 Lex();
2857 }
2858
2859 int64_t ColumnPos = 0;
2860 if (getLexer().is(AsmToken::Integer)) {
2861 ColumnPos = getTok().getIntVal();
2862 if (ColumnPos < 0)
2863 return TokError("column position less than zero in '.loc' directive");
2864 Lex();
2865 }
2866
2867 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2868 unsigned Isa = 0;
2869 int64_t Discriminator = 0;
2870 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2871 for (;;) {
2872 if (getLexer().is(AsmToken::EndOfStatement))
2873 break;
2874
2875 StringRef Name;
2876 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002877 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002878 return TokError("unexpected token in '.loc' directive");
2879
2880 if (Name == "basic_block")
2881 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2882 else if (Name == "prologue_end")
2883 Flags |= DWARF2_FLAG_PROLOGUE_END;
2884 else if (Name == "epilogue_begin")
2885 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2886 else if (Name == "is_stmt") {
2887 Loc = getTok().getLoc();
2888 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002889 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002890 return true;
2891 // The expression must be the constant 0 or 1.
2892 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2893 int Value = MCE->getValue();
2894 if (Value == 0)
2895 Flags &= ~DWARF2_FLAG_IS_STMT;
2896 else if (Value == 1)
2897 Flags |= DWARF2_FLAG_IS_STMT;
2898 else
2899 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002900 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002901 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2902 }
Craig Topperf15655b2013-04-22 04:22:40 +00002903 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002904 Loc = getTok().getLoc();
2905 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002906 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002907 return true;
2908 // The expression must be a constant greater or equal to 0.
2909 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2910 int Value = MCE->getValue();
2911 if (Value < 0)
2912 return Error(Loc, "isa number less than zero");
2913 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002914 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002915 return Error(Loc, "isa number not a constant value");
2916 }
Craig Topperf15655b2013-04-22 04:22:40 +00002917 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002918 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002919 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002920 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002921 return Error(Loc, "unknown sub-directive in '.loc' directive");
2922 }
2923
2924 if (getLexer().is(AsmToken::EndOfStatement))
2925 break;
2926 }
2927 }
2928
2929 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2930 Isa, Discriminator, StringRef());
2931
2932 return false;
2933}
2934
Jim Grosbach4b905842013-09-20 23:08:21 +00002935/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002936/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002937bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002938 return TokError("unsupported directive '.stabs'");
2939}
2940
Jim Grosbach4b905842013-09-20 23:08:21 +00002941/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002942/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002943bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002944 StringRef Name;
2945 bool EH = false;
2946 bool Debug = false;
2947
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002948 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002949 return TokError("Expected an identifier");
2950
2951 if (Name == ".eh_frame")
2952 EH = true;
2953 else if (Name == ".debug_frame")
2954 Debug = true;
2955
2956 if (getLexer().is(AsmToken::Comma)) {
2957 Lex();
2958
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002959 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002960 return TokError("Expected an identifier");
2961
2962 if (Name == ".eh_frame")
2963 EH = true;
2964 else if (Name == ".debug_frame")
2965 Debug = true;
2966 }
2967
2968 getStreamer().EmitCFISections(EH, Debug);
2969 return false;
2970}
2971
Jim Grosbach4b905842013-09-20 23:08:21 +00002972/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002973/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002974bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002975 StringRef Simple;
2976 if (getLexer().isNot(AsmToken::EndOfStatement))
2977 if (parseIdentifier(Simple) || Simple != "simple")
2978 return TokError("unexpected token in .cfi_startproc directive");
2979
2980 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002981 return false;
2982}
2983
Jim Grosbach4b905842013-09-20 23:08:21 +00002984/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002985/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002986bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002987 getStreamer().EmitCFIEndProc();
2988 return false;
2989}
2990
Jim Grosbach4b905842013-09-20 23:08:21 +00002991/// \brief parse register name or number.
2992bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002993 SMLoc DirectiveLoc) {
2994 unsigned RegNo;
2995
2996 if (getLexer().isNot(AsmToken::Integer)) {
2997 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2998 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002999 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003000 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003001 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003002
3003 return false;
3004}
3005
Jim Grosbach4b905842013-09-20 23:08:21 +00003006/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003007/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003008bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003009 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003010 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003011 return true;
3012
3013 if (getLexer().isNot(AsmToken::Comma))
3014 return TokError("unexpected token in directive");
3015 Lex();
3016
3017 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003018 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003019 return true;
3020
3021 getStreamer().EmitCFIDefCfa(Register, Offset);
3022 return false;
3023}
3024
Jim Grosbach4b905842013-09-20 23:08:21 +00003025/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003026/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003027bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003028 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003029 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003030 return true;
3031
3032 getStreamer().EmitCFIDefCfaOffset(Offset);
3033 return false;
3034}
3035
Jim Grosbach4b905842013-09-20 23:08:21 +00003036/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003037/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003038bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003039 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003040 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003041 return true;
3042
3043 if (getLexer().isNot(AsmToken::Comma))
3044 return TokError("unexpected token in directive");
3045 Lex();
3046
3047 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003048 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003049 return true;
3050
3051 getStreamer().EmitCFIRegister(Register1, Register2);
3052 return false;
3053}
3054
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003055/// parseDirectiveCFIWindowSave
3056/// ::= .cfi_window_save
3057bool AsmParser::parseDirectiveCFIWindowSave() {
3058 getStreamer().EmitCFIWindowSave();
3059 return false;
3060}
3061
Jim Grosbach4b905842013-09-20 23:08:21 +00003062/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003063/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003064bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003065 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003066 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003067 return true;
3068
3069 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3070 return false;
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003077 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003078 return true;
3079
3080 getStreamer().EmitCFIDefCfaRegister(Register);
3081 return false;
3082}
3083
Jim Grosbach4b905842013-09-20 23:08:21 +00003084/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003085/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003086bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003087 int64_t Register = 0;
3088 int64_t Offset = 0;
3089
Jim Grosbach4b905842013-09-20 23:08:21 +00003090 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return true;
3092
3093 if (getLexer().isNot(AsmToken::Comma))
3094 return TokError("unexpected token in directive");
3095 Lex();
3096
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003097 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return true;
3099
3100 getStreamer().EmitCFIOffset(Register, Offset);
3101 return false;
3102}
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003105/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003106bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003107 int64_t Register = 0;
3108
Jim Grosbach4b905842013-09-20 23:08:21 +00003109 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return true;
3111
3112 if (getLexer().isNot(AsmToken::Comma))
3113 return TokError("unexpected token in directive");
3114 Lex();
3115
3116 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003117 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003118 return true;
3119
3120 getStreamer().EmitCFIRelOffset(Register, Offset);
3121 return false;
3122}
3123
3124static bool isValidEncoding(int64_t Encoding) {
3125 if (Encoding & ~0xff)
3126 return false;
3127
3128 if (Encoding == dwarf::DW_EH_PE_omit)
3129 return true;
3130
3131 const unsigned Format = Encoding & 0xf;
3132 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3133 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3134 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3135 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3136 return false;
3137
3138 const unsigned Application = Encoding & 0x70;
3139 if (Application != dwarf::DW_EH_PE_absptr &&
3140 Application != dwarf::DW_EH_PE_pcrel)
3141 return false;
3142
3143 return true;
3144}
3145
Jim Grosbach4b905842013-09-20 23:08:21 +00003146/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003147/// IsPersonality true for cfi_personality, false for cfi_lsda
3148/// ::= .cfi_personality encoding, [symbol_name]
3149/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003150bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003151 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003152 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003153 return true;
3154 if (Encoding == dwarf::DW_EH_PE_omit)
3155 return false;
3156
3157 if (!isValidEncoding(Encoding))
3158 return TokError("unsupported encoding.");
3159
3160 if (getLexer().isNot(AsmToken::Comma))
3161 return TokError("unexpected token in directive");
3162 Lex();
3163
3164 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003165 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003166 return TokError("expected identifier in directive");
3167
3168 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3169
3170 if (IsPersonality)
3171 getStreamer().EmitCFIPersonality(Sym, Encoding);
3172 else
3173 getStreamer().EmitCFILsda(Sym, Encoding);
3174 return false;
3175}
3176
Jim Grosbach4b905842013-09-20 23:08:21 +00003177/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003178/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003179bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003180 getStreamer().EmitCFIRememberState();
3181 return false;
3182}
3183
Jim Grosbach4b905842013-09-20 23:08:21 +00003184/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003185/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003186bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003187 getStreamer().EmitCFIRestoreState();
3188 return false;
3189}
3190
Jim Grosbach4b905842013-09-20 23:08:21 +00003191/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003192/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003193bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003194 int64_t Register = 0;
3195
Jim Grosbach4b905842013-09-20 23:08:21 +00003196 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003197 return true;
3198
3199 getStreamer().EmitCFISameValue(Register);
3200 return false;
3201}
3202
Jim Grosbach4b905842013-09-20 23:08:21 +00003203/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003204/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003205bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003206 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003207 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003208 return true;
3209
3210 getStreamer().EmitCFIRestore(Register);
3211 return false;
3212}
3213
Jim Grosbach4b905842013-09-20 23:08:21 +00003214/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003215/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003216bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003217 std::string Values;
3218 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003219 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003220 return true;
3221
3222 Values.push_back((uint8_t)CurrValue);
3223
3224 while (getLexer().is(AsmToken::Comma)) {
3225 Lex();
3226
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003227 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003228 return true;
3229
3230 Values.push_back((uint8_t)CurrValue);
3231 }
3232
3233 getStreamer().EmitCFIEscape(Values);
3234 return false;
3235}
3236
Jim Grosbach4b905842013-09-20 23:08:21 +00003237/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003238/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003239bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003240 if (getLexer().isNot(AsmToken::EndOfStatement))
3241 return Error(getLexer().getLoc(),
3242 "unexpected token in '.cfi_signal_frame'");
3243
3244 getStreamer().EmitCFISignalFrame();
3245 return false;
3246}
3247
Jim Grosbach4b905842013-09-20 23:08:21 +00003248/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003249/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003250bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003251 int64_t Register = 0;
3252
Jim Grosbach4b905842013-09-20 23:08:21 +00003253 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003254 return true;
3255
3256 getStreamer().EmitCFIUndefined(Register);
3257 return false;
3258}
3259
Jim Grosbach4b905842013-09-20 23:08:21 +00003260/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003261/// ::= .macros_on
3262/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003263bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003264 if (getLexer().isNot(AsmToken::EndOfStatement))
3265 return Error(getLexer().getLoc(),
3266 "unexpected token in '" + Directive + "' directive");
3267
Jim Grosbach4b905842013-09-20 23:08:21 +00003268 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003269 return false;
3270}
3271
Jim Grosbach4b905842013-09-20 23:08:21 +00003272/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003273/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003274bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003275 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003276 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003277 return TokError("expected identifier in '.macro' directive");
3278
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003279 if (getLexer().is(AsmToken::Comma))
3280 Lex();
3281
Eli Bendersky17233942013-01-15 22:59:42 +00003282 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003283 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003284
3285 if (Parameters.size() && Parameters.back().Vararg)
3286 return Error(Lexer.getLoc(),
3287 "Vararg parameter '" + Parameters.back().Name +
3288 "' should be last one in the list of parameters.");
3289
David Majnemer91fc4c22014-01-29 18:57:46 +00003290 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003291 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003292 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003293
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003294 if (Lexer.is(AsmToken::Colon)) {
3295 Lex(); // consume ':'
3296
3297 SMLoc QualLoc;
3298 StringRef Qualifier;
3299
3300 QualLoc = Lexer.getLoc();
3301 if (parseIdentifier(Qualifier))
3302 return Error(QualLoc, "missing parameter qualifier for "
3303 "'" + Parameter.Name + "' in macro '" + Name + "'");
3304
3305 if (Qualifier == "req")
3306 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003307 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003308 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003309 else
3310 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3311 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3312 }
3313
David Majnemer91fc4c22014-01-29 18:57:46 +00003314 if (getLexer().is(AsmToken::Equal)) {
3315 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003316
3317 SMLoc ParamLoc;
3318
3319 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003320 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003321 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003322
3323 if (Parameter.Required)
3324 Warning(ParamLoc, "pointless default value for required parameter "
3325 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003326 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003327
3328 Parameters.push_back(Parameter);
3329
3330 if (getLexer().is(AsmToken::Comma))
3331 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003332 }
3333
3334 // Eat the end of statement.
3335 Lex();
3336
3337 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003338 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003339
3340 // Lex the macro definition.
3341 for (;;) {
3342 // Check whether we have reached the end of the file.
3343 if (getLexer().is(AsmToken::Eof))
3344 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3345
3346 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003347 if (getLexer().is(AsmToken::Identifier)) {
3348 if (getTok().getIdentifier() == ".endm" ||
3349 getTok().getIdentifier() == ".endmacro") {
3350 if (MacroDepth == 0) { // Outermost macro.
3351 EndToken = getTok();
3352 Lex();
3353 if (getLexer().isNot(AsmToken::EndOfStatement))
3354 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3355 "' directive");
3356 break;
3357 } else {
3358 // Otherwise we just found the end of an inner macro.
3359 --MacroDepth;
3360 }
3361 } else if (getTok().getIdentifier() == ".macro") {
3362 // We allow nested macros. Those aren't instantiated until the outermost
3363 // macro is expanded so just ignore them for now.
3364 ++MacroDepth;
3365 }
Eli Bendersky17233942013-01-15 22:59:42 +00003366 }
3367
3368 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003369 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003370 }
3371
Jim Grosbach4b905842013-09-20 23:08:21 +00003372 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003373 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3374 }
3375
3376 const char *BodyStart = StartToken.getLoc().getPointer();
3377 const char *BodyEnd = EndToken.getLoc().getPointer();
3378 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003379 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3380 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003381 return false;
3382}
3383
Jim Grosbach4b905842013-09-20 23:08:21 +00003384/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003385///
3386/// With the support added for named parameters there may be code out there that
3387/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003388/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003390/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003391/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3392/// warning that the positional parameter found in body which have no effect.
3393/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003394/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003395/// intended or change the macro to use the named parameters. It is possible
3396/// this warning will trigger when the none of the named parameters are used
3397/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003398void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003399 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003400 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003401 // If this macro is not defined with named parameters the warning we are
3402 // checking for here doesn't apply.
3403 unsigned NParameters = Parameters.size();
3404 if (NParameters == 0)
3405 return;
3406
3407 bool NamedParametersFound = false;
3408 bool PositionalParametersFound = false;
3409
3410 // Look at the body of the macro for use of both the named parameters and what
3411 // are likely to be positional parameters. This is what expandMacro() is
3412 // doing when it finds the parameters in the body.
3413 while (!Body.empty()) {
3414 // Scan for the next possible parameter.
3415 std::size_t End = Body.size(), Pos = 0;
3416 for (; Pos != End; ++Pos) {
3417 // Check for a substitution or escape.
3418 // This macro is defined with parameters, look for \foo, \bar, etc.
3419 if (Body[Pos] == '\\' && Pos + 1 != End)
3420 break;
3421
3422 // This macro should have parameters, but look for $0, $1, ..., $n too.
3423 if (Body[Pos] != '$' || Pos + 1 == End)
3424 continue;
3425 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003426 if (Next == '$' || Next == 'n' ||
3427 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003428 break;
3429 }
3430
3431 // Check if we reached the end.
3432 if (Pos == End)
3433 break;
3434
3435 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003436 switch (Body[Pos + 1]) {
3437 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 case '$':
3439 break;
3440
Jim Grosbach4b905842013-09-20 23:08:21 +00003441 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003442 case 'n':
3443 PositionalParametersFound = true;
3444 break;
3445
Jim Grosbach4b905842013-09-20 23:08:21 +00003446 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003447 default: {
3448 PositionalParametersFound = true;
3449 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003450 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003451 }
3452 Pos += 2;
3453 } else {
3454 unsigned I = Pos + 1;
3455 while (isIdentifierChar(Body[I]) && I + 1 != End)
3456 ++I;
3457
Jim Grosbach4b905842013-09-20 23:08:21 +00003458 const char *Begin = Body.data() + Pos + 1;
3459 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003460 unsigned Index = 0;
3461 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003462 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003463 break;
3464
3465 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003466 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3467 Pos += 3;
3468 else {
3469 Pos = I;
3470 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003471 } else {
3472 NamedParametersFound = true;
3473 Pos += 1 + Argument.size();
3474 }
3475 }
3476 // Update the scan point.
3477 Body = Body.substr(Pos);
3478 }
3479
3480 if (!NamedParametersFound && PositionalParametersFound)
3481 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3482 "used in macro body, possible positional parameter "
3483 "found in body which will have no effect");
3484}
3485
Nico Weber155dccd12014-07-24 17:08:39 +00003486/// parseDirectiveExitMacro
3487/// ::= .exitm
3488bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3489 if (getLexer().isNot(AsmToken::EndOfStatement))
3490 return TokError("unexpected token in '" + Directive + "' directive");
3491
3492 if (!isInsideMacroInstantiation())
3493 return TokError("unexpected '" + Directive + "' in file, "
3494 "no current macro definition");
3495
3496 // Exit all conditionals that are active in the current macro.
3497 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3498 TheCondState = TheCondStack.back();
3499 TheCondStack.pop_back();
3500 }
3501
3502 handleMacroExit();
3503 return false;
3504}
3505
Jim Grosbach4b905842013-09-20 23:08:21 +00003506/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003507/// ::= .endm
3508/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003509bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003510 if (getLexer().isNot(AsmToken::EndOfStatement))
3511 return TokError("unexpected token in '" + Directive + "' directive");
3512
3513 // If we are inside a macro instantiation, terminate the current
3514 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003515 if (isInsideMacroInstantiation()) {
3516 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003517 return false;
3518 }
3519
3520 // Otherwise, this .endmacro is a stray entry in the file; well formed
3521 // .endmacro directives are handled during the macro definition parsing.
3522 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003523 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003524}
3525
Jim Grosbach4b905842013-09-20 23:08:21 +00003526/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003527/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003528bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003529 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003530 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003531 return TokError("expected identifier in '.purgem' directive");
3532
3533 if (getLexer().isNot(AsmToken::EndOfStatement))
3534 return TokError("unexpected token in '.purgem' directive");
3535
Jim Grosbach4b905842013-09-20 23:08:21 +00003536 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003537 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3538
Jim Grosbach4b905842013-09-20 23:08:21 +00003539 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003540 return false;
3541}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003542
Jim Grosbach4b905842013-09-20 23:08:21 +00003543/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003544/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003545bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003546 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003547
3548 // Expect a single argument: an expression that evaluates to a constant
3549 // in the inclusive range 0-30.
3550 SMLoc ExprLoc = getLexer().getLoc();
3551 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003552 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003553 return true;
3554 else if (getLexer().isNot(AsmToken::EndOfStatement))
3555 return TokError("unexpected token after expression in"
3556 " '.bundle_align_mode' directive");
3557 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3558 return Error(ExprLoc,
3559 "invalid bundle alignment size (expected between 0 and 30)");
3560
3561 Lex();
3562
3563 // Because of AlignSizePow2's verified range we can safely truncate it to
3564 // unsigned.
3565 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3566 return false;
3567}
3568
Jim Grosbach4b905842013-09-20 23:08:21 +00003569/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003570/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003571bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003572 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003573 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003574
Eli Bendersky802b6282013-01-07 21:51:08 +00003575 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3576 StringRef Option;
3577 SMLoc Loc = getTok().getLoc();
3578 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003579 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003580
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003581 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003582 return Error(Loc, kInvalidOptionError);
3583
3584 if (Option != "align_to_end")
3585 return Error(Loc, kInvalidOptionError);
3586 else if (getLexer().isNot(AsmToken::EndOfStatement))
3587 return Error(Loc,
3588 "unexpected token after '.bundle_lock' directive option");
3589 AlignToEnd = true;
3590 }
3591
Eli Benderskyf483ff92012-12-20 19:05:53 +00003592 Lex();
3593
Eli Bendersky802b6282013-01-07 21:51:08 +00003594 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003595 return false;
3596}
3597
Jim Grosbach4b905842013-09-20 23:08:21 +00003598/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003599/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003600bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003601 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003602
3603 if (getLexer().isNot(AsmToken::EndOfStatement))
3604 return TokError("unexpected token in '.bundle_unlock' directive");
3605 Lex();
3606
3607 getStreamer().EmitBundleUnlock();
3608 return false;
3609}
3610
Jim Grosbach4b905842013-09-20 23:08:21 +00003611/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003612/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003613bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003614 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003615
3616 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003617 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003618 return true;
3619
3620 int64_t FillExpr = 0;
3621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3622 if (getLexer().isNot(AsmToken::Comma))
3623 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3624 Lex();
3625
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003626 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003627 return true;
3628
3629 if (getLexer().isNot(AsmToken::EndOfStatement))
3630 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3631 }
3632
3633 Lex();
3634
3635 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003636 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3637 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003638
3639 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003640 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003641
3642 return false;
3643}
3644
Jim Grosbach4b905842013-09-20 23:08:21 +00003645/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003646/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003647bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003648 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003649 const MCExpr *Value;
3650
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003651 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003652 return true;
3653
3654 if (getLexer().isNot(AsmToken::EndOfStatement))
3655 return TokError("unexpected token in directive");
3656
3657 if (Signed)
3658 getStreamer().EmitSLEB128Value(Value);
3659 else
3660 getStreamer().EmitULEB128Value(Value);
3661
3662 return false;
3663}
3664
Jim Grosbach4b905842013-09-20 23:08:21 +00003665/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003666/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003667bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003668 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003669 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003670 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003671 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003672
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003674 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003675
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003676 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003677
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003678 // Assembler local symbols don't make any sense here. Complain loudly.
3679 if (Sym->isTemporary())
3680 return Error(Loc, "non-local symbol required in directive");
3681
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003682 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3683 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003685 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003686 break;
3687
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003688 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003689 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003690 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003691 }
3692 }
3693
Sean Callanan686ed8d2010-01-19 20:22:31 +00003694 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003695 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003696}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003697
Jim Grosbach4b905842013-09-20 23:08:21 +00003698/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003699/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003700bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003701 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003702
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003703 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003704 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003705 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003706 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003707
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003708 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003709 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003710
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003711 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003712 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003713 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003714
3715 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003716 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003717 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003718 return true;
3719
3720 int64_t Pow2Alignment = 0;
3721 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003722 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003723 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003724 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003725 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003726 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003727
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003728 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3729 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003730 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3731
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003732 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003733 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3734 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003735 if (!isPowerOf2_64(Pow2Alignment))
3736 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3737 Pow2Alignment = Log2_64(Pow2Alignment);
3738 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003739 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003740
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003741 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003742 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003743
Sean Callanan686ed8d2010-01-19 20:22:31 +00003744 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003745
Chris Lattner28ad7542009-07-09 17:25:12 +00003746 // NOTE: a size of zero for a .comm should create a undefined symbol
3747 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003748 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003749 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003750 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003751
Eric Christopherbc818852010-05-14 01:38:54 +00003752 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003753 // may internally end up wanting an alignment in bytes.
3754 // FIXME: Diagnose overflow.
3755 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003756 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003757 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003758
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003759 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003760 return Error(IDLoc, "invalid symbol redefinition");
3761
Chris Lattner28ad7542009-07-09 17:25:12 +00003762 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003763 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003764 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003765 return false;
3766 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003767
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003768 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003769 return false;
3770}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003771
Jim Grosbach4b905842013-09-20 23:08:21 +00003772/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003773/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003774bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003775 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003776 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003777
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003778 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003779 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003780 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003781
Sean Callanan686ed8d2010-01-19 20:22:31 +00003782 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003783
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003784 if (Str.empty())
3785 Error(Loc, ".abort detected. Assembly stopping.");
3786 else
3787 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003788 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003789
3790 return false;
3791}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003792
Jim Grosbach4b905842013-09-20 23:08:21 +00003793/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003794/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003795bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003797 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003798
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003799 // Allow the strings to have escaped octal character sequence.
3800 std::string Filename;
3801 if (parseEscapedString(Filename))
3802 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003803 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003804 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003805
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003806 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003807 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003808
Chris Lattner693fbb82009-07-16 06:14:39 +00003809 // Attempt to switch the lexer to the included file before consuming the end
3810 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003811 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003812 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003813 return true;
3814 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003815
3816 return false;
3817}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003818
Jim Grosbach4b905842013-09-20 23:08:21 +00003819/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003820/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003821bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003822 if (getLexer().isNot(AsmToken::String))
3823 return TokError("expected string in '.incbin' directive");
3824
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003825 // Allow the strings to have escaped octal character sequence.
3826 std::string Filename;
3827 if (parseEscapedString(Filename))
3828 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003829 SMLoc IncbinLoc = getLexer().getLoc();
3830 Lex();
3831
3832 if (getLexer().isNot(AsmToken::EndOfStatement))
3833 return TokError("unexpected token in '.incbin' directive");
3834
Kevin Enderby109f25c2011-12-14 21:47:48 +00003835 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003836 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003837 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3838 return true;
3839 }
3840
3841 return false;
3842}
3843
Jim Grosbach4b905842013-09-20 23:08:21 +00003844/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003845/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3846bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003847 TheCondStack.push_back(TheCondState);
3848 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003849 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003850 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003851 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003853 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003854 return true;
3855
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003856 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003857 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003858
Sean Callanan686ed8d2010-01-19 20:22:31 +00003859 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003860
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003861 switch (DirKind) {
3862 default:
3863 llvm_unreachable("unsupported directive");
3864 case DK_IF:
3865 case DK_IFNE:
3866 break;
3867 case DK_IFEQ:
3868 ExprValue = ExprValue == 0;
3869 break;
3870 case DK_IFGE:
3871 ExprValue = ExprValue >= 0;
3872 break;
3873 case DK_IFGT:
3874 ExprValue = ExprValue > 0;
3875 break;
3876 case DK_IFLE:
3877 ExprValue = ExprValue <= 0;
3878 break;
3879 case DK_IFLT:
3880 ExprValue = ExprValue < 0;
3881 break;
3882 }
3883
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003884 TheCondState.CondMet = ExprValue;
3885 TheCondState.Ignore = !TheCondState.CondMet;
3886 }
3887
3888 return false;
3889}
3890
Jim Grosbach4b905842013-09-20 23:08:21 +00003891/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003892/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003893bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003894 TheCondStack.push_back(TheCondState);
3895 TheCondState.TheCond = AsmCond::IfCond;
3896
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003897 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003898 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003899 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003900 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003901
3902 if (getLexer().isNot(AsmToken::EndOfStatement))
3903 return TokError("unexpected token in '.ifb' directive");
3904
3905 Lex();
3906
3907 TheCondState.CondMet = ExpectBlank == Str.empty();
3908 TheCondState.Ignore = !TheCondState.CondMet;
3909 }
3910
3911 return false;
3912}
3913
Jim Grosbach4b905842013-09-20 23:08:21 +00003914/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003915/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003916/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003917bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003918 TheCondStack.push_back(TheCondState);
3919 TheCondState.TheCond = AsmCond::IfCond;
3920
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003921 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003922 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003923 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003924 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003925
3926 if (getLexer().isNot(AsmToken::Comma))
3927 return TokError("unexpected token in '.ifc' directive");
3928
3929 Lex();
3930
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003931 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003932
3933 if (getLexer().isNot(AsmToken::EndOfStatement))
3934 return TokError("unexpected token in '.ifc' directive");
3935
3936 Lex();
3937
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003938 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003939 TheCondState.Ignore = !TheCondState.CondMet;
3940 }
3941
3942 return false;
3943}
3944
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003945/// parseDirectiveIfeqs
3946/// ::= .ifeqs string1, string2
3947bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3948 if (Lexer.isNot(AsmToken::String)) {
3949 TokError("expected string parameter for '.ifeqs' directive");
3950 eatToEndOfStatement();
3951 return true;
3952 }
3953
3954 StringRef String1 = getTok().getStringContents();
3955 Lex();
3956
3957 if (Lexer.isNot(AsmToken::Comma)) {
3958 TokError("expected comma after first string for '.ifeqs' directive");
3959 eatToEndOfStatement();
3960 return true;
3961 }
3962
3963 Lex();
3964
3965 if (Lexer.isNot(AsmToken::String)) {
3966 TokError("expected string parameter for '.ifeqs' directive");
3967 eatToEndOfStatement();
3968 return true;
3969 }
3970
3971 StringRef String2 = getTok().getStringContents();
3972 Lex();
3973
3974 TheCondStack.push_back(TheCondState);
3975 TheCondState.TheCond = AsmCond::IfCond;
3976 TheCondState.CondMet = String1 == String2;
3977 TheCondState.Ignore = !TheCondState.CondMet;
3978
3979 return false;
3980}
3981
Jim Grosbach4b905842013-09-20 23:08:21 +00003982/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003983/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003984bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003985 StringRef Name;
3986 TheCondStack.push_back(TheCondState);
3987 TheCondState.TheCond = AsmCond::IfCond;
3988
3989 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003990 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003991 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003992 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003993 return TokError("expected identifier after '.ifdef'");
3994
3995 Lex();
3996
3997 MCSymbol *Sym = getContext().LookupSymbol(Name);
3998
3999 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004000 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004001 else
Craig Topper353eda42014-04-24 06:44:33 +00004002 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004003 TheCondState.Ignore = !TheCondState.CondMet;
4004 }
4005
4006 return false;
4007}
4008
Jim Grosbach4b905842013-09-20 23:08:21 +00004009/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004010/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004011bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004012 if (TheCondState.TheCond != AsmCond::IfCond &&
4013 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004014 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4015 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016 TheCondState.TheCond = AsmCond::ElseIfCond;
4017
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004018 bool LastIgnoreState = false;
4019 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004020 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004021 if (LastIgnoreState || TheCondState.CondMet) {
4022 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004023 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004024 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004025 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004026 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004027 return true;
4028
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004029 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004030 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004031
Sean Callanan686ed8d2010-01-19 20:22:31 +00004032 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004033 TheCondState.CondMet = ExprValue;
4034 TheCondState.Ignore = !TheCondState.CondMet;
4035 }
4036
4037 return false;
4038}
4039
Jim Grosbach4b905842013-09-20 23:08:21 +00004040/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004041/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004042bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004043 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004044 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004045
Sean Callanan686ed8d2010-01-19 20:22:31 +00004046 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004047
4048 if (TheCondState.TheCond != AsmCond::IfCond &&
4049 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004050 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4051 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004052 TheCondState.TheCond = AsmCond::ElseCond;
4053 bool LastIgnoreState = false;
4054 if (!TheCondStack.empty())
4055 LastIgnoreState = TheCondStack.back().Ignore;
4056 if (LastIgnoreState || TheCondState.CondMet)
4057 TheCondState.Ignore = true;
4058 else
4059 TheCondState.Ignore = false;
4060
4061 return false;
4062}
4063
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004064/// parseDirectiveEnd
4065/// ::= .end
4066bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4067 if (getLexer().isNot(AsmToken::EndOfStatement))
4068 return TokError("unexpected token in '.end' directive");
4069
4070 Lex();
4071
4072 while (Lexer.isNot(AsmToken::Eof))
4073 Lex();
4074
4075 return false;
4076}
4077
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004078/// parseDirectiveError
4079/// ::= .err
4080/// ::= .error [string]
4081bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4082 if (!TheCondStack.empty()) {
4083 if (TheCondStack.back().Ignore) {
4084 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004085 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004086 }
4087 }
4088
4089 if (!WithMessage)
4090 return Error(L, ".err encountered");
4091
4092 StringRef Message = ".error directive invoked in source file";
4093 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4094 if (Lexer.isNot(AsmToken::String)) {
4095 TokError(".error argument must be a string");
4096 eatToEndOfStatement();
4097 return true;
4098 }
4099
4100 Message = getTok().getStringContents();
4101 Lex();
4102 }
4103
4104 Error(L, Message);
4105 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004106}
4107
Nico Weber404012b2014-07-24 16:26:06 +00004108/// parseDirectiveWarning
4109/// ::= .warning [string]
4110bool AsmParser::parseDirectiveWarning(SMLoc L) {
4111 if (!TheCondStack.empty()) {
4112 if (TheCondStack.back().Ignore) {
4113 eatToEndOfStatement();
4114 return false;
4115 }
4116 }
4117
4118 StringRef Message = ".warning directive invoked in source file";
4119 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4120 if (Lexer.isNot(AsmToken::String)) {
4121 TokError(".warning argument must be a string");
4122 eatToEndOfStatement();
4123 return true;
4124 }
4125
4126 Message = getTok().getStringContents();
4127 Lex();
4128 }
4129
4130 Warning(L, Message);
4131 return false;
4132}
4133
Jim Grosbach4b905842013-09-20 23:08:21 +00004134/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004135/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004136bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004137 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004138 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004139
Sean Callanan686ed8d2010-01-19 20:22:31 +00004140 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004141
Jim Grosbach4b905842013-09-20 23:08:21 +00004142 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004143 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4144 ".else");
4145 if (!TheCondStack.empty()) {
4146 TheCondState = TheCondStack.back();
4147 TheCondStack.pop_back();
4148 }
4149
4150 return false;
4151}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004152
Eli Bendersky17233942013-01-15 22:59:42 +00004153void AsmParser::initializeDirectiveKindMap() {
4154 DirectiveKindMap[".set"] = DK_SET;
4155 DirectiveKindMap[".equ"] = DK_EQU;
4156 DirectiveKindMap[".equiv"] = DK_EQUIV;
4157 DirectiveKindMap[".ascii"] = DK_ASCII;
4158 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4159 DirectiveKindMap[".string"] = DK_STRING;
4160 DirectiveKindMap[".byte"] = DK_BYTE;
4161 DirectiveKindMap[".short"] = DK_SHORT;
4162 DirectiveKindMap[".value"] = DK_VALUE;
4163 DirectiveKindMap[".2byte"] = DK_2BYTE;
4164 DirectiveKindMap[".long"] = DK_LONG;
4165 DirectiveKindMap[".int"] = DK_INT;
4166 DirectiveKindMap[".4byte"] = DK_4BYTE;
4167 DirectiveKindMap[".quad"] = DK_QUAD;
4168 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004169 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004170 DirectiveKindMap[".single"] = DK_SINGLE;
4171 DirectiveKindMap[".float"] = DK_FLOAT;
4172 DirectiveKindMap[".double"] = DK_DOUBLE;
4173 DirectiveKindMap[".align"] = DK_ALIGN;
4174 DirectiveKindMap[".align32"] = DK_ALIGN32;
4175 DirectiveKindMap[".balign"] = DK_BALIGN;
4176 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4177 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4178 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4179 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4180 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4181 DirectiveKindMap[".org"] = DK_ORG;
4182 DirectiveKindMap[".fill"] = DK_FILL;
4183 DirectiveKindMap[".zero"] = DK_ZERO;
4184 DirectiveKindMap[".extern"] = DK_EXTERN;
4185 DirectiveKindMap[".globl"] = DK_GLOBL;
4186 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004187 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4188 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4189 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4190 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4191 DirectiveKindMap[".reference"] = DK_REFERENCE;
4192 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4193 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4194 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4195 DirectiveKindMap[".comm"] = DK_COMM;
4196 DirectiveKindMap[".common"] = DK_COMMON;
4197 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4198 DirectiveKindMap[".abort"] = DK_ABORT;
4199 DirectiveKindMap[".include"] = DK_INCLUDE;
4200 DirectiveKindMap[".incbin"] = DK_INCBIN;
4201 DirectiveKindMap[".code16"] = DK_CODE16;
4202 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4203 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004204 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004205 DirectiveKindMap[".irp"] = DK_IRP;
4206 DirectiveKindMap[".irpc"] = DK_IRPC;
4207 DirectiveKindMap[".endr"] = DK_ENDR;
4208 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4209 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4210 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4211 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004212 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4213 DirectiveKindMap[".ifge"] = DK_IFGE;
4214 DirectiveKindMap[".ifgt"] = DK_IFGT;
4215 DirectiveKindMap[".ifle"] = DK_IFLE;
4216 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004217 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004218 DirectiveKindMap[".ifb"] = DK_IFB;
4219 DirectiveKindMap[".ifnb"] = DK_IFNB;
4220 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004221 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004222 DirectiveKindMap[".ifnc"] = DK_IFNC;
4223 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4224 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4225 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4226 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4227 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004228 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004229 DirectiveKindMap[".endif"] = DK_ENDIF;
4230 DirectiveKindMap[".skip"] = DK_SKIP;
4231 DirectiveKindMap[".space"] = DK_SPACE;
4232 DirectiveKindMap[".file"] = DK_FILE;
4233 DirectiveKindMap[".line"] = DK_LINE;
4234 DirectiveKindMap[".loc"] = DK_LOC;
4235 DirectiveKindMap[".stabs"] = DK_STABS;
4236 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4237 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4238 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4239 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4240 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4241 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4242 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4243 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4244 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4245 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4246 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4247 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4248 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4249 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4250 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4251 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4252 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4253 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4254 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4255 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4256 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004257 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004258 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4259 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4260 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004261 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004262 DirectiveKindMap[".endm"] = DK_ENDM;
4263 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4264 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004265 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004266 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004267 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004268}
4269
Jim Grosbach4b905842013-09-20 23:08:21 +00004270MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004271 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004272
Rafael Espindola34b9c512012-06-03 23:57:14 +00004273 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004274 for (;;) {
4275 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004276 if (getLexer().is(AsmToken::Eof)) {
4277 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004278 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004279 }
4280
Rafael Espindola34b9c512012-06-03 23:57:14 +00004281 if (Lexer.is(AsmToken::Identifier) &&
4282 (getTok().getIdentifier() == ".rept")) {
4283 ++NestLevel;
4284 }
4285
4286 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004287 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004288 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004289 EndToken = getTok();
4290 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004291 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4292 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004293 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004294 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004295 break;
4296 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004297 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004298 }
4299
Rafael Espindola34b9c512012-06-03 23:57:14 +00004300 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004301 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004302 }
4303
4304 const char *BodyStart = StartToken.getLoc().getPointer();
4305 const char *BodyEnd = EndToken.getLoc().getPointer();
4306 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4307
Rafael Espindola34b9c512012-06-03 23:57:14 +00004308 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004309 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004310 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004311}
4312
Jim Grosbach4b905842013-09-20 23:08:21 +00004313void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004314 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004315 OS << ".endr\n";
4316
Rafael Espindola3560ff22014-08-27 20:03:13 +00004317 std::unique_ptr<MemoryBuffer> Instantiation =
4318 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004319
Rafael Espindola34b9c512012-06-03 23:57:14 +00004320 // Create the macro instantiation object and add to the current macro
4321 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004322 MacroInstantiation *MI = new MacroInstantiation(
4323 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004324 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004325
Rafael Espindola34b9c512012-06-03 23:57:14 +00004326 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004327 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004328 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004329 Lex();
4330}
4331
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004332/// parseDirectiveRept
4333/// ::= .rep | .rept count
4334bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004335 const MCExpr *CountExpr;
4336 SMLoc CountLoc = getTok().getLoc();
4337 if (parseExpression(CountExpr))
4338 return true;
4339
Rafael Espindola34b9c512012-06-03 23:57:14 +00004340 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004341 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4342 eatToEndOfStatement();
4343 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4344 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004345
4346 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004347 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004348
4349 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004350 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004351
4352 // Eat the end of statement.
4353 Lex();
4354
4355 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004356 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004357 if (!M)
4358 return true;
4359
4360 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4361 // to hold the macro body with substitutions.
4362 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004363 raw_svector_ostream OS(Buf);
4364 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004365 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004366 return true;
4367 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004368 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004369
4370 return false;
4371}
4372
Jim Grosbach4b905842013-09-20 23:08:21 +00004373/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004374/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004375bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004376 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004377
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004378 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004379 return TokError("expected identifier in '.irp' directive");
4380
Rafael Espindola768b41c2012-06-15 14:02:34 +00004381 if (Lexer.isNot(AsmToken::Comma))
4382 return TokError("expected comma in '.irp' directive");
4383
4384 Lex();
4385
Eli Bendersky38274122013-01-14 23:22:36 +00004386 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004387 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004388 return true;
4389
4390 // Eat the end of statement.
4391 Lex();
4392
4393 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004394 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004395 if (!M)
4396 return true;
4397
4398 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4399 // to hold the macro body with substitutions.
4400 SmallString<256> Buf;
4401 raw_svector_ostream OS(Buf);
4402
Eli Bendersky38274122013-01-14 23:22:36 +00004403 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004404 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004405 return true;
4406 }
4407
Jim Grosbach4b905842013-09-20 23:08:21 +00004408 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004409
4410 return false;
4411}
4412
Jim Grosbach4b905842013-09-20 23:08:21 +00004413/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004414/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004415bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004416 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004417
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004418 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004419 return TokError("expected identifier in '.irpc' directive");
4420
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004421 if (Lexer.isNot(AsmToken::Comma))
4422 return TokError("expected comma in '.irpc' directive");
4423
4424 Lex();
4425
Eli Bendersky38274122013-01-14 23:22:36 +00004426 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004427 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004428 return true;
4429
4430 if (A.size() != 1 || A.front().size() != 1)
4431 return TokError("unexpected token in '.irpc' directive");
4432
4433 // Eat the end of statement.
4434 Lex();
4435
4436 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004437 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004438 if (!M)
4439 return true;
4440
4441 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4442 // to hold the macro body with substitutions.
4443 SmallString<256> Buf;
4444 raw_svector_ostream OS(Buf);
4445
4446 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004447 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004448 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004449 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004450
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004451 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004452 return true;
4453 }
4454
Jim Grosbach4b905842013-09-20 23:08:21 +00004455 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004456
4457 return false;
4458}
4459
Jim Grosbach4b905842013-09-20 23:08:21 +00004460bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004461 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004462 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004463
4464 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004465 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004466 assert(getLexer().is(AsmToken::EndOfStatement));
4467
Jim Grosbach4b905842013-09-20 23:08:21 +00004468 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004469 return false;
4470}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004471
Jim Grosbach4b905842013-09-20 23:08:21 +00004472bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004473 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004474 const MCExpr *Value;
4475 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004476 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004477 return true;
4478 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4479 if (!MCE)
4480 return Error(ExprLoc, "unexpected expression in _emit");
4481 uint64_t IntValue = MCE->getValue();
4482 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4483 return Error(ExprLoc, "literal value out of range for directive");
4484
Chad Rosierc7f552c2013-02-12 21:33:51 +00004485 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4486 return false;
4487}
4488
Jim Grosbach4b905842013-09-20 23:08:21 +00004489bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004490 const MCExpr *Value;
4491 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004492 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004493 return true;
4494 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4495 if (!MCE)
4496 return Error(ExprLoc, "unexpected expression in align");
4497 uint64_t IntValue = MCE->getValue();
4498 if (!isPowerOf2_64(IntValue))
4499 return Error(ExprLoc, "literal value not a power of two greater then zero");
4500
Jim Grosbach4b905842013-09-20 23:08:21 +00004501 Info.AsmRewrites->push_back(
4502 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004503 return false;
4504}
4505
Chad Rosierf43fcf52013-02-13 21:27:17 +00004506// We are comparing pointers, but the pointers are relative to a single string.
4507// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004508static int rewritesSort(const AsmRewrite *AsmRewriteA,
4509 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004510 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4511 return -1;
4512 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4513 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004514
Chad Rosierfce4fab2013-04-08 17:43:47 +00004515 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4516 // rewrite to the same location. Make sure the SizeDirective rewrite is
4517 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4518 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004519 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4520 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004521 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004522
Jim Grosbach4b905842013-09-20 23:08:21 +00004523 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4524 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004525 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004526 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004527}
4528
Jim Grosbach4b905842013-09-20 23:08:21 +00004529bool AsmParser::parseMSInlineAsm(
4530 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4531 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4532 SmallVectorImpl<std::string> &Constraints,
4533 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4534 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004535 SmallVector<void *, 4> InputDecls;
4536 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004537 SmallVector<bool, 4> InputDeclsAddressOf;
4538 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004539 SmallVector<std::string, 4> InputConstraints;
4540 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004541 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004542
Benjamin Kramer1a136112013-02-15 20:37:21 +00004543 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004544
4545 // Prime the lexer.
4546 Lex();
4547
4548 // While we have input, parse each statement.
4549 unsigned InputIdx = 0;
4550 unsigned OutputIdx = 0;
4551 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004552 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004553 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004554 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004555
Chad Rosier149e8e02012-12-12 22:45:52 +00004556 if (Info.ParseError)
4557 return true;
4558
Benjamin Kramer1a136112013-02-15 20:37:21 +00004559 if (Info.Opcode == ~0U)
4560 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004561
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004563
Benjamin Kramer1a136112013-02-15 20:37:21 +00004564 // Build the list of clobbers, outputs and inputs.
4565 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004566 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004567
Benjamin Kramer1a136112013-02-15 20:37:21 +00004568 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004569 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004570 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004571
Benjamin Kramer1a136112013-02-15 20:37:21 +00004572 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004573 if (Operand.isReg() && !Operand.needAddressOf() &&
4574 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004575 unsigned NumDefs = Desc.getNumDefs();
4576 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004577 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4578 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004579 continue;
4580 }
4581
4582 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004583 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004584 if (SymName.empty())
4585 continue;
4586
David Blaikie960ea3f2014-06-08 16:18:35 +00004587 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004588 if (!OpDecl)
4589 continue;
4590
4591 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004592 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004593 if (isOutput) {
4594 ++InputIdx;
4595 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004596 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4597 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004598 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004599 } else {
4600 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004601 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4602 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004603 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004604 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004605 }
Reid Kleckneree088972013-12-10 18:27:32 +00004606
4607 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004608 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4609 Desc.getNumImplicitDefs());
4610 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004611 }
4612
4613 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004614 NumOutputs = OutputDecls.size();
4615 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004616
4617 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004618 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4619 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4620 ClobberRegs.end());
4621 Clobbers.assign(ClobberRegs.size(), std::string());
4622 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4623 raw_string_ostream OS(Clobbers[I]);
4624 IP->printRegName(OS, ClobberRegs[I]);
4625 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004626
4627 // Merge the various outputs and inputs. Output are expected first.
4628 if (NumOutputs || NumInputs) {
4629 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004630 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004631 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004632 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004633 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004634 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004635 }
4636 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004637 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004638 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004639 }
4640 }
4641
4642 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004643 std::string AsmStringIR;
4644 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004645 StringRef ASMString =
4646 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4647 const char *AsmStart = ASMString.begin();
4648 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004649 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004650 for (const AsmRewrite &AR : AsmStrRewrites) {
4651 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004652 if (Kind == AOK_Delete)
4653 continue;
4654
David Majnemer8114c1a2014-06-23 02:17:16 +00004655 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004656 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004657
Chad Rosier120eefd2013-03-19 17:32:17 +00004658 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004659 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004660 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004661
Chad Rosier37e755c2012-10-23 17:43:43 +00004662 // Skip the original expression.
4663 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004664 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004665 continue;
4666 }
4667
Chad Rosierff10ed12013-04-12 16:26:42 +00004668 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004669 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004670 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004671 default:
4672 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004673 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004674 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004675 break;
4676 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004677 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004678 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004679 case AOK_Label:
4680 OS << Ctx.getAsmInfo()->getPrivateGlobalPrefix() << AR.Label;
4681 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004682 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004683 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004684 break;
4685 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004686 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004687 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004688 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004689 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004690 default: break;
4691 case 8: OS << "byte ptr "; break;
4692 case 16: OS << "word ptr "; break;
4693 case 32: OS << "dword ptr "; break;
4694 case 64: OS << "qword ptr "; break;
4695 case 80: OS << "xword ptr "; break;
4696 case 128: OS << "xmmword ptr "; break;
4697 case 256: OS << "ymmword ptr "; break;
4698 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004699 break;
4700 case AOK_Emit:
4701 OS << ".byte";
4702 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004703 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004704 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004705 OS << ".align " << Val;
4706
4707 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004708 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004709 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4710 break;
4711 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004712 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004713 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004714 OS.flush();
4715 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004716 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004717 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004718 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004719 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004720
Chad Rosier8bce6642012-10-18 15:49:34 +00004721 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004722 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004723 }
4724
4725 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004726 if (AsmStart != AsmEnd)
4727 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004728
4729 AsmString = OS.str();
4730 return false;
4731}
4732
Daniel Dunbar01e36072010-07-17 02:26:10 +00004733/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004734MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4735 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004736 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004737}