blob: aa3489a0573f3abac22625584df0817b60490c0f [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();
Craig Topper3c76c522015-09-20 23:35:59 +0000254 bool parseCppHashLineFilenameComment(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,
Craig Topper3c76c522015-09-20 23:35:59 +0000261 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()) {
Craig Topper84008482015-10-10 05:38:14 +0000684 for (const auto &TableEntry : getContext().getSymbols()) {
685 MCSymbol *Sym = TableEntry.getValue();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000686 // Variable symbols may not be marked as defined, so check those
687 // explicitly. If we know it's a variable, we have a definition for
688 // the purposes of this check.
689 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
690 // FIXME: We would really like to refer back to where the symbol was
691 // first referenced for a source location. We need to add something
692 // to track that. Currently, we just point to the end of the file.
Jim Grosbach0fdd5722015-10-16 22:07:59 +0000693 return Error(getLexer().getLoc(), "assembler local symbol '" +
694 Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000695 }
696 }
697
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000698 // Finalize the output stream if there are no errors and if the client wants
699 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000700 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000701 Out.Finish();
702
Chris Lattner73f36112009-07-02 21:53:43 +0000703 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000704}
705
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000706void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000707 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000708 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000709 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000710 }
711}
712
Jim Grosbach4b905842013-09-20 23:08:21 +0000713/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000714void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000715 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000716 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000717
Chris Lattnere5074c42009-06-22 01:29:09 +0000718 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000719 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000720 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000721}
722
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000723StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000724 const char *Start = getTok().getLoc().getPointer();
725
Jim Grosbach4b905842013-09-20 23:08:21 +0000726 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000727 Lex();
728
729 const char *End = getTok().getLoc().getPointer();
730 return StringRef(Start, End - Start);
731}
Chris Lattner78db3622009-06-22 05:51:26 +0000732
Jim Grosbach4b905842013-09-20 23:08:21 +0000733StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000734 const char *Start = getTok().getLoc().getPointer();
735
736 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000737 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000738 Lex();
739
740 const char *End = getTok().getLoc().getPointer();
741 return StringRef(Start, End - Start);
742}
743
Jim Grosbach4b905842013-09-20 23:08:21 +0000744/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000745/// NOTE: This assumes the leading '(' has already been consumed.
746///
747/// parenexpr ::= expr)
748///
Jim Grosbach4b905842013-09-20 23:08:21 +0000749bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
750 if (parseExpression(Res))
751 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000752 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000754 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000755 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000756 return false;
757}
Chris Lattner78db3622009-06-22 05:51:26 +0000758
Jim Grosbach4b905842013-09-20 23:08:21 +0000759/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000760/// NOTE: This assumes the leading '[' has already been consumed.
761///
762/// bracketexpr ::= expr]
763///
Jim Grosbach4b905842013-09-20 23:08:21 +0000764bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
765 if (parseExpression(Res))
766 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000767 if (Lexer.isNot(AsmToken::RBrac))
768 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000769 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000770 Lex();
771 return false;
772}
773
Jim Grosbach4b905842013-09-20 23:08:21 +0000774/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000775/// primaryexpr ::= (parenexpr
776/// primaryexpr ::= symbol
777/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000778/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000779/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000780bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000781 SMLoc FirstTokenLoc = getLexer().getLoc();
782 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
783 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000784 default:
785 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000786 // If we have an error assume that we've already handled it.
787 case AsmToken::Error:
788 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000789 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000790 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000791 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000792 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000793 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000794 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000795 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000796 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000797 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000798 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000799 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000800 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000801 if (FirstTokenKind == AsmToken::Dollar) {
802 if (Lexer.getMAI().getDollarIsPC()) {
803 // This is a '$' reference, which references the current PC. Emit a
804 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000805 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000807 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000808 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 EndLoc = FirstTokenLoc;
810 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000811 }
812 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000813 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000814 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000815 // Parse symbol variant
816 std::pair<StringRef, StringRef> Split;
817 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000818 if (FirstTokenKind == AsmToken::String) {
819 if (Lexer.is(AsmToken::At)) {
820 Lexer.Lex(); // eat @
821 SMLoc AtLoc = getLexer().getLoc();
822 StringRef VName;
823 if (parseIdentifier(VName))
824 return Error(AtLoc, "expected symbol variant after '@'");
825
826 Split = std::make_pair(Identifier, VName);
827 }
828 } else {
829 Split = Identifier.split('@');
830 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000831 } else if (Lexer.is(AsmToken::LParen)) {
832 Lexer.Lex(); // eat (
833 StringRef VName;
834 parseIdentifier(VName);
835 if (Lexer.isNot(AsmToken::RParen)) {
836 return Error(Lexer.getTok().getLoc(),
837 "unexpected token in variant, expected ')'");
838 }
839 Lexer.Lex(); // eat )
840 Split = std::make_pair(Identifier, VName);
841 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000842
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000843 EndLoc = SMLoc::getFromPointer(Identifier.end());
844
Daniel Dunbard20cda02009-10-16 01:34:54 +0000845 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 StringRef SymbolName = Identifier;
847 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000848
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000849 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000850 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000851 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000852 if (Variant != MCSymbolRefExpr::VK_Invalid) {
853 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000854 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000855 Variant = MCSymbolRefExpr::VK_None;
856 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000857 return Error(SMLoc::getFromPointer(Split.second.begin()),
858 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000859 }
860 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000861
Jim Grosbach6f482002015-05-18 18:43:14 +0000862 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000863
Daniel Dunbard20cda02009-10-16 01:34:54 +0000864 // If this is an absolute variable reference, substitute it now to preserve
865 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000866 if (Sym->isVariable() &&
867 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000868 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000869 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000870
Vedant Kumar86dbd922015-08-31 17:44:53 +0000871 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000872 return false;
873 }
874
875 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000876 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000877 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000878 }
David Woodhousef42a6662014-02-01 16:20:54 +0000879 case AsmToken::BigNum:
880 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000881 case AsmToken::Integer: {
882 SMLoc Loc = getTok().getLoc();
883 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000884 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000885 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000886 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 // Look for 'b' or 'f' following an Integer as a directional label
888 if (Lexer.getKind() == AsmToken::Identifier) {
889 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 // Lookup the symbol variant if used.
891 std::pair<StringRef, StringRef> Split = IDVal.split('@');
892 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
893 if (Split.first.size() != IDVal.size()) {
894 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000895 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000897 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000898 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000899 if (IDVal == "f" || IDVal == "b") {
900 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000901 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000902 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000903 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000905 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000906 Lex(); // Eat identifier.
907 }
908 }
Chris Lattner78db3622009-06-22 05:51:26 +0000909 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000910 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000911 case AsmToken::Real: {
912 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000913 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000914 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000915 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000916 Lex(); // Eat token.
917 return false;
918 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000919 case AsmToken::Dot: {
920 // This is a '.' reference, which references the current PC. Emit a
921 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000922 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000923 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000924 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000925 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000926 Lex(); // Eat identifier.
927 return false;
928 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000932 case AsmToken::LBrac:
933 if (!PlatformParser->HasBracketExpressions())
934 return TokError("brackets expression not supported on this target");
935 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000936 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000937 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000938 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000939 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000941 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000943 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000944 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000945 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000947 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000948 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000949 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000950 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000951 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000953 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000954 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000955 }
956}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000957
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000958bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000959 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000960 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000961}
962
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000964AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000966 // Ask the target implementation about this expression first.
967 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
968 if (NewE)
969 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 // Recurse over the given expression, rebuilding it to apply the given variant
971 // if there is exactly one symbol.
972 switch (E->getKind()) {
973 case MCExpr::Target:
974 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000975 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976
977 case MCExpr::SymbolRef: {
978 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
979
980 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000981 TokError("invalid variant on expression '" + getTok().getIdentifier() +
982 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983 return E;
984 }
985
Jim Grosbach13760bd2015-05-30 01:25:56 +0000986 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 }
988
989 case MCExpr::Unary: {
990 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000991 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000993 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000994 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995 }
996
997 case MCExpr::Binary: {
998 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000999 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1000 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001
1002 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001003 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004
Jim Grosbach4b905842013-09-20 23:08:21 +00001005 if (!LHS)
1006 LHS = BE->getLHS();
1007 if (!RHS)
1008 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001009
Jim Grosbach13760bd2015-05-30 01:25:56 +00001010 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001011 }
1012 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001013
Craig Toppera2886c22012-02-07 05:05:23 +00001014 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001015}
1016
Jim Grosbach4b905842013-09-20 23:08:21 +00001017/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001018///
Jim Grosbachbd164242011-08-20 16:24:13 +00001019/// expr ::= expr &&,|| expr -> lowest.
1020/// expr ::= expr |,^,&,! expr
1021/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1022/// expr ::= expr <<,>> expr
1023/// expr ::= expr +,- expr
1024/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001025/// expr ::= primaryexpr
1026///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001027bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001028 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001029 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001031 return true;
1032
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 // As a special case, we support 'a op b @ modifier' by rewriting the
1034 // expression to include the modifier. This is inefficient, but in general we
1035 // expect users to use 'a@modifier op b'.
1036 if (Lexer.getKind() == AsmToken::At) {
1037 Lex();
1038
1039 if (Lexer.isNot(AsmToken::Identifier))
1040 return TokError("unexpected symbol modifier following '@'");
1041
1042 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 if (Variant == MCSymbolRefExpr::VK_Invalid)
1045 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1046
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 if (!ModifiedRes) {
1049 return TokError("invalid modifier '" + getTok().getIdentifier() +
1050 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001052
Daniel Dunbar55f16672010-09-17 02:47:07 +00001053 Res = ModifiedRes;
1054 Lex();
1055 }
1056
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001057 // Try to constant fold it up front, if possible.
1058 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001059 if (Res->evaluateAsAbsolute(Value))
1060 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001061
1062 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001063}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001064
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001065bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001066 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001068}
1069
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001070bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1071 SMLoc &EndLoc) {
1072 if (parseParenExpr(Res, EndLoc))
1073 return true;
1074
1075 for (; ParenDepth > 0; --ParenDepth) {
1076 if (parseBinOpRHS(1, Res, EndLoc))
1077 return true;
1078
1079 // We don't Lex() the last RParen.
1080 // This is the same behavior as parseParenExpression().
1081 if (ParenDepth - 1 > 0) {
1082 if (Lexer.isNot(AsmToken::RParen))
1083 return TokError("expected ')' in parentheses expression");
1084 EndLoc = Lexer.getTok().getEndLoc();
1085 Lex();
1086 }
1087 }
1088 return false;
1089}
1090
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001091bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001092 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001093
Daniel Dunbar75630b32009-06-30 02:10:03 +00001094 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001095 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001096 return true;
1097
Jim Grosbach13760bd2015-05-30 01:25:56 +00001098 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001099 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001100
1101 return false;
1102}
1103
David Majnemer0993e0b2015-10-26 03:15:34 +00001104static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1105 MCBinaryExpr::Opcode &Kind,
1106 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001107 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001108 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001109 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001110
Jim Grosbach4b905842013-09-20 23:08:21 +00001111 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001112 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001113 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001114 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001115 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001116 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001117 return 1;
1118
Jim Grosbach4b905842013-09-20 23:08:21 +00001119 // Low Precedence: |, &, ^
1120 //
1121 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001122 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001123 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001124 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001125 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001126 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001127 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001130 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001131
Jim Grosbach4b905842013-09-20 23:08:21 +00001132 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001133 case AsmToken::EqualEqual:
1134 Kind = MCBinaryExpr::EQ;
1135 return 3;
1136 case AsmToken::ExclaimEqual:
1137 case AsmToken::LessGreater:
1138 Kind = MCBinaryExpr::NE;
1139 return 3;
1140 case AsmToken::Less:
1141 Kind = MCBinaryExpr::LT;
1142 return 3;
1143 case AsmToken::LessEqual:
1144 Kind = MCBinaryExpr::LTE;
1145 return 3;
1146 case AsmToken::Greater:
1147 Kind = MCBinaryExpr::GT;
1148 return 3;
1149 case AsmToken::GreaterEqual:
1150 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001151 return 3;
1152
Jim Grosbach4b905842013-09-20 23:08:21 +00001153 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001154 case AsmToken::LessLess:
1155 Kind = MCBinaryExpr::Shl;
1156 return 4;
1157 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001158 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001159 return 4;
1160
Jim Grosbach4b905842013-09-20 23:08:21 +00001161 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001162 case AsmToken::Plus:
1163 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001164 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001165 case AsmToken::Minus:
1166 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001167 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001168
Jim Grosbach4b905842013-09-20 23:08:21 +00001169 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001170 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001171 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001172 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001173 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001174 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001175 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001176 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001177 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001178 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001179 }
1180}
1181
David Majnemer0993e0b2015-10-26 03:15:34 +00001182static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1183 MCBinaryExpr::Opcode &Kind,
1184 bool ShouldUseLogicalShr) {
1185 switch (K) {
1186 default:
1187 return 0; // not a binop.
1188
1189 // Lowest Precedence: &&, ||
1190 case AsmToken::AmpAmp:
1191 Kind = MCBinaryExpr::LAnd;
1192 return 2;
1193 case AsmToken::PipePipe:
1194 Kind = MCBinaryExpr::LOr;
1195 return 1;
1196
1197 // Low Precedence: ==, !=, <>, <, <=, >, >=
1198 case AsmToken::EqualEqual:
1199 Kind = MCBinaryExpr::EQ;
1200 return 3;
1201 case AsmToken::ExclaimEqual:
1202 case AsmToken::LessGreater:
1203 Kind = MCBinaryExpr::NE;
1204 return 3;
1205 case AsmToken::Less:
1206 Kind = MCBinaryExpr::LT;
1207 return 3;
1208 case AsmToken::LessEqual:
1209 Kind = MCBinaryExpr::LTE;
1210 return 3;
1211 case AsmToken::Greater:
1212 Kind = MCBinaryExpr::GT;
1213 return 3;
1214 case AsmToken::GreaterEqual:
1215 Kind = MCBinaryExpr::GTE;
1216 return 3;
1217
1218 // Low Intermediate Precedence: +, -
1219 case AsmToken::Plus:
1220 Kind = MCBinaryExpr::Add;
1221 return 4;
1222 case AsmToken::Minus:
1223 Kind = MCBinaryExpr::Sub;
1224 return 4;
1225
1226 // High Intermediate Precedence: |, &, ^
1227 //
1228 // FIXME: gas seems to support '!' as an infix operator?
1229 case AsmToken::Pipe:
1230 Kind = MCBinaryExpr::Or;
1231 return 5;
1232 case AsmToken::Caret:
1233 Kind = MCBinaryExpr::Xor;
1234 return 5;
1235 case AsmToken::Amp:
1236 Kind = MCBinaryExpr::And;
1237 return 5;
1238
1239 // Highest Precedence: *, /, %, <<, >>
1240 case AsmToken::Star:
1241 Kind = MCBinaryExpr::Mul;
1242 return 6;
1243 case AsmToken::Slash:
1244 Kind = MCBinaryExpr::Div;
1245 return 6;
1246 case AsmToken::Percent:
1247 Kind = MCBinaryExpr::Mod;
1248 return 6;
1249 case AsmToken::LessLess:
1250 Kind = MCBinaryExpr::Shl;
1251 return 6;
1252 case AsmToken::GreaterGreater:
1253 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1254 return 6;
1255 }
1256}
1257
1258unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1259 MCBinaryExpr::Opcode &Kind) {
1260 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1261 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1262 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1263}
1264
Jim Grosbach4b905842013-09-20 23:08:21 +00001265/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001266/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001267bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001268 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001269 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001270 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001271 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001272
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001273 // If the next token is lower precedence than we are allowed to eat, return
1274 // successfully with what we ate already.
1275 if (TokPrec < Precedence)
1276 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001277
Sean Callanan686ed8d2010-01-19 20:22:31 +00001278 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001279
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001280 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001281 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001282 if (parsePrimaryExpr(RHS, EndLoc))
1283 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001284
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001285 // If BinOp binds less tightly with RHS than the operator after RHS, let
1286 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001287 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001288 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001289 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1290 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001291
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001292 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001293 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001294 }
1295}
1296
Chris Lattner36e02122009-06-21 20:54:55 +00001297/// ParseStatement:
1298/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001299/// ::= Label* Directive ...Operands... EndOfStatement
1300/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001301bool AsmParser::parseStatement(ParseStatementInfo &Info,
1302 MCAsmParserSemaCallback *SI) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001303 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001304 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001305 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001306 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001307 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001308
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001309 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001310 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001311 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001312 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001313 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001314 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001315 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001316 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001317
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001318 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001319 if (Lexer.is(AsmToken::Integer)) {
1320 LocalLabelVal = getTok().getIntVal();
1321 if (LocalLabelVal < 0) {
1322 if (!TheCondState.Ignore)
1323 return TokError("unexpected token at start of statement");
1324 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001325 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001326 IDVal = getTok().getString();
1327 Lex(); // Consume the integer token to be used as an identifier token.
1328 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001329 if (!TheCondState.Ignore)
1330 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001331 }
1332 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001333 } else if (Lexer.is(AsmToken::Dot)) {
1334 // Treat '.' as a valid identifier in this context.
1335 Lex();
1336 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001337 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001338 if (!TheCondState.Ignore)
1339 return TokError("unexpected token at start of statement");
1340 IDVal = "";
1341 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001342
Chris Lattner926885c2010-04-17 18:14:27 +00001343 // Handle conditional assembly here before checking for skipping. We
1344 // have to do this so that .endif isn't skipped in a ".if 0" block for
1345 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001346 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001347 DirectiveKindMap.find(IDVal);
1348 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1349 ? DK_NO_DIRECTIVE
1350 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001351 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001352 default:
1353 break;
1354 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001355 case DK_IFEQ:
1356 case DK_IFGE:
1357 case DK_IFGT:
1358 case DK_IFLE:
1359 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001360 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001361 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001362 case DK_IFB:
1363 return parseDirectiveIfb(IDLoc, true);
1364 case DK_IFNB:
1365 return parseDirectiveIfb(IDLoc, false);
1366 case DK_IFC:
1367 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001368 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001369 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001370 case DK_IFNC:
1371 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001372 case DK_IFNES:
1373 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001374 case DK_IFDEF:
1375 return parseDirectiveIfdef(IDLoc, true);
1376 case DK_IFNDEF:
1377 case DK_IFNOTDEF:
1378 return parseDirectiveIfdef(IDLoc, false);
1379 case DK_ELSEIF:
1380 return parseDirectiveElseIf(IDLoc);
1381 case DK_ELSE:
1382 return parseDirectiveElse(IDLoc);
1383 case DK_ENDIF:
1384 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001385 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001386
Eli Bendersky88024712013-01-16 19:32:36 +00001387 // Ignore the statement if in the middle of inactive conditional
1388 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001389 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001390 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001391 return false;
1392 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001393
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001394 // FIXME: Recurse on local labels?
1395
1396 // See what kind of statement we have.
1397 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001398 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001399 if (!getTargetParser().isLabel(ID))
1400 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001401 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001402
Chris Lattner36e02122009-06-21 20:54:55 +00001403 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001404 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001405
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001406 // Diagnose attempt to use '.' as a label.
1407 if (IDVal == ".")
1408 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1409
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001410 // Diagnose attempt to use a variable as a label.
1411 //
1412 // FIXME: Diagnostics. Note the location of the definition as a label.
1413 // FIXME: This doesn't diagnose assignment to a symbol which has been
1414 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001415 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001416 if (LocalLabelVal == -1) {
1417 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001418 StringRef RewrittenLabel =
1419 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1420 assert(RewrittenLabel.size() &&
1421 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001422 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1423 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001424 IDVal = RewrittenLabel;
1425 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001426 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001427 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001428 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001429
1430 Sym->redefineIfPossible();
1431
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001432 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001433 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001434
Daniel Dunbare73b2672009-08-26 22:13:22 +00001435 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001436 if (!ParsingInlineAsm)
1437 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001438
Kevin Enderbye7739d42011-12-09 18:09:40 +00001439 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001440 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001441 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001442 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1443 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001444
Tim Northover1744d0a2013-10-25 12:49:50 +00001445 getTargetParser().onLabelParsed(Sym);
1446
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001447 // Consume any end of statement token, if present, to avoid spurious
1448 // AddBlankLine calls().
1449 if (Lexer.is(AsmToken::EndOfStatement)) {
1450 Lex();
1451 if (Lexer.is(AsmToken::Eof))
1452 return false;
1453 }
1454
Eli Friedman0f4871d2012-10-22 23:58:19 +00001455 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001456 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001457
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001458 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001459 if (!getTargetParser().equalIsAsmAssignment())
1460 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001461 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001463
Jim Grosbach4b905842013-09-20 23:08:21 +00001464 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001465
1466 default: // Normal instruction or directive.
1467 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001468 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001469
1470 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001471 if (areMacrosEnabled())
1472 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1473 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001474 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001475
Michael J. Spencer530ce852010-10-09 11:00:50 +00001476 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001477
Eli Bendersky17233942013-01-15 22:59:42 +00001478 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001479 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001480 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001481 //
Eli Bendersky17233942013-01-15 22:59:42 +00001482 // 1. The target-specific assembly parser. Some directives are target
1483 // specific or may potentially behave differently on certain targets.
1484 // 2. Asm parser extensions. For example, platform-specific parsers
1485 // (like the ELF parser) register themselves as extensions.
1486 // 3. The generic directive parser implemented by this class. These are
1487 // all the directives that behave in a target and platform independent
1488 // manner, or at least have a default behavior that's shared between
1489 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001490
Eli Bendersky17233942013-01-15 22:59:42 +00001491 // First query the target-specific parser. It will return 'true' if it
1492 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001493 if (!getTargetParser().ParseDirective(ID))
1494 return false;
1495
Alp Tokercb402912014-01-24 17:20:08 +00001496 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001497 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001498 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1499 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001500 if (Handler.first)
1501 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1502
1503 // Finally, if no one else is interested in this directive, it must be
1504 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001505 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001506 default:
1507 break;
1508 case DK_SET:
1509 case DK_EQU:
1510 return parseDirectiveSet(IDVal, true);
1511 case DK_EQUIV:
1512 return parseDirectiveSet(IDVal, false);
1513 case DK_ASCII:
1514 return parseDirectiveAscii(IDVal, false);
1515 case DK_ASCIZ:
1516 case DK_STRING:
1517 return parseDirectiveAscii(IDVal, true);
1518 case DK_BYTE:
1519 return parseDirectiveValue(1);
1520 case DK_SHORT:
1521 case DK_VALUE:
1522 case DK_2BYTE:
1523 return parseDirectiveValue(2);
1524 case DK_LONG:
1525 case DK_INT:
1526 case DK_4BYTE:
1527 return parseDirectiveValue(4);
1528 case DK_QUAD:
1529 case DK_8BYTE:
1530 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001531 case DK_OCTA:
1532 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001533 case DK_SINGLE:
1534 case DK_FLOAT:
1535 return parseDirectiveRealValue(APFloat::IEEEsingle);
1536 case DK_DOUBLE:
1537 return parseDirectiveRealValue(APFloat::IEEEdouble);
1538 case DK_ALIGN: {
1539 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1540 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1541 }
1542 case DK_ALIGN32: {
1543 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1544 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1545 }
1546 case DK_BALIGN:
1547 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1548 case DK_BALIGNW:
1549 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1550 case DK_BALIGNL:
1551 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1552 case DK_P2ALIGN:
1553 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1554 case DK_P2ALIGNW:
1555 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1556 case DK_P2ALIGNL:
1557 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1558 case DK_ORG:
1559 return parseDirectiveOrg();
1560 case DK_FILL:
1561 return parseDirectiveFill();
1562 case DK_ZERO:
1563 return parseDirectiveZero();
1564 case DK_EXTERN:
1565 eatToEndOfStatement(); // .extern is the default, ignore it.
1566 return false;
1567 case DK_GLOBL:
1568 case DK_GLOBAL:
1569 return parseDirectiveSymbolAttribute(MCSA_Global);
1570 case DK_LAZY_REFERENCE:
1571 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1572 case DK_NO_DEAD_STRIP:
1573 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1574 case DK_SYMBOL_RESOLVER:
1575 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1576 case DK_PRIVATE_EXTERN:
1577 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1578 case DK_REFERENCE:
1579 return parseDirectiveSymbolAttribute(MCSA_Reference);
1580 case DK_WEAK_DEFINITION:
1581 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1582 case DK_WEAK_REFERENCE:
1583 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1584 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1585 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1586 case DK_COMM:
1587 case DK_COMMON:
1588 return parseDirectiveComm(/*IsLocal=*/false);
1589 case DK_LCOMM:
1590 return parseDirectiveComm(/*IsLocal=*/true);
1591 case DK_ABORT:
1592 return parseDirectiveAbort();
1593 case DK_INCLUDE:
1594 return parseDirectiveInclude();
1595 case DK_INCBIN:
1596 return parseDirectiveIncbin();
1597 case DK_CODE16:
1598 case DK_CODE16GCC:
1599 return TokError(Twine(IDVal) + " not supported yet");
1600 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001601 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001602 case DK_IRP:
1603 return parseDirectiveIrp(IDLoc);
1604 case DK_IRPC:
1605 return parseDirectiveIrpc(IDLoc);
1606 case DK_ENDR:
1607 return parseDirectiveEndr(IDLoc);
1608 case DK_BUNDLE_ALIGN_MODE:
1609 return parseDirectiveBundleAlignMode();
1610 case DK_BUNDLE_LOCK:
1611 return parseDirectiveBundleLock();
1612 case DK_BUNDLE_UNLOCK:
1613 return parseDirectiveBundleUnlock();
1614 case DK_SLEB128:
1615 return parseDirectiveLEB128(true);
1616 case DK_ULEB128:
1617 return parseDirectiveLEB128(false);
1618 case DK_SPACE:
1619 case DK_SKIP:
1620 return parseDirectiveSpace(IDVal);
1621 case DK_FILE:
1622 return parseDirectiveFile(IDLoc);
1623 case DK_LINE:
1624 return parseDirectiveLine();
1625 case DK_LOC:
1626 return parseDirectiveLoc();
1627 case DK_STABS:
1628 return parseDirectiveStabs();
1629 case DK_CFI_SECTIONS:
1630 return parseDirectiveCFISections();
1631 case DK_CFI_STARTPROC:
1632 return parseDirectiveCFIStartProc();
1633 case DK_CFI_ENDPROC:
1634 return parseDirectiveCFIEndProc();
1635 case DK_CFI_DEF_CFA:
1636 return parseDirectiveCFIDefCfa(IDLoc);
1637 case DK_CFI_DEF_CFA_OFFSET:
1638 return parseDirectiveCFIDefCfaOffset();
1639 case DK_CFI_ADJUST_CFA_OFFSET:
1640 return parseDirectiveCFIAdjustCfaOffset();
1641 case DK_CFI_DEF_CFA_REGISTER:
1642 return parseDirectiveCFIDefCfaRegister(IDLoc);
1643 case DK_CFI_OFFSET:
1644 return parseDirectiveCFIOffset(IDLoc);
1645 case DK_CFI_REL_OFFSET:
1646 return parseDirectiveCFIRelOffset(IDLoc);
1647 case DK_CFI_PERSONALITY:
1648 return parseDirectiveCFIPersonalityOrLsda(true);
1649 case DK_CFI_LSDA:
1650 return parseDirectiveCFIPersonalityOrLsda(false);
1651 case DK_CFI_REMEMBER_STATE:
1652 return parseDirectiveCFIRememberState();
1653 case DK_CFI_RESTORE_STATE:
1654 return parseDirectiveCFIRestoreState();
1655 case DK_CFI_SAME_VALUE:
1656 return parseDirectiveCFISameValue(IDLoc);
1657 case DK_CFI_RESTORE:
1658 return parseDirectiveCFIRestore(IDLoc);
1659 case DK_CFI_ESCAPE:
1660 return parseDirectiveCFIEscape();
1661 case DK_CFI_SIGNAL_FRAME:
1662 return parseDirectiveCFISignalFrame();
1663 case DK_CFI_UNDEFINED:
1664 return parseDirectiveCFIUndefined(IDLoc);
1665 case DK_CFI_REGISTER:
1666 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001667 case DK_CFI_WINDOW_SAVE:
1668 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 case DK_MACROS_ON:
1670 case DK_MACROS_OFF:
1671 return parseDirectiveMacrosOnOff(IDVal);
1672 case DK_MACRO:
1673 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001674 case DK_EXITM:
1675 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 case DK_ENDM:
1677 case DK_ENDMACRO:
1678 return parseDirectiveEndMacro(IDVal);
1679 case DK_PURGEM:
1680 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001681 case DK_END:
1682 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001683 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001684 return parseDirectiveError(IDLoc, false);
1685 case DK_ERROR:
1686 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001687 case DK_WARNING:
1688 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001689 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001690
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001691 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001692 }
Chris Lattner36e02122009-06-21 20:54:55 +00001693
Chad Rosierc7f552c2013-02-12 21:33:51 +00001694 // __asm _emit or __asm __emit
1695 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1696 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001697 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001698
1699 // __asm align
1700 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001702
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001703 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001704
Chris Lattner7cbfa442010-05-19 23:34:33 +00001705 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001706 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001707 ParseInstructionInfo IInfo(Info.AsmRewrites);
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001708 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001709 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001710 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001711
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001712 // Dump the parsed representation, if requested.
1713 if (getShowParsedOperands()) {
1714 SmallString<256> Str;
1715 raw_svector_ostream OS(Str);
1716 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001717 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001718 if (i != 0)
1719 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001720 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001721 }
1722 OS << "]";
1723
Jim Grosbach4b905842013-09-20 23:08:21 +00001724 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001725 }
1726
Oliver Stannard8b273082014-06-19 15:52:37 +00001727 // If we are generating dwarf for the current section then generate a .loc
1728 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001729 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001730 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001731 getStreamer().getCurrentSection().first)) {
1732 unsigned Line;
1733 if (ActiveMacros.empty())
1734 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1735 else
Frederic Riss16238d92015-06-25 21:57:33 +00001736 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1737 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001738
Eli Bendersky88024712013-01-16 19:32:36 +00001739 // If we previously parsed a cpp hash file line comment then make sure the
1740 // current Dwarf File is for the CppHashFilename if not then emit the
1741 // Dwarf File table for it and adjust the line number for the .loc.
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001742 if (CppHashFilename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001743 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1744 0, StringRef(), CppHashFilename);
1745 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001746
Jim Grosbach4b905842013-09-20 23:08:21 +00001747 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1748 // cache with the different Loc from the call above we save the last
1749 // info we queried here with SrcMgr.FindLineNumber().
1750 unsigned CppHashLocLineNo;
1751 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1752 CppHashLocLineNo = LastQueryLine;
1753 else {
1754 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1755 LastQueryLine = CppHashLocLineNo;
1756 LastQueryIDLoc = CppHashLoc;
1757 LastQueryBuffer = CppHashBuf;
1758 }
1759 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001760 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001761
Jim Grosbach4b905842013-09-20 23:08:21 +00001762 getStreamer().EmitDwarfLocDirective(
1763 getContext().getGenDwarfFileNumber(), Line, 0,
1764 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1765 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001766 }
1767
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001768 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001769 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001770 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001771 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1772 Info.ParsedOperands, Out,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001773 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001774 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001775
Chris Lattnera2a9d162010-09-11 16:18:25 +00001776 // Don't skip the rest of the line, the instruction parser is responsible for
1777 // that.
1778 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001779}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001780
Jim Grosbach4b905842013-09-20 23:08:21 +00001781/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001782/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001783void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001784 if (!Lexer.is(AsmToken::EndOfStatement))
1785 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 // Eat EOL.
1787 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001788}
1789
Jim Grosbach4b905842013-09-20 23:08:21 +00001790/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001791/// ::= # number "filename"
1792/// or just as a full line comment if it doesn't have a number and a string.
Craig Topper3c76c522015-09-20 23:35:59 +00001793bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001794 Lex(); // Eat the hash token.
1795
1796 if (getLexer().isNot(AsmToken::Integer)) {
1797 // Consume the line since in cases it is not a well-formed line directive,
1798 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001799 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001800 return false;
1801 }
1802
1803 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001804 Lex();
1805
1806 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001808 return false;
1809 }
1810
1811 StringRef Filename = getTok().getString();
1812 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001813 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001814
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001815 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1816 CppHashLoc = L;
1817 CppHashFilename = Filename;
1818 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001819 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001820
1821 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001822 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001823 return false;
1824}
1825
Jim Grosbach4b905842013-09-20 23:08:21 +00001826/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001827/// for the Filename and LineNo if any in the diagnostic.
1828void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001829 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001830 raw_ostream &OS = errs();
1831
1832 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00001833 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001834 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1835 unsigned CppHashBuf =
1836 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001837
Jim Grosbach4b905842013-09-20 23:08:21 +00001838 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001839 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001840 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1841 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1842 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001843 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1844 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001845 }
1846
Eric Christophera7c32732012-12-18 00:30:54 +00001847 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001848 // manager changed or buffer changed (like in a nested include) then just
1849 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001850 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001851 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001852 if (Parser->SavedDiagHandler)
1853 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1854 else
Craig Topper353eda42014-04-24 06:44:33 +00001855 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001856 return;
1857 }
1858
Eric Christophera7c32732012-12-18 00:30:54 +00001859 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001860 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1861 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001862 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001863
1864 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1865 int CppHashLocLineNo =
1866 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001867 int LineNo =
1868 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001869
Jim Grosbach4b905842013-09-20 23:08:21 +00001870 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1871 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001872 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001873
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001874 if (Parser->SavedDiagHandler)
1875 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1876 else
Craig Topper353eda42014-04-24 06:44:33 +00001877 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001878}
1879
Rafael Espindola2c064482012-08-21 18:29:30 +00001880// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1881// difference being that that function accepts '@' as part of identifiers and
1882// we can't do that. AsmLexer.cpp should probably be changed to handle
1883// '@' as a special case when needed.
1884static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001885 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1886 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001887}
1888
Rafael Espindola34b9c512012-06-03 23:57:14 +00001889bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001890 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00001891 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00001892 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001893 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001894 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001895 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001896 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001897
Preston Gurd05500642012-09-19 20:36:12 +00001898 // A macro without parameters is handled differently on Darwin:
1899 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001900 while (!Body.empty()) {
1901 // Scan for the next substitution.
1902 std::size_t End = Body.size(), Pos = 0;
1903 for (; Pos != End; ++Pos) {
1904 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001905 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001906 // This macro has no parameters, look for $0, $1, etc.
1907 if (Body[Pos] != '$' || Pos + 1 == End)
1908 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001909
Rafael Espindola1134ab232011-06-05 02:43:45 +00001910 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001911 if (Next == '$' || Next == 'n' ||
1912 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001913 break;
1914 } else {
1915 // This macro has parameters, look for \foo, \bar, etc.
1916 if (Body[Pos] == '\\' && Pos + 1 != End)
1917 break;
1918 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001919 }
1920
1921 // Add the prefix.
1922 OS << Body.slice(0, Pos);
1923
1924 // Check if we reached the end.
1925 if (Pos == End)
1926 break;
1927
Benjamin Kramer513e7442014-02-20 13:36:32 +00001928 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001929 switch (Body[Pos + 1]) {
1930 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001931 case '$':
1932 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001933 break;
1934
Jim Grosbach4b905842013-09-20 23:08:21 +00001935 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001936 case 'n':
1937 OS << A.size();
1938 break;
1939
Jim Grosbach4b905842013-09-20 23:08:21 +00001940 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001941 default: {
1942 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001943 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001944 if (Index >= A.size())
1945 break;
1946
1947 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00001948 for (const AsmToken &Token : A[Index])
1949 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001950 break;
1951 }
1952 }
1953 Pos += 2;
1954 } else {
1955 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00001956
1957 // Check for the \@ pseudo-variable.
1958 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001959 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00001960 else
1961 while (isIdentifierChar(Body[I]) && I + 1 != End)
1962 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001963
Jim Grosbach4b905842013-09-20 23:08:21 +00001964 const char *Begin = Body.data() + Pos + 1;
1965 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001966 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001967
Toma Tabacu217116e2015-04-27 10:50:29 +00001968 if (Argument == "@") {
1969 OS << NumOfMacroInstantiations;
1970 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00001971 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00001972 for (; Index < NParameters; ++Index)
1973 if (Parameters[Index].Name == Argument)
1974 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00001975
Toma Tabacu217116e2015-04-27 10:50:29 +00001976 if (Index == NParameters) {
1977 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1978 Pos += 3;
1979 else {
1980 OS << '\\' << Argument;
1981 Pos = I;
1982 }
1983 } else {
1984 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00001985 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00001986 // We expect no quotes around the string's contents when
1987 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00001988 if (Token.getKind() != AsmToken::String || VarargParameter)
1989 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00001990 else
Craig Topper84008482015-10-10 05:38:14 +00001991 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00001992
1993 Pos += 1 + Argument.size();
1994 }
Preston Gurd05500642012-09-19 20:36:12 +00001995 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001996 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001997 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001998 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001999 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002000
Rafael Espindola1134ab232011-06-05 02:43:45 +00002001 return false;
2002}
Daniel Dunbar43235712010-07-18 18:54:11 +00002003
Nico Weber2a8f9222014-07-24 16:29:04 +00002004MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002005 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002006 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002007 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002008
Jim Grosbach4b905842013-09-20 23:08:21 +00002009static bool isOperator(AsmToken::TokenKind kind) {
2010 switch (kind) {
2011 default:
2012 return false;
2013 case AsmToken::Plus:
2014 case AsmToken::Minus:
2015 case AsmToken::Tilde:
2016 case AsmToken::Slash:
2017 case AsmToken::Star:
2018 case AsmToken::Dot:
2019 case AsmToken::Equal:
2020 case AsmToken::EqualEqual:
2021 case AsmToken::Pipe:
2022 case AsmToken::PipePipe:
2023 case AsmToken::Caret:
2024 case AsmToken::Amp:
2025 case AsmToken::AmpAmp:
2026 case AsmToken::Exclaim:
2027 case AsmToken::ExclaimEqual:
2028 case AsmToken::Percent:
2029 case AsmToken::Less:
2030 case AsmToken::LessEqual:
2031 case AsmToken::LessLess:
2032 case AsmToken::LessGreater:
2033 case AsmToken::Greater:
2034 case AsmToken::GreaterEqual:
2035 case AsmToken::GreaterGreater:
2036 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002037 }
2038}
2039
David Majnemer16252452014-01-29 00:07:39 +00002040namespace {
2041class AsmLexerSkipSpaceRAII {
2042public:
2043 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2044 Lexer.setSkipSpace(SkipSpace);
2045 }
2046
2047 ~AsmLexerSkipSpaceRAII() {
2048 Lexer.setSkipSpace(true);
2049 }
2050
2051private:
2052 AsmLexer &Lexer;
2053};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002054}
David Majnemer16252452014-01-29 00:07:39 +00002055
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002056bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2057
2058 if (Vararg) {
2059 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2060 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002061 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002062 }
2063 return false;
2064 }
2065
Rafael Espindola768b41c2012-06-15 14:02:34 +00002066 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002067 unsigned AddTokens = 0;
2068
David Majnemer16252452014-01-29 00:07:39 +00002069 // Darwin doesn't use spaces to delmit arguments.
2070 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002071
2072 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00002073 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002074 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002075
David Majnemer91fc4c22014-01-29 18:57:46 +00002076 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00002077 break;
Preston Gurd05500642012-09-19 20:36:12 +00002078
2079 if (Lexer.is(AsmToken::Space)) {
2080 Lex(); // Eat spaces
2081
2082 // Spaces can delimit parameters, but could also be part an expression.
2083 // If the token after a space is an operator, add the token and the next
2084 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002085 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002086 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00002087 // Check to see whether the token is used as an operator,
2088 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00002089 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00002090 if (*NextChar == ' ')
2091 AddTokens = 2;
2092 }
2093
2094 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002095 break;
2096 }
2097 }
2098 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002099
Jim Grosbach4b905842013-09-20 23:08:21 +00002100 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002101 // to be able to fill in the remaining default parameter values
2102 if (Lexer.is(AsmToken::EndOfStatement))
2103 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002104
2105 // Adjust the current parentheses level.
2106 if (Lexer.is(AsmToken::LParen))
2107 ++ParenLevel;
2108 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2109 --ParenLevel;
2110
2111 // Append the token to the current argument list.
2112 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00002113 if (AddTokens)
2114 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002115 Lex();
2116 }
Preston Gurd05500642012-09-19 20:36:12 +00002117
Rafael Espindola768b41c2012-06-15 14:02:34 +00002118 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002119 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002120 return false;
2121}
2122
2123// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002124bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002125 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002126 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002127 bool NamedParametersFound = false;
2128 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002129
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002130 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002131 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002132
Rafael Espindola768b41c2012-06-15 14:02:34 +00002133 // Parse two kinds of macro invocations:
2134 // - macros defined without any parameters accept an arbitrary number of them
2135 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002136 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002137 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2138 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002139 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002140 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002141
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002142 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002143 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002144 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002145 eatToEndOfStatement();
2146 return true;
2147 }
2148
2149 if (!Lexer.is(AsmToken::Equal)) {
2150 TokError("expected '=' after formal parameter identifier");
2151 eatToEndOfStatement();
2152 return true;
2153 }
2154 Lex();
2155
2156 NamedParametersFound = true;
2157 }
2158
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002159 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002160 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002161 eatToEndOfStatement();
2162 return true;
2163 }
2164
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002165 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2166 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002167 return true;
2168
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002169 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002170 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002171 unsigned FAI = 0;
2172 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002173 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002174 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002175
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002176 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002177 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002178 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002179 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002180 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002181 return true;
2182 }
2183 PI = FAI;
2184 }
2185
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002186 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002187 if (A.size() <= PI)
2188 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002189 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002190
2191 if (FALocs.size() <= PI)
2192 FALocs.resize(PI + 1);
2193
2194 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002195 }
Jim Grosbach206661622012-07-30 22:44:17 +00002196
Preston Gurd242ed3152012-09-19 20:29:04 +00002197 // At the end of the statement, fill in remaining arguments that have
2198 // default values. If there aren't any, then the next argument is
2199 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002200 if (Lexer.is(AsmToken::EndOfStatement)) {
2201 bool Failure = false;
2202 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2203 if (A[FAI].empty()) {
2204 if (M->Parameters[FAI].Required) {
2205 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2206 "missing value for required parameter "
2207 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2208 Failure = true;
2209 }
2210
2211 if (!M->Parameters[FAI].Value.empty())
2212 A[FAI] = M->Parameters[FAI].Value;
2213 }
2214 }
2215 return Failure;
2216 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002217
2218 if (Lexer.is(AsmToken::Comma))
2219 Lex();
2220 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002221
2222 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002223}
2224
Jim Grosbach4b905842013-09-20 23:08:21 +00002225const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002226 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2227 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002228}
2229
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002230void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2231 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002232}
2233
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002234void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002235
Jim Grosbach4b905842013-09-20 23:08:21 +00002236bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002237 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2238 // this, although we should protect against infinite loops.
2239 if (ActiveMacros.size() == 20)
2240 return TokError("macros cannot be nested more than 20 levels deep");
2241
Eli Bendersky38274122013-01-14 23:22:36 +00002242 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002243 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002244 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002245
Rafael Espindola1134ab232011-06-05 02:43:45 +00002246 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2247 // to hold the macro body with substitutions.
2248 SmallString<256> Buf;
2249 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002250 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002251
Toma Tabacu217116e2015-04-27 10:50:29 +00002252 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002253 return true;
2254
Eli Bendersky38274122013-01-14 23:22:36 +00002255 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002256 // instantiation.
2257 OS << ".endmacro\n";
2258
Rafael Espindola3560ff22014-08-27 20:03:13 +00002259 std::unique_ptr<MemoryBuffer> Instantiation =
2260 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002261
Daniel Dunbar43235712010-07-18 18:54:11 +00002262 // Create the macro instantiation object and add to the current macro
2263 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002264 MacroInstantiation *MI = new MacroInstantiation(
2265 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002266 ActiveMacros.push_back(MI);
2267
Toma Tabacu217116e2015-04-27 10:50:29 +00002268 ++NumOfMacroInstantiations;
2269
Daniel Dunbar43235712010-07-18 18:54:11 +00002270 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002271 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002272 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002273 Lex();
2274
2275 return false;
2276}
2277
Jim Grosbach4b905842013-09-20 23:08:21 +00002278void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002279 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002280 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002281 Lex();
2282
2283 // Pop the instantiation entry.
2284 delete ActiveMacros.back();
2285 ActiveMacros.pop_back();
2286}
2287
Jim Grosbach4b905842013-09-20 23:08:21 +00002288bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002289 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002290 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002291 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002292 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2293 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002294 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002295
Pete Cooper80d21cb2015-06-22 19:35:57 +00002296 if (!Sym) {
2297 // In the case where we parse an expression starting with a '.', we will
2298 // not generate an error, nor will we create a symbol. In this case we
2299 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002300 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002301 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002302
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002303 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002304 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002305 if (NoDeadStrip)
2306 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2307
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002308 return false;
2309}
2310
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002311/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002312/// ::= identifier
2313/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002314bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002315 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002316 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2317 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002318 // handle this as a context dependent token, instead we detect adjacent tokens
2319 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002320 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2321 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002322
Hans Wennborgce69d772013-10-18 20:46:28 +00002323 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002324 Lex();
2325 if (Lexer.isNot(AsmToken::Identifier))
2326 return true;
2327
Hans Wennborgce69d772013-10-18 20:46:28 +00002328 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2329 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002330 return true;
2331
2332 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002333 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002334 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002335 Lex();
2336 return false;
2337 }
2338
Jim Grosbach4b905842013-09-20 23:08:21 +00002339 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002340 return true;
2341
Sean Callanan936b0d32010-01-19 21:44:56 +00002342 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002343
Sean Callanan686ed8d2010-01-19 20:22:31 +00002344 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002345
2346 return false;
2347}
2348
Jim Grosbach4b905842013-09-20 23:08:21 +00002349/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002350/// ::= .equ identifier ',' expression
2351/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002352/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002353bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002354 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002355
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002356 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002357 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002358
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002360 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002362
Jim Grosbach4b905842013-09-20 23:08:21 +00002363 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002364}
2365
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002366bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002368
2369 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002370 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002371 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2372 if (Str[i] != '\\') {
2373 Data += Str[i];
2374 continue;
2375 }
2376
2377 // Recognize escaped characters. Note that this escape semantics currently
2378 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2379 ++i;
2380 if (i == e)
2381 return TokError("unexpected backslash at end of string");
2382
2383 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002384 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002385 // Consume up to three octal characters.
2386 unsigned Value = Str[i] - '0';
2387
Jim Grosbach4b905842013-09-20 23:08:21 +00002388 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002389 ++i;
2390 Value = Value * 8 + (Str[i] - '0');
2391
Jim Grosbach4b905842013-09-20 23:08:21 +00002392 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002393 ++i;
2394 Value = Value * 8 + (Str[i] - '0');
2395 }
2396 }
2397
2398 if (Value > 255)
2399 return TokError("invalid octal escape sequence (out of range)");
2400
Jim Grosbach4b905842013-09-20 23:08:21 +00002401 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002402 continue;
2403 }
2404
2405 // Otherwise recognize individual escapes.
2406 switch (Str[i]) {
2407 default:
2408 // Just reject invalid escape sequences for now.
2409 return TokError("invalid escape sequence (unrecognized character)");
2410
2411 case 'b': Data += '\b'; break;
2412 case 'f': Data += '\f'; break;
2413 case 'n': Data += '\n'; break;
2414 case 'r': Data += '\r'; break;
2415 case 't': Data += '\t'; break;
2416 case '"': Data += '"'; break;
2417 case '\\': Data += '\\'; break;
2418 }
2419 }
2420
2421 return false;
2422}
2423
Jim Grosbach4b905842013-09-20 23:08:21 +00002424/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002425/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002426bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002427 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002428 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002429
Daniel Dunbara10e5192009-06-24 23:30:00 +00002430 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002431 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002432 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002433
Daniel Dunbaref668c12009-08-14 18:19:52 +00002434 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002435 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002436 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002437
Rafael Espindola64e1af82013-07-02 15:49:13 +00002438 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002439 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002440 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002441
Sean Callanan686ed8d2010-01-19 20:22:31 +00002442 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002443
2444 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002445 break;
2446
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002447 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002448 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002449 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002450 }
2451 }
2452
Sean Callanan686ed8d2010-01-19 20:22:31 +00002453 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002454 return false;
2455}
2456
Jim Grosbach4b905842013-09-20 23:08:21 +00002457/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002458/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002459bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002460 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002461 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002462
Daniel Dunbara10e5192009-06-24 23:30:00 +00002463 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002464 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002465 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002466 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002467 return true;
2468
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002469 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002470 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2471 assert(Size <= 8 && "Invalid size");
2472 uint64_t IntValue = MCE->getValue();
2473 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2474 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002475 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002476 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002477 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002478
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002479 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002480 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002481
Daniel Dunbara10e5192009-06-24 23:30:00 +00002482 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002483 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002484 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002485 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002486 }
2487 }
2488
Sean Callanan686ed8d2010-01-19 20:22:31 +00002489 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002490 return false;
2491}
2492
David Woodhoused6de0d92014-02-01 16:20:59 +00002493/// ParseDirectiveOctaValue
2494/// ::= .octa [ hexconstant (, hexconstant)* ]
2495bool AsmParser::parseDirectiveOctaValue() {
2496 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2497 checkForValidSection();
2498
2499 for (;;) {
2500 if (Lexer.getKind() == AsmToken::Error)
2501 return true;
2502 if (Lexer.getKind() != AsmToken::Integer &&
2503 Lexer.getKind() != AsmToken::BigNum)
2504 return TokError("unknown token in expression");
2505
2506 SMLoc ExprLoc = getLexer().getLoc();
2507 APInt IntValue = getTok().getAPIntVal();
2508 Lex();
2509
2510 uint64_t hi, lo;
2511 if (IntValue.isIntN(64)) {
2512 hi = 0;
2513 lo = IntValue.getZExtValue();
2514 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002515 // It might actually have more than 128 bits, but the top ones are zero.
2516 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002517 lo = IntValue.getLoBits(64).getZExtValue();
2518 } else
2519 return Error(ExprLoc, "literal value out of range for directive");
2520
2521 if (MAI.isLittleEndian()) {
2522 getStreamer().EmitIntValue(lo, 8);
2523 getStreamer().EmitIntValue(hi, 8);
2524 } else {
2525 getStreamer().EmitIntValue(hi, 8);
2526 getStreamer().EmitIntValue(lo, 8);
2527 }
2528
2529 if (getLexer().is(AsmToken::EndOfStatement))
2530 break;
2531
2532 // FIXME: Improve diagnostic.
2533 if (getLexer().isNot(AsmToken::Comma))
2534 return TokError("unexpected token in directive");
2535 Lex();
2536 }
2537 }
2538
2539 Lex();
2540 return false;
2541}
2542
Jim Grosbach4b905842013-09-20 23:08:21 +00002543/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002544/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002545bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002546 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002547 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002548
2549 for (;;) {
2550 // We don't truly support arithmetic on floating point expressions, so we
2551 // have to manually parse unary prefixes.
2552 bool IsNeg = false;
2553 if (getLexer().is(AsmToken::Minus)) {
2554 Lex();
2555 IsNeg = true;
2556 } else if (getLexer().is(AsmToken::Plus))
2557 Lex();
2558
Michael J. Spencer530ce852010-10-09 11:00:50 +00002559 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002560 getLexer().isNot(AsmToken::Real) &&
2561 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002562 return TokError("unexpected token in directive");
2563
2564 // Convert to an APFloat.
2565 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002566 StringRef IDVal = getTok().getString();
2567 if (getLexer().is(AsmToken::Identifier)) {
2568 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2569 Value = APFloat::getInf(Semantics);
2570 else if (!IDVal.compare_lower("nan"))
2571 Value = APFloat::getNaN(Semantics, false, ~0);
2572 else
2573 return TokError("invalid floating point literal");
2574 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002575 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002576 return TokError("invalid floating point literal");
2577 if (IsNeg)
2578 Value.changeSign();
2579
2580 // Consume the numeric token.
2581 Lex();
2582
2583 // Emit the value as an integer.
2584 APInt AsInt = Value.bitcastToAPInt();
2585 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002586 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002587
2588 if (getLexer().is(AsmToken::EndOfStatement))
2589 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002590
Daniel Dunbar2af16532010-09-24 01:59:56 +00002591 if (getLexer().isNot(AsmToken::Comma))
2592 return TokError("unexpected token in directive");
2593 Lex();
2594 }
2595 }
2596
2597 Lex();
2598 return false;
2599}
2600
Jim Grosbach4b905842013-09-20 23:08:21 +00002601/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002602/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002603bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002604 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002605
2606 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002607 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002608 return true;
2609
Rafael Espindolab91bac62010-10-05 19:42:57 +00002610 int64_t Val = 0;
2611 if (getLexer().is(AsmToken::Comma)) {
2612 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002613 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002614 return true;
2615 }
2616
Rafael Espindola922e3f42010-09-16 15:03:59 +00002617 if (getLexer().isNot(AsmToken::EndOfStatement))
2618 return TokError("unexpected token in '.zero' directive");
2619
2620 Lex();
2621
Rafael Espindola64e1af82013-07-02 15:49:13 +00002622 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002623
2624 return false;
2625}
2626
Jim Grosbach4b905842013-09-20 23:08:21 +00002627/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002628/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002629bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002630 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002631
David Majnemer522d3db2014-02-01 07:19:38 +00002632 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002633 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002634 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002635 return true;
2636
David Majnemer522d3db2014-02-01 07:19:38 +00002637 if (NumValues < 0) {
2638 Warning(RepeatLoc,
2639 "'.fill' directive with negative repeat count has no effect");
2640 NumValues = 0;
2641 }
2642
Roman Divackye33098f2013-09-24 17:44:41 +00002643 int64_t FillSize = 1;
2644 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002645
David Majnemer522d3db2014-02-01 07:19:38 +00002646 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002647 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2648 if (getLexer().isNot(AsmToken::Comma))
2649 return TokError("unexpected token in '.fill' directive");
2650 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002651
David Majnemer522d3db2014-02-01 07:19:38 +00002652 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002653 if (parseAbsoluteExpression(FillSize))
2654 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002655
Roman Divackye33098f2013-09-24 17:44:41 +00002656 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2657 if (getLexer().isNot(AsmToken::Comma))
2658 return TokError("unexpected token in '.fill' directive");
2659 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002660
David Majnemer522d3db2014-02-01 07:19:38 +00002661 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002662 if (parseAbsoluteExpression(FillExpr))
2663 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002664
Roman Divackye33098f2013-09-24 17:44:41 +00002665 if (getLexer().isNot(AsmToken::EndOfStatement))
2666 return TokError("unexpected token in '.fill' directive");
2667
2668 Lex();
2669 }
2670 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002671
David Majnemer522d3db2014-02-01 07:19:38 +00002672 if (FillSize < 0) {
2673 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2674 NumValues = 0;
2675 }
2676 if (FillSize > 8) {
2677 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2678 FillSize = 8;
2679 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002680
David Majnemer522d3db2014-02-01 07:19:38 +00002681 if (!isUInt<32>(FillExpr) && FillSize > 4)
2682 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2683
Alexey Samsonov1b0713c2014-09-02 17:25:29 +00002684 if (NumValues > 0) {
2685 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2686 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2687 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2688 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2689 if (NonZeroFillSize < FillSize)
2690 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2691 }
David Majnemer522d3db2014-02-01 07:19:38 +00002692 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002693
2694 return false;
2695}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002696
Jim Grosbach4b905842013-09-20 23:08:21 +00002697/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002698/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002699bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002700 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002701
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002702 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002703 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002704 return true;
2705
2706 // Parse optional fill expression.
2707 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002708 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2709 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002710 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002711 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002712
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002713 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002714 return true;
2715
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002716 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002717 return TokError("unexpected token in '.org' directive");
2718 }
2719
Sean Callanan686ed8d2010-01-19 20:22:31 +00002720 Lex();
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002721 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002722 return false;
2723}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002724
Jim Grosbach4b905842013-09-20 23:08:21 +00002725/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002726/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002727bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002728 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002729
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002730 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002731 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002732 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002733 return true;
2734
2735 SMLoc MaxBytesLoc;
2736 bool HasFillExpr = false;
2737 int64_t FillExpr = 0;
2738 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002739 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2740 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002741 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002742 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002743
2744 // The fill expression can be omitted while specifying a maximum number of
2745 // alignment bytes, e.g:
2746 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002747 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002748 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002749 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002750 return true;
2751 }
2752
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002753 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2754 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002755 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002756 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002757
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002758 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002759 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002760 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002761
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002762 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002763 return TokError("unexpected token in directive");
2764 }
2765 }
2766
Sean Callanan686ed8d2010-01-19 20:22:31 +00002767 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002768
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002769 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002770 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002771
2772 // Compute alignment in bytes.
2773 if (IsPow2) {
2774 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002775 if (Alignment >= 32) {
2776 Error(AlignmentLoc, "invalid alignment value");
2777 Alignment = 31;
2778 }
2779
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002780 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002781 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00002782 // Reject alignments that aren't either a power of two or zero,
2783 // for gas compatibility. Alignment of zero is silently rounded
2784 // up to one.
2785 if (Alignment == 0)
2786 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002787 if (!isPowerOf2_64(Alignment))
2788 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002789 }
2790
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002791 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002792 if (MaxBytesLoc.isValid()) {
2793 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002794 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002795 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002796 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002797 }
2798
2799 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002800 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002801 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002802 MaxBytesToFill = 0;
2803 }
2804 }
2805
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002806 // Check whether we should use optimal code alignment for this .align
2807 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002808 const MCSection *Section = getStreamer().getCurrentSection().first;
2809 assert(Section && "must have section to emit alignment");
2810 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002811 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2812 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002813 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002814 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002815 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002816 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2817 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002818 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002819
2820 return false;
2821}
2822
Jim Grosbach4b905842013-09-20 23:08:21 +00002823/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002824/// ::= .file [number] filename
2825/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002826bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002827 // FIXME: I'm not sure what this is.
2828 int64_t FileNumber = -1;
2829 SMLoc FileNumberLoc = getLexer().getLoc();
2830 if (getLexer().is(AsmToken::Integer)) {
2831 FileNumber = getTok().getIntVal();
2832 Lex();
2833
2834 if (FileNumber < 1)
2835 return TokError("file number less than one");
2836 }
2837
2838 if (getLexer().isNot(AsmToken::String))
2839 return TokError("unexpected token in '.file' directive");
2840
2841 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002842 // Allow the strings to have escaped octal character sequence.
2843 std::string Path = getTok().getString();
2844 if (parseEscapedString(Path))
2845 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002846 Lex();
2847
2848 StringRef Directory;
2849 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002850 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002851 if (getLexer().is(AsmToken::String)) {
2852 if (FileNumber == -1)
2853 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002854 if (parseEscapedString(FilenameData))
2855 return true;
2856 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002857 Directory = Path;
2858 Lex();
2859 } else {
2860 Filename = Path;
2861 }
2862
2863 if (getLexer().isNot(AsmToken::EndOfStatement))
2864 return TokError("unexpected token in '.file' directive");
2865
2866 if (FileNumber == -1)
2867 getStreamer().EmitFileDirective(Filename);
2868 else {
David Blaikiedc3f01e2015-03-09 01:57:13 +00002869 if (getContext().getGenDwarfForAssembly())
Jim Grosbach4b905842013-09-20 23:08:21 +00002870 Error(DirectiveLoc,
2871 "input can't have .file dwarf directives when -g is "
2872 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002873
David Blaikiec714ef42014-03-17 01:52:11 +00002874 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2875 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002876 Error(FileNumberLoc, "file number already allocated");
2877 }
2878
2879 return false;
2880}
2881
Jim Grosbach4b905842013-09-20 23:08:21 +00002882/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002883/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002884bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002885 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2886 if (getLexer().isNot(AsmToken::Integer))
2887 return TokError("unexpected token in '.line' directive");
2888
2889 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002890 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002891 Lex();
2892
2893 // FIXME: Do something with the .line.
2894 }
2895
2896 if (getLexer().isNot(AsmToken::EndOfStatement))
2897 return TokError("unexpected token in '.line' directive");
2898
2899 return false;
2900}
2901
Jim Grosbach4b905842013-09-20 23:08:21 +00002902/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002903/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2904/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2905/// The first number is a file number, must have been previously assigned with
2906/// a .file directive, the second number is the line number and optionally the
2907/// third number is a column position (zero if not specified). The remaining
2908/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002909bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002910 if (getLexer().isNot(AsmToken::Integer))
2911 return TokError("unexpected token in '.loc' directive");
2912 int64_t FileNumber = getTok().getIntVal();
2913 if (FileNumber < 1)
2914 return TokError("file number less than one in '.loc' directive");
2915 if (!getContext().isValidDwarfFileNumber(FileNumber))
2916 return TokError("unassigned file number in '.loc' directive");
2917 Lex();
2918
2919 int64_t LineNumber = 0;
2920 if (getLexer().is(AsmToken::Integer)) {
2921 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002922 if (LineNumber < 0)
2923 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002924 Lex();
2925 }
2926
2927 int64_t ColumnPos = 0;
2928 if (getLexer().is(AsmToken::Integer)) {
2929 ColumnPos = getTok().getIntVal();
2930 if (ColumnPos < 0)
2931 return TokError("column position less than zero in '.loc' directive");
2932 Lex();
2933 }
2934
2935 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2936 unsigned Isa = 0;
2937 int64_t Discriminator = 0;
2938 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2939 for (;;) {
2940 if (getLexer().is(AsmToken::EndOfStatement))
2941 break;
2942
2943 StringRef Name;
2944 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002945 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002946 return TokError("unexpected token in '.loc' directive");
2947
2948 if (Name == "basic_block")
2949 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2950 else if (Name == "prologue_end")
2951 Flags |= DWARF2_FLAG_PROLOGUE_END;
2952 else if (Name == "epilogue_begin")
2953 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2954 else if (Name == "is_stmt") {
2955 Loc = getTok().getLoc();
2956 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002957 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002958 return true;
2959 // The expression must be the constant 0 or 1.
2960 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2961 int Value = MCE->getValue();
2962 if (Value == 0)
2963 Flags &= ~DWARF2_FLAG_IS_STMT;
2964 else if (Value == 1)
2965 Flags |= DWARF2_FLAG_IS_STMT;
2966 else
2967 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002968 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002969 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2970 }
Craig Topperf15655b2013-04-22 04:22:40 +00002971 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002972 Loc = getTok().getLoc();
2973 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002974 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002975 return true;
2976 // The expression must be a constant greater or equal to 0.
2977 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2978 int Value = MCE->getValue();
2979 if (Value < 0)
2980 return Error(Loc, "isa number less than zero");
2981 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002982 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002983 return Error(Loc, "isa number not a constant value");
2984 }
Craig Topperf15655b2013-04-22 04:22:40 +00002985 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002986 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002987 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002988 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002989 return Error(Loc, "unknown sub-directive in '.loc' directive");
2990 }
2991
2992 if (getLexer().is(AsmToken::EndOfStatement))
2993 break;
2994 }
2995 }
2996
2997 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2998 Isa, Discriminator, StringRef());
2999
3000 return false;
3001}
3002
Jim Grosbach4b905842013-09-20 23:08:21 +00003003/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003004/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003005bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return TokError("unsupported directive '.stabs'");
3007}
3008
Jim Grosbach4b905842013-09-20 23:08:21 +00003009/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003010/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003011bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003012 StringRef Name;
3013 bool EH = false;
3014 bool Debug = false;
3015
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003016 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return TokError("Expected an identifier");
3018
3019 if (Name == ".eh_frame")
3020 EH = true;
3021 else if (Name == ".debug_frame")
3022 Debug = true;
3023
3024 if (getLexer().is(AsmToken::Comma)) {
3025 Lex();
3026
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003027 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003028 return TokError("Expected an identifier");
3029
3030 if (Name == ".eh_frame")
3031 EH = true;
3032 else if (Name == ".debug_frame")
3033 Debug = true;
3034 }
3035
3036 getStreamer().EmitCFISections(EH, Debug);
3037 return false;
3038}
3039
Jim Grosbach4b905842013-09-20 23:08:21 +00003040/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003041/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003042bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003043 StringRef Simple;
3044 if (getLexer().isNot(AsmToken::EndOfStatement))
3045 if (parseIdentifier(Simple) || Simple != "simple")
3046 return TokError("unexpected token in .cfi_startproc directive");
3047
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003048 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003049 return false;
3050}
3051
Jim Grosbach4b905842013-09-20 23:08:21 +00003052/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003053/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003054bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003055 getStreamer().EmitCFIEndProc();
3056 return false;
3057}
3058
Jim Grosbach4b905842013-09-20 23:08:21 +00003059/// \brief parse register name or number.
3060bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003061 SMLoc DirectiveLoc) {
3062 unsigned RegNo;
3063
3064 if (getLexer().isNot(AsmToken::Integer)) {
3065 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3066 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003067 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003068 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003069 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003070
3071 return false;
3072}
3073
Jim Grosbach4b905842013-09-20 23:08:21 +00003074/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003075/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003076bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003077 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003078 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003079 return true;
3080
3081 if (getLexer().isNot(AsmToken::Comma))
3082 return TokError("unexpected token in directive");
3083 Lex();
3084
3085 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003086 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003087 return true;
3088
3089 getStreamer().EmitCFIDefCfa(Register, Offset);
3090 return false;
3091}
3092
Jim Grosbach4b905842013-09-20 23:08:21 +00003093/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003094/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003095bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003096 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003097 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return true;
3099
3100 getStreamer().EmitCFIDefCfaOffset(Offset);
3101 return false;
3102}
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003105/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003106bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003107 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003108 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 if (getLexer().isNot(AsmToken::Comma))
3112 return TokError("unexpected token in directive");
3113 Lex();
3114
3115 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003116 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003117 return true;
3118
3119 getStreamer().EmitCFIRegister(Register1, Register2);
3120 return false;
3121}
3122
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003123/// parseDirectiveCFIWindowSave
3124/// ::= .cfi_window_save
3125bool AsmParser::parseDirectiveCFIWindowSave() {
3126 getStreamer().EmitCFIWindowSave();
3127 return false;
3128}
3129
Jim Grosbach4b905842013-09-20 23:08:21 +00003130/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003131/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003132bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003133 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003134 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003135 return true;
3136
3137 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3138 return false;
3139}
3140
Jim Grosbach4b905842013-09-20 23:08:21 +00003141/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003142/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003143bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003144 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003145 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003146 return true;
3147
3148 getStreamer().EmitCFIDefCfaRegister(Register);
3149 return false;
3150}
3151
Jim Grosbach4b905842013-09-20 23:08:21 +00003152/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003153/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003154bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003155 int64_t Register = 0;
3156 int64_t Offset = 0;
3157
Jim Grosbach4b905842013-09-20 23:08:21 +00003158 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return true;
3160
3161 if (getLexer().isNot(AsmToken::Comma))
3162 return TokError("unexpected token in directive");
3163 Lex();
3164
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003165 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003166 return true;
3167
3168 getStreamer().EmitCFIOffset(Register, Offset);
3169 return false;
3170}
3171
Jim Grosbach4b905842013-09-20 23:08:21 +00003172/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003173/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003174bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003175 int64_t Register = 0;
3176
Jim Grosbach4b905842013-09-20 23:08:21 +00003177 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003178 return true;
3179
3180 if (getLexer().isNot(AsmToken::Comma))
3181 return TokError("unexpected token in directive");
3182 Lex();
3183
3184 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003185 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003186 return true;
3187
3188 getStreamer().EmitCFIRelOffset(Register, Offset);
3189 return false;
3190}
3191
3192static bool isValidEncoding(int64_t Encoding) {
3193 if (Encoding & ~0xff)
3194 return false;
3195
3196 if (Encoding == dwarf::DW_EH_PE_omit)
3197 return true;
3198
3199 const unsigned Format = Encoding & 0xf;
3200 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3201 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3202 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3203 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3204 return false;
3205
3206 const unsigned Application = Encoding & 0x70;
3207 if (Application != dwarf::DW_EH_PE_absptr &&
3208 Application != dwarf::DW_EH_PE_pcrel)
3209 return false;
3210
3211 return true;
3212}
3213
Jim Grosbach4b905842013-09-20 23:08:21 +00003214/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003215/// IsPersonality true for cfi_personality, false for cfi_lsda
3216/// ::= .cfi_personality encoding, [symbol_name]
3217/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003218bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003219 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003220 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003221 return true;
3222 if (Encoding == dwarf::DW_EH_PE_omit)
3223 return false;
3224
3225 if (!isValidEncoding(Encoding))
3226 return TokError("unsupported encoding.");
3227
3228 if (getLexer().isNot(AsmToken::Comma))
3229 return TokError("unexpected token in directive");
3230 Lex();
3231
3232 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003233 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003234 return TokError("expected identifier in directive");
3235
Jim Grosbach6f482002015-05-18 18:43:14 +00003236 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003237
3238 if (IsPersonality)
3239 getStreamer().EmitCFIPersonality(Sym, Encoding);
3240 else
3241 getStreamer().EmitCFILsda(Sym, Encoding);
3242 return false;
3243}
3244
Jim Grosbach4b905842013-09-20 23:08:21 +00003245/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003246/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003247bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003248 getStreamer().EmitCFIRememberState();
3249 return false;
3250}
3251
Jim Grosbach4b905842013-09-20 23:08:21 +00003252/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003253/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003254bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003255 getStreamer().EmitCFIRestoreState();
3256 return false;
3257}
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003260/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003261bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003262 int64_t Register = 0;
3263
Jim Grosbach4b905842013-09-20 23:08:21 +00003264 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003265 return true;
3266
3267 getStreamer().EmitCFISameValue(Register);
3268 return false;
3269}
3270
Jim Grosbach4b905842013-09-20 23:08:21 +00003271/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003272/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003273bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003274 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003275 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003276 return true;
3277
3278 getStreamer().EmitCFIRestore(Register);
3279 return false;
3280}
3281
Jim Grosbach4b905842013-09-20 23:08:21 +00003282/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003283/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003284bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003285 std::string Values;
3286 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003287 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003288 return true;
3289
3290 Values.push_back((uint8_t)CurrValue);
3291
3292 while (getLexer().is(AsmToken::Comma)) {
3293 Lex();
3294
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003295 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003296 return true;
3297
3298 Values.push_back((uint8_t)CurrValue);
3299 }
3300
3301 getStreamer().EmitCFIEscape(Values);
3302 return false;
3303}
3304
Jim Grosbach4b905842013-09-20 23:08:21 +00003305/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003306/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003307bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003308 if (getLexer().isNot(AsmToken::EndOfStatement))
3309 return Error(getLexer().getLoc(),
3310 "unexpected token in '.cfi_signal_frame'");
3311
3312 getStreamer().EmitCFISignalFrame();
3313 return false;
3314}
3315
Jim Grosbach4b905842013-09-20 23:08:21 +00003316/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003317/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003318bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003319 int64_t Register = 0;
3320
Jim Grosbach4b905842013-09-20 23:08:21 +00003321 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003322 return true;
3323
3324 getStreamer().EmitCFIUndefined(Register);
3325 return false;
3326}
3327
Jim Grosbach4b905842013-09-20 23:08:21 +00003328/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003329/// ::= .macros_on
3330/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003331bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003332 if (getLexer().isNot(AsmToken::EndOfStatement))
3333 return Error(getLexer().getLoc(),
3334 "unexpected token in '" + Directive + "' directive");
3335
Jim Grosbach4b905842013-09-20 23:08:21 +00003336 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003337 return false;
3338}
3339
Jim Grosbach4b905842013-09-20 23:08:21 +00003340/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003341/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003342bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003343 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003344 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003345 return TokError("expected identifier in '.macro' directive");
3346
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003347 if (getLexer().is(AsmToken::Comma))
3348 Lex();
3349
Eli Bendersky17233942013-01-15 22:59:42 +00003350 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003351 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003352
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003353 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003354 return Error(Lexer.getLoc(),
3355 "Vararg parameter '" + Parameters.back().Name +
3356 "' should be last one in the list of parameters.");
3357
David Majnemer91fc4c22014-01-29 18:57:46 +00003358 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003359 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003360 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003361
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003362 if (Lexer.is(AsmToken::Colon)) {
3363 Lex(); // consume ':'
3364
3365 SMLoc QualLoc;
3366 StringRef Qualifier;
3367
3368 QualLoc = Lexer.getLoc();
3369 if (parseIdentifier(Qualifier))
3370 return Error(QualLoc, "missing parameter qualifier for "
3371 "'" + Parameter.Name + "' in macro '" + Name + "'");
3372
3373 if (Qualifier == "req")
3374 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003375 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003376 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003377 else
3378 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3379 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3380 }
3381
David Majnemer91fc4c22014-01-29 18:57:46 +00003382 if (getLexer().is(AsmToken::Equal)) {
3383 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003384
3385 SMLoc ParamLoc;
3386
3387 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003388 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003389 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003390
3391 if (Parameter.Required)
3392 Warning(ParamLoc, "pointless default value for required parameter "
3393 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003394 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003395
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003396 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003397
3398 if (getLexer().is(AsmToken::Comma))
3399 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003400 }
3401
3402 // Eat the end of statement.
3403 Lex();
3404
3405 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003406 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003407
3408 // Lex the macro definition.
3409 for (;;) {
3410 // Check whether we have reached the end of the file.
3411 if (getLexer().is(AsmToken::Eof))
3412 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3413
3414 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003415 if (getLexer().is(AsmToken::Identifier)) {
3416 if (getTok().getIdentifier() == ".endm" ||
3417 getTok().getIdentifier() == ".endmacro") {
3418 if (MacroDepth == 0) { // Outermost macro.
3419 EndToken = getTok();
3420 Lex();
3421 if (getLexer().isNot(AsmToken::EndOfStatement))
3422 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3423 "' directive");
3424 break;
3425 } else {
3426 // Otherwise we just found the end of an inner macro.
3427 --MacroDepth;
3428 }
3429 } else if (getTok().getIdentifier() == ".macro") {
3430 // We allow nested macros. Those aren't instantiated until the outermost
3431 // macro is expanded so just ignore them for now.
3432 ++MacroDepth;
3433 }
Eli Bendersky17233942013-01-15 22:59:42 +00003434 }
3435
3436 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003437 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003438 }
3439
Jim Grosbach4b905842013-09-20 23:08:21 +00003440 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003441 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3442 }
3443
3444 const char *BodyStart = StartToken.getLoc().getPointer();
3445 const char *BodyEnd = EndToken.getLoc().getPointer();
3446 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003447 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003448 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003449 return false;
3450}
3451
Jim Grosbach4b905842013-09-20 23:08:21 +00003452/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003453///
3454/// With the support added for named parameters there may be code out there that
3455/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003456/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003457/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003458/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003459/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3460/// warning that the positional parameter found in body which have no effect.
3461/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003462/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003463/// intended or change the macro to use the named parameters. It is possible
3464/// this warning will trigger when the none of the named parameters are used
3465/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003466void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003467 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003468 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003469 // If this macro is not defined with named parameters the warning we are
3470 // checking for here doesn't apply.
3471 unsigned NParameters = Parameters.size();
3472 if (NParameters == 0)
3473 return;
3474
3475 bool NamedParametersFound = false;
3476 bool PositionalParametersFound = false;
3477
3478 // Look at the body of the macro for use of both the named parameters and what
3479 // are likely to be positional parameters. This is what expandMacro() is
3480 // doing when it finds the parameters in the body.
3481 while (!Body.empty()) {
3482 // Scan for the next possible parameter.
3483 std::size_t End = Body.size(), Pos = 0;
3484 for (; Pos != End; ++Pos) {
3485 // Check for a substitution or escape.
3486 // This macro is defined with parameters, look for \foo, \bar, etc.
3487 if (Body[Pos] == '\\' && Pos + 1 != End)
3488 break;
3489
3490 // This macro should have parameters, but look for $0, $1, ..., $n too.
3491 if (Body[Pos] != '$' || Pos + 1 == End)
3492 continue;
3493 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003494 if (Next == '$' || Next == 'n' ||
3495 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003496 break;
3497 }
3498
3499 // Check if we reached the end.
3500 if (Pos == End)
3501 break;
3502
3503 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003504 switch (Body[Pos + 1]) {
3505 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003506 case '$':
3507 break;
3508
Jim Grosbach4b905842013-09-20 23:08:21 +00003509 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003510 case 'n':
3511 PositionalParametersFound = true;
3512 break;
3513
Jim Grosbach4b905842013-09-20 23:08:21 +00003514 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003515 default: {
3516 PositionalParametersFound = true;
3517 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003518 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003519 }
3520 Pos += 2;
3521 } else {
3522 unsigned I = Pos + 1;
3523 while (isIdentifierChar(Body[I]) && I + 1 != End)
3524 ++I;
3525
Jim Grosbach4b905842013-09-20 23:08:21 +00003526 const char *Begin = Body.data() + Pos + 1;
3527 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003528 unsigned Index = 0;
3529 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003530 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003531 break;
3532
3533 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003534 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3535 Pos += 3;
3536 else {
3537 Pos = I;
3538 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003539 } else {
3540 NamedParametersFound = true;
3541 Pos += 1 + Argument.size();
3542 }
3543 }
3544 // Update the scan point.
3545 Body = Body.substr(Pos);
3546 }
3547
3548 if (!NamedParametersFound && PositionalParametersFound)
3549 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3550 "used in macro body, possible positional parameter "
3551 "found in body which will have no effect");
3552}
3553
Nico Weber155dccd12014-07-24 17:08:39 +00003554/// parseDirectiveExitMacro
3555/// ::= .exitm
3556bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3557 if (getLexer().isNot(AsmToken::EndOfStatement))
3558 return TokError("unexpected token in '" + Directive + "' directive");
3559
3560 if (!isInsideMacroInstantiation())
3561 return TokError("unexpected '" + Directive + "' in file, "
3562 "no current macro definition");
3563
3564 // Exit all conditionals that are active in the current macro.
3565 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3566 TheCondState = TheCondStack.back();
3567 TheCondStack.pop_back();
3568 }
3569
3570 handleMacroExit();
3571 return false;
3572}
3573
Jim Grosbach4b905842013-09-20 23:08:21 +00003574/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003575/// ::= .endm
3576/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003577bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003578 if (getLexer().isNot(AsmToken::EndOfStatement))
3579 return TokError("unexpected token in '" + Directive + "' directive");
3580
3581 // If we are inside a macro instantiation, terminate the current
3582 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003583 if (isInsideMacroInstantiation()) {
3584 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003585 return false;
3586 }
3587
3588 // Otherwise, this .endmacro is a stray entry in the file; well formed
3589 // .endmacro directives are handled during the macro definition parsing.
3590 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003591 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003592}
3593
Jim Grosbach4b905842013-09-20 23:08:21 +00003594/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003595/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003596bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003597 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003598 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003599 return TokError("expected identifier in '.purgem' directive");
3600
3601 if (getLexer().isNot(AsmToken::EndOfStatement))
3602 return TokError("unexpected token in '.purgem' directive");
3603
Jim Grosbach4b905842013-09-20 23:08:21 +00003604 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003605 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3606
Jim Grosbach4b905842013-09-20 23:08:21 +00003607 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003608 return false;
3609}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003610
Jim Grosbach4b905842013-09-20 23:08:21 +00003611/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003612/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003613bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003614 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003615
3616 // Expect a single argument: an expression that evaluates to a constant
3617 // in the inclusive range 0-30.
3618 SMLoc ExprLoc = getLexer().getLoc();
3619 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003620 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003621 return true;
3622 else if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token after expression in"
3624 " '.bundle_align_mode' directive");
3625 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3626 return Error(ExprLoc,
3627 "invalid bundle alignment size (expected between 0 and 30)");
3628
3629 Lex();
3630
3631 // Because of AlignSizePow2's verified range we can safely truncate it to
3632 // unsigned.
3633 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3634 return false;
3635}
3636
Jim Grosbach4b905842013-09-20 23:08:21 +00003637/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003638/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003639bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003640 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003641 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003642
Eli Bendersky802b6282013-01-07 21:51:08 +00003643 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3644 StringRef Option;
3645 SMLoc Loc = getTok().getLoc();
3646 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003647 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003648
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003649 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003650 return Error(Loc, kInvalidOptionError);
3651
3652 if (Option != "align_to_end")
3653 return Error(Loc, kInvalidOptionError);
3654 else if (getLexer().isNot(AsmToken::EndOfStatement))
3655 return Error(Loc,
3656 "unexpected token after '.bundle_lock' directive option");
3657 AlignToEnd = true;
3658 }
3659
Eli Benderskyf483ff92012-12-20 19:05:53 +00003660 Lex();
3661
Eli Bendersky802b6282013-01-07 21:51:08 +00003662 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003663 return false;
3664}
3665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003667/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003668bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003669 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003670
3671 if (getLexer().isNot(AsmToken::EndOfStatement))
3672 return TokError("unexpected token in '.bundle_unlock' directive");
3673 Lex();
3674
3675 getStreamer().EmitBundleUnlock();
3676 return false;
3677}
3678
Jim Grosbach4b905842013-09-20 23:08:21 +00003679/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003680/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003681bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003682 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003683
3684 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003685 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003686 return true;
3687
3688 int64_t FillExpr = 0;
3689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3690 if (getLexer().isNot(AsmToken::Comma))
3691 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3692 Lex();
3693
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003694 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003695 return true;
3696
3697 if (getLexer().isNot(AsmToken::EndOfStatement))
3698 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3699 }
3700
3701 Lex();
3702
3703 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003704 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3705 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003706
3707 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003708 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003709
3710 return false;
3711}
3712
Jim Grosbach4b905842013-09-20 23:08:21 +00003713/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003714/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003715bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003716 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003717 const MCExpr *Value;
3718
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003719 for (;;) {
3720 if (parseExpression(Value))
3721 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003722
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003723 if (Signed)
3724 getStreamer().EmitSLEB128Value(Value);
3725 else
3726 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00003727
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00003728 if (getLexer().is(AsmToken::EndOfStatement))
3729 break;
3730
3731 if (getLexer().isNot(AsmToken::Comma))
3732 return TokError("unexpected token in directive");
3733 Lex();
3734 }
Eli Bendersky17233942013-01-15 22:59:42 +00003735
3736 return false;
3737}
3738
Jim Grosbach4b905842013-09-20 23:08:21 +00003739/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003740/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003741bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003743 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003744 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003745 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003746
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003747 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003748 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003749
Jim Grosbach6f482002015-05-18 18:43:14 +00003750 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003751
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003752 // Assembler local symbols don't make any sense here. Complain loudly.
3753 if (Sym->isTemporary())
3754 return Error(Loc, "non-local symbol required in directive");
3755
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003756 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3757 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003758
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003759 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003760 break;
3761
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003762 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003763 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003764 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003765 }
3766 }
3767
Sean Callanan686ed8d2010-01-19 20:22:31 +00003768 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003769 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003770}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003771
Jim Grosbach4b905842013-09-20 23:08:21 +00003772/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003773/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003774bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003775 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003776
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003777 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003778 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003779 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003780 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003781
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003782 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00003783 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003784
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003785 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003786 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003787 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003788
3789 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003790 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003791 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003792 return true;
3793
3794 int64_t Pow2Alignment = 0;
3795 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003797 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003798 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003799 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003800 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003801
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003802 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3803 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003804 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3805
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003806 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003807 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3808 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003809 if (!isPowerOf2_64(Pow2Alignment))
3810 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3811 Pow2Alignment = Log2_64(Pow2Alignment);
3812 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003813 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003814
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003815 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003816 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003817
Sean Callanan686ed8d2010-01-19 20:22:31 +00003818 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003819
Chris Lattner28ad7542009-07-09 17:25:12 +00003820 // NOTE: a size of zero for a .comm should create a undefined symbol
3821 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003822 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003823 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003824 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003825
Eric Christopherbc818852010-05-14 01:38:54 +00003826 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003827 // may internally end up wanting an alignment in bytes.
3828 // FIXME: Diagnose overflow.
3829 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003830 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003831 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003832
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003833 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003834 return Error(IDLoc, "invalid symbol redefinition");
3835
Chris Lattner28ad7542009-07-09 17:25:12 +00003836 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003837 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003838 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003839 return false;
3840 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003841
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003842 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003843 return false;
3844}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003845
Jim Grosbach4b905842013-09-20 23:08:21 +00003846/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003847/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003848bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003849 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003850 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003851
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003852 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003853 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003854 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003855
Sean Callanan686ed8d2010-01-19 20:22:31 +00003856 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003857
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003858 if (Str.empty())
3859 Error(Loc, ".abort detected. Assembly stopping.");
3860 else
3861 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003862 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003863
3864 return false;
3865}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003866
Jim Grosbach4b905842013-09-20 23:08:21 +00003867/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003868/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003869bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003870 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003871 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003872
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003873 // Allow the strings to have escaped octal character sequence.
3874 std::string Filename;
3875 if (parseEscapedString(Filename))
3876 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003877 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003878 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003879
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003880 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003881 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003882
Chris Lattner693fbb82009-07-16 06:14:39 +00003883 // Attempt to switch the lexer to the included file before consuming the end
3884 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003885 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003886 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003887 return true;
3888 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003889
3890 return false;
3891}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003892
Jim Grosbach4b905842013-09-20 23:08:21 +00003893/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003894/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003895bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003896 if (getLexer().isNot(AsmToken::String))
3897 return TokError("expected string in '.incbin' directive");
3898
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003899 // Allow the strings to have escaped octal character sequence.
3900 std::string Filename;
3901 if (parseEscapedString(Filename))
3902 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003903 SMLoc IncbinLoc = getLexer().getLoc();
3904 Lex();
3905
3906 if (getLexer().isNot(AsmToken::EndOfStatement))
3907 return TokError("unexpected token in '.incbin' directive");
3908
Kevin Enderby109f25c2011-12-14 21:47:48 +00003909 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003910 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003911 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3912 return true;
3913 }
3914
3915 return false;
3916}
3917
Jim Grosbach4b905842013-09-20 23:08:21 +00003918/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003919/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3920bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003921 TheCondStack.push_back(TheCondState);
3922 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003923 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003924 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003925 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003926 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003927 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003928 return true;
3929
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003930 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003931 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003932
Sean Callanan686ed8d2010-01-19 20:22:31 +00003933 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003934
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003935 switch (DirKind) {
3936 default:
3937 llvm_unreachable("unsupported directive");
3938 case DK_IF:
3939 case DK_IFNE:
3940 break;
3941 case DK_IFEQ:
3942 ExprValue = ExprValue == 0;
3943 break;
3944 case DK_IFGE:
3945 ExprValue = ExprValue >= 0;
3946 break;
3947 case DK_IFGT:
3948 ExprValue = ExprValue > 0;
3949 break;
3950 case DK_IFLE:
3951 ExprValue = ExprValue <= 0;
3952 break;
3953 case DK_IFLT:
3954 ExprValue = ExprValue < 0;
3955 break;
3956 }
3957
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003958 TheCondState.CondMet = ExprValue;
3959 TheCondState.Ignore = !TheCondState.CondMet;
3960 }
3961
3962 return false;
3963}
3964
Jim Grosbach4b905842013-09-20 23:08:21 +00003965/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003966/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003967bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003968 TheCondStack.push_back(TheCondState);
3969 TheCondState.TheCond = AsmCond::IfCond;
3970
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003971 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003972 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003973 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003974 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003975
3976 if (getLexer().isNot(AsmToken::EndOfStatement))
3977 return TokError("unexpected token in '.ifb' directive");
3978
3979 Lex();
3980
3981 TheCondState.CondMet = ExpectBlank == Str.empty();
3982 TheCondState.Ignore = !TheCondState.CondMet;
3983 }
3984
3985 return false;
3986}
3987
Jim Grosbach4b905842013-09-20 23:08:21 +00003988/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003989/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003990/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003991bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003992 TheCondStack.push_back(TheCondState);
3993 TheCondState.TheCond = AsmCond::IfCond;
3994
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003995 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003996 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003997 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003998 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003999
4000 if (getLexer().isNot(AsmToken::Comma))
4001 return TokError("unexpected token in '.ifc' directive");
4002
4003 Lex();
4004
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004005 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004006
4007 if (getLexer().isNot(AsmToken::EndOfStatement))
4008 return TokError("unexpected token in '.ifc' directive");
4009
4010 Lex();
4011
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004012 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004013 TheCondState.Ignore = !TheCondState.CondMet;
4014 }
4015
4016 return false;
4017}
4018
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004019/// parseDirectiveIfeqs
4020/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004021bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004022 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004023 if (ExpectEqual)
4024 TokError("expected string parameter for '.ifeqs' directive");
4025 else
4026 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004027 eatToEndOfStatement();
4028 return true;
4029 }
4030
4031 StringRef String1 = getTok().getStringContents();
4032 Lex();
4033
4034 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004035 if (ExpectEqual)
4036 TokError("expected comma after first string for '.ifeqs' directive");
4037 else
4038 TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004039 eatToEndOfStatement();
4040 return true;
4041 }
4042
4043 Lex();
4044
4045 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004046 if (ExpectEqual)
4047 TokError("expected string parameter for '.ifeqs' directive");
4048 else
4049 TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004050 eatToEndOfStatement();
4051 return true;
4052 }
4053
4054 StringRef String2 = getTok().getStringContents();
4055 Lex();
4056
4057 TheCondStack.push_back(TheCondState);
4058 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004059 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004060 TheCondState.Ignore = !TheCondState.CondMet;
4061
4062 return false;
4063}
4064
Jim Grosbach4b905842013-09-20 23:08:21 +00004065/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004066/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004067bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004068 StringRef Name;
4069 TheCondStack.push_back(TheCondState);
4070 TheCondState.TheCond = AsmCond::IfCond;
4071
4072 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004073 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004074 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004075 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004076 return TokError("expected identifier after '.ifdef'");
4077
4078 Lex();
4079
Jim Grosbach6f482002015-05-18 18:43:14 +00004080 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004081
4082 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004083 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004084 else
Craig Topper353eda42014-04-24 06:44:33 +00004085 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004086 TheCondState.Ignore = !TheCondState.CondMet;
4087 }
4088
4089 return false;
4090}
4091
Jim Grosbach4b905842013-09-20 23:08:21 +00004092/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004093/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004094bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004095 if (TheCondState.TheCond != AsmCond::IfCond &&
4096 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004097 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4098 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004099 TheCondState.TheCond = AsmCond::ElseIfCond;
4100
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004101 bool LastIgnoreState = false;
4102 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004103 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004104 if (LastIgnoreState || TheCondState.CondMet) {
4105 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004106 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004107 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004108 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004109 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004110 return true;
4111
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004112 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004113 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004114
Sean Callanan686ed8d2010-01-19 20:22:31 +00004115 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004116 TheCondState.CondMet = ExprValue;
4117 TheCondState.Ignore = !TheCondState.CondMet;
4118 }
4119
4120 return false;
4121}
4122
Jim Grosbach4b905842013-09-20 23:08:21 +00004123/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004124/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004125bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004126 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004127 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004128
Sean Callanan686ed8d2010-01-19 20:22:31 +00004129 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004130
4131 if (TheCondState.TheCond != AsmCond::IfCond &&
4132 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004133 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4134 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004135 TheCondState.TheCond = AsmCond::ElseCond;
4136 bool LastIgnoreState = false;
4137 if (!TheCondStack.empty())
4138 LastIgnoreState = TheCondStack.back().Ignore;
4139 if (LastIgnoreState || TheCondState.CondMet)
4140 TheCondState.Ignore = true;
4141 else
4142 TheCondState.Ignore = false;
4143
4144 return false;
4145}
4146
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004147/// parseDirectiveEnd
4148/// ::= .end
4149bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4150 if (getLexer().isNot(AsmToken::EndOfStatement))
4151 return TokError("unexpected token in '.end' directive");
4152
4153 Lex();
4154
4155 while (Lexer.isNot(AsmToken::Eof))
4156 Lex();
4157
4158 return false;
4159}
4160
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004161/// parseDirectiveError
4162/// ::= .err
4163/// ::= .error [string]
4164bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4165 if (!TheCondStack.empty()) {
4166 if (TheCondStack.back().Ignore) {
4167 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004168 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004169 }
4170 }
4171
4172 if (!WithMessage)
4173 return Error(L, ".err encountered");
4174
4175 StringRef Message = ".error directive invoked in source file";
4176 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4177 if (Lexer.isNot(AsmToken::String)) {
4178 TokError(".error argument must be a string");
4179 eatToEndOfStatement();
4180 return true;
4181 }
4182
4183 Message = getTok().getStringContents();
4184 Lex();
4185 }
4186
4187 Error(L, Message);
4188 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004189}
4190
Nico Weber404012b2014-07-24 16:26:06 +00004191/// parseDirectiveWarning
4192/// ::= .warning [string]
4193bool AsmParser::parseDirectiveWarning(SMLoc L) {
4194 if (!TheCondStack.empty()) {
4195 if (TheCondStack.back().Ignore) {
4196 eatToEndOfStatement();
4197 return false;
4198 }
4199 }
4200
4201 StringRef Message = ".warning directive invoked in source file";
4202 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4203 if (Lexer.isNot(AsmToken::String)) {
4204 TokError(".warning argument must be a string");
4205 eatToEndOfStatement();
4206 return true;
4207 }
4208
4209 Message = getTok().getStringContents();
4210 Lex();
4211 }
4212
4213 Warning(L, Message);
4214 return false;
4215}
4216
Jim Grosbach4b905842013-09-20 23:08:21 +00004217/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004218/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004219bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004220 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004221 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004222
Sean Callanan686ed8d2010-01-19 20:22:31 +00004223 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004224
Jim Grosbach4b905842013-09-20 23:08:21 +00004225 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004226 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4227 ".else");
4228 if (!TheCondStack.empty()) {
4229 TheCondState = TheCondStack.back();
4230 TheCondStack.pop_back();
4231 }
4232
4233 return false;
4234}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004235
Eli Bendersky17233942013-01-15 22:59:42 +00004236void AsmParser::initializeDirectiveKindMap() {
4237 DirectiveKindMap[".set"] = DK_SET;
4238 DirectiveKindMap[".equ"] = DK_EQU;
4239 DirectiveKindMap[".equiv"] = DK_EQUIV;
4240 DirectiveKindMap[".ascii"] = DK_ASCII;
4241 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4242 DirectiveKindMap[".string"] = DK_STRING;
4243 DirectiveKindMap[".byte"] = DK_BYTE;
4244 DirectiveKindMap[".short"] = DK_SHORT;
4245 DirectiveKindMap[".value"] = DK_VALUE;
4246 DirectiveKindMap[".2byte"] = DK_2BYTE;
4247 DirectiveKindMap[".long"] = DK_LONG;
4248 DirectiveKindMap[".int"] = DK_INT;
4249 DirectiveKindMap[".4byte"] = DK_4BYTE;
4250 DirectiveKindMap[".quad"] = DK_QUAD;
4251 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004252 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004253 DirectiveKindMap[".single"] = DK_SINGLE;
4254 DirectiveKindMap[".float"] = DK_FLOAT;
4255 DirectiveKindMap[".double"] = DK_DOUBLE;
4256 DirectiveKindMap[".align"] = DK_ALIGN;
4257 DirectiveKindMap[".align32"] = DK_ALIGN32;
4258 DirectiveKindMap[".balign"] = DK_BALIGN;
4259 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4260 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4261 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4262 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4263 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4264 DirectiveKindMap[".org"] = DK_ORG;
4265 DirectiveKindMap[".fill"] = DK_FILL;
4266 DirectiveKindMap[".zero"] = DK_ZERO;
4267 DirectiveKindMap[".extern"] = DK_EXTERN;
4268 DirectiveKindMap[".globl"] = DK_GLOBL;
4269 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004270 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4271 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4272 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4273 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4274 DirectiveKindMap[".reference"] = DK_REFERENCE;
4275 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4276 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4277 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4278 DirectiveKindMap[".comm"] = DK_COMM;
4279 DirectiveKindMap[".common"] = DK_COMMON;
4280 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4281 DirectiveKindMap[".abort"] = DK_ABORT;
4282 DirectiveKindMap[".include"] = DK_INCLUDE;
4283 DirectiveKindMap[".incbin"] = DK_INCBIN;
4284 DirectiveKindMap[".code16"] = DK_CODE16;
4285 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4286 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004287 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004288 DirectiveKindMap[".irp"] = DK_IRP;
4289 DirectiveKindMap[".irpc"] = DK_IRPC;
4290 DirectiveKindMap[".endr"] = DK_ENDR;
4291 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4292 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4293 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4294 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004295 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4296 DirectiveKindMap[".ifge"] = DK_IFGE;
4297 DirectiveKindMap[".ifgt"] = DK_IFGT;
4298 DirectiveKindMap[".ifle"] = DK_IFLE;
4299 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004300 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004301 DirectiveKindMap[".ifb"] = DK_IFB;
4302 DirectiveKindMap[".ifnb"] = DK_IFNB;
4303 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004304 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004305 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004306 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004307 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4308 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4309 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4310 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4311 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004312 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004313 DirectiveKindMap[".endif"] = DK_ENDIF;
4314 DirectiveKindMap[".skip"] = DK_SKIP;
4315 DirectiveKindMap[".space"] = DK_SPACE;
4316 DirectiveKindMap[".file"] = DK_FILE;
4317 DirectiveKindMap[".line"] = DK_LINE;
4318 DirectiveKindMap[".loc"] = DK_LOC;
4319 DirectiveKindMap[".stabs"] = DK_STABS;
4320 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4321 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4322 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4323 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4324 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4325 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4326 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4327 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4328 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4329 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4330 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4331 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4332 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4333 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4334 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4335 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4336 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4337 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4338 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4339 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4340 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004341 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004342 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4343 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4344 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004345 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004346 DirectiveKindMap[".endm"] = DK_ENDM;
4347 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4348 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004349 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004350 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004351 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004352}
4353
Jim Grosbach4b905842013-09-20 23:08:21 +00004354MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004355 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004356
Rafael Espindola34b9c512012-06-03 23:57:14 +00004357 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004358 for (;;) {
4359 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004360 if (getLexer().is(AsmToken::Eof)) {
4361 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004362 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004363 }
4364
Rafael Espindola34b9c512012-06-03 23:57:14 +00004365 if (Lexer.is(AsmToken::Identifier) &&
4366 (getTok().getIdentifier() == ".rept")) {
4367 ++NestLevel;
4368 }
4369
4370 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004371 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004372 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004373 EndToken = getTok();
4374 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004375 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4376 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004377 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004378 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004379 break;
4380 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004381 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004382 }
4383
Rafael Espindola34b9c512012-06-03 23:57:14 +00004384 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004385 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004386 }
4387
4388 const char *BodyStart = StartToken.getLoc().getPointer();
4389 const char *BodyEnd = EndToken.getLoc().getPointer();
4390 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4391
Rafael Espindola34b9c512012-06-03 23:57:14 +00004392 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004393 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004394 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004395}
4396
Jim Grosbach4b905842013-09-20 23:08:21 +00004397void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004398 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004399 OS << ".endr\n";
4400
Rafael Espindola3560ff22014-08-27 20:03:13 +00004401 std::unique_ptr<MemoryBuffer> Instantiation =
4402 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004403
Rafael Espindola34b9c512012-06-03 23:57:14 +00004404 // Create the macro instantiation object and add to the current macro
4405 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004406 MacroInstantiation *MI = new MacroInstantiation(
4407 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004408 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004409
Rafael Espindola34b9c512012-06-03 23:57:14 +00004410 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004411 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004412 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004413 Lex();
4414}
4415
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004416/// parseDirectiveRept
4417/// ::= .rep | .rept count
4418bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004419 const MCExpr *CountExpr;
4420 SMLoc CountLoc = getTok().getLoc();
4421 if (parseExpression(CountExpr))
4422 return true;
4423
Rafael Espindola34b9c512012-06-03 23:57:14 +00004424 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004425 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004426 eatToEndOfStatement();
4427 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4428 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004429
4430 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004431 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004432
4433 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004434 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004435
4436 // Eat the end of statement.
4437 Lex();
4438
4439 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004440 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004441 if (!M)
4442 return true;
4443
4444 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4445 // to hold the macro body with substitutions.
4446 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004447 raw_svector_ostream OS(Buf);
4448 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004449 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4450 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004451 return true;
4452 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004453 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004454
4455 return false;
4456}
4457
Jim Grosbach4b905842013-09-20 23:08:21 +00004458/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004459/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004460bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004461 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004462
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004463 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004464 return TokError("expected identifier in '.irp' directive");
4465
Rafael Espindola768b41c2012-06-15 14:02:34 +00004466 if (Lexer.isNot(AsmToken::Comma))
4467 return TokError("expected comma in '.irp' directive");
4468
4469 Lex();
4470
Eli Bendersky38274122013-01-14 23:22:36 +00004471 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004472 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004473 return true;
4474
4475 // Eat the end of statement.
4476 Lex();
4477
4478 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004479 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004480 if (!M)
4481 return true;
4482
4483 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4484 // to hold the macro body with substitutions.
4485 SmallString<256> Buf;
4486 raw_svector_ostream OS(Buf);
4487
Craig Topper84008482015-10-10 05:38:14 +00004488 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004489 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4490 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004491 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004492 return true;
4493 }
4494
Jim Grosbach4b905842013-09-20 23:08:21 +00004495 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004496
4497 return false;
4498}
4499
Jim Grosbach4b905842013-09-20 23:08:21 +00004500/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004501/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004502bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004503 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004504
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004505 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004506 return TokError("expected identifier in '.irpc' directive");
4507
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004508 if (Lexer.isNot(AsmToken::Comma))
4509 return TokError("expected comma in '.irpc' directive");
4510
4511 Lex();
4512
Eli Bendersky38274122013-01-14 23:22:36 +00004513 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004514 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004515 return true;
4516
4517 if (A.size() != 1 || A.front().size() != 1)
4518 return TokError("unexpected token in '.irpc' directive");
4519
4520 // Eat the end of statement.
4521 Lex();
4522
4523 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004524 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004525 if (!M)
4526 return true;
4527
4528 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4529 // to hold the macro body with substitutions.
4530 SmallString<256> Buf;
4531 raw_svector_ostream OS(Buf);
4532
4533 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004534 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004535 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004536 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004537
Toma Tabacu217116e2015-04-27 10:50:29 +00004538 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
4539 // This is undocumented, but GAS seems to support it.
4540 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004541 return true;
4542 }
4543
Jim Grosbach4b905842013-09-20 23:08:21 +00004544 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004545
4546 return false;
4547}
4548
Jim Grosbach4b905842013-09-20 23:08:21 +00004549bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004550 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004551 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004552
4553 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004554 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004555 assert(getLexer().is(AsmToken::EndOfStatement));
4556
Jim Grosbach4b905842013-09-20 23:08:21 +00004557 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004558 return false;
4559}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004560
Jim Grosbach4b905842013-09-20 23:08:21 +00004561bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004563 const MCExpr *Value;
4564 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004565 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004566 return true;
4567 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4568 if (!MCE)
4569 return Error(ExprLoc, "unexpected expression in _emit");
4570 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00004571 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004572 return Error(ExprLoc, "literal value out of range for directive");
4573
Craig Topper7d5b2312015-10-10 05:25:02 +00004574 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00004575 return false;
4576}
4577
Jim Grosbach4b905842013-09-20 23:08:21 +00004578bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004579 const MCExpr *Value;
4580 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004581 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004582 return true;
4583 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4584 if (!MCE)
4585 return Error(ExprLoc, "unexpected expression in align");
4586 uint64_t IntValue = MCE->getValue();
4587 if (!isPowerOf2_64(IntValue))
4588 return Error(ExprLoc, "literal value not a power of two greater then zero");
4589
Craig Topper7d5b2312015-10-10 05:25:02 +00004590 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004591 return false;
4592}
4593
Chad Rosierf43fcf52013-02-13 21:27:17 +00004594// We are comparing pointers, but the pointers are relative to a single string.
4595// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004596static int rewritesSort(const AsmRewrite *AsmRewriteA,
4597 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004598 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4599 return -1;
4600 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4601 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004602
Chad Rosierfce4fab2013-04-08 17:43:47 +00004603 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4604 // rewrite to the same location. Make sure the SizeDirective rewrite is
4605 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4606 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004607 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4608 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004609 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004610
Jim Grosbach4b905842013-09-20 23:08:21 +00004611 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4612 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004613 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004614 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004615}
4616
Jim Grosbach4b905842013-09-20 23:08:21 +00004617bool AsmParser::parseMSInlineAsm(
4618 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4619 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4620 SmallVectorImpl<std::string> &Constraints,
4621 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4622 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004623 SmallVector<void *, 4> InputDecls;
4624 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004625 SmallVector<bool, 4> InputDeclsAddressOf;
4626 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004627 SmallVector<std::string, 4> InputConstraints;
4628 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004629 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004630
Benjamin Kramer1a136112013-02-15 20:37:21 +00004631 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004632
4633 // Prime the lexer.
4634 Lex();
4635
4636 // While we have input, parse each statement.
4637 unsigned InputIdx = 0;
4638 unsigned OutputIdx = 0;
4639 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004640 ParseStatementInfo Info(&AsmStrRewrites);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004641 if (parseStatement(Info, &SI))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004642 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004643
Chad Rosier149e8e02012-12-12 22:45:52 +00004644 if (Info.ParseError)
4645 return true;
4646
Benjamin Kramer1a136112013-02-15 20:37:21 +00004647 if (Info.Opcode == ~0U)
4648 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004649
Benjamin Kramer1a136112013-02-15 20:37:21 +00004650 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004651
Benjamin Kramer1a136112013-02-15 20:37:21 +00004652 // Build the list of clobbers, outputs and inputs.
4653 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004654 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004655
Benjamin Kramer1a136112013-02-15 20:37:21 +00004656 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004657 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004658 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004659
Benjamin Kramer1a136112013-02-15 20:37:21 +00004660 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004661 if (Operand.isReg() && !Operand.needAddressOf() &&
4662 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004663 unsigned NumDefs = Desc.getNumDefs();
4664 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004665 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4666 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004667 continue;
4668 }
4669
4670 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004671 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004672 if (SymName.empty())
4673 continue;
4674
David Blaikie960ea3f2014-06-08 16:18:35 +00004675 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004676 if (!OpDecl)
4677 continue;
4678
4679 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004680 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004681 if (isOutput) {
4682 ++InputIdx;
4683 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004684 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00004685 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004686 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004687 } else {
4688 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004689 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4690 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00004691 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00004692 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004693 }
Reid Kleckneree088972013-12-10 18:27:32 +00004694
4695 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004696 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4697 Desc.getNumImplicitDefs());
4698 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004699 }
4700
4701 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004702 NumOutputs = OutputDecls.size();
4703 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004704
4705 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004706 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4707 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4708 ClobberRegs.end());
4709 Clobbers.assign(ClobberRegs.size(), std::string());
4710 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4711 raw_string_ostream OS(Clobbers[I]);
4712 IP->printRegName(OS, ClobberRegs[I]);
4713 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004714
4715 // Merge the various outputs and inputs. Output are expected first.
4716 if (NumOutputs || NumInputs) {
4717 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004718 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004719 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004720 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004721 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004722 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004723 }
4724 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004725 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004726 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004727 }
4728 }
4729
4730 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004731 std::string AsmStringIR;
4732 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004733 StringRef ASMString =
4734 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4735 const char *AsmStart = ASMString.begin();
4736 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004737 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004738 for (const AsmRewrite &AR : AsmStrRewrites) {
4739 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004740 if (Kind == AOK_Delete)
4741 continue;
4742
David Majnemer8114c1a2014-06-23 02:17:16 +00004743 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004744 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004745
Chad Rosier120eefd2013-03-19 17:32:17 +00004746 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004747 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004748 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004749
Chad Rosier37e755c2012-10-23 17:43:43 +00004750 // Skip the original expression.
4751 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004752 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004753 continue;
4754 }
4755
Chad Rosierff10ed12013-04-12 16:26:42 +00004756 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004757 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004758 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004759 default:
4760 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004761 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004762 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004763 break;
4764 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004765 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004766 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004767 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00004768 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00004769 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004770 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004771 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004772 break;
4773 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004774 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004775 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004776 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004777 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004778 default: break;
4779 case 8: OS << "byte ptr "; break;
4780 case 16: OS << "word ptr "; break;
4781 case 32: OS << "dword ptr "; break;
4782 case 64: OS << "qword ptr "; break;
4783 case 80: OS << "xword ptr "; break;
4784 case 128: OS << "xmmword ptr "; break;
4785 case 256: OS << "ymmword ptr "; break;
4786 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004787 break;
4788 case AOK_Emit:
4789 OS << ".byte";
4790 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004791 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004792 // MS alignment directives are measured in bytes. If the native assembler
4793 // measures alignment in bytes, we can pass it straight through.
4794 OS << ".align";
4795 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
4796 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004797
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00004798 // Alignment is in log2 form, so print that instead and skip the original
4799 // immediate.
4800 unsigned Val = AR.Val;
4801 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004802 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004803 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4804 break;
4805 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004806 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004807 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004808 OS.flush();
4809 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004810 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004811 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004812 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004813 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004814
Chad Rosier8bce6642012-10-18 15:49:34 +00004815 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004816 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004817 }
4818
4819 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004820 if (AsmStart != AsmEnd)
4821 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004822
4823 AsmString = OS.str();
4824 return false;
4825}
4826
Pete Cooper80d21cb2015-06-22 19:35:57 +00004827namespace llvm {
4828namespace MCParserUtils {
4829
4830/// Returns whether the given symbol is used anywhere in the given expression,
4831/// or subexpressions.
4832static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
4833 switch (Value->getKind()) {
4834 case MCExpr::Binary: {
4835 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
4836 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
4837 isSymbolUsedInExpression(Sym, BE->getRHS());
4838 }
4839 case MCExpr::Target:
4840 case MCExpr::Constant:
4841 return false;
4842 case MCExpr::SymbolRef: {
4843 const MCSymbol &S =
4844 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
4845 if (S.isVariable())
4846 return isSymbolUsedInExpression(Sym, S.getVariableValue());
4847 return &S == Sym;
4848 }
4849 case MCExpr::Unary:
4850 return isSymbolUsedInExpression(
4851 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
4852 }
4853
4854 llvm_unreachable("Unknown expr kind!");
4855}
4856
4857bool parseAssignmentExpression(StringRef Name, bool allow_redef,
4858 MCAsmParser &Parser, MCSymbol *&Sym,
4859 const MCExpr *&Value) {
4860 MCAsmLexer &Lexer = Parser.getLexer();
4861
4862 // FIXME: Use better location, we should use proper tokens.
4863 SMLoc EqualLoc = Lexer.getLoc();
4864
4865 if (Parser.parseExpression(Value)) {
4866 Parser.TokError("missing expression");
4867 Parser.eatToEndOfStatement();
4868 return true;
4869 }
4870
4871 // Note: we don't count b as used in "a = b". This is to allow
4872 // a = b
4873 // b = c
4874
4875 if (Lexer.isNot(AsmToken::EndOfStatement))
4876 return Parser.TokError("unexpected token in assignment");
4877
4878 // Eat the end of statement marker.
4879 Parser.Lex();
4880
4881 // Validate that the LHS is allowed to be a variable (either it has not been
4882 // used as a symbol, or it is an absolute symbol).
4883 Sym = Parser.getContext().lookupSymbol(Name);
4884 if (Sym) {
4885 // Diagnose assignment to a label.
4886 //
4887 // FIXME: Diagnostics. Note the location of the definition as a label.
4888 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
4889 if (isSymbolUsedInExpression(Sym, Value))
4890 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00004891 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
4892 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00004893 ; // Allow redefinitions of undefined symbols only used in directives.
4894 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
4895 ; // Allow redefinitions of variables that haven't yet been used.
4896 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
4897 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
4898 else if (!Sym->isVariable())
4899 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
4900 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
4901 return Parser.Error(EqualLoc,
4902 "invalid reassignment of non-absolute variable '" +
4903 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00004904 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00004905 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00004906 return false;
4907 } else
4908 Sym = Parser.getContext().getOrCreateSymbol(Name);
4909
4910 Sym->setRedefinable(allow_redef);
4911
4912 return false;
4913}
4914
4915} // namespace MCParserUtils
4916} // namespace llvm
4917
Daniel Dunbar01e36072010-07-17 02:26:10 +00004918/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004919MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4920 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004921 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004922}