blob: 70877c5914eaeb5c22b207b34f717dc2b6c099f1 [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
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000175public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000176 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000177 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000178 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179
Craig Topper59be68f2014-03-08 07:14:16 +0000180 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181
Craig Topper59be68f2014-03-08 07:14:16 +0000182 void addDirectiveHandler(StringRef Directive,
183 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000184 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
Craig Topper59be68f2014-03-08 07:14:16 +0000191 SourceMgr &getSourceManager() override { return SrcMgr; }
192 MCAsmLexer &getLexer() override { return Lexer; }
193 MCContext &getContext() override { return Ctx; }
194 MCStreamer &getStreamer() override { return Out; }
195 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
Craig Topper59be68f2014-03-08 07:14:16 +0000201 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000202 AssemblerDialect = i;
203 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 void Note(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = None) override;
207 bool Warning(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = None) override;
209 bool Error(SMLoc L, const Twine &Msg,
210 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
215 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000216
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000217 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000218 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000219 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000220 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000222 const MCInstrInfo *MII, const MCInstPrinter *IP,
223 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000226 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
227 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
228 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
229 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000230
Jim Grosbach4b905842013-09-20 23:08:21 +0000231 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000232 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseIdentifier(StringRef &Res) override;
234 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000235
Craig Topper59be68f2014-03-08 07:14:16 +0000236 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000240
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000241 bool parseStatement(ParseStatementInfo &Info,
242 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000243 void eatToEndOfLine();
244 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000247 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000248 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000249 ArrayRef<MCAsmMacroParameter> Parameters,
250 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000251 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000252
Eli Benderskya313ae62013-01-16 18:56:50 +0000253 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000255
256 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000258
259 /// \brief Lookup a previously defined macro.
260 /// \param Name Macro name.
261 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000265 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000273 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000274 ///
275 /// \param M The macro.
276 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
David Majnemer91fc4c22014-01-29 18:57:46 +0000282 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000283 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void printMacroInstantiations();
289 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000290 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000291 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000292 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000293 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 /// \brief Enter the specified file. This returns true on failure.
296 bool enterIncludeFile(const std::string &Filename);
297
298 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000299 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000301
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000302 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000303 /// current token is not set; clients should ensure Lex() is called
304 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000305 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000306 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000307 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000308 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000309
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000310 /// \brief Parse up to the end of statement and a return the contents from the
311 /// current token until the end of the statement; the current token on exit
312 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000313 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000315 /// \brief Parse until the end of a statement or a comma is encountered,
316 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000317 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000318
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000320 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000321
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
323 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
324 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000327
Eli Bendersky17233942013-01-15 22:59:42 +0000328 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000329 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000330 DK_NO_DIRECTIVE, // Placeholder
331 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000332 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
333 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000334 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000335 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000336 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
338 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
339 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
340 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000341 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
342 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
343 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000344 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
345 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
346 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
347 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
348 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
349 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000350 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000351 DK_MACROS_ON, DK_MACROS_OFF,
352 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000353 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000354 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000355 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000356 };
357
Jim Grosbach4b905842013-09-20 23:08:21 +0000358 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000359 /// directives parsed by this class.
360 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361
362 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
364 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000365 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
367 bool parseDirectiveFill(); // ".fill"
368 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000369 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
371 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000372 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000374
Eli Bendersky17233942013-01-15 22:59:42 +0000375 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveFile(SMLoc DirectiveLoc);
377 bool parseDirectiveLine();
378 bool parseDirectiveLoc();
379 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000380
381 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000383 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveCFISections();
385 bool parseDirectiveCFIStartProc();
386 bool parseDirectiveCFIEndProc();
387 bool parseDirectiveCFIDefCfaOffset();
388 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
389 bool parseDirectiveCFIAdjustCfaOffset();
390 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
394 bool parseDirectiveCFIRememberState();
395 bool parseDirectiveCFIRestoreState();
396 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIEscape();
399 bool parseDirectiveCFISignalFrame();
400 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000401
402 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000403 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000404 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000405 bool parseDirectiveEndMacro(StringRef Directive);
406 bool parseDirectiveMacro(SMLoc DirectiveLoc);
407 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
Eli Benderskyf483ff92012-12-20 19:05:53 +0000409 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000411 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415
Eli Bendersky17233942013-01-15 22:59:42 +0000416 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
419 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000423 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveAbort(); // ".abort"
429 bool parseDirectiveInclude(); // ".include"
430 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000432 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
433 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000434 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000436 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000438 // ".ifeqs"
439 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000445 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000466 // "end"
467 bool parseDirectiveEnd(SMLoc DirectiveLoc);
468
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000469 // ".err" or ".error"
470 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000471
Nico Weber404012b2014-07-24 16:26:06 +0000472 // ".warning"
473 bool parseDirectiveWarning(SMLoc DirectiveLoc);
474
Eli Bendersky17233942013-01-15 22:59:42 +0000475 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000476};
Daniel Dunbar86033402010-07-12 17:54:38 +0000477}
478
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000479namespace llvm {
480
481extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000482extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000483extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484
485}
486
Chris Lattnerc35681b2010-01-19 19:46:13 +0000487enum { DEFAULT_ADDRSPACE = 0 };
488
Jim Grosbach4b905842013-09-20 23:08:21 +0000489AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
490 const MCAsmInfo &_MAI)
491 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000492 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
493 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000494 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000495 // Save the old handler.
496 SavedDiagHandler = SrcMgr.getDiagHandler();
497 SavedDiagContext = SrcMgr.getDiagContext();
498 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000499 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000500 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000501
Daniel Dunbarc5011082010-07-12 18:12:02 +0000502 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000503 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
504 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000505 PlatformParser.reset(createCOFFAsmParser());
506 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000507 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000508 PlatformParser.reset(createDarwinAsmParser());
509 IsDarwin = true;
510 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000511 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000512 PlatformParser.reset(createELFAsmParser());
513 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000514 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000515
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000516 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000517 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000518}
519
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000520AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000521 assert((HadError || ActiveMacros.empty()) &&
522 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000523}
524
Jim Grosbach4b905842013-09-20 23:08:21 +0000525void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000526 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000527 for (std::vector<MacroInstantiation *>::const_reverse_iterator
528 it = ActiveMacros.rbegin(),
529 ie = ActiveMacros.rend();
530 it != ie; ++it)
531 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000532 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000533}
534
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000535void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
536 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
537 printMacroInstantiations();
538}
539
Chris Lattnera3a06812011-10-16 04:47:35 +0000540bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000541 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000542 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000543 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
544 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000545 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000546}
547
Chris Lattnera3a06812011-10-16 04:47:35 +0000548bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000549 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000550 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
551 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000552 return true;
553}
554
Jim Grosbach4b905842013-09-20 23:08:21 +0000555bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000556 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000557 unsigned NewBuf =
558 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
559 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000560 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000561
Sean Callanan7a77eae2010-01-21 00:19:58 +0000562 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000563 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 return false;
565}
Daniel Dunbar43235712010-07-18 18:54:11 +0000566
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000567/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000568/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000569/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000570bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000571 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000572 unsigned NewBuf =
573 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
574 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 return true;
576
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000577 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000578 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579 return false;
580}
581
Alp Tokera55b95b2014-07-06 10:33:31 +0000582void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
583 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000584 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
585 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000586}
587
Sean Callanan7a77eae2010-01-21 00:19:58 +0000588const AsmToken &AsmParser::Lex() {
589 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000590
Sean Callanan7a77eae2010-01-21 00:19:58 +0000591 if (tok->is(AsmToken::Eof)) {
592 // If this is the end of an included file, pop the parent file off the
593 // include stack.
594 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
595 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000596 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597 tok = &Lexer.Lex();
598 }
599 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000600
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000602 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000603
Sean Callanan7a77eae2010-01-21 00:19:58 +0000604 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000605}
606
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000607bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000608 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000609 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000610 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000611
Chris Lattner36e02122009-06-21 20:54:55 +0000612 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000613 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000614
615 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000616 AsmCond StartingCondState = TheCondState;
617
Kevin Enderby6469fc22011-11-01 22:27:22 +0000618 // If we are generating dwarf for assembly source files save the initial text
619 // section and generate a .file directive.
620 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000621 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
622 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000623 auto InsertResult = getContext().addGenDwarfSection(
624 getStreamer().getCurrentSection().first);
625 assert(InsertResult.second && ".text section should not have debug info yet");
626 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000627 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
628 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000629 }
630
Chris Lattner73f36112009-07-02 21:53:43 +0000631 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000632 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000633 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000634 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000635 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000636
Daniel Dunbar43325c42010-09-09 22:42:56 +0000637 // We had an error, validate that one was emitted and recover by skipping to
638 // the next line.
639 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000640 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000641 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000642
643 if (TheCondState.TheCond != StartingCondState.TheCond ||
644 TheCondState.Ignore != StartingCondState.Ignore)
645 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000646
647 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000648 const auto &LineTables = getContext().getMCDwarfLineTables();
649 if (!LineTables.empty()) {
650 unsigned Index = 0;
651 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
652 if (File.Name.empty() && Index != 0)
653 TokError("unassigned file number: " + Twine(Index) +
654 " for .file directives");
655 ++Index;
656 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000657 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000658
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000659 // Check to see that all assembler local symbols were actually defined.
660 // Targets that don't do subsections via symbols may not want this, though,
661 // so conservatively exclude them. Only do this if we're finalizing, though,
662 // as otherwise we won't necessarilly have seen everything yet.
663 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
664 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
665 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000666 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000667 i != e; ++i) {
668 MCSymbol *Sym = i->getValue();
669 // Variable symbols may not be marked as defined, so check those
670 // explicitly. If we know it's a variable, we have a definition for
671 // the purposes of this check.
672 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
673 // FIXME: We would really like to refer back to where the symbol was
674 // first referenced for a source location. We need to add something
675 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000676 printMessage(
677 getLexer().getLoc(), SourceMgr::DK_Error,
678 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 }
680 }
681
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000682 // Finalize the output stream if there are no errors and if the client wants
683 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000684 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000685 Out.Finish();
686
Chris Lattner73f36112009-07-02 21:53:43 +0000687 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000688}
689
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000690void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000691 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000692 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000693 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000694 }
695}
696
Jim Grosbach4b905842013-09-20 23:08:21 +0000697/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000698void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000699 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000700 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000701
Chris Lattnere5074c42009-06-22 01:29:09 +0000702 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000703 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000704 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000705}
706
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000708 const char *Start = getTok().getLoc().getPointer();
709
Jim Grosbach4b905842013-09-20 23:08:21 +0000710 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000711 Lex();
712
713 const char *End = getTok().getLoc().getPointer();
714 return StringRef(Start, End - Start);
715}
Chris Lattner78db3622009-06-22 05:51:26 +0000716
Jim Grosbach4b905842013-09-20 23:08:21 +0000717StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000718 const char *Start = getTok().getLoc().getPointer();
719
720 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000721 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000722 Lex();
723
724 const char *End = getTok().getLoc().getPointer();
725 return StringRef(Start, End - Start);
726}
727
Jim Grosbach4b905842013-09-20 23:08:21 +0000728/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000729/// NOTE: This assumes the leading '(' has already been consumed.
730///
731/// parenexpr ::= expr)
732///
Jim Grosbach4b905842013-09-20 23:08:21 +0000733bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
734 if (parseExpression(Res))
735 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000736 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000737 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000738 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000739 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000740 return false;
741}
Chris Lattner78db3622009-06-22 05:51:26 +0000742
Jim Grosbach4b905842013-09-20 23:08:21 +0000743/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000744/// NOTE: This assumes the leading '[' has already been consumed.
745///
746/// bracketexpr ::= expr]
747///
Jim Grosbach4b905842013-09-20 23:08:21 +0000748bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
749 if (parseExpression(Res))
750 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000751 if (Lexer.isNot(AsmToken::RBrac))
752 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000753 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754 Lex();
755 return false;
756}
757
Jim Grosbach4b905842013-09-20 23:08:21 +0000758/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000759/// primaryexpr ::= (parenexpr
760/// primaryexpr ::= symbol
761/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000762/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000763/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000764bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000765 SMLoc FirstTokenLoc = getLexer().getLoc();
766 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
767 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000768 default:
769 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000770 // If we have an error assume that we've already handled it.
771 case AsmToken::Error:
772 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000773 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000774 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000775 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000776 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000777 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000778 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000779 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000780 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000781 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000782 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000783 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000784 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000785 if (FirstTokenKind == AsmToken::Dollar) {
786 if (Lexer.getMAI().getDollarIsPC()) {
787 // This is a '$' reference, which references the current PC. Emit a
788 // temporary label to the streamer and refer to it.
789 MCSymbol *Sym = Ctx.CreateTempSymbol();
790 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000791 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
792 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000793 EndLoc = FirstTokenLoc;
794 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000795 }
796 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000798 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000799 // Parse symbol variant
800 std::pair<StringRef, StringRef> Split;
801 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000802 if (FirstTokenKind == AsmToken::String) {
803 if (Lexer.is(AsmToken::At)) {
804 Lexer.Lex(); // eat @
805 SMLoc AtLoc = getLexer().getLoc();
806 StringRef VName;
807 if (parseIdentifier(VName))
808 return Error(AtLoc, "expected symbol variant after '@'");
809
810 Split = std::make_pair(Identifier, VName);
811 }
812 } else {
813 Split = Identifier.split('@');
814 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000815 } else if (Lexer.is(AsmToken::LParen)) {
816 Lexer.Lex(); // eat (
817 StringRef VName;
818 parseIdentifier(VName);
819 if (Lexer.isNot(AsmToken::RParen)) {
820 return Error(Lexer.getTok().getLoc(),
821 "unexpected token in variant, expected ')'");
822 }
823 Lexer.Lex(); // eat )
824 Split = std::make_pair(Identifier, VName);
825 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000826
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000827 EndLoc = SMLoc::getFromPointer(Identifier.end());
828
Daniel Dunbard20cda02009-10-16 01:34:54 +0000829 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 StringRef SymbolName = Identifier;
831 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000832
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000833 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000834 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000835 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 if (Variant != MCSymbolRefExpr::VK_Invalid) {
837 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 Variant = MCSymbolRefExpr::VK_None;
840 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000841 return Error(SMLoc::getFromPointer(Split.second.begin()),
842 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000843 }
844 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000845
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
847
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 // If this is an absolute variable reference, substitute it now to preserve
849 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000850 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000851 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000852 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000853
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000855 return false;
856 }
857
858 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000859 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000860 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 }
David Woodhousef42a6662014-02-01 16:20:54 +0000862 case AsmToken::BigNum:
863 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000864 case AsmToken::Integer: {
865 SMLoc Loc = getTok().getLoc();
866 int64_t IntVal = getTok().getIntVal();
867 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000868 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000869 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 // Look for 'b' or 'f' following an Integer as a directional label
871 if (Lexer.getKind() == AsmToken::Identifier) {
872 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000873 // Lookup the symbol variant if used.
874 std::pair<StringRef, StringRef> Split = IDVal.split('@');
875 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
876 if (Split.first.size() != IDVal.size()) {
877 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000878 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000880 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000881 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000882 if (IDVal == "f" || IDVal == "b") {
883 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000884 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000886 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000888 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000889 Lex(); // Eat identifier.
890 }
891 }
Chris Lattner78db3622009-06-22 05:51:26 +0000892 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 case AsmToken::Real: {
895 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000896 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000898 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000899 Lex(); // Eat token.
900 return false;
901 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000902 case AsmToken::Dot: {
903 // This is a '.' reference, which references the current PC. Emit a
904 // temporary label to the streamer and refer to it.
905 MCSymbol *Sym = Ctx.CreateTempSymbol();
906 Out.EmitLabel(Sym);
907 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000909 Lex(); // Eat identifier.
910 return false;
911 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000912 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000913 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000914 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000915 case AsmToken::LBrac:
916 if (!PlatformParser->HasBracketExpressions())
917 return TokError("brackets expression not supported on this target");
918 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000920 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000921 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000922 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000923 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000924 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000925 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000926 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000927 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000930 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000931 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000936 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000938 }
939}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000940
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000941bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000942 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000943 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000944}
945
Daniel Dunbar55f16672010-09-17 02:47:07 +0000946const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000947AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000949 // Ask the target implementation about this expression first.
950 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
951 if (NewE)
952 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000953 // Recurse over the given expression, rebuilding it to apply the given variant
954 // if there is exactly one symbol.
955 switch (E->getKind()) {
956 case MCExpr::Target:
957 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000958 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959
960 case MCExpr::SymbolRef: {
961 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
962
963 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000964 TokError("invalid variant on expression '" + getTok().getIdentifier() +
965 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 return E;
967 }
968
969 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
970 }
971
972 case MCExpr::Unary: {
973 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000976 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000977 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
978 }
979
980 case MCExpr::Binary: {
981 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000982 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
983 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984
985 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000986 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 if (!LHS)
989 LHS = BE->getLHS();
990 if (!RHS)
991 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992
993 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
994 }
995 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000996
Craig Toppera2886c22012-02-07 05:05:23 +0000997 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998}
999
Jim Grosbach4b905842013-09-20 23:08:21 +00001000/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001001///
Jim Grosbachbd164242011-08-20 16:24:13 +00001002/// expr ::= expr &&,|| expr -> lowest.
1003/// expr ::= expr |,^,&,! expr
1004/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1005/// expr ::= expr <<,>> expr
1006/// expr ::= expr +,- expr
1007/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001008/// expr ::= primaryexpr
1009///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001010bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001011 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001012 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001013 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001014 return true;
1015
Daniel Dunbar55f16672010-09-17 02:47:07 +00001016 // As a special case, we support 'a op b @ modifier' by rewriting the
1017 // expression to include the modifier. This is inefficient, but in general we
1018 // expect users to use 'a@modifier op b'.
1019 if (Lexer.getKind() == AsmToken::At) {
1020 Lex();
1021
1022 if (Lexer.isNot(AsmToken::Identifier))
1023 return TokError("unexpected symbol modifier following '@'");
1024
1025 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001026 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 if (Variant == MCSymbolRefExpr::VK_Invalid)
1028 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1029
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (!ModifiedRes) {
1032 return TokError("invalid modifier '" + getTok().getIdentifier() +
1033 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001035
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 Res = ModifiedRes;
1037 Lex();
1038 }
1039
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001040 // Try to constant fold it up front, if possible.
1041 int64_t Value;
1042 if (Res->EvaluateAsAbsolute(Value))
1043 Res = MCConstantExpr::Create(Value, getContext());
1044
1045 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001046}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001049 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001050 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001051}
1052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001053bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001054 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001055
Daniel Dunbar75630b32009-06-30 02:10:03 +00001056 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001058 return true;
1059
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001060 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001061 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062
1063 return false;
1064}
1065
Michael J. Spencer530ce852010-10-09 11:00:50 +00001066static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001067 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001068 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001069 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071
Jim Grosbach4b905842013-09-20 23:08:21 +00001072 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001073 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001075 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001076 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001078 return 1;
1079
Jim Grosbach4b905842013-09-20 23:08:21 +00001080 // Low Precedence: |, &, ^
1081 //
1082 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001092
Jim Grosbach4b905842013-09-20 23:08:21 +00001093 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 case AsmToken::EqualEqual:
1095 Kind = MCBinaryExpr::EQ;
1096 return 3;
1097 case AsmToken::ExclaimEqual:
1098 case AsmToken::LessGreater:
1099 Kind = MCBinaryExpr::NE;
1100 return 3;
1101 case AsmToken::Less:
1102 Kind = MCBinaryExpr::LT;
1103 return 3;
1104 case AsmToken::LessEqual:
1105 Kind = MCBinaryExpr::LTE;
1106 return 3;
1107 case AsmToken::Greater:
1108 Kind = MCBinaryExpr::GT;
1109 return 3;
1110 case AsmToken::GreaterEqual:
1111 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001112 return 3;
1113
Jim Grosbach4b905842013-09-20 23:08:21 +00001114 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001115 case AsmToken::LessLess:
1116 Kind = MCBinaryExpr::Shl;
1117 return 4;
1118 case AsmToken::GreaterGreater:
1119 Kind = MCBinaryExpr::Shr;
1120 return 4;
1121
Jim Grosbach4b905842013-09-20 23:08:21 +00001122 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001123 case AsmToken::Plus:
1124 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126 case AsmToken::Minus:
1127 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001129
Jim Grosbach4b905842013-09-20 23:08:21 +00001130 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001137 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001138 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001140 }
1141}
1142
Jim Grosbach4b905842013-09-20 23:08:21 +00001143/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001145bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001146 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001147 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001149 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 // If the next token is lower precedence than we are allowed to eat, return
1152 // successfully with what we ate already.
1153 if (TokPrec < Precedence)
1154 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Sean Callanan686ed8d2010-01-19 20:22:31 +00001156 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001159 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001160 if (parsePrimaryExpr(RHS, EndLoc))
1161 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // If BinOp binds less tightly with RHS than the operator after RHS, let
1164 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001165 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001166 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001167 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1168 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001171 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 }
1173}
1174
Chris Lattner36e02122009-06-21 20:54:55 +00001175/// ParseStatement:
1176/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001177/// ::= Label* Directive ...Operands... EndOfStatement
1178/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001179bool AsmParser::parseStatement(ParseStatementInfo &Info,
1180 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001181 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001182 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001183 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001184 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001185 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001187 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001188 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001189 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001193 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001194 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001197 if (Lexer.is(AsmToken::Integer)) {
1198 LocalLabelVal = getTok().getIntVal();
1199 if (LocalLabelVal < 0) {
1200 if (!TheCondState.Ignore)
1201 return TokError("unexpected token at start of statement");
1202 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001203 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 IDVal = getTok().getString();
1205 Lex(); // Consume the integer token to be used as an identifier token.
1206 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001207 if (!TheCondState.Ignore)
1208 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 }
1210 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001211 } else if (Lexer.is(AsmToken::Dot)) {
1212 // Treat '.' as a valid identifier in this context.
1213 Lex();
1214 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001215 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
1218 IDVal = "";
1219 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001220
Chris Lattner926885c2010-04-17 18:14:27 +00001221 // Handle conditional assembly here before checking for skipping. We
1222 // have to do this so that .endif isn't skipped in a ".if 0" block for
1223 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001224 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001225 DirectiveKindMap.find(IDVal);
1226 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1227 ? DK_NO_DIRECTIVE
1228 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001229 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 default:
1231 break;
1232 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001233 case DK_IFEQ:
1234 case DK_IFGE:
1235 case DK_IFGT:
1236 case DK_IFLE:
1237 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001238 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001239 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001240 case DK_IFB:
1241 return parseDirectiveIfb(IDLoc, true);
1242 case DK_IFNB:
1243 return parseDirectiveIfb(IDLoc, false);
1244 case DK_IFC:
1245 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001246 case DK_IFEQS:
1247 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001248 case DK_IFNC:
1249 return parseDirectiveIfc(IDLoc, false);
1250 case DK_IFDEF:
1251 return parseDirectiveIfdef(IDLoc, true);
1252 case DK_IFNDEF:
1253 case DK_IFNOTDEF:
1254 return parseDirectiveIfdef(IDLoc, false);
1255 case DK_ELSEIF:
1256 return parseDirectiveElseIf(IDLoc);
1257 case DK_ELSE:
1258 return parseDirectiveElse(IDLoc);
1259 case DK_ENDIF:
1260 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001261 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001262
Eli Bendersky88024712013-01-16 19:32:36 +00001263 // Ignore the statement if in the middle of inactive conditional
1264 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001265 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001266 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001267 return false;
1268 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001269
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001270 // FIXME: Recurse on local labels?
1271
1272 // See what kind of statement we have.
1273 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001274 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001275 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001276
Chris Lattner36e02122009-06-21 20:54:55 +00001277 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001278 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001279
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001280 // Diagnose attempt to use '.' as a label.
1281 if (IDVal == ".")
1282 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1283
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001284 // Diagnose attempt to use a variable as a label.
1285 //
1286 // FIXME: Diagnostics. Note the location of the definition as a label.
1287 // FIXME: This doesn't diagnose assignment to a symbol which has been
1288 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001289 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001290 if (LocalLabelVal == -1) {
1291 if (ParsingInlineAsm && SI) {
1292 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1293 assert(RewrittenLabel.size() && "We should have an internal name here.");
1294 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1295 IDVal.size(), RewrittenLabel));
1296 IDVal = RewrittenLabel;
1297 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001298 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001299 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001300 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001301
1302 Sym->redefineIfPossible();
1303
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001304 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001305 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001306
Daniel Dunbare73b2672009-08-26 22:13:22 +00001307 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001308 if (!ParsingInlineAsm)
1309 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310
Kevin Enderbye7739d42011-12-09 18:09:40 +00001311 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001312 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001313 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001314 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1315 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001316
Tim Northover1744d0a2013-10-25 12:49:50 +00001317 getTargetParser().onLabelParsed(Sym);
1318
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001319 // Consume any end of statement token, if present, to avoid spurious
1320 // AddBlankLine calls().
1321 if (Lexer.is(AsmToken::EndOfStatement)) {
1322 Lex();
1323 if (Lexer.is(AsmToken::Eof))
1324 return false;
1325 }
1326
Eli Friedman0f4871d2012-10-22 23:58:19 +00001327 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001328 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001329
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001330 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001331 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001332 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
Jim Grosbach4b905842013-09-20 23:08:21 +00001334 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335
1336 default: // Normal instruction or directive.
1337 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001338 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001339
1340 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001341 if (areMacrosEnabled())
1342 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1343 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001344 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001345
Michael J. Spencer530ce852010-10-09 11:00:50 +00001346 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001347
Eli Bendersky17233942013-01-15 22:59:42 +00001348 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001349 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001350 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001351 //
Eli Bendersky17233942013-01-15 22:59:42 +00001352 // 1. The target-specific assembly parser. Some directives are target
1353 // specific or may potentially behave differently on certain targets.
1354 // 2. Asm parser extensions. For example, platform-specific parsers
1355 // (like the ELF parser) register themselves as extensions.
1356 // 3. The generic directive parser implemented by this class. These are
1357 // all the directives that behave in a target and platform independent
1358 // manner, or at least have a default behavior that's shared between
1359 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001360
Eli Bendersky17233942013-01-15 22:59:42 +00001361 // First query the target-specific parser. It will return 'true' if it
1362 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001363 if (!getTargetParser().ParseDirective(ID))
1364 return false;
1365
Alp Tokercb402912014-01-24 17:20:08 +00001366 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001368 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1369 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001370 if (Handler.first)
1371 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1372
1373 // Finally, if no one else is interested in this directive, it must be
1374 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001375 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001376 default:
1377 break;
1378 case DK_SET:
1379 case DK_EQU:
1380 return parseDirectiveSet(IDVal, true);
1381 case DK_EQUIV:
1382 return parseDirectiveSet(IDVal, false);
1383 case DK_ASCII:
1384 return parseDirectiveAscii(IDVal, false);
1385 case DK_ASCIZ:
1386 case DK_STRING:
1387 return parseDirectiveAscii(IDVal, true);
1388 case DK_BYTE:
1389 return parseDirectiveValue(1);
1390 case DK_SHORT:
1391 case DK_VALUE:
1392 case DK_2BYTE:
1393 return parseDirectiveValue(2);
1394 case DK_LONG:
1395 case DK_INT:
1396 case DK_4BYTE:
1397 return parseDirectiveValue(4);
1398 case DK_QUAD:
1399 case DK_8BYTE:
1400 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001401 case DK_OCTA:
1402 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001403 case DK_SINGLE:
1404 case DK_FLOAT:
1405 return parseDirectiveRealValue(APFloat::IEEEsingle);
1406 case DK_DOUBLE:
1407 return parseDirectiveRealValue(APFloat::IEEEdouble);
1408 case DK_ALIGN: {
1409 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1410 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1411 }
1412 case DK_ALIGN32: {
1413 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1414 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1415 }
1416 case DK_BALIGN:
1417 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1418 case DK_BALIGNW:
1419 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1420 case DK_BALIGNL:
1421 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1422 case DK_P2ALIGN:
1423 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1424 case DK_P2ALIGNW:
1425 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1426 case DK_P2ALIGNL:
1427 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1428 case DK_ORG:
1429 return parseDirectiveOrg();
1430 case DK_FILL:
1431 return parseDirectiveFill();
1432 case DK_ZERO:
1433 return parseDirectiveZero();
1434 case DK_EXTERN:
1435 eatToEndOfStatement(); // .extern is the default, ignore it.
1436 return false;
1437 case DK_GLOBL:
1438 case DK_GLOBAL:
1439 return parseDirectiveSymbolAttribute(MCSA_Global);
1440 case DK_LAZY_REFERENCE:
1441 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1442 case DK_NO_DEAD_STRIP:
1443 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1444 case DK_SYMBOL_RESOLVER:
1445 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1446 case DK_PRIVATE_EXTERN:
1447 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1448 case DK_REFERENCE:
1449 return parseDirectiveSymbolAttribute(MCSA_Reference);
1450 case DK_WEAK_DEFINITION:
1451 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1452 case DK_WEAK_REFERENCE:
1453 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1454 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1455 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1456 case DK_COMM:
1457 case DK_COMMON:
1458 return parseDirectiveComm(/*IsLocal=*/false);
1459 case DK_LCOMM:
1460 return parseDirectiveComm(/*IsLocal=*/true);
1461 case DK_ABORT:
1462 return parseDirectiveAbort();
1463 case DK_INCLUDE:
1464 return parseDirectiveInclude();
1465 case DK_INCBIN:
1466 return parseDirectiveIncbin();
1467 case DK_CODE16:
1468 case DK_CODE16GCC:
1469 return TokError(Twine(IDVal) + " not supported yet");
1470 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001471 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001472 case DK_IRP:
1473 return parseDirectiveIrp(IDLoc);
1474 case DK_IRPC:
1475 return parseDirectiveIrpc(IDLoc);
1476 case DK_ENDR:
1477 return parseDirectiveEndr(IDLoc);
1478 case DK_BUNDLE_ALIGN_MODE:
1479 return parseDirectiveBundleAlignMode();
1480 case DK_BUNDLE_LOCK:
1481 return parseDirectiveBundleLock();
1482 case DK_BUNDLE_UNLOCK:
1483 return parseDirectiveBundleUnlock();
1484 case DK_SLEB128:
1485 return parseDirectiveLEB128(true);
1486 case DK_ULEB128:
1487 return parseDirectiveLEB128(false);
1488 case DK_SPACE:
1489 case DK_SKIP:
1490 return parseDirectiveSpace(IDVal);
1491 case DK_FILE:
1492 return parseDirectiveFile(IDLoc);
1493 case DK_LINE:
1494 return parseDirectiveLine();
1495 case DK_LOC:
1496 return parseDirectiveLoc();
1497 case DK_STABS:
1498 return parseDirectiveStabs();
1499 case DK_CFI_SECTIONS:
1500 return parseDirectiveCFISections();
1501 case DK_CFI_STARTPROC:
1502 return parseDirectiveCFIStartProc();
1503 case DK_CFI_ENDPROC:
1504 return parseDirectiveCFIEndProc();
1505 case DK_CFI_DEF_CFA:
1506 return parseDirectiveCFIDefCfa(IDLoc);
1507 case DK_CFI_DEF_CFA_OFFSET:
1508 return parseDirectiveCFIDefCfaOffset();
1509 case DK_CFI_ADJUST_CFA_OFFSET:
1510 return parseDirectiveCFIAdjustCfaOffset();
1511 case DK_CFI_DEF_CFA_REGISTER:
1512 return parseDirectiveCFIDefCfaRegister(IDLoc);
1513 case DK_CFI_OFFSET:
1514 return parseDirectiveCFIOffset(IDLoc);
1515 case DK_CFI_REL_OFFSET:
1516 return parseDirectiveCFIRelOffset(IDLoc);
1517 case DK_CFI_PERSONALITY:
1518 return parseDirectiveCFIPersonalityOrLsda(true);
1519 case DK_CFI_LSDA:
1520 return parseDirectiveCFIPersonalityOrLsda(false);
1521 case DK_CFI_REMEMBER_STATE:
1522 return parseDirectiveCFIRememberState();
1523 case DK_CFI_RESTORE_STATE:
1524 return parseDirectiveCFIRestoreState();
1525 case DK_CFI_SAME_VALUE:
1526 return parseDirectiveCFISameValue(IDLoc);
1527 case DK_CFI_RESTORE:
1528 return parseDirectiveCFIRestore(IDLoc);
1529 case DK_CFI_ESCAPE:
1530 return parseDirectiveCFIEscape();
1531 case DK_CFI_SIGNAL_FRAME:
1532 return parseDirectiveCFISignalFrame();
1533 case DK_CFI_UNDEFINED:
1534 return parseDirectiveCFIUndefined(IDLoc);
1535 case DK_CFI_REGISTER:
1536 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001537 case DK_CFI_WINDOW_SAVE:
1538 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001539 case DK_MACROS_ON:
1540 case DK_MACROS_OFF:
1541 return parseDirectiveMacrosOnOff(IDVal);
1542 case DK_MACRO:
1543 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001544 case DK_EXITM:
1545 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 case DK_ENDM:
1547 case DK_ENDMACRO:
1548 return parseDirectiveEndMacro(IDVal);
1549 case DK_PURGEM:
1550 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001551 case DK_END:
1552 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001553 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001554 return parseDirectiveError(IDLoc, false);
1555 case DK_ERROR:
1556 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001557 case DK_WARNING:
1558 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001559 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001560
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001561 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001562 }
Chris Lattner36e02122009-06-21 20:54:55 +00001563
Chad Rosierc7f552c2013-02-12 21:33:51 +00001564 // __asm _emit or __asm __emit
1565 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1566 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001568
1569 // __asm align
1570 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001571 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001572
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001573 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001574
Chris Lattner7cbfa442010-05-19 23:34:33 +00001575 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001576 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001577 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001579 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001580 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001581
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001582 // Dump the parsed representation, if requested.
1583 if (getShowParsedOperands()) {
1584 SmallString<256> Str;
1585 raw_svector_ostream OS(Str);
1586 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001587 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001588 if (i != 0)
1589 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001590 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001591 }
1592 OS << "]";
1593
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001595 }
1596
Oliver Stannard8b273082014-06-19 15:52:37 +00001597 // If we are generating dwarf for the current section then generate a .loc
1598 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001599 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001600 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001601 getStreamer().getCurrentSection().first)) {
1602 unsigned Line;
1603 if (ActiveMacros.empty())
1604 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1605 else
1606 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1607 ActiveMacros.back()->ExitBuffer);
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.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001612 if (CppHashFilename.size()) {
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
David Majnemer58cb80c2014-12-24 10:27:50 +00002223 Sym->setRedefinable(allow_redef);
2224
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002225 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002226 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002227 if (NoDeadStrip)
2228 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2229
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002230 return false;
2231}
2232
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002233/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002234/// ::= identifier
2235/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002236bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002237 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002238 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2239 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002240 // handle this as a context dependent token, instead we detect adjacent tokens
2241 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002242 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2243 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244
Hans Wennborgce69d772013-10-18 20:46:28 +00002245 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002246 Lex();
2247 if (Lexer.isNot(AsmToken::Identifier))
2248 return true;
2249
Hans Wennborgce69d772013-10-18 20:46:28 +00002250 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2251 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002252 return true;
2253
2254 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002255 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002256 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002257 Lex();
2258 return false;
2259 }
2260
Jim Grosbach4b905842013-09-20 23:08:21 +00002261 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002262 return true;
2263
Sean Callanan936b0d32010-01-19 21:44:56 +00002264 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
Sean Callanan686ed8d2010-01-19 20:22:31 +00002266 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002267
2268 return false;
2269}
2270
Jim Grosbach4b905842013-09-20 23:08:21 +00002271/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002272/// ::= .equ identifier ',' expression
2273/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002274/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002275bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002276 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002277
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002278 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002279 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002280
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002281 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002282 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284
Jim Grosbach4b905842013-09-20 23:08:21 +00002285 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002286}
2287
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002288bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002289 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002290
2291 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002292 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002293 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2294 if (Str[i] != '\\') {
2295 Data += Str[i];
2296 continue;
2297 }
2298
2299 // Recognize escaped characters. Note that this escape semantics currently
2300 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2301 ++i;
2302 if (i == e)
2303 return TokError("unexpected backslash at end of string");
2304
2305 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002306 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002307 // Consume up to three octal characters.
2308 unsigned Value = Str[i] - '0';
2309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002311 ++i;
2312 Value = Value * 8 + (Str[i] - '0');
2313
Jim Grosbach4b905842013-09-20 23:08:21 +00002314 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002315 ++i;
2316 Value = Value * 8 + (Str[i] - '0');
2317 }
2318 }
2319
2320 if (Value > 255)
2321 return TokError("invalid octal escape sequence (out of range)");
2322
Jim Grosbach4b905842013-09-20 23:08:21 +00002323 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002324 continue;
2325 }
2326
2327 // Otherwise recognize individual escapes.
2328 switch (Str[i]) {
2329 default:
2330 // Just reject invalid escape sequences for now.
2331 return TokError("invalid escape sequence (unrecognized character)");
2332
2333 case 'b': Data += '\b'; break;
2334 case 'f': Data += '\f'; break;
2335 case 'n': Data += '\n'; break;
2336 case 'r': Data += '\r'; break;
2337 case 't': Data += '\t'; break;
2338 case '"': Data += '"'; break;
2339 case '\\': Data += '\\'; break;
2340 }
2341 }
2342
2343 return false;
2344}
2345
Jim Grosbach4b905842013-09-20 23:08:21 +00002346/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002347/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002348bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002349 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002350 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002351
Daniel Dunbara10e5192009-06-24 23:30:00 +00002352 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002354 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002357 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002358 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002361 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002362 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
Sean Callanan686ed8d2010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365
2366 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002367 break;
2368
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002369 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002370 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002372 }
2373 }
2374
Sean Callanan686ed8d2010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002376 return false;
2377}
2378
Jim Grosbach4b905842013-09-20 23:08:21 +00002379/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002380/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002381bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002383 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002384
Daniel Dunbara10e5192009-06-24 23:30:00 +00002385 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002386 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002387 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002388 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002389 return true;
2390
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002391 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002392 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2393 assert(Size <= 8 && "Invalid size");
2394 uint64_t IntValue = MCE->getValue();
2395 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2396 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002397 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002398 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002399 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002401 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002403
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002405 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002407 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 }
2409 }
2410
Sean Callanan686ed8d2010-01-19 20:22:31 +00002411 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002412 return false;
2413}
2414
David Woodhoused6de0d92014-02-01 16:20:59 +00002415/// ParseDirectiveOctaValue
2416/// ::= .octa [ hexconstant (, hexconstant)* ]
2417bool AsmParser::parseDirectiveOctaValue() {
2418 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2419 checkForValidSection();
2420
2421 for (;;) {
2422 if (Lexer.getKind() == AsmToken::Error)
2423 return true;
2424 if (Lexer.getKind() != AsmToken::Integer &&
2425 Lexer.getKind() != AsmToken::BigNum)
2426 return TokError("unknown token in expression");
2427
2428 SMLoc ExprLoc = getLexer().getLoc();
2429 APInt IntValue = getTok().getAPIntVal();
2430 Lex();
2431
2432 uint64_t hi, lo;
2433 if (IntValue.isIntN(64)) {
2434 hi = 0;
2435 lo = IntValue.getZExtValue();
2436 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002437 // It might actually have more than 128 bits, but the top ones are zero.
2438 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002439 lo = IntValue.getLoBits(64).getZExtValue();
2440 } else
2441 return Error(ExprLoc, "literal value out of range for directive");
2442
2443 if (MAI.isLittleEndian()) {
2444 getStreamer().EmitIntValue(lo, 8);
2445 getStreamer().EmitIntValue(hi, 8);
2446 } else {
2447 getStreamer().EmitIntValue(hi, 8);
2448 getStreamer().EmitIntValue(lo, 8);
2449 }
2450
2451 if (getLexer().is(AsmToken::EndOfStatement))
2452 break;
2453
2454 // FIXME: Improve diagnostic.
2455 if (getLexer().isNot(AsmToken::Comma))
2456 return TokError("unexpected token in directive");
2457 Lex();
2458 }
2459 }
2460
2461 Lex();
2462 return false;
2463}
2464
Jim Grosbach4b905842013-09-20 23:08:21 +00002465/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002467bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002469 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470
2471 for (;;) {
2472 // We don't truly support arithmetic on floating point expressions, so we
2473 // have to manually parse unary prefixes.
2474 bool IsNeg = false;
2475 if (getLexer().is(AsmToken::Minus)) {
2476 Lex();
2477 IsNeg = true;
2478 } else if (getLexer().is(AsmToken::Plus))
2479 Lex();
2480
Michael J. Spencer530ce852010-10-09 11:00:50 +00002481 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002482 getLexer().isNot(AsmToken::Real) &&
2483 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002484 return TokError("unexpected token in directive");
2485
2486 // Convert to an APFloat.
2487 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002488 StringRef IDVal = getTok().getString();
2489 if (getLexer().is(AsmToken::Identifier)) {
2490 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2491 Value = APFloat::getInf(Semantics);
2492 else if (!IDVal.compare_lower("nan"))
2493 Value = APFloat::getNaN(Semantics, false, ~0);
2494 else
2495 return TokError("invalid floating point literal");
2496 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002497 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002498 return TokError("invalid floating point literal");
2499 if (IsNeg)
2500 Value.changeSign();
2501
2502 // Consume the numeric token.
2503 Lex();
2504
2505 // Emit the value as an integer.
2506 APInt AsInt = Value.bitcastToAPInt();
2507 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002508 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002509
2510 if (getLexer().is(AsmToken::EndOfStatement))
2511 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002512
Daniel Dunbar2af16532010-09-24 01:59:56 +00002513 if (getLexer().isNot(AsmToken::Comma))
2514 return TokError("unexpected token in directive");
2515 Lex();
2516 }
2517 }
2518
2519 Lex();
2520 return false;
2521}
2522
Jim Grosbach4b905842013-09-20 23:08:21 +00002523/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002524/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002525bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002526 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002527
2528 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002529 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002530 return true;
2531
Rafael Espindolab91bac62010-10-05 19:42:57 +00002532 int64_t Val = 0;
2533 if (getLexer().is(AsmToken::Comma)) {
2534 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002535 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002536 return true;
2537 }
2538
Rafael Espindola922e3f42010-09-16 15:03:59 +00002539 if (getLexer().isNot(AsmToken::EndOfStatement))
2540 return TokError("unexpected token in '.zero' directive");
2541
2542 Lex();
2543
Rafael Espindola64e1af82013-07-02 15:49:13 +00002544 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002545
2546 return false;
2547}
2548
Jim Grosbach4b905842013-09-20 23:08:21 +00002549/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002550/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002551bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002552 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002553
David Majnemer522d3db2014-02-01 07:19:38 +00002554 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002556 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002557 return true;
2558
David Majnemer522d3db2014-02-01 07:19:38 +00002559 if (NumValues < 0) {
2560 Warning(RepeatLoc,
2561 "'.fill' directive with negative repeat count has no effect");
2562 NumValues = 0;
2563 }
2564
Roman Divackye33098f2013-09-24 17:44:41 +00002565 int64_t FillSize = 1;
2566 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002567
David Majnemer522d3db2014-02-01 07:19:38 +00002568 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002569 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2570 if (getLexer().isNot(AsmToken::Comma))
2571 return TokError("unexpected token in '.fill' directive");
2572 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002573
David Majnemer522d3db2014-02-01 07:19:38 +00002574 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002575 if (parseAbsoluteExpression(FillSize))
2576 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002577
Roman Divackye33098f2013-09-24 17:44:41 +00002578 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2579 if (getLexer().isNot(AsmToken::Comma))
2580 return TokError("unexpected token in '.fill' directive");
2581 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002582
David Majnemer522d3db2014-02-01 07:19:38 +00002583 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002584 if (parseAbsoluteExpression(FillExpr))
2585 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002586
Roman Divackye33098f2013-09-24 17:44:41 +00002587 if (getLexer().isNot(AsmToken::EndOfStatement))
2588 return TokError("unexpected token in '.fill' directive");
2589
2590 Lex();
2591 }
2592 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002593
David Majnemer522d3db2014-02-01 07:19:38 +00002594 if (FillSize < 0) {
2595 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2596 NumValues = 0;
2597 }
2598 if (FillSize > 8) {
2599 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2600 FillSize = 8;
2601 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002602
David Majnemer522d3db2014-02-01 07:19:38 +00002603 if (!isUInt<32>(FillExpr) && FillSize > 4)
2604 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2605
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002606 if (NumValues > 0) {
2607 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2608 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2609 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2610 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2611 if (NonZeroFillSize < FillSize)
2612 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2613 }
David Majnemer522d3db2014-02-01 07:19:38 +00002614 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002615
2616 return false;
2617}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002618
Jim Grosbach4b905842013-09-20 23:08:21 +00002619/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002620/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002621bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002622 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002623
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002624 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002625 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002626 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002627 return true;
2628
2629 // Parse optional fill expression.
2630 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002631 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2632 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002633 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002634 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002635
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002636 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002637 return true;
2638
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002639 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002640 return TokError("unexpected token in '.org' directive");
2641 }
2642
Sean Callanan686ed8d2010-01-19 20:22:31 +00002643 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002644
Jim Grosbachb5912772012-01-27 00:37:08 +00002645 // Only limited forms of relocatable expressions are accepted here, it
2646 // has to be relative to the current section. The streamer will return
2647 // 'true' if the expression wasn't evaluatable.
2648 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2649 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002650
2651 return false;
2652}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653
Jim Grosbach4b905842013-09-20 23:08:21 +00002654/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002656bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002658
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002661 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662 return true;
2663
2664 SMLoc MaxBytesLoc;
2665 bool HasFillExpr = false;
2666 int64_t FillExpr = 0;
2667 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002668 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2669 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002670 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002671 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672
2673 // The fill expression can be omitted while specifying a maximum number of
2674 // alignment bytes, e.g:
2675 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002676 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002678 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679 return true;
2680 }
2681
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002682 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2683 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002685 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002687 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002688 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002690
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002691 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002692 return TokError("unexpected token in directive");
2693 }
2694 }
2695
Sean Callanan686ed8d2010-01-19 20:22:31 +00002696 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002698 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002700
2701 // Compute alignment in bytes.
2702 if (IsPow2) {
2703 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002704 if (Alignment >= 32) {
2705 Error(AlignmentLoc, "invalid alignment value");
2706 Alignment = 31;
2707 }
2708
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002709 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002710 } else {
2711 // Reject alignments that aren't a power of two, for gas compatibility.
2712 if (!isPowerOf2_64(Alignment))
2713 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002714 }
2715
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002716 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 if (MaxBytesLoc.isValid()) {
2718 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002719 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002720 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002721 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002722 }
2723
2724 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002725 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002726 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002727 MaxBytesToFill = 0;
2728 }
2729 }
2730
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002731 // Check whether we should use optimal code alignment for this .align
2732 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002733 const MCSection *Section = getStreamer().getCurrentSection().first;
2734 assert(Section && "must have section to emit alignment");
2735 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002736 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2737 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002738 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002739 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002740 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002741 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2742 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002743 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002744
2745 return false;
2746}
2747
Jim Grosbach4b905842013-09-20 23:08:21 +00002748/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002749/// ::= .file [number] filename
2750/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002751bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002752 // FIXME: I'm not sure what this is.
2753 int64_t FileNumber = -1;
2754 SMLoc FileNumberLoc = getLexer().getLoc();
2755 if (getLexer().is(AsmToken::Integer)) {
2756 FileNumber = getTok().getIntVal();
2757 Lex();
2758
2759 if (FileNumber < 1)
2760 return TokError("file number less than one");
2761 }
2762
2763 if (getLexer().isNot(AsmToken::String))
2764 return TokError("unexpected token in '.file' directive");
2765
2766 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002767 // Allow the strings to have escaped octal character sequence.
2768 std::string Path = getTok().getString();
2769 if (parseEscapedString(Path))
2770 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002771 Lex();
2772
2773 StringRef Directory;
2774 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002775 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002776 if (getLexer().is(AsmToken::String)) {
2777 if (FileNumber == -1)
2778 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002779 if (parseEscapedString(FilenameData))
2780 return true;
2781 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002782 Directory = Path;
2783 Lex();
2784 } else {
2785 Filename = Path;
2786 }
2787
2788 if (getLexer().isNot(AsmToken::EndOfStatement))
2789 return TokError("unexpected token in '.file' directive");
2790
2791 if (FileNumber == -1)
2792 getStreamer().EmitFileDirective(Filename);
2793 else {
2794 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002795 Error(DirectiveLoc,
2796 "input can't have .file dwarf directives when -g is "
2797 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002798
David Blaikiec714ef42014-03-17 01:52:11 +00002799 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2800 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002801 Error(FileNumberLoc, "file number already allocated");
2802 }
2803
2804 return false;
2805}
2806
Jim Grosbach4b905842013-09-20 23:08:21 +00002807/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002808/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002809bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002810 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2811 if (getLexer().isNot(AsmToken::Integer))
2812 return TokError("unexpected token in '.line' directive");
2813
2814 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002815 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002816 Lex();
2817
2818 // FIXME: Do something with the .line.
2819 }
2820
2821 if (getLexer().isNot(AsmToken::EndOfStatement))
2822 return TokError("unexpected token in '.line' directive");
2823
2824 return false;
2825}
2826
Jim Grosbach4b905842013-09-20 23:08:21 +00002827/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002828/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2829/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2830/// The first number is a file number, must have been previously assigned with
2831/// a .file directive, the second number is the line number and optionally the
2832/// third number is a column position (zero if not specified). The remaining
2833/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002834bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002835 if (getLexer().isNot(AsmToken::Integer))
2836 return TokError("unexpected token in '.loc' directive");
2837 int64_t FileNumber = getTok().getIntVal();
2838 if (FileNumber < 1)
2839 return TokError("file number less than one in '.loc' directive");
2840 if (!getContext().isValidDwarfFileNumber(FileNumber))
2841 return TokError("unassigned file number in '.loc' directive");
2842 Lex();
2843
2844 int64_t LineNumber = 0;
2845 if (getLexer().is(AsmToken::Integer)) {
2846 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002847 if (LineNumber < 0)
2848 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002849 Lex();
2850 }
2851
2852 int64_t ColumnPos = 0;
2853 if (getLexer().is(AsmToken::Integer)) {
2854 ColumnPos = getTok().getIntVal();
2855 if (ColumnPos < 0)
2856 return TokError("column position less than zero in '.loc' directive");
2857 Lex();
2858 }
2859
2860 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2861 unsigned Isa = 0;
2862 int64_t Discriminator = 0;
2863 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2864 for (;;) {
2865 if (getLexer().is(AsmToken::EndOfStatement))
2866 break;
2867
2868 StringRef Name;
2869 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002870 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002871 return TokError("unexpected token in '.loc' directive");
2872
2873 if (Name == "basic_block")
2874 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2875 else if (Name == "prologue_end")
2876 Flags |= DWARF2_FLAG_PROLOGUE_END;
2877 else if (Name == "epilogue_begin")
2878 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2879 else if (Name == "is_stmt") {
2880 Loc = getTok().getLoc();
2881 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002882 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002883 return true;
2884 // The expression must be the constant 0 or 1.
2885 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2886 int Value = MCE->getValue();
2887 if (Value == 0)
2888 Flags &= ~DWARF2_FLAG_IS_STMT;
2889 else if (Value == 1)
2890 Flags |= DWARF2_FLAG_IS_STMT;
2891 else
2892 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002893 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002894 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2895 }
Craig Topperf15655b2013-04-22 04:22:40 +00002896 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002897 Loc = getTok().getLoc();
2898 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002899 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return true;
2901 // The expression must be a constant greater or equal to 0.
2902 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2903 int Value = MCE->getValue();
2904 if (Value < 0)
2905 return Error(Loc, "isa number less than zero");
2906 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002907 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return Error(Loc, "isa number not a constant value");
2909 }
Craig Topperf15655b2013-04-22 04:22:40 +00002910 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002911 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002913 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return Error(Loc, "unknown sub-directive in '.loc' directive");
2915 }
2916
2917 if (getLexer().is(AsmToken::EndOfStatement))
2918 break;
2919 }
2920 }
2921
2922 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2923 Isa, Discriminator, StringRef());
2924
2925 return false;
2926}
2927
Jim Grosbach4b905842013-09-20 23:08:21 +00002928/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002929/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002930bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002931 return TokError("unsupported directive '.stabs'");
2932}
2933
Jim Grosbach4b905842013-09-20 23:08:21 +00002934/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002935/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002936bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002937 StringRef Name;
2938 bool EH = false;
2939 bool Debug = false;
2940
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002941 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002942 return TokError("Expected an identifier");
2943
2944 if (Name == ".eh_frame")
2945 EH = true;
2946 else if (Name == ".debug_frame")
2947 Debug = true;
2948
2949 if (getLexer().is(AsmToken::Comma)) {
2950 Lex();
2951
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002952 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002953 return TokError("Expected an identifier");
2954
2955 if (Name == ".eh_frame")
2956 EH = true;
2957 else if (Name == ".debug_frame")
2958 Debug = true;
2959 }
2960
2961 getStreamer().EmitCFISections(EH, Debug);
2962 return false;
2963}
2964
Jim Grosbach4b905842013-09-20 23:08:21 +00002965/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002966/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002967bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002968 StringRef Simple;
2969 if (getLexer().isNot(AsmToken::EndOfStatement))
2970 if (parseIdentifier(Simple) || Simple != "simple")
2971 return TokError("unexpected token in .cfi_startproc directive");
2972
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00002973 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002974 return false;
2975}
2976
Jim Grosbach4b905842013-09-20 23:08:21 +00002977/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002978/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002979bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002980 getStreamer().EmitCFIEndProc();
2981 return false;
2982}
2983
Jim Grosbach4b905842013-09-20 23:08:21 +00002984/// \brief parse register name or number.
2985bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002986 SMLoc DirectiveLoc) {
2987 unsigned RegNo;
2988
2989 if (getLexer().isNot(AsmToken::Integer)) {
2990 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2991 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002992 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002993 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002994 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002995
2996 return false;
2997}
2998
Jim Grosbach4b905842013-09-20 23:08:21 +00002999/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003000/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003001bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003002 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003003 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003004 return true;
3005
3006 if (getLexer().isNot(AsmToken::Comma))
3007 return TokError("unexpected token in directive");
3008 Lex();
3009
3010 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003011 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003012 return true;
3013
3014 getStreamer().EmitCFIDefCfa(Register, Offset);
3015 return false;
3016}
3017
Jim Grosbach4b905842013-09-20 23:08:21 +00003018/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003019/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003020bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003021 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003022 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003023 return true;
3024
3025 getStreamer().EmitCFIDefCfaOffset(Offset);
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003031bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003032 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003033 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003034 return true;
3035
3036 if (getLexer().isNot(AsmToken::Comma))
3037 return TokError("unexpected token in directive");
3038 Lex();
3039
3040 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003041 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 getStreamer().EmitCFIRegister(Register1, Register2);
3045 return false;
3046}
3047
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003048/// parseDirectiveCFIWindowSave
3049/// ::= .cfi_window_save
3050bool AsmParser::parseDirectiveCFIWindowSave() {
3051 getStreamer().EmitCFIWindowSave();
3052 return false;
3053}
3054
Jim Grosbach4b905842013-09-20 23:08:21 +00003055/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003056/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003057bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003058 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003059 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003060 return true;
3061
3062 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3063 return false;
3064}
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003067/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003068bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003069 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003070 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003071 return true;
3072
3073 getStreamer().EmitCFIDefCfaRegister(Register);
3074 return false;
3075}
3076
Jim Grosbach4b905842013-09-20 23:08:21 +00003077/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003078/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003079bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003080 int64_t Register = 0;
3081 int64_t Offset = 0;
3082
Jim Grosbach4b905842013-09-20 23:08:21 +00003083 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003084 return true;
3085
3086 if (getLexer().isNot(AsmToken::Comma))
3087 return TokError("unexpected token in directive");
3088 Lex();
3089
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003090 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return true;
3092
3093 getStreamer().EmitCFIOffset(Register, Offset);
3094 return false;
3095}
3096
Jim Grosbach4b905842013-09-20 23:08:21 +00003097/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003098/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003099bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003100 int64_t Register = 0;
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003103 return true;
3104
3105 if (getLexer().isNot(AsmToken::Comma))
3106 return TokError("unexpected token in directive");
3107 Lex();
3108
3109 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003110 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003111 return true;
3112
3113 getStreamer().EmitCFIRelOffset(Register, Offset);
3114 return false;
3115}
3116
3117static bool isValidEncoding(int64_t Encoding) {
3118 if (Encoding & ~0xff)
3119 return false;
3120
3121 if (Encoding == dwarf::DW_EH_PE_omit)
3122 return true;
3123
3124 const unsigned Format = Encoding & 0xf;
3125 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3126 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3127 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3128 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3129 return false;
3130
3131 const unsigned Application = Encoding & 0x70;
3132 if (Application != dwarf::DW_EH_PE_absptr &&
3133 Application != dwarf::DW_EH_PE_pcrel)
3134 return false;
3135
3136 return true;
3137}
3138
Jim Grosbach4b905842013-09-20 23:08:21 +00003139/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003140/// IsPersonality true for cfi_personality, false for cfi_lsda
3141/// ::= .cfi_personality encoding, [symbol_name]
3142/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003143bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003144 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003145 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003146 return true;
3147 if (Encoding == dwarf::DW_EH_PE_omit)
3148 return false;
3149
3150 if (!isValidEncoding(Encoding))
3151 return TokError("unsupported encoding.");
3152
3153 if (getLexer().isNot(AsmToken::Comma))
3154 return TokError("unexpected token in directive");
3155 Lex();
3156
3157 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003158 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return TokError("expected identifier in directive");
3160
3161 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3162
3163 if (IsPersonality)
3164 getStreamer().EmitCFIPersonality(Sym, Encoding);
3165 else
3166 getStreamer().EmitCFILsda(Sym, Encoding);
3167 return false;
3168}
3169
Jim Grosbach4b905842013-09-20 23:08:21 +00003170/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003171/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003172bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003173 getStreamer().EmitCFIRememberState();
3174 return false;
3175}
3176
Jim Grosbach4b905842013-09-20 23:08:21 +00003177/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003178/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003179bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003180 getStreamer().EmitCFIRestoreState();
3181 return false;
3182}
3183
Jim Grosbach4b905842013-09-20 23:08:21 +00003184/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003185/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003186bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003187 int64_t Register = 0;
3188
Jim Grosbach4b905842013-09-20 23:08:21 +00003189 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003190 return true;
3191
3192 getStreamer().EmitCFISameValue(Register);
3193 return false;
3194}
3195
Jim Grosbach4b905842013-09-20 23:08:21 +00003196/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003197/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003198bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003199 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003200 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003201 return true;
3202
3203 getStreamer().EmitCFIRestore(Register);
3204 return false;
3205}
3206
Jim Grosbach4b905842013-09-20 23:08:21 +00003207/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003208/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003209bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003210 std::string Values;
3211 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003212 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003213 return true;
3214
3215 Values.push_back((uint8_t)CurrValue);
3216
3217 while (getLexer().is(AsmToken::Comma)) {
3218 Lex();
3219
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003220 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003221 return true;
3222
3223 Values.push_back((uint8_t)CurrValue);
3224 }
3225
3226 getStreamer().EmitCFIEscape(Values);
3227 return false;
3228}
3229
Jim Grosbach4b905842013-09-20 23:08:21 +00003230/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003231/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003232bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003233 if (getLexer().isNot(AsmToken::EndOfStatement))
3234 return Error(getLexer().getLoc(),
3235 "unexpected token in '.cfi_signal_frame'");
3236
3237 getStreamer().EmitCFISignalFrame();
3238 return false;
3239}
3240
Jim Grosbach4b905842013-09-20 23:08:21 +00003241/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003242/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003243bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003244 int64_t Register = 0;
3245
Jim Grosbach4b905842013-09-20 23:08:21 +00003246 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003247 return true;
3248
3249 getStreamer().EmitCFIUndefined(Register);
3250 return false;
3251}
3252
Jim Grosbach4b905842013-09-20 23:08:21 +00003253/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003254/// ::= .macros_on
3255/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003256bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003257 if (getLexer().isNot(AsmToken::EndOfStatement))
3258 return Error(getLexer().getLoc(),
3259 "unexpected token in '" + Directive + "' directive");
3260
Jim Grosbach4b905842013-09-20 23:08:21 +00003261 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003262 return false;
3263}
3264
Jim Grosbach4b905842013-09-20 23:08:21 +00003265/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003266/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003267bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003268 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003269 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003270 return TokError("expected identifier in '.macro' directive");
3271
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003272 if (getLexer().is(AsmToken::Comma))
3273 Lex();
3274
Eli Bendersky17233942013-01-15 22:59:42 +00003275 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003276 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003277
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003278 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003279 return Error(Lexer.getLoc(),
3280 "Vararg parameter '" + Parameters.back().Name +
3281 "' should be last one in the list of parameters.");
3282
David Majnemer91fc4c22014-01-29 18:57:46 +00003283 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003284 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003285 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003286
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003287 if (Lexer.is(AsmToken::Colon)) {
3288 Lex(); // consume ':'
3289
3290 SMLoc QualLoc;
3291 StringRef Qualifier;
3292
3293 QualLoc = Lexer.getLoc();
3294 if (parseIdentifier(Qualifier))
3295 return Error(QualLoc, "missing parameter qualifier for "
3296 "'" + Parameter.Name + "' in macro '" + Name + "'");
3297
3298 if (Qualifier == "req")
3299 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003300 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003301 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003302 else
3303 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3304 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3305 }
3306
David Majnemer91fc4c22014-01-29 18:57:46 +00003307 if (getLexer().is(AsmToken::Equal)) {
3308 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003309
3310 SMLoc ParamLoc;
3311
3312 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003313 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003314 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003315
3316 if (Parameter.Required)
3317 Warning(ParamLoc, "pointless default value for required parameter "
3318 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003319 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003320
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003321 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003322
3323 if (getLexer().is(AsmToken::Comma))
3324 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003325 }
3326
3327 // Eat the end of statement.
3328 Lex();
3329
3330 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003331 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003332
3333 // Lex the macro definition.
3334 for (;;) {
3335 // Check whether we have reached the end of the file.
3336 if (getLexer().is(AsmToken::Eof))
3337 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3338
3339 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003340 if (getLexer().is(AsmToken::Identifier)) {
3341 if (getTok().getIdentifier() == ".endm" ||
3342 getTok().getIdentifier() == ".endmacro") {
3343 if (MacroDepth == 0) { // Outermost macro.
3344 EndToken = getTok();
3345 Lex();
3346 if (getLexer().isNot(AsmToken::EndOfStatement))
3347 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3348 "' directive");
3349 break;
3350 } else {
3351 // Otherwise we just found the end of an inner macro.
3352 --MacroDepth;
3353 }
3354 } else if (getTok().getIdentifier() == ".macro") {
3355 // We allow nested macros. Those aren't instantiated until the outermost
3356 // macro is expanded so just ignore them for now.
3357 ++MacroDepth;
3358 }
Eli Bendersky17233942013-01-15 22:59:42 +00003359 }
3360
3361 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003362 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003363 }
3364
Jim Grosbach4b905842013-09-20 23:08:21 +00003365 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003366 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3367 }
3368
3369 const char *BodyStart = StartToken.getLoc().getPointer();
3370 const char *BodyEnd = EndToken.getLoc().getPointer();
3371 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003372 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003373 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003374 return false;
3375}
3376
Jim Grosbach4b905842013-09-20 23:08:21 +00003377/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003378///
3379/// With the support added for named parameters there may be code out there that
3380/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003381/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003382/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003383/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3385/// warning that the positional parameter found in body which have no effect.
3386/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003387/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388/// intended or change the macro to use the named parameters. It is possible
3389/// this warning will trigger when the none of the named parameters are used
3390/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003391void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003392 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003393 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394 // If this macro is not defined with named parameters the warning we are
3395 // checking for here doesn't apply.
3396 unsigned NParameters = Parameters.size();
3397 if (NParameters == 0)
3398 return;
3399
3400 bool NamedParametersFound = false;
3401 bool PositionalParametersFound = false;
3402
3403 // Look at the body of the macro for use of both the named parameters and what
3404 // are likely to be positional parameters. This is what expandMacro() is
3405 // doing when it finds the parameters in the body.
3406 while (!Body.empty()) {
3407 // Scan for the next possible parameter.
3408 std::size_t End = Body.size(), Pos = 0;
3409 for (; Pos != End; ++Pos) {
3410 // Check for a substitution or escape.
3411 // This macro is defined with parameters, look for \foo, \bar, etc.
3412 if (Body[Pos] == '\\' && Pos + 1 != End)
3413 break;
3414
3415 // This macro should have parameters, but look for $0, $1, ..., $n too.
3416 if (Body[Pos] != '$' || Pos + 1 == End)
3417 continue;
3418 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003419 if (Next == '$' || Next == 'n' ||
3420 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003421 break;
3422 }
3423
3424 // Check if we reached the end.
3425 if (Pos == End)
3426 break;
3427
3428 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003429 switch (Body[Pos + 1]) {
3430 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003431 case '$':
3432 break;
3433
Jim Grosbach4b905842013-09-20 23:08:21 +00003434 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003435 case 'n':
3436 PositionalParametersFound = true;
3437 break;
3438
Jim Grosbach4b905842013-09-20 23:08:21 +00003439 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003440 default: {
3441 PositionalParametersFound = true;
3442 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003443 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003444 }
3445 Pos += 2;
3446 } else {
3447 unsigned I = Pos + 1;
3448 while (isIdentifierChar(Body[I]) && I + 1 != End)
3449 ++I;
3450
Jim Grosbach4b905842013-09-20 23:08:21 +00003451 const char *Begin = Body.data() + Pos + 1;
3452 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003453 unsigned Index = 0;
3454 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003455 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003456 break;
3457
3458 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003459 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3460 Pos += 3;
3461 else {
3462 Pos = I;
3463 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003464 } else {
3465 NamedParametersFound = true;
3466 Pos += 1 + Argument.size();
3467 }
3468 }
3469 // Update the scan point.
3470 Body = Body.substr(Pos);
3471 }
3472
3473 if (!NamedParametersFound && PositionalParametersFound)
3474 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3475 "used in macro body, possible positional parameter "
3476 "found in body which will have no effect");
3477}
3478
Nico Weber155dccd12014-07-24 17:08:39 +00003479/// parseDirectiveExitMacro
3480/// ::= .exitm
3481bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3482 if (getLexer().isNot(AsmToken::EndOfStatement))
3483 return TokError("unexpected token in '" + Directive + "' directive");
3484
3485 if (!isInsideMacroInstantiation())
3486 return TokError("unexpected '" + Directive + "' in file, "
3487 "no current macro definition");
3488
3489 // Exit all conditionals that are active in the current macro.
3490 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3491 TheCondState = TheCondStack.back();
3492 TheCondStack.pop_back();
3493 }
3494
3495 handleMacroExit();
3496 return false;
3497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003500/// ::= .endm
3501/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003502bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003503 if (getLexer().isNot(AsmToken::EndOfStatement))
3504 return TokError("unexpected token in '" + Directive + "' directive");
3505
3506 // If we are inside a macro instantiation, terminate the current
3507 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003508 if (isInsideMacroInstantiation()) {
3509 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003510 return false;
3511 }
3512
3513 // Otherwise, this .endmacro is a stray entry in the file; well formed
3514 // .endmacro directives are handled during the macro definition parsing.
3515 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003516 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003517}
3518
Jim Grosbach4b905842013-09-20 23:08:21 +00003519/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003520/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003521bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003522 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003523 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003524 return TokError("expected identifier in '.purgem' directive");
3525
3526 if (getLexer().isNot(AsmToken::EndOfStatement))
3527 return TokError("unexpected token in '.purgem' directive");
3528
Jim Grosbach4b905842013-09-20 23:08:21 +00003529 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003530 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3531
Jim Grosbach4b905842013-09-20 23:08:21 +00003532 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003533 return false;
3534}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003535
Jim Grosbach4b905842013-09-20 23:08:21 +00003536/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003537/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003538bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003539 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003540
3541 // Expect a single argument: an expression that evaluates to a constant
3542 // in the inclusive range 0-30.
3543 SMLoc ExprLoc = getLexer().getLoc();
3544 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003545 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003546 return true;
3547 else if (getLexer().isNot(AsmToken::EndOfStatement))
3548 return TokError("unexpected token after expression in"
3549 " '.bundle_align_mode' directive");
3550 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3551 return Error(ExprLoc,
3552 "invalid bundle alignment size (expected between 0 and 30)");
3553
3554 Lex();
3555
3556 // Because of AlignSizePow2's verified range we can safely truncate it to
3557 // unsigned.
3558 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3559 return false;
3560}
3561
Jim Grosbach4b905842013-09-20 23:08:21 +00003562/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003563/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003564bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003565 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003566 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003567
Eli Bendersky802b6282013-01-07 21:51:08 +00003568 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3569 StringRef Option;
3570 SMLoc Loc = getTok().getLoc();
3571 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003572 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003573
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003574 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003575 return Error(Loc, kInvalidOptionError);
3576
3577 if (Option != "align_to_end")
3578 return Error(Loc, kInvalidOptionError);
3579 else if (getLexer().isNot(AsmToken::EndOfStatement))
3580 return Error(Loc,
3581 "unexpected token after '.bundle_lock' directive option");
3582 AlignToEnd = true;
3583 }
3584
Eli Benderskyf483ff92012-12-20 19:05:53 +00003585 Lex();
3586
Eli Bendersky802b6282013-01-07 21:51:08 +00003587 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003588 return false;
3589}
3590
Jim Grosbach4b905842013-09-20 23:08:21 +00003591/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003592/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003593bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003594 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003595
3596 if (getLexer().isNot(AsmToken::EndOfStatement))
3597 return TokError("unexpected token in '.bundle_unlock' directive");
3598 Lex();
3599
3600 getStreamer().EmitBundleUnlock();
3601 return false;
3602}
3603
Jim Grosbach4b905842013-09-20 23:08:21 +00003604/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003605/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003606bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003607 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003608
3609 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003610 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003611 return true;
3612
3613 int64_t FillExpr = 0;
3614 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3615 if (getLexer().isNot(AsmToken::Comma))
3616 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3617 Lex();
3618
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003619 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003620 return true;
3621
3622 if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3624 }
3625
3626 Lex();
3627
3628 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003629 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3630 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003631
3632 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003633 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003634
3635 return false;
3636}
3637
Jim Grosbach4b905842013-09-20 23:08:21 +00003638/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003639/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003640bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003642 const MCExpr *Value;
3643
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003644 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003645 return true;
3646
3647 if (getLexer().isNot(AsmToken::EndOfStatement))
3648 return TokError("unexpected token in directive");
3649
3650 if (Signed)
3651 getStreamer().EmitSLEB128Value(Value);
3652 else
3653 getStreamer().EmitULEB128Value(Value);
3654
3655 return false;
3656}
3657
Jim Grosbach4b905842013-09-20 23:08:21 +00003658/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003659/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003660bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003661 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003662 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003663 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003664 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003665
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003666 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003667 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003668
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003669 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003670
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003671 // Assembler local symbols don't make any sense here. Complain loudly.
3672 if (Sym->isTemporary())
3673 return Error(Loc, "non-local symbol required in directive");
3674
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003675 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3676 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003677
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003678 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003679 break;
3680
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003681 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003682 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003683 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003684 }
3685 }
3686
Sean Callanan686ed8d2010-01-19 20:22:31 +00003687 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003688 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003689}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003690
Jim Grosbach4b905842013-09-20 23:08:21 +00003691/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003692/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003693bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003694 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003695
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003696 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003697 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003698 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003699 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003700
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003701 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003702 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003703
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003704 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003705 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003706 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003707
3708 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003709 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003710 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003711 return true;
3712
3713 int64_t Pow2Alignment = 0;
3714 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003715 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003716 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003717 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003718 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003719 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003720
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003721 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3722 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003723 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3724
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003725 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003726 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3727 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003728 if (!isPowerOf2_64(Pow2Alignment))
3729 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3730 Pow2Alignment = Log2_64(Pow2Alignment);
3731 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003732 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003733
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003734 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003735 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003736
Sean Callanan686ed8d2010-01-19 20:22:31 +00003737 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003738
Chris Lattner28ad7542009-07-09 17:25:12 +00003739 // NOTE: a size of zero for a .comm should create a undefined symbol
3740 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003741 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003742 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003743 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003744
Eric Christopherbc818852010-05-14 01:38:54 +00003745 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003746 // may internally end up wanting an alignment in bytes.
3747 // FIXME: Diagnose overflow.
3748 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003749 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003750 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003751
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003752 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003753 return Error(IDLoc, "invalid symbol redefinition");
3754
Chris Lattner28ad7542009-07-09 17:25:12 +00003755 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003756 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003757 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003758 return false;
3759 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003760
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003761 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003762 return false;
3763}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003764
Jim Grosbach4b905842013-09-20 23:08:21 +00003765/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003766/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003767bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003768 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003769 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003770
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003771 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003772 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003773 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003774
Sean Callanan686ed8d2010-01-19 20:22:31 +00003775 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003776
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003777 if (Str.empty())
3778 Error(Loc, ".abort detected. Assembly stopping.");
3779 else
3780 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003781 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003782
3783 return false;
3784}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003785
Jim Grosbach4b905842013-09-20 23:08:21 +00003786/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003787/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003788bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003789 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003790 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003791
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003792 // Allow the strings to have escaped octal character sequence.
3793 std::string Filename;
3794 if (parseEscapedString(Filename))
3795 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003797 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003798
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003799 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003800 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003801
Chris Lattner693fbb82009-07-16 06:14:39 +00003802 // Attempt to switch the lexer to the included file before consuming the end
3803 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003804 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003805 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003806 return true;
3807 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003808
3809 return false;
3810}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003811
Jim Grosbach4b905842013-09-20 23:08:21 +00003812/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003813/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003814bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003815 if (getLexer().isNot(AsmToken::String))
3816 return TokError("expected string in '.incbin' directive");
3817
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003818 // Allow the strings to have escaped octal character sequence.
3819 std::string Filename;
3820 if (parseEscapedString(Filename))
3821 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003822 SMLoc IncbinLoc = getLexer().getLoc();
3823 Lex();
3824
3825 if (getLexer().isNot(AsmToken::EndOfStatement))
3826 return TokError("unexpected token in '.incbin' directive");
3827
Kevin Enderby109f25c2011-12-14 21:47:48 +00003828 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003829 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003830 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3831 return true;
3832 }
3833
3834 return false;
3835}
3836
Jim Grosbach4b905842013-09-20 23:08:21 +00003837/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003838/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3839bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003840 TheCondStack.push_back(TheCondState);
3841 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003842 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003843 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003844 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003845 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003846 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003847 return true;
3848
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003849 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003850 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003851
Sean Callanan686ed8d2010-01-19 20:22:31 +00003852 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003853
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003854 switch (DirKind) {
3855 default:
3856 llvm_unreachable("unsupported directive");
3857 case DK_IF:
3858 case DK_IFNE:
3859 break;
3860 case DK_IFEQ:
3861 ExprValue = ExprValue == 0;
3862 break;
3863 case DK_IFGE:
3864 ExprValue = ExprValue >= 0;
3865 break;
3866 case DK_IFGT:
3867 ExprValue = ExprValue > 0;
3868 break;
3869 case DK_IFLE:
3870 ExprValue = ExprValue <= 0;
3871 break;
3872 case DK_IFLT:
3873 ExprValue = ExprValue < 0;
3874 break;
3875 }
3876
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003877 TheCondState.CondMet = ExprValue;
3878 TheCondState.Ignore = !TheCondState.CondMet;
3879 }
3880
3881 return false;
3882}
3883
Jim Grosbach4b905842013-09-20 23:08:21 +00003884/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003885/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003886bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003887 TheCondStack.push_back(TheCondState);
3888 TheCondState.TheCond = AsmCond::IfCond;
3889
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003890 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003891 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003892 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003893 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003894
3895 if (getLexer().isNot(AsmToken::EndOfStatement))
3896 return TokError("unexpected token in '.ifb' directive");
3897
3898 Lex();
3899
3900 TheCondState.CondMet = ExpectBlank == Str.empty();
3901 TheCondState.Ignore = !TheCondState.CondMet;
3902 }
3903
3904 return false;
3905}
3906
Jim Grosbach4b905842013-09-20 23:08:21 +00003907/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003908/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003909/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003910bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003911 TheCondStack.push_back(TheCondState);
3912 TheCondState.TheCond = AsmCond::IfCond;
3913
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003914 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003915 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003916 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003917 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003918
3919 if (getLexer().isNot(AsmToken::Comma))
3920 return TokError("unexpected token in '.ifc' directive");
3921
3922 Lex();
3923
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003924 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003925
3926 if (getLexer().isNot(AsmToken::EndOfStatement))
3927 return TokError("unexpected token in '.ifc' directive");
3928
3929 Lex();
3930
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003931 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003932 TheCondState.Ignore = !TheCondState.CondMet;
3933 }
3934
3935 return false;
3936}
3937
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003938/// parseDirectiveIfeqs
3939/// ::= .ifeqs string1, string2
3940bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3941 if (Lexer.isNot(AsmToken::String)) {
3942 TokError("expected string parameter for '.ifeqs' directive");
3943 eatToEndOfStatement();
3944 return true;
3945 }
3946
3947 StringRef String1 = getTok().getStringContents();
3948 Lex();
3949
3950 if (Lexer.isNot(AsmToken::Comma)) {
3951 TokError("expected comma after first string for '.ifeqs' directive");
3952 eatToEndOfStatement();
3953 return true;
3954 }
3955
3956 Lex();
3957
3958 if (Lexer.isNot(AsmToken::String)) {
3959 TokError("expected string parameter for '.ifeqs' directive");
3960 eatToEndOfStatement();
3961 return true;
3962 }
3963
3964 StringRef String2 = getTok().getStringContents();
3965 Lex();
3966
3967 TheCondStack.push_back(TheCondState);
3968 TheCondState.TheCond = AsmCond::IfCond;
3969 TheCondState.CondMet = String1 == String2;
3970 TheCondState.Ignore = !TheCondState.CondMet;
3971
3972 return false;
3973}
3974
Jim Grosbach4b905842013-09-20 23:08:21 +00003975/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003976/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003977bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003978 StringRef Name;
3979 TheCondStack.push_back(TheCondState);
3980 TheCondState.TheCond = AsmCond::IfCond;
3981
3982 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003983 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003984 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003985 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003986 return TokError("expected identifier after '.ifdef'");
3987
3988 Lex();
3989
3990 MCSymbol *Sym = getContext().LookupSymbol(Name);
3991
3992 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003993 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003994 else
Craig Topper353eda42014-04-24 06:44:33 +00003995 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003996 TheCondState.Ignore = !TheCondState.CondMet;
3997 }
3998
3999 return false;
4000}
4001
Jim Grosbach4b905842013-09-20 23:08:21 +00004002/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004003/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004004bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004005 if (TheCondState.TheCond != AsmCond::IfCond &&
4006 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004007 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4008 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004009 TheCondState.TheCond = AsmCond::ElseIfCond;
4010
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004011 bool LastIgnoreState = false;
4012 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004013 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014 if (LastIgnoreState || TheCondState.CondMet) {
4015 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004016 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004017 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004018 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004019 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020 return true;
4021
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004022 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004023 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004024
Sean Callanan686ed8d2010-01-19 20:22:31 +00004025 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004026 TheCondState.CondMet = ExprValue;
4027 TheCondState.Ignore = !TheCondState.CondMet;
4028 }
4029
4030 return false;
4031}
4032
Jim Grosbach4b905842013-09-20 23:08:21 +00004033/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004034/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004035bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004036 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004037 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004038
Sean Callanan686ed8d2010-01-19 20:22:31 +00004039 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004040
4041 if (TheCondState.TheCond != AsmCond::IfCond &&
4042 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004043 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4044 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004045 TheCondState.TheCond = AsmCond::ElseCond;
4046 bool LastIgnoreState = false;
4047 if (!TheCondStack.empty())
4048 LastIgnoreState = TheCondStack.back().Ignore;
4049 if (LastIgnoreState || TheCondState.CondMet)
4050 TheCondState.Ignore = true;
4051 else
4052 TheCondState.Ignore = false;
4053
4054 return false;
4055}
4056
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004057/// parseDirectiveEnd
4058/// ::= .end
4059bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4060 if (getLexer().isNot(AsmToken::EndOfStatement))
4061 return TokError("unexpected token in '.end' directive");
4062
4063 Lex();
4064
4065 while (Lexer.isNot(AsmToken::Eof))
4066 Lex();
4067
4068 return false;
4069}
4070
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004071/// parseDirectiveError
4072/// ::= .err
4073/// ::= .error [string]
4074bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4075 if (!TheCondStack.empty()) {
4076 if (TheCondStack.back().Ignore) {
4077 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004078 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004079 }
4080 }
4081
4082 if (!WithMessage)
4083 return Error(L, ".err encountered");
4084
4085 StringRef Message = ".error directive invoked in source file";
4086 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4087 if (Lexer.isNot(AsmToken::String)) {
4088 TokError(".error argument must be a string");
4089 eatToEndOfStatement();
4090 return true;
4091 }
4092
4093 Message = getTok().getStringContents();
4094 Lex();
4095 }
4096
4097 Error(L, Message);
4098 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004099}
4100
Nico Weber404012b2014-07-24 16:26:06 +00004101/// parseDirectiveWarning
4102/// ::= .warning [string]
4103bool AsmParser::parseDirectiveWarning(SMLoc L) {
4104 if (!TheCondStack.empty()) {
4105 if (TheCondStack.back().Ignore) {
4106 eatToEndOfStatement();
4107 return false;
4108 }
4109 }
4110
4111 StringRef Message = ".warning directive invoked in source file";
4112 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4113 if (Lexer.isNot(AsmToken::String)) {
4114 TokError(".warning argument must be a string");
4115 eatToEndOfStatement();
4116 return true;
4117 }
4118
4119 Message = getTok().getStringContents();
4120 Lex();
4121 }
4122
4123 Warning(L, Message);
4124 return false;
4125}
4126
Jim Grosbach4b905842013-09-20 23:08:21 +00004127/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004128/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004129bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004130 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004131 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004132
Sean Callanan686ed8d2010-01-19 20:22:31 +00004133 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004134
Jim Grosbach4b905842013-09-20 23:08:21 +00004135 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004136 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4137 ".else");
4138 if (!TheCondStack.empty()) {
4139 TheCondState = TheCondStack.back();
4140 TheCondStack.pop_back();
4141 }
4142
4143 return false;
4144}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004145
Eli Bendersky17233942013-01-15 22:59:42 +00004146void AsmParser::initializeDirectiveKindMap() {
4147 DirectiveKindMap[".set"] = DK_SET;
4148 DirectiveKindMap[".equ"] = DK_EQU;
4149 DirectiveKindMap[".equiv"] = DK_EQUIV;
4150 DirectiveKindMap[".ascii"] = DK_ASCII;
4151 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4152 DirectiveKindMap[".string"] = DK_STRING;
4153 DirectiveKindMap[".byte"] = DK_BYTE;
4154 DirectiveKindMap[".short"] = DK_SHORT;
4155 DirectiveKindMap[".value"] = DK_VALUE;
4156 DirectiveKindMap[".2byte"] = DK_2BYTE;
4157 DirectiveKindMap[".long"] = DK_LONG;
4158 DirectiveKindMap[".int"] = DK_INT;
4159 DirectiveKindMap[".4byte"] = DK_4BYTE;
4160 DirectiveKindMap[".quad"] = DK_QUAD;
4161 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004162 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004163 DirectiveKindMap[".single"] = DK_SINGLE;
4164 DirectiveKindMap[".float"] = DK_FLOAT;
4165 DirectiveKindMap[".double"] = DK_DOUBLE;
4166 DirectiveKindMap[".align"] = DK_ALIGN;
4167 DirectiveKindMap[".align32"] = DK_ALIGN32;
4168 DirectiveKindMap[".balign"] = DK_BALIGN;
4169 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4170 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4171 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4172 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4173 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4174 DirectiveKindMap[".org"] = DK_ORG;
4175 DirectiveKindMap[".fill"] = DK_FILL;
4176 DirectiveKindMap[".zero"] = DK_ZERO;
4177 DirectiveKindMap[".extern"] = DK_EXTERN;
4178 DirectiveKindMap[".globl"] = DK_GLOBL;
4179 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004180 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4181 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4182 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4183 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4184 DirectiveKindMap[".reference"] = DK_REFERENCE;
4185 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4186 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4187 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4188 DirectiveKindMap[".comm"] = DK_COMM;
4189 DirectiveKindMap[".common"] = DK_COMMON;
4190 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4191 DirectiveKindMap[".abort"] = DK_ABORT;
4192 DirectiveKindMap[".include"] = DK_INCLUDE;
4193 DirectiveKindMap[".incbin"] = DK_INCBIN;
4194 DirectiveKindMap[".code16"] = DK_CODE16;
4195 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4196 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004197 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004198 DirectiveKindMap[".irp"] = DK_IRP;
4199 DirectiveKindMap[".irpc"] = DK_IRPC;
4200 DirectiveKindMap[".endr"] = DK_ENDR;
4201 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4202 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4203 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4204 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004205 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4206 DirectiveKindMap[".ifge"] = DK_IFGE;
4207 DirectiveKindMap[".ifgt"] = DK_IFGT;
4208 DirectiveKindMap[".ifle"] = DK_IFLE;
4209 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004210 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004211 DirectiveKindMap[".ifb"] = DK_IFB;
4212 DirectiveKindMap[".ifnb"] = DK_IFNB;
4213 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004214 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004215 DirectiveKindMap[".ifnc"] = DK_IFNC;
4216 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4217 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4218 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4219 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4220 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004221 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004222 DirectiveKindMap[".endif"] = DK_ENDIF;
4223 DirectiveKindMap[".skip"] = DK_SKIP;
4224 DirectiveKindMap[".space"] = DK_SPACE;
4225 DirectiveKindMap[".file"] = DK_FILE;
4226 DirectiveKindMap[".line"] = DK_LINE;
4227 DirectiveKindMap[".loc"] = DK_LOC;
4228 DirectiveKindMap[".stabs"] = DK_STABS;
4229 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4230 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4231 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4232 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4233 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4234 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4235 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4236 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4237 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4238 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4239 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4240 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4241 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4242 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4243 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4244 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4245 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4246 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4247 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4248 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4249 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004250 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004251 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4252 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4253 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004254 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004255 DirectiveKindMap[".endm"] = DK_ENDM;
4256 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4257 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004258 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004259 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004260 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004261}
4262
Jim Grosbach4b905842013-09-20 23:08:21 +00004263MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004264 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004265
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004267 for (;;) {
4268 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004269 if (getLexer().is(AsmToken::Eof)) {
4270 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004271 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004272 }
4273
Rafael Espindola34b9c512012-06-03 23:57:14 +00004274 if (Lexer.is(AsmToken::Identifier) &&
4275 (getTok().getIdentifier() == ".rept")) {
4276 ++NestLevel;
4277 }
4278
4279 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004280 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004281 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004282 EndToken = getTok();
4283 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004284 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4285 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004286 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004287 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004288 break;
4289 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004290 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004291 }
4292
Rafael Espindola34b9c512012-06-03 23:57:14 +00004293 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004294 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004295 }
4296
4297 const char *BodyStart = StartToken.getLoc().getPointer();
4298 const char *BodyEnd = EndToken.getLoc().getPointer();
4299 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4300
Rafael Espindola34b9c512012-06-03 23:57:14 +00004301 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004302 MacroLikeBodies.push_back(
4303 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004304 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004305}
4306
Jim Grosbach4b905842013-09-20 23:08:21 +00004307void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004308 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309 OS << ".endr\n";
4310
Rafael Espindola3560ff22014-08-27 20:03:13 +00004311 std::unique_ptr<MemoryBuffer> Instantiation =
4312 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004313
Rafael Espindola34b9c512012-06-03 23:57:14 +00004314 // Create the macro instantiation object and add to the current macro
4315 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004316 MacroInstantiation *MI = new MacroInstantiation(
4317 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004318 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004319
Rafael Espindola34b9c512012-06-03 23:57:14 +00004320 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004321 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004322 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323 Lex();
4324}
4325
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004326/// parseDirectiveRept
4327/// ::= .rep | .rept count
4328bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004329 const MCExpr *CountExpr;
4330 SMLoc CountLoc = getTok().getLoc();
4331 if (parseExpression(CountExpr))
4332 return true;
4333
Rafael Espindola34b9c512012-06-03 23:57:14 +00004334 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004335 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4336 eatToEndOfStatement();
4337 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4338 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004339
4340 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004341 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004342
4343 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004344 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004345
4346 // Eat the end of statement.
4347 Lex();
4348
4349 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004350 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004351 if (!M)
4352 return true;
4353
4354 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4355 // to hold the macro body with substitutions.
4356 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004357 raw_svector_ostream OS(Buf);
4358 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004359 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004360 return true;
4361 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004362 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004363
4364 return false;
4365}
4366
Jim Grosbach4b905842013-09-20 23:08:21 +00004367/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004368/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004369bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004370 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004371
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004372 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004373 return TokError("expected identifier in '.irp' directive");
4374
Rafael Espindola768b41c2012-06-15 14:02:34 +00004375 if (Lexer.isNot(AsmToken::Comma))
4376 return TokError("expected comma in '.irp' directive");
4377
4378 Lex();
4379
Eli Bendersky38274122013-01-14 23:22:36 +00004380 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004381 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004382 return true;
4383
4384 // Eat the end of statement.
4385 Lex();
4386
4387 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004388 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004389 if (!M)
4390 return true;
4391
4392 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4393 // to hold the macro body with substitutions.
4394 SmallString<256> Buf;
4395 raw_svector_ostream OS(Buf);
4396
Eli Bendersky38274122013-01-14 23:22:36 +00004397 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004398 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004399 return true;
4400 }
4401
Jim Grosbach4b905842013-09-20 23:08:21 +00004402 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004403
4404 return false;
4405}
4406
Jim Grosbach4b905842013-09-20 23:08:21 +00004407/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004408/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004409bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004410 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004411
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004412 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004413 return TokError("expected identifier in '.irpc' directive");
4414
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004415 if (Lexer.isNot(AsmToken::Comma))
4416 return TokError("expected comma in '.irpc' directive");
4417
4418 Lex();
4419
Eli Bendersky38274122013-01-14 23:22:36 +00004420 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004421 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004422 return true;
4423
4424 if (A.size() != 1 || A.front().size() != 1)
4425 return TokError("unexpected token in '.irpc' directive");
4426
4427 // Eat the end of statement.
4428 Lex();
4429
4430 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004431 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004432 if (!M)
4433 return true;
4434
4435 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4436 // to hold the macro body with substitutions.
4437 SmallString<256> Buf;
4438 raw_svector_ostream OS(Buf);
4439
4440 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004441 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004442 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004443 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004444
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004445 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004446 return true;
4447 }
4448
Jim Grosbach4b905842013-09-20 23:08:21 +00004449 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004450
4451 return false;
4452}
4453
Jim Grosbach4b905842013-09-20 23:08:21 +00004454bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004455 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004456 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004457
4458 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004459 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004460 assert(getLexer().is(AsmToken::EndOfStatement));
4461
Jim Grosbach4b905842013-09-20 23:08:21 +00004462 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004463 return false;
4464}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004465
Jim Grosbach4b905842013-09-20 23:08:21 +00004466bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004467 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004468 const MCExpr *Value;
4469 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004470 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004471 return true;
4472 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4473 if (!MCE)
4474 return Error(ExprLoc, "unexpected expression in _emit");
4475 uint64_t IntValue = MCE->getValue();
4476 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4477 return Error(ExprLoc, "literal value out of range for directive");
4478
Chad Rosierc7f552c2013-02-12 21:33:51 +00004479 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4480 return false;
4481}
4482
Jim Grosbach4b905842013-09-20 23:08:21 +00004483bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004484 const MCExpr *Value;
4485 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004486 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004487 return true;
4488 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4489 if (!MCE)
4490 return Error(ExprLoc, "unexpected expression in align");
4491 uint64_t IntValue = MCE->getValue();
4492 if (!isPowerOf2_64(IntValue))
4493 return Error(ExprLoc, "literal value not a power of two greater then zero");
4494
Jim Grosbach4b905842013-09-20 23:08:21 +00004495 Info.AsmRewrites->push_back(
4496 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004497 return false;
4498}
4499
Chad Rosierf43fcf52013-02-13 21:27:17 +00004500// We are comparing pointers, but the pointers are relative to a single string.
4501// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004502static int rewritesSort(const AsmRewrite *AsmRewriteA,
4503 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004504 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4505 return -1;
4506 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4507 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004508
Chad Rosierfce4fab2013-04-08 17:43:47 +00004509 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4510 // rewrite to the same location. Make sure the SizeDirective rewrite is
4511 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4512 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4514 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004515 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004516
Jim Grosbach4b905842013-09-20 23:08:21 +00004517 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4518 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004519 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004520 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004521}
4522
Jim Grosbach4b905842013-09-20 23:08:21 +00004523bool AsmParser::parseMSInlineAsm(
4524 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4525 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4526 SmallVectorImpl<std::string> &Constraints,
4527 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4528 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004529 SmallVector<void *, 4> InputDecls;
4530 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004531 SmallVector<bool, 4> InputDeclsAddressOf;
4532 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004533 SmallVector<std::string, 4> InputConstraints;
4534 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004535 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004536
Benjamin Kramer1a136112013-02-15 20:37:21 +00004537 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004538
4539 // Prime the lexer.
4540 Lex();
4541
4542 // While we have input, parse each statement.
4543 unsigned InputIdx = 0;
4544 unsigned OutputIdx = 0;
4545 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004546 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004547 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004548 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004549
Chad Rosier149e8e02012-12-12 22:45:52 +00004550 if (Info.ParseError)
4551 return true;
4552
Benjamin Kramer1a136112013-02-15 20:37:21 +00004553 if (Info.Opcode == ~0U)
4554 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004555
Benjamin Kramer1a136112013-02-15 20:37:21 +00004556 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004557
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 // Build the list of clobbers, outputs and inputs.
4559 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004560 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004561
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004563 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004564 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004565
Benjamin Kramer1a136112013-02-15 20:37:21 +00004566 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004567 if (Operand.isReg() && !Operand.needAddressOf() &&
4568 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004569 unsigned NumDefs = Desc.getNumDefs();
4570 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004571 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4572 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004573 continue;
4574 }
4575
4576 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004577 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004578 if (SymName.empty())
4579 continue;
4580
David Blaikie960ea3f2014-06-08 16:18:35 +00004581 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004582 if (!OpDecl)
4583 continue;
4584
4585 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004586 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 if (isOutput) {
4588 ++InputIdx;
4589 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004590 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4591 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004592 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004593 } else {
4594 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004595 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4596 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004597 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004598 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004599 }
Reid Kleckneree088972013-12-10 18:27:32 +00004600
4601 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004602 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4603 Desc.getNumImplicitDefs());
4604 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004605 }
4606
4607 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004608 NumOutputs = OutputDecls.size();
4609 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004610
4611 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004612 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4613 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4614 ClobberRegs.end());
4615 Clobbers.assign(ClobberRegs.size(), std::string());
4616 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4617 raw_string_ostream OS(Clobbers[I]);
4618 IP->printRegName(OS, ClobberRegs[I]);
4619 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004620
4621 // Merge the various outputs and inputs. Output are expected first.
4622 if (NumOutputs || NumInputs) {
4623 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004624 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004625 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004626 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004627 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004628 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004629 }
4630 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004631 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004632 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004633 }
4634 }
4635
4636 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004637 std::string AsmStringIR;
4638 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004639 StringRef ASMString =
4640 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4641 const char *AsmStart = ASMString.begin();
4642 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004643 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004644 for (const AsmRewrite &AR : AsmStrRewrites) {
4645 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004646 if (Kind == AOK_Delete)
4647 continue;
4648
David Majnemer8114c1a2014-06-23 02:17:16 +00004649 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004650 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004651
Chad Rosier120eefd2013-03-19 17:32:17 +00004652 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004653 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004654 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004655
Chad Rosier37e755c2012-10-23 17:43:43 +00004656 // Skip the original expression.
4657 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004658 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004659 continue;
4660 }
4661
Chad Rosierff10ed12013-04-12 16:26:42 +00004662 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004663 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004664 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004665 default:
4666 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004667 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004668 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004669 break;
4670 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004671 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004672 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004673 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004674 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004675 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004676 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004677 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004678 break;
4679 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004680 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004681 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004682 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004683 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004684 default: break;
4685 case 8: OS << "byte ptr "; break;
4686 case 16: OS << "word ptr "; break;
4687 case 32: OS << "dword ptr "; break;
4688 case 64: OS << "qword ptr "; break;
4689 case 80: OS << "xword ptr "; break;
4690 case 128: OS << "xmmword ptr "; break;
4691 case 256: OS << "ymmword ptr "; break;
4692 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004693 break;
4694 case AOK_Emit:
4695 OS << ".byte";
4696 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004697 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004698 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004699 OS << ".align " << Val;
4700
4701 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004702 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004703 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4704 break;
4705 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004706 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004707 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004708 OS.flush();
4709 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004710 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004711 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004712 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004714
Chad Rosier8bce6642012-10-18 15:49:34 +00004715 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004716 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004717 }
4718
4719 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004720 if (AsmStart != AsmEnd)
4721 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004722
4723 AsmString = OS.str();
4724 return false;
4725}
4726
Daniel Dunbar01e36072010-07-17 02:26:10 +00004727/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004728MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4729 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004730 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004731}