blob: fef6bb6b641cb12071537471f1809f25fb379031 [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"
Pete Cooper80d21cb2015-06-22 19:35:57 +000029#include "llvm/MC/MCParser/MCAsmParserUtils.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000030#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000031#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Evan Cheng76792992011-07-20 05:58:47 +000032#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000033#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000034#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000035#include "llvm/MC/MCSymbol.h"
Daniel Sanders9f6ad492015-11-12 13:33:00 +000036#include "llvm/MC/MCValue.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000037#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000038#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000039#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000040#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000041#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000042#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000043#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000044#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000045#include <set>
46#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000047#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000048using namespace llvm;
49
Eric Christophera7c32732012-12-18 00:30:54 +000050MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000051
Daniel Dunbar86033402010-07-12 17:54:38 +000052namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000053/// \brief Helper types for tracking macro definitions.
54typedef std::vector<AsmToken> MCAsmMacroArgument;
55typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000056
57struct MCAsmMacroParameter {
58 StringRef Name;
59 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000064};
65
Eli Benderskya313ae62013-01-16 18:56:50 +000066typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
67
68struct MCAsmMacro {
69 StringRef Name;
70 StringRef Body;
71 MCAsmMacroParameters Parameters;
72
73public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000074 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
75 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000076};
77
Daniel Dunbar43235712010-07-18 18:54:11 +000078/// \brief Helper class for storing information about an active macro
79/// instantiation.
80struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000081 /// The location of the instantiation.
82 SMLoc InstantiationLoc;
83
Daniel Dunbar40f1d852012-12-01 01:38:48 +000084 /// The buffer where parsing should resume upon instantiation completion.
85 int ExitBuffer;
86
Daniel Dunbar43235712010-07-18 18:54:11 +000087 /// The location where parsing should resume upon instantiation completion.
88 SMLoc ExitLoc;
89
Nico Weber155dccd12014-07-24 17:08:39 +000090 /// The depth of TheCondStack at the start of the instantiation.
91 size_t CondStackDepth;
92
Daniel Dunbar43235712010-07-18 18:54:11 +000093public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000094 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000099 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Craig Topper353eda42014-04-24 06:44:33 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112};
113
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000114/// \brief The concrete assembly parser instance.
115class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000116 AsmParser(const AsmParser &) = delete;
117 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000118private:
119 AsmLexer Lexer;
120 MCContext &Ctx;
121 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000122 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000123 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000124 SourceMgr::DiagHandlerTy SavedDiagHandler;
125 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000126 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000127
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000128 /// This is the current buffer index we're lexing from as managed by the
129 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000130 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131
132 AsmCond TheCondState;
133 std::vector<AsmCond> TheCondStack;
134
Jim Grosbach4b905842013-09-20 23:08:21 +0000135 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000136 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000137 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000138 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000139
Jim Grosbach4b905842013-09-20 23:08:21 +0000140 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000141 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000142
Jim Grosbach4b905842013-09-20 23:08:21 +0000143 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000144 std::vector<MacroInstantiation*> ActiveMacros;
145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000147 std::deque<MCAsmMacro> MacroLikeBodies;
148
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000150 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000151
Toma Tabacu217116e2015-04-27 10:50:29 +0000152 /// \brief Keeps track of how many .macro's have been instantiated.
153 unsigned NumOfMacroInstantiations;
154
Daniel Dunbar43325c42010-09-09 22:42:56 +0000155 /// Flag tracking whether any errors have been encountered.
156 unsigned HadError : 1;
157
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000158 /// The values from the last parsed cpp hash file line comment if any.
159 StringRef CppHashFilename;
160 int64_t CppHashLineNumber;
161 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000162 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000163 /// When generating dwarf for assembly source files we need to calculate the
164 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000165 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
167 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000168 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000170
Devang Patela173ee52012-01-31 18:14:05 +0000171 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
172 unsigned AssemblerDialect;
173
Jim Grosbach4b905842013-09-20 23:08:21 +0000174 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000175 bool IsDarwin;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000178 bool ParsingInlineAsm;
179
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000181 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000183 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186
Craig Topper59be68f2014-03-08 07:14:16 +0000187 void addDirectiveHandler(StringRef Directive,
188 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000189 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 }
191
Toma Tabacu11e14a92015-04-21 11:50:52 +0000192 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
193 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
194 }
195
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000196public:
197 /// @name MCAsmParser Interface
198 /// {
199
Craig Topper59be68f2014-03-08 07:14:16 +0000200 SourceMgr &getSourceManager() override { return SrcMgr; }
201 MCAsmLexer &getLexer() override { return Lexer; }
202 MCContext &getContext() override { return Ctx; }
203 MCStreamer &getStreamer() override { return Out; }
204 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
Craig Topper59be68f2014-03-08 07:14:16 +0000210 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void Note(SMLoc L, const Twine &Msg,
215 ArrayRef<SMRange> Ranges = None) override;
216 bool Warning(SMLoc L, const Twine &Msg,
217 ArrayRef<SMRange> Ranges = None) override;
218 bool Error(SMLoc L, const Twine &Msg,
219 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper59be68f2014-03-08 07:14:16 +0000221 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000222
Craig Topper59be68f2014-03-08 07:14:16 +0000223 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
224 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000225
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000226 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000228 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000231 const MCInstrInfo *MII, const MCInstPrinter *IP,
232 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000233
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000234 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000235 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000238 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
239 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000240 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000241
Jim Grosbach4b905842013-09-20 23:08:21 +0000242 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000244 bool parseIdentifier(StringRef &Res) override;
245 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000246
Craig Topper59be68f2014-03-08 07:14:16 +0000247 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000248 /// }
249
250private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000251
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000252 bool parseStatement(ParseStatementInfo &Info,
253 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000254 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +0000255 void eatToEndOfLine();
Craig Topper3c76c522015-09-20 23:35:59 +0000256 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000257
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000259 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000260 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000262 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000263 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000264
Eli Benderskya313ae62013-01-16 18:56:50 +0000265 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Lookup a previously defined macro.
272 /// \param Name Macro name.
273 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000277 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000285 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000286 ///
287 /// \param M The macro.
288 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
291 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
David Majnemer91fc4c22014-01-29 18:57:46 +0000294 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000295 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
297 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000299
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 void printMacroInstantiations();
301 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000302 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000303 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000305 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 /// \brief Enter the specified file. This returns true on failure.
308 bool enterIncludeFile(const std::string &Filename);
309
310 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000311 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000312 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000314 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000315 /// current token is not set; clients should ensure Lex() is called
316 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000317 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000318 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000319 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000320 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000321
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000322 /// \brief Parse up to the end of statement and a return the contents from the
323 /// current token until the end of the statement; the current token on exit
324 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000325 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000326
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000327 /// \brief Parse until the end of a statement or a comma is encountered,
328 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000332 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000333
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000334 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
335 MCBinaryExpr::Opcode &Kind);
336
Jim Grosbach4b905842013-09-20 23:08:21 +0000337 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
338 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
339 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000340
Jim Grosbach4b905842013-09-20 23:08:21 +0000341 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000342
Eli Bendersky17233942013-01-15 22:59:42 +0000343 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000344 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_NO_DIRECTIVE, // Placeholder
346 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000347 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000348 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
349 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000350 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000351 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000352 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hames1b640e02016-03-15 01:43:05 +0000353 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_ALT_ENTRY,
354 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000355 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
356 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000357 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000358 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
359 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000360 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000361 DK_CV_FILE, DK_CV_LOC, DK_CV_LINETABLE, DK_CV_INLINE_LINETABLE,
David Majnemer408b5e62016-02-05 01:55:49 +0000362 DK_CV_DEF_RANGE, DK_CV_STRINGTABLE, DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000363 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
364 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
365 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
366 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
367 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000368 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000369 DK_MACROS_ON, DK_MACROS_OFF,
370 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000371 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000372 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000373 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000374 };
375
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000377 /// directives parsed by this class.
378 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000379
380 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000381 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000382 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000384 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
386 bool parseDirectiveFill(); // ".fill"
387 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000388 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
390 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000391 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000393
Eli Bendersky17233942013-01-15 22:59:42 +0000394 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000395 bool parseDirectiveFile(SMLoc DirectiveLoc);
396 bool parseDirectiveLine();
397 bool parseDirectiveLoc();
398 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000399
David Majnemer408b5e62016-02-05 01:55:49 +0000400 // ".cv_file", ".cv_loc", ".cv_linetable", "cv_inline_linetable",
401 // ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000402 bool parseDirectiveCVFile();
403 bool parseDirectiveCVLoc();
404 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000405 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000406 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000407 bool parseDirectiveCVStringTable();
408 bool parseDirectiveCVFileChecksums();
409
Eli Bendersky17233942013-01-15 22:59:42 +0000410 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000411 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000412 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveCFISections();
414 bool parseDirectiveCFIStartProc();
415 bool parseDirectiveCFIEndProc();
416 bool parseDirectiveCFIDefCfaOffset();
417 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
418 bool parseDirectiveCFIAdjustCfaOffset();
419 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
420 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
421 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
422 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
423 bool parseDirectiveCFIRememberState();
424 bool parseDirectiveCFIRestoreState();
425 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
426 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
427 bool parseDirectiveCFIEscape();
428 bool parseDirectiveCFISignalFrame();
429 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000430
431 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000433 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveEndMacro(StringRef Directive);
435 bool parseDirectiveMacro(SMLoc DirectiveLoc);
436 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000437
Eli Benderskyf483ff92012-12-20 19:05:53 +0000438 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000440 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000442 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000443 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000444
Eli Bendersky17233942013-01-15 22:59:42 +0000445 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000447
448 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000449 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000450
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000452 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000454
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000456
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 bool parseDirectiveAbort(); // ".abort"
458 bool parseDirectiveInclude(); // ".include"
459 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000460
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000461 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
462 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000463 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000465 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000466 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000467 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
468 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000469 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
471 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
472 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
473 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000474 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000475
Jim Grosbach4b905842013-09-20 23:08:21 +0000476 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000477 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000478
Rafael Espindola34b9c512012-06-03 23:57:14 +0000479 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000480 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
481 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000482 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000483 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000484 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
485 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
486 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000487
Chad Rosierc7f552c2013-02-12 21:33:51 +0000488 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000489 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000490 size_t Len);
491
492 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000493 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000494
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000495 // "end"
496 bool parseDirectiveEnd(SMLoc DirectiveLoc);
497
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000498 // ".err" or ".error"
499 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000500
Nico Weber404012b2014-07-24 16:26:06 +0000501 // ".warning"
502 bool parseDirectiveWarning(SMLoc DirectiveLoc);
503
Eli Bendersky17233942013-01-15 22:59:42 +0000504 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000505};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000506}
Daniel Dunbar86033402010-07-12 17:54:38 +0000507
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000508namespace llvm {
509
510extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000511extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000512extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000513
514}
515
Chris Lattnerc35681b2010-01-19 19:46:13 +0000516enum { DEFAULT_ADDRSPACE = 0 };
517
David Blaikie9f380a32015-03-16 18:06:57 +0000518AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
519 const MCAsmInfo &MAI)
520 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
521 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000522 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000523 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000524 // Save the old handler.
525 SavedDiagHandler = SrcMgr.getDiagHandler();
526 SavedDiagContext = SrcMgr.getDiagContext();
527 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000528 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000529 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000530
Daniel Dunbarc5011082010-07-12 18:12:02 +0000531 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000532 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
533 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000534 PlatformParser.reset(createCOFFAsmParser());
535 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000536 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000537 PlatformParser.reset(createDarwinAsmParser());
538 IsDarwin = true;
539 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000540 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000541 PlatformParser.reset(createELFAsmParser());
542 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000543 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000544
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000545 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000546 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000547
548 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000549}
550
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000551AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000552 assert((HadError || ActiveMacros.empty()) &&
553 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000554}
555
Jim Grosbach4b905842013-09-20 23:08:21 +0000556void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000557 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000558 for (std::vector<MacroInstantiation *>::const_reverse_iterator
559 it = ActiveMacros.rbegin(),
560 ie = ActiveMacros.rend();
561 it != ie; ++it)
562 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000563 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000564}
565
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000566void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
567 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
568 printMacroInstantiations();
569}
570
Chris Lattnera3a06812011-10-16 04:47:35 +0000571bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000572 if(getTargetParser().getTargetOptions().MCNoWarn)
573 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000574 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000575 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000576 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
577 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000578 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000579}
580
Chris Lattnera3a06812011-10-16 04:47:35 +0000581bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000582 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000583 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
584 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000585 return true;
586}
587
Jim Grosbach4b905842013-09-20 23:08:21 +0000588bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000589 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000590 unsigned NewBuf =
591 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
592 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000593 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000594
Sean Callanan7a77eae2010-01-21 00:19:58 +0000595 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597 return false;
598}
Daniel Dunbar43235712010-07-18 18:54:11 +0000599
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000600/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000601/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000602/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000603bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000604 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000605 unsigned NewBuf =
606 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
607 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000608 return true;
609
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000610 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000611 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000612 return false;
613}
614
Alp Tokera55b95b2014-07-06 10:33:31 +0000615void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
616 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000617 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
618 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000619}
620
Sean Callanan7a77eae2010-01-21 00:19:58 +0000621const AsmToken &AsmParser::Lex() {
622 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000623
Sean Callanan7a77eae2010-01-21 00:19:58 +0000624 if (tok->is(AsmToken::Eof)) {
625 // If this is the end of an included file, pop the parent file off the
626 // include stack.
627 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
628 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000629 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000630 tok = &Lexer.Lex();
631 }
632 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000633
Sean Callanan7a77eae2010-01-21 00:19:58 +0000634 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000635 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000636
Sean Callanan7a77eae2010-01-21 00:19:58 +0000637 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000638}
639
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000640bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000641 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000642 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000643 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000644
Chris Lattner36e02122009-06-21 20:54:55 +0000645 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000646 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000647
648 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000649 AsmCond StartingCondState = TheCondState;
650
Kevin Enderby6469fc22011-11-01 22:27:22 +0000651 // If we are generating dwarf for assembly source files save the initial text
652 // section and generate a .file directive.
653 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000654 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000655 if (!Sec->getBeginSymbol()) {
656 MCSymbol *SectionStartSym = getContext().createTempSymbol();
657 getStreamer().EmitLabel(SectionStartSym);
658 Sec->setBeginSymbol(SectionStartSym);
659 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000660 bool InsertResult = getContext().addGenDwarfSection(Sec);
661 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000662 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000663 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
664 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000665 }
666
Chris Lattner73f36112009-07-02 21:53:43 +0000667 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000668 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000669 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000670 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000671 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000672
Daniel Dunbar43325c42010-09-09 22:42:56 +0000673 // We had an error, validate that one was emitted and recover by skipping to
674 // the next line.
675 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000676 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000677 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000678
679 if (TheCondState.TheCond != StartingCondState.TheCond ||
680 TheCondState.Ignore != StartingCondState.Ignore)
681 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000682
683 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000684 const auto &LineTables = getContext().getMCDwarfLineTables();
685 if (!LineTables.empty()) {
686 unsigned Index = 0;
687 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
688 if (File.Name.empty() && Index != 0)
689 TokError("unassigned file number: " + Twine(Index) +
690 " for .file directives");
691 ++Index;
692 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000693 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000694
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000695 // Check to see that all assembler local symbols were actually defined.
696 // Targets that don't do subsections via symbols may not want this, though,
697 // so conservatively exclude them. Only do this if we're finalizing, though,
698 // as otherwise we won't necessarilly have seen everything yet.
699 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
Craig Topper84008482015-10-10 05:38:14 +0000700 for (const auto &TableEntry : getContext().getSymbols()) {
701 MCSymbol *Sym = TableEntry.getValue();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000702 // Variable symbols may not be marked as defined, so check those
703 // explicitly. If we know it's a variable, we have a definition for
704 // the purposes of this check.
705 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
706 // FIXME: We would really like to refer back to where the symbol was
707 // first referenced for a source location. We need to add something
708 // to track that. Currently, we just point to the end of the file.
Jim Grosbach0fdd5722015-10-16 22:07:59 +0000709 return Error(getLexer().getLoc(), "assembler local symbol '" +
710 Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000711 }
712 }
713
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000714 // Finalize the output stream if there are no errors and if the client wants
715 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000716 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000717 Out.Finish();
718
Oliver Stannard07b43d32015-11-17 09:58:07 +0000719 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000720}
721
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000722void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000723 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000724 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000725 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000726 }
727}
728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000730void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000731 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000732 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000733
Chris Lattnere5074c42009-06-22 01:29:09 +0000734 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000735 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000736 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000737}
738
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000739StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000740 const char *Start = getTok().getLoc().getPointer();
741
Jim Grosbach4b905842013-09-20 23:08:21 +0000742 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000743 Lex();
744
745 const char *End = getTok().getLoc().getPointer();
746 return StringRef(Start, End - Start);
747}
Chris Lattner78db3622009-06-22 05:51:26 +0000748
Jim Grosbach4b905842013-09-20 23:08:21 +0000749StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000750 const char *Start = getTok().getLoc().getPointer();
751
752 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000753 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000754 Lex();
755
756 const char *End = getTok().getLoc().getPointer();
757 return StringRef(Start, End - Start);
758}
759
Jim Grosbach4b905842013-09-20 23:08:21 +0000760/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000761/// NOTE: This assumes the leading '(' has already been consumed.
762///
763/// parenexpr ::= expr)
764///
Jim Grosbach4b905842013-09-20 23:08:21 +0000765bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
766 if (parseExpression(Res))
767 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000768 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000770 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000771 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772 return false;
773}
Chris Lattner78db3622009-06-22 05:51:26 +0000774
Jim Grosbach4b905842013-09-20 23:08:21 +0000775/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000776/// NOTE: This assumes the leading '[' has already been consumed.
777///
778/// bracketexpr ::= expr]
779///
Jim Grosbach4b905842013-09-20 23:08:21 +0000780bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
781 if (parseExpression(Res))
782 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000783 if (Lexer.isNot(AsmToken::RBrac))
784 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000785 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000786 Lex();
787 return false;
788}
789
Jim Grosbach4b905842013-09-20 23:08:21 +0000790/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000791/// primaryexpr ::= (parenexpr
792/// primaryexpr ::= symbol
793/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000794/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000795/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000796bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000797 SMLoc FirstTokenLoc = getLexer().getLoc();
798 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
799 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000800 default:
801 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000802 // If we have an error assume that we've already handled it.
803 case AsmToken::Error:
804 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000805 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000806 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000807 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000808 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000809 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000810 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000811 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000812 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000813 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000814 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000815 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000816 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000817 if (FirstTokenKind == AsmToken::Dollar) {
818 if (Lexer.getMAI().getDollarIsPC()) {
819 // This is a '$' reference, which references the current PC. Emit a
820 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000821 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000822 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000823 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000824 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000825 EndLoc = FirstTokenLoc;
826 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000827 }
828 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000829 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000830 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000831 // Parse symbol variant
832 std::pair<StringRef, StringRef> Split;
833 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000834 if (FirstTokenKind == AsmToken::String) {
835 if (Lexer.is(AsmToken::At)) {
836 Lexer.Lex(); // eat @
837 SMLoc AtLoc = getLexer().getLoc();
838 StringRef VName;
839 if (parseIdentifier(VName))
840 return Error(AtLoc, "expected symbol variant after '@'");
841
842 Split = std::make_pair(Identifier, VName);
843 }
844 } else {
845 Split = Identifier.split('@');
846 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000847 } else if (Lexer.is(AsmToken::LParen)) {
848 Lexer.Lex(); // eat (
849 StringRef VName;
850 parseIdentifier(VName);
851 if (Lexer.isNot(AsmToken::RParen)) {
852 return Error(Lexer.getTok().getLoc(),
853 "unexpected token in variant, expected ')'");
854 }
855 Lexer.Lex(); // eat )
856 Split = std::make_pair(Identifier, VName);
857 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000858
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000859 EndLoc = SMLoc::getFromPointer(Identifier.end());
860
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000862 StringRef SymbolName = Identifier;
863 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000864
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000865 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000866 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000867 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000868 if (Variant != MCSymbolRefExpr::VK_Invalid) {
869 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000870 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000871 Variant = MCSymbolRefExpr::VK_None;
872 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000873 return Error(SMLoc::getFromPointer(Split.second.begin()),
874 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000875 }
876 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000877
Jim Grosbach6f482002015-05-18 18:43:14 +0000878 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000879
Daniel Dunbard20cda02009-10-16 01:34:54 +0000880 // If this is an absolute variable reference, substitute it now to preserve
881 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000882 if (Sym->isVariable() &&
883 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000884 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000885 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000886
Vedant Kumar86dbd922015-08-31 17:44:53 +0000887 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000888 return false;
889 }
890
891 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000892 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000893 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000894 }
David Woodhousef42a6662014-02-01 16:20:54 +0000895 case AsmToken::BigNum:
896 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 case AsmToken::Integer: {
898 SMLoc Loc = getTok().getLoc();
899 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000900 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000901 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000902 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000903 // Look for 'b' or 'f' following an Integer as a directional label
904 if (Lexer.getKind() == AsmToken::Identifier) {
905 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000906 // Lookup the symbol variant if used.
907 std::pair<StringRef, StringRef> Split = IDVal.split('@');
908 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
909 if (Split.first.size() != IDVal.size()) {
910 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000911 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000912 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000913 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000914 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000915 if (IDVal == "f" || IDVal == "b") {
916 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000917 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000918 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000919 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000920 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000921 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000922 Lex(); // Eat identifier.
923 }
924 }
Chris Lattner78db3622009-06-22 05:51:26 +0000925 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000926 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000927 case AsmToken::Real: {
928 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000929 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000930 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000931 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000932 Lex(); // Eat token.
933 return false;
934 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000935 case AsmToken::Dot: {
936 // This is a '.' reference, which references the current PC. Emit a
937 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000938 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000939 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000940 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000941 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000942 Lex(); // Eat identifier.
943 return false;
944 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000945 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000946 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000947 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000948 case AsmToken::LBrac:
949 if (!PlatformParser->HasBracketExpressions())
950 return TokError("brackets expression not supported on this target");
951 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000952 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000953 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000954 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000956 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000957 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000958 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000959 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000960 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000961 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000962 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000963 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000964 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000965 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000966 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000967 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000968 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000969 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000970 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000971 }
972}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000973
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000974bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000975 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000976 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000977}
978
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000980AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000982 // Ask the target implementation about this expression first.
983 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
984 if (NewE)
985 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 // Recurse over the given expression, rebuilding it to apply the given variant
987 // if there is exactly one symbol.
988 switch (E->getKind()) {
989 case MCExpr::Target:
990 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000991 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992
993 case MCExpr::SymbolRef: {
994 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
995
996 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 TokError("invalid variant on expression '" + getTok().getIdentifier() +
998 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999 return E;
1000 }
1001
Jim Grosbach13760bd2015-05-30 01:25:56 +00001002 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001003 }
1004
1005 case MCExpr::Unary: {
1006 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001007 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001008 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001009 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001010 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001011 }
1012
1013 case MCExpr::Binary: {
1014 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001015 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1016 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001017
1018 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001019 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001020
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 if (!LHS)
1022 LHS = BE->getLHS();
1023 if (!RHS)
1024 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025
Jim Grosbach13760bd2015-05-30 01:25:56 +00001026 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 }
1028 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001029
Craig Toppera2886c22012-02-07 05:05:23 +00001030 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031}
1032
Jim Grosbach4b905842013-09-20 23:08:21 +00001033/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001034///
Jim Grosbachbd164242011-08-20 16:24:13 +00001035/// expr ::= expr &&,|| expr -> lowest.
1036/// expr ::= expr |,^,&,! expr
1037/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1038/// expr ::= expr <<,>> expr
1039/// expr ::= expr +,- expr
1040/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001041/// expr ::= primaryexpr
1042///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001043bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001044 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001045 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001046 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001047 return true;
1048
Daniel Dunbar55f16672010-09-17 02:47:07 +00001049 // As a special case, we support 'a op b @ modifier' by rewriting the
1050 // expression to include the modifier. This is inefficient, but in general we
1051 // expect users to use 'a@modifier op b'.
1052 if (Lexer.getKind() == AsmToken::At) {
1053 Lex();
1054
1055 if (Lexer.isNot(AsmToken::Identifier))
1056 return TokError("unexpected symbol modifier following '@'");
1057
1058 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001059 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001060 if (Variant == MCSymbolRefExpr::VK_Invalid)
1061 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1062
Jim Grosbach4b905842013-09-20 23:08:21 +00001063 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001064 if (!ModifiedRes) {
1065 return TokError("invalid modifier '" + getTok().getIdentifier() +
1066 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001067 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001068
Daniel Dunbar55f16672010-09-17 02:47:07 +00001069 Res = ModifiedRes;
1070 Lex();
1071 }
1072
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001073 // Try to constant fold it up front, if possible.
1074 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001075 if (Res->evaluateAsAbsolute(Value))
1076 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001077
1078 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001079}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001080
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001081bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001082 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001083 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001084}
1085
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001086bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1087 SMLoc &EndLoc) {
1088 if (parseParenExpr(Res, EndLoc))
1089 return true;
1090
1091 for (; ParenDepth > 0; --ParenDepth) {
1092 if (parseBinOpRHS(1, Res, EndLoc))
1093 return true;
1094
1095 // We don't Lex() the last RParen.
1096 // This is the same behavior as parseParenExpression().
1097 if (ParenDepth - 1 > 0) {
1098 if (Lexer.isNot(AsmToken::RParen))
1099 return TokError("expected ')' in parentheses expression");
1100 EndLoc = Lexer.getTok().getEndLoc();
1101 Lex();
1102 }
1103 }
1104 return false;
1105}
1106
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001107bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001108 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001109
Daniel Dunbar75630b32009-06-30 02:10:03 +00001110 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001111 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001112 return true;
1113
Jim Grosbach13760bd2015-05-30 01:25:56 +00001114 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001115 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001116
1117 return false;
1118}
1119
David Majnemer0993e0b2015-10-26 03:15:34 +00001120static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1121 MCBinaryExpr::Opcode &Kind,
1122 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001123 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001124 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001125 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001126
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001130 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001133 return 1;
1134
Jim Grosbach4b905842013-09-20 23:08:21 +00001135 // Low Precedence: |, &, ^
1136 //
1137 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001140 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001143 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001144 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001146 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001147
Jim Grosbach4b905842013-09-20 23:08:21 +00001148 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001149 case AsmToken::EqualEqual:
1150 Kind = MCBinaryExpr::EQ;
1151 return 3;
1152 case AsmToken::ExclaimEqual:
1153 case AsmToken::LessGreater:
1154 Kind = MCBinaryExpr::NE;
1155 return 3;
1156 case AsmToken::Less:
1157 Kind = MCBinaryExpr::LT;
1158 return 3;
1159 case AsmToken::LessEqual:
1160 Kind = MCBinaryExpr::LTE;
1161 return 3;
1162 case AsmToken::Greater:
1163 Kind = MCBinaryExpr::GT;
1164 return 3;
1165 case AsmToken::GreaterEqual:
1166 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167 return 3;
1168
Jim Grosbach4b905842013-09-20 23:08:21 +00001169 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001170 case AsmToken::LessLess:
1171 Kind = MCBinaryExpr::Shl;
1172 return 4;
1173 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001174 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001175 return 4;
1176
Jim Grosbach4b905842013-09-20 23:08:21 +00001177 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001178 case AsmToken::Plus:
1179 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001180 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001181 case AsmToken::Minus:
1182 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001183 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001184
Jim Grosbach4b905842013-09-20 23:08:21 +00001185 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001186 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001187 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001188 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001189 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001190 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001191 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001192 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001193 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001194 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001195 }
1196}
1197
David Majnemer0993e0b2015-10-26 03:15:34 +00001198static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1199 MCBinaryExpr::Opcode &Kind,
1200 bool ShouldUseLogicalShr) {
1201 switch (K) {
1202 default:
1203 return 0; // not a binop.
1204
1205 // Lowest Precedence: &&, ||
1206 case AsmToken::AmpAmp:
1207 Kind = MCBinaryExpr::LAnd;
1208 return 2;
1209 case AsmToken::PipePipe:
1210 Kind = MCBinaryExpr::LOr;
1211 return 1;
1212
1213 // Low Precedence: ==, !=, <>, <, <=, >, >=
1214 case AsmToken::EqualEqual:
1215 Kind = MCBinaryExpr::EQ;
1216 return 3;
1217 case AsmToken::ExclaimEqual:
1218 case AsmToken::LessGreater:
1219 Kind = MCBinaryExpr::NE;
1220 return 3;
1221 case AsmToken::Less:
1222 Kind = MCBinaryExpr::LT;
1223 return 3;
1224 case AsmToken::LessEqual:
1225 Kind = MCBinaryExpr::LTE;
1226 return 3;
1227 case AsmToken::Greater:
1228 Kind = MCBinaryExpr::GT;
1229 return 3;
1230 case AsmToken::GreaterEqual:
1231 Kind = MCBinaryExpr::GTE;
1232 return 3;
1233
1234 // Low Intermediate Precedence: +, -
1235 case AsmToken::Plus:
1236 Kind = MCBinaryExpr::Add;
1237 return 4;
1238 case AsmToken::Minus:
1239 Kind = MCBinaryExpr::Sub;
1240 return 4;
1241
1242 // High Intermediate Precedence: |, &, ^
1243 //
1244 // FIXME: gas seems to support '!' as an infix operator?
1245 case AsmToken::Pipe:
1246 Kind = MCBinaryExpr::Or;
1247 return 5;
1248 case AsmToken::Caret:
1249 Kind = MCBinaryExpr::Xor;
1250 return 5;
1251 case AsmToken::Amp:
1252 Kind = MCBinaryExpr::And;
1253 return 5;
1254
1255 // Highest Precedence: *, /, %, <<, >>
1256 case AsmToken::Star:
1257 Kind = MCBinaryExpr::Mul;
1258 return 6;
1259 case AsmToken::Slash:
1260 Kind = MCBinaryExpr::Div;
1261 return 6;
1262 case AsmToken::Percent:
1263 Kind = MCBinaryExpr::Mod;
1264 return 6;
1265 case AsmToken::LessLess:
1266 Kind = MCBinaryExpr::Shl;
1267 return 6;
1268 case AsmToken::GreaterGreater:
1269 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1270 return 6;
1271 }
1272}
1273
1274unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1275 MCBinaryExpr::Opcode &Kind) {
1276 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1277 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1278 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1279}
1280
Jim Grosbach4b905842013-09-20 23:08:21 +00001281/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001282/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001283bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001284 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001285 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001286 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001287 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001288
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001289 // If the next token is lower precedence than we are allowed to eat, return
1290 // successfully with what we ate already.
1291 if (TokPrec < Precedence)
1292 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001293
Sean Callanan686ed8d2010-01-19 20:22:31 +00001294 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001295
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001296 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001297 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001298 if (parsePrimaryExpr(RHS, EndLoc))
1299 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001300
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001301 // If BinOp binds less tightly with RHS than the operator after RHS, let
1302 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001303 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001304 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001305 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1306 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001307
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001308 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001309 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001310 }
1311}
1312
Chris Lattner36e02122009-06-21 20:54:55 +00001313/// ParseStatement:
1314/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001315/// ::= Label* Directive ...Operands... EndOfStatement
1316/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001317bool AsmParser::parseStatement(ParseStatementInfo &Info,
1318 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001319 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001320 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001321 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001322 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001323 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001324
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001325 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001326 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001327 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001328 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001329 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001330 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001331 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001332 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001333
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001334 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001335 if (Lexer.is(AsmToken::Integer)) {
1336 LocalLabelVal = getTok().getIntVal();
1337 if (LocalLabelVal < 0) {
1338 if (!TheCondState.Ignore)
1339 return TokError("unexpected token at start of statement");
1340 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001341 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001342 IDVal = getTok().getString();
1343 Lex(); // Consume the integer token to be used as an identifier token.
1344 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001345 if (!TheCondState.Ignore)
1346 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001347 }
1348 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001349 } else if (Lexer.is(AsmToken::Dot)) {
1350 // Treat '.' as a valid identifier in this context.
1351 Lex();
1352 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001353 } else if (Lexer.is(AsmToken::LCurly)) {
1354 // Treat '{' as a valid identifier in this context.
1355 Lex();
1356 IDVal = "{";
1357
1358 } else if (Lexer.is(AsmToken::RCurly)) {
1359 // Treat '}' as a valid identifier in this context.
1360 Lex();
1361 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001362 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001363 if (!TheCondState.Ignore)
1364 return TokError("unexpected token at start of statement");
1365 IDVal = "";
1366 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001367
Chris Lattner926885c2010-04-17 18:14:27 +00001368 // Handle conditional assembly here before checking for skipping. We
1369 // have to do this so that .endif isn't skipped in a ".if 0" block for
1370 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001371 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001372 DirectiveKindMap.find(IDVal);
1373 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1374 ? DK_NO_DIRECTIVE
1375 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001376 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001377 default:
1378 break;
1379 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001380 case DK_IFEQ:
1381 case DK_IFGE:
1382 case DK_IFGT:
1383 case DK_IFLE:
1384 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001385 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001386 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001387 case DK_IFB:
1388 return parseDirectiveIfb(IDLoc, true);
1389 case DK_IFNB:
1390 return parseDirectiveIfb(IDLoc, false);
1391 case DK_IFC:
1392 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001393 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001394 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001395 case DK_IFNC:
1396 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001397 case DK_IFNES:
1398 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001399 case DK_IFDEF:
1400 return parseDirectiveIfdef(IDLoc, true);
1401 case DK_IFNDEF:
1402 case DK_IFNOTDEF:
1403 return parseDirectiveIfdef(IDLoc, false);
1404 case DK_ELSEIF:
1405 return parseDirectiveElseIf(IDLoc);
1406 case DK_ELSE:
1407 return parseDirectiveElse(IDLoc);
1408 case DK_ENDIF:
1409 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001410 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001411
Eli Bendersky88024712013-01-16 19:32:36 +00001412 // Ignore the statement if in the middle of inactive conditional
1413 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001414 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001415 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001416 return false;
1417 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001418
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001419 // FIXME: Recurse on local labels?
1420
1421 // See what kind of statement we have.
1422 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001423 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001424 if (!getTargetParser().isLabel(ID))
1425 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001426 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001427
Chris Lattner36e02122009-06-21 20:54:55 +00001428 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001429 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001430
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001431 // Diagnose attempt to use '.' as a label.
1432 if (IDVal == ".")
1433 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1434
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001435 // Diagnose attempt to use a variable as a label.
1436 //
1437 // FIXME: Diagnostics. Note the location of the definition as a label.
1438 // FIXME: This doesn't diagnose assignment to a symbol which has been
1439 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001440 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001441 if (LocalLabelVal == -1) {
1442 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001443 StringRef RewrittenLabel =
1444 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1445 assert(RewrittenLabel.size() &&
1446 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001447 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1448 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001449 IDVal = RewrittenLabel;
1450 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001451 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001452 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001453 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001454
1455 Sym->redefineIfPossible();
1456
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001457 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001458 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001459
Daniel Dunbare73b2672009-08-26 22:13:22 +00001460 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001461 if (!ParsingInlineAsm)
1462 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001463
Kevin Enderbye7739d42011-12-09 18:09:40 +00001464 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001465 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001466 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001467 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1468 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001469
Tim Northover1744d0a2013-10-25 12:49:50 +00001470 getTargetParser().onLabelParsed(Sym);
1471
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001472 // Consume any end of statement token, if present, to avoid spurious
1473 // AddBlankLine calls().
1474 if (Lexer.is(AsmToken::EndOfStatement)) {
1475 Lex();
1476 if (Lexer.is(AsmToken::Eof))
1477 return false;
1478 }
1479
Eli Friedman0f4871d2012-10-22 23:58:19 +00001480 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001481 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001482
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001483 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001484 if (!getTargetParser().equalIsAsmAssignment())
1485 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001486 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001487 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001488
Jim Grosbach4b905842013-09-20 23:08:21 +00001489 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001490
1491 default: // Normal instruction or directive.
1492 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001493 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001494
1495 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001496 if (areMacrosEnabled())
1497 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1498 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001499 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001500
Michael J. Spencer530ce852010-10-09 11:00:50 +00001501 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001502
Eli Bendersky17233942013-01-15 22:59:42 +00001503 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001504 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001505 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001506 //
Eli Bendersky17233942013-01-15 22:59:42 +00001507 // 1. The target-specific assembly parser. Some directives are target
1508 // specific or may potentially behave differently on certain targets.
1509 // 2. Asm parser extensions. For example, platform-specific parsers
1510 // (like the ELF parser) register themselves as extensions.
1511 // 3. The generic directive parser implemented by this class. These are
1512 // all the directives that behave in a target and platform independent
1513 // manner, or at least have a default behavior that's shared between
1514 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001515
Eli Bendersky17233942013-01-15 22:59:42 +00001516 // First query the target-specific parser. It will return 'true' if it
1517 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001518 if (!getTargetParser().ParseDirective(ID))
1519 return false;
1520
Alp Tokercb402912014-01-24 17:20:08 +00001521 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001522 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001523 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1524 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001525 if (Handler.first)
1526 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1527
1528 // Finally, if no one else is interested in this directive, it must be
1529 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001530 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001531 default:
1532 break;
1533 case DK_SET:
1534 case DK_EQU:
1535 return parseDirectiveSet(IDVal, true);
1536 case DK_EQUIV:
1537 return parseDirectiveSet(IDVal, false);
1538 case DK_ASCII:
1539 return parseDirectiveAscii(IDVal, false);
1540 case DK_ASCIZ:
1541 case DK_STRING:
1542 return parseDirectiveAscii(IDVal, true);
1543 case DK_BYTE:
1544 return parseDirectiveValue(1);
1545 case DK_SHORT:
1546 case DK_VALUE:
1547 case DK_2BYTE:
1548 return parseDirectiveValue(2);
1549 case DK_LONG:
1550 case DK_INT:
1551 case DK_4BYTE:
1552 return parseDirectiveValue(4);
1553 case DK_QUAD:
1554 case DK_8BYTE:
1555 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001556 case DK_OCTA:
1557 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001558 case DK_SINGLE:
1559 case DK_FLOAT:
1560 return parseDirectiveRealValue(APFloat::IEEEsingle);
1561 case DK_DOUBLE:
1562 return parseDirectiveRealValue(APFloat::IEEEdouble);
1563 case DK_ALIGN: {
1564 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1565 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1566 }
1567 case DK_ALIGN32: {
1568 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1569 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1570 }
1571 case DK_BALIGN:
1572 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1573 case DK_BALIGNW:
1574 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1575 case DK_BALIGNL:
1576 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1577 case DK_P2ALIGN:
1578 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1579 case DK_P2ALIGNW:
1580 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1581 case DK_P2ALIGNL:
1582 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1583 case DK_ORG:
1584 return parseDirectiveOrg();
1585 case DK_FILL:
1586 return parseDirectiveFill();
1587 case DK_ZERO:
1588 return parseDirectiveZero();
1589 case DK_EXTERN:
1590 eatToEndOfStatement(); // .extern is the default, ignore it.
1591 return false;
1592 case DK_GLOBL:
1593 case DK_GLOBAL:
1594 return parseDirectiveSymbolAttribute(MCSA_Global);
1595 case DK_LAZY_REFERENCE:
1596 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1597 case DK_NO_DEAD_STRIP:
1598 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1599 case DK_SYMBOL_RESOLVER:
1600 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Lang Hames1b640e02016-03-15 01:43:05 +00001601 case DK_ALT_ENTRY:
1602 return parseDirectiveSymbolAttribute(MCSA_AltEntry);
Jim Grosbach4b905842013-09-20 23:08:21 +00001603 case DK_PRIVATE_EXTERN:
1604 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1605 case DK_REFERENCE:
1606 return parseDirectiveSymbolAttribute(MCSA_Reference);
1607 case DK_WEAK_DEFINITION:
1608 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1609 case DK_WEAK_REFERENCE:
1610 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1611 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1612 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1613 case DK_COMM:
1614 case DK_COMMON:
1615 return parseDirectiveComm(/*IsLocal=*/false);
1616 case DK_LCOMM:
1617 return parseDirectiveComm(/*IsLocal=*/true);
1618 case DK_ABORT:
1619 return parseDirectiveAbort();
1620 case DK_INCLUDE:
1621 return parseDirectiveInclude();
1622 case DK_INCBIN:
1623 return parseDirectiveIncbin();
1624 case DK_CODE16:
1625 case DK_CODE16GCC:
1626 return TokError(Twine(IDVal) + " not supported yet");
1627 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001628 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001629 case DK_IRP:
1630 return parseDirectiveIrp(IDLoc);
1631 case DK_IRPC:
1632 return parseDirectiveIrpc(IDLoc);
1633 case DK_ENDR:
1634 return parseDirectiveEndr(IDLoc);
1635 case DK_BUNDLE_ALIGN_MODE:
1636 return parseDirectiveBundleAlignMode();
1637 case DK_BUNDLE_LOCK:
1638 return parseDirectiveBundleLock();
1639 case DK_BUNDLE_UNLOCK:
1640 return parseDirectiveBundleUnlock();
1641 case DK_SLEB128:
1642 return parseDirectiveLEB128(true);
1643 case DK_ULEB128:
1644 return parseDirectiveLEB128(false);
1645 case DK_SPACE:
1646 case DK_SKIP:
1647 return parseDirectiveSpace(IDVal);
1648 case DK_FILE:
1649 return parseDirectiveFile(IDLoc);
1650 case DK_LINE:
1651 return parseDirectiveLine();
1652 case DK_LOC:
1653 return parseDirectiveLoc();
1654 case DK_STABS:
1655 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001656 case DK_CV_FILE:
1657 return parseDirectiveCVFile();
1658 case DK_CV_LOC:
1659 return parseDirectiveCVLoc();
1660 case DK_CV_LINETABLE:
1661 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001662 case DK_CV_INLINE_LINETABLE:
1663 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001664 case DK_CV_DEF_RANGE:
1665 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001666 case DK_CV_STRINGTABLE:
1667 return parseDirectiveCVStringTable();
1668 case DK_CV_FILECHECKSUMS:
1669 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 case DK_CFI_SECTIONS:
1671 return parseDirectiveCFISections();
1672 case DK_CFI_STARTPROC:
1673 return parseDirectiveCFIStartProc();
1674 case DK_CFI_ENDPROC:
1675 return parseDirectiveCFIEndProc();
1676 case DK_CFI_DEF_CFA:
1677 return parseDirectiveCFIDefCfa(IDLoc);
1678 case DK_CFI_DEF_CFA_OFFSET:
1679 return parseDirectiveCFIDefCfaOffset();
1680 case DK_CFI_ADJUST_CFA_OFFSET:
1681 return parseDirectiveCFIAdjustCfaOffset();
1682 case DK_CFI_DEF_CFA_REGISTER:
1683 return parseDirectiveCFIDefCfaRegister(IDLoc);
1684 case DK_CFI_OFFSET:
1685 return parseDirectiveCFIOffset(IDLoc);
1686 case DK_CFI_REL_OFFSET:
1687 return parseDirectiveCFIRelOffset(IDLoc);
1688 case DK_CFI_PERSONALITY:
1689 return parseDirectiveCFIPersonalityOrLsda(true);
1690 case DK_CFI_LSDA:
1691 return parseDirectiveCFIPersonalityOrLsda(false);
1692 case DK_CFI_REMEMBER_STATE:
1693 return parseDirectiveCFIRememberState();
1694 case DK_CFI_RESTORE_STATE:
1695 return parseDirectiveCFIRestoreState();
1696 case DK_CFI_SAME_VALUE:
1697 return parseDirectiveCFISameValue(IDLoc);
1698 case DK_CFI_RESTORE:
1699 return parseDirectiveCFIRestore(IDLoc);
1700 case DK_CFI_ESCAPE:
1701 return parseDirectiveCFIEscape();
1702 case DK_CFI_SIGNAL_FRAME:
1703 return parseDirectiveCFISignalFrame();
1704 case DK_CFI_UNDEFINED:
1705 return parseDirectiveCFIUndefined(IDLoc);
1706 case DK_CFI_REGISTER:
1707 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001708 case DK_CFI_WINDOW_SAVE:
1709 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001710 case DK_MACROS_ON:
1711 case DK_MACROS_OFF:
1712 return parseDirectiveMacrosOnOff(IDVal);
1713 case DK_MACRO:
1714 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001715 case DK_EXITM:
1716 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001717 case DK_ENDM:
1718 case DK_ENDMACRO:
1719 return parseDirectiveEndMacro(IDVal);
1720 case DK_PURGEM:
1721 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001722 case DK_END:
1723 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001724 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001725 return parseDirectiveError(IDLoc, false);
1726 case DK_ERROR:
1727 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001728 case DK_WARNING:
1729 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001730 case DK_RELOC:
1731 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001732 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001733
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001734 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001735 }
Chris Lattner36e02122009-06-21 20:54:55 +00001736
Chad Rosierc7f552c2013-02-12 21:33:51 +00001737 // __asm _emit or __asm __emit
1738 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1739 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001740 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001741
1742 // __asm align
1743 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001744 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001745
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001746 if (ParsingInlineAsm && (IDVal == "even"))
1747 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001748 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001749
Chris Lattner7cbfa442010-05-19 23:34:33 +00001750 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001751 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001752 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001753 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001754 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001755 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001756
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001757 // Dump the parsed representation, if requested.
1758 if (getShowParsedOperands()) {
1759 SmallString<256> Str;
1760 raw_svector_ostream OS(Str);
1761 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001762 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001763 if (i != 0)
1764 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001765 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001766 }
1767 OS << "]";
1768
Jim Grosbach4b905842013-09-20 23:08:21 +00001769 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001770 }
1771
Oliver Stannard8b273082014-06-19 15:52:37 +00001772 // If we are generating dwarf for the current section then generate a .loc
1773 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001774 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001775 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001776 getStreamer().getCurrentSection().first)) {
1777 unsigned Line;
1778 if (ActiveMacros.empty())
1779 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1780 else
Frederic Riss16238d92015-06-25 21:57:33 +00001781 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1782 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001783
Eli Bendersky88024712013-01-16 19:32:36 +00001784 // If we previously parsed a cpp hash file line comment then make sure the
1785 // current Dwarf File is for the CppHashFilename if not then emit the
1786 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001787 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001788 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1789 0, StringRef(), CppHashFilename);
1790 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001791
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1793 // cache with the different Loc from the call above we save the last
1794 // info we queried here with SrcMgr.FindLineNumber().
1795 unsigned CppHashLocLineNo;
1796 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1797 CppHashLocLineNo = LastQueryLine;
1798 else {
1799 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1800 LastQueryLine = CppHashLocLineNo;
1801 LastQueryIDLoc = CppHashLoc;
1802 LastQueryBuffer = CppHashBuf;
1803 }
1804 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001805 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001806
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 getStreamer().EmitDwarfLocDirective(
1808 getContext().getGenDwarfFileNumber(), Line, 0,
1809 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1810 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001811 }
1812
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001813 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001814 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001815 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001816 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1817 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001818 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001819 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001820
Chris Lattnera2a9d162010-09-11 16:18:25 +00001821 // Don't skip the rest of the line, the instruction parser is responsible for
1822 // that.
1823 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001824}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001825
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001826// Parse and erase curly braces marking block start/end
1827bool
1828AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1829 // Identify curly brace marking block start/end
1830 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1831 return false;
1832
1833 SMLoc StartLoc = Lexer.getLoc();
1834 Lex(); // Eat the brace
1835 if (Lexer.is(AsmToken::EndOfStatement))
1836 Lex(); // Eat EndOfStatement following the brace
1837
1838 // Erase the block start/end brace from the output asm string
1839 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
1840 StartLoc.getPointer());
1841 return true;
1842}
1843
Jim Grosbach4b905842013-09-20 23:08:21 +00001844/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001845/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001846void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001847 if (!Lexer.is(AsmToken::EndOfStatement))
1848 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001849 // Eat EOL.
1850 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001851}
1852
Jim Grosbach4b905842013-09-20 23:08:21 +00001853/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001854/// ::= # number "filename"
1855/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001856bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001857 Lex(); // Eat the hash token.
1858
1859 if (getLexer().isNot(AsmToken::Integer)) {
1860 // Consume the line since in cases it is not a well-formed line directive,
1861 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001862 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001863 return false;
1864 }
1865
1866 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001867 Lex();
1868
1869 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001870 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001871 return false;
1872 }
1873
1874 StringRef Filename = getTok().getString();
1875 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001876 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001877
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001878 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1879 CppHashLoc = L;
1880 CppHashFilename = Filename;
1881 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001882 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001883
1884 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001885 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001886 return false;
1887}
1888
Jim Grosbach4b905842013-09-20 23:08:21 +00001889/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001890/// for the Filename and LineNo if any in the diagnostic.
1891void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001892 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001893 raw_ostream &OS = errs();
1894
1895 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001896 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001897 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1898 unsigned CppHashBuf =
1899 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001900
Jim Grosbach4b905842013-09-20 23:08:21 +00001901 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001902 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001903 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1904 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1905 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001906 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1907 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001908 }
1909
Eric Christophera7c32732012-12-18 00:30:54 +00001910 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001911 // manager changed or buffer changed (like in a nested include) then just
1912 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001913 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001914 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001915 if (Parser->SavedDiagHandler)
1916 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1917 else
Craig Topper353eda42014-04-24 06:44:33 +00001918 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001919 return;
1920 }
1921
Eric Christophera7c32732012-12-18 00:30:54 +00001922 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001923 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1924 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001925 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001926
1927 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1928 int CppHashLocLineNo =
1929 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001930 int LineNo =
1931 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001932
Jim Grosbach4b905842013-09-20 23:08:21 +00001933 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1934 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001935 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001936
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001937 if (Parser->SavedDiagHandler)
1938 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1939 else
Craig Topper353eda42014-04-24 06:44:33 +00001940 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001941}
1942
Rafael Espindola2c064482012-08-21 18:29:30 +00001943// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1944// difference being that that function accepts '@' as part of identifiers and
1945// we can't do that. AsmLexer.cpp should probably be changed to handle
1946// '@' as a special case when needed.
1947static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001948 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1949 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001950}
1951
Rafael Espindola34b9c512012-06-03 23:57:14 +00001952bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001953 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001954 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001955 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001956 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001957 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001958 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001959 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001960
Preston Gurd05500642012-09-19 20:36:12 +00001961 // A macro without parameters is handled differently on Darwin:
1962 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001963 while (!Body.empty()) {
1964 // Scan for the next substitution.
1965 std::size_t End = Body.size(), Pos = 0;
1966 for (; Pos != End; ++Pos) {
1967 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001968 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001969 // This macro has no parameters, look for $0, $1, etc.
1970 if (Body[Pos] != '$' || Pos + 1 == End)
1971 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001972
Rafael Espindola1134ab232011-06-05 02:43:45 +00001973 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001974 if (Next == '$' || Next == 'n' ||
1975 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001976 break;
1977 } else {
1978 // This macro has parameters, look for \foo, \bar, etc.
1979 if (Body[Pos] == '\\' && Pos + 1 != End)
1980 break;
1981 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001982 }
1983
1984 // Add the prefix.
1985 OS << Body.slice(0, Pos);
1986
1987 // Check if we reached the end.
1988 if (Pos == End)
1989 break;
1990
Benjamin Kramer513e7442014-02-20 13:36:32 +00001991 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001992 switch (Body[Pos + 1]) {
1993 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001994 case '$':
1995 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001996 break;
1997
Jim Grosbach4b905842013-09-20 23:08:21 +00001998 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001999 case 'n':
2000 OS << A.size();
2001 break;
2002
Jim Grosbach4b905842013-09-20 23:08:21 +00002003 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002004 default: {
2005 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002006 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002007 if (Index >= A.size())
2008 break;
2009
2010 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002011 for (const AsmToken &Token : A[Index])
2012 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002013 break;
2014 }
2015 }
2016 Pos += 2;
2017 } else {
2018 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002019
2020 // Check for the \@ pseudo-variable.
2021 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002022 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002023 else
2024 while (isIdentifierChar(Body[I]) && I + 1 != End)
2025 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002026
Jim Grosbach4b905842013-09-20 23:08:21 +00002027 const char *Begin = Body.data() + Pos + 1;
2028 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002029 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002030
Toma Tabacu217116e2015-04-27 10:50:29 +00002031 if (Argument == "@") {
2032 OS << NumOfMacroInstantiations;
2033 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002034 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002035 for (; Index < NParameters; ++Index)
2036 if (Parameters[Index].Name == Argument)
2037 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002038
Toma Tabacu217116e2015-04-27 10:50:29 +00002039 if (Index == NParameters) {
2040 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2041 Pos += 3;
2042 else {
2043 OS << '\\' << Argument;
2044 Pos = I;
2045 }
2046 } else {
2047 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002048 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002049 // We expect no quotes around the string's contents when
2050 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002051 if (Token.getKind() != AsmToken::String || VarargParameter)
2052 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002053 else
Craig Topper84008482015-10-10 05:38:14 +00002054 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002055
2056 Pos += 1 + Argument.size();
2057 }
Preston Gurd05500642012-09-19 20:36:12 +00002058 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002059 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002060 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002061 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002062 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002063
Rafael Espindola1134ab232011-06-05 02:43:45 +00002064 return false;
2065}
Daniel Dunbar43235712010-07-18 18:54:11 +00002066
Nico Weber2a8f9222014-07-24 16:29:04 +00002067MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002068 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002069 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002070 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002071
Jim Grosbach4b905842013-09-20 23:08:21 +00002072static bool isOperator(AsmToken::TokenKind kind) {
2073 switch (kind) {
2074 default:
2075 return false;
2076 case AsmToken::Plus:
2077 case AsmToken::Minus:
2078 case AsmToken::Tilde:
2079 case AsmToken::Slash:
2080 case AsmToken::Star:
2081 case AsmToken::Dot:
2082 case AsmToken::Equal:
2083 case AsmToken::EqualEqual:
2084 case AsmToken::Pipe:
2085 case AsmToken::PipePipe:
2086 case AsmToken::Caret:
2087 case AsmToken::Amp:
2088 case AsmToken::AmpAmp:
2089 case AsmToken::Exclaim:
2090 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002091 case AsmToken::Less:
2092 case AsmToken::LessEqual:
2093 case AsmToken::LessLess:
2094 case AsmToken::LessGreater:
2095 case AsmToken::Greater:
2096 case AsmToken::GreaterEqual:
2097 case AsmToken::GreaterGreater:
2098 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002099 }
2100}
2101
David Majnemer16252452014-01-29 00:07:39 +00002102namespace {
2103class AsmLexerSkipSpaceRAII {
2104public:
2105 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2106 Lexer.setSkipSpace(SkipSpace);
2107 }
2108
2109 ~AsmLexerSkipSpaceRAII() {
2110 Lexer.setSkipSpace(true);
2111 }
2112
2113private:
2114 AsmLexer &Lexer;
2115};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002116}
David Majnemer16252452014-01-29 00:07:39 +00002117
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002118bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2119
2120 if (Vararg) {
2121 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2122 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002123 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002124 }
2125 return false;
2126 }
2127
Rafael Espindola768b41c2012-06-15 14:02:34 +00002128 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002129
David Majnemer16252452014-01-29 00:07:39 +00002130 // Darwin doesn't use spaces to delmit arguments.
2131 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002132
Scott Egertona1fa68a2016-02-11 13:48:49 +00002133 bool SpaceEaten;
2134
Rafael Espindola768b41c2012-06-15 14:02:34 +00002135 for (;;) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002136 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002137 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002138 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002139
Scott Egertona1fa68a2016-02-11 13:48:49 +00002140 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002141
Scott Egertona1fa68a2016-02-11 13:48:49 +00002142 if (Lexer.is(AsmToken::Comma))
2143 break;
2144
2145 if (Lexer.is(AsmToken::Space)) {
2146 SpaceEaten = true;
2147 Lex(); // Eat spaces
2148 }
Preston Gurd05500642012-09-19 20:36:12 +00002149
2150 // Spaces can delimit parameters, but could also be part an expression.
2151 // If the token after a space is an operator, add the token and the next
2152 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002153 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002154 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002155 MA.push_back(getTok());
2156 Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002157
Scott Egertona1fa68a2016-02-11 13:48:49 +00002158 // Whitespace after an operator can be ignored.
2159 if (Lexer.is(AsmToken::Space))
2160 Lex();
2161
2162 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002163 }
2164 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002165 if (SpaceEaten)
2166 break;
Preston Gurd05500642012-09-19 20:36:12 +00002167 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002168
Jim Grosbach4b905842013-09-20 23:08:21 +00002169 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002170 // to be able to fill in the remaining default parameter values
2171 if (Lexer.is(AsmToken::EndOfStatement))
2172 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002173
2174 // Adjust the current parentheses level.
2175 if (Lexer.is(AsmToken::LParen))
2176 ++ParenLevel;
2177 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2178 --ParenLevel;
2179
2180 // Append the token to the current argument list.
2181 MA.push_back(getTok());
2182 Lex();
2183 }
Preston Gurd05500642012-09-19 20:36:12 +00002184
Rafael Espindola768b41c2012-06-15 14:02:34 +00002185 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002186 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002187 return false;
2188}
2189
2190// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002191bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002192 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002193 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002194 bool NamedParametersFound = false;
2195 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002196
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002197 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002198 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002199
Rafael Espindola768b41c2012-06-15 14:02:34 +00002200 // Parse two kinds of macro invocations:
2201 // - macros defined without any parameters accept an arbitrary number of them
2202 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002203 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002204 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2205 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002206 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002207 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002208
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002209 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002210 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002211 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002212 eatToEndOfStatement();
2213 return true;
2214 }
2215
2216 if (!Lexer.is(AsmToken::Equal)) {
2217 TokError("expected '=' after formal parameter identifier");
2218 eatToEndOfStatement();
2219 return true;
2220 }
2221 Lex();
2222
2223 NamedParametersFound = true;
2224 }
2225
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002226 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002227 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002228 eatToEndOfStatement();
2229 return true;
2230 }
2231
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002232 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2233 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002234 return true;
2235
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002236 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002237 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002238 unsigned FAI = 0;
2239 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002240 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002241 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002242
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002243 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002244 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002245 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002246 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002247 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002248 return true;
2249 }
2250 PI = FAI;
2251 }
2252
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002253 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002254 if (A.size() <= PI)
2255 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002256 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002257
2258 if (FALocs.size() <= PI)
2259 FALocs.resize(PI + 1);
2260
2261 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002262 }
Jim Grosbach206661622012-07-30 22:44:17 +00002263
Preston Gurd242ed3152012-09-19 20:29:04 +00002264 // At the end of the statement, fill in remaining arguments that have
2265 // default values. If there aren't any, then the next argument is
2266 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002267 if (Lexer.is(AsmToken::EndOfStatement)) {
2268 bool Failure = false;
2269 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2270 if (A[FAI].empty()) {
2271 if (M->Parameters[FAI].Required) {
2272 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2273 "missing value for required parameter "
2274 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2275 Failure = true;
2276 }
2277
2278 if (!M->Parameters[FAI].Value.empty())
2279 A[FAI] = M->Parameters[FAI].Value;
2280 }
2281 }
2282 return Failure;
2283 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002284
2285 if (Lexer.is(AsmToken::Comma))
2286 Lex();
2287 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002288
2289 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002290}
2291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002293 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2294 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002295}
2296
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002297void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2298 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002299}
2300
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002301void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002302
Jim Grosbach4b905842013-09-20 23:08:21 +00002303bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002304 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2305 // this, although we should protect against infinite loops.
2306 if (ActiveMacros.size() == 20)
2307 return TokError("macros cannot be nested more than 20 levels deep");
2308
Eli Bendersky38274122013-01-14 23:22:36 +00002309 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002311 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002312
Rafael Espindola1134ab232011-06-05 02:43:45 +00002313 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2314 // to hold the macro body with substitutions.
2315 SmallString<256> Buf;
2316 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002317 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002318
Toma Tabacu217116e2015-04-27 10:50:29 +00002319 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002320 return true;
2321
Eli Bendersky38274122013-01-14 23:22:36 +00002322 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002323 // instantiation.
2324 OS << ".endmacro\n";
2325
Rafael Espindola3560ff22014-08-27 20:03:13 +00002326 std::unique_ptr<MemoryBuffer> Instantiation =
2327 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002328
Daniel Dunbar43235712010-07-18 18:54:11 +00002329 // Create the macro instantiation object and add to the current macro
2330 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002331 MacroInstantiation *MI = new MacroInstantiation(
2332 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002333 ActiveMacros.push_back(MI);
2334
Toma Tabacu217116e2015-04-27 10:50:29 +00002335 ++NumOfMacroInstantiations;
2336
Daniel Dunbar43235712010-07-18 18:54:11 +00002337 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002338 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002339 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002340 Lex();
2341
2342 return false;
2343}
2344
Jim Grosbach4b905842013-09-20 23:08:21 +00002345void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002346 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002347 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002348 Lex();
2349
2350 // Pop the instantiation entry.
2351 delete ActiveMacros.back();
2352 ActiveMacros.pop_back();
2353}
2354
Jim Grosbach4b905842013-09-20 23:08:21 +00002355bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002356 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002357 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002358 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002359 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2360 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002361 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002362
Pete Cooper80d21cb2015-06-22 19:35:57 +00002363 if (!Sym) {
2364 // In the case where we parse an expression starting with a '.', we will
2365 // not generate an error, nor will we create a symbol. In this case we
2366 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002367 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002368 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002369
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002370 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002371 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002372 if (NoDeadStrip)
2373 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2374
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002375 return false;
2376}
2377
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002378/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002379/// ::= identifier
2380/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002382 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002383 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2384 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002385 // handle this as a context dependent token, instead we detect adjacent tokens
2386 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002387 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2388 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002389
Hans Wennborgce69d772013-10-18 20:46:28 +00002390 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002391 Lex();
2392 if (Lexer.isNot(AsmToken::Identifier))
2393 return true;
2394
Hans Wennborgce69d772013-10-18 20:46:28 +00002395 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2396 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002397 return true;
2398
2399 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002400 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002401 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002402 Lex();
2403 return false;
2404 }
2405
Jim Grosbach4b905842013-09-20 23:08:21 +00002406 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002407 return true;
2408
Sean Callanan936b0d32010-01-19 21:44:56 +00002409 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002410
Sean Callanan686ed8d2010-01-19 20:22:31 +00002411 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002412
2413 return false;
2414}
2415
Jim Grosbach4b905842013-09-20 23:08:21 +00002416/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002417/// ::= .equ identifier ',' expression
2418/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002419/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002420bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002421 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002422
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002423 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002424 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002425
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002426 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002427 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002428 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002429
Jim Grosbach4b905842013-09-20 23:08:21 +00002430 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002431}
2432
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002433bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002434 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002435
2436 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002437 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002438 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2439 if (Str[i] != '\\') {
2440 Data += Str[i];
2441 continue;
2442 }
2443
2444 // Recognize escaped characters. Note that this escape semantics currently
2445 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2446 ++i;
2447 if (i == e)
2448 return TokError("unexpected backslash at end of string");
2449
2450 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002451 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002452 // Consume up to three octal characters.
2453 unsigned Value = Str[i] - '0';
2454
Jim Grosbach4b905842013-09-20 23:08:21 +00002455 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002456 ++i;
2457 Value = Value * 8 + (Str[i] - '0');
2458
Jim Grosbach4b905842013-09-20 23:08:21 +00002459 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002460 ++i;
2461 Value = Value * 8 + (Str[i] - '0');
2462 }
2463 }
2464
2465 if (Value > 255)
2466 return TokError("invalid octal escape sequence (out of range)");
2467
Jim Grosbach4b905842013-09-20 23:08:21 +00002468 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002469 continue;
2470 }
2471
2472 // Otherwise recognize individual escapes.
2473 switch (Str[i]) {
2474 default:
2475 // Just reject invalid escape sequences for now.
2476 return TokError("invalid escape sequence (unrecognized character)");
2477
2478 case 'b': Data += '\b'; break;
2479 case 'f': Data += '\f'; break;
2480 case 'n': Data += '\n'; break;
2481 case 'r': Data += '\r'; break;
2482 case 't': Data += '\t'; break;
2483 case '"': Data += '"'; break;
2484 case '\\': Data += '\\'; break;
2485 }
2486 }
2487
2488 return false;
2489}
2490
Jim Grosbach4b905842013-09-20 23:08:21 +00002491/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002492/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002493bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002494 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002495 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002496
Daniel Dunbara10e5192009-06-24 23:30:00 +00002497 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002498 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002499 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002500
Daniel Dunbaref668c12009-08-14 18:19:52 +00002501 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002502 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002503 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002504
Rafael Espindola64e1af82013-07-02 15:49:13 +00002505 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002506 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002507 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002508
Sean Callanan686ed8d2010-01-19 20:22:31 +00002509 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002510
2511 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002512 break;
2513
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002514 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002515 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002516 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002517 }
2518 }
2519
Sean Callanan686ed8d2010-01-19 20:22:31 +00002520 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002521 return false;
2522}
2523
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002524/// parseDirectiveReloc
2525/// ::= .reloc expression , identifier [ , expression ]
2526bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2527 const MCExpr *Offset;
2528 const MCExpr *Expr = nullptr;
2529
2530 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2531 if (parseExpression(Offset))
2532 return true;
2533
2534 // We can only deal with constant expressions at the moment.
2535 int64_t OffsetValue;
2536 if (!Offset->evaluateAsAbsolute(OffsetValue))
2537 return Error(OffsetLoc, "expression is not a constant value");
2538
David Majnemerce108422016-01-19 23:05:27 +00002539 if (OffsetValue < 0)
2540 return Error(OffsetLoc, "expression is negative");
2541
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002542 if (Lexer.isNot(AsmToken::Comma))
2543 return TokError("expected comma");
2544 Lexer.Lex();
2545
2546 if (Lexer.isNot(AsmToken::Identifier))
2547 return TokError("expected relocation name");
2548 SMLoc NameLoc = Lexer.getTok().getLoc();
2549 StringRef Name = Lexer.getTok().getIdentifier();
2550 Lexer.Lex();
2551
2552 if (Lexer.is(AsmToken::Comma)) {
2553 Lexer.Lex();
2554 SMLoc ExprLoc = Lexer.getLoc();
2555 if (parseExpression(Expr))
2556 return true;
2557
2558 MCValue Value;
2559 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2560 return Error(ExprLoc, "expression must be relocatable");
2561 }
2562
2563 if (Lexer.isNot(AsmToken::EndOfStatement))
2564 return TokError("unexpected token in .reloc directive");
2565
2566 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2567 return Error(NameLoc, "unknown relocation name");
2568
2569 return false;
2570}
2571
Jim Grosbach4b905842013-09-20 23:08:21 +00002572/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002573/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002574bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002575 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002576 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002577
Daniel Dunbara10e5192009-06-24 23:30:00 +00002578 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002579 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002580 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002581 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002582 return true;
2583
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002584 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002585 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2586 assert(Size <= 8 && "Invalid size");
2587 uint64_t IntValue = MCE->getValue();
2588 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2589 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002590 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002591 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002592 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002593
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002594 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002595 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002596
Daniel Dunbara10e5192009-06-24 23:30:00 +00002597 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002598 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002599 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002600 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002601 }
2602 }
2603
Sean Callanan686ed8d2010-01-19 20:22:31 +00002604 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002605 return false;
2606}
2607
David Woodhoused6de0d92014-02-01 16:20:59 +00002608/// ParseDirectiveOctaValue
2609/// ::= .octa [ hexconstant (, hexconstant)* ]
2610bool AsmParser::parseDirectiveOctaValue() {
2611 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2612 checkForValidSection();
2613
2614 for (;;) {
2615 if (Lexer.getKind() == AsmToken::Error)
2616 return true;
2617 if (Lexer.getKind() != AsmToken::Integer &&
2618 Lexer.getKind() != AsmToken::BigNum)
2619 return TokError("unknown token in expression");
2620
2621 SMLoc ExprLoc = getLexer().getLoc();
2622 APInt IntValue = getTok().getAPIntVal();
2623 Lex();
2624
2625 uint64_t hi, lo;
2626 if (IntValue.isIntN(64)) {
2627 hi = 0;
2628 lo = IntValue.getZExtValue();
2629 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002630 // It might actually have more than 128 bits, but the top ones are zero.
2631 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002632 lo = IntValue.getLoBits(64).getZExtValue();
2633 } else
2634 return Error(ExprLoc, "literal value out of range for directive");
2635
2636 if (MAI.isLittleEndian()) {
2637 getStreamer().EmitIntValue(lo, 8);
2638 getStreamer().EmitIntValue(hi, 8);
2639 } else {
2640 getStreamer().EmitIntValue(hi, 8);
2641 getStreamer().EmitIntValue(lo, 8);
2642 }
2643
2644 if (getLexer().is(AsmToken::EndOfStatement))
2645 break;
2646
2647 // FIXME: Improve diagnostic.
2648 if (getLexer().isNot(AsmToken::Comma))
2649 return TokError("unexpected token in directive");
2650 Lex();
2651 }
2652 }
2653
2654 Lex();
2655 return false;
2656}
2657
Jim Grosbach4b905842013-09-20 23:08:21 +00002658/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002659/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002660bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002661 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002662 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002663
2664 for (;;) {
2665 // We don't truly support arithmetic on floating point expressions, so we
2666 // have to manually parse unary prefixes.
2667 bool IsNeg = false;
2668 if (getLexer().is(AsmToken::Minus)) {
2669 Lex();
2670 IsNeg = true;
2671 } else if (getLexer().is(AsmToken::Plus))
2672 Lex();
2673
Michael J. Spencer530ce852010-10-09 11:00:50 +00002674 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002675 getLexer().isNot(AsmToken::Real) &&
2676 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002677 return TokError("unexpected token in directive");
2678
2679 // Convert to an APFloat.
2680 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002681 StringRef IDVal = getTok().getString();
2682 if (getLexer().is(AsmToken::Identifier)) {
2683 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2684 Value = APFloat::getInf(Semantics);
2685 else if (!IDVal.compare_lower("nan"))
2686 Value = APFloat::getNaN(Semantics, false, ~0);
2687 else
2688 return TokError("invalid floating point literal");
2689 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002690 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002691 return TokError("invalid floating point literal");
2692 if (IsNeg)
2693 Value.changeSign();
2694
2695 // Consume the numeric token.
2696 Lex();
2697
2698 // Emit the value as an integer.
2699 APInt AsInt = Value.bitcastToAPInt();
2700 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002701 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002702
2703 if (getLexer().is(AsmToken::EndOfStatement))
2704 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002705
Daniel Dunbar2af16532010-09-24 01:59:56 +00002706 if (getLexer().isNot(AsmToken::Comma))
2707 return TokError("unexpected token in directive");
2708 Lex();
2709 }
2710 }
2711
2712 Lex();
2713 return false;
2714}
2715
Jim Grosbach4b905842013-09-20 23:08:21 +00002716/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002717/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002718bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002719 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002720
2721 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002722 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002723 return true;
2724
Rafael Espindolab91bac62010-10-05 19:42:57 +00002725 int64_t Val = 0;
2726 if (getLexer().is(AsmToken::Comma)) {
2727 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002728 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002729 return true;
2730 }
2731
Rafael Espindola922e3f42010-09-16 15:03:59 +00002732 if (getLexer().isNot(AsmToken::EndOfStatement))
2733 return TokError("unexpected token in '.zero' directive");
2734
2735 Lex();
2736
Rafael Espindola64e1af82013-07-02 15:49:13 +00002737 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002738
2739 return false;
2740}
2741
Jim Grosbach4b905842013-09-20 23:08:21 +00002742/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002743/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002744bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002745 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002746
David Majnemer522d3db2014-02-01 07:19:38 +00002747 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002748 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002749 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002750 return true;
2751
David Majnemer522d3db2014-02-01 07:19:38 +00002752 if (NumValues < 0) {
2753 Warning(RepeatLoc,
2754 "'.fill' directive with negative repeat count has no effect");
2755 NumValues = 0;
2756 }
2757
Roman Divackye33098f2013-09-24 17:44:41 +00002758 int64_t FillSize = 1;
2759 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002760
David Majnemer522d3db2014-02-01 07:19:38 +00002761 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002762 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2763 if (getLexer().isNot(AsmToken::Comma))
2764 return TokError("unexpected token in '.fill' directive");
2765 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002766
David Majnemer522d3db2014-02-01 07:19:38 +00002767 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002768 if (parseAbsoluteExpression(FillSize))
2769 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002770
Roman Divackye33098f2013-09-24 17:44:41 +00002771 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2772 if (getLexer().isNot(AsmToken::Comma))
2773 return TokError("unexpected token in '.fill' directive");
2774 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002775
David Majnemer522d3db2014-02-01 07:19:38 +00002776 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002777 if (parseAbsoluteExpression(FillExpr))
2778 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002779
Roman Divackye33098f2013-09-24 17:44:41 +00002780 if (getLexer().isNot(AsmToken::EndOfStatement))
2781 return TokError("unexpected token in '.fill' directive");
2782
2783 Lex();
2784 }
2785 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002786
David Majnemer522d3db2014-02-01 07:19:38 +00002787 if (FillSize < 0) {
2788 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2789 NumValues = 0;
2790 }
2791 if (FillSize > 8) {
2792 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2793 FillSize = 8;
2794 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002795
David Majnemer522d3db2014-02-01 07:19:38 +00002796 if (!isUInt<32>(FillExpr) && FillSize > 4)
2797 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2798
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002799 if (NumValues > 0) {
2800 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2801 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2802 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2803 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2804 if (NonZeroFillSize < FillSize)
2805 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2806 }
David Majnemer522d3db2014-02-01 07:19:38 +00002807 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002808
2809 return false;
2810}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002811
Jim Grosbach4b905842013-09-20 23:08:21 +00002812/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002813/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002814bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002815 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002816
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002817 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002818 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002819 return true;
2820
2821 // Parse optional fill expression.
2822 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002823 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2824 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002825 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002826 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002827
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002828 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002829 return true;
2830
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002831 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002832 return TokError("unexpected token in '.org' directive");
2833 }
2834
Sean Callanan686ed8d2010-01-19 20:22:31 +00002835 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002836 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002837 return false;
2838}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002839
Jim Grosbach4b905842013-09-20 23:08:21 +00002840/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002841/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002842bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002843 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002844
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002845 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002846 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002847 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002848 return true;
2849
2850 SMLoc MaxBytesLoc;
2851 bool HasFillExpr = false;
2852 int64_t FillExpr = 0;
2853 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002854 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2855 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002856 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002857 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002858
2859 // The fill expression can be omitted while specifying a maximum number of
2860 // alignment bytes, e.g:
2861 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002862 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002863 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002864 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002865 return true;
2866 }
2867
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002868 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2869 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002870 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002871 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002872
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002873 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002874 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002875 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002876
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002877 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002878 return TokError("unexpected token in directive");
2879 }
2880 }
2881
Sean Callanan686ed8d2010-01-19 20:22:31 +00002882 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002883
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002884 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002885 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002886
2887 // Compute alignment in bytes.
2888 if (IsPow2) {
2889 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002890 if (Alignment >= 32) {
2891 Error(AlignmentLoc, "invalid alignment value");
2892 Alignment = 31;
2893 }
2894
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002895 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002896 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002897 // Reject alignments that aren't either a power of two or zero,
2898 // for gas compatibility. Alignment of zero is silently rounded
2899 // up to one.
2900 if (Alignment == 0)
2901 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002902 if (!isPowerOf2_64(Alignment))
2903 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002904 }
2905
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002906 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002907 if (MaxBytesLoc.isValid()) {
2908 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002909 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002910 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002911 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002912 }
2913
2914 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002915 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002916 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002917 MaxBytesToFill = 0;
2918 }
2919 }
2920
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002921 // Check whether we should use optimal code alignment for this .align
2922 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002923 const MCSection *Section = getStreamer().getCurrentSection().first;
2924 assert(Section && "must have section to emit alignment");
2925 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002926 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2927 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002928 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002929 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002930 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002931 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2932 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002933 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002934
2935 return false;
2936}
2937
Jim Grosbach4b905842013-09-20 23:08:21 +00002938/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002939/// ::= .file [number] filename
2940/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002941bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002942 // FIXME: I'm not sure what this is.
2943 int64_t FileNumber = -1;
2944 SMLoc FileNumberLoc = getLexer().getLoc();
2945 if (getLexer().is(AsmToken::Integer)) {
2946 FileNumber = getTok().getIntVal();
2947 Lex();
2948
2949 if (FileNumber < 1)
2950 return TokError("file number less than one");
2951 }
2952
2953 if (getLexer().isNot(AsmToken::String))
2954 return TokError("unexpected token in '.file' directive");
2955
2956 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002957 // Allow the strings to have escaped octal character sequence.
2958 std::string Path = getTok().getString();
2959 if (parseEscapedString(Path))
2960 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002961 Lex();
2962
2963 StringRef Directory;
2964 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002965 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002966 if (getLexer().is(AsmToken::String)) {
2967 if (FileNumber == -1)
2968 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002969 if (parseEscapedString(FilenameData))
2970 return true;
2971 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002972 Directory = Path;
2973 Lex();
2974 } else {
2975 Filename = Path;
2976 }
2977
2978 if (getLexer().isNot(AsmToken::EndOfStatement))
2979 return TokError("unexpected token in '.file' directive");
2980
2981 if (FileNumber == -1)
2982 getStreamer().EmitFileDirective(Filename);
2983 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002984 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002985 Error(DirectiveLoc,
2986 "input can't have .file dwarf directives when -g is "
2987 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002988
David Blaikiec714ef42014-03-17 01:52:11 +00002989 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2990 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002991 Error(FileNumberLoc, "file number already allocated");
2992 }
2993
2994 return false;
2995}
2996
Jim Grosbach4b905842013-09-20 23:08:21 +00002997/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002998/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002999bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00003000 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3001 if (getLexer().isNot(AsmToken::Integer))
3002 return TokError("unexpected token in '.line' directive");
3003
3004 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00003005 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003006 Lex();
3007
3008 // FIXME: Do something with the .line.
3009 }
3010
3011 if (getLexer().isNot(AsmToken::EndOfStatement))
3012 return TokError("unexpected token in '.line' directive");
3013
3014 return false;
3015}
3016
Jim Grosbach4b905842013-09-20 23:08:21 +00003017/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003018/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3019/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3020/// The first number is a file number, must have been previously assigned with
3021/// a .file directive, the second number is the line number and optionally the
3022/// third number is a column position (zero if not specified). The remaining
3023/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003024bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003025 if (getLexer().isNot(AsmToken::Integer))
3026 return TokError("unexpected token in '.loc' directive");
3027 int64_t FileNumber = getTok().getIntVal();
3028 if (FileNumber < 1)
3029 return TokError("file number less than one in '.loc' directive");
3030 if (!getContext().isValidDwarfFileNumber(FileNumber))
3031 return TokError("unassigned file number in '.loc' directive");
3032 Lex();
3033
3034 int64_t LineNumber = 0;
3035 if (getLexer().is(AsmToken::Integer)) {
3036 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003037 if (LineNumber < 0)
3038 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003039 Lex();
3040 }
3041
3042 int64_t ColumnPos = 0;
3043 if (getLexer().is(AsmToken::Integer)) {
3044 ColumnPos = getTok().getIntVal();
3045 if (ColumnPos < 0)
3046 return TokError("column position less than zero in '.loc' directive");
3047 Lex();
3048 }
3049
3050 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3051 unsigned Isa = 0;
3052 int64_t Discriminator = 0;
3053 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3054 for (;;) {
3055 if (getLexer().is(AsmToken::EndOfStatement))
3056 break;
3057
3058 StringRef Name;
3059 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003060 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003061 return TokError("unexpected token in '.loc' directive");
3062
3063 if (Name == "basic_block")
3064 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3065 else if (Name == "prologue_end")
3066 Flags |= DWARF2_FLAG_PROLOGUE_END;
3067 else if (Name == "epilogue_begin")
3068 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3069 else if (Name == "is_stmt") {
3070 Loc = getTok().getLoc();
3071 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return true;
3074 // The expression must be the constant 0 or 1.
3075 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3076 int Value = MCE->getValue();
3077 if (Value == 0)
3078 Flags &= ~DWARF2_FLAG_IS_STMT;
3079 else if (Value == 1)
3080 Flags |= DWARF2_FLAG_IS_STMT;
3081 else
3082 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003083 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003084 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3085 }
Craig Topperf15655b2013-04-22 04:22:40 +00003086 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003087 Loc = getTok().getLoc();
3088 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003089 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003090 return true;
3091 // The expression must be a constant greater or equal to 0.
3092 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3093 int Value = MCE->getValue();
3094 if (Value < 0)
3095 return Error(Loc, "isa number less than zero");
3096 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003097 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return Error(Loc, "isa number not a constant value");
3099 }
Craig Topperf15655b2013-04-22 04:22:40 +00003100 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003101 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003103 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003104 return Error(Loc, "unknown sub-directive in '.loc' directive");
3105 }
3106
3107 if (getLexer().is(AsmToken::EndOfStatement))
3108 break;
3109 }
3110 }
3111
3112 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3113 Isa, Discriminator, StringRef());
3114
3115 return false;
3116}
3117
Jim Grosbach4b905842013-09-20 23:08:21 +00003118/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003119/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003120bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003121 return TokError("unsupported directive '.stabs'");
3122}
3123
Reid Kleckner2214ed82016-01-29 00:49:42 +00003124/// parseDirectiveCVFile
3125/// ::= .cv_file number filename
3126bool AsmParser::parseDirectiveCVFile() {
3127 SMLoc FileNumberLoc = getLexer().getLoc();
3128 if (getLexer().isNot(AsmToken::Integer))
3129 return TokError("expected file number in '.cv_file' directive");
3130
3131 int64_t FileNumber = getTok().getIntVal();
3132 Lex();
3133
3134 if (FileNumber < 1)
3135 return TokError("file number less than one");
3136
3137 if (getLexer().isNot(AsmToken::String))
3138 return TokError("unexpected token in '.cv_file' directive");
3139
3140 // Usually the directory and filename together, otherwise just the directory.
3141 // Allow the strings to have escaped octal character sequence.
3142 std::string Filename;
3143 if (parseEscapedString(Filename))
3144 return true;
3145 Lex();
3146
3147 if (getLexer().isNot(AsmToken::EndOfStatement))
3148 return TokError("unexpected token in '.cv_file' directive");
3149
3150 if (getStreamer().EmitCVFileDirective(FileNumber, Filename) == 0)
3151 Error(FileNumberLoc, "file number already allocated");
3152
3153 return false;
3154}
3155
3156/// parseDirectiveCVLoc
3157/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3158/// [is_stmt VALUE]
3159/// The first number is a file number, must have been previously assigned with
3160/// a .file directive, the second number is the line number and optionally the
3161/// third number is a column position (zero if not specified). The remaining
3162/// optional items are .loc sub-directives.
3163bool AsmParser::parseDirectiveCVLoc() {
3164 if (getLexer().isNot(AsmToken::Integer))
3165 return TokError("unexpected token in '.cv_loc' directive");
3166
3167 int64_t FunctionId = getTok().getIntVal();
3168 if (FunctionId < 0)
3169 return TokError("function id less than zero in '.cv_loc' directive");
3170 Lex();
3171
3172 int64_t FileNumber = getTok().getIntVal();
3173 if (FileNumber < 1)
3174 return TokError("file number less than one in '.cv_loc' directive");
3175 if (!getContext().isValidCVFileNumber(FileNumber))
3176 return TokError("unassigned file number in '.cv_loc' directive");
3177 Lex();
3178
3179 int64_t LineNumber = 0;
3180 if (getLexer().is(AsmToken::Integer)) {
3181 LineNumber = getTok().getIntVal();
3182 if (LineNumber < 0)
3183 return TokError("line number less than zero in '.cv_loc' directive");
3184 Lex();
3185 }
3186
3187 int64_t ColumnPos = 0;
3188 if (getLexer().is(AsmToken::Integer)) {
3189 ColumnPos = getTok().getIntVal();
3190 if (ColumnPos < 0)
3191 return TokError("column position less than zero in '.cv_loc' directive");
3192 Lex();
3193 }
3194
3195 bool PrologueEnd = false;
3196 uint64_t IsStmt = 0;
3197 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3198 StringRef Name;
3199 SMLoc Loc = getTok().getLoc();
3200 if (parseIdentifier(Name))
3201 return TokError("unexpected token in '.cv_loc' directive");
3202
3203 if (Name == "prologue_end")
3204 PrologueEnd = true;
3205 else if (Name == "is_stmt") {
3206 Loc = getTok().getLoc();
3207 const MCExpr *Value;
3208 if (parseExpression(Value))
3209 return true;
3210 // The expression must be the constant 0 or 1.
3211 IsStmt = ~0ULL;
3212 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3213 IsStmt = MCE->getValue();
3214
3215 if (IsStmt > 1)
3216 return Error(Loc, "is_stmt value not 0 or 1");
3217 } else {
3218 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3219 }
3220 }
3221
3222 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
3223 ColumnPos, PrologueEnd, IsStmt, StringRef());
3224 return false;
3225}
3226
3227/// parseDirectiveCVLinetable
3228/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3229bool AsmParser::parseDirectiveCVLinetable() {
3230 int64_t FunctionId = getTok().getIntVal();
3231 if (FunctionId < 0)
3232 return TokError("function id less than zero in '.cv_linetable' directive");
3233 Lex();
3234
3235 if (Lexer.isNot(AsmToken::Comma))
3236 return TokError("unexpected token in '.cv_linetable' directive");
3237 Lex();
3238
3239 SMLoc Loc = getLexer().getLoc();
3240 StringRef FnStartName;
3241 if (parseIdentifier(FnStartName))
3242 return Error(Loc, "expected identifier in directive");
3243
3244 if (Lexer.isNot(AsmToken::Comma))
3245 return TokError("unexpected token in '.cv_linetable' directive");
3246 Lex();
3247
3248 Loc = getLexer().getLoc();
3249 StringRef FnEndName;
3250 if (parseIdentifier(FnEndName))
3251 return Error(Loc, "expected identifier in directive");
3252
3253 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3254 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3255
3256 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3257 return false;
3258}
3259
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003260/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003261/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003262/// ("contains" SecondaryFunctionId+)?
3263bool AsmParser::parseDirectiveCVInlineLinetable() {
3264 int64_t PrimaryFunctionId = getTok().getIntVal();
3265 if (PrimaryFunctionId < 0)
3266 return TokError(
3267 "function id less than zero in '.cv_inline_linetable' directive");
3268 Lex();
3269
3270 int64_t SourceFileId = getTok().getIntVal();
3271 if (SourceFileId <= 0)
3272 return TokError(
3273 "File id less than zero in '.cv_inline_linetable' directive");
3274 Lex();
3275
3276 int64_t SourceLineNum = getTok().getIntVal();
3277 if (SourceLineNum < 0)
3278 return TokError(
3279 "Line number less than zero in '.cv_inline_linetable' directive");
3280 Lex();
3281
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003282 SMLoc Loc = getLexer().getLoc();
3283 StringRef FnStartName;
3284 if (parseIdentifier(FnStartName))
3285 return Error(Loc, "expected identifier in directive");
3286 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3287
David Majnemerc9911f22016-02-02 19:22:34 +00003288 Loc = getLexer().getLoc();
3289 StringRef FnEndName;
3290 if (parseIdentifier(FnEndName))
3291 return Error(Loc, "expected identifier in directive");
3292 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3293
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003294 SmallVector<unsigned, 8> SecondaryFunctionIds;
3295 if (getLexer().is(AsmToken::Identifier)) {
3296 if (getTok().getIdentifier() != "contains")
3297 return TokError(
3298 "unexpected identifier in '.cv_inline_linetable' directive");
3299 Lex();
3300
3301 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3302 int64_t SecondaryFunctionId = getTok().getIntVal();
3303 if (SecondaryFunctionId < 0)
3304 return TokError(
3305 "function id less than zero in '.cv_inline_linetable' directive");
3306 Lex();
3307
3308 SecondaryFunctionIds.push_back(SecondaryFunctionId);
3309 }
3310 }
3311
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003312 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3313 SourceLineNum, FnStartSym,
David Majnemerc9911f22016-02-02 19:22:34 +00003314 FnEndSym, SecondaryFunctionIds);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003315 return false;
3316}
3317
David Majnemer408b5e62016-02-05 01:55:49 +00003318/// parseDirectiveCVDefRange
3319/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3320bool AsmParser::parseDirectiveCVDefRange() {
3321 SMLoc Loc;
3322 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3323 while (getLexer().is(AsmToken::Identifier)) {
3324 Loc = getLexer().getLoc();
3325 StringRef GapStartName;
3326 if (parseIdentifier(GapStartName))
3327 return Error(Loc, "expected identifier in directive");
3328 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3329
3330 Loc = getLexer().getLoc();
3331 StringRef GapEndName;
3332 if (parseIdentifier(GapEndName))
3333 return Error(Loc, "expected identifier in directive");
3334 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3335
3336 Ranges.push_back({GapStartSym, GapEndSym});
3337 }
3338
3339 if (getLexer().isNot(AsmToken::Comma))
3340 return TokError("unexpected token in directive");
3341 Lex();
3342
3343 std::string FixedSizePortion;
3344 if (parseEscapedString(FixedSizePortion))
3345 return true;
3346 Lex();
3347
3348 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3349 return false;
3350}
3351
Reid Kleckner2214ed82016-01-29 00:49:42 +00003352/// parseDirectiveCVStringTable
3353/// ::= .cv_stringtable
3354bool AsmParser::parseDirectiveCVStringTable() {
3355 getStreamer().EmitCVStringTableDirective();
3356 return false;
3357}
3358
3359/// parseDirectiveCVFileChecksums
3360/// ::= .cv_filechecksums
3361bool AsmParser::parseDirectiveCVFileChecksums() {
3362 getStreamer().EmitCVFileChecksumsDirective();
3363 return false;
3364}
3365
Jim Grosbach4b905842013-09-20 23:08:21 +00003366/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003367/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003368bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003369 StringRef Name;
3370 bool EH = false;
3371 bool Debug = false;
3372
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003373 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003374 return TokError("Expected an identifier");
3375
3376 if (Name == ".eh_frame")
3377 EH = true;
3378 else if (Name == ".debug_frame")
3379 Debug = true;
3380
3381 if (getLexer().is(AsmToken::Comma)) {
3382 Lex();
3383
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003384 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003385 return TokError("Expected an identifier");
3386
3387 if (Name == ".eh_frame")
3388 EH = true;
3389 else if (Name == ".debug_frame")
3390 Debug = true;
3391 }
3392
3393 getStreamer().EmitCFISections(EH, Debug);
3394 return false;
3395}
3396
Jim Grosbach4b905842013-09-20 23:08:21 +00003397/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003398/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003399bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003400 StringRef Simple;
3401 if (getLexer().isNot(AsmToken::EndOfStatement))
3402 if (parseIdentifier(Simple) || Simple != "simple")
3403 return TokError("unexpected token in .cfi_startproc directive");
3404
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003405 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003406 return false;
3407}
3408
Jim Grosbach4b905842013-09-20 23:08:21 +00003409/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003410/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003411bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003412 getStreamer().EmitCFIEndProc();
3413 return false;
3414}
3415
Jim Grosbach4b905842013-09-20 23:08:21 +00003416/// \brief parse register name or number.
3417bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003418 SMLoc DirectiveLoc) {
3419 unsigned RegNo;
3420
3421 if (getLexer().isNot(AsmToken::Integer)) {
3422 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3423 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003424 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003425 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003426 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003427
3428 return false;
3429}
3430
Jim Grosbach4b905842013-09-20 23:08:21 +00003431/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003432/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003433bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003434 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003435 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003436 return true;
3437
3438 if (getLexer().isNot(AsmToken::Comma))
3439 return TokError("unexpected token in directive");
3440 Lex();
3441
3442 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003443 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003444 return true;
3445
3446 getStreamer().EmitCFIDefCfa(Register, Offset);
3447 return false;
3448}
3449
Jim Grosbach4b905842013-09-20 23:08:21 +00003450/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003451/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003452bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003453 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003454 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003455 return true;
3456
3457 getStreamer().EmitCFIDefCfaOffset(Offset);
3458 return false;
3459}
3460
Jim Grosbach4b905842013-09-20 23:08:21 +00003461/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003462/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003463bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003464 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003465 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003466 return true;
3467
3468 if (getLexer().isNot(AsmToken::Comma))
3469 return TokError("unexpected token in directive");
3470 Lex();
3471
3472 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003473 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003474 return true;
3475
3476 getStreamer().EmitCFIRegister(Register1, Register2);
3477 return false;
3478}
3479
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003480/// parseDirectiveCFIWindowSave
3481/// ::= .cfi_window_save
3482bool AsmParser::parseDirectiveCFIWindowSave() {
3483 getStreamer().EmitCFIWindowSave();
3484 return false;
3485}
3486
Jim Grosbach4b905842013-09-20 23:08:21 +00003487/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003488/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003489bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003490 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003491 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003492 return true;
3493
3494 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3495 return false;
3496}
3497
Jim Grosbach4b905842013-09-20 23:08:21 +00003498/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003499/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003500bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003501 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003502 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003503 return true;
3504
3505 getStreamer().EmitCFIDefCfaRegister(Register);
3506 return false;
3507}
3508
Jim Grosbach4b905842013-09-20 23:08:21 +00003509/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003510/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003511bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003512 int64_t Register = 0;
3513 int64_t Offset = 0;
3514
Jim Grosbach4b905842013-09-20 23:08:21 +00003515 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003516 return true;
3517
3518 if (getLexer().isNot(AsmToken::Comma))
3519 return TokError("unexpected token in directive");
3520 Lex();
3521
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003522 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003523 return true;
3524
3525 getStreamer().EmitCFIOffset(Register, Offset);
3526 return false;
3527}
3528
Jim Grosbach4b905842013-09-20 23:08:21 +00003529/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003530/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003531bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003532 int64_t Register = 0;
3533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return true;
3536
3537 if (getLexer().isNot(AsmToken::Comma))
3538 return TokError("unexpected token in directive");
3539 Lex();
3540
3541 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003542 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003543 return true;
3544
3545 getStreamer().EmitCFIRelOffset(Register, Offset);
3546 return false;
3547}
3548
3549static bool isValidEncoding(int64_t Encoding) {
3550 if (Encoding & ~0xff)
3551 return false;
3552
3553 if (Encoding == dwarf::DW_EH_PE_omit)
3554 return true;
3555
3556 const unsigned Format = Encoding & 0xf;
3557 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3558 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3559 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3560 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3561 return false;
3562
3563 const unsigned Application = Encoding & 0x70;
3564 if (Application != dwarf::DW_EH_PE_absptr &&
3565 Application != dwarf::DW_EH_PE_pcrel)
3566 return false;
3567
3568 return true;
3569}
3570
Jim Grosbach4b905842013-09-20 23:08:21 +00003571/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003572/// IsPersonality true for cfi_personality, false for cfi_lsda
3573/// ::= .cfi_personality encoding, [symbol_name]
3574/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003575bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003576 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003577 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003578 return true;
3579 if (Encoding == dwarf::DW_EH_PE_omit)
3580 return false;
3581
3582 if (!isValidEncoding(Encoding))
3583 return TokError("unsupported encoding.");
3584
3585 if (getLexer().isNot(AsmToken::Comma))
3586 return TokError("unexpected token in directive");
3587 Lex();
3588
3589 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003590 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003591 return TokError("expected identifier in directive");
3592
Jim Grosbach6f482002015-05-18 18:43:14 +00003593 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003594
3595 if (IsPersonality)
3596 getStreamer().EmitCFIPersonality(Sym, Encoding);
3597 else
3598 getStreamer().EmitCFILsda(Sym, Encoding);
3599 return false;
3600}
3601
Jim Grosbach4b905842013-09-20 23:08:21 +00003602/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003603/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003604bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003605 getStreamer().EmitCFIRememberState();
3606 return false;
3607}
3608
Jim Grosbach4b905842013-09-20 23:08:21 +00003609/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003610/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003611bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003612 getStreamer().EmitCFIRestoreState();
3613 return false;
3614}
3615
Jim Grosbach4b905842013-09-20 23:08:21 +00003616/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003617/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003618bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003619 int64_t Register = 0;
3620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003622 return true;
3623
3624 getStreamer().EmitCFISameValue(Register);
3625 return false;
3626}
3627
Jim Grosbach4b905842013-09-20 23:08:21 +00003628/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003629/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003630bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003631 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003632 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003633 return true;
3634
3635 getStreamer().EmitCFIRestore(Register);
3636 return false;
3637}
3638
Jim Grosbach4b905842013-09-20 23:08:21 +00003639/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003640/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003641bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003642 std::string Values;
3643 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003644 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003645 return true;
3646
3647 Values.push_back((uint8_t)CurrValue);
3648
3649 while (getLexer().is(AsmToken::Comma)) {
3650 Lex();
3651
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003652 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003653 return true;
3654
3655 Values.push_back((uint8_t)CurrValue);
3656 }
3657
3658 getStreamer().EmitCFIEscape(Values);
3659 return false;
3660}
3661
Jim Grosbach4b905842013-09-20 23:08:21 +00003662/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003663/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003664bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003665 if (getLexer().isNot(AsmToken::EndOfStatement))
3666 return Error(getLexer().getLoc(),
3667 "unexpected token in '.cfi_signal_frame'");
3668
3669 getStreamer().EmitCFISignalFrame();
3670 return false;
3671}
3672
Jim Grosbach4b905842013-09-20 23:08:21 +00003673/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003674/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003675bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003676 int64_t Register = 0;
3677
Jim Grosbach4b905842013-09-20 23:08:21 +00003678 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003679 return true;
3680
3681 getStreamer().EmitCFIUndefined(Register);
3682 return false;
3683}
3684
Jim Grosbach4b905842013-09-20 23:08:21 +00003685/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003686/// ::= .macros_on
3687/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003688bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003689 if (getLexer().isNot(AsmToken::EndOfStatement))
3690 return Error(getLexer().getLoc(),
3691 "unexpected token in '" + Directive + "' directive");
3692
Jim Grosbach4b905842013-09-20 23:08:21 +00003693 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003694 return false;
3695}
3696
Jim Grosbach4b905842013-09-20 23:08:21 +00003697/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003698/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003699bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003700 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003701 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003702 return TokError("expected identifier in '.macro' directive");
3703
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003704 if (getLexer().is(AsmToken::Comma))
3705 Lex();
3706
Eli Bendersky17233942013-01-15 22:59:42 +00003707 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003708 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003709
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003710 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003711 return Error(Lexer.getLoc(),
3712 "Vararg parameter '" + Parameters.back().Name +
3713 "' should be last one in the list of parameters.");
3714
David Majnemer91fc4c22014-01-29 18:57:46 +00003715 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003716 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003717 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003718
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003719 if (Lexer.is(AsmToken::Colon)) {
3720 Lex(); // consume ':'
3721
3722 SMLoc QualLoc;
3723 StringRef Qualifier;
3724
3725 QualLoc = Lexer.getLoc();
3726 if (parseIdentifier(Qualifier))
3727 return Error(QualLoc, "missing parameter qualifier for "
3728 "'" + Parameter.Name + "' in macro '" + Name + "'");
3729
3730 if (Qualifier == "req")
3731 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003732 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003733 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003734 else
3735 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3736 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3737 }
3738
David Majnemer91fc4c22014-01-29 18:57:46 +00003739 if (getLexer().is(AsmToken::Equal)) {
3740 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003741
3742 SMLoc ParamLoc;
3743
3744 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003745 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003746 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003747
3748 if (Parameter.Required)
3749 Warning(ParamLoc, "pointless default value for required parameter "
3750 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003751 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003752
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003753 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003754
3755 if (getLexer().is(AsmToken::Comma))
3756 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003757 }
3758
3759 // Eat the end of statement.
3760 Lex();
3761
3762 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003763 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003764
3765 // Lex the macro definition.
3766 for (;;) {
3767 // Check whether we have reached the end of the file.
3768 if (getLexer().is(AsmToken::Eof))
3769 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3770
3771 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003772 if (getLexer().is(AsmToken::Identifier)) {
3773 if (getTok().getIdentifier() == ".endm" ||
3774 getTok().getIdentifier() == ".endmacro") {
3775 if (MacroDepth == 0) { // Outermost macro.
3776 EndToken = getTok();
3777 Lex();
3778 if (getLexer().isNot(AsmToken::EndOfStatement))
3779 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3780 "' directive");
3781 break;
3782 } else {
3783 // Otherwise we just found the end of an inner macro.
3784 --MacroDepth;
3785 }
3786 } else if (getTok().getIdentifier() == ".macro") {
3787 // We allow nested macros. Those aren't instantiated until the outermost
3788 // macro is expanded so just ignore them for now.
3789 ++MacroDepth;
3790 }
Eli Bendersky17233942013-01-15 22:59:42 +00003791 }
3792
3793 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003794 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003795 }
3796
Jim Grosbach4b905842013-09-20 23:08:21 +00003797 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003798 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3799 }
3800
3801 const char *BodyStart = StartToken.getLoc().getPointer();
3802 const char *BodyEnd = EndToken.getLoc().getPointer();
3803 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003804 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003805 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003806 return false;
3807}
3808
Jim Grosbach4b905842013-09-20 23:08:21 +00003809/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003810///
3811/// With the support added for named parameters there may be code out there that
3812/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003813/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003814/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003815/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003816/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3817/// warning that the positional parameter found in body which have no effect.
3818/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003819/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003820/// intended or change the macro to use the named parameters. It is possible
3821/// this warning will trigger when the none of the named parameters are used
3822/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003823void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003824 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003825 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003826 // If this macro is not defined with named parameters the warning we are
3827 // checking for here doesn't apply.
3828 unsigned NParameters = Parameters.size();
3829 if (NParameters == 0)
3830 return;
3831
3832 bool NamedParametersFound = false;
3833 bool PositionalParametersFound = false;
3834
3835 // Look at the body of the macro for use of both the named parameters and what
3836 // are likely to be positional parameters. This is what expandMacro() is
3837 // doing when it finds the parameters in the body.
3838 while (!Body.empty()) {
3839 // Scan for the next possible parameter.
3840 std::size_t End = Body.size(), Pos = 0;
3841 for (; Pos != End; ++Pos) {
3842 // Check for a substitution or escape.
3843 // This macro is defined with parameters, look for \foo, \bar, etc.
3844 if (Body[Pos] == '\\' && Pos + 1 != End)
3845 break;
3846
3847 // This macro should have parameters, but look for $0, $1, ..., $n too.
3848 if (Body[Pos] != '$' || Pos + 1 == End)
3849 continue;
3850 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003851 if (Next == '$' || Next == 'n' ||
3852 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003853 break;
3854 }
3855
3856 // Check if we reached the end.
3857 if (Pos == End)
3858 break;
3859
3860 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003861 switch (Body[Pos + 1]) {
3862 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003863 case '$':
3864 break;
3865
Jim Grosbach4b905842013-09-20 23:08:21 +00003866 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003867 case 'n':
3868 PositionalParametersFound = true;
3869 break;
3870
Jim Grosbach4b905842013-09-20 23:08:21 +00003871 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003872 default: {
3873 PositionalParametersFound = true;
3874 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003875 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003876 }
3877 Pos += 2;
3878 } else {
3879 unsigned I = Pos + 1;
3880 while (isIdentifierChar(Body[I]) && I + 1 != End)
3881 ++I;
3882
Jim Grosbach4b905842013-09-20 23:08:21 +00003883 const char *Begin = Body.data() + Pos + 1;
3884 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003885 unsigned Index = 0;
3886 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003887 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003888 break;
3889
3890 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003891 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3892 Pos += 3;
3893 else {
3894 Pos = I;
3895 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003896 } else {
3897 NamedParametersFound = true;
3898 Pos += 1 + Argument.size();
3899 }
3900 }
3901 // Update the scan point.
3902 Body = Body.substr(Pos);
3903 }
3904
3905 if (!NamedParametersFound && PositionalParametersFound)
3906 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3907 "used in macro body, possible positional parameter "
3908 "found in body which will have no effect");
3909}
3910
Nico Weber155dccd12014-07-24 17:08:39 +00003911/// parseDirectiveExitMacro
3912/// ::= .exitm
3913bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3914 if (getLexer().isNot(AsmToken::EndOfStatement))
3915 return TokError("unexpected token in '" + Directive + "' directive");
3916
3917 if (!isInsideMacroInstantiation())
3918 return TokError("unexpected '" + Directive + "' in file, "
3919 "no current macro definition");
3920
3921 // Exit all conditionals that are active in the current macro.
3922 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3923 TheCondState = TheCondStack.back();
3924 TheCondStack.pop_back();
3925 }
3926
3927 handleMacroExit();
3928 return false;
3929}
3930
Jim Grosbach4b905842013-09-20 23:08:21 +00003931/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003932/// ::= .endm
3933/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003934bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003935 if (getLexer().isNot(AsmToken::EndOfStatement))
3936 return TokError("unexpected token in '" + Directive + "' directive");
3937
3938 // If we are inside a macro instantiation, terminate the current
3939 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003940 if (isInsideMacroInstantiation()) {
3941 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003942 return false;
3943 }
3944
3945 // Otherwise, this .endmacro is a stray entry in the file; well formed
3946 // .endmacro directives are handled during the macro definition parsing.
3947 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003948 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003949}
3950
Jim Grosbach4b905842013-09-20 23:08:21 +00003951/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003952/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003953bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003954 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003955 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003956 return TokError("expected identifier in '.purgem' directive");
3957
3958 if (getLexer().isNot(AsmToken::EndOfStatement))
3959 return TokError("unexpected token in '.purgem' directive");
3960
Jim Grosbach4b905842013-09-20 23:08:21 +00003961 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003962 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3963
Jim Grosbach4b905842013-09-20 23:08:21 +00003964 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003965 return false;
3966}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003967
Jim Grosbach4b905842013-09-20 23:08:21 +00003968/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003969/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003970bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003971 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003972
3973 // Expect a single argument: an expression that evaluates to a constant
3974 // in the inclusive range 0-30.
3975 SMLoc ExprLoc = getLexer().getLoc();
3976 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003977 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003978 return true;
3979 else if (getLexer().isNot(AsmToken::EndOfStatement))
3980 return TokError("unexpected token after expression in"
3981 " '.bundle_align_mode' directive");
3982 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3983 return Error(ExprLoc,
3984 "invalid bundle alignment size (expected between 0 and 30)");
3985
3986 Lex();
3987
3988 // Because of AlignSizePow2's verified range we can safely truncate it to
3989 // unsigned.
3990 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3991 return false;
3992}
3993
Jim Grosbach4b905842013-09-20 23:08:21 +00003994/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003995/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003996bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003997 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003998 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003999
Eli Bendersky802b6282013-01-07 21:51:08 +00004000 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4001 StringRef Option;
4002 SMLoc Loc = getTok().getLoc();
4003 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004004 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004005
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004006 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00004007 return Error(Loc, kInvalidOptionError);
4008
4009 if (Option != "align_to_end")
4010 return Error(Loc, kInvalidOptionError);
4011 else if (getLexer().isNot(AsmToken::EndOfStatement))
4012 return Error(Loc,
4013 "unexpected token after '.bundle_lock' directive option");
4014 AlignToEnd = true;
4015 }
4016
Eli Benderskyf483ff92012-12-20 19:05:53 +00004017 Lex();
4018
Eli Bendersky802b6282013-01-07 21:51:08 +00004019 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004020 return false;
4021}
4022
Jim Grosbach4b905842013-09-20 23:08:21 +00004023/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004024/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004025bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004026 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004027
4028 if (getLexer().isNot(AsmToken::EndOfStatement))
4029 return TokError("unexpected token in '.bundle_unlock' directive");
4030 Lex();
4031
4032 getStreamer().EmitBundleUnlock();
4033 return false;
4034}
4035
Jim Grosbach4b905842013-09-20 23:08:21 +00004036/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004037/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004038bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004039 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004040
4041 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004042 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004043 return true;
4044
4045 int64_t FillExpr = 0;
4046 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4047 if (getLexer().isNot(AsmToken::Comma))
4048 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4049 Lex();
4050
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004051 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004052 return true;
4053
4054 if (getLexer().isNot(AsmToken::EndOfStatement))
4055 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4056 }
4057
4058 Lex();
4059
4060 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00004061 return TokError("invalid number of bytes in '" + Twine(IDVal) +
4062 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00004063
4064 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00004065 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00004066
4067 return false;
4068}
4069
Jim Grosbach4b905842013-09-20 23:08:21 +00004070/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004071/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004072bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004073 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004074 const MCExpr *Value;
4075
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004076 for (;;) {
4077 if (parseExpression(Value))
4078 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004079
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004080 if (Signed)
4081 getStreamer().EmitSLEB128Value(Value);
4082 else
4083 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004084
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004085 if (getLexer().is(AsmToken::EndOfStatement))
4086 break;
4087
4088 if (getLexer().isNot(AsmToken::Comma))
4089 return TokError("unexpected token in directive");
4090 Lex();
4091 }
Eli Bendersky17233942013-01-15 22:59:42 +00004092
4093 return false;
4094}
4095
Jim Grosbach4b905842013-09-20 23:08:21 +00004096/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004097/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004098bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004099 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00004100 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004101 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004102 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004103
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004104 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004105 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004106
Jim Grosbach6f482002015-05-18 18:43:14 +00004107 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004108
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004109 // Assembler local symbols don't make any sense here. Complain loudly.
4110 if (Sym->isTemporary())
4111 return Error(Loc, "non-local symbol required in directive");
4112
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004113 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4114 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004115
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004116 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004117 break;
4118
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004119 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004120 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004121 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00004122 }
4123 }
4124
Sean Callanan686ed8d2010-01-19 20:22:31 +00004125 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004126 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004127}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004128
Jim Grosbach4b905842013-09-20 23:08:21 +00004129/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004130/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004131bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004132 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004133
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004134 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004135 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004136 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004137 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004138
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004139 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004140 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004141
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004142 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004143 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004144 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004145
4146 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004147 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004148 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004149 return true;
4150
4151 int64_t Pow2Alignment = 0;
4152 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004153 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004154 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004155 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004156 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004157 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004158
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004159 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4160 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004161 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4162
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004163 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004164 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4165 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004166 if (!isPowerOf2_64(Pow2Alignment))
4167 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4168 Pow2Alignment = Log2_64(Pow2Alignment);
4169 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004170 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004171
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004172 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004173 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004174
Sean Callanan686ed8d2010-01-19 20:22:31 +00004175 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004176
Chris Lattner28ad7542009-07-09 17:25:12 +00004177 // NOTE: a size of zero for a .comm should create a undefined symbol
4178 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004179 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004180 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004181 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004182
Eric Christopherbc818852010-05-14 01:38:54 +00004183 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004184 // may internally end up wanting an alignment in bytes.
4185 // FIXME: Diagnose overflow.
4186 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004187 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004188 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004189
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004190 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004191 return Error(IDLoc, "invalid symbol redefinition");
4192
Chris Lattner28ad7542009-07-09 17:25:12 +00004193 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004194 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004195 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004196 return false;
4197 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004198
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004199 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004200 return false;
4201}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004202
Jim Grosbach4b905842013-09-20 23:08:21 +00004203/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004204/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004205bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004206 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004207 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004208
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004209 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004210 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00004211 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004212
Sean Callanan686ed8d2010-01-19 20:22:31 +00004213 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00004214
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004215 if (Str.empty())
4216 Error(Loc, ".abort detected. Assembly stopping.");
4217 else
4218 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004219 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004220
4221 return false;
4222}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004223
Jim Grosbach4b905842013-09-20 23:08:21 +00004224/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004225/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004226bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004227 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004228 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004229
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004230 // Allow the strings to have escaped octal character sequence.
4231 std::string Filename;
4232 if (parseEscapedString(Filename))
4233 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004234 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00004235 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004236
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004237 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004238 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004239
Chris Lattner693fbb82009-07-16 06:14:39 +00004240 // Attempt to switch the lexer to the included file before consuming the end
4241 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00004242 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00004243 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00004244 return true;
4245 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004246
4247 return false;
4248}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004249
Jim Grosbach4b905842013-09-20 23:08:21 +00004250/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004251/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004252bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004253 if (getLexer().isNot(AsmToken::String))
4254 return TokError("expected string in '.incbin' directive");
4255
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004256 // Allow the strings to have escaped octal character sequence.
4257 std::string Filename;
4258 if (parseEscapedString(Filename))
4259 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00004260 SMLoc IncbinLoc = getLexer().getLoc();
4261 Lex();
4262
4263 if (getLexer().isNot(AsmToken::EndOfStatement))
4264 return TokError("unexpected token in '.incbin' directive");
4265
Kevin Enderby109f25c2011-12-14 21:47:48 +00004266 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00004267 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004268 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
4269 return true;
4270 }
4271
4272 return false;
4273}
4274
Jim Grosbach4b905842013-09-20 23:08:21 +00004275/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004276/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4277bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004278 TheCondStack.push_back(TheCondState);
4279 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004280 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004281 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004282 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004283 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004284 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004285 return true;
4286
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004287 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004288 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004289
Sean Callanan686ed8d2010-01-19 20:22:31 +00004290 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004291
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004292 switch (DirKind) {
4293 default:
4294 llvm_unreachable("unsupported directive");
4295 case DK_IF:
4296 case DK_IFNE:
4297 break;
4298 case DK_IFEQ:
4299 ExprValue = ExprValue == 0;
4300 break;
4301 case DK_IFGE:
4302 ExprValue = ExprValue >= 0;
4303 break;
4304 case DK_IFGT:
4305 ExprValue = ExprValue > 0;
4306 break;
4307 case DK_IFLE:
4308 ExprValue = ExprValue <= 0;
4309 break;
4310 case DK_IFLT:
4311 ExprValue = ExprValue < 0;
4312 break;
4313 }
4314
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004315 TheCondState.CondMet = ExprValue;
4316 TheCondState.Ignore = !TheCondState.CondMet;
4317 }
4318
4319 return false;
4320}
4321
Jim Grosbach4b905842013-09-20 23:08:21 +00004322/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004323/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004324bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004325 TheCondStack.push_back(TheCondState);
4326 TheCondState.TheCond = AsmCond::IfCond;
4327
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004328 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004329 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004330 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004331 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004332
4333 if (getLexer().isNot(AsmToken::EndOfStatement))
4334 return TokError("unexpected token in '.ifb' directive");
4335
4336 Lex();
4337
4338 TheCondState.CondMet = ExpectBlank == Str.empty();
4339 TheCondState.Ignore = !TheCondState.CondMet;
4340 }
4341
4342 return false;
4343}
4344
Jim Grosbach4b905842013-09-20 23:08:21 +00004345/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004346/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004347/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004348bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004349 TheCondStack.push_back(TheCondState);
4350 TheCondState.TheCond = AsmCond::IfCond;
4351
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004352 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004353 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004354 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004355 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004356
4357 if (getLexer().isNot(AsmToken::Comma))
4358 return TokError("unexpected token in '.ifc' directive");
4359
4360 Lex();
4361
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004362 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004363
4364 if (getLexer().isNot(AsmToken::EndOfStatement))
4365 return TokError("unexpected token in '.ifc' directive");
4366
4367 Lex();
4368
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004369 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004370 TheCondState.Ignore = !TheCondState.CondMet;
4371 }
4372
4373 return false;
4374}
4375
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004376/// parseDirectiveIfeqs
4377/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004378bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004379 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004380 if (ExpectEqual)
4381 TokError("expected string parameter for '.ifeqs' directive");
4382 else
4383 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004384 eatToEndOfStatement();
4385 return true;
4386 }
4387
4388 StringRef String1 = getTok().getStringContents();
4389 Lex();
4390
4391 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004392 if (ExpectEqual)
4393 TokError("expected comma after first string for '.ifeqs' directive");
4394 else
4395 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004396 eatToEndOfStatement();
4397 return true;
4398 }
4399
4400 Lex();
4401
4402 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004403 if (ExpectEqual)
4404 TokError("expected string parameter for '.ifeqs' directive");
4405 else
4406 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004407 eatToEndOfStatement();
4408 return true;
4409 }
4410
4411 StringRef String2 = getTok().getStringContents();
4412 Lex();
4413
4414 TheCondStack.push_back(TheCondState);
4415 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004416 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004417 TheCondState.Ignore = !TheCondState.CondMet;
4418
4419 return false;
4420}
4421
Jim Grosbach4b905842013-09-20 23:08:21 +00004422/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004423/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004424bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004425 StringRef Name;
4426 TheCondStack.push_back(TheCondState);
4427 TheCondState.TheCond = AsmCond::IfCond;
4428
4429 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004430 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004431 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004432 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004433 return TokError("expected identifier after '.ifdef'");
4434
4435 Lex();
4436
Jim Grosbach6f482002015-05-18 18:43:14 +00004437 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004438
4439 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004440 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004441 else
Craig Topper353eda42014-04-24 06:44:33 +00004442 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004443 TheCondState.Ignore = !TheCondState.CondMet;
4444 }
4445
4446 return false;
4447}
4448
Jim Grosbach4b905842013-09-20 23:08:21 +00004449/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004450/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004451bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004452 if (TheCondState.TheCond != AsmCond::IfCond &&
4453 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004454 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4455 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004456 TheCondState.TheCond = AsmCond::ElseIfCond;
4457
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004458 bool LastIgnoreState = false;
4459 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004460 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004461 if (LastIgnoreState || TheCondState.CondMet) {
4462 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004463 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004464 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004465 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004466 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004467 return true;
4468
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004469 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004470 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004471
Sean Callanan686ed8d2010-01-19 20:22:31 +00004472 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004473 TheCondState.CondMet = ExprValue;
4474 TheCondState.Ignore = !TheCondState.CondMet;
4475 }
4476
4477 return false;
4478}
4479
Jim Grosbach4b905842013-09-20 23:08:21 +00004480/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004481/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004482bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004483 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004484 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004485
Sean Callanan686ed8d2010-01-19 20:22:31 +00004486 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004487
4488 if (TheCondState.TheCond != AsmCond::IfCond &&
4489 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004490 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4491 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004492 TheCondState.TheCond = AsmCond::ElseCond;
4493 bool LastIgnoreState = false;
4494 if (!TheCondStack.empty())
4495 LastIgnoreState = TheCondStack.back().Ignore;
4496 if (LastIgnoreState || TheCondState.CondMet)
4497 TheCondState.Ignore = true;
4498 else
4499 TheCondState.Ignore = false;
4500
4501 return false;
4502}
4503
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004504/// parseDirectiveEnd
4505/// ::= .end
4506bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4507 if (getLexer().isNot(AsmToken::EndOfStatement))
4508 return TokError("unexpected token in '.end' directive");
4509
4510 Lex();
4511
4512 while (Lexer.isNot(AsmToken::Eof))
4513 Lex();
4514
4515 return false;
4516}
4517
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004518/// parseDirectiveError
4519/// ::= .err
4520/// ::= .error [string]
4521bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4522 if (!TheCondStack.empty()) {
4523 if (TheCondStack.back().Ignore) {
4524 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004525 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004526 }
4527 }
4528
4529 if (!WithMessage)
4530 return Error(L, ".err encountered");
4531
4532 StringRef Message = ".error directive invoked in source file";
4533 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4534 if (Lexer.isNot(AsmToken::String)) {
4535 TokError(".error argument must be a string");
4536 eatToEndOfStatement();
4537 return true;
4538 }
4539
4540 Message = getTok().getStringContents();
4541 Lex();
4542 }
4543
4544 Error(L, Message);
4545 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004546}
4547
Nico Weber404012b2014-07-24 16:26:06 +00004548/// parseDirectiveWarning
4549/// ::= .warning [string]
4550bool AsmParser::parseDirectiveWarning(SMLoc L) {
4551 if (!TheCondStack.empty()) {
4552 if (TheCondStack.back().Ignore) {
4553 eatToEndOfStatement();
4554 return false;
4555 }
4556 }
4557
4558 StringRef Message = ".warning directive invoked in source file";
4559 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4560 if (Lexer.isNot(AsmToken::String)) {
4561 TokError(".warning argument must be a string");
4562 eatToEndOfStatement();
4563 return true;
4564 }
4565
4566 Message = getTok().getStringContents();
4567 Lex();
4568 }
4569
4570 Warning(L, Message);
4571 return false;
4572}
4573
Jim Grosbach4b905842013-09-20 23:08:21 +00004574/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004575/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004576bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004577 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004578 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004579
Sean Callanan686ed8d2010-01-19 20:22:31 +00004580 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004581
Jim Grosbach4b905842013-09-20 23:08:21 +00004582 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004583 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4584 ".else");
4585 if (!TheCondStack.empty()) {
4586 TheCondState = TheCondStack.back();
4587 TheCondStack.pop_back();
4588 }
4589
4590 return false;
4591}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004592
Eli Bendersky17233942013-01-15 22:59:42 +00004593void AsmParser::initializeDirectiveKindMap() {
4594 DirectiveKindMap[".set"] = DK_SET;
4595 DirectiveKindMap[".equ"] = DK_EQU;
4596 DirectiveKindMap[".equiv"] = DK_EQUIV;
4597 DirectiveKindMap[".ascii"] = DK_ASCII;
4598 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4599 DirectiveKindMap[".string"] = DK_STRING;
4600 DirectiveKindMap[".byte"] = DK_BYTE;
4601 DirectiveKindMap[".short"] = DK_SHORT;
4602 DirectiveKindMap[".value"] = DK_VALUE;
4603 DirectiveKindMap[".2byte"] = DK_2BYTE;
4604 DirectiveKindMap[".long"] = DK_LONG;
4605 DirectiveKindMap[".int"] = DK_INT;
4606 DirectiveKindMap[".4byte"] = DK_4BYTE;
4607 DirectiveKindMap[".quad"] = DK_QUAD;
4608 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004609 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004610 DirectiveKindMap[".single"] = DK_SINGLE;
4611 DirectiveKindMap[".float"] = DK_FLOAT;
4612 DirectiveKindMap[".double"] = DK_DOUBLE;
4613 DirectiveKindMap[".align"] = DK_ALIGN;
4614 DirectiveKindMap[".align32"] = DK_ALIGN32;
4615 DirectiveKindMap[".balign"] = DK_BALIGN;
4616 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4617 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4618 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4619 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4620 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4621 DirectiveKindMap[".org"] = DK_ORG;
4622 DirectiveKindMap[".fill"] = DK_FILL;
4623 DirectiveKindMap[".zero"] = DK_ZERO;
4624 DirectiveKindMap[".extern"] = DK_EXTERN;
4625 DirectiveKindMap[".globl"] = DK_GLOBL;
4626 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004627 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4628 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4629 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
Lang Hames1b640e02016-03-15 01:43:05 +00004630 DirectiveKindMap[".alt_entry"] = DK_ALT_ENTRY;
Eli Bendersky17233942013-01-15 22:59:42 +00004631 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4632 DirectiveKindMap[".reference"] = DK_REFERENCE;
4633 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4634 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4635 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4636 DirectiveKindMap[".comm"] = DK_COMM;
4637 DirectiveKindMap[".common"] = DK_COMMON;
4638 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4639 DirectiveKindMap[".abort"] = DK_ABORT;
4640 DirectiveKindMap[".include"] = DK_INCLUDE;
4641 DirectiveKindMap[".incbin"] = DK_INCBIN;
4642 DirectiveKindMap[".code16"] = DK_CODE16;
4643 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4644 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004645 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004646 DirectiveKindMap[".irp"] = DK_IRP;
4647 DirectiveKindMap[".irpc"] = DK_IRPC;
4648 DirectiveKindMap[".endr"] = DK_ENDR;
4649 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4650 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4651 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4652 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004653 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4654 DirectiveKindMap[".ifge"] = DK_IFGE;
4655 DirectiveKindMap[".ifgt"] = DK_IFGT;
4656 DirectiveKindMap[".ifle"] = DK_IFLE;
4657 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004658 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004659 DirectiveKindMap[".ifb"] = DK_IFB;
4660 DirectiveKindMap[".ifnb"] = DK_IFNB;
4661 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004662 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004663 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004664 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004665 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4666 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4667 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4668 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4669 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004670 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004671 DirectiveKindMap[".endif"] = DK_ENDIF;
4672 DirectiveKindMap[".skip"] = DK_SKIP;
4673 DirectiveKindMap[".space"] = DK_SPACE;
4674 DirectiveKindMap[".file"] = DK_FILE;
4675 DirectiveKindMap[".line"] = DK_LINE;
4676 DirectiveKindMap[".loc"] = DK_LOC;
4677 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004678 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
4679 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4680 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004681 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
David Majnemer408b5e62016-02-05 01:55:49 +00004682 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004683 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4684 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004685 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4686 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4687 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4688 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4689 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4690 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4691 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4692 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4693 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4694 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4695 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4696 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4697 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4698 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4699 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4700 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4701 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4702 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4703 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4704 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4705 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004706 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004707 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4708 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4709 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004710 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004711 DirectiveKindMap[".endm"] = DK_ENDM;
4712 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4713 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004714 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004715 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004716 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004717 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004718}
4719
Jim Grosbach4b905842013-09-20 23:08:21 +00004720MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004721 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004722
Rafael Espindola34b9c512012-06-03 23:57:14 +00004723 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004724 for (;;) {
4725 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004726 if (getLexer().is(AsmToken::Eof)) {
4727 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004728 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004729 }
4730
Rafael Espindola34b9c512012-06-03 23:57:14 +00004731 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004732 (getTok().getIdentifier() == ".rept" ||
4733 getTok().getIdentifier() == ".irp" ||
4734 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004735 ++NestLevel;
4736 }
4737
4738 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004739 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004740 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004741 EndToken = getTok();
4742 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004743 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4744 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004745 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004746 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004747 break;
4748 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004749 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004750 }
4751
Rafael Espindola34b9c512012-06-03 23:57:14 +00004752 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004753 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004754 }
4755
4756 const char *BodyStart = StartToken.getLoc().getPointer();
4757 const char *BodyEnd = EndToken.getLoc().getPointer();
4758 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4759
Rafael Espindola34b9c512012-06-03 23:57:14 +00004760 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004761 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004762 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004763}
4764
Jim Grosbach4b905842013-09-20 23:08:21 +00004765void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004766 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004767 OS << ".endr\n";
4768
Rafael Espindola3560ff22014-08-27 20:03:13 +00004769 std::unique_ptr<MemoryBuffer> Instantiation =
4770 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004771
Rafael Espindola34b9c512012-06-03 23:57:14 +00004772 // Create the macro instantiation object and add to the current macro
4773 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004774 MacroInstantiation *MI = new MacroInstantiation(
4775 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004776 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004777
Rafael Espindola34b9c512012-06-03 23:57:14 +00004778 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004779 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004780 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004781 Lex();
4782}
4783
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004784/// parseDirectiveRept
4785/// ::= .rep | .rept count
4786bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004787 const MCExpr *CountExpr;
4788 SMLoc CountLoc = getTok().getLoc();
4789 if (parseExpression(CountExpr))
4790 return true;
4791
Rafael Espindola34b9c512012-06-03 23:57:14 +00004792 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004793 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004794 eatToEndOfStatement();
4795 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4796 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004797
4798 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004799 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004800
4801 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004802 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004803
4804 // Eat the end of statement.
4805 Lex();
4806
4807 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004808 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004809 if (!M)
4810 return true;
4811
4812 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4813 // to hold the macro body with substitutions.
4814 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004815 raw_svector_ostream OS(Buf);
4816 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004817 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4818 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004819 return true;
4820 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004821 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004822
4823 return false;
4824}
4825
Jim Grosbach4b905842013-09-20 23:08:21 +00004826/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004827/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004828bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004829 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004830
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004831 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004832 return TokError("expected identifier in '.irp' directive");
4833
Rafael Espindola768b41c2012-06-15 14:02:34 +00004834 if (Lexer.isNot(AsmToken::Comma))
4835 return TokError("expected comma in '.irp' directive");
4836
4837 Lex();
4838
Eli Bendersky38274122013-01-14 23:22:36 +00004839 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004840 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004841 return true;
4842
4843 // Eat the end of statement.
4844 Lex();
4845
4846 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004847 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004848 if (!M)
4849 return true;
4850
4851 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4852 // to hold the macro body with substitutions.
4853 SmallString<256> Buf;
4854 raw_svector_ostream OS(Buf);
4855
Craig Topper84008482015-10-10 05:38:14 +00004856 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004857 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4858 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004859 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004860 return true;
4861 }
4862
Jim Grosbach4b905842013-09-20 23:08:21 +00004863 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004864
4865 return false;
4866}
4867
Jim Grosbach4b905842013-09-20 23:08:21 +00004868/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004869/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004870bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004871 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004872
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004873 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004874 return TokError("expected identifier in '.irpc' directive");
4875
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004876 if (Lexer.isNot(AsmToken::Comma))
4877 return TokError("expected comma in '.irpc' directive");
4878
4879 Lex();
4880
Eli Bendersky38274122013-01-14 23:22:36 +00004881 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004882 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004883 return true;
4884
4885 if (A.size() != 1 || A.front().size() != 1)
4886 return TokError("unexpected token in '.irpc' directive");
4887
4888 // Eat the end of statement.
4889 Lex();
4890
4891 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004892 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004893 if (!M)
4894 return true;
4895
4896 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4897 // to hold the macro body with substitutions.
4898 SmallString<256> Buf;
4899 raw_svector_ostream OS(Buf);
4900
4901 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004902 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004903 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004904 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004905
Toma Tabacu217116e2015-04-27 10:50:29 +00004906 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4907 // This is undocumented, but GAS seems to support it.
4908 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004909 return true;
4910 }
4911
Jim Grosbach4b905842013-09-20 23:08:21 +00004912 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004913
4914 return false;
4915}
4916
Jim Grosbach4b905842013-09-20 23:08:21 +00004917bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004918 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004919 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004920
4921 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004922 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004923 assert(getLexer().is(AsmToken::EndOfStatement));
4924
Jim Grosbach4b905842013-09-20 23:08:21 +00004925 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004926 return false;
4927}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004928
Jim Grosbach4b905842013-09-20 23:08:21 +00004929bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004930 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004931 const MCExpr *Value;
4932 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004933 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004934 return true;
4935 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4936 if (!MCE)
4937 return Error(ExprLoc, "unexpected expression in _emit");
4938 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004939 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004940 return Error(ExprLoc, "literal value out of range for directive");
4941
Craig Topper7d5b2312015-10-10 05:25:02 +00004942 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004943 return false;
4944}
4945
Jim Grosbach4b905842013-09-20 23:08:21 +00004946bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004947 const MCExpr *Value;
4948 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004949 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004950 return true;
4951 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4952 if (!MCE)
4953 return Error(ExprLoc, "unexpected expression in align");
4954 uint64_t IntValue = MCE->getValue();
4955 if (!isPowerOf2_64(IntValue))
4956 return Error(ExprLoc, "literal value not a power of two greater then zero");
4957
Craig Topper7d5b2312015-10-10 05:25:02 +00004958 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004959 return false;
4960}
4961
Chad Rosierf43fcf52013-02-13 21:27:17 +00004962// We are comparing pointers, but the pointers are relative to a single string.
4963// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004964static int rewritesSort(const AsmRewrite *AsmRewriteA,
4965 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004966 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4967 return -1;
4968 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4969 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004970
Chad Rosierfce4fab2013-04-08 17:43:47 +00004971 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4972 // rewrite to the same location. Make sure the SizeDirective rewrite is
4973 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4974 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004975 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4976 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004977 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004978
Jim Grosbach4b905842013-09-20 23:08:21 +00004979 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4980 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004981 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004982 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004983}
4984
Jim Grosbach4b905842013-09-20 23:08:21 +00004985bool AsmParser::parseMSInlineAsm(
4986 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4987 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4988 SmallVectorImpl<std::string> &Constraints,
4989 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4990 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004991 SmallVector<void *, 4> InputDecls;
4992 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004993 SmallVector<bool, 4> InputDeclsAddressOf;
4994 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004995 SmallVector<std::string, 4> InputConstraints;
4996 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004997 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004998
Benjamin Kramer1a136112013-02-15 20:37:21 +00004999 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005000
5001 // Prime the lexer.
5002 Lex();
5003
5004 // While we have input, parse each statement.
5005 unsigned InputIdx = 0;
5006 unsigned OutputIdx = 0;
5007 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005008 // Parse curly braces marking block start/end
5009 if (parseCurlyBlockScope(AsmStrRewrites))
5010 continue;
5011
Eli Friedman0f4871d2012-10-22 23:58:19 +00005012 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005013 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00005014 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00005015
Chad Rosier149e8e02012-12-12 22:45:52 +00005016 if (Info.ParseError)
5017 return true;
5018
Benjamin Kramer1a136112013-02-15 20:37:21 +00005019 if (Info.Opcode == ~0U)
5020 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005021
Benjamin Kramer1a136112013-02-15 20:37:21 +00005022 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005023
Benjamin Kramer1a136112013-02-15 20:37:21 +00005024 // Build the list of clobbers, outputs and inputs.
5025 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005026 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005027
Benjamin Kramer1a136112013-02-15 20:37:21 +00005028 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005029 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005030 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005031
Benjamin Kramer1a136112013-02-15 20:37:21 +00005032 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005033 if (Operand.isReg() && !Operand.needAddressOf() &&
5034 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005035 unsigned NumDefs = Desc.getNumDefs();
5036 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005037 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5038 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005039 continue;
5040 }
5041
5042 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005043 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005044 if (SymName.empty())
5045 continue;
5046
David Blaikie960ea3f2014-06-08 16:18:35 +00005047 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005048 if (!OpDecl)
5049 continue;
5050
5051 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005052 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005053 if (isOutput) {
5054 ++InputIdx;
5055 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005056 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005057 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005058 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005059 } else {
5060 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005061 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5062 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005063 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005064 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005065 }
Reid Kleckneree088972013-12-10 18:27:32 +00005066
5067 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005068 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5069 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005070 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005071 }
5072
5073 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005074 NumOutputs = OutputDecls.size();
5075 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005076
5077 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005078 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5079 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5080 ClobberRegs.end());
5081 Clobbers.assign(ClobberRegs.size(), std::string());
5082 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5083 raw_string_ostream OS(Clobbers[I]);
5084 IP->printRegName(OS, ClobberRegs[I]);
5085 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005086
5087 // Merge the various outputs and inputs. Output are expected first.
5088 if (NumOutputs || NumInputs) {
5089 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005090 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005091 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005092 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005093 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005094 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005095 }
5096 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005097 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005098 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005099 }
5100 }
5101
5102 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005103 std::string AsmStringIR;
5104 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005105 StringRef ASMString =
5106 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5107 const char *AsmStart = ASMString.begin();
5108 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005109 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005110 for (const AsmRewrite &AR : AsmStrRewrites) {
5111 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005112 if (Kind == AOK_Delete)
5113 continue;
5114
David Majnemer8114c1a2014-06-23 02:17:16 +00005115 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005116 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005117
Chad Rosier120eefd2013-03-19 17:32:17 +00005118 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005119 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005120 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005121
Chad Rosier37e755c2012-10-23 17:43:43 +00005122 // Skip the original expression.
5123 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005124 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005125 continue;
5126 }
5127
Chad Rosierff10ed12013-04-12 16:26:42 +00005128 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005129 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005130 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005131 default:
5132 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005133 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005134 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005135 break;
5136 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005137 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005138 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005139 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005140 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005141 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005142 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005143 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005144 break;
5145 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005146 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005147 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005148 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005149 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005150 default: break;
5151 case 8: OS << "byte ptr "; break;
5152 case 16: OS << "word ptr "; break;
5153 case 32: OS << "dword ptr "; break;
5154 case 64: OS << "qword ptr "; break;
5155 case 80: OS << "xword ptr "; break;
5156 case 128: OS << "xmmword ptr "; break;
5157 case 256: OS << "ymmword ptr "; break;
5158 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005159 break;
5160 case AOK_Emit:
5161 OS << ".byte";
5162 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005163 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005164 // MS alignment directives are measured in bytes. If the native assembler
5165 // measures alignment in bytes, we can pass it straight through.
5166 OS << ".align";
5167 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5168 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005169
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005170 // Alignment is in log2 form, so print that instead and skip the original
5171 // immediate.
5172 unsigned Val = AR.Val;
5173 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005174 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005175 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5176 break;
5177 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005178 case AOK_EVEN:
5179 OS << ".even";
5180 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005181 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005182 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005183 OS.flush();
5184 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005185 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005186 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005187 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005188 case AOK_EndOfStatement:
5189 OS << "\n\t";
5190 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005191 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005192
Chad Rosier8bce6642012-10-18 15:49:34 +00005193 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005194 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005195 }
5196
5197 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005198 if (AsmStart != AsmEnd)
5199 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005200
5201 AsmString = OS.str();
5202 return false;
5203}
5204
Pete Cooper80d21cb2015-06-22 19:35:57 +00005205namespace llvm {
5206namespace MCParserUtils {
5207
5208/// Returns whether the given symbol is used anywhere in the given expression,
5209/// or subexpressions.
5210static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5211 switch (Value->getKind()) {
5212 case MCExpr::Binary: {
5213 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5214 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5215 isSymbolUsedInExpression(Sym, BE->getRHS());
5216 }
5217 case MCExpr::Target:
5218 case MCExpr::Constant:
5219 return false;
5220 case MCExpr::SymbolRef: {
5221 const MCSymbol &S =
5222 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5223 if (S.isVariable())
5224 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5225 return &S == Sym;
5226 }
5227 case MCExpr::Unary:
5228 return isSymbolUsedInExpression(
5229 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5230 }
5231
5232 llvm_unreachable("Unknown expr kind!");
5233}
5234
5235bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5236 MCAsmParser &Parser, MCSymbol *&Sym,
5237 const MCExpr *&Value) {
5238 MCAsmLexer &Lexer = Parser.getLexer();
5239
5240 // FIXME: Use better location, we should use proper tokens.
5241 SMLoc EqualLoc = Lexer.getLoc();
5242
5243 if (Parser.parseExpression(Value)) {
5244 Parser.TokError("missing expression");
5245 Parser.eatToEndOfStatement();
5246 return true;
5247 }
5248
5249 // Note: we don't count b as used in "a = b". This is to allow
5250 // a = b
5251 // b = c
5252
5253 if (Lexer.isNot(AsmToken::EndOfStatement))
5254 return Parser.TokError("unexpected token in assignment");
5255
5256 // Eat the end of statement marker.
5257 Parser.Lex();
5258
5259 // Validate that the LHS is allowed to be a variable (either it has not been
5260 // used as a symbol, or it is an absolute symbol).
5261 Sym = Parser.getContext().lookupSymbol(Name);
5262 if (Sym) {
5263 // Diagnose assignment to a label.
5264 //
5265 // FIXME: Diagnostics. Note the location of the definition as a label.
5266 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5267 if (isSymbolUsedInExpression(Sym, Value))
5268 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005269 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5270 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005271 ; // Allow redefinitions of undefined symbols only used in directives.
5272 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5273 ; // Allow redefinitions of variables that haven't yet been used.
5274 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5275 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5276 else if (!Sym->isVariable())
5277 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5278 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5279 return Parser.Error(EqualLoc,
5280 "invalid reassignment of non-absolute variable '" +
5281 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005282 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005283 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005284 return false;
5285 } else
5286 Sym = Parser.getContext().getOrCreateSymbol(Name);
5287
5288 Sym->setRedefinable(allow_redef);
5289
5290 return false;
5291}
5292
5293} // namespace MCParserUtils
5294} // namespace llvm
5295
Daniel Dunbar01e36072010-07-17 02:26:10 +00005296/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005297MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5298 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005299 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005300}