blob: 5a56094a3c75a2d5b6cc0133df784c9c4de47ba8 [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 Kramercb3e06b2014-10-03 18:32:55 +000072 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
73 : Name(N), Body(B), Parameters(std::move(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;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000124 std::unique_ptr<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.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +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
Oliver Stannard652ec6e2014-11-03 12:02:51 +0000175 /// \brief The last symbol we emitted, used for call frame information.
176 MCSymbol *LastFuncSymbol;
177
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000178public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000179 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000181 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182
Craig Topper59be68f2014-03-08 07:14:16 +0000183 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 void addDirectiveHandler(StringRef Directive,
186 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000187 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188 }
189
190public:
191 /// @name MCAsmParser Interface
192 /// {
193
Craig Topper59be68f2014-03-08 07:14:16 +0000194 SourceMgr &getSourceManager() override { return SrcMgr; }
195 MCAsmLexer &getLexer() override { return Lexer; }
196 MCContext &getContext() override { return Ctx; }
197 MCStreamer &getStreamer() override { return Out; }
198 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000199 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000200 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000201 else
202 return AssemblerDialect;
203 }
Craig Topper59be68f2014-03-08 07:14:16 +0000204 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000205 AssemblerDialect = i;
206 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000207
Craig Topper59be68f2014-03-08 07:14:16 +0000208 void Note(SMLoc L, const Twine &Msg,
209 ArrayRef<SMRange> Ranges = None) override;
210 bool Warning(SMLoc L, const Twine &Msg,
211 ArrayRef<SMRange> Ranges = None) override;
212 bool Error(SMLoc L, const Twine &Msg,
213 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000214
Craig Topper59be68f2014-03-08 07:14:16 +0000215 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000216
Craig Topper59be68f2014-03-08 07:14:16 +0000217 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
218 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000219
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000220 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000222 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000223 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000224 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000225 const MCInstrInfo *MII, const MCInstPrinter *IP,
226 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000227
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000228 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000229 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
230 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
231 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
232 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000233
Jim Grosbach4b905842013-09-20 23:08:21 +0000234 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000235 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000236 bool parseIdentifier(StringRef &Res) override;
237 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000238
Craig Topper59be68f2014-03-08 07:14:16 +0000239 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000240 /// }
241
242private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000243
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000244 bool parseStatement(ParseStatementInfo &Info,
245 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 void eatToEndOfLine();
247 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000250 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000251 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000252 ArrayRef<MCAsmMacroParameter> Parameters,
253 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000254 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000255
Eli Benderskya313ae62013-01-16 18:56:50 +0000256 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000258
259 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000260 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000261
262 /// \brief Lookup a previously defined macro.
263 /// \param Name Macro name.
264 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000268 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
273 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000276 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000277 ///
278 /// \param M The macro.
279 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
David Majnemer91fc4c22014-01-29 18:57:46 +0000285 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000286 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
288 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 void printMacroInstantiations();
292 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000293 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000294 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000295 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000296 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000297
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 /// \brief Enter the specified file. This returns true on failure.
299 bool enterIncludeFile(const std::string &Filename);
300
301 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000302 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000303 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000304
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000305 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000306 /// current token is not set; clients should ensure Lex() is called
307 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000308 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000309 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000310 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000311 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000312
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000313 /// \brief Parse up to the end of statement and a return the contents from the
314 /// current token until the end of the statement; the current token on exit
315 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000316 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000318 /// \brief Parse until the end of a statement or a comma is encountered,
319 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000320 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000321
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000323 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000324
Jim Grosbach4b905842013-09-20 23:08:21 +0000325 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
326 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
327 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000330
Eli Bendersky17233942013-01-15 22:59:42 +0000331 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000332 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000333 DK_NO_DIRECTIVE, // Placeholder
334 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000335 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
336 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000337 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000338 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000339 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
341 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
342 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
343 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000344 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
345 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
346 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000347 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
348 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
349 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
350 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
351 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
352 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000353 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000354 DK_MACROS_ON, DK_MACROS_OFF,
355 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000356 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000357 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000358 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000359 };
360
Jim Grosbach4b905842013-09-20 23:08:21 +0000361 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000362 /// directives parsed by this class.
363 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000364
365 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
367 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000368 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
370 bool parseDirectiveFill(); // ".fill"
371 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000372 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
374 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000375 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000377
Eli Bendersky17233942013-01-15 22:59:42 +0000378 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveFile(SMLoc DirectiveLoc);
380 bool parseDirectiveLine();
381 bool parseDirectiveLoc();
382 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000383
384 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000386 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000387 bool parseDirectiveCFISections();
388 bool parseDirectiveCFIStartProc();
389 bool parseDirectiveCFIEndProc();
390 bool parseDirectiveCFIDefCfaOffset();
391 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIAdjustCfaOffset();
393 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
394 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
397 bool parseDirectiveCFIRememberState();
398 bool parseDirectiveCFIRestoreState();
399 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIEscape();
402 bool parseDirectiveCFISignalFrame();
403 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000404
405 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000406 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000407 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000408 bool parseDirectiveEndMacro(StringRef Directive);
409 bool parseDirectiveMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
Eli Benderskyf483ff92012-12-20 19:05:53 +0000412 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418
Eli Bendersky17233942013-01-15 22:59:42 +0000419 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
422 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000424
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000426 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveAbort(); // ".abort"
432 bool parseDirectiveInclude(); // ".include"
433 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000435 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
436 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000437 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000439 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000441 // ".ifeqs"
442 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000443 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
445 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
446 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
447 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000448 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000449
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000452
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
455 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000456 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000457 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
459 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
460 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000461
Chad Rosierc7f552c2013-02-12 21:33:51 +0000462 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000463 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 size_t Len);
465
466 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000468
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000469 // "end"
470 bool parseDirectiveEnd(SMLoc DirectiveLoc);
471
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000472 // ".err" or ".error"
473 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000474
Nico Weber404012b2014-07-24 16:26:06 +0000475 // ".warning"
476 bool parseDirectiveWarning(SMLoc DirectiveLoc);
477
Eli Bendersky17233942013-01-15 22:59:42 +0000478 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000479};
Daniel Dunbar86033402010-07-12 17:54:38 +0000480}
481
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000482namespace llvm {
483
484extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000485extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000486extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000487
488}
489
Chris Lattnerc35681b2010-01-19 19:46:13 +0000490enum { DEFAULT_ADDRSPACE = 0 };
491
Jim Grosbach4b905842013-09-20 23:08:21 +0000492AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
493 const MCAsmInfo &_MAI)
494 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000495 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
496 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannard652ec6e2014-11-03 12:02:51 +0000497 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false),
498 LastFuncSymbol(nullptr) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000499 // Save the old handler.
500 SavedDiagHandler = SrcMgr.getDiagHandler();
501 SavedDiagContext = SrcMgr.getDiagContext();
502 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000503 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000504 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000505
Daniel Dunbarc5011082010-07-12 18:12:02 +0000506 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000507 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
508 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000509 PlatformParser.reset(createCOFFAsmParser());
510 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000512 PlatformParser.reset(createDarwinAsmParser());
513 IsDarwin = true;
514 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000515 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000516 PlatformParser.reset(createELFAsmParser());
517 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000518 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000519
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000520 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000521 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000522}
523
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000527}
528
Jim Grosbach4b905842013-09-20 23:08:21 +0000529void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000530 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000531 for (std::vector<MacroInstantiation *>::const_reverse_iterator
532 it = ActiveMacros.rbegin(),
533 ie = ActiveMacros.rend();
534 it != ie; ++it)
535 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000536 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000537}
538
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000539void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
540 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
541 printMacroInstantiations();
542}
543
Chris Lattnera3a06812011-10-16 04:47:35 +0000544bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000545 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000546 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000547 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
548 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000549 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000553 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000554 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
555 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000556 return true;
557}
558
Jim Grosbach4b905842013-09-20 23:08:21 +0000559bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000560 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000561 unsigned NewBuf =
562 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
563 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000565
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000567 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000568 return false;
569}
Daniel Dunbar43235712010-07-18 18:54:11 +0000570
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000571/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000572/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000573/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000574bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000576 unsigned NewBuf =
577 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
578 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579 return true;
580
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000581 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000582 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 return false;
584}
585
Alp Tokera55b95b2014-07-06 10:33:31 +0000586void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
587 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000588 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
589 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000590}
591
Sean Callanan7a77eae2010-01-21 00:19:58 +0000592const AsmToken &AsmParser::Lex() {
593 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000594
Sean Callanan7a77eae2010-01-21 00:19:58 +0000595 if (tok->is(AsmToken::Eof)) {
596 // If this is the end of an included file, pop the parent file off the
597 // include stack.
598 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
599 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000600 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 tok = &Lexer.Lex();
602 }
603 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000606 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000609}
610
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000611bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000612 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000613 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000614 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000615
Chris Lattner36e02122009-06-21 20:54:55 +0000616 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000618
619 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000620 AsmCond StartingCondState = TheCondState;
621
Kevin Enderby6469fc22011-11-01 22:27:22 +0000622 // If we are generating dwarf for assembly source files save the initial text
623 // section and generate a .file directive.
624 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000625 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
626 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000627 auto InsertResult = getContext().addGenDwarfSection(
628 getStreamer().getCurrentSection().first);
629 assert(InsertResult.second && ".text section should not have debug info yet");
630 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000631 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
632 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 }
634
Chris Lattner73f36112009-07-02 21:53:43 +0000635 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000636 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000637 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000638 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000639 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000640
Daniel Dunbar43325c42010-09-09 22:42:56 +0000641 // We had an error, validate that one was emitted and recover by skipping to
642 // the next line.
643 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000644 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000645 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000646
647 if (TheCondState.TheCond != StartingCondState.TheCond ||
648 TheCondState.Ignore != StartingCondState.Ignore)
649 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000650
651 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000652 const auto &LineTables = getContext().getMCDwarfLineTables();
653 if (!LineTables.empty()) {
654 unsigned Index = 0;
655 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
656 if (File.Name.empty() && Index != 0)
657 TokError("unassigned file number: " + Twine(Index) +
658 " for .file directives");
659 ++Index;
660 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000662
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000663 // Check to see that all assembler local symbols were actually defined.
664 // Targets that don't do subsections via symbols may not want this, though,
665 // so conservatively exclude them. Only do this if we're finalizing, though,
666 // as otherwise we won't necessarilly have seen everything yet.
667 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
668 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
669 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000670 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 i != e; ++i) {
672 MCSymbol *Sym = i->getValue();
673 // Variable symbols may not be marked as defined, so check those
674 // explicitly. If we know it's a variable, we have a definition for
675 // the purposes of this check.
676 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
677 // FIXME: We would really like to refer back to where the symbol was
678 // first referenced for a source location. We need to add something
679 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000680 printMessage(
681 getLexer().getLoc(), SourceMgr::DK_Error,
682 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000683 }
684 }
685
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000686 // Finalize the output stream if there are no errors and if the client wants
687 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000688 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000689 Out.Finish();
690
Chris Lattner73f36112009-07-02 21:53:43 +0000691 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000692}
693
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000694void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000695 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000696 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000697 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000698 }
699}
700
Jim Grosbach4b905842013-09-20 23:08:21 +0000701/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000703 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000704 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000705
Chris Lattnere5074c42009-06-22 01:29:09 +0000706 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000707 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000708 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000709}
710
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000711StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000712 const char *Start = getTok().getLoc().getPointer();
713
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000715 Lex();
716
717 const char *End = getTok().getLoc().getPointer();
718 return StringRef(Start, End - Start);
719}
Chris Lattner78db3622009-06-22 05:51:26 +0000720
Jim Grosbach4b905842013-09-20 23:08:21 +0000721StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000722 const char *Start = getTok().getLoc().getPointer();
723
724 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000725 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000726 Lex();
727
728 const char *End = getTok().getLoc().getPointer();
729 return StringRef(Start, End - Start);
730}
731
Jim Grosbach4b905842013-09-20 23:08:21 +0000732/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000733/// NOTE: This assumes the leading '(' has already been consumed.
734///
735/// parenexpr ::= expr)
736///
Jim Grosbach4b905842013-09-20 23:08:21 +0000737bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
738 if (parseExpression(Res))
739 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000740 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000742 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000743 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000744 return false;
745}
Chris Lattner78db3622009-06-22 05:51:26 +0000746
Jim Grosbach4b905842013-09-20 23:08:21 +0000747/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000748/// NOTE: This assumes the leading '[' has already been consumed.
749///
750/// bracketexpr ::= expr]
751///
Jim Grosbach4b905842013-09-20 23:08:21 +0000752bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
753 if (parseExpression(Res))
754 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000755 if (Lexer.isNot(AsmToken::RBrac))
756 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000757 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000758 Lex();
759 return false;
760}
761
Jim Grosbach4b905842013-09-20 23:08:21 +0000762/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000763/// primaryexpr ::= (parenexpr
764/// primaryexpr ::= symbol
765/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000766/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000767/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000768bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000769 SMLoc FirstTokenLoc = getLexer().getLoc();
770 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
771 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000772 default:
773 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000774 // If we have an error assume that we've already handled it.
775 case AsmToken::Error:
776 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000777 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000778 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000779 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000780 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000781 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000782 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000783 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000784 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000785 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000786 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000787 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000788 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000789 if (FirstTokenKind == AsmToken::Dollar) {
790 if (Lexer.getMAI().getDollarIsPC()) {
791 // This is a '$' reference, which references the current PC. Emit a
792 // temporary label to the streamer and refer to it.
793 MCSymbol *Sym = Ctx.CreateTempSymbol();
794 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000795 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
796 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 EndLoc = FirstTokenLoc;
798 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000799 }
800 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000801 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000802 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000803 // Parse symbol variant
804 std::pair<StringRef, StringRef> Split;
805 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000806 if (FirstTokenKind == AsmToken::String) {
807 if (Lexer.is(AsmToken::At)) {
808 Lexer.Lex(); // eat @
809 SMLoc AtLoc = getLexer().getLoc();
810 StringRef VName;
811 if (parseIdentifier(VName))
812 return Error(AtLoc, "expected symbol variant after '@'");
813
814 Split = std::make_pair(Identifier, VName);
815 }
816 } else {
817 Split = Identifier.split('@');
818 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000819 } else if (Lexer.is(AsmToken::LParen)) {
820 Lexer.Lex(); // eat (
821 StringRef VName;
822 parseIdentifier(VName);
823 if (Lexer.isNot(AsmToken::RParen)) {
824 return Error(Lexer.getTok().getLoc(),
825 "unexpected token in variant, expected ')'");
826 }
827 Lexer.Lex(); // eat )
828 Split = std::make_pair(Identifier, VName);
829 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000830
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000831 EndLoc = SMLoc::getFromPointer(Identifier.end());
832
Daniel Dunbard20cda02009-10-16 01:34:54 +0000833 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000834 StringRef SymbolName = Identifier;
835 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000836
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000837 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000839 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 if (Variant != MCSymbolRefExpr::VK_Invalid) {
841 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000842 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000843 Variant = MCSymbolRefExpr::VK_None;
844 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000845 return Error(SMLoc::getFromPointer(Split.second.begin()),
846 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000847 }
848 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000849
Hans Wennborgce69d772013-10-18 20:46:28 +0000850 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
851
Daniel Dunbard20cda02009-10-16 01:34:54 +0000852 // If this is an absolute variable reference, substitute it now to preserve
853 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000855 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000856 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000857
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000858 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000859 return false;
860 }
861
862 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000863 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000864 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000865 }
David Woodhousef42a6662014-02-01 16:20:54 +0000866 case AsmToken::BigNum:
867 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000868 case AsmToken::Integer: {
869 SMLoc Loc = getTok().getLoc();
870 int64_t IntVal = getTok().getIntVal();
871 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000872 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000873 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000874 // Look for 'b' or 'f' following an Integer as a directional label
875 if (Lexer.getKind() == AsmToken::Identifier) {
876 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000877 // Lookup the symbol variant if used.
878 std::pair<StringRef, StringRef> Split = IDVal.split('@');
879 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
880 if (Split.first.size() != IDVal.size()) {
881 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000882 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000884 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000886 if (IDVal == "f" || IDVal == "b") {
887 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000888 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000889 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000890 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000892 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 Lex(); // Eat identifier.
894 }
895 }
Chris Lattner78db3622009-06-22 05:51:26 +0000896 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000898 case AsmToken::Real: {
899 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000900 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000901 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000902 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 Lex(); // Eat token.
904 return false;
905 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000906 case AsmToken::Dot: {
907 // This is a '.' reference, which references the current PC. Emit a
908 // temporary label to the streamer and refer to it.
909 MCSymbol *Sym = Ctx.CreateTempSymbol();
910 Out.EmitLabel(Sym);
911 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000912 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000913 Lex(); // Eat identifier.
914 return false;
915 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000916 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000917 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000918 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000919 case AsmToken::LBrac:
920 if (!PlatformParser->HasBracketExpressions())
921 return TokError("brackets expression not supported on this target");
922 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000928 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000936 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000937 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000938 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000940 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000942 }
943}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000944
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000945bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000946 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000947 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000948}
949
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000951AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000952 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000953 // Ask the target implementation about this expression first.
954 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
955 if (NewE)
956 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 // Recurse over the given expression, rebuilding it to apply the given variant
958 // if there is exactly one symbol.
959 switch (E->getKind()) {
960 case MCExpr::Target:
961 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000962 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963
964 case MCExpr::SymbolRef: {
965 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
966
967 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000968 TokError("invalid variant on expression '" + getTok().getIdentifier() +
969 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 return E;
971 }
972
973 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
974 }
975
976 case MCExpr::Unary: {
977 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000978 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000980 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
982 }
983
984 case MCExpr::Binary: {
985 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
987 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000988
989 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000990 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000991
Jim Grosbach4b905842013-09-20 23:08:21 +0000992 if (!LHS)
993 LHS = BE->getLHS();
994 if (!RHS)
995 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
997 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
998 }
999 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001000
Craig Toppera2886c22012-02-07 05:05:23 +00001001 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002}
1003
Jim Grosbach4b905842013-09-20 23:08:21 +00001004/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001005///
Jim Grosbachbd164242011-08-20 16:24:13 +00001006/// expr ::= expr &&,|| expr -> lowest.
1007/// expr ::= expr |,^,&,! expr
1008/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1009/// expr ::= expr <<,>> expr
1010/// expr ::= expr +,- expr
1011/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001012/// expr ::= primaryexpr
1013///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001014bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001015 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001016 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001018 return true;
1019
Daniel Dunbar55f16672010-09-17 02:47:07 +00001020 // As a special case, we support 'a op b @ modifier' by rewriting the
1021 // expression to include the modifier. This is inefficient, but in general we
1022 // expect users to use 'a@modifier op b'.
1023 if (Lexer.getKind() == AsmToken::At) {
1024 Lex();
1025
1026 if (Lexer.isNot(AsmToken::Identifier))
1027 return TokError("unexpected symbol modifier following '@'");
1028
1029 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (Variant == MCSymbolRefExpr::VK_Invalid)
1032 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1033
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001035 if (!ModifiedRes) {
1036 return TokError("invalid modifier '" + getTok().getIdentifier() +
1037 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001038 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001039
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 Res = ModifiedRes;
1041 Lex();
1042 }
1043
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001044 // Try to constant fold it up front, if possible.
1045 int64_t Value;
1046 if (Res->EvaluateAsAbsolute(Value))
1047 Res = MCConstantExpr::Create(Value, getContext());
1048
1049 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001050}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001051
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001052bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001053 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001054 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001055}
1056
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001058 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001059
Daniel Dunbar75630b32009-06-30 02:10:03 +00001060 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001061 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062 return true;
1063
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001064 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001065 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001066
1067 return false;
1068}
1069
Michael J. Spencer530ce852010-10-09 11:00:50 +00001070static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001071 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001072 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001073 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001074 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075
Jim Grosbach4b905842013-09-20 23:08:21 +00001076 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001079 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001082 return 1;
1083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Low Precedence: |, &, ^
1085 //
1086 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 case AsmToken::EqualEqual:
1099 Kind = MCBinaryExpr::EQ;
1100 return 3;
1101 case AsmToken::ExclaimEqual:
1102 case AsmToken::LessGreater:
1103 Kind = MCBinaryExpr::NE;
1104 return 3;
1105 case AsmToken::Less:
1106 Kind = MCBinaryExpr::LT;
1107 return 3;
1108 case AsmToken::LessEqual:
1109 Kind = MCBinaryExpr::LTE;
1110 return 3;
1111 case AsmToken::Greater:
1112 Kind = MCBinaryExpr::GT;
1113 return 3;
1114 case AsmToken::GreaterEqual:
1115 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001116 return 3;
1117
Jim Grosbach4b905842013-09-20 23:08:21 +00001118 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 case AsmToken::LessLess:
1120 Kind = MCBinaryExpr::Shl;
1121 return 4;
1122 case AsmToken::GreaterGreater:
1123 Kind = MCBinaryExpr::Shr;
1124 return 4;
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127 case AsmToken::Plus:
1128 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130 case AsmToken::Minus:
1131 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001132 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 }
1145}
1146
Jim Grosbach4b905842013-09-20 23:08:21 +00001147/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001149bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001150 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001152 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001153 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001154
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 // If the next token is lower precedence than we are allowed to eat, return
1156 // successfully with what we ate already.
1157 if (TokPrec < Precedence)
1158 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Sean Callanan686ed8d2010-01-19 20:22:31 +00001160 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001162 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001163 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 if (parsePrimaryExpr(RHS, EndLoc))
1165 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 // If BinOp binds less tightly with RHS than the operator after RHS, let
1168 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001169 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001171 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1172 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001174 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001175 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001176 }
1177}
1178
Chris Lattner36e02122009-06-21 20:54:55 +00001179/// ParseStatement:
1180/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001181/// ::= Label* Directive ...Operands... EndOfStatement
1182/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001183bool AsmParser::parseStatement(ParseStatementInfo &Info,
1184 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001185 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001186 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001187 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001188 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001189 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001191 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001192 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001193 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001195 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001197 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001198 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001199
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 if (Lexer.is(AsmToken::Integer)) {
1202 LocalLabelVal = getTok().getIntVal();
1203 if (LocalLabelVal < 0) {
1204 if (!TheCondState.Ignore)
1205 return TokError("unexpected token at start of statement");
1206 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001207 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001208 IDVal = getTok().getString();
1209 Lex(); // Consume the integer token to be used as an identifier token.
1210 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001211 if (!TheCondState.Ignore)
1212 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 }
1214 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001215 } else if (Lexer.is(AsmToken::Dot)) {
1216 // Treat '.' as a valid identifier in this context.
1217 Lex();
1218 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001219 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001220 if (!TheCondState.Ignore)
1221 return TokError("unexpected token at start of statement");
1222 IDVal = "";
1223 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001224
Chris Lattner926885c2010-04-17 18:14:27 +00001225 // Handle conditional assembly here before checking for skipping. We
1226 // have to do this so that .endif isn't skipped in a ".if 0" block for
1227 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001228 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001229 DirectiveKindMap.find(IDVal);
1230 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1231 ? DK_NO_DIRECTIVE
1232 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001233 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001234 default:
1235 break;
1236 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001237 case DK_IFEQ:
1238 case DK_IFGE:
1239 case DK_IFGT:
1240 case DK_IFLE:
1241 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001242 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001243 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 case DK_IFB:
1245 return parseDirectiveIfb(IDLoc, true);
1246 case DK_IFNB:
1247 return parseDirectiveIfb(IDLoc, false);
1248 case DK_IFC:
1249 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001250 case DK_IFEQS:
1251 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001252 case DK_IFNC:
1253 return parseDirectiveIfc(IDLoc, false);
1254 case DK_IFDEF:
1255 return parseDirectiveIfdef(IDLoc, true);
1256 case DK_IFNDEF:
1257 case DK_IFNOTDEF:
1258 return parseDirectiveIfdef(IDLoc, false);
1259 case DK_ELSEIF:
1260 return parseDirectiveElseIf(IDLoc);
1261 case DK_ELSE:
1262 return parseDirectiveElse(IDLoc);
1263 case DK_ENDIF:
1264 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001265 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001266
Eli Bendersky88024712013-01-16 19:32:36 +00001267 // Ignore the statement if in the middle of inactive conditional
1268 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001269 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001270 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001271 return false;
1272 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001273
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001274 // FIXME: Recurse on local labels?
1275
1276 // See what kind of statement we have.
1277 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001278 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001279 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001280
Chris Lattner36e02122009-06-21 20:54:55 +00001281 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001282 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001283
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001284 // Diagnose attempt to use '.' as a label.
1285 if (IDVal == ".")
1286 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1287
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001288 // Diagnose attempt to use a variable as a label.
1289 //
1290 // FIXME: Diagnostics. Note the location of the definition as a label.
1291 // FIXME: This doesn't diagnose assignment to a symbol which has been
1292 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001293 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001294 if (LocalLabelVal == -1) {
1295 if (ParsingInlineAsm && SI) {
1296 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1297 assert(RewrittenLabel.size() && "We should have an internal name here.");
1298 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1299 IDVal.size(), RewrittenLabel));
1300 IDVal = RewrittenLabel;
1301 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001302 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001303 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001304 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001305 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001306 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001307
Daniel Dunbare73b2672009-08-26 22:13:22 +00001308 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001309 if (!ParsingInlineAsm)
1310 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001311
Oliver Stannard652ec6e2014-11-03 12:02:51 +00001312 // Record the symbol, so that it can be used for call frame information
1313 LastFuncSymbol = Sym;
1314
Kevin Enderbye7739d42011-12-09 18:09:40 +00001315 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001316 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001318 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1319 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001320
Tim Northover1744d0a2013-10-25 12:49:50 +00001321 getTargetParser().onLabelParsed(Sym);
1322
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001323 // Consume any end of statement token, if present, to avoid spurious
1324 // AddBlankLine calls().
1325 if (Lexer.is(AsmToken::EndOfStatement)) {
1326 Lex();
1327 if (Lexer.is(AsmToken::Eof))
1328 return false;
1329 }
1330
Eli Friedman0f4871d2012-10-22 23:58:19 +00001331 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001332 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001334 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001336 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337
Jim Grosbach4b905842013-09-20 23:08:21 +00001338 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001339
1340 default: // Normal instruction or directive.
1341 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001342 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001343
1344 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001345 if (areMacrosEnabled())
1346 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1347 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001348 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001349
Michael J. Spencer530ce852010-10-09 11:00:50 +00001350 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001351
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001353 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001355 //
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // 1. The target-specific assembly parser. Some directives are target
1357 // specific or may potentially behave differently on certain targets.
1358 // 2. Asm parser extensions. For example, platform-specific parsers
1359 // (like the ELF parser) register themselves as extensions.
1360 // 3. The generic directive parser implemented by this class. These are
1361 // all the directives that behave in a target and platform independent
1362 // manner, or at least have a default behavior that's shared between
1363 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001364
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // First query the target-specific parser. It will return 'true' if it
1366 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001367 if (!getTargetParser().ParseDirective(ID))
1368 return false;
1369
Alp Tokercb402912014-01-24 17:20:08 +00001370 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001371 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001372 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1373 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001374 if (Handler.first)
1375 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1376
1377 // Finally, if no one else is interested in this directive, it must be
1378 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001379 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001380 default:
1381 break;
1382 case DK_SET:
1383 case DK_EQU:
1384 return parseDirectiveSet(IDVal, true);
1385 case DK_EQUIV:
1386 return parseDirectiveSet(IDVal, false);
1387 case DK_ASCII:
1388 return parseDirectiveAscii(IDVal, false);
1389 case DK_ASCIZ:
1390 case DK_STRING:
1391 return parseDirectiveAscii(IDVal, true);
1392 case DK_BYTE:
1393 return parseDirectiveValue(1);
1394 case DK_SHORT:
1395 case DK_VALUE:
1396 case DK_2BYTE:
1397 return parseDirectiveValue(2);
1398 case DK_LONG:
1399 case DK_INT:
1400 case DK_4BYTE:
1401 return parseDirectiveValue(4);
1402 case DK_QUAD:
1403 case DK_8BYTE:
1404 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001405 case DK_OCTA:
1406 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001407 case DK_SINGLE:
1408 case DK_FLOAT:
1409 return parseDirectiveRealValue(APFloat::IEEEsingle);
1410 case DK_DOUBLE:
1411 return parseDirectiveRealValue(APFloat::IEEEdouble);
1412 case DK_ALIGN: {
1413 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1414 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1415 }
1416 case DK_ALIGN32: {
1417 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1418 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1419 }
1420 case DK_BALIGN:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1422 case DK_BALIGNW:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1424 case DK_BALIGNL:
1425 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1426 case DK_P2ALIGN:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1428 case DK_P2ALIGNW:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1430 case DK_P2ALIGNL:
1431 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1432 case DK_ORG:
1433 return parseDirectiveOrg();
1434 case DK_FILL:
1435 return parseDirectiveFill();
1436 case DK_ZERO:
1437 return parseDirectiveZero();
1438 case DK_EXTERN:
1439 eatToEndOfStatement(); // .extern is the default, ignore it.
1440 return false;
1441 case DK_GLOBL:
1442 case DK_GLOBAL:
1443 return parseDirectiveSymbolAttribute(MCSA_Global);
1444 case DK_LAZY_REFERENCE:
1445 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1446 case DK_NO_DEAD_STRIP:
1447 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1448 case DK_SYMBOL_RESOLVER:
1449 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1450 case DK_PRIVATE_EXTERN:
1451 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1452 case DK_REFERENCE:
1453 return parseDirectiveSymbolAttribute(MCSA_Reference);
1454 case DK_WEAK_DEFINITION:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1456 case DK_WEAK_REFERENCE:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1458 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1459 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1460 case DK_COMM:
1461 case DK_COMMON:
1462 return parseDirectiveComm(/*IsLocal=*/false);
1463 case DK_LCOMM:
1464 return parseDirectiveComm(/*IsLocal=*/true);
1465 case DK_ABORT:
1466 return parseDirectiveAbort();
1467 case DK_INCLUDE:
1468 return parseDirectiveInclude();
1469 case DK_INCBIN:
1470 return parseDirectiveIncbin();
1471 case DK_CODE16:
1472 case DK_CODE16GCC:
1473 return TokError(Twine(IDVal) + " not supported yet");
1474 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001475 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001476 case DK_IRP:
1477 return parseDirectiveIrp(IDLoc);
1478 case DK_IRPC:
1479 return parseDirectiveIrpc(IDLoc);
1480 case DK_ENDR:
1481 return parseDirectiveEndr(IDLoc);
1482 case DK_BUNDLE_ALIGN_MODE:
1483 return parseDirectiveBundleAlignMode();
1484 case DK_BUNDLE_LOCK:
1485 return parseDirectiveBundleLock();
1486 case DK_BUNDLE_UNLOCK:
1487 return parseDirectiveBundleUnlock();
1488 case DK_SLEB128:
1489 return parseDirectiveLEB128(true);
1490 case DK_ULEB128:
1491 return parseDirectiveLEB128(false);
1492 case DK_SPACE:
1493 case DK_SKIP:
1494 return parseDirectiveSpace(IDVal);
1495 case DK_FILE:
1496 return parseDirectiveFile(IDLoc);
1497 case DK_LINE:
1498 return parseDirectiveLine();
1499 case DK_LOC:
1500 return parseDirectiveLoc();
1501 case DK_STABS:
1502 return parseDirectiveStabs();
1503 case DK_CFI_SECTIONS:
1504 return parseDirectiveCFISections();
1505 case DK_CFI_STARTPROC:
1506 return parseDirectiveCFIStartProc();
1507 case DK_CFI_ENDPROC:
1508 return parseDirectiveCFIEndProc();
1509 case DK_CFI_DEF_CFA:
1510 return parseDirectiveCFIDefCfa(IDLoc);
1511 case DK_CFI_DEF_CFA_OFFSET:
1512 return parseDirectiveCFIDefCfaOffset();
1513 case DK_CFI_ADJUST_CFA_OFFSET:
1514 return parseDirectiveCFIAdjustCfaOffset();
1515 case DK_CFI_DEF_CFA_REGISTER:
1516 return parseDirectiveCFIDefCfaRegister(IDLoc);
1517 case DK_CFI_OFFSET:
1518 return parseDirectiveCFIOffset(IDLoc);
1519 case DK_CFI_REL_OFFSET:
1520 return parseDirectiveCFIRelOffset(IDLoc);
1521 case DK_CFI_PERSONALITY:
1522 return parseDirectiveCFIPersonalityOrLsda(true);
1523 case DK_CFI_LSDA:
1524 return parseDirectiveCFIPersonalityOrLsda(false);
1525 case DK_CFI_REMEMBER_STATE:
1526 return parseDirectiveCFIRememberState();
1527 case DK_CFI_RESTORE_STATE:
1528 return parseDirectiveCFIRestoreState();
1529 case DK_CFI_SAME_VALUE:
1530 return parseDirectiveCFISameValue(IDLoc);
1531 case DK_CFI_RESTORE:
1532 return parseDirectiveCFIRestore(IDLoc);
1533 case DK_CFI_ESCAPE:
1534 return parseDirectiveCFIEscape();
1535 case DK_CFI_SIGNAL_FRAME:
1536 return parseDirectiveCFISignalFrame();
1537 case DK_CFI_UNDEFINED:
1538 return parseDirectiveCFIUndefined(IDLoc);
1539 case DK_CFI_REGISTER:
1540 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001541 case DK_CFI_WINDOW_SAVE:
1542 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001543 case DK_MACROS_ON:
1544 case DK_MACROS_OFF:
1545 return parseDirectiveMacrosOnOff(IDVal);
1546 case DK_MACRO:
1547 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001548 case DK_EXITM:
1549 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001550 case DK_ENDM:
1551 case DK_ENDMACRO:
1552 return parseDirectiveEndMacro(IDVal);
1553 case DK_PURGEM:
1554 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001555 case DK_END:
1556 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001557 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001558 return parseDirectiveError(IDLoc, false);
1559 case DK_ERROR:
1560 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001561 case DK_WARNING:
1562 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001563 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001564
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001565 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001566 }
Chris Lattner36e02122009-06-21 20:54:55 +00001567
Chad Rosierc7f552c2013-02-12 21:33:51 +00001568 // __asm _emit or __asm __emit
1569 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1570 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001571 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001572
1573 // __asm align
1574 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001575 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001576
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001577 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001578
Chris Lattner7cbfa442010-05-19 23:34:33 +00001579 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001580 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001581 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001582 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001583 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001584 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001585
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001586 // Dump the parsed representation, if requested.
1587 if (getShowParsedOperands()) {
1588 SmallString<256> Str;
1589 raw_svector_ostream OS(Str);
1590 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001591 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001592 if (i != 0)
1593 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001594 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001595 }
1596 OS << "]";
1597
Jim Grosbach4b905842013-09-20 23:08:21 +00001598 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001599 }
1600
Oliver Stannard8b273082014-06-19 15:52:37 +00001601 // If we are generating dwarf for the current section then generate a .loc
1602 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001603 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001604 getContext().getGenDwarfSectionSyms().count(
1605 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001606
Eli Bendersky88024712013-01-16 19:32:36 +00001607 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001608
Eli Bendersky88024712013-01-16 19:32:36 +00001609 // If we previously parsed a cpp hash file line comment then make sure the
1610 // current Dwarf File is for the CppHashFilename if not then emit the
1611 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001612 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001613 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1614 0, StringRef(), CppHashFilename);
1615 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001616
Jim Grosbach4b905842013-09-20 23:08:21 +00001617 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1618 // cache with the different Loc from the call above we save the last
1619 // info we queried here with SrcMgr.FindLineNumber().
1620 unsigned CppHashLocLineNo;
1621 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1622 CppHashLocLineNo = LastQueryLine;
1623 else {
1624 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1625 LastQueryLine = CppHashLocLineNo;
1626 LastQueryIDLoc = CppHashLoc;
1627 LastQueryBuffer = CppHashBuf;
1628 }
1629 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001630 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001631
Jim Grosbach4b905842013-09-20 23:08:21 +00001632 getStreamer().EmitDwarfLocDirective(
1633 getContext().getGenDwarfFileNumber(), Line, 0,
1634 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1635 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001636 }
1637
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001638 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001639 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001640 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001641 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1642 Info.ParsedOperands, Out,
1643 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001644 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001645
Chris Lattnera2a9d162010-09-11 16:18:25 +00001646 // Don't skip the rest of the line, the instruction parser is responsible for
1647 // that.
1648 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001649}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001650
Jim Grosbach4b905842013-09-20 23:08:21 +00001651/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001652/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001653void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001654 if (!Lexer.is(AsmToken::EndOfStatement))
1655 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001656 // Eat EOL.
1657 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001658}
1659
Jim Grosbach4b905842013-09-20 23:08:21 +00001660/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001661/// ::= # number "filename"
1662/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001663bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001664 Lex(); // Eat the hash token.
1665
1666 if (getLexer().isNot(AsmToken::Integer)) {
1667 // Consume the line since in cases it is not a well-formed line directive,
1668 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001670 return false;
1671 }
1672
1673 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001674 Lex();
1675
1676 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001677 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001678 return false;
1679 }
1680
1681 StringRef Filename = getTok().getString();
1682 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001684
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1686 CppHashLoc = L;
1687 CppHashFilename = Filename;
1688 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001689 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001690
1691 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001692 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001693 return false;
1694}
1695
Jim Grosbach4b905842013-09-20 23:08:21 +00001696/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001697/// for the Filename and LineNo if any in the diagnostic.
1698void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001699 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700 raw_ostream &OS = errs();
1701
1702 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1703 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001704 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1705 unsigned CppHashBuf =
1706 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707
Jim Grosbach4b905842013-09-20 23:08:21 +00001708 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001710 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1711 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1712 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001713 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1714 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715 }
1716
Eric Christophera7c32732012-12-18 00:30:54 +00001717 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718 // manager changed or buffer changed (like in a nested include) then just
1719 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001720 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001722 if (Parser->SavedDiagHandler)
1723 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1724 else
Craig Topper353eda42014-04-24 06:44:33 +00001725 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001726 return;
1727 }
1728
Eric Christophera7c32732012-12-18 00:30:54 +00001729 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001730 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1731 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001732 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001733
1734 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1735 int CppHashLocLineNo =
1736 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001737 int LineNo =
1738 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001739
Jim Grosbach4b905842013-09-20 23:08:21 +00001740 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1741 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001742 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001743
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001744 if (Parser->SavedDiagHandler)
1745 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1746 else
Craig Topper353eda42014-04-24 06:44:33 +00001747 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001748}
1749
Rafael Espindola2c064482012-08-21 18:29:30 +00001750// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1751// difference being that that function accepts '@' as part of identifiers and
1752// we can't do that. AsmLexer.cpp should probably be changed to handle
1753// '@' as a special case when needed.
1754static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001755 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1756 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001757}
1758
Rafael Espindola34b9c512012-06-03 23:57:14 +00001759bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001760 ArrayRef<MCAsmMacroParameter> Parameters,
1761 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001762 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001763 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001764 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001765 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001766
Preston Gurd05500642012-09-19 20:36:12 +00001767 // A macro without parameters is handled differently on Darwin:
1768 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001769 while (!Body.empty()) {
1770 // Scan for the next substitution.
1771 std::size_t End = Body.size(), Pos = 0;
1772 for (; Pos != End; ++Pos) {
1773 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001774 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001775 // This macro has no parameters, look for $0, $1, etc.
1776 if (Body[Pos] != '$' || Pos + 1 == End)
1777 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001778
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001780 if (Next == '$' || Next == 'n' ||
1781 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 break;
1783 } else {
1784 // This macro has parameters, look for \foo, \bar, etc.
1785 if (Body[Pos] == '\\' && Pos + 1 != End)
1786 break;
1787 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001788 }
1789
1790 // Add the prefix.
1791 OS << Body.slice(0, Pos);
1792
1793 // Check if we reached the end.
1794 if (Pos == End)
1795 break;
1796
Benjamin Kramer513e7442014-02-20 13:36:32 +00001797 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 switch (Body[Pos + 1]) {
1799 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001800 case '$':
1801 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001802 break;
1803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001805 case 'n':
1806 OS << A.size();
1807 break;
1808
Jim Grosbach4b905842013-09-20 23:08:21 +00001809 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001810 default: {
1811 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001813 if (Index >= A.size())
1814 break;
1815
1816 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001817 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001818 ie = A[Index].end();
1819 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001820 OS << it->getString();
1821 break;
1822 }
1823 }
1824 Pos += 2;
1825 } else {
1826 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001827 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828 ++I;
1829
Jim Grosbach4b905842013-09-20 23:08:21 +00001830 const char *Begin = Body.data() + Pos + 1;
1831 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001832 unsigned Index = 0;
1833 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001834 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001835 break;
1836
Preston Gurd05500642012-09-19 20:36:12 +00001837 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001838 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1839 Pos += 3;
1840 else {
1841 OS << '\\' << Argument;
1842 Pos = I;
1843 }
Preston Gurd05500642012-09-19 20:36:12 +00001844 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001845 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001846 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001847 ie = A[Index].end();
1848 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001849 // We expect no quotes around the string's contents when
1850 // parsing for varargs.
1851 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001852 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001853 else
1854 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001855
Preston Gurd05500642012-09-19 20:36:12 +00001856 Pos += 1 + Argument.size();
1857 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001858 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001859 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001860 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001861 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001862
Rafael Espindola1134ab232011-06-05 02:43:45 +00001863 return false;
1864}
Daniel Dunbar43235712010-07-18 18:54:11 +00001865
Nico Weber2a8f9222014-07-24 16:29:04 +00001866MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001867 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001868 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001869 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001870
Jim Grosbach4b905842013-09-20 23:08:21 +00001871static bool isOperator(AsmToken::TokenKind kind) {
1872 switch (kind) {
1873 default:
1874 return false;
1875 case AsmToken::Plus:
1876 case AsmToken::Minus:
1877 case AsmToken::Tilde:
1878 case AsmToken::Slash:
1879 case AsmToken::Star:
1880 case AsmToken::Dot:
1881 case AsmToken::Equal:
1882 case AsmToken::EqualEqual:
1883 case AsmToken::Pipe:
1884 case AsmToken::PipePipe:
1885 case AsmToken::Caret:
1886 case AsmToken::Amp:
1887 case AsmToken::AmpAmp:
1888 case AsmToken::Exclaim:
1889 case AsmToken::ExclaimEqual:
1890 case AsmToken::Percent:
1891 case AsmToken::Less:
1892 case AsmToken::LessEqual:
1893 case AsmToken::LessLess:
1894 case AsmToken::LessGreater:
1895 case AsmToken::Greater:
1896 case AsmToken::GreaterEqual:
1897 case AsmToken::GreaterGreater:
1898 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001899 }
1900}
1901
David Majnemer16252452014-01-29 00:07:39 +00001902namespace {
1903class AsmLexerSkipSpaceRAII {
1904public:
1905 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1906 Lexer.setSkipSpace(SkipSpace);
1907 }
1908
1909 ~AsmLexerSkipSpaceRAII() {
1910 Lexer.setSkipSpace(true);
1911 }
1912
1913private:
1914 AsmLexer &Lexer;
1915};
1916}
1917
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001918bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1919
1920 if (Vararg) {
1921 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1922 StringRef Str = parseStringToEndOfStatement();
1923 MA.push_back(AsmToken(AsmToken::String, Str));
1924 }
1925 return false;
1926 }
1927
Rafael Espindola768b41c2012-06-15 14:02:34 +00001928 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001929 unsigned AddTokens = 0;
1930
David Majnemer16252452014-01-29 00:07:39 +00001931 // Darwin doesn't use spaces to delmit arguments.
1932 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001933
1934 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001935 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001937
David Majnemer91fc4c22014-01-29 18:57:46 +00001938 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001939 break;
Preston Gurd05500642012-09-19 20:36:12 +00001940
1941 if (Lexer.is(AsmToken::Space)) {
1942 Lex(); // Eat spaces
1943
1944 // Spaces can delimit parameters, but could also be part an expression.
1945 // If the token after a space is an operator, add the token and the next
1946 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001947 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001948 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001949 // Check to see whether the token is used as an operator,
1950 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001951 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001952 if (*NextChar == ' ')
1953 AddTokens = 2;
1954 }
1955
1956 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001957 break;
1958 }
1959 }
1960 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961
Jim Grosbach4b905842013-09-20 23:08:21 +00001962 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963 // to be able to fill in the remaining default parameter values
1964 if (Lexer.is(AsmToken::EndOfStatement))
1965 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001966
1967 // Adjust the current parentheses level.
1968 if (Lexer.is(AsmToken::LParen))
1969 ++ParenLevel;
1970 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1971 --ParenLevel;
1972
1973 // Append the token to the current argument list.
1974 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001975 if (AddTokens)
1976 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001977 Lex();
1978 }
Preston Gurd05500642012-09-19 20:36:12 +00001979
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001981 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001982 return false;
1983}
1984
1985// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001986bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001987 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001988 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001989 bool NamedParametersFound = false;
1990 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001991
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001992 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001993 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001994
Rafael Espindola768b41c2012-06-15 14:02:34 +00001995 // Parse two kinds of macro invocations:
1996 // - macros defined without any parameters accept an arbitrary number of them
1997 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001998 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001999 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2000 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002001 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002002 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002003
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002004 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002005 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002006 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002007 eatToEndOfStatement();
2008 return true;
2009 }
2010
2011 if (!Lexer.is(AsmToken::Equal)) {
2012 TokError("expected '=' after formal parameter identifier");
2013 eatToEndOfStatement();
2014 return true;
2015 }
2016 Lex();
2017
2018 NamedParametersFound = true;
2019 }
2020
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002021 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002022 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002023 eatToEndOfStatement();
2024 return true;
2025 }
2026
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002027 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2028 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002029 return true;
2030
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002032 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 unsigned FAI = 0;
2034 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002035 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002037
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002038 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002039 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002040 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002041 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002042 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002043 return true;
2044 }
2045 PI = FAI;
2046 }
2047
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002048 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002049 if (A.size() <= PI)
2050 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002051 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002052
2053 if (FALocs.size() <= PI)
2054 FALocs.resize(PI + 1);
2055
2056 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002057 }
Jim Grosbach206661622012-07-30 22:44:17 +00002058
Preston Gurd242ed3152012-09-19 20:29:04 +00002059 // At the end of the statement, fill in remaining arguments that have
2060 // default values. If there aren't any, then the next argument is
2061 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002062 if (Lexer.is(AsmToken::EndOfStatement)) {
2063 bool Failure = false;
2064 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2065 if (A[FAI].empty()) {
2066 if (M->Parameters[FAI].Required) {
2067 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2068 "missing value for required parameter "
2069 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2070 Failure = true;
2071 }
2072
2073 if (!M->Parameters[FAI].Value.empty())
2074 A[FAI] = M->Parameters[FAI].Value;
2075 }
2076 }
2077 return Failure;
2078 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002079
2080 if (Lexer.is(AsmToken::Comma))
2081 Lex();
2082 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002083
2084 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002085}
2086
Jim Grosbach4b905842013-09-20 23:08:21 +00002087const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002088 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2089 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002090}
2091
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002092void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2093 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002094}
2095
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002096void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002097
Jim Grosbach4b905842013-09-20 23:08:21 +00002098bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002099 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2100 // this, although we should protect against infinite loops.
2101 if (ActiveMacros.size() == 20)
2102 return TokError("macros cannot be nested more than 20 levels deep");
2103
Eli Bendersky38274122013-01-14 23:22:36 +00002104 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002105 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002106 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002107
Rafael Espindola1134ab232011-06-05 02:43:45 +00002108 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2109 // to hold the macro body with substitutions.
2110 SmallString<256> Buf;
2111 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002112 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002114 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002115 return true;
2116
Eli Bendersky38274122013-01-14 23:22:36 +00002117 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002118 // instantiation.
2119 OS << ".endmacro\n";
2120
Rafael Espindola3560ff22014-08-27 20:03:13 +00002121 std::unique_ptr<MemoryBuffer> Instantiation =
2122 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002123
Daniel Dunbar43235712010-07-18 18:54:11 +00002124 // Create the macro instantiation object and add to the current macro
2125 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002126 MacroInstantiation *MI = new MacroInstantiation(
2127 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 ActiveMacros.push_back(MI);
2129
2130 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002131 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002132 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002133 Lex();
2134
2135 return false;
2136}
2137
Jim Grosbach4b905842013-09-20 23:08:21 +00002138void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002140 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002141 Lex();
2142
2143 // Pop the instantiation entry.
2144 delete ActiveMacros.back();
2145 ActiveMacros.pop_back();
2146}
2147
Jim Grosbach4b905842013-09-20 23:08:21 +00002148static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002149 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002150 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002151 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2152 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002153 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002154 case MCExpr::Target:
2155 case MCExpr::Constant:
2156 return false;
2157 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002158 const MCSymbol &S =
2159 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002160 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002161 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002162 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002163 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002164 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002165 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002166 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002167
2168 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002169}
2170
Jim Grosbach4b905842013-09-20 23:08:21 +00002171bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002172 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002173 // FIXME: Use better location, we should use proper tokens.
2174 SMLoc EqualLoc = Lexer.getLoc();
2175
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002176 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002177 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002178 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002179
Rafael Espindola72f5f172012-01-28 05:57:00 +00002180 // Note: we don't count b as used in "a = b". This is to allow
2181 // a = b
2182 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002183
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002184 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002185 return TokError("unexpected token in assignment");
2186
2187 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002188 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189
Daniel Dunbar5f339242009-10-16 01:57:39 +00002190 // Validate that the LHS is allowed to be a variable (either it has not been
2191 // used as a symbol, or it is an absolute symbol).
2192 MCSymbol *Sym = getContext().LookupSymbol(Name);
2193 if (Sym) {
2194 // Diagnose assignment to a label.
2195 //
2196 // FIXME: Diagnostics. Note the location of the definition as a label.
2197 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002198 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002199 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2200 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002201 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002202 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2203 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002204 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002205 return Error(EqualLoc, "redefinition of '" + Name + "'");
2206 else if (!Sym->isVariable())
2207 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002208 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002209 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002210 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002211
2212 // Don't count these checks as uses.
2213 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002214 } else if (Name == ".") {
2215 if (Out.EmitValueToOffset(Value, 0)) {
2216 Error(EqualLoc, "expected absolute expression");
2217 eatToEndOfStatement();
2218 }
2219 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002220 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002221 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002223 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002224 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002225 if (NoDeadStrip)
2226 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2227
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002228 return false;
2229}
2230
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002231/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002232/// ::= identifier
2233/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002235 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002236 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2237 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002238 // handle this as a context dependent token, instead we detect adjacent tokens
2239 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2241 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242
Hans Wennborgce69d772013-10-18 20:46:28 +00002243 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 Lex();
2245 if (Lexer.isNot(AsmToken::Identifier))
2246 return true;
2247
Hans Wennborgce69d772013-10-18 20:46:28 +00002248 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2249 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250 return true;
2251
2252 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002253 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002254 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002255 Lex();
2256 return false;
2257 }
2258
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002260 return true;
2261
Sean Callanan936b0d32010-01-19 21:44:56 +00002262 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263
Sean Callanan686ed8d2010-01-19 20:22:31 +00002264 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
2266 return false;
2267}
2268
Jim Grosbach4b905842013-09-20 23:08:21 +00002269/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002270/// ::= .equ identifier ',' expression
2271/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002272/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002273bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002275
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002276 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002277 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002278
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002280 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002281 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002282
Jim Grosbach4b905842013-09-20 23:08:21 +00002283 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284}
2285
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002286bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002288
2289 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002290 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002291 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2292 if (Str[i] != '\\') {
2293 Data += Str[i];
2294 continue;
2295 }
2296
2297 // Recognize escaped characters. Note that this escape semantics currently
2298 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2299 ++i;
2300 if (i == e)
2301 return TokError("unexpected backslash at end of string");
2302
2303 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002304 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002305 // Consume up to three octal characters.
2306 unsigned Value = Str[i] - '0';
2307
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 ++i;
2310 Value = Value * 8 + (Str[i] - '0');
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 ++i;
2314 Value = Value * 8 + (Str[i] - '0');
2315 }
2316 }
2317
2318 if (Value > 255)
2319 return TokError("invalid octal escape sequence (out of range)");
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 continue;
2323 }
2324
2325 // Otherwise recognize individual escapes.
2326 switch (Str[i]) {
2327 default:
2328 // Just reject invalid escape sequences for now.
2329 return TokError("invalid escape sequence (unrecognized character)");
2330
2331 case 'b': Data += '\b'; break;
2332 case 'f': Data += '\f'; break;
2333 case 'n': Data += '\n'; break;
2334 case 'r': Data += '\r'; break;
2335 case 't': Data += '\t'; break;
2336 case '"': Data += '"'; break;
2337 case '\\': Data += '\\'; break;
2338 }
2339 }
2340
2341 return false;
2342}
2343
Jim Grosbach4b905842013-09-20 23:08:21 +00002344/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002345/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002346bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002347 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002348 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002349
Daniel Dunbara10e5192009-06-24 23:30:00 +00002350 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002352 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353
Daniel Dunbaref668c12009-08-14 18:19:52 +00002354 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002355 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357
Rafael Espindola64e1af82013-07-02 15:49:13 +00002358 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Sean Callanan686ed8d2010-01-19 20:22:31 +00002362 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
2364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 break;
2366
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002368 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002369 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 }
2371 }
2372
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 return false;
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002379bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002382
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002384 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002385 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002386 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 return true;
2388
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002389 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002390 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2391 assert(Size <= 8 && "Invalid size");
2392 uint64_t IntValue = MCE->getValue();
2393 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2394 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002395 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002396 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002397 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002399 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002401
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002403 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002405 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 }
2407 }
2408
Sean Callanan686ed8d2010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 return false;
2411}
2412
David Woodhoused6de0d92014-02-01 16:20:59 +00002413/// ParseDirectiveOctaValue
2414/// ::= .octa [ hexconstant (, hexconstant)* ]
2415bool AsmParser::parseDirectiveOctaValue() {
2416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2417 checkForValidSection();
2418
2419 for (;;) {
2420 if (Lexer.getKind() == AsmToken::Error)
2421 return true;
2422 if (Lexer.getKind() != AsmToken::Integer &&
2423 Lexer.getKind() != AsmToken::BigNum)
2424 return TokError("unknown token in expression");
2425
2426 SMLoc ExprLoc = getLexer().getLoc();
2427 APInt IntValue = getTok().getAPIntVal();
2428 Lex();
2429
2430 uint64_t hi, lo;
2431 if (IntValue.isIntN(64)) {
2432 hi = 0;
2433 lo = IntValue.getZExtValue();
2434 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002435 // It might actually have more than 128 bits, but the top ones are zero.
2436 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002437 lo = IntValue.getLoBits(64).getZExtValue();
2438 } else
2439 return Error(ExprLoc, "literal value out of range for directive");
2440
2441 if (MAI.isLittleEndian()) {
2442 getStreamer().EmitIntValue(lo, 8);
2443 getStreamer().EmitIntValue(hi, 8);
2444 } else {
2445 getStreamer().EmitIntValue(hi, 8);
2446 getStreamer().EmitIntValue(lo, 8);
2447 }
2448
2449 if (getLexer().is(AsmToken::EndOfStatement))
2450 break;
2451
2452 // FIXME: Improve diagnostic.
2453 if (getLexer().isNot(AsmToken::Comma))
2454 return TokError("unexpected token in directive");
2455 Lex();
2456 }
2457 }
2458
2459 Lex();
2460 return false;
2461}
2462
Jim Grosbach4b905842013-09-20 23:08:21 +00002463/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002465bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002467 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468
2469 for (;;) {
2470 // We don't truly support arithmetic on floating point expressions, so we
2471 // have to manually parse unary prefixes.
2472 bool IsNeg = false;
2473 if (getLexer().is(AsmToken::Minus)) {
2474 Lex();
2475 IsNeg = true;
2476 } else if (getLexer().is(AsmToken::Plus))
2477 Lex();
2478
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002480 getLexer().isNot(AsmToken::Real) &&
2481 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002482 return TokError("unexpected token in directive");
2483
2484 // Convert to an APFloat.
2485 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002486 StringRef IDVal = getTok().getString();
2487 if (getLexer().is(AsmToken::Identifier)) {
2488 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2489 Value = APFloat::getInf(Semantics);
2490 else if (!IDVal.compare_lower("nan"))
2491 Value = APFloat::getNaN(Semantics, false, ~0);
2492 else
2493 return TokError("invalid floating point literal");
2494 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002495 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002496 return TokError("invalid floating point literal");
2497 if (IsNeg)
2498 Value.changeSign();
2499
2500 // Consume the numeric token.
2501 Lex();
2502
2503 // Emit the value as an integer.
2504 APInt AsInt = Value.bitcastToAPInt();
2505 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002506 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002507
2508 if (getLexer().is(AsmToken::EndOfStatement))
2509 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002510
Daniel Dunbar2af16532010-09-24 01:59:56 +00002511 if (getLexer().isNot(AsmToken::Comma))
2512 return TokError("unexpected token in directive");
2513 Lex();
2514 }
2515 }
2516
2517 Lex();
2518 return false;
2519}
2520
Jim Grosbach4b905842013-09-20 23:08:21 +00002521/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002522/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002523bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002525
2526 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002528 return true;
2529
Rafael Espindolab91bac62010-10-05 19:42:57 +00002530 int64_t Val = 0;
2531 if (getLexer().is(AsmToken::Comma)) {
2532 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002534 return true;
2535 }
2536
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
2538 return TokError("unexpected token in '.zero' directive");
2539
2540 Lex();
2541
Rafael Espindola64e1af82013-07-02 15:49:13 +00002542 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002543
2544 return false;
2545}
2546
Jim Grosbach4b905842013-09-20 23:08:21 +00002547/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002548/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002549bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002550 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002551
David Majnemer522d3db2014-02-01 07:19:38 +00002552 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 return true;
2556
David Majnemer522d3db2014-02-01 07:19:38 +00002557 if (NumValues < 0) {
2558 Warning(RepeatLoc,
2559 "'.fill' directive with negative repeat count has no effect");
2560 NumValues = 0;
2561 }
2562
Roman Divackye33098f2013-09-24 17:44:41 +00002563 int64_t FillSize = 1;
2564 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2568 if (getLexer().isNot(AsmToken::Comma))
2569 return TokError("unexpected token in '.fill' directive");
2570 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002571
David Majnemer522d3db2014-02-01 07:19:38 +00002572 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (parseAbsoluteExpression(FillSize))
2574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002575
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 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillExpr))
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 return TokError("unexpected token in '.fill' directive");
2587
2588 Lex();
2589 }
2590 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002591
David Majnemer522d3db2014-02-01 07:19:38 +00002592 if (FillSize < 0) {
2593 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2594 NumValues = 0;
2595 }
2596 if (FillSize > 8) {
2597 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2598 FillSize = 8;
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (!isUInt<32>(FillExpr) && FillSize > 4)
2602 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2603
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002604 if (NumValues > 0) {
2605 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2606 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2607 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2608 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2609 if (NonZeroFillSize < FillSize)
2610 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2611 }
David Majnemer522d3db2014-02-01 07:19:38 +00002612 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002613
2614 return false;
2615}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002616
Jim Grosbach4b905842013-09-20 23:08:21 +00002617/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002618/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002619bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002620 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002621
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002622 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002623 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002624 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002625 return true;
2626
2627 // Parse optional fill expression.
2628 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2630 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002631 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002632 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002633
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002634 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002635 return true;
2636
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002638 return TokError("unexpected token in '.org' directive");
2639 }
2640
Sean Callanan686ed8d2010-01-19 20:22:31 +00002641 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002642
Jim Grosbachb5912772012-01-27 00:37:08 +00002643 // Only limited forms of relocatable expressions are accepted here, it
2644 // has to be relative to the current section. The streamer will return
2645 // 'true' if the expression wasn't evaluatable.
2646 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2647 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002648
2649 return false;
2650}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002651
Jim Grosbach4b905842013-09-20 23:08:21 +00002652/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002654bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002655 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002656
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002657 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002659 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 return true;
2661
2662 SMLoc MaxBytesLoc;
2663 bool HasFillExpr = false;
2664 int64_t FillExpr = 0;
2665 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002666 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2667 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002669 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002670
2671 // The fill expression can be omitted while specifying a maximum number of
2672 // alignment bytes, e.g:
2673 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002674 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002676 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 return true;
2678 }
2679
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002680 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2681 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002683 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002685 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002686 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002687 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002688
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002689 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002690 return TokError("unexpected token in directive");
2691 }
2692 }
2693
Sean Callanan686ed8d2010-01-19 20:22:31 +00002694 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002696 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698
2699 // Compute alignment in bytes.
2700 if (IsPow2) {
2701 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002702 if (Alignment >= 32) {
2703 Error(AlignmentLoc, "invalid alignment value");
2704 Alignment = 31;
2705 }
2706
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002707 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002708 } else {
2709 // Reject alignments that aren't a power of two, for gas compatibility.
2710 if (!isPowerOf2_64(Alignment))
2711 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002712 }
2713
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002714 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002715 if (MaxBytesLoc.isValid()) {
2716 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002717 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002718 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002719 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 }
2721
2722 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002723 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002724 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002725 MaxBytesToFill = 0;
2726 }
2727 }
2728
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002729 // Check whether we should use optimal code alignment for this .align
2730 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002731 const MCSection *Section = getStreamer().getCurrentSection().first;
2732 assert(Section && "must have section to emit alignment");
2733 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002734 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2735 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002736 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002737 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002738 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002739 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2740 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002741 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002742
2743 return false;
2744}
2745
Jim Grosbach4b905842013-09-20 23:08:21 +00002746/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002747/// ::= .file [number] filename
2748/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002749bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002750 // FIXME: I'm not sure what this is.
2751 int64_t FileNumber = -1;
2752 SMLoc FileNumberLoc = getLexer().getLoc();
2753 if (getLexer().is(AsmToken::Integer)) {
2754 FileNumber = getTok().getIntVal();
2755 Lex();
2756
2757 if (FileNumber < 1)
2758 return TokError("file number less than one");
2759 }
2760
2761 if (getLexer().isNot(AsmToken::String))
2762 return TokError("unexpected token in '.file' directive");
2763
2764 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002765 // Allow the strings to have escaped octal character sequence.
2766 std::string Path = getTok().getString();
2767 if (parseEscapedString(Path))
2768 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002769 Lex();
2770
2771 StringRef Directory;
2772 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002773 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002774 if (getLexer().is(AsmToken::String)) {
2775 if (FileNumber == -1)
2776 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002777 if (parseEscapedString(FilenameData))
2778 return true;
2779 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002780 Directory = Path;
2781 Lex();
2782 } else {
2783 Filename = Path;
2784 }
2785
2786 if (getLexer().isNot(AsmToken::EndOfStatement))
2787 return TokError("unexpected token in '.file' directive");
2788
2789 if (FileNumber == -1)
2790 getStreamer().EmitFileDirective(Filename);
2791 else {
2792 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002793 Error(DirectiveLoc,
2794 "input can't have .file dwarf directives when -g is "
2795 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002796
David Blaikiec714ef42014-03-17 01:52:11 +00002797 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2798 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002799 Error(FileNumberLoc, "file number already allocated");
2800 }
2801
2802 return false;
2803}
2804
Jim Grosbach4b905842013-09-20 23:08:21 +00002805/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002806/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002807bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002808 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2809 if (getLexer().isNot(AsmToken::Integer))
2810 return TokError("unexpected token in '.line' directive");
2811
2812 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002813 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002814 Lex();
2815
2816 // FIXME: Do something with the .line.
2817 }
2818
2819 if (getLexer().isNot(AsmToken::EndOfStatement))
2820 return TokError("unexpected token in '.line' directive");
2821
2822 return false;
2823}
2824
Jim Grosbach4b905842013-09-20 23:08:21 +00002825/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002826/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2827/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2828/// The first number is a file number, must have been previously assigned with
2829/// a .file directive, the second number is the line number and optionally the
2830/// third number is a column position (zero if not specified). The remaining
2831/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002832bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002833 if (getLexer().isNot(AsmToken::Integer))
2834 return TokError("unexpected token in '.loc' directive");
2835 int64_t FileNumber = getTok().getIntVal();
2836 if (FileNumber < 1)
2837 return TokError("file number less than one in '.loc' directive");
2838 if (!getContext().isValidDwarfFileNumber(FileNumber))
2839 return TokError("unassigned file number in '.loc' directive");
2840 Lex();
2841
2842 int64_t LineNumber = 0;
2843 if (getLexer().is(AsmToken::Integer)) {
2844 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002845 if (LineNumber < 0)
2846 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002847 Lex();
2848 }
2849
2850 int64_t ColumnPos = 0;
2851 if (getLexer().is(AsmToken::Integer)) {
2852 ColumnPos = getTok().getIntVal();
2853 if (ColumnPos < 0)
2854 return TokError("column position less than zero in '.loc' directive");
2855 Lex();
2856 }
2857
2858 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2859 unsigned Isa = 0;
2860 int64_t Discriminator = 0;
2861 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2862 for (;;) {
2863 if (getLexer().is(AsmToken::EndOfStatement))
2864 break;
2865
2866 StringRef Name;
2867 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002868 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002869 return TokError("unexpected token in '.loc' directive");
2870
2871 if (Name == "basic_block")
2872 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2873 else if (Name == "prologue_end")
2874 Flags |= DWARF2_FLAG_PROLOGUE_END;
2875 else if (Name == "epilogue_begin")
2876 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2877 else if (Name == "is_stmt") {
2878 Loc = getTok().getLoc();
2879 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002880 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002881 return true;
2882 // The expression must be the constant 0 or 1.
2883 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2884 int Value = MCE->getValue();
2885 if (Value == 0)
2886 Flags &= ~DWARF2_FLAG_IS_STMT;
2887 else if (Value == 1)
2888 Flags |= DWARF2_FLAG_IS_STMT;
2889 else
2890 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002891 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002892 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2893 }
Craig Topperf15655b2013-04-22 04:22:40 +00002894 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002895 Loc = getTok().getLoc();
2896 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002897 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002898 return true;
2899 // The expression must be a constant greater or equal to 0.
2900 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2901 int Value = MCE->getValue();
2902 if (Value < 0)
2903 return Error(Loc, "isa number less than zero");
2904 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002905 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return Error(Loc, "isa number not a constant value");
2907 }
Craig Topperf15655b2013-04-22 04:22:40 +00002908 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002909 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002910 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002911 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return Error(Loc, "unknown sub-directive in '.loc' directive");
2913 }
2914
2915 if (getLexer().is(AsmToken::EndOfStatement))
2916 break;
2917 }
2918 }
2919
2920 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2921 Isa, Discriminator, StringRef());
2922
2923 return false;
2924}
2925
Jim Grosbach4b905842013-09-20 23:08:21 +00002926/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002927/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002928bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002929 return TokError("unsupported directive '.stabs'");
2930}
2931
Jim Grosbach4b905842013-09-20 23:08:21 +00002932/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002933/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002934bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002935 StringRef Name;
2936 bool EH = false;
2937 bool Debug = false;
2938
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002939 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002940 return TokError("Expected an identifier");
2941
2942 if (Name == ".eh_frame")
2943 EH = true;
2944 else if (Name == ".debug_frame")
2945 Debug = true;
2946
2947 if (getLexer().is(AsmToken::Comma)) {
2948 Lex();
2949
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002950 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002951 return TokError("Expected an identifier");
2952
2953 if (Name == ".eh_frame")
2954 EH = true;
2955 else if (Name == ".debug_frame")
2956 Debug = true;
2957 }
2958
2959 getStreamer().EmitCFISections(EH, Debug);
2960 return false;
2961}
2962
Jim Grosbach4b905842013-09-20 23:08:21 +00002963/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002964/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002965bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002966 StringRef Simple;
2967 if (getLexer().isNot(AsmToken::EndOfStatement))
2968 if (parseIdentifier(Simple) || Simple != "simple")
2969 return TokError("unexpected token in .cfi_startproc directive");
2970
Oliver Stannard652ec6e2014-11-03 12:02:51 +00002971 getStreamer().EmitCFIStartProc(!Simple.empty(), LastFuncSymbol);
Eli Bendersky17233942013-01-15 22:59:42 +00002972 return false;
2973}
2974
Jim Grosbach4b905842013-09-20 23:08:21 +00002975/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002976/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002977bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002978 getStreamer().EmitCFIEndProc();
2979 return false;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// \brief parse register name or number.
2983bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002984 SMLoc DirectiveLoc) {
2985 unsigned RegNo;
2986
2987 if (getLexer().isNot(AsmToken::Integer)) {
2988 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2989 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002990 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002991 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002992 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002993
2994 return false;
2995}
2996
Jim Grosbach4b905842013-09-20 23:08:21 +00002997/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002998/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002999bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003000 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003001 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003002 return true;
3003
3004 if (getLexer().isNot(AsmToken::Comma))
3005 return TokError("unexpected token in directive");
3006 Lex();
3007
3008 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003009 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003010 return true;
3011
3012 getStreamer().EmitCFIDefCfa(Register, Offset);
3013 return false;
3014}
3015
Jim Grosbach4b905842013-09-20 23:08:21 +00003016/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003017/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003018bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003019 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003020 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003021 return true;
3022
3023 getStreamer().EmitCFIDefCfaOffset(Offset);
3024 return false;
3025}
3026
Jim Grosbach4b905842013-09-20 23:08:21 +00003027/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003028/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003029bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003030 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003031 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003032 return true;
3033
3034 if (getLexer().isNot(AsmToken::Comma))
3035 return TokError("unexpected token in directive");
3036 Lex();
3037
3038 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003039 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 getStreamer().EmitCFIRegister(Register1, Register2);
3043 return false;
3044}
3045
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003046/// parseDirectiveCFIWindowSave
3047/// ::= .cfi_window_save
3048bool AsmParser::parseDirectiveCFIWindowSave() {
3049 getStreamer().EmitCFIWindowSave();
3050 return false;
3051}
3052
Jim Grosbach4b905842013-09-20 23:08:21 +00003053/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003054/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003055bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003056 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003057 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003058 return true;
3059
3060 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3061 return false;
3062}
3063
Jim Grosbach4b905842013-09-20 23:08:21 +00003064/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003065/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003066bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003067 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003068 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003069 return true;
3070
3071 getStreamer().EmitCFIDefCfaRegister(Register);
3072 return false;
3073}
3074
Jim Grosbach4b905842013-09-20 23:08:21 +00003075/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003076/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003077bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003078 int64_t Register = 0;
3079 int64_t Offset = 0;
3080
Jim Grosbach4b905842013-09-20 23:08:21 +00003081 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003082 return true;
3083
3084 if (getLexer().isNot(AsmToken::Comma))
3085 return TokError("unexpected token in directive");
3086 Lex();
3087
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003088 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003089 return true;
3090
3091 getStreamer().EmitCFIOffset(Register, Offset);
3092 return false;
3093}
3094
Jim Grosbach4b905842013-09-20 23:08:21 +00003095/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003096/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003097bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003098 int64_t Register = 0;
3099
Jim Grosbach4b905842013-09-20 23:08:21 +00003100 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003101 return true;
3102
3103 if (getLexer().isNot(AsmToken::Comma))
3104 return TokError("unexpected token in directive");
3105 Lex();
3106
3107 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003108 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 getStreamer().EmitCFIRelOffset(Register, Offset);
3112 return false;
3113}
3114
3115static bool isValidEncoding(int64_t Encoding) {
3116 if (Encoding & ~0xff)
3117 return false;
3118
3119 if (Encoding == dwarf::DW_EH_PE_omit)
3120 return true;
3121
3122 const unsigned Format = Encoding & 0xf;
3123 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3124 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3125 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3126 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3127 return false;
3128
3129 const unsigned Application = Encoding & 0x70;
3130 if (Application != dwarf::DW_EH_PE_absptr &&
3131 Application != dwarf::DW_EH_PE_pcrel)
3132 return false;
3133
3134 return true;
3135}
3136
Jim Grosbach4b905842013-09-20 23:08:21 +00003137/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003138/// IsPersonality true for cfi_personality, false for cfi_lsda
3139/// ::= .cfi_personality encoding, [symbol_name]
3140/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003141bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003142 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003143 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003144 return true;
3145 if (Encoding == dwarf::DW_EH_PE_omit)
3146 return false;
3147
3148 if (!isValidEncoding(Encoding))
3149 return TokError("unsupported encoding.");
3150
3151 if (getLexer().isNot(AsmToken::Comma))
3152 return TokError("unexpected token in directive");
3153 Lex();
3154
3155 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003156 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003157 return TokError("expected identifier in directive");
3158
3159 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3160
3161 if (IsPersonality)
3162 getStreamer().EmitCFIPersonality(Sym, Encoding);
3163 else
3164 getStreamer().EmitCFILsda(Sym, Encoding);
3165 return false;
3166}
3167
Jim Grosbach4b905842013-09-20 23:08:21 +00003168/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003169/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003170bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003171 getStreamer().EmitCFIRememberState();
3172 return false;
3173}
3174
Jim Grosbach4b905842013-09-20 23:08:21 +00003175/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003176/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003177bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003178 getStreamer().EmitCFIRestoreState();
3179 return false;
3180}
3181
Jim Grosbach4b905842013-09-20 23:08:21 +00003182/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003183/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003184bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003185 int64_t Register = 0;
3186
Jim Grosbach4b905842013-09-20 23:08:21 +00003187 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003188 return true;
3189
3190 getStreamer().EmitCFISameValue(Register);
3191 return false;
3192}
3193
Jim Grosbach4b905842013-09-20 23:08:21 +00003194/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003195/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003196bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003197 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003198 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003199 return true;
3200
3201 getStreamer().EmitCFIRestore(Register);
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003207bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003208 std::string Values;
3209 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003210 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003211 return true;
3212
3213 Values.push_back((uint8_t)CurrValue);
3214
3215 while (getLexer().is(AsmToken::Comma)) {
3216 Lex();
3217
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003218 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003219 return true;
3220
3221 Values.push_back((uint8_t)CurrValue);
3222 }
3223
3224 getStreamer().EmitCFIEscape(Values);
3225 return false;
3226}
3227
Jim Grosbach4b905842013-09-20 23:08:21 +00003228/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003229/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003230bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003231 if (getLexer().isNot(AsmToken::EndOfStatement))
3232 return Error(getLexer().getLoc(),
3233 "unexpected token in '.cfi_signal_frame'");
3234
3235 getStreamer().EmitCFISignalFrame();
3236 return false;
3237}
3238
Jim Grosbach4b905842013-09-20 23:08:21 +00003239/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003240/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003241bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003242 int64_t Register = 0;
3243
Jim Grosbach4b905842013-09-20 23:08:21 +00003244 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003245 return true;
3246
3247 getStreamer().EmitCFIUndefined(Register);
3248 return false;
3249}
3250
Jim Grosbach4b905842013-09-20 23:08:21 +00003251/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003252/// ::= .macros_on
3253/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003254bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003255 if (getLexer().isNot(AsmToken::EndOfStatement))
3256 return Error(getLexer().getLoc(),
3257 "unexpected token in '" + Directive + "' directive");
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003260 return false;
3261}
3262
Jim Grosbach4b905842013-09-20 23:08:21 +00003263/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003264/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003265bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003266 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003267 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003268 return TokError("expected identifier in '.macro' directive");
3269
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003270 if (getLexer().is(AsmToken::Comma))
3271 Lex();
3272
Eli Bendersky17233942013-01-15 22:59:42 +00003273 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003274 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003275
3276 if (Parameters.size() && Parameters.back().Vararg)
3277 return Error(Lexer.getLoc(),
3278 "Vararg parameter '" + Parameters.back().Name +
3279 "' should be last one in the list of parameters.");
3280
David Majnemer91fc4c22014-01-29 18:57:46 +00003281 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003282 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003283 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003284
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003285 if (Lexer.is(AsmToken::Colon)) {
3286 Lex(); // consume ':'
3287
3288 SMLoc QualLoc;
3289 StringRef Qualifier;
3290
3291 QualLoc = Lexer.getLoc();
3292 if (parseIdentifier(Qualifier))
3293 return Error(QualLoc, "missing parameter qualifier for "
3294 "'" + Parameter.Name + "' in macro '" + Name + "'");
3295
3296 if (Qualifier == "req")
3297 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003298 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003299 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003300 else
3301 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3302 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3303 }
3304
David Majnemer91fc4c22014-01-29 18:57:46 +00003305 if (getLexer().is(AsmToken::Equal)) {
3306 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003307
3308 SMLoc ParamLoc;
3309
3310 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003311 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003312 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003313
3314 if (Parameter.Required)
3315 Warning(ParamLoc, "pointless default value for required parameter "
3316 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003317 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003318
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003319 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003320
3321 if (getLexer().is(AsmToken::Comma))
3322 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003323 }
3324
3325 // Eat the end of statement.
3326 Lex();
3327
3328 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003329 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003330
3331 // Lex the macro definition.
3332 for (;;) {
3333 // Check whether we have reached the end of the file.
3334 if (getLexer().is(AsmToken::Eof))
3335 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3336
3337 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003338 if (getLexer().is(AsmToken::Identifier)) {
3339 if (getTok().getIdentifier() == ".endm" ||
3340 getTok().getIdentifier() == ".endmacro") {
3341 if (MacroDepth == 0) { // Outermost macro.
3342 EndToken = getTok();
3343 Lex();
3344 if (getLexer().isNot(AsmToken::EndOfStatement))
3345 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3346 "' directive");
3347 break;
3348 } else {
3349 // Otherwise we just found the end of an inner macro.
3350 --MacroDepth;
3351 }
3352 } else if (getTok().getIdentifier() == ".macro") {
3353 // We allow nested macros. Those aren't instantiated until the outermost
3354 // macro is expanded so just ignore them for now.
3355 ++MacroDepth;
3356 }
Eli Bendersky17233942013-01-15 22:59:42 +00003357 }
3358
3359 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003360 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003361 }
3362
Jim Grosbach4b905842013-09-20 23:08:21 +00003363 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003364 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3365 }
3366
3367 const char *BodyStart = StartToken.getLoc().getPointer();
3368 const char *BodyEnd = EndToken.getLoc().getPointer();
3369 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003370 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003371 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003372 return false;
3373}
3374
Jim Grosbach4b905842013-09-20 23:08:21 +00003375/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003376///
3377/// With the support added for named parameters there may be code out there that
3378/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003379/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003380/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003381/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003382/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3383/// warning that the positional parameter found in body which have no effect.
3384/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003385/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003386/// intended or change the macro to use the named parameters. It is possible
3387/// this warning will trigger when the none of the named parameters are used
3388/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003389void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003391 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003392 // If this macro is not defined with named parameters the warning we are
3393 // checking for here doesn't apply.
3394 unsigned NParameters = Parameters.size();
3395 if (NParameters == 0)
3396 return;
3397
3398 bool NamedParametersFound = false;
3399 bool PositionalParametersFound = false;
3400
3401 // Look at the body of the macro for use of both the named parameters and what
3402 // are likely to be positional parameters. This is what expandMacro() is
3403 // doing when it finds the parameters in the body.
3404 while (!Body.empty()) {
3405 // Scan for the next possible parameter.
3406 std::size_t End = Body.size(), Pos = 0;
3407 for (; Pos != End; ++Pos) {
3408 // Check for a substitution or escape.
3409 // This macro is defined with parameters, look for \foo, \bar, etc.
3410 if (Body[Pos] == '\\' && Pos + 1 != End)
3411 break;
3412
3413 // This macro should have parameters, but look for $0, $1, ..., $n too.
3414 if (Body[Pos] != '$' || Pos + 1 == End)
3415 continue;
3416 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003417 if (Next == '$' || Next == 'n' ||
3418 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003419 break;
3420 }
3421
3422 // Check if we reached the end.
3423 if (Pos == End)
3424 break;
3425
3426 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003427 switch (Body[Pos + 1]) {
3428 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003429 case '$':
3430 break;
3431
Jim Grosbach4b905842013-09-20 23:08:21 +00003432 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003433 case 'n':
3434 PositionalParametersFound = true;
3435 break;
3436
Jim Grosbach4b905842013-09-20 23:08:21 +00003437 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 default: {
3439 PositionalParametersFound = true;
3440 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003441 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003442 }
3443 Pos += 2;
3444 } else {
3445 unsigned I = Pos + 1;
3446 while (isIdentifierChar(Body[I]) && I + 1 != End)
3447 ++I;
3448
Jim Grosbach4b905842013-09-20 23:08:21 +00003449 const char *Begin = Body.data() + Pos + 1;
3450 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003451 unsigned Index = 0;
3452 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003453 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003454 break;
3455
3456 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3458 Pos += 3;
3459 else {
3460 Pos = I;
3461 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003462 } else {
3463 NamedParametersFound = true;
3464 Pos += 1 + Argument.size();
3465 }
3466 }
3467 // Update the scan point.
3468 Body = Body.substr(Pos);
3469 }
3470
3471 if (!NamedParametersFound && PositionalParametersFound)
3472 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3473 "used in macro body, possible positional parameter "
3474 "found in body which will have no effect");
3475}
3476
Nico Weber155dccd12014-07-24 17:08:39 +00003477/// parseDirectiveExitMacro
3478/// ::= .exitm
3479bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3480 if (getLexer().isNot(AsmToken::EndOfStatement))
3481 return TokError("unexpected token in '" + Directive + "' directive");
3482
3483 if (!isInsideMacroInstantiation())
3484 return TokError("unexpected '" + Directive + "' in file, "
3485 "no current macro definition");
3486
3487 // Exit all conditionals that are active in the current macro.
3488 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3489 TheCondState = TheCondStack.back();
3490 TheCondStack.pop_back();
3491 }
3492
3493 handleMacroExit();
3494 return false;
3495}
3496
Jim Grosbach4b905842013-09-20 23:08:21 +00003497/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003498/// ::= .endm
3499/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003500bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003501 if (getLexer().isNot(AsmToken::EndOfStatement))
3502 return TokError("unexpected token in '" + Directive + "' directive");
3503
3504 // If we are inside a macro instantiation, terminate the current
3505 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003506 if (isInsideMacroInstantiation()) {
3507 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003508 return false;
3509 }
3510
3511 // Otherwise, this .endmacro is a stray entry in the file; well formed
3512 // .endmacro directives are handled during the macro definition parsing.
3513 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003515}
3516
Jim Grosbach4b905842013-09-20 23:08:21 +00003517/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003518/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003519bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003520 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003521 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003522 return TokError("expected identifier in '.purgem' directive");
3523
3524 if (getLexer().isNot(AsmToken::EndOfStatement))
3525 return TokError("unexpected token in '.purgem' directive");
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003528 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3529
Jim Grosbach4b905842013-09-20 23:08:21 +00003530 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003531 return false;
3532}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003535/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003536bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003537 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003538
3539 // Expect a single argument: an expression that evaluates to a constant
3540 // in the inclusive range 0-30.
3541 SMLoc ExprLoc = getLexer().getLoc();
3542 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003543 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003544 return true;
3545 else if (getLexer().isNot(AsmToken::EndOfStatement))
3546 return TokError("unexpected token after expression in"
3547 " '.bundle_align_mode' directive");
3548 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3549 return Error(ExprLoc,
3550 "invalid bundle alignment size (expected between 0 and 30)");
3551
3552 Lex();
3553
3554 // Because of AlignSizePow2's verified range we can safely truncate it to
3555 // unsigned.
3556 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3557 return false;
3558}
3559
Jim Grosbach4b905842013-09-20 23:08:21 +00003560/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003561/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003562bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003563 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003564 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003565
Eli Bendersky802b6282013-01-07 21:51:08 +00003566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3567 StringRef Option;
3568 SMLoc Loc = getTok().getLoc();
3569 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003570 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003571
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003572 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003573 return Error(Loc, kInvalidOptionError);
3574
3575 if (Option != "align_to_end")
3576 return Error(Loc, kInvalidOptionError);
3577 else if (getLexer().isNot(AsmToken::EndOfStatement))
3578 return Error(Loc,
3579 "unexpected token after '.bundle_lock' directive option");
3580 AlignToEnd = true;
3581 }
3582
Eli Benderskyf483ff92012-12-20 19:05:53 +00003583 Lex();
3584
Eli Bendersky802b6282013-01-07 21:51:08 +00003585 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003586 return false;
3587}
3588
Jim Grosbach4b905842013-09-20 23:08:21 +00003589/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003590/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003591bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003592 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003593
3594 if (getLexer().isNot(AsmToken::EndOfStatement))
3595 return TokError("unexpected token in '.bundle_unlock' directive");
3596 Lex();
3597
3598 getStreamer().EmitBundleUnlock();
3599 return false;
3600}
3601
Jim Grosbach4b905842013-09-20 23:08:21 +00003602/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003603/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003604bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003605 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003606
3607 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003608 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003609 return true;
3610
3611 int64_t FillExpr = 0;
3612 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3613 if (getLexer().isNot(AsmToken::Comma))
3614 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3615 Lex();
3616
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003617 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003618 return true;
3619
3620 if (getLexer().isNot(AsmToken::EndOfStatement))
3621 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3622 }
3623
3624 Lex();
3625
3626 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003627 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3628 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003629
3630 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003631 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003632
3633 return false;
3634}
3635
Jim Grosbach4b905842013-09-20 23:08:21 +00003636/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003637/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003638bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003639 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003640 const MCExpr *Value;
3641
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003642 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003643 return true;
3644
3645 if (getLexer().isNot(AsmToken::EndOfStatement))
3646 return TokError("unexpected token in directive");
3647
3648 if (Signed)
3649 getStreamer().EmitSLEB128Value(Value);
3650 else
3651 getStreamer().EmitULEB128Value(Value);
3652
3653 return false;
3654}
3655
Jim Grosbach4b905842013-09-20 23:08:21 +00003656/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003657/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003658bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003659 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003660 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003661 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003662 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003663
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003664 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003665 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003666
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003667 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003668
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003669 // Assembler local symbols don't make any sense here. Complain loudly.
3670 if (Sym->isTemporary())
3671 return Error(Loc, "non-local symbol required in directive");
3672
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003673 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3674 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003675
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003676 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003677 break;
3678
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003679 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003680 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003681 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003682 }
3683 }
3684
Sean Callanan686ed8d2010-01-19 20:22:31 +00003685 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003686 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003687}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003688
Jim Grosbach4b905842013-09-20 23:08:21 +00003689/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003690/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003691bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003692 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003693
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003694 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003695 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003696 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003697 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003698
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003699 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003700 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003701
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003702 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003703 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003704 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003705
3706 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003707 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003708 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003709 return true;
3710
3711 int64_t Pow2Alignment = 0;
3712 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003713 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003714 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003715 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003716 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003717 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003718
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003719 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3720 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003721 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3722
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003723 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003724 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3725 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003726 if (!isPowerOf2_64(Pow2Alignment))
3727 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3728 Pow2Alignment = Log2_64(Pow2Alignment);
3729 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003730 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003731
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003732 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003733 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003734
Sean Callanan686ed8d2010-01-19 20:22:31 +00003735 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736
Chris Lattner28ad7542009-07-09 17:25:12 +00003737 // NOTE: a size of zero for a .comm should create a undefined symbol
3738 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003739 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003740 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003741 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742
Eric Christopherbc818852010-05-14 01:38:54 +00003743 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003744 // may internally end up wanting an alignment in bytes.
3745 // FIXME: Diagnose overflow.
3746 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003747 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003748 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003749
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003750 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003751 return Error(IDLoc, "invalid symbol redefinition");
3752
Chris Lattner28ad7542009-07-09 17:25:12 +00003753 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003754 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003755 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003756 return false;
3757 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003758
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003759 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003760 return false;
3761}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003762
Jim Grosbach4b905842013-09-20 23:08:21 +00003763/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003764/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003765bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003766 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003767 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003768
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003769 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003770 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003771 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003772
Sean Callanan686ed8d2010-01-19 20:22:31 +00003773 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003774
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003775 if (Str.empty())
3776 Error(Loc, ".abort detected. Assembly stopping.");
3777 else
3778 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003779 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003780
3781 return false;
3782}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003783
Jim Grosbach4b905842013-09-20 23:08:21 +00003784/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003785/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003786bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003787 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003788 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003789
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003790 // Allow the strings to have escaped octal character sequence.
3791 std::string Filename;
3792 if (parseEscapedString(Filename))
3793 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003794 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003795 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003796
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003797 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003798 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003799
Chris Lattner693fbb82009-07-16 06:14:39 +00003800 // Attempt to switch the lexer to the included file before consuming the end
3801 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003802 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003803 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003804 return true;
3805 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003806
3807 return false;
3808}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003809
Jim Grosbach4b905842013-09-20 23:08:21 +00003810/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003811/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003812bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003813 if (getLexer().isNot(AsmToken::String))
3814 return TokError("expected string in '.incbin' directive");
3815
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003816 // Allow the strings to have escaped octal character sequence.
3817 std::string Filename;
3818 if (parseEscapedString(Filename))
3819 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003820 SMLoc IncbinLoc = getLexer().getLoc();
3821 Lex();
3822
3823 if (getLexer().isNot(AsmToken::EndOfStatement))
3824 return TokError("unexpected token in '.incbin' directive");
3825
Kevin Enderby109f25c2011-12-14 21:47:48 +00003826 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003827 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003828 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3829 return true;
3830 }
3831
3832 return false;
3833}
3834
Jim Grosbach4b905842013-09-20 23:08:21 +00003835/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003836/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3837bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003838 TheCondStack.push_back(TheCondState);
3839 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003840 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003841 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003842 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003843 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003844 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003845 return true;
3846
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003847 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003848 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003849
Sean Callanan686ed8d2010-01-19 20:22:31 +00003850 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003851
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003852 switch (DirKind) {
3853 default:
3854 llvm_unreachable("unsupported directive");
3855 case DK_IF:
3856 case DK_IFNE:
3857 break;
3858 case DK_IFEQ:
3859 ExprValue = ExprValue == 0;
3860 break;
3861 case DK_IFGE:
3862 ExprValue = ExprValue >= 0;
3863 break;
3864 case DK_IFGT:
3865 ExprValue = ExprValue > 0;
3866 break;
3867 case DK_IFLE:
3868 ExprValue = ExprValue <= 0;
3869 break;
3870 case DK_IFLT:
3871 ExprValue = ExprValue < 0;
3872 break;
3873 }
3874
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003875 TheCondState.CondMet = ExprValue;
3876 TheCondState.Ignore = !TheCondState.CondMet;
3877 }
3878
3879 return false;
3880}
3881
Jim Grosbach4b905842013-09-20 23:08:21 +00003882/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003883/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003884bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003885 TheCondStack.push_back(TheCondState);
3886 TheCondState.TheCond = AsmCond::IfCond;
3887
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003888 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003889 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003890 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003891 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003892
3893 if (getLexer().isNot(AsmToken::EndOfStatement))
3894 return TokError("unexpected token in '.ifb' directive");
3895
3896 Lex();
3897
3898 TheCondState.CondMet = ExpectBlank == Str.empty();
3899 TheCondState.Ignore = !TheCondState.CondMet;
3900 }
3901
3902 return false;
3903}
3904
Jim Grosbach4b905842013-09-20 23:08:21 +00003905/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003906/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003907/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003908bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003909 TheCondStack.push_back(TheCondState);
3910 TheCondState.TheCond = AsmCond::IfCond;
3911
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003912 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003913 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003914 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003915 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003916
3917 if (getLexer().isNot(AsmToken::Comma))
3918 return TokError("unexpected token in '.ifc' directive");
3919
3920 Lex();
3921
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003922 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003923
3924 if (getLexer().isNot(AsmToken::EndOfStatement))
3925 return TokError("unexpected token in '.ifc' directive");
3926
3927 Lex();
3928
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003929 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003930 TheCondState.Ignore = !TheCondState.CondMet;
3931 }
3932
3933 return false;
3934}
3935
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003936/// parseDirectiveIfeqs
3937/// ::= .ifeqs string1, string2
3938bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3939 if (Lexer.isNot(AsmToken::String)) {
3940 TokError("expected string parameter for '.ifeqs' directive");
3941 eatToEndOfStatement();
3942 return true;
3943 }
3944
3945 StringRef String1 = getTok().getStringContents();
3946 Lex();
3947
3948 if (Lexer.isNot(AsmToken::Comma)) {
3949 TokError("expected comma after first string for '.ifeqs' directive");
3950 eatToEndOfStatement();
3951 return true;
3952 }
3953
3954 Lex();
3955
3956 if (Lexer.isNot(AsmToken::String)) {
3957 TokError("expected string parameter for '.ifeqs' directive");
3958 eatToEndOfStatement();
3959 return true;
3960 }
3961
3962 StringRef String2 = getTok().getStringContents();
3963 Lex();
3964
3965 TheCondStack.push_back(TheCondState);
3966 TheCondState.TheCond = AsmCond::IfCond;
3967 TheCondState.CondMet = String1 == String2;
3968 TheCondState.Ignore = !TheCondState.CondMet;
3969
3970 return false;
3971}
3972
Jim Grosbach4b905842013-09-20 23:08:21 +00003973/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003974/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003975bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003976 StringRef Name;
3977 TheCondStack.push_back(TheCondState);
3978 TheCondState.TheCond = AsmCond::IfCond;
3979
3980 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003981 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003982 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003983 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003984 return TokError("expected identifier after '.ifdef'");
3985
3986 Lex();
3987
3988 MCSymbol *Sym = getContext().LookupSymbol(Name);
3989
3990 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003991 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003992 else
Craig Topper353eda42014-04-24 06:44:33 +00003993 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003994 TheCondState.Ignore = !TheCondState.CondMet;
3995 }
3996
3997 return false;
3998}
3999
Jim Grosbach4b905842013-09-20 23:08:21 +00004000/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004001/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004002bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004003 if (TheCondState.TheCond != AsmCond::IfCond &&
4004 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004005 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4006 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004007 TheCondState.TheCond = AsmCond::ElseIfCond;
4008
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004009 bool LastIgnoreState = false;
4010 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004011 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004012 if (LastIgnoreState || TheCondState.CondMet) {
4013 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004014 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004015 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004017 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004018 return true;
4019
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004020 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004021 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004022
Sean Callanan686ed8d2010-01-19 20:22:31 +00004023 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004024 TheCondState.CondMet = ExprValue;
4025 TheCondState.Ignore = !TheCondState.CondMet;
4026 }
4027
4028 return false;
4029}
4030
Jim Grosbach4b905842013-09-20 23:08:21 +00004031/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004032/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004033bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004034 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004035 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004036
Sean Callanan686ed8d2010-01-19 20:22:31 +00004037 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004038
4039 if (TheCondState.TheCond != AsmCond::IfCond &&
4040 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004041 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4042 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004043 TheCondState.TheCond = AsmCond::ElseCond;
4044 bool LastIgnoreState = false;
4045 if (!TheCondStack.empty())
4046 LastIgnoreState = TheCondStack.back().Ignore;
4047 if (LastIgnoreState || TheCondState.CondMet)
4048 TheCondState.Ignore = true;
4049 else
4050 TheCondState.Ignore = false;
4051
4052 return false;
4053}
4054
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004055/// parseDirectiveEnd
4056/// ::= .end
4057bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4058 if (getLexer().isNot(AsmToken::EndOfStatement))
4059 return TokError("unexpected token in '.end' directive");
4060
4061 Lex();
4062
4063 while (Lexer.isNot(AsmToken::Eof))
4064 Lex();
4065
4066 return false;
4067}
4068
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004069/// parseDirectiveError
4070/// ::= .err
4071/// ::= .error [string]
4072bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4073 if (!TheCondStack.empty()) {
4074 if (TheCondStack.back().Ignore) {
4075 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004076 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004077 }
4078 }
4079
4080 if (!WithMessage)
4081 return Error(L, ".err encountered");
4082
4083 StringRef Message = ".error directive invoked in source file";
4084 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4085 if (Lexer.isNot(AsmToken::String)) {
4086 TokError(".error argument must be a string");
4087 eatToEndOfStatement();
4088 return true;
4089 }
4090
4091 Message = getTok().getStringContents();
4092 Lex();
4093 }
4094
4095 Error(L, Message);
4096 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004097}
4098
Nico Weber404012b2014-07-24 16:26:06 +00004099/// parseDirectiveWarning
4100/// ::= .warning [string]
4101bool AsmParser::parseDirectiveWarning(SMLoc L) {
4102 if (!TheCondStack.empty()) {
4103 if (TheCondStack.back().Ignore) {
4104 eatToEndOfStatement();
4105 return false;
4106 }
4107 }
4108
4109 StringRef Message = ".warning directive invoked in source file";
4110 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4111 if (Lexer.isNot(AsmToken::String)) {
4112 TokError(".warning argument must be a string");
4113 eatToEndOfStatement();
4114 return true;
4115 }
4116
4117 Message = getTok().getStringContents();
4118 Lex();
4119 }
4120
4121 Warning(L, Message);
4122 return false;
4123}
4124
Jim Grosbach4b905842013-09-20 23:08:21 +00004125/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004126/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004127bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004128 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004129 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004130
Sean Callanan686ed8d2010-01-19 20:22:31 +00004131 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004132
Jim Grosbach4b905842013-09-20 23:08:21 +00004133 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004134 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4135 ".else");
4136 if (!TheCondStack.empty()) {
4137 TheCondState = TheCondStack.back();
4138 TheCondStack.pop_back();
4139 }
4140
4141 return false;
4142}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004143
Eli Bendersky17233942013-01-15 22:59:42 +00004144void AsmParser::initializeDirectiveKindMap() {
4145 DirectiveKindMap[".set"] = DK_SET;
4146 DirectiveKindMap[".equ"] = DK_EQU;
4147 DirectiveKindMap[".equiv"] = DK_EQUIV;
4148 DirectiveKindMap[".ascii"] = DK_ASCII;
4149 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4150 DirectiveKindMap[".string"] = DK_STRING;
4151 DirectiveKindMap[".byte"] = DK_BYTE;
4152 DirectiveKindMap[".short"] = DK_SHORT;
4153 DirectiveKindMap[".value"] = DK_VALUE;
4154 DirectiveKindMap[".2byte"] = DK_2BYTE;
4155 DirectiveKindMap[".long"] = DK_LONG;
4156 DirectiveKindMap[".int"] = DK_INT;
4157 DirectiveKindMap[".4byte"] = DK_4BYTE;
4158 DirectiveKindMap[".quad"] = DK_QUAD;
4159 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004160 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004161 DirectiveKindMap[".single"] = DK_SINGLE;
4162 DirectiveKindMap[".float"] = DK_FLOAT;
4163 DirectiveKindMap[".double"] = DK_DOUBLE;
4164 DirectiveKindMap[".align"] = DK_ALIGN;
4165 DirectiveKindMap[".align32"] = DK_ALIGN32;
4166 DirectiveKindMap[".balign"] = DK_BALIGN;
4167 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4168 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4169 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4170 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4171 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4172 DirectiveKindMap[".org"] = DK_ORG;
4173 DirectiveKindMap[".fill"] = DK_FILL;
4174 DirectiveKindMap[".zero"] = DK_ZERO;
4175 DirectiveKindMap[".extern"] = DK_EXTERN;
4176 DirectiveKindMap[".globl"] = DK_GLOBL;
4177 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004178 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4179 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4180 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4181 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4182 DirectiveKindMap[".reference"] = DK_REFERENCE;
4183 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4184 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4185 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4186 DirectiveKindMap[".comm"] = DK_COMM;
4187 DirectiveKindMap[".common"] = DK_COMMON;
4188 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4189 DirectiveKindMap[".abort"] = DK_ABORT;
4190 DirectiveKindMap[".include"] = DK_INCLUDE;
4191 DirectiveKindMap[".incbin"] = DK_INCBIN;
4192 DirectiveKindMap[".code16"] = DK_CODE16;
4193 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4194 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004195 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004196 DirectiveKindMap[".irp"] = DK_IRP;
4197 DirectiveKindMap[".irpc"] = DK_IRPC;
4198 DirectiveKindMap[".endr"] = DK_ENDR;
4199 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4200 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4201 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4202 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004203 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4204 DirectiveKindMap[".ifge"] = DK_IFGE;
4205 DirectiveKindMap[".ifgt"] = DK_IFGT;
4206 DirectiveKindMap[".ifle"] = DK_IFLE;
4207 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004208 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004209 DirectiveKindMap[".ifb"] = DK_IFB;
4210 DirectiveKindMap[".ifnb"] = DK_IFNB;
4211 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004212 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004213 DirectiveKindMap[".ifnc"] = DK_IFNC;
4214 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4215 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4216 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4217 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4218 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004219 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004220 DirectiveKindMap[".endif"] = DK_ENDIF;
4221 DirectiveKindMap[".skip"] = DK_SKIP;
4222 DirectiveKindMap[".space"] = DK_SPACE;
4223 DirectiveKindMap[".file"] = DK_FILE;
4224 DirectiveKindMap[".line"] = DK_LINE;
4225 DirectiveKindMap[".loc"] = DK_LOC;
4226 DirectiveKindMap[".stabs"] = DK_STABS;
4227 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4228 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4229 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4230 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4231 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4232 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4233 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4234 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4235 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4236 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4237 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4238 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4239 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4240 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4241 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4242 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4243 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4244 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4245 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4246 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4247 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004248 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004249 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4250 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4251 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004252 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004253 DirectiveKindMap[".endm"] = DK_ENDM;
4254 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4255 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004256 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004257 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004258 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004259}
4260
Jim Grosbach4b905842013-09-20 23:08:21 +00004261MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004262 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004263
Rafael Espindola34b9c512012-06-03 23:57:14 +00004264 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004265 for (;;) {
4266 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004267 if (getLexer().is(AsmToken::Eof)) {
4268 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004269 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004270 }
4271
Rafael Espindola34b9c512012-06-03 23:57:14 +00004272 if (Lexer.is(AsmToken::Identifier) &&
4273 (getTok().getIdentifier() == ".rept")) {
4274 ++NestLevel;
4275 }
4276
4277 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004278 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004279 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004280 EndToken = getTok();
4281 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004282 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4283 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004284 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004285 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004286 break;
4287 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004288 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004289 }
4290
Rafael Espindola34b9c512012-06-03 23:57:14 +00004291 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004292 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004293 }
4294
4295 const char *BodyStart = StartToken.getLoc().getPointer();
4296 const char *BodyEnd = EndToken.getLoc().getPointer();
4297 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4298
Rafael Espindola34b9c512012-06-03 23:57:14 +00004299 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004300 MacroLikeBodies.push_back(
4301 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004302 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004303}
4304
Jim Grosbach4b905842013-09-20 23:08:21 +00004305void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004306 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004307 OS << ".endr\n";
4308
Rafael Espindola3560ff22014-08-27 20:03:13 +00004309 std::unique_ptr<MemoryBuffer> Instantiation =
4310 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004311
Rafael Espindola34b9c512012-06-03 23:57:14 +00004312 // Create the macro instantiation object and add to the current macro
4313 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004314 MacroInstantiation *MI = new MacroInstantiation(
4315 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004316 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004317
Rafael Espindola34b9c512012-06-03 23:57:14 +00004318 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004319 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004320 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004321 Lex();
4322}
4323
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004324/// parseDirectiveRept
4325/// ::= .rep | .rept count
4326bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004327 const MCExpr *CountExpr;
4328 SMLoc CountLoc = getTok().getLoc();
4329 if (parseExpression(CountExpr))
4330 return true;
4331
Rafael Espindola34b9c512012-06-03 23:57:14 +00004332 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004333 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4334 eatToEndOfStatement();
4335 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4336 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004337
4338 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004339 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004340
4341 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004342 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004343
4344 // Eat the end of statement.
4345 Lex();
4346
4347 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004348 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004349 if (!M)
4350 return true;
4351
4352 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4353 // to hold the macro body with substitutions.
4354 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004355 raw_svector_ostream OS(Buf);
4356 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004357 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004358 return true;
4359 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004360 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004361
4362 return false;
4363}
4364
Jim Grosbach4b905842013-09-20 23:08:21 +00004365/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004366/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004367bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004368 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004369
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004370 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004371 return TokError("expected identifier in '.irp' directive");
4372
Rafael Espindola768b41c2012-06-15 14:02:34 +00004373 if (Lexer.isNot(AsmToken::Comma))
4374 return TokError("expected comma in '.irp' directive");
4375
4376 Lex();
4377
Eli Bendersky38274122013-01-14 23:22:36 +00004378 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004379 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004380 return true;
4381
4382 // Eat the end of statement.
4383 Lex();
4384
4385 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004386 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004387 if (!M)
4388 return true;
4389
4390 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4391 // to hold the macro body with substitutions.
4392 SmallString<256> Buf;
4393 raw_svector_ostream OS(Buf);
4394
Eli Bendersky38274122013-01-14 23:22:36 +00004395 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004396 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004397 return true;
4398 }
4399
Jim Grosbach4b905842013-09-20 23:08:21 +00004400 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004401
4402 return false;
4403}
4404
Jim Grosbach4b905842013-09-20 23:08:21 +00004405/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004406/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004407bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004408 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004409
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004410 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004411 return TokError("expected identifier in '.irpc' directive");
4412
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004413 if (Lexer.isNot(AsmToken::Comma))
4414 return TokError("expected comma in '.irpc' directive");
4415
4416 Lex();
4417
Eli Bendersky38274122013-01-14 23:22:36 +00004418 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004419 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004420 return true;
4421
4422 if (A.size() != 1 || A.front().size() != 1)
4423 return TokError("unexpected token in '.irpc' directive");
4424
4425 // Eat the end of statement.
4426 Lex();
4427
4428 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004429 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004430 if (!M)
4431 return true;
4432
4433 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4434 // to hold the macro body with substitutions.
4435 SmallString<256> Buf;
4436 raw_svector_ostream OS(Buf);
4437
4438 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004439 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004440 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004441 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004442
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004443 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004444 return true;
4445 }
4446
Jim Grosbach4b905842013-09-20 23:08:21 +00004447 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004448
4449 return false;
4450}
4451
Jim Grosbach4b905842013-09-20 23:08:21 +00004452bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004453 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004454 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004455
4456 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004457 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004458 assert(getLexer().is(AsmToken::EndOfStatement));
4459
Jim Grosbach4b905842013-09-20 23:08:21 +00004460 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004461 return false;
4462}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004463
Jim Grosbach4b905842013-09-20 23:08:21 +00004464bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004465 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004466 const MCExpr *Value;
4467 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004468 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004469 return true;
4470 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4471 if (!MCE)
4472 return Error(ExprLoc, "unexpected expression in _emit");
4473 uint64_t IntValue = MCE->getValue();
4474 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4475 return Error(ExprLoc, "literal value out of range for directive");
4476
Chad Rosierc7f552c2013-02-12 21:33:51 +00004477 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4478 return false;
4479}
4480
Jim Grosbach4b905842013-09-20 23:08:21 +00004481bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004482 const MCExpr *Value;
4483 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004484 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004485 return true;
4486 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4487 if (!MCE)
4488 return Error(ExprLoc, "unexpected expression in align");
4489 uint64_t IntValue = MCE->getValue();
4490 if (!isPowerOf2_64(IntValue))
4491 return Error(ExprLoc, "literal value not a power of two greater then zero");
4492
Jim Grosbach4b905842013-09-20 23:08:21 +00004493 Info.AsmRewrites->push_back(
4494 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004495 return false;
4496}
4497
Chad Rosierf43fcf52013-02-13 21:27:17 +00004498// We are comparing pointers, but the pointers are relative to a single string.
4499// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004500static int rewritesSort(const AsmRewrite *AsmRewriteA,
4501 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004502 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4503 return -1;
4504 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4505 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004506
Chad Rosierfce4fab2013-04-08 17:43:47 +00004507 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4508 // rewrite to the same location. Make sure the SizeDirective rewrite is
4509 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4510 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004511 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4512 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004513 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004514
Jim Grosbach4b905842013-09-20 23:08:21 +00004515 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4516 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004517 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004518 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004519}
4520
Jim Grosbach4b905842013-09-20 23:08:21 +00004521bool AsmParser::parseMSInlineAsm(
4522 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4523 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4524 SmallVectorImpl<std::string> &Constraints,
4525 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4526 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004527 SmallVector<void *, 4> InputDecls;
4528 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004529 SmallVector<bool, 4> InputDeclsAddressOf;
4530 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004531 SmallVector<std::string, 4> InputConstraints;
4532 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004533 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004534
Benjamin Kramer1a136112013-02-15 20:37:21 +00004535 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004536
4537 // Prime the lexer.
4538 Lex();
4539
4540 // While we have input, parse each statement.
4541 unsigned InputIdx = 0;
4542 unsigned OutputIdx = 0;
4543 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004544 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004545 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004546 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004547
Chad Rosier149e8e02012-12-12 22:45:52 +00004548 if (Info.ParseError)
4549 return true;
4550
Benjamin Kramer1a136112013-02-15 20:37:21 +00004551 if (Info.Opcode == ~0U)
4552 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004553
Benjamin Kramer1a136112013-02-15 20:37:21 +00004554 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004555
Benjamin Kramer1a136112013-02-15 20:37:21 +00004556 // Build the list of clobbers, outputs and inputs.
4557 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004558 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004559
Benjamin Kramer1a136112013-02-15 20:37:21 +00004560 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004561 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004563
Benjamin Kramer1a136112013-02-15 20:37:21 +00004564 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004565 if (Operand.isReg() && !Operand.needAddressOf() &&
4566 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004567 unsigned NumDefs = Desc.getNumDefs();
4568 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004569 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4570 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004571 continue;
4572 }
4573
4574 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004575 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004576 if (SymName.empty())
4577 continue;
4578
David Blaikie960ea3f2014-06-08 16:18:35 +00004579 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 if (!OpDecl)
4581 continue;
4582
4583 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004584 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004585 if (isOutput) {
4586 ++InputIdx;
4587 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004588 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4589 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004590 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004591 } else {
4592 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004593 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4594 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004595 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004596 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004597 }
Reid Kleckneree088972013-12-10 18:27:32 +00004598
4599 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004600 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4601 Desc.getNumImplicitDefs());
4602 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004603 }
4604
4605 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004606 NumOutputs = OutputDecls.size();
4607 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004608
4609 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004610 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4611 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4612 ClobberRegs.end());
4613 Clobbers.assign(ClobberRegs.size(), std::string());
4614 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4615 raw_string_ostream OS(Clobbers[I]);
4616 IP->printRegName(OS, ClobberRegs[I]);
4617 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004618
4619 // Merge the various outputs and inputs. Output are expected first.
4620 if (NumOutputs || NumInputs) {
4621 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004622 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004623 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004624 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004625 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004626 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004627 }
4628 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004629 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004630 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004631 }
4632 }
4633
4634 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004635 std::string AsmStringIR;
4636 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004637 StringRef ASMString =
4638 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4639 const char *AsmStart = ASMString.begin();
4640 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004641 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004642 for (const AsmRewrite &AR : AsmStrRewrites) {
4643 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004644 if (Kind == AOK_Delete)
4645 continue;
4646
David Majnemer8114c1a2014-06-23 02:17:16 +00004647 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004648 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004649
Chad Rosier120eefd2013-03-19 17:32:17 +00004650 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004651 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004652 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004653
Chad Rosier37e755c2012-10-23 17:43:43 +00004654 // Skip the original expression.
4655 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004656 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004657 continue;
4658 }
4659
Chad Rosierff10ed12013-04-12 16:26:42 +00004660 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004661 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004662 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004663 default:
4664 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004665 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004666 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004667 break;
4668 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004669 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004670 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004671 case AOK_Label:
4672 OS << Ctx.getAsmInfo()->getPrivateGlobalPrefix() << AR.Label;
4673 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004674 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004675 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004676 break;
4677 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004678 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004679 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004680 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004681 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004682 default: break;
4683 case 8: OS << "byte ptr "; break;
4684 case 16: OS << "word ptr "; break;
4685 case 32: OS << "dword ptr "; break;
4686 case 64: OS << "qword ptr "; break;
4687 case 80: OS << "xword ptr "; break;
4688 case 128: OS << "xmmword ptr "; break;
4689 case 256: OS << "ymmword ptr "; break;
4690 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004691 break;
4692 case AOK_Emit:
4693 OS << ".byte";
4694 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004695 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004696 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004697 OS << ".align " << Val;
4698
4699 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004700 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004701 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4702 break;
4703 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004704 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004705 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004706 OS.flush();
4707 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004708 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004709 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004710 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004711 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004712
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004714 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004715 }
4716
4717 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004718 if (AsmStart != AsmEnd)
4719 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004720
4721 AsmString = OS.str();
4722 return false;
4723}
4724
Daniel Dunbar01e36072010-07-17 02:26:10 +00004725/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004726MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4727 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004728 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004729}