blob: 6f6b28530aad6bf42e3a851f7d9c4623055b5c46 [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 {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000114 AsmParser(const AsmParser &) = delete;
115 void operator=(const AsmParser &) = delete;
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
Toma Tabacu217116e2015-04-27 10:50:29 +0000150 /// \brief Keeps track of how many .macro's have been instantiated.
151 unsigned NumOfMacroInstantiations;
152
Daniel Dunbar43325c42010-09-09 22:42:56 +0000153 /// Flag tracking whether any errors have been encountered.
154 unsigned HadError : 1;
155
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000156 /// The values from the last parsed cpp hash file line comment if any.
157 StringRef CppHashFilename;
158 int64_t CppHashLineNumber;
159 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000160 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// When generating dwarf for assembly source files we need to calculate the
162 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000163 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
165 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000166 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000168
Devang Patela173ee52012-01-31 18:14:05 +0000169 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
170 unsigned AssemblerDialect;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000173 bool IsDarwin;
174
Jim Grosbach4b905842013-09-20 23:08:21 +0000175 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000176 bool ParsingInlineAsm;
177
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000178public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000179 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000181 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182
Craig Topper59be68f2014-03-08 07:14:16 +0000183 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 void addDirectiveHandler(StringRef Directive,
186 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000187 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188 }
189
Toma Tabacu11e14a92015-04-21 11:50:52 +0000190 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
191 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
192 }
193
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194public:
195 /// @name MCAsmParser Interface
196 /// {
197
Craig Topper59be68f2014-03-08 07:14:16 +0000198 SourceMgr &getSourceManager() override { return SrcMgr; }
199 MCAsmLexer &getLexer() override { return Lexer; }
200 MCContext &getContext() override { return Ctx; }
201 MCStreamer &getStreamer() override { return Out; }
202 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000203 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000204 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000205 else
206 return AssemblerDialect;
207 }
Craig Topper59be68f2014-03-08 07:14:16 +0000208 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000209 AssemblerDialect = i;
210 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 void Note(SMLoc L, const Twine &Msg,
213 ArrayRef<SMRange> Ranges = None) override;
214 bool Warning(SMLoc L, const Twine &Msg,
215 ArrayRef<SMRange> Ranges = None) override;
216 bool Error(SMLoc L, const Twine &Msg,
217 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper59be68f2014-03-08 07:14:16 +0000219 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper59be68f2014-03-08 07:14:16 +0000221 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
222 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000223
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000224 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000226 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000229 const MCInstrInfo *MII, const MCInstPrinter *IP,
230 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000231
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
234 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237
Jim Grosbach4b905842013-09-20 23:08:21 +0000238 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000239 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseIdentifier(StringRef &Res) override;
241 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000242
Craig Topper59be68f2014-03-08 07:14:16 +0000243 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244 /// }
245
246private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000247
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000248 bool parseStatement(ParseStatementInfo &Info,
249 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000257 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000272 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000313 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000329 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
330 MCBinaryExpr::Opcode &Kind);
331
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
334 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000337
Eli Bendersky17233942013-01-15 22:59:42 +0000338 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000339 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_NO_DIRECTIVE, // Placeholder
341 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000342 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
343 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000344 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000346 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
348 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
349 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
350 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000351 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000352 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
353 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000354 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
355 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
356 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
357 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
358 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
359 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000360 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000361 DK_MACROS_ON, DK_MACROS_OFF,
362 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000363 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000364 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366 };
367
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000369 /// directives parsed by this class.
370 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371
372 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
374 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000375 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
377 bool parseDirectiveFill(); // ".fill"
378 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000379 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
381 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000382 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384
Eli Bendersky17233942013-01-15 22:59:42 +0000385 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveFile(SMLoc DirectiveLoc);
387 bool parseDirectiveLine();
388 bool parseDirectiveLoc();
389 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000390
391 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000393 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFISections();
395 bool parseDirectiveCFIStartProc();
396 bool parseDirectiveCFIEndProc();
397 bool parseDirectiveCFIDefCfaOffset();
398 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIAdjustCfaOffset();
400 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
404 bool parseDirectiveCFIRememberState();
405 bool parseDirectiveCFIRestoreState();
406 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIEscape();
409 bool parseDirectiveCFISignalFrame();
410 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
412 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000414 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveEndMacro(StringRef Directive);
416 bool parseDirectiveMacro(SMLoc DirectiveLoc);
417 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000421 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000423 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000425
Eli Bendersky17233942013-01-15 22:59:42 +0000426 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000428
429 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000433 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000437
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveAbort(); // ".abort"
439 bool parseDirectiveInclude(); // ".include"
440 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000441
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000442 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
443 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000444 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000446 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000448 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
449 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000450 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
452 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
453 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
454 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000455 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000456
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000458 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000459
Rafael Espindola34b9c512012-06-03 23:57:14 +0000460 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
462 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000463 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000464 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
466 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
467 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000468
Chad Rosierc7f552c2013-02-12 21:33:51 +0000469 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000471 size_t Len);
472
473 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000474 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000475
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000476 // "end"
477 bool parseDirectiveEnd(SMLoc DirectiveLoc);
478
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000479 // ".err" or ".error"
480 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000481
Nico Weber404012b2014-07-24 16:26:06 +0000482 // ".warning"
483 bool parseDirectiveWarning(SMLoc DirectiveLoc);
484
Eli Bendersky17233942013-01-15 22:59:42 +0000485 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000486};
Daniel Dunbar86033402010-07-12 17:54:38 +0000487}
488
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000489namespace llvm {
490
491extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000492extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000493extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000494
495}
496
Chris Lattnerc35681b2010-01-19 19:46:13 +0000497enum { DEFAULT_ADDRSPACE = 0 };
498
David Blaikie9f380a32015-03-16 18:06:57 +0000499AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
500 const MCAsmInfo &MAI)
501 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
502 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000503 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000504 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000505 // Save the old handler.
506 SavedDiagHandler = SrcMgr.getDiagHandler();
507 SavedDiagContext = SrcMgr.getDiagContext();
508 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000509 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000510 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000511
Daniel Dunbarc5011082010-07-12 18:12:02 +0000512 // Initialize the platform / file format parser.
David Blaikie9f380a32015-03-16 18:06:57 +0000513 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +0000514 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000515 PlatformParser.reset(createCOFFAsmParser());
516 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000517 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000518 PlatformParser.reset(createDarwinAsmParser());
519 IsDarwin = true;
520 break;
Rafael Espindolae28610d2013-12-09 20:26:40 +0000521 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000522 PlatformParser.reset(createELFAsmParser());
523 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000524 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000525
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000526 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000527 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000528
529 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000530}
531
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000532AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000533 assert((HadError || ActiveMacros.empty()) &&
534 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000553 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000569 unsigned NewBuf =
570 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
571 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000573
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 return false;
577}
Daniel Dunbar43235712010-07-18 18:54:11 +0000578
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000579/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000580/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000582bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000584 unsigned NewBuf =
585 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Alp Tokera55b95b2014-07-06 10:33:31 +0000594void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
595 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
597 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000598}
599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600const AsmToken &AsmParser::Lex() {
601 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603 if (tok->is(AsmToken::Eof)) {
604 // If this is the end of an included file, pop the parent file off the
605 // include stack.
606 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
607 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000608 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 tok = &Lexer.Lex();
610 }
611 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000614 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617}
618
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000619bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000620 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000621 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000622 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000623
Chris Lattner36e02122009-06-21 20:54:55 +0000624 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000625 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000626
627 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000628 AsmCond StartingCondState = TheCondState;
629
Kevin Enderby6469fc22011-11-01 22:27:22 +0000630 // If we are generating dwarf for assembly source files save the initial text
631 // section and generate a .file directive.
632 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
634 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000635 auto InsertResult = getContext().addGenDwarfSection(
636 getStreamer().getCurrentSection().first);
637 assert(InsertResult.second && ".text section should not have debug info yet");
638 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000639 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
640 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000641 }
642
Chris Lattner73f36112009-07-02 21:53:43 +0000643 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000644 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000645 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000646 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000647 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000648
Daniel Dunbar43325c42010-09-09 22:42:56 +0000649 // We had an error, validate that one was emitted and recover by skipping to
650 // the next line.
651 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000652 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000653 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000654
655 if (TheCondState.TheCond != StartingCondState.TheCond ||
656 TheCondState.Ignore != StartingCondState.Ignore)
657 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000658
659 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000660 const auto &LineTables = getContext().getMCDwarfLineTables();
661 if (!LineTables.empty()) {
662 unsigned Index = 0;
663 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
664 if (File.Name.empty() && Index != 0)
665 TokError("unassigned file number: " + Twine(Index) +
666 " for .file directives");
667 ++Index;
668 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000670
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 // Check to see that all assembler local symbols were actually defined.
672 // Targets that don't do subsections via symbols may not want this, though,
673 // so conservatively exclude them. Only do this if we're finalizing, though,
674 // as otherwise we won't necessarilly have seen everything yet.
675 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
676 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
677 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 i != e; ++i) {
680 MCSymbol *Sym = i->getValue();
681 // Variable symbols may not be marked as defined, so check those
682 // explicitly. If we know it's a variable, we have a definition for
683 // the purposes of this check.
684 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
685 // FIXME: We would really like to refer back to where the symbol was
686 // first referenced for a source location. We need to add something
687 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000688 printMessage(
689 getLexer().getLoc(), SourceMgr::DK_Error,
690 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000691 }
692 }
693
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000694 // Finalize the output stream if there are no errors and if the client wants
695 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000696 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000697 Out.Finish();
698
Chris Lattner73f36112009-07-02 21:53:43 +0000699 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000700}
701
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000703 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000705 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000706 }
707}
708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000711 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000712 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000713
Chris Lattnere5074c42009-06-22 01:29:09 +0000714 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000715 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000716 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000717}
718
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000719StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 const char *Start = getTok().getLoc().getPointer();
721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 Lex();
724
725 const char *End = getTok().getLoc().getPointer();
726 return StringRef(Start, End - Start);
727}
Chris Lattner78db3622009-06-22 05:51:26 +0000728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000730 const char *Start = getTok().getLoc().getPointer();
731
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000733 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
739
Jim Grosbach4b905842013-09-20 23:08:21 +0000740/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741/// NOTE: This assumes the leading '(' has already been consumed.
742///
743/// parenexpr ::= expr)
744///
Jim Grosbach4b905842013-09-20 23:08:21 +0000745bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
746 if (parseExpression(Res))
747 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000748 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000750 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000751 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return false;
753}
Chris Lattner78db3622009-06-22 05:51:26 +0000754
Jim Grosbach4b905842013-09-20 23:08:21 +0000755/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756/// NOTE: This assumes the leading '[' has already been consumed.
757///
758/// bracketexpr ::= expr]
759///
Jim Grosbach4b905842013-09-20 23:08:21 +0000760bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
761 if (parseExpression(Res))
762 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 if (Lexer.isNot(AsmToken::RBrac))
764 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000765 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 Lex();
767 return false;
768}
769
Jim Grosbach4b905842013-09-20 23:08:21 +0000770/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000771/// primaryexpr ::= (parenexpr
772/// primaryexpr ::= symbol
773/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000774/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000775/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000776bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000777 SMLoc FirstTokenLoc = getLexer().getLoc();
778 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
779 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000780 default:
781 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000782 // If we have an error assume that we've already handled it.
783 case AsmToken::Error:
784 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000785 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000786 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000787 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000789 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000790 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000791 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000792 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000793 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000794 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000795 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000796 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 if (FirstTokenKind == AsmToken::Dollar) {
798 if (Lexer.getMAI().getDollarIsPC()) {
799 // This is a '$' reference, which references the current PC. Emit a
800 // temporary label to the streamer and refer to it.
801 MCSymbol *Sym = Ctx.CreateTempSymbol();
802 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000803 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
804 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 EndLoc = FirstTokenLoc;
806 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000807 }
808 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000810 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000811 // Parse symbol variant
812 std::pair<StringRef, StringRef> Split;
813 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000814 if (FirstTokenKind == AsmToken::String) {
815 if (Lexer.is(AsmToken::At)) {
816 Lexer.Lex(); // eat @
817 SMLoc AtLoc = getLexer().getLoc();
818 StringRef VName;
819 if (parseIdentifier(VName))
820 return Error(AtLoc, "expected symbol variant after '@'");
821
822 Split = std::make_pair(Identifier, VName);
823 }
824 } else {
825 Split = Identifier.split('@');
826 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000827 } else if (Lexer.is(AsmToken::LParen)) {
828 Lexer.Lex(); // eat (
829 StringRef VName;
830 parseIdentifier(VName);
831 if (Lexer.isNot(AsmToken::RParen)) {
832 return Error(Lexer.getTok().getLoc(),
833 "unexpected token in variant, expected ')'");
834 }
835 Lexer.Lex(); // eat )
836 Split = std::make_pair(Identifier, VName);
837 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000838
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000839 EndLoc = SMLoc::getFromPointer(Identifier.end());
840
Daniel Dunbard20cda02009-10-16 01:34:54 +0000841 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000842 StringRef SymbolName = Identifier;
843 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000844
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000845 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000846 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000847 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000848 if (Variant != MCSymbolRefExpr::VK_Invalid) {
849 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000850 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000851 Variant = MCSymbolRefExpr::VK_None;
852 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000853 return Error(SMLoc::getFromPointer(Split.second.begin()),
854 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000855 }
856 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000857
Hans Wennborgce69d772013-10-18 20:46:28 +0000858 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
859
Daniel Dunbard20cda02009-10-16 01:34:54 +0000860 // If this is an absolute variable reference, substitute it now to preserve
861 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000862 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000863 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000864 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000865
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000866 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000867 return false;
868 }
869
870 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000871 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000872 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000873 }
David Woodhousef42a6662014-02-01 16:20:54 +0000874 case AsmToken::BigNum:
875 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000876 case AsmToken::Integer: {
877 SMLoc Loc = getTok().getLoc();
878 int64_t IntVal = getTok().getIntVal();
879 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000880 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000881 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000882 // Look for 'b' or 'f' following an Integer as a directional label
883 if (Lexer.getKind() == AsmToken::Identifier) {
884 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 // Lookup the symbol variant if used.
886 std::pair<StringRef, StringRef> Split = IDVal.split('@');
887 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
888 if (Split.first.size() != IDVal.size()) {
889 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000890 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000891 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000892 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000893 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000894 if (IDVal == "f" || IDVal == "b") {
895 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000896 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000897 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000898 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000899 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000900 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000901 Lex(); // Eat identifier.
902 }
903 }
Chris Lattner78db3622009-06-22 05:51:26 +0000904 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000905 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000906 case AsmToken::Real: {
907 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000908 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000910 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000911 Lex(); // Eat token.
912 return false;
913 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000914 case AsmToken::Dot: {
915 // This is a '.' reference, which references the current PC. Emit a
916 // temporary label to the streamer and refer to it.
917 MCSymbol *Sym = Ctx.CreateTempSymbol();
918 Out.EmitLabel(Sym);
919 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000920 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000921 Lex(); // Eat identifier.
922 return false;
923 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000927 case AsmToken::LBrac:
928 if (!PlatformParser->HasBracketExpressions())
929 return TokError("brackets expression not supported on this target");
930 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Minus:
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::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000938 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000939 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000940 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000942 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000943 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000944 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000945 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000946 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000947 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000948 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000949 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000950 }
951}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000952
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000953bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000954 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000955 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000956}
957
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000959AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000960 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000961 // Ask the target implementation about this expression first.
962 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
963 if (NewE)
964 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965 // Recurse over the given expression, rebuilding it to apply the given variant
966 // if there is exactly one symbol.
967 switch (E->getKind()) {
968 case MCExpr::Target:
969 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000970 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000971
972 case MCExpr::SymbolRef: {
973 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
974
975 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000976 TokError("invalid variant on expression '" + getTok().getIdentifier() +
977 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000978 return E;
979 }
980
981 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
982 }
983
984 case MCExpr::Unary: {
985 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000988 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
990 }
991
992 case MCExpr::Binary: {
993 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000994 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
995 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
997 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000998 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999
Jim Grosbach4b905842013-09-20 23:08:21 +00001000 if (!LHS)
1001 LHS = BE->getLHS();
1002 if (!RHS)
1003 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004
1005 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1006 }
1007 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001008
Craig Toppera2886c22012-02-07 05:05:23 +00001009 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001010}
1011
Jim Grosbach4b905842013-09-20 23:08:21 +00001012/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001013///
Jim Grosbachbd164242011-08-20 16:24:13 +00001014/// expr ::= expr &&,|| expr -> lowest.
1015/// expr ::= expr |,^,&,! expr
1016/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1017/// expr ::= expr <<,>> expr
1018/// expr ::= expr +,- expr
1019/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001020/// expr ::= primaryexpr
1021///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001022bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001023 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001024 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001025 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001026 return true;
1027
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 // As a special case, we support 'a op b @ modifier' by rewriting the
1029 // expression to include the modifier. This is inefficient, but in general we
1030 // expect users to use 'a@modifier op b'.
1031 if (Lexer.getKind() == AsmToken::At) {
1032 Lex();
1033
1034 if (Lexer.isNot(AsmToken::Identifier))
1035 return TokError("unexpected symbol modifier following '@'");
1036
1037 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001038 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039 if (Variant == MCSymbolRefExpr::VK_Invalid)
1040 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1041
Jim Grosbach4b905842013-09-20 23:08:21 +00001042 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001043 if (!ModifiedRes) {
1044 return TokError("invalid modifier '" + getTok().getIdentifier() +
1045 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001047
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 Res = ModifiedRes;
1049 Lex();
1050 }
1051
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001052 // Try to constant fold it up front, if possible.
1053 int64_t Value;
1054 if (Res->EvaluateAsAbsolute(Value))
1055 Res = MCConstantExpr::Create(Value, getContext());
1056
1057 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001058}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001059
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001060bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001061 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001062 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001063}
1064
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001065bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001066 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001067
Daniel Dunbar75630b32009-06-30 02:10:03 +00001068 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001069 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001070 return true;
1071
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001072 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001073 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001074
1075 return false;
1076}
1077
Ahmed Bougacha457852f2015-04-28 00:17:39 +00001078unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1079 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001080 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001081 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001082 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001085 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001087 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001088 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001090 return 1;
1091
Jim Grosbach4b905842013-09-20 23:08:21 +00001092 // Low Precedence: |, &, ^
1093 //
1094 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001095 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001104
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001106 case AsmToken::EqualEqual:
1107 Kind = MCBinaryExpr::EQ;
1108 return 3;
1109 case AsmToken::ExclaimEqual:
1110 case AsmToken::LessGreater:
1111 Kind = MCBinaryExpr::NE;
1112 return 3;
1113 case AsmToken::Less:
1114 Kind = MCBinaryExpr::LT;
1115 return 3;
1116 case AsmToken::LessEqual:
1117 Kind = MCBinaryExpr::LTE;
1118 return 3;
1119 case AsmToken::Greater:
1120 Kind = MCBinaryExpr::GT;
1121 return 3;
1122 case AsmToken::GreaterEqual:
1123 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001124 return 3;
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001127 case AsmToken::LessLess:
1128 Kind = MCBinaryExpr::Shl;
1129 return 4;
1130 case AsmToken::GreaterGreater:
1131 Kind = MCBinaryExpr::Shr;
1132 return 4;
1133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001135 case AsmToken::Plus:
1136 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138 case AsmToken::Minus:
1139 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001141
Jim Grosbach4b905842013-09-20 23:08:21 +00001142 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001143 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001144 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001149 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001150 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001151 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 }
1153}
1154
Jim Grosbach4b905842013-09-20 23:08:21 +00001155/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001157bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001158 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001160 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // If the next token is lower precedence than we are allowed to eat, return
1164 // successfully with what we ate already.
1165 if (TokPrec < Precedence)
1166 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Sean Callanan686ed8d2010-01-19 20:22:31 +00001168 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001169
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001171 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001172 if (parsePrimaryExpr(RHS, EndLoc))
1173 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001174
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001175 // If BinOp binds less tightly with RHS than the operator after RHS, let
1176 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001177 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001178 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001179 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1180 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001181
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001182 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001183 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001184 }
1185}
1186
Chris Lattner36e02122009-06-21 20:54:55 +00001187/// ParseStatement:
1188/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001189/// ::= Label* Directive ...Operands... EndOfStatement
1190/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001191bool AsmParser::parseStatement(ParseStatementInfo &Info,
1192 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001193 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001194 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001195 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001196 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001197 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001198
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001199 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001200 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001201 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001202 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001203 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001204 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001205 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001206 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001207
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001208 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 if (Lexer.is(AsmToken::Integer)) {
1210 LocalLabelVal = getTok().getIntVal();
1211 if (LocalLabelVal < 0) {
1212 if (!TheCondState.Ignore)
1213 return TokError("unexpected token at start of statement");
1214 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001215 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001216 IDVal = getTok().getString();
1217 Lex(); // Consume the integer token to be used as an identifier token.
1218 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001219 if (!TheCondState.Ignore)
1220 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001221 }
1222 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001223 } else if (Lexer.is(AsmToken::Dot)) {
1224 // Treat '.' as a valid identifier in this context.
1225 Lex();
1226 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001227 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001228 if (!TheCondState.Ignore)
1229 return TokError("unexpected token at start of statement");
1230 IDVal = "";
1231 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001232
Chris Lattner926885c2010-04-17 18:14:27 +00001233 // Handle conditional assembly here before checking for skipping. We
1234 // have to do this so that .endif isn't skipped in a ".if 0" block for
1235 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001236 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001237 DirectiveKindMap.find(IDVal);
1238 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1239 ? DK_NO_DIRECTIVE
1240 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001241 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001242 default:
1243 break;
1244 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001245 case DK_IFEQ:
1246 case DK_IFGE:
1247 case DK_IFGT:
1248 case DK_IFLE:
1249 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001250 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001251 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001252 case DK_IFB:
1253 return parseDirectiveIfb(IDLoc, true);
1254 case DK_IFNB:
1255 return parseDirectiveIfb(IDLoc, false);
1256 case DK_IFC:
1257 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001258 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001259 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001260 case DK_IFNC:
1261 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001262 case DK_IFNES:
1263 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001264 case DK_IFDEF:
1265 return parseDirectiveIfdef(IDLoc, true);
1266 case DK_IFNDEF:
1267 case DK_IFNOTDEF:
1268 return parseDirectiveIfdef(IDLoc, false);
1269 case DK_ELSEIF:
1270 return parseDirectiveElseIf(IDLoc);
1271 case DK_ELSE:
1272 return parseDirectiveElse(IDLoc);
1273 case DK_ENDIF:
1274 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001275 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001276
Eli Bendersky88024712013-01-16 19:32:36 +00001277 // Ignore the statement if in the middle of inactive conditional
1278 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001279 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001280 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001281 return false;
1282 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001283
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001284 // FIXME: Recurse on local labels?
1285
1286 // See what kind of statement we have.
1287 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001288 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001289 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001290
Chris Lattner36e02122009-06-21 20:54:55 +00001291 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001292 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001293
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001294 // Diagnose attempt to use '.' as a label.
1295 if (IDVal == ".")
1296 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1297
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001298 // Diagnose attempt to use a variable as a label.
1299 //
1300 // FIXME: Diagnostics. Note the location of the definition as a label.
1301 // FIXME: This doesn't diagnose assignment to a symbol which has been
1302 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001303 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001304 if (LocalLabelVal == -1) {
1305 if (ParsingInlineAsm && SI) {
1306 StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1307 assert(RewrittenLabel.size() && "We should have an internal name here.");
1308 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1309 IDVal.size(), RewrittenLabel));
1310 IDVal = RewrittenLabel;
1311 }
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001312 Sym = getContext().GetOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001313 } else
Kevin Enderby0510b482010-05-17 23:08:19 +00001314 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001315
1316 Sym->redefineIfPossible();
1317
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001318 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001319 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001320
Daniel Dunbare73b2672009-08-26 22:13:22 +00001321 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001322 if (!ParsingInlineAsm)
1323 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001324
Kevin Enderbye7739d42011-12-09 18:09:40 +00001325 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001326 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001327 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001328 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1329 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001330
Tim Northover1744d0a2013-10-25 12:49:50 +00001331 getTargetParser().onLabelParsed(Sym);
1332
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001333 // Consume any end of statement token, if present, to avoid spurious
1334 // AddBlankLine calls().
1335 if (Lexer.is(AsmToken::EndOfStatement)) {
1336 Lex();
1337 if (Lexer.is(AsmToken::Eof))
1338 return false;
1339 }
1340
Eli Friedman0f4871d2012-10-22 23:58:19 +00001341 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001342 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001343
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001344 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001345 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001346 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001347
Jim Grosbach4b905842013-09-20 23:08:21 +00001348 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001349
1350 default: // Normal instruction or directive.
1351 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001352 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001353
1354 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001355 if (areMacrosEnabled())
1356 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1357 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001358 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001359
Michael J. Spencer530ce852010-10-09 11:00:50 +00001360 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001361
Eli Bendersky17233942013-01-15 22:59:42 +00001362 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001363 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001364 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001365 //
Eli Bendersky17233942013-01-15 22:59:42 +00001366 // 1. The target-specific assembly parser. Some directives are target
1367 // specific or may potentially behave differently on certain targets.
1368 // 2. Asm parser extensions. For example, platform-specific parsers
1369 // (like the ELF parser) register themselves as extensions.
1370 // 3. The generic directive parser implemented by this class. These are
1371 // all the directives that behave in a target and platform independent
1372 // manner, or at least have a default behavior that's shared between
1373 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001374
Eli Bendersky17233942013-01-15 22:59:42 +00001375 // First query the target-specific parser. It will return 'true' if it
1376 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001377 if (!getTargetParser().ParseDirective(ID))
1378 return false;
1379
Alp Tokercb402912014-01-24 17:20:08 +00001380 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001381 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001382 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1383 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001384 if (Handler.first)
1385 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1386
1387 // Finally, if no one else is interested in this directive, it must be
1388 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001389 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001390 default:
1391 break;
1392 case DK_SET:
1393 case DK_EQU:
1394 return parseDirectiveSet(IDVal, true);
1395 case DK_EQUIV:
1396 return parseDirectiveSet(IDVal, false);
1397 case DK_ASCII:
1398 return parseDirectiveAscii(IDVal, false);
1399 case DK_ASCIZ:
1400 case DK_STRING:
1401 return parseDirectiveAscii(IDVal, true);
1402 case DK_BYTE:
1403 return parseDirectiveValue(1);
1404 case DK_SHORT:
1405 case DK_VALUE:
1406 case DK_2BYTE:
1407 return parseDirectiveValue(2);
1408 case DK_LONG:
1409 case DK_INT:
1410 case DK_4BYTE:
1411 return parseDirectiveValue(4);
1412 case DK_QUAD:
1413 case DK_8BYTE:
1414 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001415 case DK_OCTA:
1416 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001417 case DK_SINGLE:
1418 case DK_FLOAT:
1419 return parseDirectiveRealValue(APFloat::IEEEsingle);
1420 case DK_DOUBLE:
1421 return parseDirectiveRealValue(APFloat::IEEEdouble);
1422 case DK_ALIGN: {
1423 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1424 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1425 }
1426 case DK_ALIGN32: {
1427 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1428 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1429 }
1430 case DK_BALIGN:
1431 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1432 case DK_BALIGNW:
1433 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1434 case DK_BALIGNL:
1435 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1436 case DK_P2ALIGN:
1437 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1438 case DK_P2ALIGNW:
1439 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1440 case DK_P2ALIGNL:
1441 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1442 case DK_ORG:
1443 return parseDirectiveOrg();
1444 case DK_FILL:
1445 return parseDirectiveFill();
1446 case DK_ZERO:
1447 return parseDirectiveZero();
1448 case DK_EXTERN:
1449 eatToEndOfStatement(); // .extern is the default, ignore it.
1450 return false;
1451 case DK_GLOBL:
1452 case DK_GLOBAL:
1453 return parseDirectiveSymbolAttribute(MCSA_Global);
1454 case DK_LAZY_REFERENCE:
1455 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1456 case DK_NO_DEAD_STRIP:
1457 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1458 case DK_SYMBOL_RESOLVER:
1459 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1460 case DK_PRIVATE_EXTERN:
1461 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1462 case DK_REFERENCE:
1463 return parseDirectiveSymbolAttribute(MCSA_Reference);
1464 case DK_WEAK_DEFINITION:
1465 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1466 case DK_WEAK_REFERENCE:
1467 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1468 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1469 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1470 case DK_COMM:
1471 case DK_COMMON:
1472 return parseDirectiveComm(/*IsLocal=*/false);
1473 case DK_LCOMM:
1474 return parseDirectiveComm(/*IsLocal=*/true);
1475 case DK_ABORT:
1476 return parseDirectiveAbort();
1477 case DK_INCLUDE:
1478 return parseDirectiveInclude();
1479 case DK_INCBIN:
1480 return parseDirectiveIncbin();
1481 case DK_CODE16:
1482 case DK_CODE16GCC:
1483 return TokError(Twine(IDVal) + " not supported yet");
1484 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001485 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001486 case DK_IRP:
1487 return parseDirectiveIrp(IDLoc);
1488 case DK_IRPC:
1489 return parseDirectiveIrpc(IDLoc);
1490 case DK_ENDR:
1491 return parseDirectiveEndr(IDLoc);
1492 case DK_BUNDLE_ALIGN_MODE:
1493 return parseDirectiveBundleAlignMode();
1494 case DK_BUNDLE_LOCK:
1495 return parseDirectiveBundleLock();
1496 case DK_BUNDLE_UNLOCK:
1497 return parseDirectiveBundleUnlock();
1498 case DK_SLEB128:
1499 return parseDirectiveLEB128(true);
1500 case DK_ULEB128:
1501 return parseDirectiveLEB128(false);
1502 case DK_SPACE:
1503 case DK_SKIP:
1504 return parseDirectiveSpace(IDVal);
1505 case DK_FILE:
1506 return parseDirectiveFile(IDLoc);
1507 case DK_LINE:
1508 return parseDirectiveLine();
1509 case DK_LOC:
1510 return parseDirectiveLoc();
1511 case DK_STABS:
1512 return parseDirectiveStabs();
1513 case DK_CFI_SECTIONS:
1514 return parseDirectiveCFISections();
1515 case DK_CFI_STARTPROC:
1516 return parseDirectiveCFIStartProc();
1517 case DK_CFI_ENDPROC:
1518 return parseDirectiveCFIEndProc();
1519 case DK_CFI_DEF_CFA:
1520 return parseDirectiveCFIDefCfa(IDLoc);
1521 case DK_CFI_DEF_CFA_OFFSET:
1522 return parseDirectiveCFIDefCfaOffset();
1523 case DK_CFI_ADJUST_CFA_OFFSET:
1524 return parseDirectiveCFIAdjustCfaOffset();
1525 case DK_CFI_DEF_CFA_REGISTER:
1526 return parseDirectiveCFIDefCfaRegister(IDLoc);
1527 case DK_CFI_OFFSET:
1528 return parseDirectiveCFIOffset(IDLoc);
1529 case DK_CFI_REL_OFFSET:
1530 return parseDirectiveCFIRelOffset(IDLoc);
1531 case DK_CFI_PERSONALITY:
1532 return parseDirectiveCFIPersonalityOrLsda(true);
1533 case DK_CFI_LSDA:
1534 return parseDirectiveCFIPersonalityOrLsda(false);
1535 case DK_CFI_REMEMBER_STATE:
1536 return parseDirectiveCFIRememberState();
1537 case DK_CFI_RESTORE_STATE:
1538 return parseDirectiveCFIRestoreState();
1539 case DK_CFI_SAME_VALUE:
1540 return parseDirectiveCFISameValue(IDLoc);
1541 case DK_CFI_RESTORE:
1542 return parseDirectiveCFIRestore(IDLoc);
1543 case DK_CFI_ESCAPE:
1544 return parseDirectiveCFIEscape();
1545 case DK_CFI_SIGNAL_FRAME:
1546 return parseDirectiveCFISignalFrame();
1547 case DK_CFI_UNDEFINED:
1548 return parseDirectiveCFIUndefined(IDLoc);
1549 case DK_CFI_REGISTER:
1550 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001551 case DK_CFI_WINDOW_SAVE:
1552 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001553 case DK_MACROS_ON:
1554 case DK_MACROS_OFF:
1555 return parseDirectiveMacrosOnOff(IDVal);
1556 case DK_MACRO:
1557 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001558 case DK_EXITM:
1559 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001560 case DK_ENDM:
1561 case DK_ENDMACRO:
1562 return parseDirectiveEndMacro(IDVal);
1563 case DK_PURGEM:
1564 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001565 case DK_END:
1566 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001567 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001568 return parseDirectiveError(IDLoc, false);
1569 case DK_ERROR:
1570 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001571 case DK_WARNING:
1572 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001573 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001574
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001575 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001576 }
Chris Lattner36e02122009-06-21 20:54:55 +00001577
Chad Rosierc7f552c2013-02-12 21:33:51 +00001578 // __asm _emit or __asm __emit
1579 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1580 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001582
1583 // __asm align
1584 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001586
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001587 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001588
Chris Lattner7cbfa442010-05-19 23:34:33 +00001589 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001590 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001591 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001592 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001593 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001594 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001595
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001596 // Dump the parsed representation, if requested.
1597 if (getShowParsedOperands()) {
1598 SmallString<256> Str;
1599 raw_svector_ostream OS(Str);
1600 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001601 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001602 if (i != 0)
1603 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001604 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001605 }
1606 OS << "]";
1607
Jim Grosbach4b905842013-09-20 23:08:21 +00001608 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001609 }
1610
Oliver Stannard8b273082014-06-19 15:52:37 +00001611 // If we are generating dwarf for the current section then generate a .loc
1612 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001613 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001614 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001615 getStreamer().getCurrentSection().first)) {
1616 unsigned Line;
1617 if (ActiveMacros.empty())
1618 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1619 else
1620 Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
1621 ActiveMacros.back()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001622
Eli Bendersky88024712013-01-16 19:32:36 +00001623 // If we previously parsed a cpp hash file line comment then make sure the
1624 // current Dwarf File is for the CppHashFilename if not then emit the
1625 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001626 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001627 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1628 0, StringRef(), CppHashFilename);
1629 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001630
Jim Grosbach4b905842013-09-20 23:08:21 +00001631 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1632 // cache with the different Loc from the call above we save the last
1633 // info we queried here with SrcMgr.FindLineNumber().
1634 unsigned CppHashLocLineNo;
1635 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1636 CppHashLocLineNo = LastQueryLine;
1637 else {
1638 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1639 LastQueryLine = CppHashLocLineNo;
1640 LastQueryIDLoc = CppHashLoc;
1641 LastQueryBuffer = CppHashBuf;
1642 }
1643 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001644 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001645
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 getStreamer().EmitDwarfLocDirective(
1647 getContext().getGenDwarfFileNumber(), Line, 0,
1648 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1649 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001650 }
1651
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001652 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001653 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001654 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001655 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1656 Info.ParsedOperands, Out,
1657 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001658 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001659
Chris Lattnera2a9d162010-09-11 16:18:25 +00001660 // Don't skip the rest of the line, the instruction parser is responsible for
1661 // that.
1662 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001663}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001664
Jim Grosbach4b905842013-09-20 23:08:21 +00001665/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001666/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001667void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001668 if (!Lexer.is(AsmToken::EndOfStatement))
1669 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 // Eat EOL.
1671 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001672}
1673
Jim Grosbach4b905842013-09-20 23:08:21 +00001674/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001675/// ::= # number "filename"
1676/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001677bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001678 Lex(); // Eat the hash token.
1679
1680 if (getLexer().isNot(AsmToken::Integer)) {
1681 // Consume the line since in cases it is not a well-formed line directive,
1682 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001684 return false;
1685 }
1686
1687 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001688 Lex();
1689
1690 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001691 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001692 return false;
1693 }
1694
1695 StringRef Filename = getTok().getString();
1696 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001697 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001698
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1700 CppHashLoc = L;
1701 CppHashFilename = Filename;
1702 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001703 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001704
1705 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001706 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001707 return false;
1708}
1709
Jim Grosbach4b905842013-09-20 23:08:21 +00001710/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711/// for the Filename and LineNo if any in the diagnostic.
1712void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001713 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714 raw_ostream &OS = errs();
1715
1716 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1717 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001718 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1719 unsigned CppHashBuf =
1720 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001724 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1725 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1726 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001727 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1728 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001729 }
1730
Eric Christophera7c32732012-12-18 00:30:54 +00001731 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 // manager changed or buffer changed (like in a nested include) then just
1733 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001734 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001735 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001736 if (Parser->SavedDiagHandler)
1737 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1738 else
Craig Topper353eda42014-04-24 06:44:33 +00001739 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001740 return;
1741 }
1742
Eric Christophera7c32732012-12-18 00:30:54 +00001743 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001744 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1745 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001746 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001747
1748 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1749 int CppHashLocLineNo =
1750 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001751 int LineNo =
1752 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001753
Jim Grosbach4b905842013-09-20 23:08:21 +00001754 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1755 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001756 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001757
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001758 if (Parser->SavedDiagHandler)
1759 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1760 else
Craig Topper353eda42014-04-24 06:44:33 +00001761 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001762}
1763
Rafael Espindola2c064482012-08-21 18:29:30 +00001764// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1765// difference being that that function accepts '@' as part of identifiers and
1766// we can't do that. AsmLexer.cpp should probably be changed to handle
1767// '@' as a special case when needed.
1768static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001769 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1770 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001771}
1772
Rafael Espindola34b9c512012-06-03 23:57:14 +00001773bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001774 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001775 ArrayRef<MCAsmMacroArgument> A,
1776 bool EnableAtPseudoVariable, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001778 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001779 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001781
Preston Gurd05500642012-09-19 20:36:12 +00001782 // A macro without parameters is handled differently on Darwin:
1783 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001784 while (!Body.empty()) {
1785 // Scan for the next substitution.
1786 std::size_t End = Body.size(), Pos = 0;
1787 for (; Pos != End; ++Pos) {
1788 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001789 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001790 // This macro has no parameters, look for $0, $1, etc.
1791 if (Body[Pos] != '$' || Pos + 1 == End)
1792 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001793
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001795 if (Next == '$' || Next == 'n' ||
1796 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001797 break;
1798 } else {
1799 // This macro has parameters, look for \foo, \bar, etc.
1800 if (Body[Pos] == '\\' && Pos + 1 != End)
1801 break;
1802 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001803 }
1804
1805 // Add the prefix.
1806 OS << Body.slice(0, Pos);
1807
1808 // Check if we reached the end.
1809 if (Pos == End)
1810 break;
1811
Benjamin Kramer513e7442014-02-20 13:36:32 +00001812 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001813 switch (Body[Pos + 1]) {
1814 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 case '$':
1816 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001817 break;
1818
Jim Grosbach4b905842013-09-20 23:08:21 +00001819 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001820 case 'n':
1821 OS << A.size();
1822 break;
1823
Jim Grosbach4b905842013-09-20 23:08:21 +00001824 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001825 default: {
1826 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001827 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828 if (Index >= A.size())
1829 break;
1830
1831 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001832 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001833 ie = A[Index].end();
1834 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001835 OS << it->getString();
1836 break;
1837 }
1838 }
1839 Pos += 2;
1840 } else {
1841 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001842
1843 // Check for the \@ pseudo-variable.
1844 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001845 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001846 else
1847 while (isIdentifierChar(Body[I]) && I + 1 != End)
1848 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001849
Jim Grosbach4b905842013-09-20 23:08:21 +00001850 const char *Begin = Body.data() + Pos + 1;
1851 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001853
Toma Tabacu217116e2015-04-27 10:50:29 +00001854 if (Argument == "@") {
1855 OS << NumOfMacroInstantiations;
1856 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001857 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001858 for (; Index < NParameters; ++Index)
1859 if (Parameters[Index].Name == Argument)
1860 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001861
Toma Tabacu217116e2015-04-27 10:50:29 +00001862 if (Index == NParameters) {
1863 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1864 Pos += 3;
1865 else {
1866 OS << '\\' << Argument;
1867 Pos = I;
1868 }
1869 } else {
1870 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1871 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1872 ie = A[Index].end();
1873 it != ie; ++it)
1874 // We expect no quotes around the string's contents when
1875 // parsing for varargs.
1876 if (it->getKind() != AsmToken::String || VarargParameter)
1877 OS << it->getString();
1878 else
1879 OS << it->getStringContents();
1880
1881 Pos += 1 + Argument.size();
1882 }
Preston Gurd05500642012-09-19 20:36:12 +00001883 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001884 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001885 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001886 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001887 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001888
Rafael Espindola1134ab232011-06-05 02:43:45 +00001889 return false;
1890}
Daniel Dunbar43235712010-07-18 18:54:11 +00001891
Nico Weber2a8f9222014-07-24 16:29:04 +00001892MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001893 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001894 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001895 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001896
Jim Grosbach4b905842013-09-20 23:08:21 +00001897static bool isOperator(AsmToken::TokenKind kind) {
1898 switch (kind) {
1899 default:
1900 return false;
1901 case AsmToken::Plus:
1902 case AsmToken::Minus:
1903 case AsmToken::Tilde:
1904 case AsmToken::Slash:
1905 case AsmToken::Star:
1906 case AsmToken::Dot:
1907 case AsmToken::Equal:
1908 case AsmToken::EqualEqual:
1909 case AsmToken::Pipe:
1910 case AsmToken::PipePipe:
1911 case AsmToken::Caret:
1912 case AsmToken::Amp:
1913 case AsmToken::AmpAmp:
1914 case AsmToken::Exclaim:
1915 case AsmToken::ExclaimEqual:
1916 case AsmToken::Percent:
1917 case AsmToken::Less:
1918 case AsmToken::LessEqual:
1919 case AsmToken::LessLess:
1920 case AsmToken::LessGreater:
1921 case AsmToken::Greater:
1922 case AsmToken::GreaterEqual:
1923 case AsmToken::GreaterGreater:
1924 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001925 }
1926}
1927
David Majnemer16252452014-01-29 00:07:39 +00001928namespace {
1929class AsmLexerSkipSpaceRAII {
1930public:
1931 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1932 Lexer.setSkipSpace(SkipSpace);
1933 }
1934
1935 ~AsmLexerSkipSpaceRAII() {
1936 Lexer.setSkipSpace(true);
1937 }
1938
1939private:
1940 AsmLexer &Lexer;
1941};
1942}
1943
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001944bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1945
1946 if (Vararg) {
1947 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1948 StringRef Str = parseStringToEndOfStatement();
1949 MA.push_back(AsmToken(AsmToken::String, Str));
1950 }
1951 return false;
1952 }
1953
Rafael Espindola768b41c2012-06-15 14:02:34 +00001954 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001955 unsigned AddTokens = 0;
1956
David Majnemer16252452014-01-29 00:07:39 +00001957 // Darwin doesn't use spaces to delmit arguments.
1958 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001959
1960 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001961 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001962 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001963
David Majnemer91fc4c22014-01-29 18:57:46 +00001964 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001965 break;
Preston Gurd05500642012-09-19 20:36:12 +00001966
1967 if (Lexer.is(AsmToken::Space)) {
1968 Lex(); // Eat spaces
1969
1970 // Spaces can delimit parameters, but could also be part an expression.
1971 // If the token after a space is an operator, add the token and the next
1972 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001973 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001974 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001975 // Check to see whether the token is used as an operator,
1976 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001977 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001978 if (*NextChar == ' ')
1979 AddTokens = 2;
1980 }
1981
1982 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001983 break;
1984 }
1985 }
1986 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001987
Jim Grosbach4b905842013-09-20 23:08:21 +00001988 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989 // to be able to fill in the remaining default parameter values
1990 if (Lexer.is(AsmToken::EndOfStatement))
1991 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001992
1993 // Adjust the current parentheses level.
1994 if (Lexer.is(AsmToken::LParen))
1995 ++ParenLevel;
1996 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1997 --ParenLevel;
1998
1999 // Append the token to the current argument list.
2000 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002001 if (AddTokens)
2002 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002003 Lex();
2004 }
Preston Gurd05500642012-09-19 20:36:12 +00002005
Rafael Espindola768b41c2012-06-15 14:02:34 +00002006 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002007 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002008 return false;
2009}
2010
2011// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002012bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002013 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002014 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002015 bool NamedParametersFound = false;
2016 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002017
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002018 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002019 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002020
Rafael Espindola768b41c2012-06-15 14:02:34 +00002021 // Parse two kinds of macro invocations:
2022 // - macros defined without any parameters accept an arbitrary number of them
2023 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002024 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002025 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2026 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002027 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002028 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002029
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002031 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002032 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 eatToEndOfStatement();
2034 return true;
2035 }
2036
2037 if (!Lexer.is(AsmToken::Equal)) {
2038 TokError("expected '=' after formal parameter identifier");
2039 eatToEndOfStatement();
2040 return true;
2041 }
2042 Lex();
2043
2044 NamedParametersFound = true;
2045 }
2046
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002047 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002048 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002049 eatToEndOfStatement();
2050 return true;
2051 }
2052
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002053 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2054 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002055 return true;
2056
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002057 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002058 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002059 unsigned FAI = 0;
2060 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002061 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002062 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002063
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002064 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002065 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002066 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002067 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002068 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002069 return true;
2070 }
2071 PI = FAI;
2072 }
2073
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002074 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002075 if (A.size() <= PI)
2076 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002077 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002078
2079 if (FALocs.size() <= PI)
2080 FALocs.resize(PI + 1);
2081
2082 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002083 }
Jim Grosbach206661622012-07-30 22:44:17 +00002084
Preston Gurd242ed3152012-09-19 20:29:04 +00002085 // At the end of the statement, fill in remaining arguments that have
2086 // default values. If there aren't any, then the next argument is
2087 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002088 if (Lexer.is(AsmToken::EndOfStatement)) {
2089 bool Failure = false;
2090 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2091 if (A[FAI].empty()) {
2092 if (M->Parameters[FAI].Required) {
2093 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2094 "missing value for required parameter "
2095 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2096 Failure = true;
2097 }
2098
2099 if (!M->Parameters[FAI].Value.empty())
2100 A[FAI] = M->Parameters[FAI].Value;
2101 }
2102 }
2103 return Failure;
2104 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002105
2106 if (Lexer.is(AsmToken::Comma))
2107 Lex();
2108 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002109
2110 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002111}
2112
Jim Grosbach4b905842013-09-20 23:08:21 +00002113const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002114 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2115 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002116}
2117
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002118void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2119 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002120}
2121
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002122void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002123
Jim Grosbach4b905842013-09-20 23:08:21 +00002124bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002125 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2126 // this, although we should protect against infinite loops.
2127 if (ActiveMacros.size() == 20)
2128 return TokError("macros cannot be nested more than 20 levels deep");
2129
Eli Bendersky38274122013-01-14 23:22:36 +00002130 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002131 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002132 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002133
Rafael Espindola1134ab232011-06-05 02:43:45 +00002134 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2135 // to hold the macro body with substitutions.
2136 SmallString<256> Buf;
2137 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002138 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002139
Toma Tabacu217116e2015-04-27 10:50:29 +00002140 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002141 return true;
2142
Eli Bendersky38274122013-01-14 23:22:36 +00002143 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002144 // instantiation.
2145 OS << ".endmacro\n";
2146
Rafael Espindola3560ff22014-08-27 20:03:13 +00002147 std::unique_ptr<MemoryBuffer> Instantiation =
2148 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002149
Daniel Dunbar43235712010-07-18 18:54:11 +00002150 // Create the macro instantiation object and add to the current macro
2151 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002152 MacroInstantiation *MI = new MacroInstantiation(
2153 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002154 ActiveMacros.push_back(MI);
2155
Toma Tabacu217116e2015-04-27 10:50:29 +00002156 ++NumOfMacroInstantiations;
2157
Daniel Dunbar43235712010-07-18 18:54:11 +00002158 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002159 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002160 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002161 Lex();
2162
2163 return false;
2164}
2165
Jim Grosbach4b905842013-09-20 23:08:21 +00002166void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002167 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002168 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002169 Lex();
2170
2171 // Pop the instantiation entry.
2172 delete ActiveMacros.back();
2173 ActiveMacros.pop_back();
2174}
2175
Jim Grosbach4b905842013-09-20 23:08:21 +00002176static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002177 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002178 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002179 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2180 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002181 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002182 case MCExpr::Target:
2183 case MCExpr::Constant:
2184 return false;
2185 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002186 const MCSymbol &S =
2187 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002188 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002189 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002190 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002191 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002192 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002193 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002194 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002195
2196 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002197}
2198
Jim Grosbach4b905842013-09-20 23:08:21 +00002199bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002200 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002201 // FIXME: Use better location, we should use proper tokens.
2202 SMLoc EqualLoc = Lexer.getLoc();
2203
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002204 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002205 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002206 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002207
Rafael Espindola72f5f172012-01-28 05:57:00 +00002208 // Note: we don't count b as used in "a = b". This is to allow
2209 // a = b
2210 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002211
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002212 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002213 return TokError("unexpected token in assignment");
2214
2215 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002216 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002217
Daniel Dunbar5f339242009-10-16 01:57:39 +00002218 // Validate that the LHS is allowed to be a variable (either it has not been
2219 // used as a symbol, or it is an absolute symbol).
2220 MCSymbol *Sym = getContext().LookupSymbol(Name);
2221 if (Sym) {
2222 // Diagnose assignment to a label.
2223 //
2224 // FIXME: Diagnostics. Note the location of the definition as a label.
2225 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002226 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002227 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2228 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002229 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002230 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2231 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002232 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002233 return Error(EqualLoc, "redefinition of '" + Name + "'");
2234 else if (!Sym->isVariable())
2235 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002236 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002237 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002238 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002239
2240 // Don't count these checks as uses.
2241 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002242 } else if (Name == ".") {
2243 if (Out.EmitValueToOffset(Value, 0)) {
2244 Error(EqualLoc, "expected absolute expression");
2245 eatToEndOfStatement();
2246 }
2247 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002248 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002249 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002250
David Majnemer58cb80c2014-12-24 10:27:50 +00002251 Sym->setRedefinable(allow_redef);
2252
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002253 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002254 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002255 if (NoDeadStrip)
2256 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2257
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002258 return false;
2259}
2260
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002261/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002262/// ::= identifier
2263/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002264bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002265 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002266 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2267 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002268 // handle this as a context dependent token, instead we detect adjacent tokens
2269 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002270 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2271 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002272
Hans Wennborgce69d772013-10-18 20:46:28 +00002273 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002274 Lex();
2275 if (Lexer.isNot(AsmToken::Identifier))
2276 return true;
2277
Hans Wennborgce69d772013-10-18 20:46:28 +00002278 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2279 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002280 return true;
2281
2282 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002283 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002284 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002285 Lex();
2286 return false;
2287 }
2288
Jim Grosbach4b905842013-09-20 23:08:21 +00002289 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002290 return true;
2291
Sean Callanan936b0d32010-01-19 21:44:56 +00002292 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002293
Sean Callanan686ed8d2010-01-19 20:22:31 +00002294 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002295
2296 return false;
2297}
2298
Jim Grosbach4b905842013-09-20 23:08:21 +00002299/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002300/// ::= .equ identifier ',' expression
2301/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002302/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002303bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002304 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002305
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002306 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002307 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002308
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002309 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002310 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002311 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002312
Jim Grosbach4b905842013-09-20 23:08:21 +00002313 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002314}
2315
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002316bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002317 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002318
2319 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002320 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002321 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2322 if (Str[i] != '\\') {
2323 Data += Str[i];
2324 continue;
2325 }
2326
2327 // Recognize escaped characters. Note that this escape semantics currently
2328 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2329 ++i;
2330 if (i == e)
2331 return TokError("unexpected backslash at end of string");
2332
2333 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002334 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002335 // Consume up to three octal characters.
2336 unsigned Value = Str[i] - '0';
2337
Jim Grosbach4b905842013-09-20 23:08:21 +00002338 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002339 ++i;
2340 Value = Value * 8 + (Str[i] - '0');
2341
Jim Grosbach4b905842013-09-20 23:08:21 +00002342 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002343 ++i;
2344 Value = Value * 8 + (Str[i] - '0');
2345 }
2346 }
2347
2348 if (Value > 255)
2349 return TokError("invalid octal escape sequence (out of range)");
2350
Jim Grosbach4b905842013-09-20 23:08:21 +00002351 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002352 continue;
2353 }
2354
2355 // Otherwise recognize individual escapes.
2356 switch (Str[i]) {
2357 default:
2358 // Just reject invalid escape sequences for now.
2359 return TokError("invalid escape sequence (unrecognized character)");
2360
2361 case 'b': Data += '\b'; break;
2362 case 'f': Data += '\f'; break;
2363 case 'n': Data += '\n'; break;
2364 case 'r': Data += '\r'; break;
2365 case 't': Data += '\t'; break;
2366 case '"': Data += '"'; break;
2367 case '\\': Data += '\\'; break;
2368 }
2369 }
2370
2371 return false;
2372}
2373
Jim Grosbach4b905842013-09-20 23:08:21 +00002374/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002375/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002376bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002377 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002378 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002379
Daniel Dunbara10e5192009-06-24 23:30:00 +00002380 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002381 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002382 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002383
Daniel Dunbaref668c12009-08-14 18:19:52 +00002384 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002385 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002386 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002387
Rafael Espindola64e1af82013-07-02 15:49:13 +00002388 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002389 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002390 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002391
Sean Callanan686ed8d2010-01-19 20:22:31 +00002392 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002393
2394 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002395 break;
2396
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002397 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002398 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002399 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 }
2401 }
2402
Sean Callanan686ed8d2010-01-19 20:22:31 +00002403 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 return false;
2405}
2406
Jim Grosbach4b905842013-09-20 23:08:21 +00002407/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002409bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002410 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002411 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002412
Daniel Dunbara10e5192009-06-24 23:30:00 +00002413 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002414 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002415 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002416 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002417 return true;
2418
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002419 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002420 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2421 assert(Size <= 8 && "Invalid size");
2422 uint64_t IntValue = MCE->getValue();
2423 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2424 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002425 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002426 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002427 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002428
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002429 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002430 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002431
Daniel Dunbara10e5192009-06-24 23:30:00 +00002432 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002433 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002434 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002435 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002436 }
2437 }
2438
Sean Callanan686ed8d2010-01-19 20:22:31 +00002439 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002440 return false;
2441}
2442
David Woodhoused6de0d92014-02-01 16:20:59 +00002443/// ParseDirectiveOctaValue
2444/// ::= .octa [ hexconstant (, hexconstant)* ]
2445bool AsmParser::parseDirectiveOctaValue() {
2446 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2447 checkForValidSection();
2448
2449 for (;;) {
2450 if (Lexer.getKind() == AsmToken::Error)
2451 return true;
2452 if (Lexer.getKind() != AsmToken::Integer &&
2453 Lexer.getKind() != AsmToken::BigNum)
2454 return TokError("unknown token in expression");
2455
2456 SMLoc ExprLoc = getLexer().getLoc();
2457 APInt IntValue = getTok().getAPIntVal();
2458 Lex();
2459
2460 uint64_t hi, lo;
2461 if (IntValue.isIntN(64)) {
2462 hi = 0;
2463 lo = IntValue.getZExtValue();
2464 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002465 // It might actually have more than 128 bits, but the top ones are zero.
2466 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002467 lo = IntValue.getLoBits(64).getZExtValue();
2468 } else
2469 return Error(ExprLoc, "literal value out of range for directive");
2470
2471 if (MAI.isLittleEndian()) {
2472 getStreamer().EmitIntValue(lo, 8);
2473 getStreamer().EmitIntValue(hi, 8);
2474 } else {
2475 getStreamer().EmitIntValue(hi, 8);
2476 getStreamer().EmitIntValue(lo, 8);
2477 }
2478
2479 if (getLexer().is(AsmToken::EndOfStatement))
2480 break;
2481
2482 // FIXME: Improve diagnostic.
2483 if (getLexer().isNot(AsmToken::Comma))
2484 return TokError("unexpected token in directive");
2485 Lex();
2486 }
2487 }
2488
2489 Lex();
2490 return false;
2491}
2492
Jim Grosbach4b905842013-09-20 23:08:21 +00002493/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002494/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002495bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002496 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002497 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002498
2499 for (;;) {
2500 // We don't truly support arithmetic on floating point expressions, so we
2501 // have to manually parse unary prefixes.
2502 bool IsNeg = false;
2503 if (getLexer().is(AsmToken::Minus)) {
2504 Lex();
2505 IsNeg = true;
2506 } else if (getLexer().is(AsmToken::Plus))
2507 Lex();
2508
Michael J. Spencer530ce852010-10-09 11:00:50 +00002509 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002510 getLexer().isNot(AsmToken::Real) &&
2511 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002512 return TokError("unexpected token in directive");
2513
2514 // Convert to an APFloat.
2515 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002516 StringRef IDVal = getTok().getString();
2517 if (getLexer().is(AsmToken::Identifier)) {
2518 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2519 Value = APFloat::getInf(Semantics);
2520 else if (!IDVal.compare_lower("nan"))
2521 Value = APFloat::getNaN(Semantics, false, ~0);
2522 else
2523 return TokError("invalid floating point literal");
2524 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002525 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002526 return TokError("invalid floating point literal");
2527 if (IsNeg)
2528 Value.changeSign();
2529
2530 // Consume the numeric token.
2531 Lex();
2532
2533 // Emit the value as an integer.
2534 APInt AsInt = Value.bitcastToAPInt();
2535 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002536 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002537
2538 if (getLexer().is(AsmToken::EndOfStatement))
2539 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002540
Daniel Dunbar2af16532010-09-24 01:59:56 +00002541 if (getLexer().isNot(AsmToken::Comma))
2542 return TokError("unexpected token in directive");
2543 Lex();
2544 }
2545 }
2546
2547 Lex();
2548 return false;
2549}
2550
Jim Grosbach4b905842013-09-20 23:08:21 +00002551/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002552/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002553bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002555
2556 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002557 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002558 return true;
2559
Rafael Espindolab91bac62010-10-05 19:42:57 +00002560 int64_t Val = 0;
2561 if (getLexer().is(AsmToken::Comma)) {
2562 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002563 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002564 return true;
2565 }
2566
Rafael Espindola922e3f42010-09-16 15:03:59 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement))
2568 return TokError("unexpected token in '.zero' directive");
2569
2570 Lex();
2571
Rafael Espindola64e1af82013-07-02 15:49:13 +00002572 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002573
2574 return false;
2575}
2576
Jim Grosbach4b905842013-09-20 23:08:21 +00002577/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002578/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002579bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002580 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002581
David Majnemer522d3db2014-02-01 07:19:38 +00002582 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002583 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002584 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002585 return true;
2586
David Majnemer522d3db2014-02-01 07:19:38 +00002587 if (NumValues < 0) {
2588 Warning(RepeatLoc,
2589 "'.fill' directive with negative repeat count has no effect");
2590 NumValues = 0;
2591 }
2592
Roman Divackye33098f2013-09-24 17:44:41 +00002593 int64_t FillSize = 1;
2594 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002595
David Majnemer522d3db2014-02-01 07:19:38 +00002596 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002597 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2598 if (getLexer().isNot(AsmToken::Comma))
2599 return TokError("unexpected token in '.fill' directive");
2600 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002601
David Majnemer522d3db2014-02-01 07:19:38 +00002602 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002603 if (parseAbsoluteExpression(FillSize))
2604 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002605
Roman Divackye33098f2013-09-24 17:44:41 +00002606 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2607 if (getLexer().isNot(AsmToken::Comma))
2608 return TokError("unexpected token in '.fill' directive");
2609 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002610
David Majnemer522d3db2014-02-01 07:19:38 +00002611 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002612 if (parseAbsoluteExpression(FillExpr))
2613 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002614
Roman Divackye33098f2013-09-24 17:44:41 +00002615 if (getLexer().isNot(AsmToken::EndOfStatement))
2616 return TokError("unexpected token in '.fill' directive");
2617
2618 Lex();
2619 }
2620 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002621
David Majnemer522d3db2014-02-01 07:19:38 +00002622 if (FillSize < 0) {
2623 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2624 NumValues = 0;
2625 }
2626 if (FillSize > 8) {
2627 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2628 FillSize = 8;
2629 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002630
David Majnemer522d3db2014-02-01 07:19:38 +00002631 if (!isUInt<32>(FillExpr) && FillSize > 4)
2632 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2633
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002634 if (NumValues > 0) {
2635 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2636 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2637 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2638 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2639 if (NonZeroFillSize < FillSize)
2640 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2641 }
David Majnemer522d3db2014-02-01 07:19:38 +00002642 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002643
2644 return false;
2645}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002646
Jim Grosbach4b905842013-09-20 23:08:21 +00002647/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002648/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002649bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002650 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002651
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002652 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002653 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002654 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002655 return true;
2656
2657 // Parse optional fill expression.
2658 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2660 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002661 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002662 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002663
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002664 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002665 return true;
2666
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002667 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002668 return TokError("unexpected token in '.org' directive");
2669 }
2670
Sean Callanan686ed8d2010-01-19 20:22:31 +00002671 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002672
Jim Grosbachb5912772012-01-27 00:37:08 +00002673 // Only limited forms of relocatable expressions are accepted here, it
2674 // has to be relative to the current section. The streamer will return
2675 // 'true' if the expression wasn't evaluatable.
2676 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2677 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002678
2679 return false;
2680}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681
Jim Grosbach4b905842013-09-20 23:08:21 +00002682/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002684bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002685 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002686
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002687 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002688 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002689 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002690 return true;
2691
2692 SMLoc MaxBytesLoc;
2693 bool HasFillExpr = false;
2694 int64_t FillExpr = 0;
2695 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002696 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2697 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002699 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002700
2701 // The fill expression can be omitted while specifying a maximum number of
2702 // alignment bytes, e.g:
2703 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002704 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002705 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002706 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002707 return true;
2708 }
2709
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002710 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2711 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002712 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002713 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002714
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002715 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002716 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002718
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002719 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 return TokError("unexpected token in directive");
2721 }
2722 }
2723
Sean Callanan686ed8d2010-01-19 20:22:31 +00002724 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002725
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002726 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002727 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002728
2729 // Compute alignment in bytes.
2730 if (IsPow2) {
2731 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002732 if (Alignment >= 32) {
2733 Error(AlignmentLoc, "invalid alignment value");
2734 Alignment = 31;
2735 }
2736
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002737 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002738 } else {
2739 // Reject alignments that aren't a power of two, for gas compatibility.
2740 if (!isPowerOf2_64(Alignment))
2741 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002742 }
2743
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002744 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002745 if (MaxBytesLoc.isValid()) {
2746 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002747 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002748 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002749 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002750 }
2751
2752 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002753 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002754 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002755 MaxBytesToFill = 0;
2756 }
2757 }
2758
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002759 // Check whether we should use optimal code alignment for this .align
2760 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002761 const MCSection *Section = getStreamer().getCurrentSection().first;
2762 assert(Section && "must have section to emit alignment");
2763 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002764 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2765 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002766 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002767 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002768 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002769 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2770 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002771 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002772
2773 return false;
2774}
2775
Jim Grosbach4b905842013-09-20 23:08:21 +00002776/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002777/// ::= .file [number] filename
2778/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002779bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002780 // FIXME: I'm not sure what this is.
2781 int64_t FileNumber = -1;
2782 SMLoc FileNumberLoc = getLexer().getLoc();
2783 if (getLexer().is(AsmToken::Integer)) {
2784 FileNumber = getTok().getIntVal();
2785 Lex();
2786
2787 if (FileNumber < 1)
2788 return TokError("file number less than one");
2789 }
2790
2791 if (getLexer().isNot(AsmToken::String))
2792 return TokError("unexpected token in '.file' directive");
2793
2794 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002795 // Allow the strings to have escaped octal character sequence.
2796 std::string Path = getTok().getString();
2797 if (parseEscapedString(Path))
2798 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002799 Lex();
2800
2801 StringRef Directory;
2802 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002803 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002804 if (getLexer().is(AsmToken::String)) {
2805 if (FileNumber == -1)
2806 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002807 if (parseEscapedString(FilenameData))
2808 return true;
2809 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002810 Directory = Path;
2811 Lex();
2812 } else {
2813 Filename = Path;
2814 }
2815
2816 if (getLexer().isNot(AsmToken::EndOfStatement))
2817 return TokError("unexpected token in '.file' directive");
2818
2819 if (FileNumber == -1)
2820 getStreamer().EmitFileDirective(Filename);
2821 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002822 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002823 Error(DirectiveLoc,
2824 "input can't have .file dwarf directives when -g is "
2825 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002826
David Blaikiec714ef42014-03-17 01:52:11 +00002827 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2828 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002829 Error(FileNumberLoc, "file number already allocated");
2830 }
2831
2832 return false;
2833}
2834
Jim Grosbach4b905842013-09-20 23:08:21 +00002835/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002836/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002837bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002838 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2839 if (getLexer().isNot(AsmToken::Integer))
2840 return TokError("unexpected token in '.line' directive");
2841
2842 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002843 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002844 Lex();
2845
2846 // FIXME: Do something with the .line.
2847 }
2848
2849 if (getLexer().isNot(AsmToken::EndOfStatement))
2850 return TokError("unexpected token in '.line' directive");
2851
2852 return false;
2853}
2854
Jim Grosbach4b905842013-09-20 23:08:21 +00002855/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002856/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2857/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2858/// The first number is a file number, must have been previously assigned with
2859/// a .file directive, the second number is the line number and optionally the
2860/// third number is a column position (zero if not specified). The remaining
2861/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002862bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002863 if (getLexer().isNot(AsmToken::Integer))
2864 return TokError("unexpected token in '.loc' directive");
2865 int64_t FileNumber = getTok().getIntVal();
2866 if (FileNumber < 1)
2867 return TokError("file number less than one in '.loc' directive");
2868 if (!getContext().isValidDwarfFileNumber(FileNumber))
2869 return TokError("unassigned file number in '.loc' directive");
2870 Lex();
2871
2872 int64_t LineNumber = 0;
2873 if (getLexer().is(AsmToken::Integer)) {
2874 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002875 if (LineNumber < 0)
2876 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002877 Lex();
2878 }
2879
2880 int64_t ColumnPos = 0;
2881 if (getLexer().is(AsmToken::Integer)) {
2882 ColumnPos = getTok().getIntVal();
2883 if (ColumnPos < 0)
2884 return TokError("column position less than zero in '.loc' directive");
2885 Lex();
2886 }
2887
2888 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2889 unsigned Isa = 0;
2890 int64_t Discriminator = 0;
2891 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2892 for (;;) {
2893 if (getLexer().is(AsmToken::EndOfStatement))
2894 break;
2895
2896 StringRef Name;
2897 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002898 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002899 return TokError("unexpected token in '.loc' directive");
2900
2901 if (Name == "basic_block")
2902 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2903 else if (Name == "prologue_end")
2904 Flags |= DWARF2_FLAG_PROLOGUE_END;
2905 else if (Name == "epilogue_begin")
2906 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2907 else if (Name == "is_stmt") {
2908 Loc = getTok().getLoc();
2909 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002910 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return true;
2912 // The expression must be the constant 0 or 1.
2913 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2914 int Value = MCE->getValue();
2915 if (Value == 0)
2916 Flags &= ~DWARF2_FLAG_IS_STMT;
2917 else if (Value == 1)
2918 Flags |= DWARF2_FLAG_IS_STMT;
2919 else
2920 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002921 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002922 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2923 }
Craig Topperf15655b2013-04-22 04:22:40 +00002924 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002925 Loc = getTok().getLoc();
2926 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002927 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002928 return true;
2929 // The expression must be a constant greater or equal to 0.
2930 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2931 int Value = MCE->getValue();
2932 if (Value < 0)
2933 return Error(Loc, "isa number less than zero");
2934 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002935 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return Error(Loc, "isa number not a constant value");
2937 }
Craig Topperf15655b2013-04-22 04:22:40 +00002938 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002939 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002940 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002941 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002942 return Error(Loc, "unknown sub-directive in '.loc' directive");
2943 }
2944
2945 if (getLexer().is(AsmToken::EndOfStatement))
2946 break;
2947 }
2948 }
2949
2950 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2951 Isa, Discriminator, StringRef());
2952
2953 return false;
2954}
2955
Jim Grosbach4b905842013-09-20 23:08:21 +00002956/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002957/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002958bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002959 return TokError("unsupported directive '.stabs'");
2960}
2961
Jim Grosbach4b905842013-09-20 23:08:21 +00002962/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002963/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002964bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002965 StringRef Name;
2966 bool EH = false;
2967 bool Debug = false;
2968
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002969 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002970 return TokError("Expected an identifier");
2971
2972 if (Name == ".eh_frame")
2973 EH = true;
2974 else if (Name == ".debug_frame")
2975 Debug = true;
2976
2977 if (getLexer().is(AsmToken::Comma)) {
2978 Lex();
2979
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002980 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002981 return TokError("Expected an identifier");
2982
2983 if (Name == ".eh_frame")
2984 EH = true;
2985 else if (Name == ".debug_frame")
2986 Debug = true;
2987 }
2988
2989 getStreamer().EmitCFISections(EH, Debug);
2990 return false;
2991}
2992
Jim Grosbach4b905842013-09-20 23:08:21 +00002993/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002994/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002995bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002996 StringRef Simple;
2997 if (getLexer().isNot(AsmToken::EndOfStatement))
2998 if (parseIdentifier(Simple) || Simple != "simple")
2999 return TokError("unexpected token in .cfi_startproc directive");
3000
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003001 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003002 return false;
3003}
3004
Jim Grosbach4b905842013-09-20 23:08:21 +00003005/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003006/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003007bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003008 getStreamer().EmitCFIEndProc();
3009 return false;
3010}
3011
Jim Grosbach4b905842013-09-20 23:08:21 +00003012/// \brief parse register name or number.
3013bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003014 SMLoc DirectiveLoc) {
3015 unsigned RegNo;
3016
3017 if (getLexer().isNot(AsmToken::Integer)) {
3018 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3019 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003020 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003021 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003022 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003023
3024 return false;
3025}
3026
Jim Grosbach4b905842013-09-20 23:08:21 +00003027/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003028/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003029bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003030 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003031 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003032 return true;
3033
3034 if (getLexer().isNot(AsmToken::Comma))
3035 return TokError("unexpected token in directive");
3036 Lex();
3037
3038 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003039 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 getStreamer().EmitCFIDefCfa(Register, Offset);
3043 return false;
3044}
3045
Jim Grosbach4b905842013-09-20 23:08:21 +00003046/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003047/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003048bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003049 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003050 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003051 return true;
3052
3053 getStreamer().EmitCFIDefCfaOffset(Offset);
3054 return false;
3055}
3056
Jim Grosbach4b905842013-09-20 23:08:21 +00003057/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003058/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003059bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003060 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003061 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003062 return true;
3063
3064 if (getLexer().isNot(AsmToken::Comma))
3065 return TokError("unexpected token in directive");
3066 Lex();
3067
3068 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003069 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003070 return true;
3071
3072 getStreamer().EmitCFIRegister(Register1, Register2);
3073 return false;
3074}
3075
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003076/// parseDirectiveCFIWindowSave
3077/// ::= .cfi_window_save
3078bool AsmParser::parseDirectiveCFIWindowSave() {
3079 getStreamer().EmitCFIWindowSave();
3080 return false;
3081}
3082
Jim Grosbach4b905842013-09-20 23:08:21 +00003083/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003084/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003085bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003086 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003087 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003088 return true;
3089
3090 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3091 return false;
3092}
3093
Jim Grosbach4b905842013-09-20 23:08:21 +00003094/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003095/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003096bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003097 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003098 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003099 return true;
3100
3101 getStreamer().EmitCFIDefCfaRegister(Register);
3102 return false;
3103}
3104
Jim Grosbach4b905842013-09-20 23:08:21 +00003105/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003106/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003107bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003108 int64_t Register = 0;
3109 int64_t Offset = 0;
3110
Jim Grosbach4b905842013-09-20 23:08:21 +00003111 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003112 return true;
3113
3114 if (getLexer().isNot(AsmToken::Comma))
3115 return TokError("unexpected token in directive");
3116 Lex();
3117
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003118 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003119 return true;
3120
3121 getStreamer().EmitCFIOffset(Register, Offset);
3122 return false;
3123}
3124
Jim Grosbach4b905842013-09-20 23:08:21 +00003125/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003126/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003127bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003128 int64_t Register = 0;
3129
Jim Grosbach4b905842013-09-20 23:08:21 +00003130 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003131 return true;
3132
3133 if (getLexer().isNot(AsmToken::Comma))
3134 return TokError("unexpected token in directive");
3135 Lex();
3136
3137 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003138 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003139 return true;
3140
3141 getStreamer().EmitCFIRelOffset(Register, Offset);
3142 return false;
3143}
3144
3145static bool isValidEncoding(int64_t Encoding) {
3146 if (Encoding & ~0xff)
3147 return false;
3148
3149 if (Encoding == dwarf::DW_EH_PE_omit)
3150 return true;
3151
3152 const unsigned Format = Encoding & 0xf;
3153 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3154 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3155 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3156 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3157 return false;
3158
3159 const unsigned Application = Encoding & 0x70;
3160 if (Application != dwarf::DW_EH_PE_absptr &&
3161 Application != dwarf::DW_EH_PE_pcrel)
3162 return false;
3163
3164 return true;
3165}
3166
Jim Grosbach4b905842013-09-20 23:08:21 +00003167/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003168/// IsPersonality true for cfi_personality, false for cfi_lsda
3169/// ::= .cfi_personality encoding, [symbol_name]
3170/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003171bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003172 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003173 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003174 return true;
3175 if (Encoding == dwarf::DW_EH_PE_omit)
3176 return false;
3177
3178 if (!isValidEncoding(Encoding))
3179 return TokError("unsupported encoding.");
3180
3181 if (getLexer().isNot(AsmToken::Comma))
3182 return TokError("unexpected token in directive");
3183 Lex();
3184
3185 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003186 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003187 return TokError("expected identifier in directive");
3188
3189 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3190
3191 if (IsPersonality)
3192 getStreamer().EmitCFIPersonality(Sym, Encoding);
3193 else
3194 getStreamer().EmitCFILsda(Sym, Encoding);
3195 return false;
3196}
3197
Jim Grosbach4b905842013-09-20 23:08:21 +00003198/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003199/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003200bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003201 getStreamer().EmitCFIRememberState();
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003207bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003208 getStreamer().EmitCFIRestoreState();
3209 return false;
3210}
3211
Jim Grosbach4b905842013-09-20 23:08:21 +00003212/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003213/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003214bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003215 int64_t Register = 0;
3216
Jim Grosbach4b905842013-09-20 23:08:21 +00003217 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003218 return true;
3219
3220 getStreamer().EmitCFISameValue(Register);
3221 return false;
3222}
3223
Jim Grosbach4b905842013-09-20 23:08:21 +00003224/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003225/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003226bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003227 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003228 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003229 return true;
3230
3231 getStreamer().EmitCFIRestore(Register);
3232 return false;
3233}
3234
Jim Grosbach4b905842013-09-20 23:08:21 +00003235/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003236/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003237bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003238 std::string Values;
3239 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003240 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003241 return true;
3242
3243 Values.push_back((uint8_t)CurrValue);
3244
3245 while (getLexer().is(AsmToken::Comma)) {
3246 Lex();
3247
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003248 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003249 return true;
3250
3251 Values.push_back((uint8_t)CurrValue);
3252 }
3253
3254 getStreamer().EmitCFIEscape(Values);
3255 return false;
3256}
3257
Jim Grosbach4b905842013-09-20 23:08:21 +00003258/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003259/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003260bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003261 if (getLexer().isNot(AsmToken::EndOfStatement))
3262 return Error(getLexer().getLoc(),
3263 "unexpected token in '.cfi_signal_frame'");
3264
3265 getStreamer().EmitCFISignalFrame();
3266 return false;
3267}
3268
Jim Grosbach4b905842013-09-20 23:08:21 +00003269/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003270/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003271bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003272 int64_t Register = 0;
3273
Jim Grosbach4b905842013-09-20 23:08:21 +00003274 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003275 return true;
3276
3277 getStreamer().EmitCFIUndefined(Register);
3278 return false;
3279}
3280
Jim Grosbach4b905842013-09-20 23:08:21 +00003281/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003282/// ::= .macros_on
3283/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003284bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003285 if (getLexer().isNot(AsmToken::EndOfStatement))
3286 return Error(getLexer().getLoc(),
3287 "unexpected token in '" + Directive + "' directive");
3288
Jim Grosbach4b905842013-09-20 23:08:21 +00003289 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003290 return false;
3291}
3292
Jim Grosbach4b905842013-09-20 23:08:21 +00003293/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003294/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003295bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003296 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003297 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003298 return TokError("expected identifier in '.macro' directive");
3299
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003300 if (getLexer().is(AsmToken::Comma))
3301 Lex();
3302
Eli Bendersky17233942013-01-15 22:59:42 +00003303 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003304 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003305
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003306 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003307 return Error(Lexer.getLoc(),
3308 "Vararg parameter '" + Parameters.back().Name +
3309 "' should be last one in the list of parameters.");
3310
David Majnemer91fc4c22014-01-29 18:57:46 +00003311 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003312 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003313 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003314
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003315 if (Lexer.is(AsmToken::Colon)) {
3316 Lex(); // consume ':'
3317
3318 SMLoc QualLoc;
3319 StringRef Qualifier;
3320
3321 QualLoc = Lexer.getLoc();
3322 if (parseIdentifier(Qualifier))
3323 return Error(QualLoc, "missing parameter qualifier for "
3324 "'" + Parameter.Name + "' in macro '" + Name + "'");
3325
3326 if (Qualifier == "req")
3327 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003328 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003329 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003330 else
3331 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3332 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3333 }
3334
David Majnemer91fc4c22014-01-29 18:57:46 +00003335 if (getLexer().is(AsmToken::Equal)) {
3336 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003337
3338 SMLoc ParamLoc;
3339
3340 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003341 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003342 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003343
3344 if (Parameter.Required)
3345 Warning(ParamLoc, "pointless default value for required parameter "
3346 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003347 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003348
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003349 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003350
3351 if (getLexer().is(AsmToken::Comma))
3352 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003353 }
3354
3355 // Eat the end of statement.
3356 Lex();
3357
3358 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003359 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003360
3361 // Lex the macro definition.
3362 for (;;) {
3363 // Check whether we have reached the end of the file.
3364 if (getLexer().is(AsmToken::Eof))
3365 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3366
3367 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003368 if (getLexer().is(AsmToken::Identifier)) {
3369 if (getTok().getIdentifier() == ".endm" ||
3370 getTok().getIdentifier() == ".endmacro") {
3371 if (MacroDepth == 0) { // Outermost macro.
3372 EndToken = getTok();
3373 Lex();
3374 if (getLexer().isNot(AsmToken::EndOfStatement))
3375 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3376 "' directive");
3377 break;
3378 } else {
3379 // Otherwise we just found the end of an inner macro.
3380 --MacroDepth;
3381 }
3382 } else if (getTok().getIdentifier() == ".macro") {
3383 // We allow nested macros. Those aren't instantiated until the outermost
3384 // macro is expanded so just ignore them for now.
3385 ++MacroDepth;
3386 }
Eli Bendersky17233942013-01-15 22:59:42 +00003387 }
3388
3389 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003390 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003391 }
3392
Jim Grosbach4b905842013-09-20 23:08:21 +00003393 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003394 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3395 }
3396
3397 const char *BodyStart = StartToken.getLoc().getPointer();
3398 const char *BodyEnd = EndToken.getLoc().getPointer();
3399 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003400 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003401 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003402 return false;
3403}
3404
Jim Grosbach4b905842013-09-20 23:08:21 +00003405/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003406///
3407/// With the support added for named parameters there may be code out there that
3408/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003409/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003410/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003411/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003412/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3413/// warning that the positional parameter found in body which have no effect.
3414/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003415/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003416/// intended or change the macro to use the named parameters. It is possible
3417/// this warning will trigger when the none of the named parameters are used
3418/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003419void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003420 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003421 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003422 // If this macro is not defined with named parameters the warning we are
3423 // checking for here doesn't apply.
3424 unsigned NParameters = Parameters.size();
3425 if (NParameters == 0)
3426 return;
3427
3428 bool NamedParametersFound = false;
3429 bool PositionalParametersFound = false;
3430
3431 // Look at the body of the macro for use of both the named parameters and what
3432 // are likely to be positional parameters. This is what expandMacro() is
3433 // doing when it finds the parameters in the body.
3434 while (!Body.empty()) {
3435 // Scan for the next possible parameter.
3436 std::size_t End = Body.size(), Pos = 0;
3437 for (; Pos != End; ++Pos) {
3438 // Check for a substitution or escape.
3439 // This macro is defined with parameters, look for \foo, \bar, etc.
3440 if (Body[Pos] == '\\' && Pos + 1 != End)
3441 break;
3442
3443 // This macro should have parameters, but look for $0, $1, ..., $n too.
3444 if (Body[Pos] != '$' || Pos + 1 == End)
3445 continue;
3446 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003447 if (Next == '$' || Next == 'n' ||
3448 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003449 break;
3450 }
3451
3452 // Check if we reached the end.
3453 if (Pos == End)
3454 break;
3455
3456 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 switch (Body[Pos + 1]) {
3458 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003459 case '$':
3460 break;
3461
Jim Grosbach4b905842013-09-20 23:08:21 +00003462 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003463 case 'n':
3464 PositionalParametersFound = true;
3465 break;
3466
Jim Grosbach4b905842013-09-20 23:08:21 +00003467 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003468 default: {
3469 PositionalParametersFound = true;
3470 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003471 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003472 }
3473 Pos += 2;
3474 } else {
3475 unsigned I = Pos + 1;
3476 while (isIdentifierChar(Body[I]) && I + 1 != End)
3477 ++I;
3478
Jim Grosbach4b905842013-09-20 23:08:21 +00003479 const char *Begin = Body.data() + Pos + 1;
3480 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003481 unsigned Index = 0;
3482 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003483 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003484 break;
3485
3486 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003487 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3488 Pos += 3;
3489 else {
3490 Pos = I;
3491 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003492 } else {
3493 NamedParametersFound = true;
3494 Pos += 1 + Argument.size();
3495 }
3496 }
3497 // Update the scan point.
3498 Body = Body.substr(Pos);
3499 }
3500
3501 if (!NamedParametersFound && PositionalParametersFound)
3502 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3503 "used in macro body, possible positional parameter "
3504 "found in body which will have no effect");
3505}
3506
Nico Weber155dccd12014-07-24 17:08:39 +00003507/// parseDirectiveExitMacro
3508/// ::= .exitm
3509bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3510 if (getLexer().isNot(AsmToken::EndOfStatement))
3511 return TokError("unexpected token in '" + Directive + "' directive");
3512
3513 if (!isInsideMacroInstantiation())
3514 return TokError("unexpected '" + Directive + "' in file, "
3515 "no current macro definition");
3516
3517 // Exit all conditionals that are active in the current macro.
3518 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3519 TheCondState = TheCondStack.back();
3520 TheCondStack.pop_back();
3521 }
3522
3523 handleMacroExit();
3524 return false;
3525}
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003528/// ::= .endm
3529/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003530bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003531 if (getLexer().isNot(AsmToken::EndOfStatement))
3532 return TokError("unexpected token in '" + Directive + "' directive");
3533
3534 // If we are inside a macro instantiation, terminate the current
3535 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003536 if (isInsideMacroInstantiation()) {
3537 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003538 return false;
3539 }
3540
3541 // Otherwise, this .endmacro is a stray entry in the file; well formed
3542 // .endmacro directives are handled during the macro definition parsing.
3543 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003544 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003545}
3546
Jim Grosbach4b905842013-09-20 23:08:21 +00003547/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003548/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003549bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003550 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003551 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003552 return TokError("expected identifier in '.purgem' directive");
3553
3554 if (getLexer().isNot(AsmToken::EndOfStatement))
3555 return TokError("unexpected token in '.purgem' directive");
3556
Jim Grosbach4b905842013-09-20 23:08:21 +00003557 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003558 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3559
Jim Grosbach4b905842013-09-20 23:08:21 +00003560 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003561 return false;
3562}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003563
Jim Grosbach4b905842013-09-20 23:08:21 +00003564/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003565/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003566bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003567 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003568
3569 // Expect a single argument: an expression that evaluates to a constant
3570 // in the inclusive range 0-30.
3571 SMLoc ExprLoc = getLexer().getLoc();
3572 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003573 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003574 return true;
3575 else if (getLexer().isNot(AsmToken::EndOfStatement))
3576 return TokError("unexpected token after expression in"
3577 " '.bundle_align_mode' directive");
3578 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3579 return Error(ExprLoc,
3580 "invalid bundle alignment size (expected between 0 and 30)");
3581
3582 Lex();
3583
3584 // Because of AlignSizePow2's verified range we can safely truncate it to
3585 // unsigned.
3586 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3587 return false;
3588}
3589
Jim Grosbach4b905842013-09-20 23:08:21 +00003590/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003591/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003592bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003593 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003594 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003595
Eli Bendersky802b6282013-01-07 21:51:08 +00003596 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3597 StringRef Option;
3598 SMLoc Loc = getTok().getLoc();
3599 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003600 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003601
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003602 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003603 return Error(Loc, kInvalidOptionError);
3604
3605 if (Option != "align_to_end")
3606 return Error(Loc, kInvalidOptionError);
3607 else if (getLexer().isNot(AsmToken::EndOfStatement))
3608 return Error(Loc,
3609 "unexpected token after '.bundle_lock' directive option");
3610 AlignToEnd = true;
3611 }
3612
Eli Benderskyf483ff92012-12-20 19:05:53 +00003613 Lex();
3614
Eli Bendersky802b6282013-01-07 21:51:08 +00003615 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003616 return false;
3617}
3618
Jim Grosbach4b905842013-09-20 23:08:21 +00003619/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003620/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003621bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003622 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003623
3624 if (getLexer().isNot(AsmToken::EndOfStatement))
3625 return TokError("unexpected token in '.bundle_unlock' directive");
3626 Lex();
3627
3628 getStreamer().EmitBundleUnlock();
3629 return false;
3630}
3631
Jim Grosbach4b905842013-09-20 23:08:21 +00003632/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003633/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003634bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003635 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003636
3637 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003638 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003639 return true;
3640
3641 int64_t FillExpr = 0;
3642 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3643 if (getLexer().isNot(AsmToken::Comma))
3644 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3645 Lex();
3646
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003647 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003648 return true;
3649
3650 if (getLexer().isNot(AsmToken::EndOfStatement))
3651 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3652 }
3653
3654 Lex();
3655
3656 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003657 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3658 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003659
3660 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003661 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003662
3663 return false;
3664}
3665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003667/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003668bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003669 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003670 const MCExpr *Value;
3671
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003672 for (;;) {
3673 if (parseExpression(Value))
3674 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003675
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003676 if (Signed)
3677 getStreamer().EmitSLEB128Value(Value);
3678 else
3679 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003680
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003681 if (getLexer().is(AsmToken::EndOfStatement))
3682 break;
3683
3684 if (getLexer().isNot(AsmToken::Comma))
3685 return TokError("unexpected token in directive");
3686 Lex();
3687 }
Eli Bendersky17233942013-01-15 22:59:42 +00003688
3689 return false;
3690}
3691
Jim Grosbach4b905842013-09-20 23:08:21 +00003692/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003693/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003694bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003696 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003697 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003698 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003699
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003700 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003701 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003702
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003703 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003704
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003705 // Assembler local symbols don't make any sense here. Complain loudly.
3706 if (Sym->isTemporary())
3707 return Error(Loc, "non-local symbol required in directive");
3708
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003709 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3710 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003711
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003712 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003713 break;
3714
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003715 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003716 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003717 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003718 }
3719 }
3720
Sean Callanan686ed8d2010-01-19 20:22:31 +00003721 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003722 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003723}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003724
Jim Grosbach4b905842013-09-20 23:08:21 +00003725/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003726/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003727bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003728 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003729
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003730 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003731 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003732 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003733 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003734
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003735 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003736 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003737
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003738 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003739 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003740 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003741
3742 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003743 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003744 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003745 return true;
3746
3747 int64_t Pow2Alignment = 0;
3748 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003749 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003750 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003751 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003752 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003753 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003754
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003755 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3756 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003757 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3758
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003759 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003760 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3761 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003762 if (!isPowerOf2_64(Pow2Alignment))
3763 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3764 Pow2Alignment = Log2_64(Pow2Alignment);
3765 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003766 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003767
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003768 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003769 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003770
Sean Callanan686ed8d2010-01-19 20:22:31 +00003771 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003772
Chris Lattner28ad7542009-07-09 17:25:12 +00003773 // NOTE: a size of zero for a .comm should create a undefined symbol
3774 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003775 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003776 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003777 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003778
Eric Christopherbc818852010-05-14 01:38:54 +00003779 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003780 // may internally end up wanting an alignment in bytes.
3781 // FIXME: Diagnose overflow.
3782 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003783 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003784 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003785
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003786 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003787 return Error(IDLoc, "invalid symbol redefinition");
3788
Chris Lattner28ad7542009-07-09 17:25:12 +00003789 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003790 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003791 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003792 return false;
3793 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003794
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003795 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003796 return false;
3797}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003798
Jim Grosbach4b905842013-09-20 23:08:21 +00003799/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003800/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003801bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003802 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003803 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003804
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003805 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003806 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003807 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003808
Sean Callanan686ed8d2010-01-19 20:22:31 +00003809 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003810
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003811 if (Str.empty())
3812 Error(Loc, ".abort detected. Assembly stopping.");
3813 else
3814 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003815 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003816
3817 return false;
3818}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003819
Jim Grosbach4b905842013-09-20 23:08:21 +00003820/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003821/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003822bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003823 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003824 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003825
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003826 // Allow the strings to have escaped octal character sequence.
3827 std::string Filename;
3828 if (parseEscapedString(Filename))
3829 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003830 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003831 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003832
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003833 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003834 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003835
Chris Lattner693fbb82009-07-16 06:14:39 +00003836 // Attempt to switch the lexer to the included file before consuming the end
3837 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003838 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003839 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003840 return true;
3841 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003842
3843 return false;
3844}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003845
Jim Grosbach4b905842013-09-20 23:08:21 +00003846/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003847/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003848bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003849 if (getLexer().isNot(AsmToken::String))
3850 return TokError("expected string in '.incbin' directive");
3851
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003852 // Allow the strings to have escaped octal character sequence.
3853 std::string Filename;
3854 if (parseEscapedString(Filename))
3855 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003856 SMLoc IncbinLoc = getLexer().getLoc();
3857 Lex();
3858
3859 if (getLexer().isNot(AsmToken::EndOfStatement))
3860 return TokError("unexpected token in '.incbin' directive");
3861
Kevin Enderby109f25c2011-12-14 21:47:48 +00003862 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003863 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003864 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3865 return true;
3866 }
3867
3868 return false;
3869}
3870
Jim Grosbach4b905842013-09-20 23:08:21 +00003871/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003872/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3873bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003874 TheCondStack.push_back(TheCondState);
3875 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003876 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003877 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003878 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003879 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003880 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003881 return true;
3882
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003883 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003884 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003885
Sean Callanan686ed8d2010-01-19 20:22:31 +00003886 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003887
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003888 switch (DirKind) {
3889 default:
3890 llvm_unreachable("unsupported directive");
3891 case DK_IF:
3892 case DK_IFNE:
3893 break;
3894 case DK_IFEQ:
3895 ExprValue = ExprValue == 0;
3896 break;
3897 case DK_IFGE:
3898 ExprValue = ExprValue >= 0;
3899 break;
3900 case DK_IFGT:
3901 ExprValue = ExprValue > 0;
3902 break;
3903 case DK_IFLE:
3904 ExprValue = ExprValue <= 0;
3905 break;
3906 case DK_IFLT:
3907 ExprValue = ExprValue < 0;
3908 break;
3909 }
3910
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003911 TheCondState.CondMet = ExprValue;
3912 TheCondState.Ignore = !TheCondState.CondMet;
3913 }
3914
3915 return false;
3916}
3917
Jim Grosbach4b905842013-09-20 23:08:21 +00003918/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003919/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003920bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003921 TheCondStack.push_back(TheCondState);
3922 TheCondState.TheCond = AsmCond::IfCond;
3923
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003924 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003925 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003926 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003927 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003928
3929 if (getLexer().isNot(AsmToken::EndOfStatement))
3930 return TokError("unexpected token in '.ifb' directive");
3931
3932 Lex();
3933
3934 TheCondState.CondMet = ExpectBlank == Str.empty();
3935 TheCondState.Ignore = !TheCondState.CondMet;
3936 }
3937
3938 return false;
3939}
3940
Jim Grosbach4b905842013-09-20 23:08:21 +00003941/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003942/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003943/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003944bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003945 TheCondStack.push_back(TheCondState);
3946 TheCondState.TheCond = AsmCond::IfCond;
3947
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003948 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003949 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003950 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003951 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003952
3953 if (getLexer().isNot(AsmToken::Comma))
3954 return TokError("unexpected token in '.ifc' directive");
3955
3956 Lex();
3957
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003958 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003959
3960 if (getLexer().isNot(AsmToken::EndOfStatement))
3961 return TokError("unexpected token in '.ifc' directive");
3962
3963 Lex();
3964
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003965 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003966 TheCondState.Ignore = !TheCondState.CondMet;
3967 }
3968
3969 return false;
3970}
3971
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003972/// parseDirectiveIfeqs
3973/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003974bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003975 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003976 if (ExpectEqual)
3977 TokError("expected string parameter for '.ifeqs' directive");
3978 else
3979 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003980 eatToEndOfStatement();
3981 return true;
3982 }
3983
3984 StringRef String1 = getTok().getStringContents();
3985 Lex();
3986
3987 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003988 if (ExpectEqual)
3989 TokError("expected comma after first string for '.ifeqs' directive");
3990 else
3991 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003992 eatToEndOfStatement();
3993 return true;
3994 }
3995
3996 Lex();
3997
3998 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003999 if (ExpectEqual)
4000 TokError("expected string parameter for '.ifeqs' directive");
4001 else
4002 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004003 eatToEndOfStatement();
4004 return true;
4005 }
4006
4007 StringRef String2 = getTok().getStringContents();
4008 Lex();
4009
4010 TheCondStack.push_back(TheCondState);
4011 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004012 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004013 TheCondState.Ignore = !TheCondState.CondMet;
4014
4015 return false;
4016}
4017
Jim Grosbach4b905842013-09-20 23:08:21 +00004018/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004019/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004020bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004021 StringRef Name;
4022 TheCondStack.push_back(TheCondState);
4023 TheCondState.TheCond = AsmCond::IfCond;
4024
4025 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004026 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004027 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004028 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004029 return TokError("expected identifier after '.ifdef'");
4030
4031 Lex();
4032
4033 MCSymbol *Sym = getContext().LookupSymbol(Name);
4034
4035 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004036 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004037 else
Craig Topper353eda42014-04-24 06:44:33 +00004038 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004039 TheCondState.Ignore = !TheCondState.CondMet;
4040 }
4041
4042 return false;
4043}
4044
Jim Grosbach4b905842013-09-20 23:08:21 +00004045/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004046/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004047bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004048 if (TheCondState.TheCond != AsmCond::IfCond &&
4049 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004050 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4051 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004052 TheCondState.TheCond = AsmCond::ElseIfCond;
4053
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004054 bool LastIgnoreState = false;
4055 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004056 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004057 if (LastIgnoreState || TheCondState.CondMet) {
4058 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004059 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004060 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004061 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004062 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004063 return true;
4064
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004065 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004066 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004067
Sean Callanan686ed8d2010-01-19 20:22:31 +00004068 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004069 TheCondState.CondMet = ExprValue;
4070 TheCondState.Ignore = !TheCondState.CondMet;
4071 }
4072
4073 return false;
4074}
4075
Jim Grosbach4b905842013-09-20 23:08:21 +00004076/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004077/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004078bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004079 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004080 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004081
Sean Callanan686ed8d2010-01-19 20:22:31 +00004082 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004083
4084 if (TheCondState.TheCond != AsmCond::IfCond &&
4085 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004086 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4087 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004088 TheCondState.TheCond = AsmCond::ElseCond;
4089 bool LastIgnoreState = false;
4090 if (!TheCondStack.empty())
4091 LastIgnoreState = TheCondStack.back().Ignore;
4092 if (LastIgnoreState || TheCondState.CondMet)
4093 TheCondState.Ignore = true;
4094 else
4095 TheCondState.Ignore = false;
4096
4097 return false;
4098}
4099
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004100/// parseDirectiveEnd
4101/// ::= .end
4102bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4103 if (getLexer().isNot(AsmToken::EndOfStatement))
4104 return TokError("unexpected token in '.end' directive");
4105
4106 Lex();
4107
4108 while (Lexer.isNot(AsmToken::Eof))
4109 Lex();
4110
4111 return false;
4112}
4113
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004114/// parseDirectiveError
4115/// ::= .err
4116/// ::= .error [string]
4117bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4118 if (!TheCondStack.empty()) {
4119 if (TheCondStack.back().Ignore) {
4120 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004121 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004122 }
4123 }
4124
4125 if (!WithMessage)
4126 return Error(L, ".err encountered");
4127
4128 StringRef Message = ".error directive invoked in source file";
4129 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4130 if (Lexer.isNot(AsmToken::String)) {
4131 TokError(".error argument must be a string");
4132 eatToEndOfStatement();
4133 return true;
4134 }
4135
4136 Message = getTok().getStringContents();
4137 Lex();
4138 }
4139
4140 Error(L, Message);
4141 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004142}
4143
Nico Weber404012b2014-07-24 16:26:06 +00004144/// parseDirectiveWarning
4145/// ::= .warning [string]
4146bool AsmParser::parseDirectiveWarning(SMLoc L) {
4147 if (!TheCondStack.empty()) {
4148 if (TheCondStack.back().Ignore) {
4149 eatToEndOfStatement();
4150 return false;
4151 }
4152 }
4153
4154 StringRef Message = ".warning directive invoked in source file";
4155 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4156 if (Lexer.isNot(AsmToken::String)) {
4157 TokError(".warning argument must be a string");
4158 eatToEndOfStatement();
4159 return true;
4160 }
4161
4162 Message = getTok().getStringContents();
4163 Lex();
4164 }
4165
4166 Warning(L, Message);
4167 return false;
4168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004171/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004172bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004173 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004174 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004175
Sean Callanan686ed8d2010-01-19 20:22:31 +00004176 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004177
Jim Grosbach4b905842013-09-20 23:08:21 +00004178 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004179 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4180 ".else");
4181 if (!TheCondStack.empty()) {
4182 TheCondState = TheCondStack.back();
4183 TheCondStack.pop_back();
4184 }
4185
4186 return false;
4187}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004188
Eli Bendersky17233942013-01-15 22:59:42 +00004189void AsmParser::initializeDirectiveKindMap() {
4190 DirectiveKindMap[".set"] = DK_SET;
4191 DirectiveKindMap[".equ"] = DK_EQU;
4192 DirectiveKindMap[".equiv"] = DK_EQUIV;
4193 DirectiveKindMap[".ascii"] = DK_ASCII;
4194 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4195 DirectiveKindMap[".string"] = DK_STRING;
4196 DirectiveKindMap[".byte"] = DK_BYTE;
4197 DirectiveKindMap[".short"] = DK_SHORT;
4198 DirectiveKindMap[".value"] = DK_VALUE;
4199 DirectiveKindMap[".2byte"] = DK_2BYTE;
4200 DirectiveKindMap[".long"] = DK_LONG;
4201 DirectiveKindMap[".int"] = DK_INT;
4202 DirectiveKindMap[".4byte"] = DK_4BYTE;
4203 DirectiveKindMap[".quad"] = DK_QUAD;
4204 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004205 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004206 DirectiveKindMap[".single"] = DK_SINGLE;
4207 DirectiveKindMap[".float"] = DK_FLOAT;
4208 DirectiveKindMap[".double"] = DK_DOUBLE;
4209 DirectiveKindMap[".align"] = DK_ALIGN;
4210 DirectiveKindMap[".align32"] = DK_ALIGN32;
4211 DirectiveKindMap[".balign"] = DK_BALIGN;
4212 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4213 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4214 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4215 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4216 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4217 DirectiveKindMap[".org"] = DK_ORG;
4218 DirectiveKindMap[".fill"] = DK_FILL;
4219 DirectiveKindMap[".zero"] = DK_ZERO;
4220 DirectiveKindMap[".extern"] = DK_EXTERN;
4221 DirectiveKindMap[".globl"] = DK_GLOBL;
4222 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004223 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4224 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4225 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4226 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4227 DirectiveKindMap[".reference"] = DK_REFERENCE;
4228 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4229 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4230 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4231 DirectiveKindMap[".comm"] = DK_COMM;
4232 DirectiveKindMap[".common"] = DK_COMMON;
4233 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4234 DirectiveKindMap[".abort"] = DK_ABORT;
4235 DirectiveKindMap[".include"] = DK_INCLUDE;
4236 DirectiveKindMap[".incbin"] = DK_INCBIN;
4237 DirectiveKindMap[".code16"] = DK_CODE16;
4238 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4239 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004240 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004241 DirectiveKindMap[".irp"] = DK_IRP;
4242 DirectiveKindMap[".irpc"] = DK_IRPC;
4243 DirectiveKindMap[".endr"] = DK_ENDR;
4244 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4245 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4246 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4247 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004248 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4249 DirectiveKindMap[".ifge"] = DK_IFGE;
4250 DirectiveKindMap[".ifgt"] = DK_IFGT;
4251 DirectiveKindMap[".ifle"] = DK_IFLE;
4252 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004253 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004254 DirectiveKindMap[".ifb"] = DK_IFB;
4255 DirectiveKindMap[".ifnb"] = DK_IFNB;
4256 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004257 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004258 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004259 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004260 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4261 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4262 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4263 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4264 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004265 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004266 DirectiveKindMap[".endif"] = DK_ENDIF;
4267 DirectiveKindMap[".skip"] = DK_SKIP;
4268 DirectiveKindMap[".space"] = DK_SPACE;
4269 DirectiveKindMap[".file"] = DK_FILE;
4270 DirectiveKindMap[".line"] = DK_LINE;
4271 DirectiveKindMap[".loc"] = DK_LOC;
4272 DirectiveKindMap[".stabs"] = DK_STABS;
4273 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4274 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4275 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4276 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4277 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4278 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4279 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4280 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4281 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4282 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4283 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4284 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4285 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4286 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4287 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4288 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4289 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4290 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4291 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4292 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4293 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004294 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004295 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4296 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4297 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004298 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004299 DirectiveKindMap[".endm"] = DK_ENDM;
4300 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4301 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004302 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004303 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004304 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004305}
4306
Jim Grosbach4b905842013-09-20 23:08:21 +00004307MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004308 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309
Rafael Espindola34b9c512012-06-03 23:57:14 +00004310 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004311 for (;;) {
4312 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004313 if (getLexer().is(AsmToken::Eof)) {
4314 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004315 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004316 }
4317
Rafael Espindola34b9c512012-06-03 23:57:14 +00004318 if (Lexer.is(AsmToken::Identifier) &&
4319 (getTok().getIdentifier() == ".rept")) {
4320 ++NestLevel;
4321 }
4322
4323 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004324 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004325 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004326 EndToken = getTok();
4327 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004328 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4329 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004330 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004331 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004332 break;
4333 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004334 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004335 }
4336
Rafael Espindola34b9c512012-06-03 23:57:14 +00004337 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004338 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004339 }
4340
4341 const char *BodyStart = StartToken.getLoc().getPointer();
4342 const char *BodyEnd = EndToken.getLoc().getPointer();
4343 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4344
Rafael Espindola34b9c512012-06-03 23:57:14 +00004345 // We Are Anonymous.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004346 MacroLikeBodies.push_back(
4347 MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004348 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004349}
4350
Jim Grosbach4b905842013-09-20 23:08:21 +00004351void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004352 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004353 OS << ".endr\n";
4354
Rafael Espindola3560ff22014-08-27 20:03:13 +00004355 std::unique_ptr<MemoryBuffer> Instantiation =
4356 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004357
Rafael Espindola34b9c512012-06-03 23:57:14 +00004358 // Create the macro instantiation object and add to the current macro
4359 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004360 MacroInstantiation *MI = new MacroInstantiation(
4361 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004362 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004363
Rafael Espindola34b9c512012-06-03 23:57:14 +00004364 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004365 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004366 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004367 Lex();
4368}
4369
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004370/// parseDirectiveRept
4371/// ::= .rep | .rept count
4372bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004373 const MCExpr *CountExpr;
4374 SMLoc CountLoc = getTok().getLoc();
4375 if (parseExpression(CountExpr))
4376 return true;
4377
Rafael Espindola34b9c512012-06-03 23:57:14 +00004378 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004379 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4380 eatToEndOfStatement();
4381 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4382 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004383
4384 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004385 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004386
4387 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004388 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004389
4390 // Eat the end of statement.
4391 Lex();
4392
4393 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004394 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004395 if (!M)
4396 return true;
4397
4398 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4399 // to hold the macro body with substitutions.
4400 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004401 raw_svector_ostream OS(Buf);
4402 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004403 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4404 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004405 return true;
4406 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004407 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004408
4409 return false;
4410}
4411
Jim Grosbach4b905842013-09-20 23:08:21 +00004412/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004413/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004414bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004415 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004416
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004417 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004418 return TokError("expected identifier in '.irp' directive");
4419
Rafael Espindola768b41c2012-06-15 14:02:34 +00004420 if (Lexer.isNot(AsmToken::Comma))
4421 return TokError("expected comma in '.irp' directive");
4422
4423 Lex();
4424
Eli Bendersky38274122013-01-14 23:22:36 +00004425 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004426 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004427 return true;
4428
4429 // Eat the end of statement.
4430 Lex();
4431
4432 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004433 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004434 if (!M)
4435 return true;
4436
4437 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4438 // to hold the macro body with substitutions.
4439 SmallString<256> Buf;
4440 raw_svector_ostream OS(Buf);
4441
Eli Bendersky38274122013-01-14 23:22:36 +00004442 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004443 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4444 // This is undocumented, but GAS seems to support it.
4445 if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004446 return true;
4447 }
4448
Jim Grosbach4b905842013-09-20 23:08:21 +00004449 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004450
4451 return false;
4452}
4453
Jim Grosbach4b905842013-09-20 23:08:21 +00004454/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004455/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004456bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004457 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004458
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004459 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004460 return TokError("expected identifier in '.irpc' directive");
4461
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004462 if (Lexer.isNot(AsmToken::Comma))
4463 return TokError("expected comma in '.irpc' directive");
4464
4465 Lex();
4466
Eli Bendersky38274122013-01-14 23:22:36 +00004467 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004468 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004469 return true;
4470
4471 if (A.size() != 1 || A.front().size() != 1)
4472 return TokError("unexpected token in '.irpc' directive");
4473
4474 // Eat the end of statement.
4475 Lex();
4476
4477 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004478 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004479 if (!M)
4480 return true;
4481
4482 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4483 // to hold the macro body with substitutions.
4484 SmallString<256> Buf;
4485 raw_svector_ostream OS(Buf);
4486
4487 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004488 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004489 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004490 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004491
Toma Tabacu217116e2015-04-27 10:50:29 +00004492 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4493 // This is undocumented, but GAS seems to support it.
4494 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004495 return true;
4496 }
4497
Jim Grosbach4b905842013-09-20 23:08:21 +00004498 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004499
4500 return false;
4501}
4502
Jim Grosbach4b905842013-09-20 23:08:21 +00004503bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004504 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004505 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004506
4507 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004508 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004509 assert(getLexer().is(AsmToken::EndOfStatement));
4510
Jim Grosbach4b905842013-09-20 23:08:21 +00004511 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004512 return false;
4513}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004514
Jim Grosbach4b905842013-09-20 23:08:21 +00004515bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004516 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004517 const MCExpr *Value;
4518 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004519 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004520 return true;
4521 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4522 if (!MCE)
4523 return Error(ExprLoc, "unexpected expression in _emit");
4524 uint64_t IntValue = MCE->getValue();
4525 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4526 return Error(ExprLoc, "literal value out of range for directive");
4527
Chad Rosierc7f552c2013-02-12 21:33:51 +00004528 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4529 return false;
4530}
4531
Jim Grosbach4b905842013-09-20 23:08:21 +00004532bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004533 const MCExpr *Value;
4534 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004535 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004536 return true;
4537 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4538 if (!MCE)
4539 return Error(ExprLoc, "unexpected expression in align");
4540 uint64_t IntValue = MCE->getValue();
4541 if (!isPowerOf2_64(IntValue))
4542 return Error(ExprLoc, "literal value not a power of two greater then zero");
4543
Jim Grosbach4b905842013-09-20 23:08:21 +00004544 Info.AsmRewrites->push_back(
4545 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004546 return false;
4547}
4548
Chad Rosierf43fcf52013-02-13 21:27:17 +00004549// We are comparing pointers, but the pointers are relative to a single string.
4550// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004551static int rewritesSort(const AsmRewrite *AsmRewriteA,
4552 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004553 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4554 return -1;
4555 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4556 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004557
Chad Rosierfce4fab2013-04-08 17:43:47 +00004558 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4559 // rewrite to the same location. Make sure the SizeDirective rewrite is
4560 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4561 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004562 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4563 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004564 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004565
Jim Grosbach4b905842013-09-20 23:08:21 +00004566 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4567 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004568 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004569 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004570}
4571
Jim Grosbach4b905842013-09-20 23:08:21 +00004572bool AsmParser::parseMSInlineAsm(
4573 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4574 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4575 SmallVectorImpl<std::string> &Constraints,
4576 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4577 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004578 SmallVector<void *, 4> InputDecls;
4579 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004580 SmallVector<bool, 4> InputDeclsAddressOf;
4581 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004582 SmallVector<std::string, 4> InputConstraints;
4583 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004584 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004585
Benjamin Kramer1a136112013-02-15 20:37:21 +00004586 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004587
4588 // Prime the lexer.
4589 Lex();
4590
4591 // While we have input, parse each statement.
4592 unsigned InputIdx = 0;
4593 unsigned OutputIdx = 0;
4594 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004595 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004596 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004597 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004598
Chad Rosier149e8e02012-12-12 22:45:52 +00004599 if (Info.ParseError)
4600 return true;
4601
Benjamin Kramer1a136112013-02-15 20:37:21 +00004602 if (Info.Opcode == ~0U)
4603 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004604
Benjamin Kramer1a136112013-02-15 20:37:21 +00004605 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004606
Benjamin Kramer1a136112013-02-15 20:37:21 +00004607 // Build the list of clobbers, outputs and inputs.
4608 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004609 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004610
Benjamin Kramer1a136112013-02-15 20:37:21 +00004611 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004612 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004613 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004614
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004616 if (Operand.isReg() && !Operand.needAddressOf() &&
4617 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004618 unsigned NumDefs = Desc.getNumDefs();
4619 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004620 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4621 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004622 continue;
4623 }
4624
4625 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004626 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004627 if (SymName.empty())
4628 continue;
4629
David Blaikie960ea3f2014-06-08 16:18:35 +00004630 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004631 if (!OpDecl)
4632 continue;
4633
4634 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004635 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004636 if (isOutput) {
4637 ++InputIdx;
4638 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004639 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004640 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004641 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004642 } else {
4643 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004644 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4645 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004646 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004647 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004648 }
Reid Kleckneree088972013-12-10 18:27:32 +00004649
4650 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004651 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4652 Desc.getNumImplicitDefs());
4653 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004654 }
4655
4656 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004657 NumOutputs = OutputDecls.size();
4658 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004659
4660 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004661 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4662 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4663 ClobberRegs.end());
4664 Clobbers.assign(ClobberRegs.size(), std::string());
4665 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4666 raw_string_ostream OS(Clobbers[I]);
4667 IP->printRegName(OS, ClobberRegs[I]);
4668 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004669
4670 // Merge the various outputs and inputs. Output are expected first.
4671 if (NumOutputs || NumInputs) {
4672 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004673 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004674 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004675 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004676 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004677 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004678 }
4679 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004680 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004681 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004682 }
4683 }
4684
4685 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004686 std::string AsmStringIR;
4687 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004688 StringRef ASMString =
4689 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4690 const char *AsmStart = ASMString.begin();
4691 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004692 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004693 for (const AsmRewrite &AR : AsmStrRewrites) {
4694 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004695 if (Kind == AOK_Delete)
4696 continue;
4697
David Majnemer8114c1a2014-06-23 02:17:16 +00004698 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004699 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004700
Chad Rosier120eefd2013-03-19 17:32:17 +00004701 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004702 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004703 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004704
Chad Rosier37e755c2012-10-23 17:43:43 +00004705 // Skip the original expression.
4706 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004707 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004708 continue;
4709 }
4710
Chad Rosierff10ed12013-04-12 16:26:42 +00004711 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004712 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004713 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004714 default:
4715 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004716 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004717 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004718 break;
4719 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004720 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004721 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004722 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004723 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004724 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004725 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004726 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004727 break;
4728 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004729 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004730 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004731 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004732 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004733 default: break;
4734 case 8: OS << "byte ptr "; break;
4735 case 16: OS << "word ptr "; break;
4736 case 32: OS << "dword ptr "; break;
4737 case 64: OS << "qword ptr "; break;
4738 case 80: OS << "xword ptr "; break;
4739 case 128: OS << "xmmword ptr "; break;
4740 case 256: OS << "ymmword ptr "; break;
4741 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004742 break;
4743 case AOK_Emit:
4744 OS << ".byte";
4745 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004746 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004747 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004748 OS << ".align " << Val;
4749
4750 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004751 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004752 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4753 break;
4754 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004755 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004756 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004757 OS.flush();
4758 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004759 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004760 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004761 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004762 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004763
Chad Rosier8bce6642012-10-18 15:49:34 +00004764 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004765 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004766 }
4767
4768 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004769 if (AsmStart != AsmEnd)
4770 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004771
4772 AsmString = OS.str();
4773 return false;
4774}
4775
Daniel Dunbar01e36072010-07-17 02:26:10 +00004776/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004777MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4778 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004779 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004780}