blob: 700461576cbbfe386a0ca37071d8105467ce8d8e [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
Tim Northover6b3169b2016-04-11 19:50:46 +0000149 /// \brief List of forward directional labels for diagnosis at the end.
150 SmallVector<std::pair<SMLoc, MCSymbol *>, 4> DirectionalLabels;
151
Daniel Dunbar828984f2010-07-18 18:38:02 +0000152 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000153 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000154
Toma Tabacu217116e2015-04-27 10:50:29 +0000155 /// \brief Keeps track of how many .macro's have been instantiated.
156 unsigned NumOfMacroInstantiations;
157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000165 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000171 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000186 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
Craig Topper59be68f2014-03-08 07:14:16 +0000188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
Toma Tabacu11e14a92015-04-21 11:50:52 +0000195 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
196 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
197 }
198
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000199public:
200 /// @name MCAsmParser Interface
201 /// {
202
Craig Topper59be68f2014-03-08 07:14:16 +0000203 SourceMgr &getSourceManager() override { return SrcMgr; }
204 MCAsmLexer &getLexer() override { return Lexer; }
205 MCContext &getContext() override { return Ctx; }
206 MCStreamer &getStreamer() override { return Out; }
207 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000208 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000209 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000210 else
211 return AssemblerDialect;
212 }
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000214 AssemblerDialect = i;
215 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000216
Craig Topper59be68f2014-03-08 07:14:16 +0000217 void Note(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
219 bool Warning(SMLoc L, const Twine &Msg,
220 ArrayRef<SMRange> Ranges = None) override;
221 bool Error(SMLoc L, const Twine &Msg,
222 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000223
Craig Topper59be68f2014-03-08 07:14:16 +0000224 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000225
Craig Topper59be68f2014-03-08 07:14:16 +0000226 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
227 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000228
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000229 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000231 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000232 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000234 const MCInstrInfo *MII, const MCInstPrinter *IP,
235 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000236
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000237 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000238 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
239 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
240 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000241 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
242 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000243 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244
Jim Grosbach4b905842013-09-20 23:08:21 +0000245 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000246 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000247 bool parseIdentifier(StringRef &Res) override;
248 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000249
Craig Topper59be68f2014-03-08 07:14:16 +0000250 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000251 /// }
252
253private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000254
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000255 bool parseStatement(ParseStatementInfo &Info,
256 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000257 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 void eatToEndOfLine();
Craig Topper3c76c522015-09-20 23:35:59 +0000259 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000260
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000262 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000263 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000264 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000265 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000266 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000267
Eli Benderskya313ae62013-01-16 18:56:50 +0000268 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Lookup a previously defined macro.
275 /// \param Name Macro name.
276 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000280 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000288 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000289 ///
290 /// \param M The macro.
291 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
294 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
David Majnemer91fc4c22014-01-29 18:57:46 +0000297 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000298 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000299
300 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000301 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000302
Jim Grosbach4b905842013-09-20 23:08:21 +0000303 void printMacroInstantiations();
304 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000305 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000306 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000307 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000308 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000309
Jim Grosbach4b905842013-09-20 23:08:21 +0000310 /// \brief Enter the specified file. This returns true on failure.
311 bool enterIncludeFile(const std::string &Filename);
312
313 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000314 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000315 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000317 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000318 /// current token is not set; clients should ensure Lex() is called
319 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000320 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000321 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000322 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000323 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000324
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000325 /// \brief Parse up to the end of statement and a return the contents from the
326 /// current token until the end of the statement; the current token on exit
327 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000328 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000329
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000330 /// \brief Parse until the end of a statement or a comma is encountered,
331 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000333
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000335 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000336
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000337 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
338 MCBinaryExpr::Opcode &Kind);
339
Jim Grosbach4b905842013-09-20 23:08:21 +0000340 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
341 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
342 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000343
Jim Grosbach4b905842013-09-20 23:08:21 +0000344 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000345
Eli Bendersky17233942013-01-15 22:59:42 +0000346 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000347 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000348 DK_NO_DIRECTIVE, // Placeholder
349 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000350 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000351 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
352 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000353 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000354 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000355 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000356 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000357 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000358 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
359 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000360 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000361 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
362 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000363 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000364 DK_CV_FILE, DK_CV_LOC, DK_CV_LINETABLE, DK_CV_INLINE_LINETABLE,
David Majnemer408b5e62016-02-05 01:55:49 +0000365 DK_CV_DEF_RANGE, DK_CV_STRINGTABLE, DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000366 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
367 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
368 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
369 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
370 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000371 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000372 DK_MACROS_ON, DK_MACROS_OFF,
373 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000374 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000375 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000376 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000377 };
378
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000380 /// directives parsed by this class.
381 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000382
383 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000385 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000387 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
389 bool parseDirectiveFill(); // ".fill"
390 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000391 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
393 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000394 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000395 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000396
Eli Bendersky17233942013-01-15 22:59:42 +0000397 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000398 bool parseDirectiveFile(SMLoc DirectiveLoc);
399 bool parseDirectiveLine();
400 bool parseDirectiveLoc();
401 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000402
David Majnemer408b5e62016-02-05 01:55:49 +0000403 // ".cv_file", ".cv_loc", ".cv_linetable", "cv_inline_linetable",
404 // ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000405 bool parseDirectiveCVFile();
406 bool parseDirectiveCVLoc();
407 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000408 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000409 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000410 bool parseDirectiveCVStringTable();
411 bool parseDirectiveCVFileChecksums();
412
Eli Bendersky17233942013-01-15 22:59:42 +0000413 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000415 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveCFISections();
417 bool parseDirectiveCFIStartProc();
418 bool parseDirectiveCFIEndProc();
419 bool parseDirectiveCFIDefCfaOffset();
420 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
421 bool parseDirectiveCFIAdjustCfaOffset();
422 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
423 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
424 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
425 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
426 bool parseDirectiveCFIRememberState();
427 bool parseDirectiveCFIRestoreState();
428 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
429 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
430 bool parseDirectiveCFIEscape();
431 bool parseDirectiveCFISignalFrame();
432 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000433
434 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000436 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveEndMacro(StringRef Directive);
438 bool parseDirectiveMacro(SMLoc DirectiveLoc);
439 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000440
Eli Benderskyf483ff92012-12-20 19:05:53 +0000441 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000443 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000445 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000447
Eli Bendersky17233942013-01-15 22:59:42 +0000448 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000449 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000450
451 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000453
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000455 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000457
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000459
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveAbort(); // ".abort"
461 bool parseDirectiveInclude(); // ".include"
462 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000463
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000464 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
465 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000466 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000468 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000470 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
471 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000472 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000473 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
474 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
475 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
476 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000477 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000478
Jim Grosbach4b905842013-09-20 23:08:21 +0000479 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000480 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000481
Rafael Espindola34b9c512012-06-03 23:57:14 +0000482 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000483 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
484 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000485 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000486 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000487 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
488 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
489 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000490
Chad Rosierc7f552c2013-02-12 21:33:51 +0000491 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000492 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000493 size_t Len);
494
495 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000496 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000497
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000498 // "end"
499 bool parseDirectiveEnd(SMLoc DirectiveLoc);
500
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000501 // ".err" or ".error"
502 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000503
Nico Weber404012b2014-07-24 16:26:06 +0000504 // ".warning"
505 bool parseDirectiveWarning(SMLoc DirectiveLoc);
506
Eli Bendersky17233942013-01-15 22:59:42 +0000507 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000508};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000509}
Daniel Dunbar86033402010-07-12 17:54:38 +0000510
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000511namespace llvm {
512
513extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000514extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000515extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000516
517}
518
Chris Lattnerc35681b2010-01-19 19:46:13 +0000519enum { DEFAULT_ADDRSPACE = 0 };
520
David Blaikie9f380a32015-03-16 18:06:57 +0000521AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
522 const MCAsmInfo &MAI)
523 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
524 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000525 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000526 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000527 // Save the old handler.
528 SavedDiagHandler = SrcMgr.getDiagHandler();
529 SavedDiagContext = SrcMgr.getDiagContext();
530 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000531 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000532 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000533
Daniel Dunbarc5011082010-07-12 18:12:02 +0000534 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000535 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
536 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000537 PlatformParser.reset(createCOFFAsmParser());
538 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000539 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000540 PlatformParser.reset(createDarwinAsmParser());
541 IsDarwin = true;
542 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000543 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000544 PlatformParser.reset(createELFAsmParser());
545 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000546 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000547
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000548 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000549 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000550
551 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000552}
553
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000554AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000555 assert((HadError || ActiveMacros.empty()) &&
556 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000557}
558
Jim Grosbach4b905842013-09-20 23:08:21 +0000559void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000560 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000561 for (std::vector<MacroInstantiation *>::const_reverse_iterator
562 it = ActiveMacros.rbegin(),
563 ie = ActiveMacros.rend();
564 it != ie; ++it)
565 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000566 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000567}
568
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000569void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
570 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
571 printMacroInstantiations();
572}
573
Chris Lattnera3a06812011-10-16 04:47:35 +0000574bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000575 if(getTargetParser().getTargetOptions().MCNoWarn)
576 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000577 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000578 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000579 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
580 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000581 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000582}
583
Chris Lattnera3a06812011-10-16 04:47:35 +0000584bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000585 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000586 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
587 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000588 return true;
589}
590
Jim Grosbach4b905842013-09-20 23:08:21 +0000591bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000592 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000593 unsigned NewBuf =
594 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
595 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000596 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000597
Sean Callanan7a77eae2010-01-21 00:19:58 +0000598 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000599 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 return false;
601}
Daniel Dunbar43235712010-07-18 18:54:11 +0000602
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000603/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000604/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000605/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000606bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000607 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000608 unsigned NewBuf =
609 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
610 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000611 return true;
612
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000613 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000614 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000615 return false;
616}
617
Alp Tokera55b95b2014-07-06 10:33:31 +0000618void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
619 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000620 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
621 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000622}
623
Sean Callanan7a77eae2010-01-21 00:19:58 +0000624const AsmToken &AsmParser::Lex() {
625 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000626
Sean Callanan7a77eae2010-01-21 00:19:58 +0000627 if (tok->is(AsmToken::Eof)) {
628 // If this is the end of an included file, pop the parent file off the
629 // include stack.
630 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
631 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000632 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000633 tok = &Lexer.Lex();
634 }
635 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000636
Sean Callanan7a77eae2010-01-21 00:19:58 +0000637 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000638 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000639
Sean Callanan7a77eae2010-01-21 00:19:58 +0000640 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000641}
642
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000643bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000644 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000645 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000646 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000647
Chris Lattner36e02122009-06-21 20:54:55 +0000648 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000649 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000650
651 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000652 AsmCond StartingCondState = TheCondState;
653
Kevin Enderby6469fc22011-11-01 22:27:22 +0000654 // If we are generating dwarf for assembly source files save the initial text
655 // section and generate a .file directive.
656 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000657 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000658 if (!Sec->getBeginSymbol()) {
659 MCSymbol *SectionStartSym = getContext().createTempSymbol();
660 getStreamer().EmitLabel(SectionStartSym);
661 Sec->setBeginSymbol(SectionStartSym);
662 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000663 bool InsertResult = getContext().addGenDwarfSection(Sec);
664 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000665 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000666 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
667 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000668 }
669
Chris Lattner73f36112009-07-02 21:53:43 +0000670 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000671 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000672 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000673 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000674 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000675
Daniel Dunbar43325c42010-09-09 22:42:56 +0000676 // We had an error, validate that one was emitted and recover by skipping to
677 // the next line.
678 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000679 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000680 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000681
682 if (TheCondState.TheCond != StartingCondState.TheCond ||
683 TheCondState.Ignore != StartingCondState.Ignore)
684 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000685
686 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000687 const auto &LineTables = getContext().getMCDwarfLineTables();
688 if (!LineTables.empty()) {
689 unsigned Index = 0;
690 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
691 if (File.Name.empty() && Index != 0)
692 TokError("unassigned file number: " + Twine(Index) +
693 " for .file directives");
694 ++Index;
695 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000696 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000697
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000698 // Check to see that all assembler local symbols were actually defined.
699 // Targets that don't do subsections via symbols may not want this, though,
700 // so conservatively exclude them. Only do this if we're finalizing, though,
701 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000702 if (!NoFinalize) {
703 if (MAI.hasSubsectionsViaSymbols()) {
704 for (const auto &TableEntry : getContext().getSymbols()) {
705 MCSymbol *Sym = TableEntry.getValue();
706 // Variable symbols may not be marked as defined, so check those
707 // explicitly. If we know it's a variable, we have a definition for
708 // the purposes of this check.
709 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
710 // FIXME: We would really like to refer back to where the symbol was
711 // first referenced for a source location. We need to add something
712 // to track that. Currently, we just point to the end of the file.
713 HadError |=
714 Error(getLexer().getLoc(), "assembler local symbol '" +
715 Sym->getName() + "' not defined");
716 }
717 }
718
719 // Temporary symbols like the ones for directional jumps don't go in the
720 // symbol table. They also need to be diagnosed in all (final) cases.
721 for (std::pair<SMLoc, MCSymbol *> &LocSym : DirectionalLabels) {
722 if (LocSym.second->isUndefined())
723 HadError |= Error(LocSym.first, "directional label undefined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000724 }
725 }
726
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000727 // Finalize the output stream if there are no errors and if the client wants
728 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000729 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000730 Out.Finish();
731
Oliver Stannard07b43d32015-11-17 09:58:07 +0000732 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000733}
734
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000735void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000736 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000737 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000738 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000739 }
740}
741
Jim Grosbach4b905842013-09-20 23:08:21 +0000742/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000743void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000744 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000745 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000746
Chris Lattnere5074c42009-06-22 01:29:09 +0000747 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000748 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000749 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000750}
751
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000752StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000753 const char *Start = getTok().getLoc().getPointer();
754
Jim Grosbach4b905842013-09-20 23:08:21 +0000755 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000756 Lex();
757
758 const char *End = getTok().getLoc().getPointer();
759 return StringRef(Start, End - Start);
760}
Chris Lattner78db3622009-06-22 05:51:26 +0000761
Jim Grosbach4b905842013-09-20 23:08:21 +0000762StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000763 const char *Start = getTok().getLoc().getPointer();
764
765 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000766 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000767 Lex();
768
769 const char *End = getTok().getLoc().getPointer();
770 return StringRef(Start, End - Start);
771}
772
Jim Grosbach4b905842013-09-20 23:08:21 +0000773/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000774/// NOTE: This assumes the leading '(' has already been consumed.
775///
776/// parenexpr ::= expr)
777///
Jim Grosbach4b905842013-09-20 23:08:21 +0000778bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
779 if (parseExpression(Res))
780 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000781 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000782 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000783 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000784 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000785 return false;
786}
Chris Lattner78db3622009-06-22 05:51:26 +0000787
Jim Grosbach4b905842013-09-20 23:08:21 +0000788/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000789/// NOTE: This assumes the leading '[' has already been consumed.
790///
791/// bracketexpr ::= expr]
792///
Jim Grosbach4b905842013-09-20 23:08:21 +0000793bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
794 if (parseExpression(Res))
795 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000796 if (Lexer.isNot(AsmToken::RBrac))
797 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000798 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000799 Lex();
800 return false;
801}
802
Jim Grosbach4b905842013-09-20 23:08:21 +0000803/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000804/// primaryexpr ::= (parenexpr
805/// primaryexpr ::= symbol
806/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000807/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000808/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000809bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000810 SMLoc FirstTokenLoc = getLexer().getLoc();
811 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
812 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000813 default:
814 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000815 // If we have an error assume that we've already handled it.
816 case AsmToken::Error:
817 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000818 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000819 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000820 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000821 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000822 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000823 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000824 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000825 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000826 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000827 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000828 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000829 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000830 if (FirstTokenKind == AsmToken::Dollar) {
831 if (Lexer.getMAI().getDollarIsPC()) {
832 // This is a '$' reference, which references the current PC. Emit a
833 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000834 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000835 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000836 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000837 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000838 EndLoc = FirstTokenLoc;
839 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000840 }
841 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000842 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000843 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000844 // Parse symbol variant
845 std::pair<StringRef, StringRef> Split;
846 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000847 if (FirstTokenKind == AsmToken::String) {
848 if (Lexer.is(AsmToken::At)) {
849 Lexer.Lex(); // eat @
850 SMLoc AtLoc = getLexer().getLoc();
851 StringRef VName;
852 if (parseIdentifier(VName))
853 return Error(AtLoc, "expected symbol variant after '@'");
854
855 Split = std::make_pair(Identifier, VName);
856 }
857 } else {
858 Split = Identifier.split('@');
859 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000860 } else if (Lexer.is(AsmToken::LParen)) {
861 Lexer.Lex(); // eat (
862 StringRef VName;
863 parseIdentifier(VName);
864 if (Lexer.isNot(AsmToken::RParen)) {
865 return Error(Lexer.getTok().getLoc(),
866 "unexpected token in variant, expected ')'");
867 }
868 Lexer.Lex(); // eat )
869 Split = std::make_pair(Identifier, VName);
870 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000871
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000872 EndLoc = SMLoc::getFromPointer(Identifier.end());
873
Daniel Dunbard20cda02009-10-16 01:34:54 +0000874 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000875 StringRef SymbolName = Identifier;
876 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000877
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000878 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000879 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000880 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000881 if (Variant != MCSymbolRefExpr::VK_Invalid) {
882 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000883 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000884 Variant = MCSymbolRefExpr::VK_None;
885 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000886 return Error(SMLoc::getFromPointer(Split.second.begin()),
887 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000888 }
889 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000890
Jim Grosbach6f482002015-05-18 18:43:14 +0000891 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000892
Daniel Dunbard20cda02009-10-16 01:34:54 +0000893 // If this is an absolute variable reference, substitute it now to preserve
894 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000895 if (Sym->isVariable() &&
896 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000897 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000898 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000899
Vedant Kumar86dbd922015-08-31 17:44:53 +0000900 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000901 return false;
902 }
903
904 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000905 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000906 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000907 }
David Woodhousef42a6662014-02-01 16:20:54 +0000908 case AsmToken::BigNum:
909 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000910 case AsmToken::Integer: {
911 SMLoc Loc = getTok().getLoc();
912 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000913 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000914 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000915 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000916 // Look for 'b' or 'f' following an Integer as a directional label
917 if (Lexer.getKind() == AsmToken::Identifier) {
918 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000919 // Lookup the symbol variant if used.
920 std::pair<StringRef, StringRef> Split = IDVal.split('@');
921 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
922 if (Split.first.size() != IDVal.size()) {
923 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000924 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000925 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000926 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000927 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 if (IDVal == "f" || IDVal == "b") {
929 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000930 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000931 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000932 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +0000933 return Error(Loc, "directional label undefined");
934 DirectionalLabels.push_back(std::make_pair(Loc, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000935 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000936 Lex(); // Eat identifier.
937 }
938 }
Chris Lattner78db3622009-06-22 05:51:26 +0000939 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000940 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000941 case AsmToken::Real: {
942 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000943 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000944 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000945 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000946 Lex(); // Eat token.
947 return false;
948 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000949 case AsmToken::Dot: {
950 // This is a '.' reference, which references the current PC. Emit a
951 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000952 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000953 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000954 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000955 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000956 Lex(); // Eat identifier.
957 return false;
958 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000959 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000960 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000961 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000962 case AsmToken::LBrac:
963 if (!PlatformParser->HasBracketExpressions())
964 return TokError("brackets expression not supported on this target");
965 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000966 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000967 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000968 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000969 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000970 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000971 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000972 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000973 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000974 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000975 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000976 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000977 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000978 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000979 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000980 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000981 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000982 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000983 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000984 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000985 }
986}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000987
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000988bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000989 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000990 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000991}
992
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000994AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000996 // Ask the target implementation about this expression first.
997 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
998 if (NewE)
999 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001000 // Recurse over the given expression, rebuilding it to apply the given variant
1001 // if there is exactly one symbol.
1002 switch (E->getKind()) {
1003 case MCExpr::Target:
1004 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001005 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001006
1007 case MCExpr::SymbolRef: {
1008 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1009
1010 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001011 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1012 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013 return E;
1014 }
1015
Jim Grosbach13760bd2015-05-30 01:25:56 +00001016 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001017 }
1018
1019 case MCExpr::Unary: {
1020 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001023 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001024 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 }
1026
1027 case MCExpr::Binary: {
1028 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001029 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1030 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031
1032 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001033 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034
Jim Grosbach4b905842013-09-20 23:08:21 +00001035 if (!LHS)
1036 LHS = BE->getLHS();
1037 if (!RHS)
1038 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001039
Jim Grosbach13760bd2015-05-30 01:25:56 +00001040 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 }
1042 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001043
Craig Toppera2886c22012-02-07 05:05:23 +00001044 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001045}
1046
Jim Grosbach4b905842013-09-20 23:08:21 +00001047/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001048///
Jim Grosbachbd164242011-08-20 16:24:13 +00001049/// expr ::= expr &&,|| expr -> lowest.
1050/// expr ::= expr |,^,&,! expr
1051/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1052/// expr ::= expr <<,>> expr
1053/// expr ::= expr +,- expr
1054/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001055/// expr ::= primaryexpr
1056///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001058 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001059 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001060 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001061 return true;
1062
Daniel Dunbar55f16672010-09-17 02:47:07 +00001063 // As a special case, we support 'a op b @ modifier' by rewriting the
1064 // expression to include the modifier. This is inefficient, but in general we
1065 // expect users to use 'a@modifier op b'.
1066 if (Lexer.getKind() == AsmToken::At) {
1067 Lex();
1068
1069 if (Lexer.isNot(AsmToken::Identifier))
1070 return TokError("unexpected symbol modifier following '@'");
1071
1072 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001073 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001074 if (Variant == MCSymbolRefExpr::VK_Invalid)
1075 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1076
Jim Grosbach4b905842013-09-20 23:08:21 +00001077 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001078 if (!ModifiedRes) {
1079 return TokError("invalid modifier '" + getTok().getIdentifier() +
1080 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001081 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001082
Daniel Dunbar55f16672010-09-17 02:47:07 +00001083 Res = ModifiedRes;
1084 Lex();
1085 }
1086
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001087 // Try to constant fold it up front, if possible.
1088 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001089 if (Res->evaluateAsAbsolute(Value))
1090 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001091
1092 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001093}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001094
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001095bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001096 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001098}
1099
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001100bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1101 SMLoc &EndLoc) {
1102 if (parseParenExpr(Res, EndLoc))
1103 return true;
1104
1105 for (; ParenDepth > 0; --ParenDepth) {
1106 if (parseBinOpRHS(1, Res, EndLoc))
1107 return true;
1108
1109 // We don't Lex() the last RParen.
1110 // This is the same behavior as parseParenExpression().
1111 if (ParenDepth - 1 > 0) {
1112 if (Lexer.isNot(AsmToken::RParen))
1113 return TokError("expected ')' in parentheses expression");
1114 EndLoc = Lexer.getTok().getEndLoc();
1115 Lex();
1116 }
1117 }
1118 return false;
1119}
1120
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001121bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001122 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001123
Daniel Dunbar75630b32009-06-30 02:10:03 +00001124 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001125 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001126 return true;
1127
Jim Grosbach13760bd2015-05-30 01:25:56 +00001128 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001129 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001130
1131 return false;
1132}
1133
David Majnemer0993e0b2015-10-26 03:15:34 +00001134static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1135 MCBinaryExpr::Opcode &Kind,
1136 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001137 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001138 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001139 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001140
Jim Grosbach4b905842013-09-20 23:08:21 +00001141 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001142 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001143 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001145 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001146 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001147 return 1;
1148
Jim Grosbach4b905842013-09-20 23:08:21 +00001149 // Low Precedence: |, &, ^
1150 //
1151 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001152 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001153 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001154 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001155 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001156 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001157 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001158 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001159 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001160 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001161
Jim Grosbach4b905842013-09-20 23:08:21 +00001162 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001163 case AsmToken::EqualEqual:
1164 Kind = MCBinaryExpr::EQ;
1165 return 3;
1166 case AsmToken::ExclaimEqual:
1167 case AsmToken::LessGreater:
1168 Kind = MCBinaryExpr::NE;
1169 return 3;
1170 case AsmToken::Less:
1171 Kind = MCBinaryExpr::LT;
1172 return 3;
1173 case AsmToken::LessEqual:
1174 Kind = MCBinaryExpr::LTE;
1175 return 3;
1176 case AsmToken::Greater:
1177 Kind = MCBinaryExpr::GT;
1178 return 3;
1179 case AsmToken::GreaterEqual:
1180 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001181 return 3;
1182
Jim Grosbach4b905842013-09-20 23:08:21 +00001183 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001184 case AsmToken::LessLess:
1185 Kind = MCBinaryExpr::Shl;
1186 return 4;
1187 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001188 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001189 return 4;
1190
Jim Grosbach4b905842013-09-20 23:08:21 +00001191 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001192 case AsmToken::Plus:
1193 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001194 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001195 case AsmToken::Minus:
1196 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001197 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001198
Jim Grosbach4b905842013-09-20 23:08:21 +00001199 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001200 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001201 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001202 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001203 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001204 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001205 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001206 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001207 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001208 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001209 }
1210}
1211
David Majnemer0993e0b2015-10-26 03:15:34 +00001212static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1213 MCBinaryExpr::Opcode &Kind,
1214 bool ShouldUseLogicalShr) {
1215 switch (K) {
1216 default:
1217 return 0; // not a binop.
1218
1219 // Lowest Precedence: &&, ||
1220 case AsmToken::AmpAmp:
1221 Kind = MCBinaryExpr::LAnd;
1222 return 2;
1223 case AsmToken::PipePipe:
1224 Kind = MCBinaryExpr::LOr;
1225 return 1;
1226
1227 // Low Precedence: ==, !=, <>, <, <=, >, >=
1228 case AsmToken::EqualEqual:
1229 Kind = MCBinaryExpr::EQ;
1230 return 3;
1231 case AsmToken::ExclaimEqual:
1232 case AsmToken::LessGreater:
1233 Kind = MCBinaryExpr::NE;
1234 return 3;
1235 case AsmToken::Less:
1236 Kind = MCBinaryExpr::LT;
1237 return 3;
1238 case AsmToken::LessEqual:
1239 Kind = MCBinaryExpr::LTE;
1240 return 3;
1241 case AsmToken::Greater:
1242 Kind = MCBinaryExpr::GT;
1243 return 3;
1244 case AsmToken::GreaterEqual:
1245 Kind = MCBinaryExpr::GTE;
1246 return 3;
1247
1248 // Low Intermediate Precedence: +, -
1249 case AsmToken::Plus:
1250 Kind = MCBinaryExpr::Add;
1251 return 4;
1252 case AsmToken::Minus:
1253 Kind = MCBinaryExpr::Sub;
1254 return 4;
1255
1256 // High Intermediate Precedence: |, &, ^
1257 //
1258 // FIXME: gas seems to support '!' as an infix operator?
1259 case AsmToken::Pipe:
1260 Kind = MCBinaryExpr::Or;
1261 return 5;
1262 case AsmToken::Caret:
1263 Kind = MCBinaryExpr::Xor;
1264 return 5;
1265 case AsmToken::Amp:
1266 Kind = MCBinaryExpr::And;
1267 return 5;
1268
1269 // Highest Precedence: *, /, %, <<, >>
1270 case AsmToken::Star:
1271 Kind = MCBinaryExpr::Mul;
1272 return 6;
1273 case AsmToken::Slash:
1274 Kind = MCBinaryExpr::Div;
1275 return 6;
1276 case AsmToken::Percent:
1277 Kind = MCBinaryExpr::Mod;
1278 return 6;
1279 case AsmToken::LessLess:
1280 Kind = MCBinaryExpr::Shl;
1281 return 6;
1282 case AsmToken::GreaterGreater:
1283 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1284 return 6;
1285 }
1286}
1287
1288unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1289 MCBinaryExpr::Opcode &Kind) {
1290 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1291 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1292 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1293}
1294
Jim Grosbach4b905842013-09-20 23:08:21 +00001295/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001296/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001297bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001298 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001299 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001300 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001301 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001302
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001303 // If the next token is lower precedence than we are allowed to eat, return
1304 // successfully with what we ate already.
1305 if (TokPrec < Precedence)
1306 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001307
Sean Callanan686ed8d2010-01-19 20:22:31 +00001308 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001309
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001310 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001311 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001312 if (parsePrimaryExpr(RHS, EndLoc))
1313 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001314
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001315 // If BinOp binds less tightly with RHS than the operator after RHS, let
1316 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001317 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001318 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001319 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1320 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001321
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001322 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001323 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001324 }
1325}
1326
Chris Lattner36e02122009-06-21 20:54:55 +00001327/// ParseStatement:
1328/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001329/// ::= Label* Directive ...Operands... EndOfStatement
1330/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001331bool AsmParser::parseStatement(ParseStatementInfo &Info,
1332 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001333 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001334 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001335 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001336 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001337 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001338
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001339 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001340 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001341 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001342 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001343 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001344 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001345 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001346 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001347
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001348 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001349 if (Lexer.is(AsmToken::Integer)) {
1350 LocalLabelVal = getTok().getIntVal();
1351 if (LocalLabelVal < 0) {
1352 if (!TheCondState.Ignore)
1353 return TokError("unexpected token at start of statement");
1354 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001355 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001356 IDVal = getTok().getString();
1357 Lex(); // Consume the integer token to be used as an identifier token.
1358 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001359 if (!TheCondState.Ignore)
1360 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001361 }
1362 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001363 } else if (Lexer.is(AsmToken::Dot)) {
1364 // Treat '.' as a valid identifier in this context.
1365 Lex();
1366 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001367 } else if (Lexer.is(AsmToken::LCurly)) {
1368 // Treat '{' as a valid identifier in this context.
1369 Lex();
1370 IDVal = "{";
1371
1372 } else if (Lexer.is(AsmToken::RCurly)) {
1373 // Treat '}' as a valid identifier in this context.
1374 Lex();
1375 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001376 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001377 if (!TheCondState.Ignore)
1378 return TokError("unexpected token at start of statement");
1379 IDVal = "";
1380 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001381
Chris Lattner926885c2010-04-17 18:14:27 +00001382 // Handle conditional assembly here before checking for skipping. We
1383 // have to do this so that .endif isn't skipped in a ".if 0" block for
1384 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001385 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001386 DirectiveKindMap.find(IDVal);
1387 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1388 ? DK_NO_DIRECTIVE
1389 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001390 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001391 default:
1392 break;
1393 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001394 case DK_IFEQ:
1395 case DK_IFGE:
1396 case DK_IFGT:
1397 case DK_IFLE:
1398 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001399 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001400 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001401 case DK_IFB:
1402 return parseDirectiveIfb(IDLoc, true);
1403 case DK_IFNB:
1404 return parseDirectiveIfb(IDLoc, false);
1405 case DK_IFC:
1406 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001407 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001408 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001409 case DK_IFNC:
1410 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001411 case DK_IFNES:
1412 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001413 case DK_IFDEF:
1414 return parseDirectiveIfdef(IDLoc, true);
1415 case DK_IFNDEF:
1416 case DK_IFNOTDEF:
1417 return parseDirectiveIfdef(IDLoc, false);
1418 case DK_ELSEIF:
1419 return parseDirectiveElseIf(IDLoc);
1420 case DK_ELSE:
1421 return parseDirectiveElse(IDLoc);
1422 case DK_ENDIF:
1423 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001424 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001425
Eli Bendersky88024712013-01-16 19:32:36 +00001426 // Ignore the statement if in the middle of inactive conditional
1427 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001428 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001429 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001430 return false;
1431 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001432
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001433 // FIXME: Recurse on local labels?
1434
1435 // See what kind of statement we have.
1436 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001437 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001438 if (!getTargetParser().isLabel(ID))
1439 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001440 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001441
Chris Lattner36e02122009-06-21 20:54:55 +00001442 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001443 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001444
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001445 // Diagnose attempt to use '.' as a label.
1446 if (IDVal == ".")
1447 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1448
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001449 // Diagnose attempt to use a variable as a label.
1450 //
1451 // FIXME: Diagnostics. Note the location of the definition as a label.
1452 // FIXME: This doesn't diagnose assignment to a symbol which has been
1453 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001454 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001455 if (LocalLabelVal == -1) {
1456 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001457 StringRef RewrittenLabel =
1458 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1459 assert(RewrittenLabel.size() &&
1460 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001461 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1462 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001463 IDVal = RewrittenLabel;
1464 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001465 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001466 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001467 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001468
1469 Sym->redefineIfPossible();
1470
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001471 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001472 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001473
Daniel Dunbare73b2672009-08-26 22:13:22 +00001474 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001475 if (!ParsingInlineAsm)
1476 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001477
Kevin Enderbye7739d42011-12-09 18:09:40 +00001478 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001479 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001480 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001481 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1482 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001483
Tim Northover1744d0a2013-10-25 12:49:50 +00001484 getTargetParser().onLabelParsed(Sym);
1485
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001486 // Consume any end of statement token, if present, to avoid spurious
1487 // AddBlankLine calls().
1488 if (Lexer.is(AsmToken::EndOfStatement)) {
1489 Lex();
1490 if (Lexer.is(AsmToken::Eof))
1491 return false;
1492 }
1493
Eli Friedman0f4871d2012-10-22 23:58:19 +00001494 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001495 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001496
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001497 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001498 if (!getTargetParser().equalIsAsmAssignment())
1499 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001500 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001501 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001502
Jim Grosbach4b905842013-09-20 23:08:21 +00001503 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001504
1505 default: // Normal instruction or directive.
1506 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001507 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001508
1509 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001510 if (areMacrosEnabled())
1511 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1512 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001513 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001514
Michael J. Spencer530ce852010-10-09 11:00:50 +00001515 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001516
Eli Bendersky17233942013-01-15 22:59:42 +00001517 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001518 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001519 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001520 //
Eli Bendersky17233942013-01-15 22:59:42 +00001521 // 1. The target-specific assembly parser. Some directives are target
1522 // specific or may potentially behave differently on certain targets.
1523 // 2. Asm parser extensions. For example, platform-specific parsers
1524 // (like the ELF parser) register themselves as extensions.
1525 // 3. The generic directive parser implemented by this class. These are
1526 // all the directives that behave in a target and platform independent
1527 // manner, or at least have a default behavior that's shared between
1528 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001529
Eli Bendersky17233942013-01-15 22:59:42 +00001530 // First query the target-specific parser. It will return 'true' if it
1531 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001532 if (!getTargetParser().ParseDirective(ID))
1533 return false;
1534
Alp Tokercb402912014-01-24 17:20:08 +00001535 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001536 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001537 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1538 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001539 if (Handler.first)
1540 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1541
1542 // Finally, if no one else is interested in this directive, it must be
1543 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001544 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 default:
1546 break;
1547 case DK_SET:
1548 case DK_EQU:
1549 return parseDirectiveSet(IDVal, true);
1550 case DK_EQUIV:
1551 return parseDirectiveSet(IDVal, false);
1552 case DK_ASCII:
1553 return parseDirectiveAscii(IDVal, false);
1554 case DK_ASCIZ:
1555 case DK_STRING:
1556 return parseDirectiveAscii(IDVal, true);
1557 case DK_BYTE:
1558 return parseDirectiveValue(1);
1559 case DK_SHORT:
1560 case DK_VALUE:
1561 case DK_2BYTE:
1562 return parseDirectiveValue(2);
1563 case DK_LONG:
1564 case DK_INT:
1565 case DK_4BYTE:
1566 return parseDirectiveValue(4);
1567 case DK_QUAD:
1568 case DK_8BYTE:
1569 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001570 case DK_OCTA:
1571 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001572 case DK_SINGLE:
1573 case DK_FLOAT:
1574 return parseDirectiveRealValue(APFloat::IEEEsingle);
1575 case DK_DOUBLE:
1576 return parseDirectiveRealValue(APFloat::IEEEdouble);
1577 case DK_ALIGN: {
1578 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1579 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1580 }
1581 case DK_ALIGN32: {
1582 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1583 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1584 }
1585 case DK_BALIGN:
1586 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1587 case DK_BALIGNW:
1588 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1589 case DK_BALIGNL:
1590 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1591 case DK_P2ALIGN:
1592 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1593 case DK_P2ALIGNW:
1594 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1595 case DK_P2ALIGNL:
1596 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1597 case DK_ORG:
1598 return parseDirectiveOrg();
1599 case DK_FILL:
1600 return parseDirectiveFill();
1601 case DK_ZERO:
1602 return parseDirectiveZero();
1603 case DK_EXTERN:
1604 eatToEndOfStatement(); // .extern is the default, ignore it.
1605 return false;
1606 case DK_GLOBL:
1607 case DK_GLOBAL:
1608 return parseDirectiveSymbolAttribute(MCSA_Global);
1609 case DK_LAZY_REFERENCE:
1610 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1611 case DK_NO_DEAD_STRIP:
1612 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1613 case DK_SYMBOL_RESOLVER:
1614 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1615 case DK_PRIVATE_EXTERN:
1616 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1617 case DK_REFERENCE:
1618 return parseDirectiveSymbolAttribute(MCSA_Reference);
1619 case DK_WEAK_DEFINITION:
1620 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1621 case DK_WEAK_REFERENCE:
1622 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1623 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1624 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1625 case DK_COMM:
1626 case DK_COMMON:
1627 return parseDirectiveComm(/*IsLocal=*/false);
1628 case DK_LCOMM:
1629 return parseDirectiveComm(/*IsLocal=*/true);
1630 case DK_ABORT:
1631 return parseDirectiveAbort();
1632 case DK_INCLUDE:
1633 return parseDirectiveInclude();
1634 case DK_INCBIN:
1635 return parseDirectiveIncbin();
1636 case DK_CODE16:
1637 case DK_CODE16GCC:
1638 return TokError(Twine(IDVal) + " not supported yet");
1639 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001640 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001641 case DK_IRP:
1642 return parseDirectiveIrp(IDLoc);
1643 case DK_IRPC:
1644 return parseDirectiveIrpc(IDLoc);
1645 case DK_ENDR:
1646 return parseDirectiveEndr(IDLoc);
1647 case DK_BUNDLE_ALIGN_MODE:
1648 return parseDirectiveBundleAlignMode();
1649 case DK_BUNDLE_LOCK:
1650 return parseDirectiveBundleLock();
1651 case DK_BUNDLE_UNLOCK:
1652 return parseDirectiveBundleUnlock();
1653 case DK_SLEB128:
1654 return parseDirectiveLEB128(true);
1655 case DK_ULEB128:
1656 return parseDirectiveLEB128(false);
1657 case DK_SPACE:
1658 case DK_SKIP:
1659 return parseDirectiveSpace(IDVal);
1660 case DK_FILE:
1661 return parseDirectiveFile(IDLoc);
1662 case DK_LINE:
1663 return parseDirectiveLine();
1664 case DK_LOC:
1665 return parseDirectiveLoc();
1666 case DK_STABS:
1667 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001668 case DK_CV_FILE:
1669 return parseDirectiveCVFile();
1670 case DK_CV_LOC:
1671 return parseDirectiveCVLoc();
1672 case DK_CV_LINETABLE:
1673 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001674 case DK_CV_INLINE_LINETABLE:
1675 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001676 case DK_CV_DEF_RANGE:
1677 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001678 case DK_CV_STRINGTABLE:
1679 return parseDirectiveCVStringTable();
1680 case DK_CV_FILECHECKSUMS:
1681 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001682 case DK_CFI_SECTIONS:
1683 return parseDirectiveCFISections();
1684 case DK_CFI_STARTPROC:
1685 return parseDirectiveCFIStartProc();
1686 case DK_CFI_ENDPROC:
1687 return parseDirectiveCFIEndProc();
1688 case DK_CFI_DEF_CFA:
1689 return parseDirectiveCFIDefCfa(IDLoc);
1690 case DK_CFI_DEF_CFA_OFFSET:
1691 return parseDirectiveCFIDefCfaOffset();
1692 case DK_CFI_ADJUST_CFA_OFFSET:
1693 return parseDirectiveCFIAdjustCfaOffset();
1694 case DK_CFI_DEF_CFA_REGISTER:
1695 return parseDirectiveCFIDefCfaRegister(IDLoc);
1696 case DK_CFI_OFFSET:
1697 return parseDirectiveCFIOffset(IDLoc);
1698 case DK_CFI_REL_OFFSET:
1699 return parseDirectiveCFIRelOffset(IDLoc);
1700 case DK_CFI_PERSONALITY:
1701 return parseDirectiveCFIPersonalityOrLsda(true);
1702 case DK_CFI_LSDA:
1703 return parseDirectiveCFIPersonalityOrLsda(false);
1704 case DK_CFI_REMEMBER_STATE:
1705 return parseDirectiveCFIRememberState();
1706 case DK_CFI_RESTORE_STATE:
1707 return parseDirectiveCFIRestoreState();
1708 case DK_CFI_SAME_VALUE:
1709 return parseDirectiveCFISameValue(IDLoc);
1710 case DK_CFI_RESTORE:
1711 return parseDirectiveCFIRestore(IDLoc);
1712 case DK_CFI_ESCAPE:
1713 return parseDirectiveCFIEscape();
1714 case DK_CFI_SIGNAL_FRAME:
1715 return parseDirectiveCFISignalFrame();
1716 case DK_CFI_UNDEFINED:
1717 return parseDirectiveCFIUndefined(IDLoc);
1718 case DK_CFI_REGISTER:
1719 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001720 case DK_CFI_WINDOW_SAVE:
1721 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 case DK_MACROS_ON:
1723 case DK_MACROS_OFF:
1724 return parseDirectiveMacrosOnOff(IDVal);
1725 case DK_MACRO:
1726 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001727 case DK_EXITM:
1728 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001729 case DK_ENDM:
1730 case DK_ENDMACRO:
1731 return parseDirectiveEndMacro(IDVal);
1732 case DK_PURGEM:
1733 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001734 case DK_END:
1735 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001736 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001737 return parseDirectiveError(IDLoc, false);
1738 case DK_ERROR:
1739 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001740 case DK_WARNING:
1741 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001742 case DK_RELOC:
1743 return parseDirectiveReloc(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001744 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001745
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001746 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001747 }
Chris Lattner36e02122009-06-21 20:54:55 +00001748
Chad Rosierc7f552c2013-02-12 21:33:51 +00001749 // __asm _emit or __asm __emit
1750 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1751 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001752 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001753
1754 // __asm align
1755 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001756 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001757
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001758 if (ParsingInlineAsm && (IDVal == "even"))
1759 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001760 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001761
Chris Lattner7cbfa442010-05-19 23:34:33 +00001762 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001763 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001764 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001765 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001766 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001767 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001768
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001769 // Dump the parsed representation, if requested.
1770 if (getShowParsedOperands()) {
1771 SmallString<256> Str;
1772 raw_svector_ostream OS(Str);
1773 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001774 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001775 if (i != 0)
1776 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001777 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001778 }
1779 OS << "]";
1780
Jim Grosbach4b905842013-09-20 23:08:21 +00001781 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001782 }
1783
Oliver Stannard8b273082014-06-19 15:52:37 +00001784 // If we are generating dwarf for the current section then generate a .loc
1785 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001786 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001787 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001788 getStreamer().getCurrentSection().first)) {
1789 unsigned Line;
1790 if (ActiveMacros.empty())
1791 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1792 else
Frederic Riss16238d92015-06-25 21:57:33 +00001793 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1794 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001795
Eli Bendersky88024712013-01-16 19:32:36 +00001796 // If we previously parsed a cpp hash file line comment then make sure the
1797 // current Dwarf File is for the CppHashFilename if not then emit the
1798 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001799 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001800 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1801 0, StringRef(), CppHashFilename);
1802 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1805 // cache with the different Loc from the call above we save the last
1806 // info we queried here with SrcMgr.FindLineNumber().
1807 unsigned CppHashLocLineNo;
1808 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1809 CppHashLocLineNo = LastQueryLine;
1810 else {
1811 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1812 LastQueryLine = CppHashLocLineNo;
1813 LastQueryIDLoc = CppHashLoc;
1814 LastQueryBuffer = CppHashBuf;
1815 }
1816 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001817 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001818
Jim Grosbach4b905842013-09-20 23:08:21 +00001819 getStreamer().EmitDwarfLocDirective(
1820 getContext().getGenDwarfFileNumber(), Line, 0,
1821 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1822 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001823 }
1824
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001825 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001826 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001827 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001828 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1829 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001830 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001831 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001832
Chris Lattnera2a9d162010-09-11 16:18:25 +00001833 // Don't skip the rest of the line, the instruction parser is responsible for
1834 // that.
1835 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001836}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001837
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001838// Parse and erase curly braces marking block start/end
1839bool
1840AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1841 // Identify curly brace marking block start/end
1842 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1843 return false;
1844
1845 SMLoc StartLoc = Lexer.getLoc();
1846 Lex(); // Eat the brace
1847 if (Lexer.is(AsmToken::EndOfStatement))
1848 Lex(); // Eat EndOfStatement following the brace
1849
1850 // Erase the block start/end brace from the output asm string
1851 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
1852 StartLoc.getPointer());
1853 return true;
1854}
1855
Jim Grosbach4b905842013-09-20 23:08:21 +00001856/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001857/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001858void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001859 if (!Lexer.is(AsmToken::EndOfStatement))
1860 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001861 // Eat EOL.
1862 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001863}
1864
Jim Grosbach4b905842013-09-20 23:08:21 +00001865/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001866/// ::= # number "filename"
1867/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001868bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001869 Lex(); // Eat the hash token.
1870
1871 if (getLexer().isNot(AsmToken::Integer)) {
1872 // Consume the line since in cases it is not a well-formed line directive,
1873 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001874 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001875 return false;
1876 }
1877
1878 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001879 Lex();
1880
1881 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001882 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001883 return false;
1884 }
1885
1886 StringRef Filename = getTok().getString();
1887 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001888 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001889
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001890 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1891 CppHashLoc = L;
1892 CppHashFilename = Filename;
1893 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001894 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001895
1896 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001897 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001898 return false;
1899}
1900
Jim Grosbach4b905842013-09-20 23:08:21 +00001901/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001902/// for the Filename and LineNo if any in the diagnostic.
1903void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001904 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001905 raw_ostream &OS = errs();
1906
1907 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001908 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001909 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1910 unsigned CppHashBuf =
1911 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001912
Jim Grosbach4b905842013-09-20 23:08:21 +00001913 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001914 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001915 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1916 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1917 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001918 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1919 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001920 }
1921
Eric Christophera7c32732012-12-18 00:30:54 +00001922 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001923 // manager changed or buffer changed (like in a nested include) then just
1924 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001925 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001926 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001927 if (Parser->SavedDiagHandler)
1928 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1929 else
Craig Topper353eda42014-04-24 06:44:33 +00001930 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001931 return;
1932 }
1933
Eric Christophera7c32732012-12-18 00:30:54 +00001934 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001935 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1936 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001937 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001938
1939 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1940 int CppHashLocLineNo =
1941 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001942 int LineNo =
1943 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001944
Jim Grosbach4b905842013-09-20 23:08:21 +00001945 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1946 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001947 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001948
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001949 if (Parser->SavedDiagHandler)
1950 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1951 else
Craig Topper353eda42014-04-24 06:44:33 +00001952 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001953}
1954
Rafael Espindola2c064482012-08-21 18:29:30 +00001955// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1956// difference being that that function accepts '@' as part of identifiers and
1957// we can't do that. AsmLexer.cpp should probably be changed to handle
1958// '@' as a special case when needed.
1959static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001960 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1961 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001962}
1963
Rafael Espindola34b9c512012-06-03 23:57:14 +00001964bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001965 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001966 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001967 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001968 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001969 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001970 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001971 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001972
Preston Gurd05500642012-09-19 20:36:12 +00001973 // A macro without parameters is handled differently on Darwin:
1974 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001975 while (!Body.empty()) {
1976 // Scan for the next substitution.
1977 std::size_t End = Body.size(), Pos = 0;
1978 for (; Pos != End; ++Pos) {
1979 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001980 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001981 // This macro has no parameters, look for $0, $1, etc.
1982 if (Body[Pos] != '$' || Pos + 1 == End)
1983 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001984
Rafael Espindola1134ab232011-06-05 02:43:45 +00001985 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001986 if (Next == '$' || Next == 'n' ||
1987 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001988 break;
1989 } else {
1990 // This macro has parameters, look for \foo, \bar, etc.
1991 if (Body[Pos] == '\\' && Pos + 1 != End)
1992 break;
1993 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001994 }
1995
1996 // Add the prefix.
1997 OS << Body.slice(0, Pos);
1998
1999 // Check if we reached the end.
2000 if (Pos == End)
2001 break;
2002
Benjamin Kramer513e7442014-02-20 13:36:32 +00002003 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002004 switch (Body[Pos + 1]) {
2005 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002006 case '$':
2007 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002008 break;
2009
Jim Grosbach4b905842013-09-20 23:08:21 +00002010 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002011 case 'n':
2012 OS << A.size();
2013 break;
2014
Jim Grosbach4b905842013-09-20 23:08:21 +00002015 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002016 default: {
2017 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002018 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002019 if (Index >= A.size())
2020 break;
2021
2022 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002023 for (const AsmToken &Token : A[Index])
2024 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002025 break;
2026 }
2027 }
2028 Pos += 2;
2029 } else {
2030 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002031
2032 // Check for the \@ pseudo-variable.
2033 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002034 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002035 else
2036 while (isIdentifierChar(Body[I]) && I + 1 != End)
2037 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002038
Jim Grosbach4b905842013-09-20 23:08:21 +00002039 const char *Begin = Body.data() + Pos + 1;
2040 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002041 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002042
Toma Tabacu217116e2015-04-27 10:50:29 +00002043 if (Argument == "@") {
2044 OS << NumOfMacroInstantiations;
2045 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002046 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002047 for (; Index < NParameters; ++Index)
2048 if (Parameters[Index].Name == Argument)
2049 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002050
Toma Tabacu217116e2015-04-27 10:50:29 +00002051 if (Index == NParameters) {
2052 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2053 Pos += 3;
2054 else {
2055 OS << '\\' << Argument;
2056 Pos = I;
2057 }
2058 } else {
2059 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002060 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002061 // We expect no quotes around the string's contents when
2062 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002063 if (Token.getKind() != AsmToken::String || VarargParameter)
2064 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002065 else
Craig Topper84008482015-10-10 05:38:14 +00002066 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002067
2068 Pos += 1 + Argument.size();
2069 }
Preston Gurd05500642012-09-19 20:36:12 +00002070 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002071 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002072 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002073 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002074 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002075
Rafael Espindola1134ab232011-06-05 02:43:45 +00002076 return false;
2077}
Daniel Dunbar43235712010-07-18 18:54:11 +00002078
Nico Weber2a8f9222014-07-24 16:29:04 +00002079MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002080 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002081 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002082 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002083
Jim Grosbach4b905842013-09-20 23:08:21 +00002084static bool isOperator(AsmToken::TokenKind kind) {
2085 switch (kind) {
2086 default:
2087 return false;
2088 case AsmToken::Plus:
2089 case AsmToken::Minus:
2090 case AsmToken::Tilde:
2091 case AsmToken::Slash:
2092 case AsmToken::Star:
2093 case AsmToken::Dot:
2094 case AsmToken::Equal:
2095 case AsmToken::EqualEqual:
2096 case AsmToken::Pipe:
2097 case AsmToken::PipePipe:
2098 case AsmToken::Caret:
2099 case AsmToken::Amp:
2100 case AsmToken::AmpAmp:
2101 case AsmToken::Exclaim:
2102 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002103 case AsmToken::Less:
2104 case AsmToken::LessEqual:
2105 case AsmToken::LessLess:
2106 case AsmToken::LessGreater:
2107 case AsmToken::Greater:
2108 case AsmToken::GreaterEqual:
2109 case AsmToken::GreaterGreater:
2110 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002111 }
2112}
2113
David Majnemer16252452014-01-29 00:07:39 +00002114namespace {
2115class AsmLexerSkipSpaceRAII {
2116public:
2117 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2118 Lexer.setSkipSpace(SkipSpace);
2119 }
2120
2121 ~AsmLexerSkipSpaceRAII() {
2122 Lexer.setSkipSpace(true);
2123 }
2124
2125private:
2126 AsmLexer &Lexer;
2127};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002128}
David Majnemer16252452014-01-29 00:07:39 +00002129
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002130bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2131
2132 if (Vararg) {
2133 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2134 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002135 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002136 }
2137 return false;
2138 }
2139
Rafael Espindola768b41c2012-06-15 14:02:34 +00002140 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002141
David Majnemer16252452014-01-29 00:07:39 +00002142 // Darwin doesn't use spaces to delmit arguments.
2143 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002144
Scott Egertona1fa68a2016-02-11 13:48:49 +00002145 bool SpaceEaten;
2146
Rafael Espindola768b41c2012-06-15 14:02:34 +00002147 for (;;) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002148 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002149 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002150 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002151
Scott Egertona1fa68a2016-02-11 13:48:49 +00002152 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002153
Scott Egertona1fa68a2016-02-11 13:48:49 +00002154 if (Lexer.is(AsmToken::Comma))
2155 break;
2156
2157 if (Lexer.is(AsmToken::Space)) {
2158 SpaceEaten = true;
2159 Lex(); // Eat spaces
2160 }
Preston Gurd05500642012-09-19 20:36:12 +00002161
2162 // Spaces can delimit parameters, but could also be part an expression.
2163 // If the token after a space is an operator, add the token and the next
2164 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002165 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002166 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002167 MA.push_back(getTok());
2168 Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002169
Scott Egertona1fa68a2016-02-11 13:48:49 +00002170 // Whitespace after an operator can be ignored.
2171 if (Lexer.is(AsmToken::Space))
2172 Lex();
2173
2174 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002175 }
2176 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002177 if (SpaceEaten)
2178 break;
Preston Gurd05500642012-09-19 20:36:12 +00002179 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002180
Jim Grosbach4b905842013-09-20 23:08:21 +00002181 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002182 // to be able to fill in the remaining default parameter values
2183 if (Lexer.is(AsmToken::EndOfStatement))
2184 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002185
2186 // Adjust the current parentheses level.
2187 if (Lexer.is(AsmToken::LParen))
2188 ++ParenLevel;
2189 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2190 --ParenLevel;
2191
2192 // Append the token to the current argument list.
2193 MA.push_back(getTok());
2194 Lex();
2195 }
Preston Gurd05500642012-09-19 20:36:12 +00002196
Rafael Espindola768b41c2012-06-15 14:02:34 +00002197 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002198 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002199 return false;
2200}
2201
2202// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002203bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002204 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002205 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002206 bool NamedParametersFound = false;
2207 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002208
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002209 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002210 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002211
Rafael Espindola768b41c2012-06-15 14:02:34 +00002212 // Parse two kinds of macro invocations:
2213 // - macros defined without any parameters accept an arbitrary number of them
2214 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002215 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002216 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2217 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002218 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002219 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002220
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002221 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002222 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002223 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002224 eatToEndOfStatement();
2225 return true;
2226 }
2227
2228 if (!Lexer.is(AsmToken::Equal)) {
2229 TokError("expected '=' after formal parameter identifier");
2230 eatToEndOfStatement();
2231 return true;
2232 }
2233 Lex();
2234
2235 NamedParametersFound = true;
2236 }
2237
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002238 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002239 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002240 eatToEndOfStatement();
2241 return true;
2242 }
2243
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002244 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2245 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002246 return true;
2247
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002248 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002249 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002250 unsigned FAI = 0;
2251 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002252 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002253 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002254
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002255 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002256 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002257 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002258 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002259 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002260 return true;
2261 }
2262 PI = FAI;
2263 }
2264
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002265 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002266 if (A.size() <= PI)
2267 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002268 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002269
2270 if (FALocs.size() <= PI)
2271 FALocs.resize(PI + 1);
2272
2273 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002274 }
Jim Grosbach206661622012-07-30 22:44:17 +00002275
Preston Gurd242ed3152012-09-19 20:29:04 +00002276 // At the end of the statement, fill in remaining arguments that have
2277 // default values. If there aren't any, then the next argument is
2278 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002279 if (Lexer.is(AsmToken::EndOfStatement)) {
2280 bool Failure = false;
2281 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2282 if (A[FAI].empty()) {
2283 if (M->Parameters[FAI].Required) {
2284 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2285 "missing value for required parameter "
2286 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2287 Failure = true;
2288 }
2289
2290 if (!M->Parameters[FAI].Value.empty())
2291 A[FAI] = M->Parameters[FAI].Value;
2292 }
2293 }
2294 return Failure;
2295 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002296
2297 if (Lexer.is(AsmToken::Comma))
2298 Lex();
2299 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002300
2301 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002302}
2303
Jim Grosbach4b905842013-09-20 23:08:21 +00002304const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002305 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2306 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002307}
2308
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002309void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2310 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002311}
2312
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002313void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002314
Jim Grosbach4b905842013-09-20 23:08:21 +00002315bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002316 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2317 // this, although we should protect against infinite loops.
2318 if (ActiveMacros.size() == 20)
2319 return TokError("macros cannot be nested more than 20 levels deep");
2320
Eli Bendersky38274122013-01-14 23:22:36 +00002321 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002322 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002323 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002324
Rafael Espindola1134ab232011-06-05 02:43:45 +00002325 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2326 // to hold the macro body with substitutions.
2327 SmallString<256> Buf;
2328 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002329 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002330
Toma Tabacu217116e2015-04-27 10:50:29 +00002331 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002332 return true;
2333
Eli Bendersky38274122013-01-14 23:22:36 +00002334 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002335 // instantiation.
2336 OS << ".endmacro\n";
2337
Rafael Espindola3560ff22014-08-27 20:03:13 +00002338 std::unique_ptr<MemoryBuffer> Instantiation =
2339 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002340
Daniel Dunbar43235712010-07-18 18:54:11 +00002341 // Create the macro instantiation object and add to the current macro
2342 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002343 MacroInstantiation *MI = new MacroInstantiation(
2344 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002345 ActiveMacros.push_back(MI);
2346
Toma Tabacu217116e2015-04-27 10:50:29 +00002347 ++NumOfMacroInstantiations;
2348
Daniel Dunbar43235712010-07-18 18:54:11 +00002349 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002350 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002351 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002352 Lex();
2353
2354 return false;
2355}
2356
Jim Grosbach4b905842013-09-20 23:08:21 +00002357void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002358 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002359 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002360 Lex();
2361
2362 // Pop the instantiation entry.
2363 delete ActiveMacros.back();
2364 ActiveMacros.pop_back();
2365}
2366
Jim Grosbach4b905842013-09-20 23:08:21 +00002367bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002368 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002369 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002370 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002371 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2372 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002373 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002374
Pete Cooper80d21cb2015-06-22 19:35:57 +00002375 if (!Sym) {
2376 // In the case where we parse an expression starting with a '.', we will
2377 // not generate an error, nor will we create a symbol. In this case we
2378 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002379 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002380 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002381
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002382 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002383 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002384 if (NoDeadStrip)
2385 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2386
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002387 return false;
2388}
2389
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002391/// ::= identifier
2392/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002393bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002394 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002395 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2396 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002397 // handle this as a context dependent token, instead we detect adjacent tokens
2398 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002399 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2400 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002401
Hans Wennborgce69d772013-10-18 20:46:28 +00002402 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002403 Lex();
2404 if (Lexer.isNot(AsmToken::Identifier))
2405 return true;
2406
Hans Wennborgce69d772013-10-18 20:46:28 +00002407 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2408 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002409 return true;
2410
2411 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002412 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002413 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002414 Lex();
2415 return false;
2416 }
2417
Jim Grosbach4b905842013-09-20 23:08:21 +00002418 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002419 return true;
2420
Sean Callanan936b0d32010-01-19 21:44:56 +00002421 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002422
Sean Callanan686ed8d2010-01-19 20:22:31 +00002423 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002424
2425 return false;
2426}
2427
Jim Grosbach4b905842013-09-20 23:08:21 +00002428/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002429/// ::= .equ identifier ',' expression
2430/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002431/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002432bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002433 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002434
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002435 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002436 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002437
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002438 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002439 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002440 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002441
Jim Grosbach4b905842013-09-20 23:08:21 +00002442 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002443}
2444
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002445bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002446 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002447
2448 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002449 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002450 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2451 if (Str[i] != '\\') {
2452 Data += Str[i];
2453 continue;
2454 }
2455
2456 // Recognize escaped characters. Note that this escape semantics currently
2457 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2458 ++i;
2459 if (i == e)
2460 return TokError("unexpected backslash at end of string");
2461
2462 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002463 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002464 // Consume up to three octal characters.
2465 unsigned Value = Str[i] - '0';
2466
Jim Grosbach4b905842013-09-20 23:08:21 +00002467 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002468 ++i;
2469 Value = Value * 8 + (Str[i] - '0');
2470
Jim Grosbach4b905842013-09-20 23:08:21 +00002471 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002472 ++i;
2473 Value = Value * 8 + (Str[i] - '0');
2474 }
2475 }
2476
2477 if (Value > 255)
2478 return TokError("invalid octal escape sequence (out of range)");
2479
Jim Grosbach4b905842013-09-20 23:08:21 +00002480 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002481 continue;
2482 }
2483
2484 // Otherwise recognize individual escapes.
2485 switch (Str[i]) {
2486 default:
2487 // Just reject invalid escape sequences for now.
2488 return TokError("invalid escape sequence (unrecognized character)");
2489
2490 case 'b': Data += '\b'; break;
2491 case 'f': Data += '\f'; break;
2492 case 'n': Data += '\n'; break;
2493 case 'r': Data += '\r'; break;
2494 case 't': Data += '\t'; break;
2495 case '"': Data += '"'; break;
2496 case '\\': Data += '\\'; break;
2497 }
2498 }
2499
2500 return false;
2501}
2502
Jim Grosbach4b905842013-09-20 23:08:21 +00002503/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002504/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002505bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002506 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002507 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002508
Daniel Dunbara10e5192009-06-24 23:30:00 +00002509 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002510 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002511 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002512
Daniel Dunbaref668c12009-08-14 18:19:52 +00002513 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002514 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002515 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002516
Rafael Espindola64e1af82013-07-02 15:49:13 +00002517 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002518 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002519 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002520
Sean Callanan686ed8d2010-01-19 20:22:31 +00002521 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002522
2523 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002524 break;
2525
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002526 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002527 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002528 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002529 }
2530 }
2531
Sean Callanan686ed8d2010-01-19 20:22:31 +00002532 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002533 return false;
2534}
2535
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002536/// parseDirectiveReloc
2537/// ::= .reloc expression , identifier [ , expression ]
2538bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2539 const MCExpr *Offset;
2540 const MCExpr *Expr = nullptr;
2541
2542 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2543 if (parseExpression(Offset))
2544 return true;
2545
2546 // We can only deal with constant expressions at the moment.
2547 int64_t OffsetValue;
2548 if (!Offset->evaluateAsAbsolute(OffsetValue))
2549 return Error(OffsetLoc, "expression is not a constant value");
2550
David Majnemerce108422016-01-19 23:05:27 +00002551 if (OffsetValue < 0)
2552 return Error(OffsetLoc, "expression is negative");
2553
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002554 if (Lexer.isNot(AsmToken::Comma))
2555 return TokError("expected comma");
2556 Lexer.Lex();
2557
2558 if (Lexer.isNot(AsmToken::Identifier))
2559 return TokError("expected relocation name");
2560 SMLoc NameLoc = Lexer.getTok().getLoc();
2561 StringRef Name = Lexer.getTok().getIdentifier();
2562 Lexer.Lex();
2563
2564 if (Lexer.is(AsmToken::Comma)) {
2565 Lexer.Lex();
2566 SMLoc ExprLoc = Lexer.getLoc();
2567 if (parseExpression(Expr))
2568 return true;
2569
2570 MCValue Value;
2571 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2572 return Error(ExprLoc, "expression must be relocatable");
2573 }
2574
2575 if (Lexer.isNot(AsmToken::EndOfStatement))
2576 return TokError("unexpected token in .reloc directive");
2577
2578 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2579 return Error(NameLoc, "unknown relocation name");
2580
2581 return false;
2582}
2583
Jim Grosbach4b905842013-09-20 23:08:21 +00002584/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002585/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002586bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002587 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002588 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002589
Daniel Dunbara10e5192009-06-24 23:30:00 +00002590 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002591 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002592 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002593 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002594 return true;
2595
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002596 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002597 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2598 assert(Size <= 8 && "Invalid size");
2599 uint64_t IntValue = MCE->getValue();
2600 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2601 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002602 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002603 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002604 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002605
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002606 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002607 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002608
Daniel Dunbara10e5192009-06-24 23:30:00 +00002609 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002610 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002611 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002612 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002613 }
2614 }
2615
Sean Callanan686ed8d2010-01-19 20:22:31 +00002616 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002617 return false;
2618}
2619
David Woodhoused6de0d92014-02-01 16:20:59 +00002620/// ParseDirectiveOctaValue
2621/// ::= .octa [ hexconstant (, hexconstant)* ]
2622bool AsmParser::parseDirectiveOctaValue() {
2623 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2624 checkForValidSection();
2625
2626 for (;;) {
2627 if (Lexer.getKind() == AsmToken::Error)
2628 return true;
2629 if (Lexer.getKind() != AsmToken::Integer &&
2630 Lexer.getKind() != AsmToken::BigNum)
2631 return TokError("unknown token in expression");
2632
2633 SMLoc ExprLoc = getLexer().getLoc();
2634 APInt IntValue = getTok().getAPIntVal();
2635 Lex();
2636
2637 uint64_t hi, lo;
2638 if (IntValue.isIntN(64)) {
2639 hi = 0;
2640 lo = IntValue.getZExtValue();
2641 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002642 // It might actually have more than 128 bits, but the top ones are zero.
2643 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002644 lo = IntValue.getLoBits(64).getZExtValue();
2645 } else
2646 return Error(ExprLoc, "literal value out of range for directive");
2647
2648 if (MAI.isLittleEndian()) {
2649 getStreamer().EmitIntValue(lo, 8);
2650 getStreamer().EmitIntValue(hi, 8);
2651 } else {
2652 getStreamer().EmitIntValue(hi, 8);
2653 getStreamer().EmitIntValue(lo, 8);
2654 }
2655
2656 if (getLexer().is(AsmToken::EndOfStatement))
2657 break;
2658
2659 // FIXME: Improve diagnostic.
2660 if (getLexer().isNot(AsmToken::Comma))
2661 return TokError("unexpected token in directive");
2662 Lex();
2663 }
2664 }
2665
2666 Lex();
2667 return false;
2668}
2669
Jim Grosbach4b905842013-09-20 23:08:21 +00002670/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002671/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002672bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002674 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002675
2676 for (;;) {
2677 // We don't truly support arithmetic on floating point expressions, so we
2678 // have to manually parse unary prefixes.
2679 bool IsNeg = false;
2680 if (getLexer().is(AsmToken::Minus)) {
2681 Lex();
2682 IsNeg = true;
2683 } else if (getLexer().is(AsmToken::Plus))
2684 Lex();
2685
Michael J. Spencer530ce852010-10-09 11:00:50 +00002686 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002687 getLexer().isNot(AsmToken::Real) &&
2688 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002689 return TokError("unexpected token in directive");
2690
2691 // Convert to an APFloat.
2692 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002693 StringRef IDVal = getTok().getString();
2694 if (getLexer().is(AsmToken::Identifier)) {
2695 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2696 Value = APFloat::getInf(Semantics);
2697 else if (!IDVal.compare_lower("nan"))
2698 Value = APFloat::getNaN(Semantics, false, ~0);
2699 else
2700 return TokError("invalid floating point literal");
2701 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002702 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002703 return TokError("invalid floating point literal");
2704 if (IsNeg)
2705 Value.changeSign();
2706
2707 // Consume the numeric token.
2708 Lex();
2709
2710 // Emit the value as an integer.
2711 APInt AsInt = Value.bitcastToAPInt();
2712 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002713 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002714
2715 if (getLexer().is(AsmToken::EndOfStatement))
2716 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002717
Daniel Dunbar2af16532010-09-24 01:59:56 +00002718 if (getLexer().isNot(AsmToken::Comma))
2719 return TokError("unexpected token in directive");
2720 Lex();
2721 }
2722 }
2723
2724 Lex();
2725 return false;
2726}
2727
Jim Grosbach4b905842013-09-20 23:08:21 +00002728/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002729/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002730bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002731 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002732
2733 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002734 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002735 return true;
2736
Rafael Espindolab91bac62010-10-05 19:42:57 +00002737 int64_t Val = 0;
2738 if (getLexer().is(AsmToken::Comma)) {
2739 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002740 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002741 return true;
2742 }
2743
Rafael Espindola922e3f42010-09-16 15:03:59 +00002744 if (getLexer().isNot(AsmToken::EndOfStatement))
2745 return TokError("unexpected token in '.zero' directive");
2746
2747 Lex();
2748
Rafael Espindola64e1af82013-07-02 15:49:13 +00002749 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002750
2751 return false;
2752}
2753
Jim Grosbach4b905842013-09-20 23:08:21 +00002754/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002755/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002756bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002757 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002758
David Majnemer522d3db2014-02-01 07:19:38 +00002759 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002760 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002761 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002762 return true;
2763
David Majnemer522d3db2014-02-01 07:19:38 +00002764 if (NumValues < 0) {
2765 Warning(RepeatLoc,
2766 "'.fill' directive with negative repeat count has no effect");
2767 NumValues = 0;
2768 }
2769
Roman Divackye33098f2013-09-24 17:44:41 +00002770 int64_t FillSize = 1;
2771 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002772
David Majnemer522d3db2014-02-01 07:19:38 +00002773 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002774 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2775 if (getLexer().isNot(AsmToken::Comma))
2776 return TokError("unexpected token in '.fill' directive");
2777 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002778
David Majnemer522d3db2014-02-01 07:19:38 +00002779 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002780 if (parseAbsoluteExpression(FillSize))
2781 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002782
Roman Divackye33098f2013-09-24 17:44:41 +00002783 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2784 if (getLexer().isNot(AsmToken::Comma))
2785 return TokError("unexpected token in '.fill' directive");
2786 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002787
David Majnemer522d3db2014-02-01 07:19:38 +00002788 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002789 if (parseAbsoluteExpression(FillExpr))
2790 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002791
Roman Divackye33098f2013-09-24 17:44:41 +00002792 if (getLexer().isNot(AsmToken::EndOfStatement))
2793 return TokError("unexpected token in '.fill' directive");
2794
2795 Lex();
2796 }
2797 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002798
David Majnemer522d3db2014-02-01 07:19:38 +00002799 if (FillSize < 0) {
2800 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2801 NumValues = 0;
2802 }
2803 if (FillSize > 8) {
2804 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2805 FillSize = 8;
2806 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002807
David Majnemer522d3db2014-02-01 07:19:38 +00002808 if (!isUInt<32>(FillExpr) && FillSize > 4)
2809 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2810
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002811 if (NumValues > 0) {
2812 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2813 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2814 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2815 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2816 if (NonZeroFillSize < FillSize)
2817 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2818 }
David Majnemer522d3db2014-02-01 07:19:38 +00002819 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002820
2821 return false;
2822}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002823
Jim Grosbach4b905842013-09-20 23:08:21 +00002824/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002825/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002826bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002827 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002828
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002829 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002830 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002831 return true;
2832
2833 // Parse optional fill expression.
2834 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2836 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002837 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002838 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002839
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002840 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002841 return true;
2842
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002843 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002844 return TokError("unexpected token in '.org' directive");
2845 }
2846
Sean Callanan686ed8d2010-01-19 20:22:31 +00002847 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002848 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002849 return false;
2850}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002851
Jim Grosbach4b905842013-09-20 23:08:21 +00002852/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002853/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002854bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002855 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002856
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002857 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002858 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002859 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002860 return true;
2861
2862 SMLoc MaxBytesLoc;
2863 bool HasFillExpr = false;
2864 int64_t FillExpr = 0;
2865 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002866 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2867 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002868 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002869 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002870
2871 // The fill expression can be omitted while specifying a maximum number of
2872 // alignment bytes, e.g:
2873 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002874 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002875 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002876 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002877 return true;
2878 }
2879
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002880 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2881 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002882 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002883 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002884
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002885 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002886 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002887 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002888
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002889 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002890 return TokError("unexpected token in directive");
2891 }
2892 }
2893
Sean Callanan686ed8d2010-01-19 20:22:31 +00002894 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002895
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002896 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002897 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002898
2899 // Compute alignment in bytes.
2900 if (IsPow2) {
2901 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002902 if (Alignment >= 32) {
2903 Error(AlignmentLoc, "invalid alignment value");
2904 Alignment = 31;
2905 }
2906
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002907 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002908 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002909 // Reject alignments that aren't either a power of two or zero,
2910 // for gas compatibility. Alignment of zero is silently rounded
2911 // up to one.
2912 if (Alignment == 0)
2913 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002914 if (!isPowerOf2_64(Alignment))
2915 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002916 }
2917
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002918 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002919 if (MaxBytesLoc.isValid()) {
2920 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002921 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002922 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002923 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002924 }
2925
2926 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002927 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002928 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002929 MaxBytesToFill = 0;
2930 }
2931 }
2932
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002933 // Check whether we should use optimal code alignment for this .align
2934 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002935 const MCSection *Section = getStreamer().getCurrentSection().first;
2936 assert(Section && "must have section to emit alignment");
2937 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002938 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2939 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002940 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002941 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002942 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002943 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2944 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002945 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002946
2947 return false;
2948}
2949
Jim Grosbach4b905842013-09-20 23:08:21 +00002950/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002951/// ::= .file [number] filename
2952/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002953bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002954 // FIXME: I'm not sure what this is.
2955 int64_t FileNumber = -1;
2956 SMLoc FileNumberLoc = getLexer().getLoc();
2957 if (getLexer().is(AsmToken::Integer)) {
2958 FileNumber = getTok().getIntVal();
2959 Lex();
2960
2961 if (FileNumber < 1)
2962 return TokError("file number less than one");
2963 }
2964
2965 if (getLexer().isNot(AsmToken::String))
2966 return TokError("unexpected token in '.file' directive");
2967
2968 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002969 // Allow the strings to have escaped octal character sequence.
2970 std::string Path = getTok().getString();
2971 if (parseEscapedString(Path))
2972 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002973 Lex();
2974
2975 StringRef Directory;
2976 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002977 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002978 if (getLexer().is(AsmToken::String)) {
2979 if (FileNumber == -1)
2980 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002981 if (parseEscapedString(FilenameData))
2982 return true;
2983 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002984 Directory = Path;
2985 Lex();
2986 } else {
2987 Filename = Path;
2988 }
2989
2990 if (getLexer().isNot(AsmToken::EndOfStatement))
2991 return TokError("unexpected token in '.file' directive");
2992
2993 if (FileNumber == -1)
2994 getStreamer().EmitFileDirective(Filename);
2995 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002996 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002997 Error(DirectiveLoc,
2998 "input can't have .file dwarf directives when -g is "
2999 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00003000
David Blaikiec714ef42014-03-17 01:52:11 +00003001 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
3002 0)
Eli Bendersky17233942013-01-15 22:59:42 +00003003 Error(FileNumberLoc, "file number already allocated");
3004 }
3005
3006 return false;
3007}
3008
Jim Grosbach4b905842013-09-20 23:08:21 +00003009/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003010/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003011bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00003012 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3013 if (getLexer().isNot(AsmToken::Integer))
3014 return TokError("unexpected token in '.line' directive");
3015
3016 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00003017 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003018 Lex();
3019
3020 // FIXME: Do something with the .line.
3021 }
3022
3023 if (getLexer().isNot(AsmToken::EndOfStatement))
3024 return TokError("unexpected token in '.line' directive");
3025
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3031/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3032/// The first number is a file number, must have been previously assigned with
3033/// a .file directive, the second number is the line number and optionally the
3034/// third number is a column position (zero if not specified). The remaining
3035/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003037 if (getLexer().isNot(AsmToken::Integer))
3038 return TokError("unexpected token in '.loc' directive");
3039 int64_t FileNumber = getTok().getIntVal();
3040 if (FileNumber < 1)
3041 return TokError("file number less than one in '.loc' directive");
3042 if (!getContext().isValidDwarfFileNumber(FileNumber))
3043 return TokError("unassigned file number in '.loc' directive");
3044 Lex();
3045
3046 int64_t LineNumber = 0;
3047 if (getLexer().is(AsmToken::Integer)) {
3048 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003049 if (LineNumber < 0)
3050 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003051 Lex();
3052 }
3053
3054 int64_t ColumnPos = 0;
3055 if (getLexer().is(AsmToken::Integer)) {
3056 ColumnPos = getTok().getIntVal();
3057 if (ColumnPos < 0)
3058 return TokError("column position less than zero in '.loc' directive");
3059 Lex();
3060 }
3061
3062 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3063 unsigned Isa = 0;
3064 int64_t Discriminator = 0;
3065 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3066 for (;;) {
3067 if (getLexer().is(AsmToken::EndOfStatement))
3068 break;
3069
3070 StringRef Name;
3071 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return TokError("unexpected token in '.loc' directive");
3074
3075 if (Name == "basic_block")
3076 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3077 else if (Name == "prologue_end")
3078 Flags |= DWARF2_FLAG_PROLOGUE_END;
3079 else if (Name == "epilogue_begin")
3080 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3081 else if (Name == "is_stmt") {
3082 Loc = getTok().getLoc();
3083 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003084 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003085 return true;
3086 // The expression must be the constant 0 or 1.
3087 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3088 int Value = MCE->getValue();
3089 if (Value == 0)
3090 Flags &= ~DWARF2_FLAG_IS_STMT;
3091 else if (Value == 1)
3092 Flags |= DWARF2_FLAG_IS_STMT;
3093 else
3094 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003095 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003096 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3097 }
Craig Topperf15655b2013-04-22 04:22:40 +00003098 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003099 Loc = getTok().getLoc();
3100 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003101 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
3103 // The expression must be a constant greater or equal to 0.
3104 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3105 int Value = MCE->getValue();
3106 if (Value < 0)
3107 return Error(Loc, "isa number less than zero");
3108 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003109 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return Error(Loc, "isa number not a constant value");
3111 }
Craig Topperf15655b2013-04-22 04:22:40 +00003112 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003113 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003114 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003115 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003116 return Error(Loc, "unknown sub-directive in '.loc' directive");
3117 }
3118
3119 if (getLexer().is(AsmToken::EndOfStatement))
3120 break;
3121 }
3122 }
3123
3124 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3125 Isa, Discriminator, StringRef());
3126
3127 return false;
3128}
3129
Jim Grosbach4b905842013-09-20 23:08:21 +00003130/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003131/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003132bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003133 return TokError("unsupported directive '.stabs'");
3134}
3135
Reid Kleckner2214ed82016-01-29 00:49:42 +00003136/// parseDirectiveCVFile
3137/// ::= .cv_file number filename
3138bool AsmParser::parseDirectiveCVFile() {
3139 SMLoc FileNumberLoc = getLexer().getLoc();
3140 if (getLexer().isNot(AsmToken::Integer))
3141 return TokError("expected file number in '.cv_file' directive");
3142
3143 int64_t FileNumber = getTok().getIntVal();
3144 Lex();
3145
3146 if (FileNumber < 1)
3147 return TokError("file number less than one");
3148
3149 if (getLexer().isNot(AsmToken::String))
3150 return TokError("unexpected token in '.cv_file' directive");
3151
3152 // Usually the directory and filename together, otherwise just the directory.
3153 // Allow the strings to have escaped octal character sequence.
3154 std::string Filename;
3155 if (parseEscapedString(Filename))
3156 return true;
3157 Lex();
3158
3159 if (getLexer().isNot(AsmToken::EndOfStatement))
3160 return TokError("unexpected token in '.cv_file' directive");
3161
3162 if (getStreamer().EmitCVFileDirective(FileNumber, Filename) == 0)
3163 Error(FileNumberLoc, "file number already allocated");
3164
3165 return false;
3166}
3167
3168/// parseDirectiveCVLoc
3169/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3170/// [is_stmt VALUE]
3171/// The first number is a file number, must have been previously assigned with
3172/// a .file directive, the second number is the line number and optionally the
3173/// third number is a column position (zero if not specified). The remaining
3174/// optional items are .loc sub-directives.
3175bool AsmParser::parseDirectiveCVLoc() {
3176 if (getLexer().isNot(AsmToken::Integer))
3177 return TokError("unexpected token in '.cv_loc' directive");
3178
3179 int64_t FunctionId = getTok().getIntVal();
3180 if (FunctionId < 0)
3181 return TokError("function id less than zero in '.cv_loc' directive");
3182 Lex();
3183
3184 int64_t FileNumber = getTok().getIntVal();
3185 if (FileNumber < 1)
3186 return TokError("file number less than one in '.cv_loc' directive");
3187 if (!getContext().isValidCVFileNumber(FileNumber))
3188 return TokError("unassigned file number in '.cv_loc' directive");
3189 Lex();
3190
3191 int64_t LineNumber = 0;
3192 if (getLexer().is(AsmToken::Integer)) {
3193 LineNumber = getTok().getIntVal();
3194 if (LineNumber < 0)
3195 return TokError("line number less than zero in '.cv_loc' directive");
3196 Lex();
3197 }
3198
3199 int64_t ColumnPos = 0;
3200 if (getLexer().is(AsmToken::Integer)) {
3201 ColumnPos = getTok().getIntVal();
3202 if (ColumnPos < 0)
3203 return TokError("column position less than zero in '.cv_loc' directive");
3204 Lex();
3205 }
3206
3207 bool PrologueEnd = false;
3208 uint64_t IsStmt = 0;
3209 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3210 StringRef Name;
3211 SMLoc Loc = getTok().getLoc();
3212 if (parseIdentifier(Name))
3213 return TokError("unexpected token in '.cv_loc' directive");
3214
3215 if (Name == "prologue_end")
3216 PrologueEnd = true;
3217 else if (Name == "is_stmt") {
3218 Loc = getTok().getLoc();
3219 const MCExpr *Value;
3220 if (parseExpression(Value))
3221 return true;
3222 // The expression must be the constant 0 or 1.
3223 IsStmt = ~0ULL;
3224 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3225 IsStmt = MCE->getValue();
3226
3227 if (IsStmt > 1)
3228 return Error(Loc, "is_stmt value not 0 or 1");
3229 } else {
3230 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3231 }
3232 }
3233
3234 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
3235 ColumnPos, PrologueEnd, IsStmt, StringRef());
3236 return false;
3237}
3238
3239/// parseDirectiveCVLinetable
3240/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3241bool AsmParser::parseDirectiveCVLinetable() {
3242 int64_t FunctionId = getTok().getIntVal();
3243 if (FunctionId < 0)
3244 return TokError("function id less than zero in '.cv_linetable' directive");
3245 Lex();
3246
3247 if (Lexer.isNot(AsmToken::Comma))
3248 return TokError("unexpected token in '.cv_linetable' directive");
3249 Lex();
3250
3251 SMLoc Loc = getLexer().getLoc();
3252 StringRef FnStartName;
3253 if (parseIdentifier(FnStartName))
3254 return Error(Loc, "expected identifier in directive");
3255
3256 if (Lexer.isNot(AsmToken::Comma))
3257 return TokError("unexpected token in '.cv_linetable' directive");
3258 Lex();
3259
3260 Loc = getLexer().getLoc();
3261 StringRef FnEndName;
3262 if (parseIdentifier(FnEndName))
3263 return Error(Loc, "expected identifier in directive");
3264
3265 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3266 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3267
3268 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3269 return false;
3270}
3271
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003272/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003273/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003274/// ("contains" SecondaryFunctionId+)?
3275bool AsmParser::parseDirectiveCVInlineLinetable() {
3276 int64_t PrimaryFunctionId = getTok().getIntVal();
3277 if (PrimaryFunctionId < 0)
3278 return TokError(
3279 "function id less than zero in '.cv_inline_linetable' directive");
3280 Lex();
3281
3282 int64_t SourceFileId = getTok().getIntVal();
3283 if (SourceFileId <= 0)
3284 return TokError(
3285 "File id less than zero in '.cv_inline_linetable' directive");
3286 Lex();
3287
3288 int64_t SourceLineNum = getTok().getIntVal();
3289 if (SourceLineNum < 0)
3290 return TokError(
3291 "Line number less than zero in '.cv_inline_linetable' directive");
3292 Lex();
3293
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003294 SMLoc Loc = getLexer().getLoc();
3295 StringRef FnStartName;
3296 if (parseIdentifier(FnStartName))
3297 return Error(Loc, "expected identifier in directive");
3298 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3299
David Majnemerc9911f22016-02-02 19:22:34 +00003300 Loc = getLexer().getLoc();
3301 StringRef FnEndName;
3302 if (parseIdentifier(FnEndName))
3303 return Error(Loc, "expected identifier in directive");
3304 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3305
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003306 SmallVector<unsigned, 8> SecondaryFunctionIds;
3307 if (getLexer().is(AsmToken::Identifier)) {
3308 if (getTok().getIdentifier() != "contains")
3309 return TokError(
3310 "unexpected identifier in '.cv_inline_linetable' directive");
3311 Lex();
3312
3313 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3314 int64_t SecondaryFunctionId = getTok().getIntVal();
3315 if (SecondaryFunctionId < 0)
3316 return TokError(
3317 "function id less than zero in '.cv_inline_linetable' directive");
3318 Lex();
3319
3320 SecondaryFunctionIds.push_back(SecondaryFunctionId);
3321 }
3322 }
3323
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003324 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3325 SourceLineNum, FnStartSym,
David Majnemerc9911f22016-02-02 19:22:34 +00003326 FnEndSym, SecondaryFunctionIds);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003327 return false;
3328}
3329
David Majnemer408b5e62016-02-05 01:55:49 +00003330/// parseDirectiveCVDefRange
3331/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3332bool AsmParser::parseDirectiveCVDefRange() {
3333 SMLoc Loc;
3334 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3335 while (getLexer().is(AsmToken::Identifier)) {
3336 Loc = getLexer().getLoc();
3337 StringRef GapStartName;
3338 if (parseIdentifier(GapStartName))
3339 return Error(Loc, "expected identifier in directive");
3340 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3341
3342 Loc = getLexer().getLoc();
3343 StringRef GapEndName;
3344 if (parseIdentifier(GapEndName))
3345 return Error(Loc, "expected identifier in directive");
3346 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3347
3348 Ranges.push_back({GapStartSym, GapEndSym});
3349 }
3350
3351 if (getLexer().isNot(AsmToken::Comma))
3352 return TokError("unexpected token in directive");
3353 Lex();
3354
3355 std::string FixedSizePortion;
3356 if (parseEscapedString(FixedSizePortion))
3357 return true;
3358 Lex();
3359
3360 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3361 return false;
3362}
3363
Reid Kleckner2214ed82016-01-29 00:49:42 +00003364/// parseDirectiveCVStringTable
3365/// ::= .cv_stringtable
3366bool AsmParser::parseDirectiveCVStringTable() {
3367 getStreamer().EmitCVStringTableDirective();
3368 return false;
3369}
3370
3371/// parseDirectiveCVFileChecksums
3372/// ::= .cv_filechecksums
3373bool AsmParser::parseDirectiveCVFileChecksums() {
3374 getStreamer().EmitCVFileChecksumsDirective();
3375 return false;
3376}
3377
Jim Grosbach4b905842013-09-20 23:08:21 +00003378/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003379/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003380bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003381 StringRef Name;
3382 bool EH = false;
3383 bool Debug = false;
3384
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003385 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003386 return TokError("Expected an identifier");
3387
3388 if (Name == ".eh_frame")
3389 EH = true;
3390 else if (Name == ".debug_frame")
3391 Debug = true;
3392
3393 if (getLexer().is(AsmToken::Comma)) {
3394 Lex();
3395
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003396 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003397 return TokError("Expected an identifier");
3398
3399 if (Name == ".eh_frame")
3400 EH = true;
3401 else if (Name == ".debug_frame")
3402 Debug = true;
3403 }
3404
3405 getStreamer().EmitCFISections(EH, Debug);
3406 return false;
3407}
3408
Jim Grosbach4b905842013-09-20 23:08:21 +00003409/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003410/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003411bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003412 StringRef Simple;
3413 if (getLexer().isNot(AsmToken::EndOfStatement))
3414 if (parseIdentifier(Simple) || Simple != "simple")
3415 return TokError("unexpected token in .cfi_startproc directive");
3416
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003417 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003418 return false;
3419}
3420
Jim Grosbach4b905842013-09-20 23:08:21 +00003421/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003422/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003423bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003424 getStreamer().EmitCFIEndProc();
3425 return false;
3426}
3427
Jim Grosbach4b905842013-09-20 23:08:21 +00003428/// \brief parse register name or number.
3429bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003430 SMLoc DirectiveLoc) {
3431 unsigned RegNo;
3432
3433 if (getLexer().isNot(AsmToken::Integer)) {
3434 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3435 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003436 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003437 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003438 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003439
3440 return false;
3441}
3442
Jim Grosbach4b905842013-09-20 23:08:21 +00003443/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003444/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003445bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003446 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003447 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003448 return true;
3449
3450 if (getLexer().isNot(AsmToken::Comma))
3451 return TokError("unexpected token in directive");
3452 Lex();
3453
3454 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003455 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003456 return true;
3457
3458 getStreamer().EmitCFIDefCfa(Register, Offset);
3459 return false;
3460}
3461
Jim Grosbach4b905842013-09-20 23:08:21 +00003462/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003463/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003464bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003465 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003466 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003467 return true;
3468
3469 getStreamer().EmitCFIDefCfaOffset(Offset);
3470 return false;
3471}
3472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003474/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003475bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003476 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003477 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003478 return true;
3479
3480 if (getLexer().isNot(AsmToken::Comma))
3481 return TokError("unexpected token in directive");
3482 Lex();
3483
3484 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003485 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003486 return true;
3487
3488 getStreamer().EmitCFIRegister(Register1, Register2);
3489 return false;
3490}
3491
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003492/// parseDirectiveCFIWindowSave
3493/// ::= .cfi_window_save
3494bool AsmParser::parseDirectiveCFIWindowSave() {
3495 getStreamer().EmitCFIWindowSave();
3496 return false;
3497}
3498
Jim Grosbach4b905842013-09-20 23:08:21 +00003499/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003500/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003502 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003504 return true;
3505
3506 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3507 return false;
3508}
3509
Jim Grosbach4b905842013-09-20 23:08:21 +00003510/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003511/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003512bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003513 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003515 return true;
3516
3517 getStreamer().EmitCFIDefCfaRegister(Register);
3518 return false;
3519}
3520
Jim Grosbach4b905842013-09-20 23:08:21 +00003521/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003522/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003523bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003524 int64_t Register = 0;
3525 int64_t Offset = 0;
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003528 return true;
3529
3530 if (getLexer().isNot(AsmToken::Comma))
3531 return TokError("unexpected token in directive");
3532 Lex();
3533
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003534 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return true;
3536
3537 getStreamer().EmitCFIOffset(Register, Offset);
3538 return false;
3539}
3540
Jim Grosbach4b905842013-09-20 23:08:21 +00003541/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003542/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003543bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003544 int64_t Register = 0;
3545
Jim Grosbach4b905842013-09-20 23:08:21 +00003546 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003547 return true;
3548
3549 if (getLexer().isNot(AsmToken::Comma))
3550 return TokError("unexpected token in directive");
3551 Lex();
3552
3553 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003554 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003555 return true;
3556
3557 getStreamer().EmitCFIRelOffset(Register, Offset);
3558 return false;
3559}
3560
3561static bool isValidEncoding(int64_t Encoding) {
3562 if (Encoding & ~0xff)
3563 return false;
3564
3565 if (Encoding == dwarf::DW_EH_PE_omit)
3566 return true;
3567
3568 const unsigned Format = Encoding & 0xf;
3569 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3570 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3571 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3572 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3573 return false;
3574
3575 const unsigned Application = Encoding & 0x70;
3576 if (Application != dwarf::DW_EH_PE_absptr &&
3577 Application != dwarf::DW_EH_PE_pcrel)
3578 return false;
3579
3580 return true;
3581}
3582
Jim Grosbach4b905842013-09-20 23:08:21 +00003583/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003584/// IsPersonality true for cfi_personality, false for cfi_lsda
3585/// ::= .cfi_personality encoding, [symbol_name]
3586/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003587bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003588 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003589 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003590 return true;
3591 if (Encoding == dwarf::DW_EH_PE_omit)
3592 return false;
3593
3594 if (!isValidEncoding(Encoding))
3595 return TokError("unsupported encoding.");
3596
3597 if (getLexer().isNot(AsmToken::Comma))
3598 return TokError("unexpected token in directive");
3599 Lex();
3600
3601 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003602 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003603 return TokError("expected identifier in directive");
3604
Jim Grosbach6f482002015-05-18 18:43:14 +00003605 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003606
3607 if (IsPersonality)
3608 getStreamer().EmitCFIPersonality(Sym, Encoding);
3609 else
3610 getStreamer().EmitCFILsda(Sym, Encoding);
3611 return false;
3612}
3613
Jim Grosbach4b905842013-09-20 23:08:21 +00003614/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003615/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003616bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003617 getStreamer().EmitCFIRememberState();
3618 return false;
3619}
3620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003622/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003624 getStreamer().EmitCFIRestoreState();
3625 return false;
3626}
3627
Jim Grosbach4b905842013-09-20 23:08:21 +00003628/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003629/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003630bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003631 int64_t Register = 0;
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003634 return true;
3635
3636 getStreamer().EmitCFISameValue(Register);
3637 return false;
3638}
3639
Jim Grosbach4b905842013-09-20 23:08:21 +00003640/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003641/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003642bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003643 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003644 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003645 return true;
3646
3647 getStreamer().EmitCFIRestore(Register);
3648 return false;
3649}
3650
Jim Grosbach4b905842013-09-20 23:08:21 +00003651/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003652/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003653bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003654 std::string Values;
3655 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003656 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003657 return true;
3658
3659 Values.push_back((uint8_t)CurrValue);
3660
3661 while (getLexer().is(AsmToken::Comma)) {
3662 Lex();
3663
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003664 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003665 return true;
3666
3667 Values.push_back((uint8_t)CurrValue);
3668 }
3669
3670 getStreamer().EmitCFIEscape(Values);
3671 return false;
3672}
3673
Jim Grosbach4b905842013-09-20 23:08:21 +00003674/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003675/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003676bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003677 if (getLexer().isNot(AsmToken::EndOfStatement))
3678 return Error(getLexer().getLoc(),
3679 "unexpected token in '.cfi_signal_frame'");
3680
3681 getStreamer().EmitCFISignalFrame();
3682 return false;
3683}
3684
Jim Grosbach4b905842013-09-20 23:08:21 +00003685/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003686/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003687bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003688 int64_t Register = 0;
3689
Jim Grosbach4b905842013-09-20 23:08:21 +00003690 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003691 return true;
3692
3693 getStreamer().EmitCFIUndefined(Register);
3694 return false;
3695}
3696
Jim Grosbach4b905842013-09-20 23:08:21 +00003697/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003698/// ::= .macros_on
3699/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003700bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003701 if (getLexer().isNot(AsmToken::EndOfStatement))
3702 return Error(getLexer().getLoc(),
3703 "unexpected token in '" + Directive + "' directive");
3704
Jim Grosbach4b905842013-09-20 23:08:21 +00003705 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003706 return false;
3707}
3708
Jim Grosbach4b905842013-09-20 23:08:21 +00003709/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003710/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003711bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003712 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003713 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003714 return TokError("expected identifier in '.macro' directive");
3715
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003716 if (getLexer().is(AsmToken::Comma))
3717 Lex();
3718
Eli Bendersky17233942013-01-15 22:59:42 +00003719 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003720 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003721
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003722 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003723 return Error(Lexer.getLoc(),
3724 "Vararg parameter '" + Parameters.back().Name +
3725 "' should be last one in the list of parameters.");
3726
David Majnemer91fc4c22014-01-29 18:57:46 +00003727 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003728 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003729 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003730
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003731 if (Lexer.is(AsmToken::Colon)) {
3732 Lex(); // consume ':'
3733
3734 SMLoc QualLoc;
3735 StringRef Qualifier;
3736
3737 QualLoc = Lexer.getLoc();
3738 if (parseIdentifier(Qualifier))
3739 return Error(QualLoc, "missing parameter qualifier for "
3740 "'" + Parameter.Name + "' in macro '" + Name + "'");
3741
3742 if (Qualifier == "req")
3743 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003744 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003745 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003746 else
3747 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3748 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3749 }
3750
David Majnemer91fc4c22014-01-29 18:57:46 +00003751 if (getLexer().is(AsmToken::Equal)) {
3752 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003753
3754 SMLoc ParamLoc;
3755
3756 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003757 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003758 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003759
3760 if (Parameter.Required)
3761 Warning(ParamLoc, "pointless default value for required parameter "
3762 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003763 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003764
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003765 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003766
3767 if (getLexer().is(AsmToken::Comma))
3768 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003769 }
3770
3771 // Eat the end of statement.
3772 Lex();
3773
3774 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003775 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003776
3777 // Lex the macro definition.
3778 for (;;) {
3779 // Check whether we have reached the end of the file.
3780 if (getLexer().is(AsmToken::Eof))
3781 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3782
3783 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003784 if (getLexer().is(AsmToken::Identifier)) {
3785 if (getTok().getIdentifier() == ".endm" ||
3786 getTok().getIdentifier() == ".endmacro") {
3787 if (MacroDepth == 0) { // Outermost macro.
3788 EndToken = getTok();
3789 Lex();
3790 if (getLexer().isNot(AsmToken::EndOfStatement))
3791 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3792 "' directive");
3793 break;
3794 } else {
3795 // Otherwise we just found the end of an inner macro.
3796 --MacroDepth;
3797 }
3798 } else if (getTok().getIdentifier() == ".macro") {
3799 // We allow nested macros. Those aren't instantiated until the outermost
3800 // macro is expanded so just ignore them for now.
3801 ++MacroDepth;
3802 }
Eli Bendersky17233942013-01-15 22:59:42 +00003803 }
3804
3805 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003806 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003807 }
3808
Jim Grosbach4b905842013-09-20 23:08:21 +00003809 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003810 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3811 }
3812
3813 const char *BodyStart = StartToken.getLoc().getPointer();
3814 const char *BodyEnd = EndToken.getLoc().getPointer();
3815 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003816 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003817 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003818 return false;
3819}
3820
Jim Grosbach4b905842013-09-20 23:08:21 +00003821/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003822///
3823/// With the support added for named parameters there may be code out there that
3824/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003825/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003826/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003827/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003828/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3829/// warning that the positional parameter found in body which have no effect.
3830/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003831/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003832/// intended or change the macro to use the named parameters. It is possible
3833/// this warning will trigger when the none of the named parameters are used
3834/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003835void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003836 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003837 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003838 // If this macro is not defined with named parameters the warning we are
3839 // checking for here doesn't apply.
3840 unsigned NParameters = Parameters.size();
3841 if (NParameters == 0)
3842 return;
3843
3844 bool NamedParametersFound = false;
3845 bool PositionalParametersFound = false;
3846
3847 // Look at the body of the macro for use of both the named parameters and what
3848 // are likely to be positional parameters. This is what expandMacro() is
3849 // doing when it finds the parameters in the body.
3850 while (!Body.empty()) {
3851 // Scan for the next possible parameter.
3852 std::size_t End = Body.size(), Pos = 0;
3853 for (; Pos != End; ++Pos) {
3854 // Check for a substitution or escape.
3855 // This macro is defined with parameters, look for \foo, \bar, etc.
3856 if (Body[Pos] == '\\' && Pos + 1 != End)
3857 break;
3858
3859 // This macro should have parameters, but look for $0, $1, ..., $n too.
3860 if (Body[Pos] != '$' || Pos + 1 == End)
3861 continue;
3862 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003863 if (Next == '$' || Next == 'n' ||
3864 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003865 break;
3866 }
3867
3868 // Check if we reached the end.
3869 if (Pos == End)
3870 break;
3871
3872 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003873 switch (Body[Pos + 1]) {
3874 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003875 case '$':
3876 break;
3877
Jim Grosbach4b905842013-09-20 23:08:21 +00003878 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003879 case 'n':
3880 PositionalParametersFound = true;
3881 break;
3882
Jim Grosbach4b905842013-09-20 23:08:21 +00003883 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003884 default: {
3885 PositionalParametersFound = true;
3886 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003887 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003888 }
3889 Pos += 2;
3890 } else {
3891 unsigned I = Pos + 1;
3892 while (isIdentifierChar(Body[I]) && I + 1 != End)
3893 ++I;
3894
Jim Grosbach4b905842013-09-20 23:08:21 +00003895 const char *Begin = Body.data() + Pos + 1;
3896 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003897 unsigned Index = 0;
3898 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003899 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003900 break;
3901
3902 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003903 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3904 Pos += 3;
3905 else {
3906 Pos = I;
3907 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003908 } else {
3909 NamedParametersFound = true;
3910 Pos += 1 + Argument.size();
3911 }
3912 }
3913 // Update the scan point.
3914 Body = Body.substr(Pos);
3915 }
3916
3917 if (!NamedParametersFound && PositionalParametersFound)
3918 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3919 "used in macro body, possible positional parameter "
3920 "found in body which will have no effect");
3921}
3922
Nico Weber155dccd12014-07-24 17:08:39 +00003923/// parseDirectiveExitMacro
3924/// ::= .exitm
3925bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3926 if (getLexer().isNot(AsmToken::EndOfStatement))
3927 return TokError("unexpected token in '" + Directive + "' directive");
3928
3929 if (!isInsideMacroInstantiation())
3930 return TokError("unexpected '" + Directive + "' in file, "
3931 "no current macro definition");
3932
3933 // Exit all conditionals that are active in the current macro.
3934 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3935 TheCondState = TheCondStack.back();
3936 TheCondStack.pop_back();
3937 }
3938
3939 handleMacroExit();
3940 return false;
3941}
3942
Jim Grosbach4b905842013-09-20 23:08:21 +00003943/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003944/// ::= .endm
3945/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003946bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003947 if (getLexer().isNot(AsmToken::EndOfStatement))
3948 return TokError("unexpected token in '" + Directive + "' directive");
3949
3950 // If we are inside a macro instantiation, terminate the current
3951 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003952 if (isInsideMacroInstantiation()) {
3953 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003954 return false;
3955 }
3956
3957 // Otherwise, this .endmacro is a stray entry in the file; well formed
3958 // .endmacro directives are handled during the macro definition parsing.
3959 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003960 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003961}
3962
Jim Grosbach4b905842013-09-20 23:08:21 +00003963/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003964/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003965bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003966 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003967 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003968 return TokError("expected identifier in '.purgem' directive");
3969
3970 if (getLexer().isNot(AsmToken::EndOfStatement))
3971 return TokError("unexpected token in '.purgem' directive");
3972
Jim Grosbach4b905842013-09-20 23:08:21 +00003973 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003974 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3975
Jim Grosbach4b905842013-09-20 23:08:21 +00003976 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003977 return false;
3978}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003979
Jim Grosbach4b905842013-09-20 23:08:21 +00003980/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003981/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003982bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003983 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003984
3985 // Expect a single argument: an expression that evaluates to a constant
3986 // in the inclusive range 0-30.
3987 SMLoc ExprLoc = getLexer().getLoc();
3988 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003989 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003990 return true;
3991 else if (getLexer().isNot(AsmToken::EndOfStatement))
3992 return TokError("unexpected token after expression in"
3993 " '.bundle_align_mode' directive");
3994 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3995 return Error(ExprLoc,
3996 "invalid bundle alignment size (expected between 0 and 30)");
3997
3998 Lex();
3999
4000 // Because of AlignSizePow2's verified range we can safely truncate it to
4001 // unsigned.
4002 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4003 return false;
4004}
4005
Jim Grosbach4b905842013-09-20 23:08:21 +00004006/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00004007/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00004008bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004009 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00004010 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004011
Eli Bendersky802b6282013-01-07 21:51:08 +00004012 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4013 StringRef Option;
4014 SMLoc Loc = getTok().getLoc();
4015 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004016 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004017
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004018 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00004019 return Error(Loc, kInvalidOptionError);
4020
4021 if (Option != "align_to_end")
4022 return Error(Loc, kInvalidOptionError);
4023 else if (getLexer().isNot(AsmToken::EndOfStatement))
4024 return Error(Loc,
4025 "unexpected token after '.bundle_lock' directive option");
4026 AlignToEnd = true;
4027 }
4028
Eli Benderskyf483ff92012-12-20 19:05:53 +00004029 Lex();
4030
Eli Bendersky802b6282013-01-07 21:51:08 +00004031 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004032 return false;
4033}
4034
Jim Grosbach4b905842013-09-20 23:08:21 +00004035/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004036/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004037bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004038 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004039
4040 if (getLexer().isNot(AsmToken::EndOfStatement))
4041 return TokError("unexpected token in '.bundle_unlock' directive");
4042 Lex();
4043
4044 getStreamer().EmitBundleUnlock();
4045 return false;
4046}
4047
Jim Grosbach4b905842013-09-20 23:08:21 +00004048/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004049/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004050bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004051 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004052
4053 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004054 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004055 return true;
4056
4057 int64_t FillExpr = 0;
4058 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4059 if (getLexer().isNot(AsmToken::Comma))
4060 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4061 Lex();
4062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004063 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004064 return true;
4065
4066 if (getLexer().isNot(AsmToken::EndOfStatement))
4067 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4068 }
4069
4070 Lex();
4071
4072 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00004073 return TokError("invalid number of bytes in '" + Twine(IDVal) +
4074 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00004075
4076 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00004077 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00004078
4079 return false;
4080}
4081
Jim Grosbach4b905842013-09-20 23:08:21 +00004082/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004083/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004084bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004085 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004086 const MCExpr *Value;
4087
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004088 for (;;) {
4089 if (parseExpression(Value))
4090 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004091
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004092 if (Signed)
4093 getStreamer().EmitSLEB128Value(Value);
4094 else
4095 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004096
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004097 if (getLexer().is(AsmToken::EndOfStatement))
4098 break;
4099
4100 if (getLexer().isNot(AsmToken::Comma))
4101 return TokError("unexpected token in directive");
4102 Lex();
4103 }
Eli Bendersky17233942013-01-15 22:59:42 +00004104
4105 return false;
4106}
4107
Jim Grosbach4b905842013-09-20 23:08:21 +00004108/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004109/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004110bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004111 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00004112 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004113 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004114 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004115
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004116 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004117 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004118
Jim Grosbach6f482002015-05-18 18:43:14 +00004119 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004120
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004121 // Assembler local symbols don't make any sense here. Complain loudly.
4122 if (Sym->isTemporary())
4123 return Error(Loc, "non-local symbol required in directive");
4124
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004125 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4126 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004127
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004128 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004129 break;
4130
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004131 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004132 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004133 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00004134 }
4135 }
4136
Sean Callanan686ed8d2010-01-19 20:22:31 +00004137 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004138 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004139}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004140
Jim Grosbach4b905842013-09-20 23:08:21 +00004141/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004142/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004143bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004144 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004145
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004146 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004147 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004148 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004149 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004150
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004151 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004152 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004153
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004154 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004155 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004156 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004157
4158 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004159 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004160 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004161 return true;
4162
4163 int64_t Pow2Alignment = 0;
4164 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004165 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004166 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004167 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004168 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004169 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004170
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004171 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4172 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004173 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4174
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004175 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004176 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4177 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004178 if (!isPowerOf2_64(Pow2Alignment))
4179 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4180 Pow2Alignment = Log2_64(Pow2Alignment);
4181 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004182 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004183
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004184 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004185 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004186
Sean Callanan686ed8d2010-01-19 20:22:31 +00004187 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004188
Chris Lattner28ad7542009-07-09 17:25:12 +00004189 // NOTE: a size of zero for a .comm should create a undefined symbol
4190 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004191 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004192 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004193 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004194
Eric Christopherbc818852010-05-14 01:38:54 +00004195 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004196 // may internally end up wanting an alignment in bytes.
4197 // FIXME: Diagnose overflow.
4198 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004199 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004200 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004201
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004202 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004203 return Error(IDLoc, "invalid symbol redefinition");
4204
Chris Lattner28ad7542009-07-09 17:25:12 +00004205 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004206 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004207 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004208 return false;
4209 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004210
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004211 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004212 return false;
4213}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004214
Jim Grosbach4b905842013-09-20 23:08:21 +00004215/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004216/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004217bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004218 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004219 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004220
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004221 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004222 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00004223 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004224
Sean Callanan686ed8d2010-01-19 20:22:31 +00004225 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00004226
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004227 if (Str.empty())
4228 Error(Loc, ".abort detected. Assembly stopping.");
4229 else
4230 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004231 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004232
4233 return false;
4234}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004235
Jim Grosbach4b905842013-09-20 23:08:21 +00004236/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004237/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004238bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004239 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004240 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004241
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004242 // Allow the strings to have escaped octal character sequence.
4243 std::string Filename;
4244 if (parseEscapedString(Filename))
4245 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004246 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00004247 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004248
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004249 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004250 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004251
Chris Lattner693fbb82009-07-16 06:14:39 +00004252 // Attempt to switch the lexer to the included file before consuming the end
4253 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00004254 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00004255 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00004256 return true;
4257 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004258
4259 return false;
4260}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004261
Jim Grosbach4b905842013-09-20 23:08:21 +00004262/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004263/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004264bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004265 if (getLexer().isNot(AsmToken::String))
4266 return TokError("expected string in '.incbin' directive");
4267
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004268 // Allow the strings to have escaped octal character sequence.
4269 std::string Filename;
4270 if (parseEscapedString(Filename))
4271 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00004272 SMLoc IncbinLoc = getLexer().getLoc();
4273 Lex();
4274
4275 if (getLexer().isNot(AsmToken::EndOfStatement))
4276 return TokError("unexpected token in '.incbin' directive");
4277
Kevin Enderby109f25c2011-12-14 21:47:48 +00004278 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00004279 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00004280 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
4281 return true;
4282 }
4283
4284 return false;
4285}
4286
Jim Grosbach4b905842013-09-20 23:08:21 +00004287/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004288/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4289bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004290 TheCondStack.push_back(TheCondState);
4291 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004292 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004293 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004294 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004295 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004296 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004297 return true;
4298
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004299 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004300 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004301
Sean Callanan686ed8d2010-01-19 20:22:31 +00004302 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004303
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004304 switch (DirKind) {
4305 default:
4306 llvm_unreachable("unsupported directive");
4307 case DK_IF:
4308 case DK_IFNE:
4309 break;
4310 case DK_IFEQ:
4311 ExprValue = ExprValue == 0;
4312 break;
4313 case DK_IFGE:
4314 ExprValue = ExprValue >= 0;
4315 break;
4316 case DK_IFGT:
4317 ExprValue = ExprValue > 0;
4318 break;
4319 case DK_IFLE:
4320 ExprValue = ExprValue <= 0;
4321 break;
4322 case DK_IFLT:
4323 ExprValue = ExprValue < 0;
4324 break;
4325 }
4326
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004327 TheCondState.CondMet = ExprValue;
4328 TheCondState.Ignore = !TheCondState.CondMet;
4329 }
4330
4331 return false;
4332}
4333
Jim Grosbach4b905842013-09-20 23:08:21 +00004334/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004335/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004336bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004337 TheCondStack.push_back(TheCondState);
4338 TheCondState.TheCond = AsmCond::IfCond;
4339
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004340 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004341 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004342 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004343 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004344
4345 if (getLexer().isNot(AsmToken::EndOfStatement))
4346 return TokError("unexpected token in '.ifb' directive");
4347
4348 Lex();
4349
4350 TheCondState.CondMet = ExpectBlank == Str.empty();
4351 TheCondState.Ignore = !TheCondState.CondMet;
4352 }
4353
4354 return false;
4355}
4356
Jim Grosbach4b905842013-09-20 23:08:21 +00004357/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004358/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004359/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004360bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004361 TheCondStack.push_back(TheCondState);
4362 TheCondState.TheCond = AsmCond::IfCond;
4363
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004364 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004365 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004366 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004367 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004368
4369 if (getLexer().isNot(AsmToken::Comma))
4370 return TokError("unexpected token in '.ifc' directive");
4371
4372 Lex();
4373
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004374 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004375
4376 if (getLexer().isNot(AsmToken::EndOfStatement))
4377 return TokError("unexpected token in '.ifc' directive");
4378
4379 Lex();
4380
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004381 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004382 TheCondState.Ignore = !TheCondState.CondMet;
4383 }
4384
4385 return false;
4386}
4387
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004388/// parseDirectiveIfeqs
4389/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004390bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004391 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004392 if (ExpectEqual)
4393 TokError("expected string parameter for '.ifeqs' directive");
4394 else
4395 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004396 eatToEndOfStatement();
4397 return true;
4398 }
4399
4400 StringRef String1 = getTok().getStringContents();
4401 Lex();
4402
4403 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004404 if (ExpectEqual)
4405 TokError("expected comma after first string for '.ifeqs' directive");
4406 else
4407 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004408 eatToEndOfStatement();
4409 return true;
4410 }
4411
4412 Lex();
4413
4414 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004415 if (ExpectEqual)
4416 TokError("expected string parameter for '.ifeqs' directive");
4417 else
4418 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004419 eatToEndOfStatement();
4420 return true;
4421 }
4422
4423 StringRef String2 = getTok().getStringContents();
4424 Lex();
4425
4426 TheCondStack.push_back(TheCondState);
4427 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004428 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004429 TheCondState.Ignore = !TheCondState.CondMet;
4430
4431 return false;
4432}
4433
Jim Grosbach4b905842013-09-20 23:08:21 +00004434/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004435/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004436bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004437 StringRef Name;
4438 TheCondStack.push_back(TheCondState);
4439 TheCondState.TheCond = AsmCond::IfCond;
4440
4441 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004442 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004443 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004444 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004445 return TokError("expected identifier after '.ifdef'");
4446
4447 Lex();
4448
Jim Grosbach6f482002015-05-18 18:43:14 +00004449 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004450
4451 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004452 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004453 else
Craig Topper353eda42014-04-24 06:44:33 +00004454 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004455 TheCondState.Ignore = !TheCondState.CondMet;
4456 }
4457
4458 return false;
4459}
4460
Jim Grosbach4b905842013-09-20 23:08:21 +00004461/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004462/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004463bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004464 if (TheCondState.TheCond != AsmCond::IfCond &&
4465 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004466 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4467 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004468 TheCondState.TheCond = AsmCond::ElseIfCond;
4469
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004470 bool LastIgnoreState = false;
4471 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004472 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004473 if (LastIgnoreState || TheCondState.CondMet) {
4474 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004475 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004476 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004477 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004478 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004479 return true;
4480
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004481 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004482 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004483
Sean Callanan686ed8d2010-01-19 20:22:31 +00004484 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004485 TheCondState.CondMet = ExprValue;
4486 TheCondState.Ignore = !TheCondState.CondMet;
4487 }
4488
4489 return false;
4490}
4491
Jim Grosbach4b905842013-09-20 23:08:21 +00004492/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004493/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004494bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004495 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004496 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004497
Sean Callanan686ed8d2010-01-19 20:22:31 +00004498 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004499
4500 if (TheCondState.TheCond != AsmCond::IfCond &&
4501 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004502 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4503 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004504 TheCondState.TheCond = AsmCond::ElseCond;
4505 bool LastIgnoreState = false;
4506 if (!TheCondStack.empty())
4507 LastIgnoreState = TheCondStack.back().Ignore;
4508 if (LastIgnoreState || TheCondState.CondMet)
4509 TheCondState.Ignore = true;
4510 else
4511 TheCondState.Ignore = false;
4512
4513 return false;
4514}
4515
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004516/// parseDirectiveEnd
4517/// ::= .end
4518bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4519 if (getLexer().isNot(AsmToken::EndOfStatement))
4520 return TokError("unexpected token in '.end' directive");
4521
4522 Lex();
4523
4524 while (Lexer.isNot(AsmToken::Eof))
4525 Lex();
4526
4527 return false;
4528}
4529
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004530/// parseDirectiveError
4531/// ::= .err
4532/// ::= .error [string]
4533bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4534 if (!TheCondStack.empty()) {
4535 if (TheCondStack.back().Ignore) {
4536 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004537 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004538 }
4539 }
4540
4541 if (!WithMessage)
4542 return Error(L, ".err encountered");
4543
4544 StringRef Message = ".error directive invoked in source file";
4545 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4546 if (Lexer.isNot(AsmToken::String)) {
4547 TokError(".error argument must be a string");
4548 eatToEndOfStatement();
4549 return true;
4550 }
4551
4552 Message = getTok().getStringContents();
4553 Lex();
4554 }
4555
4556 Error(L, Message);
4557 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004558}
4559
Nico Weber404012b2014-07-24 16:26:06 +00004560/// parseDirectiveWarning
4561/// ::= .warning [string]
4562bool AsmParser::parseDirectiveWarning(SMLoc L) {
4563 if (!TheCondStack.empty()) {
4564 if (TheCondStack.back().Ignore) {
4565 eatToEndOfStatement();
4566 return false;
4567 }
4568 }
4569
4570 StringRef Message = ".warning directive invoked in source file";
4571 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4572 if (Lexer.isNot(AsmToken::String)) {
4573 TokError(".warning argument must be a string");
4574 eatToEndOfStatement();
4575 return true;
4576 }
4577
4578 Message = getTok().getStringContents();
4579 Lex();
4580 }
4581
4582 Warning(L, Message);
4583 return false;
4584}
4585
Jim Grosbach4b905842013-09-20 23:08:21 +00004586/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004587/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004588bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004589 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004590 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004591
Sean Callanan686ed8d2010-01-19 20:22:31 +00004592 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004593
Jim Grosbach4b905842013-09-20 23:08:21 +00004594 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004595 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4596 ".else");
4597 if (!TheCondStack.empty()) {
4598 TheCondState = TheCondStack.back();
4599 TheCondStack.pop_back();
4600 }
4601
4602 return false;
4603}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004604
Eli Bendersky17233942013-01-15 22:59:42 +00004605void AsmParser::initializeDirectiveKindMap() {
4606 DirectiveKindMap[".set"] = DK_SET;
4607 DirectiveKindMap[".equ"] = DK_EQU;
4608 DirectiveKindMap[".equiv"] = DK_EQUIV;
4609 DirectiveKindMap[".ascii"] = DK_ASCII;
4610 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4611 DirectiveKindMap[".string"] = DK_STRING;
4612 DirectiveKindMap[".byte"] = DK_BYTE;
4613 DirectiveKindMap[".short"] = DK_SHORT;
4614 DirectiveKindMap[".value"] = DK_VALUE;
4615 DirectiveKindMap[".2byte"] = DK_2BYTE;
4616 DirectiveKindMap[".long"] = DK_LONG;
4617 DirectiveKindMap[".int"] = DK_INT;
4618 DirectiveKindMap[".4byte"] = DK_4BYTE;
4619 DirectiveKindMap[".quad"] = DK_QUAD;
4620 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004621 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004622 DirectiveKindMap[".single"] = DK_SINGLE;
4623 DirectiveKindMap[".float"] = DK_FLOAT;
4624 DirectiveKindMap[".double"] = DK_DOUBLE;
4625 DirectiveKindMap[".align"] = DK_ALIGN;
4626 DirectiveKindMap[".align32"] = DK_ALIGN32;
4627 DirectiveKindMap[".balign"] = DK_BALIGN;
4628 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4629 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4630 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4631 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4632 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4633 DirectiveKindMap[".org"] = DK_ORG;
4634 DirectiveKindMap[".fill"] = DK_FILL;
4635 DirectiveKindMap[".zero"] = DK_ZERO;
4636 DirectiveKindMap[".extern"] = DK_EXTERN;
4637 DirectiveKindMap[".globl"] = DK_GLOBL;
4638 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004639 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4640 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4641 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4642 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4643 DirectiveKindMap[".reference"] = DK_REFERENCE;
4644 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4645 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4646 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4647 DirectiveKindMap[".comm"] = DK_COMM;
4648 DirectiveKindMap[".common"] = DK_COMMON;
4649 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4650 DirectiveKindMap[".abort"] = DK_ABORT;
4651 DirectiveKindMap[".include"] = DK_INCLUDE;
4652 DirectiveKindMap[".incbin"] = DK_INCBIN;
4653 DirectiveKindMap[".code16"] = DK_CODE16;
4654 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4655 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004656 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004657 DirectiveKindMap[".irp"] = DK_IRP;
4658 DirectiveKindMap[".irpc"] = DK_IRPC;
4659 DirectiveKindMap[".endr"] = DK_ENDR;
4660 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4661 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4662 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4663 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004664 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4665 DirectiveKindMap[".ifge"] = DK_IFGE;
4666 DirectiveKindMap[".ifgt"] = DK_IFGT;
4667 DirectiveKindMap[".ifle"] = DK_IFLE;
4668 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004669 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004670 DirectiveKindMap[".ifb"] = DK_IFB;
4671 DirectiveKindMap[".ifnb"] = DK_IFNB;
4672 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004673 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004674 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004675 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004676 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4677 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4678 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4679 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4680 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004681 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004682 DirectiveKindMap[".endif"] = DK_ENDIF;
4683 DirectiveKindMap[".skip"] = DK_SKIP;
4684 DirectiveKindMap[".space"] = DK_SPACE;
4685 DirectiveKindMap[".file"] = DK_FILE;
4686 DirectiveKindMap[".line"] = DK_LINE;
4687 DirectiveKindMap[".loc"] = DK_LOC;
4688 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004689 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
4690 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4691 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004692 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
David Majnemer408b5e62016-02-05 01:55:49 +00004693 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004694 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4695 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004696 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4697 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4698 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4699 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4700 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4701 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4702 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4703 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4704 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4705 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4706 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4707 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4708 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4709 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4710 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4711 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4712 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4713 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4714 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4715 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4716 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004717 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004718 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4719 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4720 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004721 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004722 DirectiveKindMap[".endm"] = DK_ENDM;
4723 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4724 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004725 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004726 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004727 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004728 DirectiveKindMap[".reloc"] = DK_RELOC;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004729}
4730
Jim Grosbach4b905842013-09-20 23:08:21 +00004731MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004732 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004733
Rafael Espindola34b9c512012-06-03 23:57:14 +00004734 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004735 for (;;) {
4736 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004737 if (getLexer().is(AsmToken::Eof)) {
4738 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004739 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004740 }
4741
Rafael Espindola34b9c512012-06-03 23:57:14 +00004742 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004743 (getTok().getIdentifier() == ".rept" ||
4744 getTok().getIdentifier() == ".irp" ||
4745 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004746 ++NestLevel;
4747 }
4748
4749 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004750 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004751 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004752 EndToken = getTok();
4753 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004754 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4755 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004756 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004757 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004758 break;
4759 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004760 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004761 }
4762
Rafael Espindola34b9c512012-06-03 23:57:14 +00004763 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004764 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004765 }
4766
4767 const char *BodyStart = StartToken.getLoc().getPointer();
4768 const char *BodyEnd = EndToken.getLoc().getPointer();
4769 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4770
Rafael Espindola34b9c512012-06-03 23:57:14 +00004771 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004772 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004773 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004774}
4775
Jim Grosbach4b905842013-09-20 23:08:21 +00004776void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004777 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004778 OS << ".endr\n";
4779
Rafael Espindola3560ff22014-08-27 20:03:13 +00004780 std::unique_ptr<MemoryBuffer> Instantiation =
4781 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004782
Rafael Espindola34b9c512012-06-03 23:57:14 +00004783 // Create the macro instantiation object and add to the current macro
4784 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004785 MacroInstantiation *MI = new MacroInstantiation(
4786 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004787 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004788
Rafael Espindola34b9c512012-06-03 23:57:14 +00004789 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004790 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004791 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004792 Lex();
4793}
4794
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004795/// parseDirectiveRept
4796/// ::= .rep | .rept count
4797bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004798 const MCExpr *CountExpr;
4799 SMLoc CountLoc = getTok().getLoc();
4800 if (parseExpression(CountExpr))
4801 return true;
4802
Rafael Espindola34b9c512012-06-03 23:57:14 +00004803 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004804 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004805 eatToEndOfStatement();
4806 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4807 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004808
4809 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004810 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004811
4812 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004813 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004814
4815 // Eat the end of statement.
4816 Lex();
4817
4818 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004819 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004820 if (!M)
4821 return true;
4822
4823 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4824 // to hold the macro body with substitutions.
4825 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004826 raw_svector_ostream OS(Buf);
4827 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004828 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4829 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004830 return true;
4831 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004832 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004833
4834 return false;
4835}
4836
Jim Grosbach4b905842013-09-20 23:08:21 +00004837/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004838/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004839bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004840 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004841
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004842 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004843 return TokError("expected identifier in '.irp' directive");
4844
Rafael Espindola768b41c2012-06-15 14:02:34 +00004845 if (Lexer.isNot(AsmToken::Comma))
4846 return TokError("expected comma in '.irp' directive");
4847
4848 Lex();
4849
Eli Bendersky38274122013-01-14 23:22:36 +00004850 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004851 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004852 return true;
4853
4854 // Eat the end of statement.
4855 Lex();
4856
4857 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004858 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004859 if (!M)
4860 return true;
4861
4862 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4863 // to hold the macro body with substitutions.
4864 SmallString<256> Buf;
4865 raw_svector_ostream OS(Buf);
4866
Craig Topper84008482015-10-10 05:38:14 +00004867 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004868 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4869 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004870 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004871 return true;
4872 }
4873
Jim Grosbach4b905842013-09-20 23:08:21 +00004874 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004875
4876 return false;
4877}
4878
Jim Grosbach4b905842013-09-20 23:08:21 +00004879/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004880/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004881bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004882 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004883
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004884 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004885 return TokError("expected identifier in '.irpc' directive");
4886
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004887 if (Lexer.isNot(AsmToken::Comma))
4888 return TokError("expected comma in '.irpc' directive");
4889
4890 Lex();
4891
Eli Bendersky38274122013-01-14 23:22:36 +00004892 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004893 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004894 return true;
4895
4896 if (A.size() != 1 || A.front().size() != 1)
4897 return TokError("unexpected token in '.irpc' directive");
4898
4899 // Eat the end of statement.
4900 Lex();
4901
4902 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004903 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004904 if (!M)
4905 return true;
4906
4907 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4908 // to hold the macro body with substitutions.
4909 SmallString<256> Buf;
4910 raw_svector_ostream OS(Buf);
4911
4912 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004913 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004914 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004915 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004916
Toma Tabacu217116e2015-04-27 10:50:29 +00004917 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4918 // This is undocumented, but GAS seems to support it.
4919 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004920 return true;
4921 }
4922
Jim Grosbach4b905842013-09-20 23:08:21 +00004923 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004924
4925 return false;
4926}
4927
Jim Grosbach4b905842013-09-20 23:08:21 +00004928bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004929 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004930 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004931
4932 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004933 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004934 assert(getLexer().is(AsmToken::EndOfStatement));
4935
Jim Grosbach4b905842013-09-20 23:08:21 +00004936 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004937 return false;
4938}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004939
Jim Grosbach4b905842013-09-20 23:08:21 +00004940bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004941 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004942 const MCExpr *Value;
4943 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004944 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004945 return true;
4946 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4947 if (!MCE)
4948 return Error(ExprLoc, "unexpected expression in _emit");
4949 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004950 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004951 return Error(ExprLoc, "literal value out of range for directive");
4952
Craig Topper7d5b2312015-10-10 05:25:02 +00004953 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004954 return false;
4955}
4956
Jim Grosbach4b905842013-09-20 23:08:21 +00004957bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004958 const MCExpr *Value;
4959 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004960 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004961 return true;
4962 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4963 if (!MCE)
4964 return Error(ExprLoc, "unexpected expression in align");
4965 uint64_t IntValue = MCE->getValue();
4966 if (!isPowerOf2_64(IntValue))
4967 return Error(ExprLoc, "literal value not a power of two greater then zero");
4968
Craig Topper7d5b2312015-10-10 05:25:02 +00004969 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004970 return false;
4971}
4972
Chad Rosierf43fcf52013-02-13 21:27:17 +00004973// We are comparing pointers, but the pointers are relative to a single string.
4974// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004975static int rewritesSort(const AsmRewrite *AsmRewriteA,
4976 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004977 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4978 return -1;
4979 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4980 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004981
Chad Rosierfce4fab2013-04-08 17:43:47 +00004982 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4983 // rewrite to the same location. Make sure the SizeDirective rewrite is
4984 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4985 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004986 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4987 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004988 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004989
Jim Grosbach4b905842013-09-20 23:08:21 +00004990 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4991 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004992 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004993 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004994}
4995
Jim Grosbach4b905842013-09-20 23:08:21 +00004996bool AsmParser::parseMSInlineAsm(
4997 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4998 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4999 SmallVectorImpl<std::string> &Constraints,
5000 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5001 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00005002 SmallVector<void *, 4> InputDecls;
5003 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00005004 SmallVector<bool, 4> InputDeclsAddressOf;
5005 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00005006 SmallVector<std::string, 4> InputConstraints;
5007 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005008 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00005009
Benjamin Kramer1a136112013-02-15 20:37:21 +00005010 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005011
5012 // Prime the lexer.
5013 Lex();
5014
5015 // While we have input, parse each statement.
5016 unsigned InputIdx = 0;
5017 unsigned OutputIdx = 0;
5018 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005019 // Parse curly braces marking block start/end
5020 if (parseCurlyBlockScope(AsmStrRewrites))
5021 continue;
5022
Eli Friedman0f4871d2012-10-22 23:58:19 +00005023 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005024 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00005025 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00005026
Chad Rosier149e8e02012-12-12 22:45:52 +00005027 if (Info.ParseError)
5028 return true;
5029
Benjamin Kramer1a136112013-02-15 20:37:21 +00005030 if (Info.Opcode == ~0U)
5031 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005032
Benjamin Kramer1a136112013-02-15 20:37:21 +00005033 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005034
Benjamin Kramer1a136112013-02-15 20:37:21 +00005035 // Build the list of clobbers, outputs and inputs.
5036 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005037 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005038
Benjamin Kramer1a136112013-02-15 20:37:21 +00005039 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005040 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005041 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005042
Benjamin Kramer1a136112013-02-15 20:37:21 +00005043 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005044 if (Operand.isReg() && !Operand.needAddressOf() &&
5045 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005046 unsigned NumDefs = Desc.getNumDefs();
5047 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005048 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5049 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005050 continue;
5051 }
5052
5053 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005054 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005055 if (SymName.empty())
5056 continue;
5057
David Blaikie960ea3f2014-06-08 16:18:35 +00005058 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005059 if (!OpDecl)
5060 continue;
5061
5062 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005063 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005064 if (isOutput) {
5065 ++InputIdx;
5066 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005067 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005068 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005069 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005070 } else {
5071 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005072 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5073 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005074 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005075 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005076 }
Reid Kleckneree088972013-12-10 18:27:32 +00005077
5078 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005079 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5080 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005081 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005082 }
5083
5084 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005085 NumOutputs = OutputDecls.size();
5086 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005087
5088 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005089 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5090 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5091 ClobberRegs.end());
5092 Clobbers.assign(ClobberRegs.size(), std::string());
5093 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5094 raw_string_ostream OS(Clobbers[I]);
5095 IP->printRegName(OS, ClobberRegs[I]);
5096 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005097
5098 // Merge the various outputs and inputs. Output are expected first.
5099 if (NumOutputs || NumInputs) {
5100 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005101 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005102 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005103 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005104 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005105 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005106 }
5107 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005108 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005109 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005110 }
5111 }
5112
5113 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005114 std::string AsmStringIR;
5115 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005116 StringRef ASMString =
5117 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5118 const char *AsmStart = ASMString.begin();
5119 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005120 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005121 for (const AsmRewrite &AR : AsmStrRewrites) {
5122 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005123 if (Kind == AOK_Delete)
5124 continue;
5125
David Majnemer8114c1a2014-06-23 02:17:16 +00005126 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005127 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005128
Chad Rosier120eefd2013-03-19 17:32:17 +00005129 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005130 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005131 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005132
Chad Rosier37e755c2012-10-23 17:43:43 +00005133 // Skip the original expression.
5134 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005135 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005136 continue;
5137 }
5138
Chad Rosierff10ed12013-04-12 16:26:42 +00005139 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005140 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005141 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005142 default:
5143 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005144 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005145 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005146 break;
5147 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005148 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005149 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005150 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005151 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005152 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005153 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005154 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005155 break;
5156 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005157 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005158 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005159 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005160 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005161 default: break;
5162 case 8: OS << "byte ptr "; break;
5163 case 16: OS << "word ptr "; break;
5164 case 32: OS << "dword ptr "; break;
5165 case 64: OS << "qword ptr "; break;
5166 case 80: OS << "xword ptr "; break;
5167 case 128: OS << "xmmword ptr "; break;
5168 case 256: OS << "ymmword ptr "; break;
5169 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005170 break;
5171 case AOK_Emit:
5172 OS << ".byte";
5173 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005174 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005175 // MS alignment directives are measured in bytes. If the native assembler
5176 // measures alignment in bytes, we can pass it straight through.
5177 OS << ".align";
5178 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5179 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005180
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005181 // Alignment is in log2 form, so print that instead and skip the original
5182 // immediate.
5183 unsigned Val = AR.Val;
5184 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005185 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005186 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5187 break;
5188 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005189 case AOK_EVEN:
5190 OS << ".even";
5191 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005192 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005193 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005194 OS.flush();
5195 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005196 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005197 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005198 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005199 case AOK_EndOfStatement:
5200 OS << "\n\t";
5201 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005202 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005203
Chad Rosier8bce6642012-10-18 15:49:34 +00005204 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005205 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005206 }
5207
5208 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005209 if (AsmStart != AsmEnd)
5210 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005211
5212 AsmString = OS.str();
5213 return false;
5214}
5215
Pete Cooper80d21cb2015-06-22 19:35:57 +00005216namespace llvm {
5217namespace MCParserUtils {
5218
5219/// Returns whether the given symbol is used anywhere in the given expression,
5220/// or subexpressions.
5221static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5222 switch (Value->getKind()) {
5223 case MCExpr::Binary: {
5224 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5225 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5226 isSymbolUsedInExpression(Sym, BE->getRHS());
5227 }
5228 case MCExpr::Target:
5229 case MCExpr::Constant:
5230 return false;
5231 case MCExpr::SymbolRef: {
5232 const MCSymbol &S =
5233 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5234 if (S.isVariable())
5235 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5236 return &S == Sym;
5237 }
5238 case MCExpr::Unary:
5239 return isSymbolUsedInExpression(
5240 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5241 }
5242
5243 llvm_unreachable("Unknown expr kind!");
5244}
5245
5246bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5247 MCAsmParser &Parser, MCSymbol *&Sym,
5248 const MCExpr *&Value) {
5249 MCAsmLexer &Lexer = Parser.getLexer();
5250
5251 // FIXME: Use better location, we should use proper tokens.
5252 SMLoc EqualLoc = Lexer.getLoc();
5253
5254 if (Parser.parseExpression(Value)) {
5255 Parser.TokError("missing expression");
5256 Parser.eatToEndOfStatement();
5257 return true;
5258 }
5259
5260 // Note: we don't count b as used in "a = b". This is to allow
5261 // a = b
5262 // b = c
5263
5264 if (Lexer.isNot(AsmToken::EndOfStatement))
5265 return Parser.TokError("unexpected token in assignment");
5266
5267 // Eat the end of statement marker.
5268 Parser.Lex();
5269
5270 // Validate that the LHS is allowed to be a variable (either it has not been
5271 // used as a symbol, or it is an absolute symbol).
5272 Sym = Parser.getContext().lookupSymbol(Name);
5273 if (Sym) {
5274 // Diagnose assignment to a label.
5275 //
5276 // FIXME: Diagnostics. Note the location of the definition as a label.
5277 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5278 if (isSymbolUsedInExpression(Sym, Value))
5279 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005280 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5281 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005282 ; // Allow redefinitions of undefined symbols only used in directives.
5283 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5284 ; // Allow redefinitions of variables that haven't yet been used.
5285 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5286 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5287 else if (!Sym->isVariable())
5288 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5289 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5290 return Parser.Error(EqualLoc,
5291 "invalid reassignment of non-absolute variable '" +
5292 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005293 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005294 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005295 return false;
5296 } else
5297 Sym = Parser.getContext().getOrCreateSymbol(Name);
5298
5299 Sym->setRedefinable(allow_redef);
5300
5301 return false;
5302}
5303
5304} // namespace MCParserUtils
5305} // namespace llvm
5306
Daniel Dunbar01e36072010-07-17 02:26:10 +00005307/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005308MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5309 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005310 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005311}