blob: 5e174de8a9335cdf9a4ac36a15cbd5fa07233c9e [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"
Evan Cheng76792992011-07-20 05:58:47 +000031#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000032#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000033#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000034#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000035#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000036#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000037#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000038#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000039#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000040#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000041#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000042#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000043#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000044#include <set>
45#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000046#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000047using namespace llvm;
48
Eric Christophera7c32732012-12-18 00:30:54 +000049MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000050
Daniel Dunbar86033402010-07-12 17:54:38 +000051namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000052/// \brief Helper types for tracking macro definitions.
53typedef std::vector<AsmToken> MCAsmMacroArgument;
54typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000055
56struct MCAsmMacroParameter {
57 StringRef Name;
58 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000059 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000060 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000061
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000062 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000063};
64
Eli Benderskya313ae62013-01-16 18:56:50 +000065typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
66
67struct MCAsmMacro {
68 StringRef Name;
69 StringRef Body;
70 MCAsmMacroParameters Parameters;
71
72public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000073 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
74 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000075};
76
Daniel Dunbar43235712010-07-18 18:54:11 +000077/// \brief Helper class for storing information about an active macro
78/// instantiation.
79struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000080 /// The location of the instantiation.
81 SMLoc InstantiationLoc;
82
Daniel Dunbar40f1d852012-12-01 01:38:48 +000083 /// The buffer where parsing should resume upon instantiation completion.
84 int ExitBuffer;
85
Daniel Dunbar43235712010-07-18 18:54:11 +000086 /// The location where parsing should resume upon instantiation completion.
87 SMLoc ExitLoc;
88
Nico Weber155dccd12014-07-24 17:08:39 +000089 /// The depth of TheCondStack at the start of the instantiation.
90 size_t CondStackDepth;
91
Daniel Dunbar43235712010-07-18 18:54:11 +000092public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000093 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000094};
95
Eli Friedman0f4871d2012-10-22 23:58:19 +000096struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000097 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000098 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000099
Jim Grosbach4b905842013-09-20 23:08:21 +0000100 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101 unsigned Opcode;
102
Jim Grosbach4b905842013-09-20 23:08:21 +0000103 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000104 bool ParseError;
105
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 SmallVectorImpl<AsmRewrite> *AsmRewrites;
107
Craig Topper353eda42014-04-24 06:44:33 +0000108 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000110 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111};
112
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000113/// \brief The concrete assembly parser instance.
114class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000115 AsmParser(const AsmParser &) = delete;
116 void operator=(const AsmParser &) = delete;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000117private:
118 AsmLexer Lexer;
119 MCContext &Ctx;
120 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000121 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000122 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000123 SourceMgr::DiagHandlerTy SavedDiagHandler;
124 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000125 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000126
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127 /// This is the current buffer index we're lexing from as managed by the
128 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000129 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130
131 AsmCond TheCondState;
132 std::vector<AsmCond> TheCondStack;
133
Jim Grosbach4b905842013-09-20 23:08:21 +0000134 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000135 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000136 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000137 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000138
Jim Grosbach4b905842013-09-20 23:08:21 +0000139 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000140 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000143 std::vector<MacroInstantiation*> ActiveMacros;
144
Jim Grosbach4b905842013-09-20 23:08:21 +0000145 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000146 std::deque<MCAsmMacro> MacroLikeBodies;
147
Daniel Dunbar828984f2010-07-18 18:38:02 +0000148 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000149 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000150
Toma Tabacu217116e2015-04-27 10:50:29 +0000151 /// \brief Keeps track of how many .macro's have been instantiated.
152 unsigned NumOfMacroInstantiations;
153
Daniel Dunbar43325c42010-09-09 22:42:56 +0000154 /// Flag tracking whether any errors have been encountered.
155 unsigned HadError : 1;
156
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000157 /// The values from the last parsed cpp hash file line comment if any.
158 StringRef CppHashFilename;
159 int64_t CppHashLineNumber;
160 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000161 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000162 /// When generating dwarf for assembly source files we need to calculate the
163 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000164 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000165 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
166 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000167 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000168 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000169
Devang Patela173ee52012-01-31 18:14:05 +0000170 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
171 unsigned AssemblerDialect;
172
Jim Grosbach4b905842013-09-20 23:08:21 +0000173 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000174 bool IsDarwin;
175
Jim Grosbach4b905842013-09-20 23:08:21 +0000176 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000177 bool ParsingInlineAsm;
178
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000180 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000182 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183
Craig Topper59be68f2014-03-08 07:14:16 +0000184 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185
Craig Topper59be68f2014-03-08 07:14:16 +0000186 void addDirectiveHandler(StringRef Directive,
187 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000188 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189 }
190
Toma Tabacu11e14a92015-04-21 11:50:52 +0000191 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
192 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
193 }
194
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000195public:
196 /// @name MCAsmParser Interface
197 /// {
198
Craig Topper59be68f2014-03-08 07:14:16 +0000199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Craig Topper59be68f2014-03-08 07:14:16 +0000222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000237 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
238 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000239 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000240
Jim Grosbach4b905842013-09-20 23:08:21 +0000241 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000242 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000243 bool parseIdentifier(StringRef &Res) override;
244 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000245
Craig Topper59be68f2014-03-08 07:14:16 +0000246 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000247 /// }
248
249private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000250
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000251 bool parseStatement(ParseStatementInfo &Info,
252 MCAsmParserSemaCallback *SI);
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void eatToEndOfLine();
254 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000255
Jim Grosbach4b905842013-09-20 23:08:21 +0000256 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000257 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000258 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000259 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000260 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000261 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000262
Eli Benderskya313ae62013-01-16 18:56:50 +0000263 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000267 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000268
269 /// \brief Lookup a previously defined macro.
270 /// \param Name Macro name.
271 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000275 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
280 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000283 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000284 ///
285 /// \param M The macro.
286 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000290 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
David Majnemer91fc4c22014-01-29 18:57:46 +0000292 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000293 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
295 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 void printMacroInstantiations();
299 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000300 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000301 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000302 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000303 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 /// \brief Enter the specified file. This returns true on failure.
306 bool enterIncludeFile(const std::string &Filename);
307
308 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000309 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000310 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000311
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000312 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000313 /// current token is not set; clients should ensure Lex() is called
314 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000315 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000316 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000317 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000318 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000319
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000320 /// \brief Parse up to the end of statement and a return the contents from the
321 /// current token until the end of the statement; the current token on exit
322 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000323 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000324
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325 /// \brief Parse until the end of a statement or a comma is encountered,
326 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000330 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000331
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000332 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
333 MCBinaryExpr::Opcode &Kind);
334
Jim Grosbach4b905842013-09-20 23:08:21 +0000335 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
336 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
337 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000338
Jim Grosbach4b905842013-09-20 23:08:21 +0000339 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000340
Eli Bendersky17233942013-01-15 22:59:42 +0000341 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000342 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000343 DK_NO_DIRECTIVE, // Placeholder
344 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000345 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
346 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000347 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000348 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000349 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000350 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
351 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
352 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
353 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000354 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000355 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
356 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000357 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
358 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
359 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
360 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
361 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
362 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000363 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000364 DK_MACROS_ON, DK_MACROS_OFF,
365 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000366 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000367 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000368 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000369 };
370
Jim Grosbach4b905842013-09-20 23:08:21 +0000371 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000372 /// directives parsed by this class.
373 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000374
375 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
377 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000378 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
380 bool parseDirectiveFill(); // ".fill"
381 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000382 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
384 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000385 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000387
Eli Bendersky17233942013-01-15 22:59:42 +0000388 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveFile(SMLoc DirectiveLoc);
390 bool parseDirectiveLine();
391 bool parseDirectiveLoc();
392 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000393
394 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000395 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000396 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000397 bool parseDirectiveCFISections();
398 bool parseDirectiveCFIStartProc();
399 bool parseDirectiveCFIEndProc();
400 bool parseDirectiveCFIDefCfaOffset();
401 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIAdjustCfaOffset();
403 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
406 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
407 bool parseDirectiveCFIRememberState();
408 bool parseDirectiveCFIRestoreState();
409 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
410 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
411 bool parseDirectiveCFIEscape();
412 bool parseDirectiveCFISignalFrame();
413 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000414
415 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000417 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveEndMacro(StringRef Directive);
419 bool parseDirectiveMacro(SMLoc DirectiveLoc);
420 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000426 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000428
Eli Bendersky17233942013-01-15 22:59:42 +0000429 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000431
432 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000440
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveAbort(); // ".abort"
442 bool parseDirectiveInclude(); // ".include"
443 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000444
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000445 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
446 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000447 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000449 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000451 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
452 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000453 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
455 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
456 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
457 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000458 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000459
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000461 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000462
Rafael Espindola34b9c512012-06-03 23:57:14 +0000463 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
465 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000466 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000467 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000468 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
469 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
470 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000471
Chad Rosierc7f552c2013-02-12 21:33:51 +0000472 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000473 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000474 size_t Len);
475
476 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000477 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000478
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000479 // "end"
480 bool parseDirectiveEnd(SMLoc DirectiveLoc);
481
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000482 // ".err" or ".error"
483 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000484
Nico Weber404012b2014-07-24 16:26:06 +0000485 // ".warning"
486 bool parseDirectiveWarning(SMLoc DirectiveLoc);
487
Eli Bendersky17233942013-01-15 22:59:42 +0000488 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000489};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000490}
Daniel Dunbar86033402010-07-12 17:54:38 +0000491
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000492namespace llvm {
493
494extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000495extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000496extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000497
498}
499
Chris Lattnerc35681b2010-01-19 19:46:13 +0000500enum { DEFAULT_ADDRSPACE = 0 };
501
David Blaikie9f380a32015-03-16 18:06:57 +0000502AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
503 const MCAsmInfo &MAI)
504 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
505 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Alp Tokera55b95b2014-07-06 10:33:31 +0000506 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000507 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000508 // Save the old handler.
509 SavedDiagHandler = SrcMgr.getDiagHandler();
510 SavedDiagContext = SrcMgr.getDiagContext();
511 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000512 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000513 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000514
Daniel Dunbarc5011082010-07-12 18:12:02 +0000515 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000516 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
517 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000518 PlatformParser.reset(createCOFFAsmParser());
519 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000520 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000521 PlatformParser.reset(createDarwinAsmParser());
522 IsDarwin = true;
523 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000524 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000525 PlatformParser.reset(createELFAsmParser());
526 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000527 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000528
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000529 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000530 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000531
532 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000533}
534
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000536 assert((HadError || ActiveMacros.empty()) &&
537 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000538}
539
Jim Grosbach4b905842013-09-20 23:08:21 +0000540void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000541 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000542 for (std::vector<MacroInstantiation *>::const_reverse_iterator
543 it = ActiveMacros.rbegin(),
544 ie = ActiveMacros.rend();
545 it != ie; ++it)
546 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000547 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000548}
549
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000550void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
551 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
552 printMacroInstantiations();
553}
554
Chris Lattnera3a06812011-10-16 04:47:35 +0000555bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000556 if(getTargetParser().getTargetOptions().MCNoWarn)
557 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000558 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000559 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000560 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
561 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000562 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000563}
564
Chris Lattnera3a06812011-10-16 04:47:35 +0000565bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000566 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000567 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
568 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000569 return true;
570}
571
Jim Grosbach4b905842013-09-20 23:08:21 +0000572bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000573 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000574 unsigned NewBuf =
575 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
576 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000578
Sean Callanan7a77eae2010-01-21 00:19:58 +0000579 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000580 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000581 return false;
582}
Daniel Dunbar43235712010-07-18 18:54:11 +0000583
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000584/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000585/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000586/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000587bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000589 unsigned NewBuf =
590 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
591 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000592 return true;
593
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000594 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000595 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000596 return false;
597}
598
Alp Tokera55b95b2014-07-06 10:33:31 +0000599void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
600 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000601 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
602 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000603}
604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605const AsmToken &AsmParser::Lex() {
606 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 if (tok->is(AsmToken::Eof)) {
609 // If this is the end of an included file, pop the parent file off the
610 // include stack.
611 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
612 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000613 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000614 tok = &Lexer.Lex();
615 }
616 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000617
Sean Callanan7a77eae2010-01-21 00:19:58 +0000618 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000619 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000620
Sean Callanan7a77eae2010-01-21 00:19:58 +0000621 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000622}
623
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000624bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000625 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000626 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000627 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000628
Chris Lattner36e02122009-06-21 20:54:55 +0000629 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000630 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000631
632 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000633 AsmCond StartingCondState = TheCondState;
634
Kevin Enderby6469fc22011-11-01 22:27:22 +0000635 // If we are generating dwarf for assembly source files save the initial text
636 // section and generate a .file directive.
637 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000638 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000639 if (!Sec->getBeginSymbol()) {
640 MCSymbol *SectionStartSym = getContext().createTempSymbol();
641 getStreamer().EmitLabel(SectionStartSym);
642 Sec->setBeginSymbol(SectionStartSym);
643 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000644 bool InsertResult = getContext().addGenDwarfSection(Sec);
645 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000646 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000647 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
648 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000649 }
650
Chris Lattner73f36112009-07-02 21:53:43 +0000651 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000652 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000653 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000654 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000655 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000656
Daniel Dunbar43325c42010-09-09 22:42:56 +0000657 // We had an error, validate that one was emitted and recover by skipping to
658 // the next line.
659 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000660 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000661 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000662
663 if (TheCondState.TheCond != StartingCondState.TheCond ||
664 TheCondState.Ignore != StartingCondState.Ignore)
665 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666
667 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000668 const auto &LineTables = getContext().getMCDwarfLineTables();
669 if (!LineTables.empty()) {
670 unsigned Index = 0;
671 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
672 if (File.Name.empty() && Index != 0)
673 TokError("unassigned file number: " + Twine(Index) +
674 " for .file directives");
675 ++Index;
676 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000677 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000678
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 // Check to see that all assembler local symbols were actually defined.
680 // Targets that don't do subsections via symbols may not want this, though,
681 // so conservatively exclude them. Only do this if we're finalizing, though,
682 // as otherwise we won't necessarilly have seen everything yet.
683 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
684 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
685 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000686 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000687 i != e; ++i) {
688 MCSymbol *Sym = i->getValue();
689 // Variable symbols may not be marked as defined, so check those
690 // explicitly. If we know it's a variable, we have a definition for
691 // the purposes of this check.
692 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
693 // FIXME: We would really like to refer back to where the symbol was
694 // first referenced for a source location. We need to add something
695 // to track that. Currently, we just point to the end of the file.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000696 printMessage(getLexer().getLoc(), SourceMgr::DK_Error,
697 "assembler local symbol '" + Sym->getName() +
698 "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000699 }
700 }
701
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000702 // Finalize the output stream if there are no errors and if the client wants
703 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000704 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000705 Out.Finish();
706
Chris Lattner73f36112009-07-02 21:53:43 +0000707 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000708}
709
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000711 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000712 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000713 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000714 }
715}
716
Jim Grosbach4b905842013-09-20 23:08:21 +0000717/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000718void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000719 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000720 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000721
Chris Lattnere5074c42009-06-22 01:29:09 +0000722 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000723 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000724 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000725}
726
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000727StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000728 const char *Start = getTok().getLoc().getPointer();
729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000731 Lex();
732
733 const char *End = getTok().getLoc().getPointer();
734 return StringRef(Start, End - Start);
735}
Chris Lattner78db3622009-06-22 05:51:26 +0000736
Jim Grosbach4b905842013-09-20 23:08:21 +0000737StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000738 const char *Start = getTok().getLoc().getPointer();
739
740 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000741 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000742 Lex();
743
744 const char *End = getTok().getLoc().getPointer();
745 return StringRef(Start, End - Start);
746}
747
Jim Grosbach4b905842013-09-20 23:08:21 +0000748/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749/// NOTE: This assumes the leading '(' has already been consumed.
750///
751/// parenexpr ::= expr)
752///
Jim Grosbach4b905842013-09-20 23:08:21 +0000753bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
754 if (parseExpression(Res))
755 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000756 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000757 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000758 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000759 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000760 return false;
761}
Chris Lattner78db3622009-06-22 05:51:26 +0000762
Jim Grosbach4b905842013-09-20 23:08:21 +0000763/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764/// NOTE: This assumes the leading '[' has already been consumed.
765///
766/// bracketexpr ::= expr]
767///
Jim Grosbach4b905842013-09-20 23:08:21 +0000768bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
769 if (parseExpression(Res))
770 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000771 if (Lexer.isNot(AsmToken::RBrac))
772 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000773 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000774 Lex();
775 return false;
776}
777
Jim Grosbach4b905842013-09-20 23:08:21 +0000778/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000779/// primaryexpr ::= (parenexpr
780/// primaryexpr ::= symbol
781/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000782/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000783/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000784bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000785 SMLoc FirstTokenLoc = getLexer().getLoc();
786 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
787 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000788 default:
789 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000790 // If we have an error assume that we've already handled it.
791 case AsmToken::Error:
792 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000793 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000794 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000795 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000796 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000797 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000798 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000799 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000800 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000801 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000802 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000803 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000804 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 if (FirstTokenKind == AsmToken::Dollar) {
806 if (Lexer.getMAI().getDollarIsPC()) {
807 // This is a '$' reference, which references the current PC. Emit a
808 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000809 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000811 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000812 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000813 EndLoc = FirstTokenLoc;
814 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000815 }
816 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000817 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000818 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000819 // Parse symbol variant
820 std::pair<StringRef, StringRef> Split;
821 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000822 if (FirstTokenKind == AsmToken::String) {
823 if (Lexer.is(AsmToken::At)) {
824 Lexer.Lex(); // eat @
825 SMLoc AtLoc = getLexer().getLoc();
826 StringRef VName;
827 if (parseIdentifier(VName))
828 return Error(AtLoc, "expected symbol variant after '@'");
829
830 Split = std::make_pair(Identifier, VName);
831 }
832 } else {
833 Split = Identifier.split('@');
834 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000835 } else if (Lexer.is(AsmToken::LParen)) {
836 Lexer.Lex(); // eat (
837 StringRef VName;
838 parseIdentifier(VName);
839 if (Lexer.isNot(AsmToken::RParen)) {
840 return Error(Lexer.getTok().getLoc(),
841 "unexpected token in variant, expected ')'");
842 }
843 Lexer.Lex(); // eat )
844 Split = std::make_pair(Identifier, VName);
845 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000846
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000847 EndLoc = SMLoc::getFromPointer(Identifier.end());
848
Daniel Dunbard20cda02009-10-16 01:34:54 +0000849 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000850 StringRef SymbolName = Identifier;
851 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000852
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000853 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000854 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000855 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000856 if (Variant != MCSymbolRefExpr::VK_Invalid) {
857 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000858 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000859 Variant = MCSymbolRefExpr::VK_None;
860 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000861 return Error(SMLoc::getFromPointer(Split.second.begin()),
862 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000863 }
864 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000865
Jim Grosbach6f482002015-05-18 18:43:14 +0000866 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000867
Daniel Dunbard20cda02009-10-16 01:34:54 +0000868 // If this is an absolute variable reference, substitute it now to preserve
869 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000870 if (Sym->isVariable() &&
871 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000872 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000873 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000874
Vedant Kumar86dbd922015-08-31 17:44:53 +0000875 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000876 return false;
877 }
878
879 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000880 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000881 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000882 }
David Woodhousef42a6662014-02-01 16:20:54 +0000883 case AsmToken::BigNum:
884 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 case AsmToken::Integer: {
886 SMLoc Loc = getTok().getLoc();
887 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000888 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000889 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000890 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 // Look for 'b' or 'f' following an Integer as a directional label
892 if (Lexer.getKind() == AsmToken::Identifier) {
893 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 // Lookup the symbol variant if used.
895 std::pair<StringRef, StringRef> Split = IDVal.split('@');
896 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
897 if (Split.first.size() != IDVal.size()) {
898 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000899 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000900 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000901 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000902 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000903 if (IDVal == "f" || IDVal == "b") {
904 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000905 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000906 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000907 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000908 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000909 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000910 Lex(); // Eat identifier.
911 }
912 }
Chris Lattner78db3622009-06-22 05:51:26 +0000913 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000914 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000915 case AsmToken::Real: {
916 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000917 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000918 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000919 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000920 Lex(); // Eat token.
921 return false;
922 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000923 case AsmToken::Dot: {
924 // This is a '.' reference, which references the current PC. Emit a
925 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000926 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000927 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000928 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000929 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000930 Lex(); // Eat identifier.
931 return false;
932 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000933 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000934 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000935 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000936 case AsmToken::LBrac:
937 if (!PlatformParser->HasBracketExpressions())
938 return TokError("brackets expression not supported on this target");
939 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000940 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000945 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000947 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000948 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000949 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000950 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000951 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000953 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000954 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000956 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000957 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000958 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000959 }
960}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000961
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000962bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000963 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000964 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000965}
966
Daniel Dunbar55f16672010-09-17 02:47:07 +0000967const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000968AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000969 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000970 // Ask the target implementation about this expression first.
971 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
972 if (NewE)
973 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000974 // Recurse over the given expression, rebuilding it to apply the given variant
975 // if there is exactly one symbol.
976 switch (E->getKind()) {
977 case MCExpr::Target:
978 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000979 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000980
981 case MCExpr::SymbolRef: {
982 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
983
984 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000985 TokError("invalid variant on expression '" + getTok().getIdentifier() +
986 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 return E;
988 }
989
Jim Grosbach13760bd2015-05-30 01:25:56 +0000990 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000991 }
992
993 case MCExpr::Unary: {
994 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000995 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000997 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000998 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999 }
1000
1001 case MCExpr::Binary: {
1002 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001003 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1004 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001005
1006 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001007 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001008
Jim Grosbach4b905842013-09-20 23:08:21 +00001009 if (!LHS)
1010 LHS = BE->getLHS();
1011 if (!RHS)
1012 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013
Jim Grosbach13760bd2015-05-30 01:25:56 +00001014 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001015 }
1016 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001017
Craig Toppera2886c22012-02-07 05:05:23 +00001018 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001019}
1020
Jim Grosbach4b905842013-09-20 23:08:21 +00001021/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001022///
Jim Grosbachbd164242011-08-20 16:24:13 +00001023/// expr ::= expr &&,|| expr -> lowest.
1024/// expr ::= expr |,^,&,! expr
1025/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1026/// expr ::= expr <<,>> expr
1027/// expr ::= expr +,- expr
1028/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001029/// expr ::= primaryexpr
1030///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001031bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001032 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001033 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001035 return true;
1036
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 // As a special case, we support 'a op b @ modifier' by rewriting the
1038 // expression to include the modifier. This is inefficient, but in general we
1039 // expect users to use 'a@modifier op b'.
1040 if (Lexer.getKind() == AsmToken::At) {
1041 Lex();
1042
1043 if (Lexer.isNot(AsmToken::Identifier))
1044 return TokError("unexpected symbol modifier following '@'");
1045
1046 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 if (Variant == MCSymbolRefExpr::VK_Invalid)
1049 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1050
Jim Grosbach4b905842013-09-20 23:08:21 +00001051 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001052 if (!ModifiedRes) {
1053 return TokError("invalid modifier '" + getTok().getIdentifier() +
1054 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001055 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001056
Daniel Dunbar55f16672010-09-17 02:47:07 +00001057 Res = ModifiedRes;
1058 Lex();
1059 }
1060
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001061 // Try to constant fold it up front, if possible.
1062 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001063 if (Res->evaluateAsAbsolute(Value))
1064 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001065
1066 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001067}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001068
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001069bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001070 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001071 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001072}
1073
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001074bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1075 SMLoc &EndLoc) {
1076 if (parseParenExpr(Res, EndLoc))
1077 return true;
1078
1079 for (; ParenDepth > 0; --ParenDepth) {
1080 if (parseBinOpRHS(1, Res, EndLoc))
1081 return true;
1082
1083 // We don't Lex() the last RParen.
1084 // This is the same behavior as parseParenExpression().
1085 if (ParenDepth - 1 > 0) {
1086 if (Lexer.isNot(AsmToken::RParen))
1087 return TokError("expected ')' in parentheses expression");
1088 EndLoc = Lexer.getTok().getEndLoc();
1089 Lex();
1090 }
1091 }
1092 return false;
1093}
1094
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001095bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001096 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001097
Daniel Dunbar75630b32009-06-30 02:10:03 +00001098 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001099 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001100 return true;
1101
Jim Grosbach13760bd2015-05-30 01:25:56 +00001102 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001103 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001104
1105 return false;
1106}
1107
Ahmed Bougacha457852f2015-04-28 00:17:39 +00001108unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1109 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001110 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001111 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001112 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001113
Jim Grosbach4b905842013-09-20 23:08:21 +00001114 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001115 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001116 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001117 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001118 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001119 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001120 return 1;
1121
Jim Grosbach4b905842013-09-20 23:08:21 +00001122 // Low Precedence: |, &, ^
1123 //
1124 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001125 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001126 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001127 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001130 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001133 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001134
Jim Grosbach4b905842013-09-20 23:08:21 +00001135 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001136 case AsmToken::EqualEqual:
1137 Kind = MCBinaryExpr::EQ;
1138 return 3;
1139 case AsmToken::ExclaimEqual:
1140 case AsmToken::LessGreater:
1141 Kind = MCBinaryExpr::NE;
1142 return 3;
1143 case AsmToken::Less:
1144 Kind = MCBinaryExpr::LT;
1145 return 3;
1146 case AsmToken::LessEqual:
1147 Kind = MCBinaryExpr::LTE;
1148 return 3;
1149 case AsmToken::Greater:
1150 Kind = MCBinaryExpr::GT;
1151 return 3;
1152 case AsmToken::GreaterEqual:
1153 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001154 return 3;
1155
Jim Grosbach4b905842013-09-20 23:08:21 +00001156 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001157 case AsmToken::LessLess:
1158 Kind = MCBinaryExpr::Shl;
1159 return 4;
1160 case AsmToken::GreaterGreater:
Ahmed Bougacha177c1482015-04-28 00:21:32 +00001161 Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001162 return 4;
1163
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001165 case AsmToken::Plus:
1166 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001167 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001168 case AsmToken::Minus:
1169 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001170 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001171
Jim Grosbach4b905842013-09-20 23:08:21 +00001172 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001173 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001174 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001175 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001176 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001177 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001178 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001179 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001180 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001181 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001182 }
1183}
1184
Jim Grosbach4b905842013-09-20 23:08:21 +00001185/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001186/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001187bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001188 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001189 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001190 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001191 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001192
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001193 // If the next token is lower precedence than we are allowed to eat, return
1194 // successfully with what we ate already.
1195 if (TokPrec < Precedence)
1196 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001197
Sean Callanan686ed8d2010-01-19 20:22:31 +00001198 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001199
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001200 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001201 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001202 if (parsePrimaryExpr(RHS, EndLoc))
1203 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001204
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001205 // If BinOp binds less tightly with RHS than the operator after RHS, let
1206 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001207 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001208 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001209 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1210 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001211
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001212 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001213 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001214 }
1215}
1216
Chris Lattner36e02122009-06-21 20:54:55 +00001217/// ParseStatement:
1218/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001219/// ::= Label* Directive ...Operands... EndOfStatement
1220/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001221bool AsmParser::parseStatement(ParseStatementInfo &Info,
1222 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001223 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001224 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001225 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001226 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001227 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001228
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001229 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001230 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001231 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001232 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001233 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001234 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001235 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001236 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001237
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001238 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001239 if (Lexer.is(AsmToken::Integer)) {
1240 LocalLabelVal = getTok().getIntVal();
1241 if (LocalLabelVal < 0) {
1242 if (!TheCondState.Ignore)
1243 return TokError("unexpected token at start of statement");
1244 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001245 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001246 IDVal = getTok().getString();
1247 Lex(); // Consume the integer token to be used as an identifier token.
1248 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001249 if (!TheCondState.Ignore)
1250 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001251 }
1252 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001253 } else if (Lexer.is(AsmToken::Dot)) {
1254 // Treat '.' as a valid identifier in this context.
1255 Lex();
1256 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001257 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001258 if (!TheCondState.Ignore)
1259 return TokError("unexpected token at start of statement");
1260 IDVal = "";
1261 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001262
Chris Lattner926885c2010-04-17 18:14:27 +00001263 // Handle conditional assembly here before checking for skipping. We
1264 // have to do this so that .endif isn't skipped in a ".if 0" block for
1265 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001266 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001267 DirectiveKindMap.find(IDVal);
1268 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1269 ? DK_NO_DIRECTIVE
1270 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001271 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001272 default:
1273 break;
1274 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001275 case DK_IFEQ:
1276 case DK_IFGE:
1277 case DK_IFGT:
1278 case DK_IFLE:
1279 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001280 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001281 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001282 case DK_IFB:
1283 return parseDirectiveIfb(IDLoc, true);
1284 case DK_IFNB:
1285 return parseDirectiveIfb(IDLoc, false);
1286 case DK_IFC:
1287 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001288 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001289 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001290 case DK_IFNC:
1291 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001292 case DK_IFNES:
1293 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001294 case DK_IFDEF:
1295 return parseDirectiveIfdef(IDLoc, true);
1296 case DK_IFNDEF:
1297 case DK_IFNOTDEF:
1298 return parseDirectiveIfdef(IDLoc, false);
1299 case DK_ELSEIF:
1300 return parseDirectiveElseIf(IDLoc);
1301 case DK_ELSE:
1302 return parseDirectiveElse(IDLoc);
1303 case DK_ENDIF:
1304 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001305 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001306
Eli Bendersky88024712013-01-16 19:32:36 +00001307 // Ignore the statement if in the middle of inactive conditional
1308 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001309 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001310 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001311 return false;
1312 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001313
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001314 // FIXME: Recurse on local labels?
1315
1316 // See what kind of statement we have.
1317 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001318 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001319 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001320
Chris Lattner36e02122009-06-21 20:54:55 +00001321 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001322 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001323
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001324 // Diagnose attempt to use '.' as a label.
1325 if (IDVal == ".")
1326 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1327
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001328 // Diagnose attempt to use a variable as a label.
1329 //
1330 // FIXME: Diagnostics. Note the location of the definition as a label.
1331 // FIXME: This doesn't diagnose assignment to a symbol which has been
1332 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001333 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001334 if (LocalLabelVal == -1) {
1335 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001336 StringRef RewrittenLabel =
1337 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1338 assert(RewrittenLabel.size() &&
1339 "We should have an internal name here.");
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001340 Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
1341 IDVal.size(), RewrittenLabel));
1342 IDVal = RewrittenLabel;
1343 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001344 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001345 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001346 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001347
1348 Sym->redefineIfPossible();
1349
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001350 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001351 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001352
Daniel Dunbare73b2672009-08-26 22:13:22 +00001353 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001354 if (!ParsingInlineAsm)
1355 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001356
Kevin Enderbye7739d42011-12-09 18:09:40 +00001357 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001358 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001359 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001360 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1361 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001362
Tim Northover1744d0a2013-10-25 12:49:50 +00001363 getTargetParser().onLabelParsed(Sym);
1364
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001365 // Consume any end of statement token, if present, to avoid spurious
1366 // AddBlankLine calls().
1367 if (Lexer.is(AsmToken::EndOfStatement)) {
1368 Lex();
1369 if (Lexer.is(AsmToken::Eof))
1370 return false;
1371 }
1372
Eli Friedman0f4871d2012-10-22 23:58:19 +00001373 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001374 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001375
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001376 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001377 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001378 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001379
Jim Grosbach4b905842013-09-20 23:08:21 +00001380 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001381
1382 default: // Normal instruction or directive.
1383 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001384 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001385
1386 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001387 if (areMacrosEnabled())
1388 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1389 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001390 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001391
Michael J. Spencer530ce852010-10-09 11:00:50 +00001392 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001393
Eli Bendersky17233942013-01-15 22:59:42 +00001394 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001395 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001396 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001397 //
Eli Bendersky17233942013-01-15 22:59:42 +00001398 // 1. The target-specific assembly parser. Some directives are target
1399 // specific or may potentially behave differently on certain targets.
1400 // 2. Asm parser extensions. For example, platform-specific parsers
1401 // (like the ELF parser) register themselves as extensions.
1402 // 3. The generic directive parser implemented by this class. These are
1403 // all the directives that behave in a target and platform independent
1404 // manner, or at least have a default behavior that's shared between
1405 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001406
Eli Bendersky17233942013-01-15 22:59:42 +00001407 // First query the target-specific parser. It will return 'true' if it
1408 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001409 if (!getTargetParser().ParseDirective(ID))
1410 return false;
1411
Alp Tokercb402912014-01-24 17:20:08 +00001412 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001413 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001414 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1415 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001416 if (Handler.first)
1417 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1418
1419 // Finally, if no one else is interested in this directive, it must be
1420 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001421 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001422 default:
1423 break;
1424 case DK_SET:
1425 case DK_EQU:
1426 return parseDirectiveSet(IDVal, true);
1427 case DK_EQUIV:
1428 return parseDirectiveSet(IDVal, false);
1429 case DK_ASCII:
1430 return parseDirectiveAscii(IDVal, false);
1431 case DK_ASCIZ:
1432 case DK_STRING:
1433 return parseDirectiveAscii(IDVal, true);
1434 case DK_BYTE:
1435 return parseDirectiveValue(1);
1436 case DK_SHORT:
1437 case DK_VALUE:
1438 case DK_2BYTE:
1439 return parseDirectiveValue(2);
1440 case DK_LONG:
1441 case DK_INT:
1442 case DK_4BYTE:
1443 return parseDirectiveValue(4);
1444 case DK_QUAD:
1445 case DK_8BYTE:
1446 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001447 case DK_OCTA:
1448 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001449 case DK_SINGLE:
1450 case DK_FLOAT:
1451 return parseDirectiveRealValue(APFloat::IEEEsingle);
1452 case DK_DOUBLE:
1453 return parseDirectiveRealValue(APFloat::IEEEdouble);
1454 case DK_ALIGN: {
1455 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1456 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1457 }
1458 case DK_ALIGN32: {
1459 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1460 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1461 }
1462 case DK_BALIGN:
1463 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1464 case DK_BALIGNW:
1465 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1466 case DK_BALIGNL:
1467 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1468 case DK_P2ALIGN:
1469 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1470 case DK_P2ALIGNW:
1471 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1472 case DK_P2ALIGNL:
1473 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1474 case DK_ORG:
1475 return parseDirectiveOrg();
1476 case DK_FILL:
1477 return parseDirectiveFill();
1478 case DK_ZERO:
1479 return parseDirectiveZero();
1480 case DK_EXTERN:
1481 eatToEndOfStatement(); // .extern is the default, ignore it.
1482 return false;
1483 case DK_GLOBL:
1484 case DK_GLOBAL:
1485 return parseDirectiveSymbolAttribute(MCSA_Global);
1486 case DK_LAZY_REFERENCE:
1487 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1488 case DK_NO_DEAD_STRIP:
1489 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1490 case DK_SYMBOL_RESOLVER:
1491 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1492 case DK_PRIVATE_EXTERN:
1493 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1494 case DK_REFERENCE:
1495 return parseDirectiveSymbolAttribute(MCSA_Reference);
1496 case DK_WEAK_DEFINITION:
1497 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1498 case DK_WEAK_REFERENCE:
1499 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1500 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1501 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1502 case DK_COMM:
1503 case DK_COMMON:
1504 return parseDirectiveComm(/*IsLocal=*/false);
1505 case DK_LCOMM:
1506 return parseDirectiveComm(/*IsLocal=*/true);
1507 case DK_ABORT:
1508 return parseDirectiveAbort();
1509 case DK_INCLUDE:
1510 return parseDirectiveInclude();
1511 case DK_INCBIN:
1512 return parseDirectiveIncbin();
1513 case DK_CODE16:
1514 case DK_CODE16GCC:
1515 return TokError(Twine(IDVal) + " not supported yet");
1516 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001517 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001518 case DK_IRP:
1519 return parseDirectiveIrp(IDLoc);
1520 case DK_IRPC:
1521 return parseDirectiveIrpc(IDLoc);
1522 case DK_ENDR:
1523 return parseDirectiveEndr(IDLoc);
1524 case DK_BUNDLE_ALIGN_MODE:
1525 return parseDirectiveBundleAlignMode();
1526 case DK_BUNDLE_LOCK:
1527 return parseDirectiveBundleLock();
1528 case DK_BUNDLE_UNLOCK:
1529 return parseDirectiveBundleUnlock();
1530 case DK_SLEB128:
1531 return parseDirectiveLEB128(true);
1532 case DK_ULEB128:
1533 return parseDirectiveLEB128(false);
1534 case DK_SPACE:
1535 case DK_SKIP:
1536 return parseDirectiveSpace(IDVal);
1537 case DK_FILE:
1538 return parseDirectiveFile(IDLoc);
1539 case DK_LINE:
1540 return parseDirectiveLine();
1541 case DK_LOC:
1542 return parseDirectiveLoc();
1543 case DK_STABS:
1544 return parseDirectiveStabs();
1545 case DK_CFI_SECTIONS:
1546 return parseDirectiveCFISections();
1547 case DK_CFI_STARTPROC:
1548 return parseDirectiveCFIStartProc();
1549 case DK_CFI_ENDPROC:
1550 return parseDirectiveCFIEndProc();
1551 case DK_CFI_DEF_CFA:
1552 return parseDirectiveCFIDefCfa(IDLoc);
1553 case DK_CFI_DEF_CFA_OFFSET:
1554 return parseDirectiveCFIDefCfaOffset();
1555 case DK_CFI_ADJUST_CFA_OFFSET:
1556 return parseDirectiveCFIAdjustCfaOffset();
1557 case DK_CFI_DEF_CFA_REGISTER:
1558 return parseDirectiveCFIDefCfaRegister(IDLoc);
1559 case DK_CFI_OFFSET:
1560 return parseDirectiveCFIOffset(IDLoc);
1561 case DK_CFI_REL_OFFSET:
1562 return parseDirectiveCFIRelOffset(IDLoc);
1563 case DK_CFI_PERSONALITY:
1564 return parseDirectiveCFIPersonalityOrLsda(true);
1565 case DK_CFI_LSDA:
1566 return parseDirectiveCFIPersonalityOrLsda(false);
1567 case DK_CFI_REMEMBER_STATE:
1568 return parseDirectiveCFIRememberState();
1569 case DK_CFI_RESTORE_STATE:
1570 return parseDirectiveCFIRestoreState();
1571 case DK_CFI_SAME_VALUE:
1572 return parseDirectiveCFISameValue(IDLoc);
1573 case DK_CFI_RESTORE:
1574 return parseDirectiveCFIRestore(IDLoc);
1575 case DK_CFI_ESCAPE:
1576 return parseDirectiveCFIEscape();
1577 case DK_CFI_SIGNAL_FRAME:
1578 return parseDirectiveCFISignalFrame();
1579 case DK_CFI_UNDEFINED:
1580 return parseDirectiveCFIUndefined(IDLoc);
1581 case DK_CFI_REGISTER:
1582 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001583 case DK_CFI_WINDOW_SAVE:
1584 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 case DK_MACROS_ON:
1586 case DK_MACROS_OFF:
1587 return parseDirectiveMacrosOnOff(IDVal);
1588 case DK_MACRO:
1589 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001590 case DK_EXITM:
1591 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001592 case DK_ENDM:
1593 case DK_ENDMACRO:
1594 return parseDirectiveEndMacro(IDVal);
1595 case DK_PURGEM:
1596 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001597 case DK_END:
1598 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001599 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001600 return parseDirectiveError(IDLoc, false);
1601 case DK_ERROR:
1602 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001603 case DK_WARNING:
1604 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001605 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001606
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001607 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001608 }
Chris Lattner36e02122009-06-21 20:54:55 +00001609
Chad Rosierc7f552c2013-02-12 21:33:51 +00001610 // __asm _emit or __asm __emit
1611 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1612 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001613 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001614
1615 // __asm align
1616 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001617 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001618
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001619 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001620
Chris Lattner7cbfa442010-05-19 23:34:33 +00001621 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001622 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001623 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001624 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001625 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001626 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001627
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001628 // Dump the parsed representation, if requested.
1629 if (getShowParsedOperands()) {
1630 SmallString<256> Str;
1631 raw_svector_ostream OS(Str);
1632 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001633 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001634 if (i != 0)
1635 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001636 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001637 }
1638 OS << "]";
1639
Jim Grosbach4b905842013-09-20 23:08:21 +00001640 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001641 }
1642
Oliver Stannard8b273082014-06-19 15:52:37 +00001643 // If we are generating dwarf for the current section then generate a .loc
1644 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001645 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001646 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001647 getStreamer().getCurrentSection().first)) {
1648 unsigned Line;
1649 if (ActiveMacros.empty())
1650 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1651 else
Frederic Riss16238d92015-06-25 21:57:33 +00001652 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1653 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001654
Eli Bendersky88024712013-01-16 19:32:36 +00001655 // If we previously parsed a cpp hash file line comment then make sure the
1656 // current Dwarf File is for the CppHashFilename if not then emit the
1657 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001658 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001659 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1660 0, StringRef(), CppHashFilename);
1661 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001662
Jim Grosbach4b905842013-09-20 23:08:21 +00001663 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1664 // cache with the different Loc from the call above we save the last
1665 // info we queried here with SrcMgr.FindLineNumber().
1666 unsigned CppHashLocLineNo;
1667 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1668 CppHashLocLineNo = LastQueryLine;
1669 else {
1670 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1671 LastQueryLine = CppHashLocLineNo;
1672 LastQueryIDLoc = CppHashLoc;
1673 LastQueryBuffer = CppHashBuf;
1674 }
1675 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001676 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001677
Jim Grosbach4b905842013-09-20 23:08:21 +00001678 getStreamer().EmitDwarfLocDirective(
1679 getContext().getGenDwarfFileNumber(), Line, 0,
1680 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1681 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001682 }
1683
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001684 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001685 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001686 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001687 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1688 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001689 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001690 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001691
Chris Lattnera2a9d162010-09-11 16:18:25 +00001692 // Don't skip the rest of the line, the instruction parser is responsible for
1693 // that.
1694 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001695}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001696
Jim Grosbach4b905842013-09-20 23:08:21 +00001697/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001698/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001699void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001700 if (!Lexer.is(AsmToken::EndOfStatement))
1701 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001702 // Eat EOL.
1703 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001704}
1705
Jim Grosbach4b905842013-09-20 23:08:21 +00001706/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001707/// ::= # number "filename"
1708/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001709bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001710 Lex(); // Eat the hash token.
1711
1712 if (getLexer().isNot(AsmToken::Integer)) {
1713 // Consume the line since in cases it is not a well-formed line directive,
1714 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001716 return false;
1717 }
1718
1719 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001720 Lex();
1721
1722 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001723 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001724 return false;
1725 }
1726
1727 StringRef Filename = getTok().getString();
1728 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001729 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001730
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001731 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1732 CppHashLoc = L;
1733 CppHashFilename = Filename;
1734 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001735 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001736
1737 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001738 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001739 return false;
1740}
1741
Jim Grosbach4b905842013-09-20 23:08:21 +00001742/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001743/// for the Filename and LineNo if any in the diagnostic.
1744void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001745 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001746 raw_ostream &OS = errs();
1747
1748 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1749 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001750 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1751 unsigned CppHashBuf =
1752 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001753
Jim Grosbach4b905842013-09-20 23:08:21 +00001754 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001755 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001756 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1757 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1758 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001759 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1760 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001761 }
1762
Eric Christophera7c32732012-12-18 00:30:54 +00001763 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001764 // manager changed or buffer changed (like in a nested include) then just
1765 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001766 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001767 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001768 if (Parser->SavedDiagHandler)
1769 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1770 else
Craig Topper353eda42014-04-24 06:44:33 +00001771 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001772 return;
1773 }
1774
Eric Christophera7c32732012-12-18 00:30:54 +00001775 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001776 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1777 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001778 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001779
1780 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1781 int CppHashLocLineNo =
1782 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001783 int LineNo =
1784 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001785
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1787 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001788 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001789
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001790 if (Parser->SavedDiagHandler)
1791 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1792 else
Craig Topper353eda42014-04-24 06:44:33 +00001793 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001794}
1795
Rafael Espindola2c064482012-08-21 18:29:30 +00001796// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1797// difference being that that function accepts '@' as part of identifiers and
1798// we can't do that. AsmLexer.cpp should probably be changed to handle
1799// '@' as a special case when needed.
1800static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001801 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1802 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001803}
1804
Rafael Espindola34b9c512012-06-03 23:57:14 +00001805bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001806 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001807 ArrayRef<MCAsmMacroArgument> A,
1808 bool EnableAtPseudoVariable, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001810 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001811 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001813
Preston Gurd05500642012-09-19 20:36:12 +00001814 // A macro without parameters is handled differently on Darwin:
1815 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001816 while (!Body.empty()) {
1817 // Scan for the next substitution.
1818 std::size_t End = Body.size(), Pos = 0;
1819 for (; Pos != End; ++Pos) {
1820 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001821 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 // This macro has no parameters, look for $0, $1, etc.
1823 if (Body[Pos] != '$' || Pos + 1 == End)
1824 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001825
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001827 if (Next == '$' || Next == 'n' ||
1828 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 break;
1830 } else {
1831 // This macro has parameters, look for \foo, \bar, etc.
1832 if (Body[Pos] == '\\' && Pos + 1 != End)
1833 break;
1834 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001835 }
1836
1837 // Add the prefix.
1838 OS << Body.slice(0, Pos);
1839
1840 // Check if we reached the end.
1841 if (Pos == End)
1842 break;
1843
Benjamin Kramer513e7442014-02-20 13:36:32 +00001844 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001845 switch (Body[Pos + 1]) {
1846 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001847 case '$':
1848 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001849 break;
1850
Jim Grosbach4b905842013-09-20 23:08:21 +00001851 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852 case 'n':
1853 OS << A.size();
1854 break;
1855
Jim Grosbach4b905842013-09-20 23:08:21 +00001856 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857 default: {
1858 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001859 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001860 if (Index >= A.size())
1861 break;
1862
1863 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001864 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001865 ie = A[Index].end();
1866 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001867 OS << it->getString();
1868 break;
1869 }
1870 }
1871 Pos += 2;
1872 } else {
1873 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001874
1875 // Check for the \@ pseudo-variable.
1876 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001877 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001878 else
1879 while (isIdentifierChar(Body[I]) && I + 1 != End)
1880 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001881
Jim Grosbach4b905842013-09-20 23:08:21 +00001882 const char *Begin = Body.data() + Pos + 1;
1883 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001884 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001885
Toma Tabacu217116e2015-04-27 10:50:29 +00001886 if (Argument == "@") {
1887 OS << NumOfMacroInstantiations;
1888 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001889 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001890 for (; Index < NParameters; ++Index)
1891 if (Parameters[Index].Name == Argument)
1892 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001893
Toma Tabacu217116e2015-04-27 10:50:29 +00001894 if (Index == NParameters) {
1895 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1896 Pos += 3;
1897 else {
1898 OS << '\\' << Argument;
1899 Pos = I;
1900 }
1901 } else {
1902 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1903 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1904 ie = A[Index].end();
1905 it != ie; ++it)
1906 // We expect no quotes around the string's contents when
1907 // parsing for varargs.
1908 if (it->getKind() != AsmToken::String || VarargParameter)
1909 OS << it->getString();
1910 else
1911 OS << it->getStringContents();
1912
1913 Pos += 1 + Argument.size();
1914 }
Preston Gurd05500642012-09-19 20:36:12 +00001915 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001916 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001917 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001918 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001919 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001920
Rafael Espindola1134ab232011-06-05 02:43:45 +00001921 return false;
1922}
Daniel Dunbar43235712010-07-18 18:54:11 +00001923
Nico Weber2a8f9222014-07-24 16:29:04 +00001924MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001925 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001926 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001927 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001928
Jim Grosbach4b905842013-09-20 23:08:21 +00001929static bool isOperator(AsmToken::TokenKind kind) {
1930 switch (kind) {
1931 default:
1932 return false;
1933 case AsmToken::Plus:
1934 case AsmToken::Minus:
1935 case AsmToken::Tilde:
1936 case AsmToken::Slash:
1937 case AsmToken::Star:
1938 case AsmToken::Dot:
1939 case AsmToken::Equal:
1940 case AsmToken::EqualEqual:
1941 case AsmToken::Pipe:
1942 case AsmToken::PipePipe:
1943 case AsmToken::Caret:
1944 case AsmToken::Amp:
1945 case AsmToken::AmpAmp:
1946 case AsmToken::Exclaim:
1947 case AsmToken::ExclaimEqual:
1948 case AsmToken::Percent:
1949 case AsmToken::Less:
1950 case AsmToken::LessEqual:
1951 case AsmToken::LessLess:
1952 case AsmToken::LessGreater:
1953 case AsmToken::Greater:
1954 case AsmToken::GreaterEqual:
1955 case AsmToken::GreaterGreater:
1956 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001957 }
1958}
1959
David Majnemer16252452014-01-29 00:07:39 +00001960namespace {
1961class AsmLexerSkipSpaceRAII {
1962public:
1963 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1964 Lexer.setSkipSpace(SkipSpace);
1965 }
1966
1967 ~AsmLexerSkipSpaceRAII() {
1968 Lexer.setSkipSpace(true);
1969 }
1970
1971private:
1972 AsmLexer &Lexer;
1973};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001974}
David Majnemer16252452014-01-29 00:07:39 +00001975
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001976bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1977
1978 if (Vararg) {
1979 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1980 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001981 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001982 }
1983 return false;
1984 }
1985
Rafael Espindola768b41c2012-06-15 14:02:34 +00001986 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001987 unsigned AddTokens = 0;
1988
David Majnemer16252452014-01-29 00:07:39 +00001989 // Darwin doesn't use spaces to delmit arguments.
1990 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001991
1992 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001993 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001994 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001995
David Majnemer91fc4c22014-01-29 18:57:46 +00001996 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001997 break;
Preston Gurd05500642012-09-19 20:36:12 +00001998
1999 if (Lexer.is(AsmToken::Space)) {
2000 Lex(); // Eat spaces
2001
2002 // Spaces can delimit parameters, but could also be part an expression.
2003 // If the token after a space is an operator, add the token and the next
2004 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002005 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002006 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00002007 // Check to see whether the token is used as an operator,
2008 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002009 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00002010 if (*NextChar == ' ')
2011 AddTokens = 2;
2012 }
2013
2014 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002015 break;
2016 }
2017 }
2018 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002019
Jim Grosbach4b905842013-09-20 23:08:21 +00002020 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002021 // to be able to fill in the remaining default parameter values
2022 if (Lexer.is(AsmToken::EndOfStatement))
2023 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002024
2025 // Adjust the current parentheses level.
2026 if (Lexer.is(AsmToken::LParen))
2027 ++ParenLevel;
2028 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2029 --ParenLevel;
2030
2031 // Append the token to the current argument list.
2032 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002033 if (AddTokens)
2034 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002035 Lex();
2036 }
Preston Gurd05500642012-09-19 20:36:12 +00002037
Rafael Espindola768b41c2012-06-15 14:02:34 +00002038 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002039 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002040 return false;
2041}
2042
2043// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002044bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002045 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002046 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002047 bool NamedParametersFound = false;
2048 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002049
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002050 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002051 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002052
Rafael Espindola768b41c2012-06-15 14:02:34 +00002053 // Parse two kinds of macro invocations:
2054 // - macros defined without any parameters accept an arbitrary number of them
2055 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002056 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002057 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2058 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002059 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002060 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002061
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002062 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002063 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002064 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002065 eatToEndOfStatement();
2066 return true;
2067 }
2068
2069 if (!Lexer.is(AsmToken::Equal)) {
2070 TokError("expected '=' after formal parameter identifier");
2071 eatToEndOfStatement();
2072 return true;
2073 }
2074 Lex();
2075
2076 NamedParametersFound = true;
2077 }
2078
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002079 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002080 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002081 eatToEndOfStatement();
2082 return true;
2083 }
2084
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002085 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2086 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002087 return true;
2088
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002089 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002090 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002091 unsigned FAI = 0;
2092 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002093 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002094 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002095
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002096 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002097 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002098 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002099 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002100 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002101 return true;
2102 }
2103 PI = FAI;
2104 }
2105
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002106 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002107 if (A.size() <= PI)
2108 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002109 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002110
2111 if (FALocs.size() <= PI)
2112 FALocs.resize(PI + 1);
2113
2114 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002115 }
Jim Grosbach206661622012-07-30 22:44:17 +00002116
Preston Gurd242ed3152012-09-19 20:29:04 +00002117 // At the end of the statement, fill in remaining arguments that have
2118 // default values. If there aren't any, then the next argument is
2119 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002120 if (Lexer.is(AsmToken::EndOfStatement)) {
2121 bool Failure = false;
2122 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2123 if (A[FAI].empty()) {
2124 if (M->Parameters[FAI].Required) {
2125 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2126 "missing value for required parameter "
2127 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2128 Failure = true;
2129 }
2130
2131 if (!M->Parameters[FAI].Value.empty())
2132 A[FAI] = M->Parameters[FAI].Value;
2133 }
2134 }
2135 return Failure;
2136 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002137
2138 if (Lexer.is(AsmToken::Comma))
2139 Lex();
2140 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002141
2142 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002143}
2144
Jim Grosbach4b905842013-09-20 23:08:21 +00002145const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002146 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2147 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002148}
2149
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002150void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2151 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002152}
2153
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002154void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002155
Jim Grosbach4b905842013-09-20 23:08:21 +00002156bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002157 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2158 // this, although we should protect against infinite loops.
2159 if (ActiveMacros.size() == 20)
2160 return TokError("macros cannot be nested more than 20 levels deep");
2161
Eli Bendersky38274122013-01-14 23:22:36 +00002162 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002163 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002164 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002165
Rafael Espindola1134ab232011-06-05 02:43:45 +00002166 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2167 // to hold the macro body with substitutions.
2168 SmallString<256> Buf;
2169 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002170 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002171
Toma Tabacu217116e2015-04-27 10:50:29 +00002172 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002173 return true;
2174
Eli Bendersky38274122013-01-14 23:22:36 +00002175 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002176 // instantiation.
2177 OS << ".endmacro\n";
2178
Rafael Espindola3560ff22014-08-27 20:03:13 +00002179 std::unique_ptr<MemoryBuffer> Instantiation =
2180 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002181
Daniel Dunbar43235712010-07-18 18:54:11 +00002182 // Create the macro instantiation object and add to the current macro
2183 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002184 MacroInstantiation *MI = new MacroInstantiation(
2185 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002186 ActiveMacros.push_back(MI);
2187
Toma Tabacu217116e2015-04-27 10:50:29 +00002188 ++NumOfMacroInstantiations;
2189
Daniel Dunbar43235712010-07-18 18:54:11 +00002190 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002191 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002192 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002193 Lex();
2194
2195 return false;
2196}
2197
Jim Grosbach4b905842013-09-20 23:08:21 +00002198void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002199 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002200 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002201 Lex();
2202
2203 // Pop the instantiation entry.
2204 delete ActiveMacros.back();
2205 ActiveMacros.pop_back();
2206}
2207
Jim Grosbach4b905842013-09-20 23:08:21 +00002208bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002209 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002210 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002211 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002212 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2213 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002214 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002215
Pete Cooper80d21cb2015-06-22 19:35:57 +00002216 if (!Sym) {
2217 // In the case where we parse an expression starting with a '.', we will
2218 // not generate an error, nor will we create a symbol. In this case we
2219 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002220 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002221 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002222
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002223 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002224 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002225 if (NoDeadStrip)
2226 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2227
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002228 return false;
2229}
2230
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002231/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002232/// ::= identifier
2233/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002235 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002236 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2237 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002238 // handle this as a context dependent token, instead we detect adjacent tokens
2239 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2241 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242
Hans Wennborgce69d772013-10-18 20:46:28 +00002243 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 Lex();
2245 if (Lexer.isNot(AsmToken::Identifier))
2246 return true;
2247
Hans Wennborgce69d772013-10-18 20:46:28 +00002248 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2249 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250 return true;
2251
2252 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002253 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002254 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002255 Lex();
2256 return false;
2257 }
2258
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002260 return true;
2261
Sean Callanan936b0d32010-01-19 21:44:56 +00002262 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263
Sean Callanan686ed8d2010-01-19 20:22:31 +00002264 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
2266 return false;
2267}
2268
Jim Grosbach4b905842013-09-20 23:08:21 +00002269/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002270/// ::= .equ identifier ',' expression
2271/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002272/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002273bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002275
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002276 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002277 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002278
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002280 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002281 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002282
Jim Grosbach4b905842013-09-20 23:08:21 +00002283 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284}
2285
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002286bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002288
2289 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002290 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002291 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2292 if (Str[i] != '\\') {
2293 Data += Str[i];
2294 continue;
2295 }
2296
2297 // Recognize escaped characters. Note that this escape semantics currently
2298 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2299 ++i;
2300 if (i == e)
2301 return TokError("unexpected backslash at end of string");
2302
2303 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002304 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002305 // Consume up to three octal characters.
2306 unsigned Value = Str[i] - '0';
2307
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 ++i;
2310 Value = Value * 8 + (Str[i] - '0');
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 ++i;
2314 Value = Value * 8 + (Str[i] - '0');
2315 }
2316 }
2317
2318 if (Value > 255)
2319 return TokError("invalid octal escape sequence (out of range)");
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 continue;
2323 }
2324
2325 // Otherwise recognize individual escapes.
2326 switch (Str[i]) {
2327 default:
2328 // Just reject invalid escape sequences for now.
2329 return TokError("invalid escape sequence (unrecognized character)");
2330
2331 case 'b': Data += '\b'; break;
2332 case 'f': Data += '\f'; break;
2333 case 'n': Data += '\n'; break;
2334 case 'r': Data += '\r'; break;
2335 case 't': Data += '\t'; break;
2336 case '"': Data += '"'; break;
2337 case '\\': Data += '\\'; break;
2338 }
2339 }
2340
2341 return false;
2342}
2343
Jim Grosbach4b905842013-09-20 23:08:21 +00002344/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002345/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002346bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002347 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002348 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002349
Daniel Dunbara10e5192009-06-24 23:30:00 +00002350 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002352 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353
Daniel Dunbaref668c12009-08-14 18:19:52 +00002354 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002355 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357
Rafael Espindola64e1af82013-07-02 15:49:13 +00002358 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Sean Callanan686ed8d2010-01-19 20:22:31 +00002362 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
2364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 break;
2366
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002368 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002369 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 }
2371 }
2372
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 return false;
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002379bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002382
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002384 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002385 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002386 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 return true;
2388
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002389 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002390 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2391 assert(Size <= 8 && "Invalid size");
2392 uint64_t IntValue = MCE->getValue();
2393 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2394 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002395 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002396 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002397 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002399 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002401
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002403 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002405 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 }
2407 }
2408
Sean Callanan686ed8d2010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 return false;
2411}
2412
David Woodhoused6de0d92014-02-01 16:20:59 +00002413/// ParseDirectiveOctaValue
2414/// ::= .octa [ hexconstant (, hexconstant)* ]
2415bool AsmParser::parseDirectiveOctaValue() {
2416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2417 checkForValidSection();
2418
2419 for (;;) {
2420 if (Lexer.getKind() == AsmToken::Error)
2421 return true;
2422 if (Lexer.getKind() != AsmToken::Integer &&
2423 Lexer.getKind() != AsmToken::BigNum)
2424 return TokError("unknown token in expression");
2425
2426 SMLoc ExprLoc = getLexer().getLoc();
2427 APInt IntValue = getTok().getAPIntVal();
2428 Lex();
2429
2430 uint64_t hi, lo;
2431 if (IntValue.isIntN(64)) {
2432 hi = 0;
2433 lo = IntValue.getZExtValue();
2434 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002435 // It might actually have more than 128 bits, but the top ones are zero.
2436 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002437 lo = IntValue.getLoBits(64).getZExtValue();
2438 } else
2439 return Error(ExprLoc, "literal value out of range for directive");
2440
2441 if (MAI.isLittleEndian()) {
2442 getStreamer().EmitIntValue(lo, 8);
2443 getStreamer().EmitIntValue(hi, 8);
2444 } else {
2445 getStreamer().EmitIntValue(hi, 8);
2446 getStreamer().EmitIntValue(lo, 8);
2447 }
2448
2449 if (getLexer().is(AsmToken::EndOfStatement))
2450 break;
2451
2452 // FIXME: Improve diagnostic.
2453 if (getLexer().isNot(AsmToken::Comma))
2454 return TokError("unexpected token in directive");
2455 Lex();
2456 }
2457 }
2458
2459 Lex();
2460 return false;
2461}
2462
Jim Grosbach4b905842013-09-20 23:08:21 +00002463/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002465bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002467 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468
2469 for (;;) {
2470 // We don't truly support arithmetic on floating point expressions, so we
2471 // have to manually parse unary prefixes.
2472 bool IsNeg = false;
2473 if (getLexer().is(AsmToken::Minus)) {
2474 Lex();
2475 IsNeg = true;
2476 } else if (getLexer().is(AsmToken::Plus))
2477 Lex();
2478
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002480 getLexer().isNot(AsmToken::Real) &&
2481 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002482 return TokError("unexpected token in directive");
2483
2484 // Convert to an APFloat.
2485 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002486 StringRef IDVal = getTok().getString();
2487 if (getLexer().is(AsmToken::Identifier)) {
2488 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2489 Value = APFloat::getInf(Semantics);
2490 else if (!IDVal.compare_lower("nan"))
2491 Value = APFloat::getNaN(Semantics, false, ~0);
2492 else
2493 return TokError("invalid floating point literal");
2494 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002495 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002496 return TokError("invalid floating point literal");
2497 if (IsNeg)
2498 Value.changeSign();
2499
2500 // Consume the numeric token.
2501 Lex();
2502
2503 // Emit the value as an integer.
2504 APInt AsInt = Value.bitcastToAPInt();
2505 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002506 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002507
2508 if (getLexer().is(AsmToken::EndOfStatement))
2509 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002510
Daniel Dunbar2af16532010-09-24 01:59:56 +00002511 if (getLexer().isNot(AsmToken::Comma))
2512 return TokError("unexpected token in directive");
2513 Lex();
2514 }
2515 }
2516
2517 Lex();
2518 return false;
2519}
2520
Jim Grosbach4b905842013-09-20 23:08:21 +00002521/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002522/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002523bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002525
2526 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002528 return true;
2529
Rafael Espindolab91bac62010-10-05 19:42:57 +00002530 int64_t Val = 0;
2531 if (getLexer().is(AsmToken::Comma)) {
2532 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002534 return true;
2535 }
2536
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
2538 return TokError("unexpected token in '.zero' directive");
2539
2540 Lex();
2541
Rafael Espindola64e1af82013-07-02 15:49:13 +00002542 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002543
2544 return false;
2545}
2546
Jim Grosbach4b905842013-09-20 23:08:21 +00002547/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002548/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002549bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002550 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002551
David Majnemer522d3db2014-02-01 07:19:38 +00002552 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 return true;
2556
David Majnemer522d3db2014-02-01 07:19:38 +00002557 if (NumValues < 0) {
2558 Warning(RepeatLoc,
2559 "'.fill' directive with negative repeat count has no effect");
2560 NumValues = 0;
2561 }
2562
Roman Divackye33098f2013-09-24 17:44:41 +00002563 int64_t FillSize = 1;
2564 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2568 if (getLexer().isNot(AsmToken::Comma))
2569 return TokError("unexpected token in '.fill' directive");
2570 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002571
David Majnemer522d3db2014-02-01 07:19:38 +00002572 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (parseAbsoluteExpression(FillSize))
2574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002575
Roman Divackye33098f2013-09-24 17:44:41 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2577 if (getLexer().isNot(AsmToken::Comma))
2578 return TokError("unexpected token in '.fill' directive");
2579 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002580
David Majnemer522d3db2014-02-01 07:19:38 +00002581 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillExpr))
2583 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002584
Roman Divackye33098f2013-09-24 17:44:41 +00002585 if (getLexer().isNot(AsmToken::EndOfStatement))
2586 return TokError("unexpected token in '.fill' directive");
2587
2588 Lex();
2589 }
2590 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002591
David Majnemer522d3db2014-02-01 07:19:38 +00002592 if (FillSize < 0) {
2593 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2594 NumValues = 0;
2595 }
2596 if (FillSize > 8) {
2597 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2598 FillSize = 8;
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (!isUInt<32>(FillExpr) && FillSize > 4)
2602 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2603
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002604 if (NumValues > 0) {
2605 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2606 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2607 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2608 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2609 if (NonZeroFillSize < FillSize)
2610 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2611 }
David Majnemer522d3db2014-02-01 07:19:38 +00002612 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002613
2614 return false;
2615}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002616
Jim Grosbach4b905842013-09-20 23:08:21 +00002617/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002618/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002619bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002620 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002621
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002622 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002623 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002624 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002625 return true;
2626
2627 // Parse optional fill expression.
2628 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2630 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002631 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002632 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002633
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002634 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002635 return true;
2636
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002638 return TokError("unexpected token in '.org' directive");
2639 }
2640
Sean Callanan686ed8d2010-01-19 20:22:31 +00002641 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002642
Jim Grosbachb5912772012-01-27 00:37:08 +00002643 // Only limited forms of relocatable expressions are accepted here, it
2644 // has to be relative to the current section. The streamer will return
2645 // 'true' if the expression wasn't evaluatable.
2646 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2647 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002648
2649 return false;
2650}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002651
Jim Grosbach4b905842013-09-20 23:08:21 +00002652/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002654bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002655 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002656
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002657 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002659 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 return true;
2661
2662 SMLoc MaxBytesLoc;
2663 bool HasFillExpr = false;
2664 int64_t FillExpr = 0;
2665 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002666 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2667 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002668 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002669 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002670
2671 // The fill expression can be omitted while specifying a maximum number of
2672 // alignment bytes, e.g:
2673 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002674 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002676 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 return true;
2678 }
2679
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002680 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2681 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002683 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002685 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002686 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002687 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002688
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002689 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002690 return TokError("unexpected token in directive");
2691 }
2692 }
2693
Sean Callanan686ed8d2010-01-19 20:22:31 +00002694 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002696 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698
2699 // Compute alignment in bytes.
2700 if (IsPow2) {
2701 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002702 if (Alignment >= 32) {
2703 Error(AlignmentLoc, "invalid alignment value");
2704 Alignment = 31;
2705 }
2706
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002707 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002708 } else {
2709 // Reject alignments that aren't a power of two, for gas compatibility.
2710 if (!isPowerOf2_64(Alignment))
2711 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002712 }
2713
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002714 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002715 if (MaxBytesLoc.isValid()) {
2716 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002717 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002718 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002719 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 }
2721
2722 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002723 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002724 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002725 MaxBytesToFill = 0;
2726 }
2727 }
2728
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002729 // Check whether we should use optimal code alignment for this .align
2730 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002731 const MCSection *Section = getStreamer().getCurrentSection().first;
2732 assert(Section && "must have section to emit alignment");
2733 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002734 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2735 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002736 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002737 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002738 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002739 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2740 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002741 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002742
2743 return false;
2744}
2745
Jim Grosbach4b905842013-09-20 23:08:21 +00002746/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002747/// ::= .file [number] filename
2748/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002749bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002750 // FIXME: I'm not sure what this is.
2751 int64_t FileNumber = -1;
2752 SMLoc FileNumberLoc = getLexer().getLoc();
2753 if (getLexer().is(AsmToken::Integer)) {
2754 FileNumber = getTok().getIntVal();
2755 Lex();
2756
2757 if (FileNumber < 1)
2758 return TokError("file number less than one");
2759 }
2760
2761 if (getLexer().isNot(AsmToken::String))
2762 return TokError("unexpected token in '.file' directive");
2763
2764 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002765 // Allow the strings to have escaped octal character sequence.
2766 std::string Path = getTok().getString();
2767 if (parseEscapedString(Path))
2768 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002769 Lex();
2770
2771 StringRef Directory;
2772 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002773 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002774 if (getLexer().is(AsmToken::String)) {
2775 if (FileNumber == -1)
2776 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002777 if (parseEscapedString(FilenameData))
2778 return true;
2779 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002780 Directory = Path;
2781 Lex();
2782 } else {
2783 Filename = Path;
2784 }
2785
2786 if (getLexer().isNot(AsmToken::EndOfStatement))
2787 return TokError("unexpected token in '.file' directive");
2788
2789 if (FileNumber == -1)
2790 getStreamer().EmitFileDirective(Filename);
2791 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002792 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002793 Error(DirectiveLoc,
2794 "input can't have .file dwarf directives when -g is "
2795 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002796
David Blaikiec714ef42014-03-17 01:52:11 +00002797 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2798 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002799 Error(FileNumberLoc, "file number already allocated");
2800 }
2801
2802 return false;
2803}
2804
Jim Grosbach4b905842013-09-20 23:08:21 +00002805/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002806/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002807bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002808 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2809 if (getLexer().isNot(AsmToken::Integer))
2810 return TokError("unexpected token in '.line' directive");
2811
2812 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002813 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002814 Lex();
2815
2816 // FIXME: Do something with the .line.
2817 }
2818
2819 if (getLexer().isNot(AsmToken::EndOfStatement))
2820 return TokError("unexpected token in '.line' directive");
2821
2822 return false;
2823}
2824
Jim Grosbach4b905842013-09-20 23:08:21 +00002825/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002826/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2827/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2828/// The first number is a file number, must have been previously assigned with
2829/// a .file directive, the second number is the line number and optionally the
2830/// third number is a column position (zero if not specified). The remaining
2831/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002832bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002833 if (getLexer().isNot(AsmToken::Integer))
2834 return TokError("unexpected token in '.loc' directive");
2835 int64_t FileNumber = getTok().getIntVal();
2836 if (FileNumber < 1)
2837 return TokError("file number less than one in '.loc' directive");
2838 if (!getContext().isValidDwarfFileNumber(FileNumber))
2839 return TokError("unassigned file number in '.loc' directive");
2840 Lex();
2841
2842 int64_t LineNumber = 0;
2843 if (getLexer().is(AsmToken::Integer)) {
2844 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002845 if (LineNumber < 0)
2846 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002847 Lex();
2848 }
2849
2850 int64_t ColumnPos = 0;
2851 if (getLexer().is(AsmToken::Integer)) {
2852 ColumnPos = getTok().getIntVal();
2853 if (ColumnPos < 0)
2854 return TokError("column position less than zero in '.loc' directive");
2855 Lex();
2856 }
2857
2858 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2859 unsigned Isa = 0;
2860 int64_t Discriminator = 0;
2861 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2862 for (;;) {
2863 if (getLexer().is(AsmToken::EndOfStatement))
2864 break;
2865
2866 StringRef Name;
2867 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002868 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002869 return TokError("unexpected token in '.loc' directive");
2870
2871 if (Name == "basic_block")
2872 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2873 else if (Name == "prologue_end")
2874 Flags |= DWARF2_FLAG_PROLOGUE_END;
2875 else if (Name == "epilogue_begin")
2876 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2877 else if (Name == "is_stmt") {
2878 Loc = getTok().getLoc();
2879 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002880 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002881 return true;
2882 // The expression must be the constant 0 or 1.
2883 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2884 int Value = MCE->getValue();
2885 if (Value == 0)
2886 Flags &= ~DWARF2_FLAG_IS_STMT;
2887 else if (Value == 1)
2888 Flags |= DWARF2_FLAG_IS_STMT;
2889 else
2890 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002891 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002892 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2893 }
Craig Topperf15655b2013-04-22 04:22:40 +00002894 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002895 Loc = getTok().getLoc();
2896 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002897 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002898 return true;
2899 // The expression must be a constant greater or equal to 0.
2900 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2901 int Value = MCE->getValue();
2902 if (Value < 0)
2903 return Error(Loc, "isa number less than zero");
2904 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002905 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return Error(Loc, "isa number not a constant value");
2907 }
Craig Topperf15655b2013-04-22 04:22:40 +00002908 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002909 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002910 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002911 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return Error(Loc, "unknown sub-directive in '.loc' directive");
2913 }
2914
2915 if (getLexer().is(AsmToken::EndOfStatement))
2916 break;
2917 }
2918 }
2919
2920 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2921 Isa, Discriminator, StringRef());
2922
2923 return false;
2924}
2925
Jim Grosbach4b905842013-09-20 23:08:21 +00002926/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002927/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002928bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002929 return TokError("unsupported directive '.stabs'");
2930}
2931
Jim Grosbach4b905842013-09-20 23:08:21 +00002932/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002933/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002934bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002935 StringRef Name;
2936 bool EH = false;
2937 bool Debug = false;
2938
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002939 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002940 return TokError("Expected an identifier");
2941
2942 if (Name == ".eh_frame")
2943 EH = true;
2944 else if (Name == ".debug_frame")
2945 Debug = true;
2946
2947 if (getLexer().is(AsmToken::Comma)) {
2948 Lex();
2949
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002950 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002951 return TokError("Expected an identifier");
2952
2953 if (Name == ".eh_frame")
2954 EH = true;
2955 else if (Name == ".debug_frame")
2956 Debug = true;
2957 }
2958
2959 getStreamer().EmitCFISections(EH, Debug);
2960 return false;
2961}
2962
Jim Grosbach4b905842013-09-20 23:08:21 +00002963/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002964/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002965bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002966 StringRef Simple;
2967 if (getLexer().isNot(AsmToken::EndOfStatement))
2968 if (parseIdentifier(Simple) || Simple != "simple")
2969 return TokError("unexpected token in .cfi_startproc directive");
2970
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00002971 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002972 return false;
2973}
2974
Jim Grosbach4b905842013-09-20 23:08:21 +00002975/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002976/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002977bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002978 getStreamer().EmitCFIEndProc();
2979 return false;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// \brief parse register name or number.
2983bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002984 SMLoc DirectiveLoc) {
2985 unsigned RegNo;
2986
2987 if (getLexer().isNot(AsmToken::Integer)) {
2988 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2989 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002990 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002991 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002992 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002993
2994 return false;
2995}
2996
Jim Grosbach4b905842013-09-20 23:08:21 +00002997/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002998/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002999bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003000 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003001 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003002 return true;
3003
3004 if (getLexer().isNot(AsmToken::Comma))
3005 return TokError("unexpected token in directive");
3006 Lex();
3007
3008 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003009 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003010 return true;
3011
3012 getStreamer().EmitCFIDefCfa(Register, Offset);
3013 return false;
3014}
3015
Jim Grosbach4b905842013-09-20 23:08:21 +00003016/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003017/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003018bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003019 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003020 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003021 return true;
3022
3023 getStreamer().EmitCFIDefCfaOffset(Offset);
3024 return false;
3025}
3026
Jim Grosbach4b905842013-09-20 23:08:21 +00003027/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003028/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003029bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003030 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003031 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003032 return true;
3033
3034 if (getLexer().isNot(AsmToken::Comma))
3035 return TokError("unexpected token in directive");
3036 Lex();
3037
3038 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003039 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 getStreamer().EmitCFIRegister(Register1, Register2);
3043 return false;
3044}
3045
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003046/// parseDirectiveCFIWindowSave
3047/// ::= .cfi_window_save
3048bool AsmParser::parseDirectiveCFIWindowSave() {
3049 getStreamer().EmitCFIWindowSave();
3050 return false;
3051}
3052
Jim Grosbach4b905842013-09-20 23:08:21 +00003053/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003054/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003055bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003056 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003057 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003058 return true;
3059
3060 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3061 return false;
3062}
3063
Jim Grosbach4b905842013-09-20 23:08:21 +00003064/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003065/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003066bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003067 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003068 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003069 return true;
3070
3071 getStreamer().EmitCFIDefCfaRegister(Register);
3072 return false;
3073}
3074
Jim Grosbach4b905842013-09-20 23:08:21 +00003075/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003076/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003077bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003078 int64_t Register = 0;
3079 int64_t Offset = 0;
3080
Jim Grosbach4b905842013-09-20 23:08:21 +00003081 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003082 return true;
3083
3084 if (getLexer().isNot(AsmToken::Comma))
3085 return TokError("unexpected token in directive");
3086 Lex();
3087
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003088 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003089 return true;
3090
3091 getStreamer().EmitCFIOffset(Register, Offset);
3092 return false;
3093}
3094
Jim Grosbach4b905842013-09-20 23:08:21 +00003095/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003096/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003097bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003098 int64_t Register = 0;
3099
Jim Grosbach4b905842013-09-20 23:08:21 +00003100 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003101 return true;
3102
3103 if (getLexer().isNot(AsmToken::Comma))
3104 return TokError("unexpected token in directive");
3105 Lex();
3106
3107 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003108 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 getStreamer().EmitCFIRelOffset(Register, Offset);
3112 return false;
3113}
3114
3115static bool isValidEncoding(int64_t Encoding) {
3116 if (Encoding & ~0xff)
3117 return false;
3118
3119 if (Encoding == dwarf::DW_EH_PE_omit)
3120 return true;
3121
3122 const unsigned Format = Encoding & 0xf;
3123 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3124 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3125 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3126 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3127 return false;
3128
3129 const unsigned Application = Encoding & 0x70;
3130 if (Application != dwarf::DW_EH_PE_absptr &&
3131 Application != dwarf::DW_EH_PE_pcrel)
3132 return false;
3133
3134 return true;
3135}
3136
Jim Grosbach4b905842013-09-20 23:08:21 +00003137/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003138/// IsPersonality true for cfi_personality, false for cfi_lsda
3139/// ::= .cfi_personality encoding, [symbol_name]
3140/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003141bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003142 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003143 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003144 return true;
3145 if (Encoding == dwarf::DW_EH_PE_omit)
3146 return false;
3147
3148 if (!isValidEncoding(Encoding))
3149 return TokError("unsupported encoding.");
3150
3151 if (getLexer().isNot(AsmToken::Comma))
3152 return TokError("unexpected token in directive");
3153 Lex();
3154
3155 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003156 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003157 return TokError("expected identifier in directive");
3158
Jim Grosbach6f482002015-05-18 18:43:14 +00003159 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003160
3161 if (IsPersonality)
3162 getStreamer().EmitCFIPersonality(Sym, Encoding);
3163 else
3164 getStreamer().EmitCFILsda(Sym, Encoding);
3165 return false;
3166}
3167
Jim Grosbach4b905842013-09-20 23:08:21 +00003168/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003169/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003170bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003171 getStreamer().EmitCFIRememberState();
3172 return false;
3173}
3174
Jim Grosbach4b905842013-09-20 23:08:21 +00003175/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003176/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003177bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003178 getStreamer().EmitCFIRestoreState();
3179 return false;
3180}
3181
Jim Grosbach4b905842013-09-20 23:08:21 +00003182/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003183/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003184bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003185 int64_t Register = 0;
3186
Jim Grosbach4b905842013-09-20 23:08:21 +00003187 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003188 return true;
3189
3190 getStreamer().EmitCFISameValue(Register);
3191 return false;
3192}
3193
Jim Grosbach4b905842013-09-20 23:08:21 +00003194/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003195/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003196bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003197 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003198 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003199 return true;
3200
3201 getStreamer().EmitCFIRestore(Register);
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003207bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003208 std::string Values;
3209 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003210 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003211 return true;
3212
3213 Values.push_back((uint8_t)CurrValue);
3214
3215 while (getLexer().is(AsmToken::Comma)) {
3216 Lex();
3217
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003218 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003219 return true;
3220
3221 Values.push_back((uint8_t)CurrValue);
3222 }
3223
3224 getStreamer().EmitCFIEscape(Values);
3225 return false;
3226}
3227
Jim Grosbach4b905842013-09-20 23:08:21 +00003228/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003229/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003230bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003231 if (getLexer().isNot(AsmToken::EndOfStatement))
3232 return Error(getLexer().getLoc(),
3233 "unexpected token in '.cfi_signal_frame'");
3234
3235 getStreamer().EmitCFISignalFrame();
3236 return false;
3237}
3238
Jim Grosbach4b905842013-09-20 23:08:21 +00003239/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003240/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003241bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003242 int64_t Register = 0;
3243
Jim Grosbach4b905842013-09-20 23:08:21 +00003244 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003245 return true;
3246
3247 getStreamer().EmitCFIUndefined(Register);
3248 return false;
3249}
3250
Jim Grosbach4b905842013-09-20 23:08:21 +00003251/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003252/// ::= .macros_on
3253/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003254bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003255 if (getLexer().isNot(AsmToken::EndOfStatement))
3256 return Error(getLexer().getLoc(),
3257 "unexpected token in '" + Directive + "' directive");
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003260 return false;
3261}
3262
Jim Grosbach4b905842013-09-20 23:08:21 +00003263/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003264/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003265bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003266 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003267 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003268 return TokError("expected identifier in '.macro' directive");
3269
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003270 if (getLexer().is(AsmToken::Comma))
3271 Lex();
3272
Eli Bendersky17233942013-01-15 22:59:42 +00003273 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003274 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003275
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003276 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003277 return Error(Lexer.getLoc(),
3278 "Vararg parameter '" + Parameters.back().Name +
3279 "' should be last one in the list of parameters.");
3280
David Majnemer91fc4c22014-01-29 18:57:46 +00003281 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003282 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003283 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003284
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003285 if (Lexer.is(AsmToken::Colon)) {
3286 Lex(); // consume ':'
3287
3288 SMLoc QualLoc;
3289 StringRef Qualifier;
3290
3291 QualLoc = Lexer.getLoc();
3292 if (parseIdentifier(Qualifier))
3293 return Error(QualLoc, "missing parameter qualifier for "
3294 "'" + Parameter.Name + "' in macro '" + Name + "'");
3295
3296 if (Qualifier == "req")
3297 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003298 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003299 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003300 else
3301 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3302 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3303 }
3304
David Majnemer91fc4c22014-01-29 18:57:46 +00003305 if (getLexer().is(AsmToken::Equal)) {
3306 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003307
3308 SMLoc ParamLoc;
3309
3310 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003311 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003312 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003313
3314 if (Parameter.Required)
3315 Warning(ParamLoc, "pointless default value for required parameter "
3316 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003317 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003318
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003319 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003320
3321 if (getLexer().is(AsmToken::Comma))
3322 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003323 }
3324
3325 // Eat the end of statement.
3326 Lex();
3327
3328 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003329 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003330
3331 // Lex the macro definition.
3332 for (;;) {
3333 // Check whether we have reached the end of the file.
3334 if (getLexer().is(AsmToken::Eof))
3335 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3336
3337 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003338 if (getLexer().is(AsmToken::Identifier)) {
3339 if (getTok().getIdentifier() == ".endm" ||
3340 getTok().getIdentifier() == ".endmacro") {
3341 if (MacroDepth == 0) { // Outermost macro.
3342 EndToken = getTok();
3343 Lex();
3344 if (getLexer().isNot(AsmToken::EndOfStatement))
3345 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3346 "' directive");
3347 break;
3348 } else {
3349 // Otherwise we just found the end of an inner macro.
3350 --MacroDepth;
3351 }
3352 } else if (getTok().getIdentifier() == ".macro") {
3353 // We allow nested macros. Those aren't instantiated until the outermost
3354 // macro is expanded so just ignore them for now.
3355 ++MacroDepth;
3356 }
Eli Bendersky17233942013-01-15 22:59:42 +00003357 }
3358
3359 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003360 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003361 }
3362
Jim Grosbach4b905842013-09-20 23:08:21 +00003363 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003364 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3365 }
3366
3367 const char *BodyStart = StartToken.getLoc().getPointer();
3368 const char *BodyEnd = EndToken.getLoc().getPointer();
3369 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003370 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003371 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003372 return false;
3373}
3374
Jim Grosbach4b905842013-09-20 23:08:21 +00003375/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003376///
3377/// With the support added for named parameters there may be code out there that
3378/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003379/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003380/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003381/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003382/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3383/// warning that the positional parameter found in body which have no effect.
3384/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003385/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003386/// intended or change the macro to use the named parameters. It is possible
3387/// this warning will trigger when the none of the named parameters are used
3388/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003389void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003391 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003392 // If this macro is not defined with named parameters the warning we are
3393 // checking for here doesn't apply.
3394 unsigned NParameters = Parameters.size();
3395 if (NParameters == 0)
3396 return;
3397
3398 bool NamedParametersFound = false;
3399 bool PositionalParametersFound = false;
3400
3401 // Look at the body of the macro for use of both the named parameters and what
3402 // are likely to be positional parameters. This is what expandMacro() is
3403 // doing when it finds the parameters in the body.
3404 while (!Body.empty()) {
3405 // Scan for the next possible parameter.
3406 std::size_t End = Body.size(), Pos = 0;
3407 for (; Pos != End; ++Pos) {
3408 // Check for a substitution or escape.
3409 // This macro is defined with parameters, look for \foo, \bar, etc.
3410 if (Body[Pos] == '\\' && Pos + 1 != End)
3411 break;
3412
3413 // This macro should have parameters, but look for $0, $1, ..., $n too.
3414 if (Body[Pos] != '$' || Pos + 1 == End)
3415 continue;
3416 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003417 if (Next == '$' || Next == 'n' ||
3418 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003419 break;
3420 }
3421
3422 // Check if we reached the end.
3423 if (Pos == End)
3424 break;
3425
3426 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003427 switch (Body[Pos + 1]) {
3428 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003429 case '$':
3430 break;
3431
Jim Grosbach4b905842013-09-20 23:08:21 +00003432 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003433 case 'n':
3434 PositionalParametersFound = true;
3435 break;
3436
Jim Grosbach4b905842013-09-20 23:08:21 +00003437 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 default: {
3439 PositionalParametersFound = true;
3440 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003441 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003442 }
3443 Pos += 2;
3444 } else {
3445 unsigned I = Pos + 1;
3446 while (isIdentifierChar(Body[I]) && I + 1 != End)
3447 ++I;
3448
Jim Grosbach4b905842013-09-20 23:08:21 +00003449 const char *Begin = Body.data() + Pos + 1;
3450 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003451 unsigned Index = 0;
3452 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003453 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003454 break;
3455
3456 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3458 Pos += 3;
3459 else {
3460 Pos = I;
3461 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003462 } else {
3463 NamedParametersFound = true;
3464 Pos += 1 + Argument.size();
3465 }
3466 }
3467 // Update the scan point.
3468 Body = Body.substr(Pos);
3469 }
3470
3471 if (!NamedParametersFound && PositionalParametersFound)
3472 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3473 "used in macro body, possible positional parameter "
3474 "found in body which will have no effect");
3475}
3476
Nico Weber155dccd12014-07-24 17:08:39 +00003477/// parseDirectiveExitMacro
3478/// ::= .exitm
3479bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3480 if (getLexer().isNot(AsmToken::EndOfStatement))
3481 return TokError("unexpected token in '" + Directive + "' directive");
3482
3483 if (!isInsideMacroInstantiation())
3484 return TokError("unexpected '" + Directive + "' in file, "
3485 "no current macro definition");
3486
3487 // Exit all conditionals that are active in the current macro.
3488 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3489 TheCondState = TheCondStack.back();
3490 TheCondStack.pop_back();
3491 }
3492
3493 handleMacroExit();
3494 return false;
3495}
3496
Jim Grosbach4b905842013-09-20 23:08:21 +00003497/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003498/// ::= .endm
3499/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003500bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003501 if (getLexer().isNot(AsmToken::EndOfStatement))
3502 return TokError("unexpected token in '" + Directive + "' directive");
3503
3504 // If we are inside a macro instantiation, terminate the current
3505 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003506 if (isInsideMacroInstantiation()) {
3507 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003508 return false;
3509 }
3510
3511 // Otherwise, this .endmacro is a stray entry in the file; well formed
3512 // .endmacro directives are handled during the macro definition parsing.
3513 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003515}
3516
Jim Grosbach4b905842013-09-20 23:08:21 +00003517/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003518/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003519bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003520 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003521 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003522 return TokError("expected identifier in '.purgem' directive");
3523
3524 if (getLexer().isNot(AsmToken::EndOfStatement))
3525 return TokError("unexpected token in '.purgem' directive");
3526
Jim Grosbach4b905842013-09-20 23:08:21 +00003527 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003528 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3529
Jim Grosbach4b905842013-09-20 23:08:21 +00003530 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003531 return false;
3532}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003535/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003536bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003537 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003538
3539 // Expect a single argument: an expression that evaluates to a constant
3540 // in the inclusive range 0-30.
3541 SMLoc ExprLoc = getLexer().getLoc();
3542 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003543 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003544 return true;
3545 else if (getLexer().isNot(AsmToken::EndOfStatement))
3546 return TokError("unexpected token after expression in"
3547 " '.bundle_align_mode' directive");
3548 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3549 return Error(ExprLoc,
3550 "invalid bundle alignment size (expected between 0 and 30)");
3551
3552 Lex();
3553
3554 // Because of AlignSizePow2's verified range we can safely truncate it to
3555 // unsigned.
3556 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3557 return false;
3558}
3559
Jim Grosbach4b905842013-09-20 23:08:21 +00003560/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003561/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003562bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003563 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003564 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003565
Eli Bendersky802b6282013-01-07 21:51:08 +00003566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3567 StringRef Option;
3568 SMLoc Loc = getTok().getLoc();
3569 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003570 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003571
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003572 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003573 return Error(Loc, kInvalidOptionError);
3574
3575 if (Option != "align_to_end")
3576 return Error(Loc, kInvalidOptionError);
3577 else if (getLexer().isNot(AsmToken::EndOfStatement))
3578 return Error(Loc,
3579 "unexpected token after '.bundle_lock' directive option");
3580 AlignToEnd = true;
3581 }
3582
Eli Benderskyf483ff92012-12-20 19:05:53 +00003583 Lex();
3584
Eli Bendersky802b6282013-01-07 21:51:08 +00003585 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003586 return false;
3587}
3588
Jim Grosbach4b905842013-09-20 23:08:21 +00003589/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003590/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003591bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003592 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003593
3594 if (getLexer().isNot(AsmToken::EndOfStatement))
3595 return TokError("unexpected token in '.bundle_unlock' directive");
3596 Lex();
3597
3598 getStreamer().EmitBundleUnlock();
3599 return false;
3600}
3601
Jim Grosbach4b905842013-09-20 23:08:21 +00003602/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003603/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003604bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003605 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003606
3607 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003608 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003609 return true;
3610
3611 int64_t FillExpr = 0;
3612 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3613 if (getLexer().isNot(AsmToken::Comma))
3614 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3615 Lex();
3616
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003617 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003618 return true;
3619
3620 if (getLexer().isNot(AsmToken::EndOfStatement))
3621 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3622 }
3623
3624 Lex();
3625
3626 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003627 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3628 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003629
3630 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003631 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003632
3633 return false;
3634}
3635
Jim Grosbach4b905842013-09-20 23:08:21 +00003636/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003637/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003638bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003639 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003640 const MCExpr *Value;
3641
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003642 for (;;) {
3643 if (parseExpression(Value))
3644 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003645
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003646 if (Signed)
3647 getStreamer().EmitSLEB128Value(Value);
3648 else
3649 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003650
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003651 if (getLexer().is(AsmToken::EndOfStatement))
3652 break;
3653
3654 if (getLexer().isNot(AsmToken::Comma))
3655 return TokError("unexpected token in directive");
3656 Lex();
3657 }
Eli Bendersky17233942013-01-15 22:59:42 +00003658
3659 return false;
3660}
3661
Jim Grosbach4b905842013-09-20 23:08:21 +00003662/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003663/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003664bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003665 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003666 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003667 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003668 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003669
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003670 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003671 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003672
Jim Grosbach6f482002015-05-18 18:43:14 +00003673 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003674
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003675 // Assembler local symbols don't make any sense here. Complain loudly.
3676 if (Sym->isTemporary())
3677 return Error(Loc, "non-local symbol required in directive");
3678
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003679 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3680 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003681
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003682 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003683 break;
3684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003685 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003686 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003687 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003688 }
3689 }
3690
Sean Callanan686ed8d2010-01-19 20:22:31 +00003691 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003692 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003693}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003694
Jim Grosbach4b905842013-09-20 23:08:21 +00003695/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003696/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003697bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003698 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003699
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003700 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003701 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003702 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003703 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003704
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003705 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003706 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003707
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003708 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003709 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003710 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003711
3712 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003713 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003714 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003715 return true;
3716
3717 int64_t Pow2Alignment = 0;
3718 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003719 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003720 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003721 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003722 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003723 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003724
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003725 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3726 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003727 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3728
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003729 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003730 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3731 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003732 if (!isPowerOf2_64(Pow2Alignment))
3733 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3734 Pow2Alignment = Log2_64(Pow2Alignment);
3735 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003736 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003737
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003738 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003739 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003740
Sean Callanan686ed8d2010-01-19 20:22:31 +00003741 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003742
Chris Lattner28ad7542009-07-09 17:25:12 +00003743 // NOTE: a size of zero for a .comm should create a undefined symbol
3744 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003745 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003746 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003747 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003748
Eric Christopherbc818852010-05-14 01:38:54 +00003749 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003750 // may internally end up wanting an alignment in bytes.
3751 // FIXME: Diagnose overflow.
3752 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003753 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003754 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003755
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003756 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003757 return Error(IDLoc, "invalid symbol redefinition");
3758
Chris Lattner28ad7542009-07-09 17:25:12 +00003759 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003760 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003761 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003762 return false;
3763 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003764
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003765 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003766 return false;
3767}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003768
Jim Grosbach4b905842013-09-20 23:08:21 +00003769/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003770/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003771bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003772 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003773 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003774
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003775 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003776 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003777 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003778
Sean Callanan686ed8d2010-01-19 20:22:31 +00003779 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003780
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003781 if (Str.empty())
3782 Error(Loc, ".abort detected. Assembly stopping.");
3783 else
3784 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003785 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003786
3787 return false;
3788}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003789
Jim Grosbach4b905842013-09-20 23:08:21 +00003790/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003791/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003792bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003793 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003794 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003795
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003796 // Allow the strings to have escaped octal character sequence.
3797 std::string Filename;
3798 if (parseEscapedString(Filename))
3799 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003800 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003801 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003802
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003803 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003804 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003805
Chris Lattner693fbb82009-07-16 06:14:39 +00003806 // Attempt to switch the lexer to the included file before consuming the end
3807 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003808 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003809 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003810 return true;
3811 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003812
3813 return false;
3814}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003815
Jim Grosbach4b905842013-09-20 23:08:21 +00003816/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003817/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003818bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003819 if (getLexer().isNot(AsmToken::String))
3820 return TokError("expected string in '.incbin' directive");
3821
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003822 // Allow the strings to have escaped octal character sequence.
3823 std::string Filename;
3824 if (parseEscapedString(Filename))
3825 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003826 SMLoc IncbinLoc = getLexer().getLoc();
3827 Lex();
3828
3829 if (getLexer().isNot(AsmToken::EndOfStatement))
3830 return TokError("unexpected token in '.incbin' directive");
3831
Kevin Enderby109f25c2011-12-14 21:47:48 +00003832 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003833 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003834 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3835 return true;
3836 }
3837
3838 return false;
3839}
3840
Jim Grosbach4b905842013-09-20 23:08:21 +00003841/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003842/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3843bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003844 TheCondStack.push_back(TheCondState);
3845 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003846 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003847 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003848 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003849 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003850 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003851 return true;
3852
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003853 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003854 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003855
Sean Callanan686ed8d2010-01-19 20:22:31 +00003856 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003857
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003858 switch (DirKind) {
3859 default:
3860 llvm_unreachable("unsupported directive");
3861 case DK_IF:
3862 case DK_IFNE:
3863 break;
3864 case DK_IFEQ:
3865 ExprValue = ExprValue == 0;
3866 break;
3867 case DK_IFGE:
3868 ExprValue = ExprValue >= 0;
3869 break;
3870 case DK_IFGT:
3871 ExprValue = ExprValue > 0;
3872 break;
3873 case DK_IFLE:
3874 ExprValue = ExprValue <= 0;
3875 break;
3876 case DK_IFLT:
3877 ExprValue = ExprValue < 0;
3878 break;
3879 }
3880
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003881 TheCondState.CondMet = ExprValue;
3882 TheCondState.Ignore = !TheCondState.CondMet;
3883 }
3884
3885 return false;
3886}
3887
Jim Grosbach4b905842013-09-20 23:08:21 +00003888/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003889/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003890bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003891 TheCondStack.push_back(TheCondState);
3892 TheCondState.TheCond = AsmCond::IfCond;
3893
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003894 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003895 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003896 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003897 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003898
3899 if (getLexer().isNot(AsmToken::EndOfStatement))
3900 return TokError("unexpected token in '.ifb' directive");
3901
3902 Lex();
3903
3904 TheCondState.CondMet = ExpectBlank == Str.empty();
3905 TheCondState.Ignore = !TheCondState.CondMet;
3906 }
3907
3908 return false;
3909}
3910
Jim Grosbach4b905842013-09-20 23:08:21 +00003911/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003912/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003913/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003914bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003915 TheCondStack.push_back(TheCondState);
3916 TheCondState.TheCond = AsmCond::IfCond;
3917
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003918 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003919 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003920 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003921 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003922
3923 if (getLexer().isNot(AsmToken::Comma))
3924 return TokError("unexpected token in '.ifc' directive");
3925
3926 Lex();
3927
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003928 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003929
3930 if (getLexer().isNot(AsmToken::EndOfStatement))
3931 return TokError("unexpected token in '.ifc' directive");
3932
3933 Lex();
3934
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003935 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003936 TheCondState.Ignore = !TheCondState.CondMet;
3937 }
3938
3939 return false;
3940}
3941
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003942/// parseDirectiveIfeqs
3943/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00003944bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003945 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003946 if (ExpectEqual)
3947 TokError("expected string parameter for '.ifeqs' directive");
3948 else
3949 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003950 eatToEndOfStatement();
3951 return true;
3952 }
3953
3954 StringRef String1 = getTok().getStringContents();
3955 Lex();
3956
3957 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00003958 if (ExpectEqual)
3959 TokError("expected comma after first string for '.ifeqs' directive");
3960 else
3961 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003962 eatToEndOfStatement();
3963 return true;
3964 }
3965
3966 Lex();
3967
3968 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00003969 if (ExpectEqual)
3970 TokError("expected string parameter for '.ifeqs' directive");
3971 else
3972 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003973 eatToEndOfStatement();
3974 return true;
3975 }
3976
3977 StringRef String2 = getTok().getStringContents();
3978 Lex();
3979
3980 TheCondStack.push_back(TheCondState);
3981 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00003982 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003983 TheCondState.Ignore = !TheCondState.CondMet;
3984
3985 return false;
3986}
3987
Jim Grosbach4b905842013-09-20 23:08:21 +00003988/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003989/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003990bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003991 StringRef Name;
3992 TheCondStack.push_back(TheCondState);
3993 TheCondState.TheCond = AsmCond::IfCond;
3994
3995 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003996 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003997 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003998 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003999 return TokError("expected identifier after '.ifdef'");
4000
4001 Lex();
4002
Jim Grosbach6f482002015-05-18 18:43:14 +00004003 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004004
4005 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004006 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004007 else
Craig Topper353eda42014-04-24 06:44:33 +00004008 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004009 TheCondState.Ignore = !TheCondState.CondMet;
4010 }
4011
4012 return false;
4013}
4014
Jim Grosbach4b905842013-09-20 23:08:21 +00004015/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004017bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004018 if (TheCondState.TheCond != AsmCond::IfCond &&
4019 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004020 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4021 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022 TheCondState.TheCond = AsmCond::ElseIfCond;
4023
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004024 bool LastIgnoreState = false;
4025 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004026 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004027 if (LastIgnoreState || TheCondState.CondMet) {
4028 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004029 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004030 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004031 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004032 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004033 return true;
4034
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004035 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004036 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004037
Sean Callanan686ed8d2010-01-19 20:22:31 +00004038 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004039 TheCondState.CondMet = ExprValue;
4040 TheCondState.Ignore = !TheCondState.CondMet;
4041 }
4042
4043 return false;
4044}
4045
Jim Grosbach4b905842013-09-20 23:08:21 +00004046/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004047/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004048bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004049 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004050 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004051
Sean Callanan686ed8d2010-01-19 20:22:31 +00004052 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004053
4054 if (TheCondState.TheCond != AsmCond::IfCond &&
4055 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004056 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4057 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004058 TheCondState.TheCond = AsmCond::ElseCond;
4059 bool LastIgnoreState = false;
4060 if (!TheCondStack.empty())
4061 LastIgnoreState = TheCondStack.back().Ignore;
4062 if (LastIgnoreState || TheCondState.CondMet)
4063 TheCondState.Ignore = true;
4064 else
4065 TheCondState.Ignore = false;
4066
4067 return false;
4068}
4069
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004070/// parseDirectiveEnd
4071/// ::= .end
4072bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4073 if (getLexer().isNot(AsmToken::EndOfStatement))
4074 return TokError("unexpected token in '.end' directive");
4075
4076 Lex();
4077
4078 while (Lexer.isNot(AsmToken::Eof))
4079 Lex();
4080
4081 return false;
4082}
4083
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004084/// parseDirectiveError
4085/// ::= .err
4086/// ::= .error [string]
4087bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4088 if (!TheCondStack.empty()) {
4089 if (TheCondStack.back().Ignore) {
4090 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004091 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004092 }
4093 }
4094
4095 if (!WithMessage)
4096 return Error(L, ".err encountered");
4097
4098 StringRef Message = ".error directive invoked in source file";
4099 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4100 if (Lexer.isNot(AsmToken::String)) {
4101 TokError(".error argument must be a string");
4102 eatToEndOfStatement();
4103 return true;
4104 }
4105
4106 Message = getTok().getStringContents();
4107 Lex();
4108 }
4109
4110 Error(L, Message);
4111 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004112}
4113
Nico Weber404012b2014-07-24 16:26:06 +00004114/// parseDirectiveWarning
4115/// ::= .warning [string]
4116bool AsmParser::parseDirectiveWarning(SMLoc L) {
4117 if (!TheCondStack.empty()) {
4118 if (TheCondStack.back().Ignore) {
4119 eatToEndOfStatement();
4120 return false;
4121 }
4122 }
4123
4124 StringRef Message = ".warning directive invoked in source file";
4125 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4126 if (Lexer.isNot(AsmToken::String)) {
4127 TokError(".warning argument must be a string");
4128 eatToEndOfStatement();
4129 return true;
4130 }
4131
4132 Message = getTok().getStringContents();
4133 Lex();
4134 }
4135
4136 Warning(L, Message);
4137 return false;
4138}
4139
Jim Grosbach4b905842013-09-20 23:08:21 +00004140/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004141/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004142bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004143 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004144 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004145
Sean Callanan686ed8d2010-01-19 20:22:31 +00004146 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004147
Jim Grosbach4b905842013-09-20 23:08:21 +00004148 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004149 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4150 ".else");
4151 if (!TheCondStack.empty()) {
4152 TheCondState = TheCondStack.back();
4153 TheCondStack.pop_back();
4154 }
4155
4156 return false;
4157}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004158
Eli Bendersky17233942013-01-15 22:59:42 +00004159void AsmParser::initializeDirectiveKindMap() {
4160 DirectiveKindMap[".set"] = DK_SET;
4161 DirectiveKindMap[".equ"] = DK_EQU;
4162 DirectiveKindMap[".equiv"] = DK_EQUIV;
4163 DirectiveKindMap[".ascii"] = DK_ASCII;
4164 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4165 DirectiveKindMap[".string"] = DK_STRING;
4166 DirectiveKindMap[".byte"] = DK_BYTE;
4167 DirectiveKindMap[".short"] = DK_SHORT;
4168 DirectiveKindMap[".value"] = DK_VALUE;
4169 DirectiveKindMap[".2byte"] = DK_2BYTE;
4170 DirectiveKindMap[".long"] = DK_LONG;
4171 DirectiveKindMap[".int"] = DK_INT;
4172 DirectiveKindMap[".4byte"] = DK_4BYTE;
4173 DirectiveKindMap[".quad"] = DK_QUAD;
4174 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004175 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004176 DirectiveKindMap[".single"] = DK_SINGLE;
4177 DirectiveKindMap[".float"] = DK_FLOAT;
4178 DirectiveKindMap[".double"] = DK_DOUBLE;
4179 DirectiveKindMap[".align"] = DK_ALIGN;
4180 DirectiveKindMap[".align32"] = DK_ALIGN32;
4181 DirectiveKindMap[".balign"] = DK_BALIGN;
4182 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4183 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4184 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4185 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4186 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4187 DirectiveKindMap[".org"] = DK_ORG;
4188 DirectiveKindMap[".fill"] = DK_FILL;
4189 DirectiveKindMap[".zero"] = DK_ZERO;
4190 DirectiveKindMap[".extern"] = DK_EXTERN;
4191 DirectiveKindMap[".globl"] = DK_GLOBL;
4192 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004193 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4194 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4195 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4196 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4197 DirectiveKindMap[".reference"] = DK_REFERENCE;
4198 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4199 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4200 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4201 DirectiveKindMap[".comm"] = DK_COMM;
4202 DirectiveKindMap[".common"] = DK_COMMON;
4203 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4204 DirectiveKindMap[".abort"] = DK_ABORT;
4205 DirectiveKindMap[".include"] = DK_INCLUDE;
4206 DirectiveKindMap[".incbin"] = DK_INCBIN;
4207 DirectiveKindMap[".code16"] = DK_CODE16;
4208 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4209 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004210 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004211 DirectiveKindMap[".irp"] = DK_IRP;
4212 DirectiveKindMap[".irpc"] = DK_IRPC;
4213 DirectiveKindMap[".endr"] = DK_ENDR;
4214 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4215 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4216 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4217 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004218 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4219 DirectiveKindMap[".ifge"] = DK_IFGE;
4220 DirectiveKindMap[".ifgt"] = DK_IFGT;
4221 DirectiveKindMap[".ifle"] = DK_IFLE;
4222 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004223 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004224 DirectiveKindMap[".ifb"] = DK_IFB;
4225 DirectiveKindMap[".ifnb"] = DK_IFNB;
4226 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004227 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004228 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004229 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004230 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4231 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4232 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4233 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4234 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004235 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004236 DirectiveKindMap[".endif"] = DK_ENDIF;
4237 DirectiveKindMap[".skip"] = DK_SKIP;
4238 DirectiveKindMap[".space"] = DK_SPACE;
4239 DirectiveKindMap[".file"] = DK_FILE;
4240 DirectiveKindMap[".line"] = DK_LINE;
4241 DirectiveKindMap[".loc"] = DK_LOC;
4242 DirectiveKindMap[".stabs"] = DK_STABS;
4243 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4244 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4245 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4246 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4247 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4248 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4249 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4250 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4251 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4252 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4253 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4254 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4255 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4256 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4257 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4258 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4259 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4260 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4261 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4262 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4263 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004264 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004265 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4266 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4267 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004268 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004269 DirectiveKindMap[".endm"] = DK_ENDM;
4270 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4271 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004272 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004273 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004274 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004275}
4276
Jim Grosbach4b905842013-09-20 23:08:21 +00004277MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004278 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004279
Rafael Espindola34b9c512012-06-03 23:57:14 +00004280 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004281 for (;;) {
4282 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004283 if (getLexer().is(AsmToken::Eof)) {
4284 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004285 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004286 }
4287
Rafael Espindola34b9c512012-06-03 23:57:14 +00004288 if (Lexer.is(AsmToken::Identifier) &&
4289 (getTok().getIdentifier() == ".rept")) {
4290 ++NestLevel;
4291 }
4292
4293 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004294 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004295 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004296 EndToken = getTok();
4297 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004298 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4299 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004300 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004301 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004302 break;
4303 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004304 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004305 }
4306
Rafael Espindola34b9c512012-06-03 23:57:14 +00004307 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004308 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309 }
4310
4311 const char *BodyStart = StartToken.getLoc().getPointer();
4312 const char *BodyEnd = EndToken.getLoc().getPointer();
4313 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4314
Rafael Espindola34b9c512012-06-03 23:57:14 +00004315 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004316 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004317 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004318}
4319
Jim Grosbach4b905842013-09-20 23:08:21 +00004320void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004321 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004322 OS << ".endr\n";
4323
Rafael Espindola3560ff22014-08-27 20:03:13 +00004324 std::unique_ptr<MemoryBuffer> Instantiation =
4325 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004326
Rafael Espindola34b9c512012-06-03 23:57:14 +00004327 // Create the macro instantiation object and add to the current macro
4328 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004329 MacroInstantiation *MI = new MacroInstantiation(
4330 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004331 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004332
Rafael Espindola34b9c512012-06-03 23:57:14 +00004333 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004334 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004335 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004336 Lex();
4337}
4338
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004339/// parseDirectiveRept
4340/// ::= .rep | .rept count
4341bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004342 const MCExpr *CountExpr;
4343 SMLoc CountLoc = getTok().getLoc();
4344 if (parseExpression(CountExpr))
4345 return true;
4346
Rafael Espindola34b9c512012-06-03 23:57:14 +00004347 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004348 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004349 eatToEndOfStatement();
4350 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4351 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004352
4353 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004354 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004355
4356 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004357 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004358
4359 // Eat the end of statement.
4360 Lex();
4361
4362 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004363 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004364 if (!M)
4365 return true;
4366
4367 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4368 // to hold the macro body with substitutions.
4369 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004370 raw_svector_ostream OS(Buf);
4371 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004372 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4373 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004374 return true;
4375 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004376 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004377
4378 return false;
4379}
4380
Jim Grosbach4b905842013-09-20 23:08:21 +00004381/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004382/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004383bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004384 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004385
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004386 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004387 return TokError("expected identifier in '.irp' directive");
4388
Rafael Espindola768b41c2012-06-15 14:02:34 +00004389 if (Lexer.isNot(AsmToken::Comma))
4390 return TokError("expected comma in '.irp' directive");
4391
4392 Lex();
4393
Eli Bendersky38274122013-01-14 23:22:36 +00004394 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004395 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004396 return true;
4397
4398 // Eat the end of statement.
4399 Lex();
4400
4401 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004402 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004403 if (!M)
4404 return true;
4405
4406 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4407 // to hold the macro body with substitutions.
4408 SmallString<256> Buf;
4409 raw_svector_ostream OS(Buf);
4410
Eli Bendersky38274122013-01-14 23:22:36 +00004411 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004412 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4413 // This is undocumented, but GAS seems to support it.
4414 if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004415 return true;
4416 }
4417
Jim Grosbach4b905842013-09-20 23:08:21 +00004418 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004419
4420 return false;
4421}
4422
Jim Grosbach4b905842013-09-20 23:08:21 +00004423/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004424/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004425bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004426 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004427
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004428 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004429 return TokError("expected identifier in '.irpc' directive");
4430
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004431 if (Lexer.isNot(AsmToken::Comma))
4432 return TokError("expected comma in '.irpc' directive");
4433
4434 Lex();
4435
Eli Bendersky38274122013-01-14 23:22:36 +00004436 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004437 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004438 return true;
4439
4440 if (A.size() != 1 || A.front().size() != 1)
4441 return TokError("unexpected token in '.irpc' directive");
4442
4443 // Eat the end of statement.
4444 Lex();
4445
4446 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004447 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004448 if (!M)
4449 return true;
4450
4451 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4452 // to hold the macro body with substitutions.
4453 SmallString<256> Buf;
4454 raw_svector_ostream OS(Buf);
4455
4456 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004457 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004458 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004459 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004460
Toma Tabacu217116e2015-04-27 10:50:29 +00004461 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4462 // This is undocumented, but GAS seems to support it.
4463 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004464 return true;
4465 }
4466
Jim Grosbach4b905842013-09-20 23:08:21 +00004467 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004468
4469 return false;
4470}
4471
Jim Grosbach4b905842013-09-20 23:08:21 +00004472bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004473 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004474 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004475
4476 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004477 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004478 assert(getLexer().is(AsmToken::EndOfStatement));
4479
Jim Grosbach4b905842013-09-20 23:08:21 +00004480 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004481 return false;
4482}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004483
Jim Grosbach4b905842013-09-20 23:08:21 +00004484bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004485 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004486 const MCExpr *Value;
4487 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004488 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004489 return true;
4490 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4491 if (!MCE)
4492 return Error(ExprLoc, "unexpected expression in _emit");
4493 uint64_t IntValue = MCE->getValue();
4494 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4495 return Error(ExprLoc, "literal value out of range for directive");
4496
Chad Rosierc7f552c2013-02-12 21:33:51 +00004497 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4498 return false;
4499}
4500
Jim Grosbach4b905842013-09-20 23:08:21 +00004501bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004502 const MCExpr *Value;
4503 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004504 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004505 return true;
4506 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4507 if (!MCE)
4508 return Error(ExprLoc, "unexpected expression in align");
4509 uint64_t IntValue = MCE->getValue();
4510 if (!isPowerOf2_64(IntValue))
4511 return Error(ExprLoc, "literal value not a power of two greater then zero");
4512
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 Info.AsmRewrites->push_back(
4514 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004515 return false;
4516}
4517
Chad Rosierf43fcf52013-02-13 21:27:17 +00004518// We are comparing pointers, but the pointers are relative to a single string.
4519// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004520static int rewritesSort(const AsmRewrite *AsmRewriteA,
4521 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004522 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4523 return -1;
4524 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4525 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004526
Chad Rosierfce4fab2013-04-08 17:43:47 +00004527 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4528 // rewrite to the same location. Make sure the SizeDirective rewrite is
4529 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4530 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004531 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4532 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004533 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004534
Jim Grosbach4b905842013-09-20 23:08:21 +00004535 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4536 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004537 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004538 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004539}
4540
Jim Grosbach4b905842013-09-20 23:08:21 +00004541bool AsmParser::parseMSInlineAsm(
4542 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4543 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4544 SmallVectorImpl<std::string> &Constraints,
4545 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4546 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004547 SmallVector<void *, 4> InputDecls;
4548 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004549 SmallVector<bool, 4> InputDeclsAddressOf;
4550 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004551 SmallVector<std::string, 4> InputConstraints;
4552 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004553 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004554
Benjamin Kramer1a136112013-02-15 20:37:21 +00004555 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004556
4557 // Prime the lexer.
4558 Lex();
4559
4560 // While we have input, parse each statement.
4561 unsigned InputIdx = 0;
4562 unsigned OutputIdx = 0;
4563 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004564 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004565 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004566 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004567
Chad Rosier149e8e02012-12-12 22:45:52 +00004568 if (Info.ParseError)
4569 return true;
4570
Benjamin Kramer1a136112013-02-15 20:37:21 +00004571 if (Info.Opcode == ~0U)
4572 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004573
Benjamin Kramer1a136112013-02-15 20:37:21 +00004574 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004575
Benjamin Kramer1a136112013-02-15 20:37:21 +00004576 // Build the list of clobbers, outputs and inputs.
4577 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004578 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004579
Benjamin Kramer1a136112013-02-15 20:37:21 +00004580 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004581 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004582 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004583
Benjamin Kramer1a136112013-02-15 20:37:21 +00004584 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004585 if (Operand.isReg() && !Operand.needAddressOf() &&
4586 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 unsigned NumDefs = Desc.getNumDefs();
4588 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004589 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4590 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004591 continue;
4592 }
4593
4594 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004595 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004596 if (SymName.empty())
4597 continue;
4598
David Blaikie960ea3f2014-06-08 16:18:35 +00004599 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004600 if (!OpDecl)
4601 continue;
4602
4603 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004604 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004605 if (isOutput) {
4606 ++InputIdx;
4607 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004608 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004609 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004610 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004611 } else {
4612 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004613 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4614 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004615 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004616 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004617 }
Reid Kleckneree088972013-12-10 18:27:32 +00004618
4619 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004620 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4621 Desc.getNumImplicitDefs());
4622 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004623 }
4624
4625 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004626 NumOutputs = OutputDecls.size();
4627 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004628
4629 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004630 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4631 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4632 ClobberRegs.end());
4633 Clobbers.assign(ClobberRegs.size(), std::string());
4634 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4635 raw_string_ostream OS(Clobbers[I]);
4636 IP->printRegName(OS, ClobberRegs[I]);
4637 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004638
4639 // Merge the various outputs and inputs. Output are expected first.
4640 if (NumOutputs || NumInputs) {
4641 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004642 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004643 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004644 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004645 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004646 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004647 }
4648 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004649 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004650 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004651 }
4652 }
4653
4654 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004655 std::string AsmStringIR;
4656 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004657 StringRef ASMString =
4658 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4659 const char *AsmStart = ASMString.begin();
4660 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004661 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004662 for (const AsmRewrite &AR : AsmStrRewrites) {
4663 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004664 if (Kind == AOK_Delete)
4665 continue;
4666
David Majnemer8114c1a2014-06-23 02:17:16 +00004667 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004668 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004669
Chad Rosier120eefd2013-03-19 17:32:17 +00004670 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004671 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004672 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004673
Chad Rosier37e755c2012-10-23 17:43:43 +00004674 // Skip the original expression.
4675 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004676 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004677 continue;
4678 }
4679
Chad Rosierff10ed12013-04-12 16:26:42 +00004680 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004681 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004682 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004683 default:
4684 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004685 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004686 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004687 break;
4688 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004689 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004690 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004691 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004692 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004693 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004694 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004695 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004696 break;
4697 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004698 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004699 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004700 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004701 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004702 default: break;
4703 case 8: OS << "byte ptr "; break;
4704 case 16: OS << "word ptr "; break;
4705 case 32: OS << "dword ptr "; break;
4706 case 64: OS << "qword ptr "; break;
4707 case 80: OS << "xword ptr "; break;
4708 case 128: OS << "xmmword ptr "; break;
4709 case 256: OS << "ymmword ptr "; break;
4710 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004711 break;
4712 case AOK_Emit:
4713 OS << ".byte";
4714 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004715 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004716 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004717 OS << ".align " << Val;
4718
4719 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004720 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004721 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4722 break;
4723 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004724 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004725 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004726 OS.flush();
4727 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004728 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004729 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004730 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004731 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004732
Chad Rosier8bce6642012-10-18 15:49:34 +00004733 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004734 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004735 }
4736
4737 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004738 if (AsmStart != AsmEnd)
4739 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004740
4741 AsmString = OS.str();
4742 return false;
4743}
4744
Pete Cooper80d21cb2015-06-22 19:35:57 +00004745namespace llvm {
4746namespace MCParserUtils {
4747
4748/// Returns whether the given symbol is used anywhere in the given expression,
4749/// or subexpressions.
4750static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
4751 switch (Value->getKind()) {
4752 case MCExpr::Binary: {
4753 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
4754 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
4755 isSymbolUsedInExpression(Sym, BE->getRHS());
4756 }
4757 case MCExpr::Target:
4758 case MCExpr::Constant:
4759 return false;
4760 case MCExpr::SymbolRef: {
4761 const MCSymbol &S =
4762 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
4763 if (S.isVariable())
4764 return isSymbolUsedInExpression(Sym, S.getVariableValue());
4765 return &S == Sym;
4766 }
4767 case MCExpr::Unary:
4768 return isSymbolUsedInExpression(
4769 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
4770 }
4771
4772 llvm_unreachable("Unknown expr kind!");
4773}
4774
4775bool parseAssignmentExpression(StringRef Name, bool allow_redef,
4776 MCAsmParser &Parser, MCSymbol *&Sym,
4777 const MCExpr *&Value) {
4778 MCAsmLexer &Lexer = Parser.getLexer();
4779
4780 // FIXME: Use better location, we should use proper tokens.
4781 SMLoc EqualLoc = Lexer.getLoc();
4782
4783 if (Parser.parseExpression(Value)) {
4784 Parser.TokError("missing expression");
4785 Parser.eatToEndOfStatement();
4786 return true;
4787 }
4788
4789 // Note: we don't count b as used in "a = b". This is to allow
4790 // a = b
4791 // b = c
4792
4793 if (Lexer.isNot(AsmToken::EndOfStatement))
4794 return Parser.TokError("unexpected token in assignment");
4795
4796 // Eat the end of statement marker.
4797 Parser.Lex();
4798
4799 // Validate that the LHS is allowed to be a variable (either it has not been
4800 // used as a symbol, or it is an absolute symbol).
4801 Sym = Parser.getContext().lookupSymbol(Name);
4802 if (Sym) {
4803 // Diagnose assignment to a label.
4804 //
4805 // FIXME: Diagnostics. Note the location of the definition as a label.
4806 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
4807 if (isSymbolUsedInExpression(Sym, Value))
4808 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00004809 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
4810 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00004811 ; // Allow redefinitions of undefined symbols only used in directives.
4812 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
4813 ; // Allow redefinitions of variables that haven't yet been used.
4814 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
4815 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
4816 else if (!Sym->isVariable())
4817 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
4818 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
4819 return Parser.Error(EqualLoc,
4820 "invalid reassignment of non-absolute variable '" +
4821 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00004822 } else if (Name == ".") {
4823 if (Parser.getStreamer().EmitValueToOffset(Value, 0)) {
4824 Parser.Error(EqualLoc, "expected absolute expression");
4825 Parser.eatToEndOfStatement();
4826 return true;
4827 }
4828 return false;
4829 } else
4830 Sym = Parser.getContext().getOrCreateSymbol(Name);
4831
4832 Sym->setRedefinable(allow_redef);
4833
4834 return false;
4835}
4836
4837} // namespace MCParserUtils
4838} // namespace llvm
4839
Daniel Dunbar01e36072010-07-17 02:26:10 +00004840/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004841MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4842 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004843 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004844}