blob: a75818e99cdca9809e22c32b2731e0e686536828 [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"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/None.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/ADT/SmallString.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/STLExtras.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000021#include "llvm/ADT/StringMap.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000022#include "llvm/ADT/StringRef.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000023#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000024#include "llvm/MC/MCAsmInfo.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000025#include "llvm/MC/MCCodeView.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000026#include "llvm/MC/MCContext.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000027#include "llvm/MC/MCDirectives.h"
Evan Cheng11424442011-07-26 00:24:13 +000028#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000029#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000030#include "llvm/MC/MCInstPrinter.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000031#include "llvm/MC/MCInstrDesc.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000032#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000033#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000034#include "llvm/MC/MCParser/AsmCond.h"
35#include "llvm/MC/MCParser/AsmLexer.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000036#include "llvm/MC/MCParser/MCAsmLexer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000037#include "llvm/MC/MCParser/MCAsmParser.h"
Pete Cooper80d21cb2015-06-22 19:35:57 +000038#include "llvm/MC/MCParser/MCAsmParserUtils.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000040#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Evan Cheng76792992011-07-20 05:58:47 +000041#include "llvm/MC/MCRegisterInfo.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000042#include "llvm/MC/MCSection.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000043#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000044#include "llvm/MC/MCSymbol.h"
Daniel Sanders9f6ad492015-11-12 13:33:00 +000045#include "llvm/MC/MCValue.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000046#include "llvm/Support/Casting.h"
Davide Italiano7c9fc732016-07-27 05:51:56 +000047#include "llvm/Support/CommandLine.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000048#include "llvm/Support/Dwarf.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000049#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000050#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000051#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000052#include "llvm/Support/SMLoc.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000053#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000054#include "llvm/Support/raw_ostream.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000055#include <algorithm>
56#include <cassert>
Nick Lewycky0de20af2010-12-19 20:43:38 +000057#include <cctype>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000058#include <cstddef>
59#include <cstdint>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000060#include <deque>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000061#include <memory>
Davide Italiano7c9fc732016-07-27 05:51:56 +000062#include <sstream>
Chad Rosier8bce6642012-10-18 15:49:34 +000063#include <string>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000064#include <tuple>
65#include <utility>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000066#include <vector>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000067
Chris Lattnerb0133452009-06-21 20:16:42 +000068using namespace llvm;
69
Eric Christophera7c32732012-12-18 00:30:54 +000070MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000071
Davide Italiano7c9fc732016-07-27 05:51:56 +000072static cl::opt<unsigned> AsmMacroMaxNestingDepth(
73 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
74 cl::desc("The maximum nesting depth allowed for assembly macros."));
75
Daniel Dunbar86033402010-07-12 17:54:38 +000076namespace {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000077
Eli Benderskya313ae62013-01-16 18:56:50 +000078/// \brief Helper types for tracking macro definitions.
79typedef std::vector<AsmToken> MCAsmMacroArgument;
80typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000081
82struct MCAsmMacroParameter {
83 StringRef Name;
84 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000085 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000086 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000087
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000088 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000089};
90
Eli Benderskya313ae62013-01-16 18:56:50 +000091typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
92
93struct MCAsmMacro {
94 StringRef Name;
95 StringRef Body;
96 MCAsmMacroParameters Parameters;
97
98public:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +000099 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
100 : Name(N), Body(B), Parameters(std::move(P)) {}
Eli Benderskya313ae62013-01-16 18:56:50 +0000101};
102
Daniel Dunbar43235712010-07-18 18:54:11 +0000103/// \brief Helper class for storing information about an active macro
104/// instantiation.
105struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +0000106 /// The location of the instantiation.
107 SMLoc InstantiationLoc;
108
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000109 /// The buffer where parsing should resume upon instantiation completion.
110 int ExitBuffer;
111
Daniel Dunbar43235712010-07-18 18:54:11 +0000112 /// The location where parsing should resume upon instantiation completion.
113 SMLoc ExitLoc;
114
Nico Weber155dccd12014-07-24 17:08:39 +0000115 /// The depth of TheCondStack at the start of the instantiation.
116 size_t CondStackDepth;
117
Daniel Dunbar43235712010-07-18 18:54:11 +0000118public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +0000119 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +0000120};
121
Eli Friedman0f4871d2012-10-22 23:58:19 +0000122struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000123 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000124 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000125
Jim Grosbach4b905842013-09-20 23:08:21 +0000126 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000127 unsigned Opcode;
128
Jim Grosbach4b905842013-09-20 23:08:21 +0000129 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000130 bool ParseError;
131
Eli Friedman0f4871d2012-10-22 23:58:19 +0000132 SmallVectorImpl<AsmRewrite> *AsmRewrites;
133
Craig Topper353eda42014-04-24 06:44:33 +0000134 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000135 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000136 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000137};
138
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139/// \brief The concrete assembly parser instance.
140class AsmParser : public MCAsmParser {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000141 AsmParser(const AsmParser &) = delete;
142 void operator=(const AsmParser &) = delete;
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000143
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000144private:
145 AsmLexer Lexer;
146 MCContext &Ctx;
147 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000148 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000149 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000150 SourceMgr::DiagHandlerTy SavedDiagHandler;
151 void *SavedDiagContext;
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000152 std::unique_ptr<MCAsmParserExtension> PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000153
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000154 /// This is the current buffer index we're lexing from as managed by the
155 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000156 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000157
158 AsmCond TheCondState;
159 std::vector<AsmCond> TheCondStack;
160
Jim Grosbach4b905842013-09-20 23:08:21 +0000161 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000162 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000163 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000164 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000165
Jim Grosbach4b905842013-09-20 23:08:21 +0000166 /// \brief Map of currently defined macros.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000167 StringMap<MCAsmMacro> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000168
Jim Grosbach4b905842013-09-20 23:08:21 +0000169 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000170 std::vector<MacroInstantiation*> ActiveMacros;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000173 std::deque<MCAsmMacro> MacroLikeBodies;
174
Daniel Dunbar828984f2010-07-18 18:38:02 +0000175 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000176 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000177
Toma Tabacu217116e2015-04-27 10:50:29 +0000178 /// \brief Keeps track of how many .macro's have been instantiated.
179 unsigned NumOfMacroInstantiations;
180
Eric Christopher04c7db32016-09-13 00:19:29 +0000181 /// Flag tracking whether any errors have been encountered.
182 unsigned HadError : 1;
183
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000184 /// The values from the last parsed cpp hash file line comment if any.
Tim Northoverc0bef992016-04-13 19:46:54 +0000185 struct CppHashInfoTy {
186 StringRef Filename;
Andrew Kaylorca196472016-04-21 20:09:35 +0000187 int64_t LineNumber = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000188 SMLoc Loc;
Andrew Kaylorca196472016-04-21 20:09:35 +0000189 unsigned Buf = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000190 };
191 CppHashInfoTy CppHashInfo;
192
193 /// \brief List of forward directional labels for diagnosis at the end.
194 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
195
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000196 /// When generating dwarf for assembly source files we need to calculate the
197 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000198 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000199 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
200 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000201 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000202 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000203
Devang Patela173ee52012-01-31 18:14:05 +0000204 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
205 unsigned AssemblerDialect;
206
Jim Grosbach4b905842013-09-20 23:08:21 +0000207 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000208 bool IsDarwin;
209
Jim Grosbach4b905842013-09-20 23:08:21 +0000210 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000211 bool ParsingInlineAsm;
212
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000214 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000215 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000216 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Craig Topper59be68f2014-03-08 07:14:16 +0000218 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 void addDirectiveHandler(StringRef Directive,
221 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000222 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000223 }
224
Toma Tabacu11e14a92015-04-21 11:50:52 +0000225 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
226 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
227 }
228
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000229public:
230 /// @name MCAsmParser Interface
231 /// {
232
Craig Topper59be68f2014-03-08 07:14:16 +0000233 SourceMgr &getSourceManager() override { return SrcMgr; }
234 MCAsmLexer &getLexer() override { return Lexer; }
235 MCContext &getContext() override { return Ctx; }
236 MCStreamer &getStreamer() override { return Out; }
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000237
Reid Klecknera5b1eef2016-08-26 17:58:37 +0000238 CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
239
Craig Topper59be68f2014-03-08 07:14:16 +0000240 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000241 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000242 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000243 else
244 return AssemblerDialect;
245 }
Craig Topper59be68f2014-03-08 07:14:16 +0000246 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000247 AssemblerDialect = i;
248 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000249
Eric Christopher04c7db32016-09-13 00:19:29 +0000250 void Note(SMLoc L, const Twine &Msg,
251 ArrayRef<SMRange> Ranges = None) override;
252 bool Warning(SMLoc L, const Twine &Msg,
253 ArrayRef<SMRange> Ranges = None) override;
254 bool Error(SMLoc L, const Twine &Msg,
255 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000256
Craig Topper59be68f2014-03-08 07:14:16 +0000257 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000258
Yunzhong Gao27ea29b2016-09-02 23:15:29 +0000259 void setParsingInlineAsm(bool V) override {
260 ParsingInlineAsm = V;
261 Lexer.setParsingMSInlineAsm(V);
262 }
Craig Topper59be68f2014-03-08 07:14:16 +0000263 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000264
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000265 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000266 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000267 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000268 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000269 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000270 const MCInstrInfo *MII, const MCInstPrinter *IP,
271 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000272
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000273 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000274 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
275 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
276 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000277 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
278 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000279 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000280
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000282 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000283 bool parseIdentifier(StringRef &Res) override;
284 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000285
Craig Topper59be68f2014-03-08 07:14:16 +0000286 void checkForValidSection() override;
Nirav Davea645433c2016-07-18 15:24:03 +0000287
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000288 /// }
289
290private:
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000291 bool parseStatement(ParseStatementInfo &Info,
292 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000293 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Craig Topper3c76c522015-09-20 23:35:59 +0000294 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000295
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000297 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000298 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000299 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000300 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000301 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000302
Eli Benderskya313ae62013-01-16 18:56:50 +0000303 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000304 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000305
306 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000308
309 /// \brief Lookup a previously defined macro.
310 /// \param Name Macro name.
311 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000312 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000313
314 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000315 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000316
317 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000318 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000319
320 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000321 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000322
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000323 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000324 ///
325 /// \param M The macro.
326 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000328
329 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000331
David Majnemer91fc4c22014-01-29 18:57:46 +0000332 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000333 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000334
335 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000337
Jim Grosbach4b905842013-09-20 23:08:21 +0000338 void printMacroInstantiations();
339 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Eric Christopher04c7db32016-09-13 00:19:29 +0000340 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000341 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000342 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000343 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000344
Jim Grosbach4b905842013-09-20 23:08:21 +0000345 /// \brief Enter the specified file. This returns true on failure.
346 bool enterIncludeFile(const std::string &Filename);
347
348 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000349 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000350 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000351
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000352 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000353 /// current token is not set; clients should ensure Lex() is called
354 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000355 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000356 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000357 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000358 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000359
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000360 /// \brief Parse up to the end of statement and a return the contents from the
361 /// current token until the end of the statement; the current token on exit
362 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000363 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000364
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000365 /// \brief Parse until the end of a statement or a comma is encountered,
366 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000367 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000368
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000370 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000371
Ahmed Bougacha457852f2015-04-28 00:17:39 +0000372 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
373 MCBinaryExpr::Opcode &Kind);
374
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
376 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
377 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000380
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000381 bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
382 bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
383
Eli Bendersky17233942013-01-15 22:59:42 +0000384 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000385 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000386 DK_NO_DIRECTIVE, // Placeholder
387 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000388 DK_RELOC,
David Woodhoused6de0d92014-02-01 16:20:59 +0000389 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
Petr Hosek731bb9c2016-08-23 21:34:53 +0000390 DK_DC, DK_DC_A, DK_DC_B, DK_DC_D, DK_DC_L, DK_DC_S, DK_DC_W, DK_DC_X,
David Woodhoused6de0d92014-02-01 16:20:59 +0000391 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000392 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000393 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000394 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000395 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000396 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000397 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
398 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000399 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000400 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
401 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000402 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000403 DK_CV_FILE, DK_CV_FUNC_ID, DK_CV_INLINE_SITE_ID, DK_CV_LOC, DK_CV_LINETABLE,
404 DK_CV_INLINE_LINETABLE, DK_CV_DEF_RANGE, DK_CV_STRINGTABLE,
405 DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000406 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
407 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
408 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
409 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
410 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000411 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000412 DK_MACROS_ON, DK_MACROS_OFF,
413 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000414 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000415 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000416 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000417 };
418
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000420 /// directives parsed by this class.
421 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000422
423 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000425 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000427 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
429 bool parseDirectiveFill(); // ".fill"
430 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000431 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
433 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Eli Bendersky17233942013-01-15 22:59:42 +0000437 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveFile(SMLoc DirectiveLoc);
439 bool parseDirectiveLine();
440 bool parseDirectiveLoc();
441 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000442
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000443 // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
444 // ".cv_inline_linetable", ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000445 bool parseDirectiveCVFile();
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000446 bool parseDirectiveCVFuncId();
447 bool parseDirectiveCVInlineSiteId();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000448 bool parseDirectiveCVLoc();
449 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000450 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000451 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000452 bool parseDirectiveCVStringTable();
453 bool parseDirectiveCVFileChecksums();
454
Eli Bendersky17233942013-01-15 22:59:42 +0000455 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000457 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveCFISections();
459 bool parseDirectiveCFIStartProc();
460 bool parseDirectiveCFIEndProc();
461 bool parseDirectiveCFIDefCfaOffset();
462 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
463 bool parseDirectiveCFIAdjustCfaOffset();
464 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
465 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
466 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
467 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
468 bool parseDirectiveCFIRememberState();
469 bool parseDirectiveCFIRestoreState();
470 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
471 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
472 bool parseDirectiveCFIEscape();
473 bool parseDirectiveCFISignalFrame();
474 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000475
476 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000477 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000478 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000479 bool parseDirectiveEndMacro(StringRef Directive);
480 bool parseDirectiveMacro(SMLoc DirectiveLoc);
481 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000482
Eli Benderskyf483ff92012-12-20 19:05:53 +0000483 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000484 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000485 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000486 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000487 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000488 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000489
Eli Bendersky17233942013-01-15 22:59:42 +0000490 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000491 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000492
493 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000494 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000495
Jim Grosbach4b905842013-09-20 23:08:21 +0000496 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000497 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000498 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000499
Jim Grosbach4b905842013-09-20 23:08:21 +0000500 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000501
Jim Grosbach4b905842013-09-20 23:08:21 +0000502 bool parseDirectiveAbort(); // ".abort"
503 bool parseDirectiveInclude(); // ".include"
504 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000505
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000506 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
507 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000508 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000509 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000510 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000511 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000512 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
513 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000514 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000515 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
516 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
517 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
518 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000519 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000520
Jim Grosbach4b905842013-09-20 23:08:21 +0000521 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000522 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000523
Rafael Espindola34b9c512012-06-03 23:57:14 +0000524 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000525 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
526 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000527 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000528 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
530 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
531 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000532
Chad Rosierc7f552c2013-02-12 21:33:51 +0000533 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000534 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000535 size_t Len);
536
537 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000538 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000539
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000540 // "end"
541 bool parseDirectiveEnd(SMLoc DirectiveLoc);
542
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000543 // ".err" or ".error"
544 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000545
Nico Weber404012b2014-07-24 16:26:06 +0000546 // ".warning"
547 bool parseDirectiveWarning(SMLoc DirectiveLoc);
548
Eli Bendersky17233942013-01-15 22:59:42 +0000549 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000550};
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000551
552} // end anonymous namespace
Daniel Dunbar86033402010-07-12 17:54:38 +0000553
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000554namespace llvm {
555
556extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000557extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000558extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000559
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000560} // end namespace llvm
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000561
Chris Lattnerc35681b2010-01-19 19:46:13 +0000562enum { DEFAULT_ADDRSPACE = 0 };
563
David Blaikie9f380a32015-03-16 18:06:57 +0000564AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
565 const MCAsmInfo &MAI)
566 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
567 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Eric Christopher04c7db32016-09-13 00:19:29 +0000568 MacrosEnabledFlag(true), HadError(false), CppHashInfo(),
569 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000570 // Save the old handler.
571 SavedDiagHandler = SrcMgr.getDiagHandler();
572 SavedDiagContext = SrcMgr.getDiagContext();
573 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000574 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000576
Daniel Dunbarc5011082010-07-12 18:12:02 +0000577 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000578 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
579 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000580 PlatformParser.reset(createCOFFAsmParser());
581 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000582 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000583 PlatformParser.reset(createDarwinAsmParser());
584 IsDarwin = true;
585 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000586 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000587 PlatformParser.reset(createELFAsmParser());
588 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000589 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000590
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000591 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000592 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000593
594 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000595}
596
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000597AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000598 assert((HadError || ActiveMacros.empty()) &&
599 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000600}
601
Jim Grosbach4b905842013-09-20 23:08:21 +0000602void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000603 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000604 for (std::vector<MacroInstantiation *>::const_reverse_iterator
605 it = ActiveMacros.rbegin(),
606 ie = ActiveMacros.rend();
607 it != ie; ++it)
608 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000609 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000610}
611
Eric Christopher04c7db32016-09-13 00:19:29 +0000612void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
613 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000614 printMacroInstantiations();
615}
616
Eric Christopher04c7db32016-09-13 00:19:29 +0000617bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000618 if(getTargetParser().getTargetOptions().MCNoWarn)
619 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000620 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Eric Christopher04c7db32016-09-13 00:19:29 +0000621 return Error(L, Msg, Ranges);
622 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000623 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000624 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000625}
626
Eric Christopher04c7db32016-09-13 00:19:29 +0000627bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000628 HadError = true;
Eric Christopher04c7db32016-09-13 00:19:29 +0000629 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000630 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000631 return true;
632}
633
Jim Grosbach4b905842013-09-20 23:08:21 +0000634bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000635 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000636 unsigned NewBuf =
637 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
638 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000639 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000640
Sean Callanan7a77eae2010-01-21 00:19:58 +0000641 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000642 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000643 return false;
644}
Daniel Dunbar43235712010-07-18 18:54:11 +0000645
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000646/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000647/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000648/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000649bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000650 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000651 unsigned NewBuf =
652 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
653 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000654 return true;
655
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000656 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000657 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000658 return false;
659}
660
Alp Tokera55b95b2014-07-06 10:33:31 +0000661void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
662 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000663 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
664 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000665}
666
Sean Callanan7a77eae2010-01-21 00:19:58 +0000667const AsmToken &AsmParser::Lex() {
Nirav Dave1180e6892016-06-02 17:15:05 +0000668 if (Lexer.getTok().is(AsmToken::Error))
669 Error(Lexer.getErrLoc(), Lexer.getErr());
670
Nirav Dave53a72f42016-07-11 12:42:14 +0000671 // if it's a end of statement with a comment in it
672 if (getTok().is(AsmToken::EndOfStatement)) {
673 // if this is a line comment output it.
674 if (getTok().getString().front() != '\n' &&
675 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
676 Out.addExplicitComment(Twine(getTok().getString()));
677 }
678
Sean Callanan7a77eae2010-01-21 00:19:58 +0000679 const AsmToken *tok = &Lexer.Lex();
Nirav Dave53a72f42016-07-11 12:42:14 +0000680
681 // Parse comments here to be deferred until end of next statement.
Nirav Davefd910412016-06-17 16:06:17 +0000682 while (tok->is(AsmToken::Comment)) {
Nirav Dave53a72f42016-07-11 12:42:14 +0000683 if (MAI.preserveAsmComments())
684 Out.addExplicitComment(Twine(tok->getString()));
Nirav Davefd910412016-06-17 16:06:17 +0000685 tok = &Lexer.Lex();
686 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000687
Sean Callanan7a77eae2010-01-21 00:19:58 +0000688 if (tok->is(AsmToken::Eof)) {
689 // If this is the end of an included file, pop the parent file off the
690 // include stack.
691 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
692 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000693 jumpToLoc(ParentIncludeLoc);
Nirav Davefd910412016-06-17 16:06:17 +0000694 return Lex();
Sean Callanan7a77eae2010-01-21 00:19:58 +0000695 }
696 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000697
Sean Callanan7a77eae2010-01-21 00:19:58 +0000698 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000699}
700
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000701bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000702 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000703 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000704 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000705
Chris Lattner36e02122009-06-21 20:54:55 +0000706 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000707 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000708
709 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000710 AsmCond StartingCondState = TheCondState;
711
Kevin Enderby6469fc22011-11-01 22:27:22 +0000712 // If we are generating dwarf for assembly source files save the initial text
713 // section and generate a .file directive.
714 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000715 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000716 if (!Sec->getBeginSymbol()) {
717 MCSymbol *SectionStartSym = getContext().createTempSymbol();
718 getStreamer().EmitLabel(SectionStartSym);
719 Sec->setBeginSymbol(SectionStartSym);
720 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000721 bool InsertResult = getContext().addGenDwarfSection(Sec);
722 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000723 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000724 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
725 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000726 }
727
Chris Lattner73f36112009-07-02 21:53:43 +0000728 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000729 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000730 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000731 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000732 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000733
Eric Christopher04c7db32016-09-13 00:19:29 +0000734 // If we've failed, but on a Error Token, but did not consume it in
735 // favor of a better message, emit it now.
736 if (Lexer.getTok().is(AsmToken::Error)) {
Nirav Dave1180e6892016-06-02 17:15:05 +0000737 Lex();
738 }
739
Eric Christopher04c7db32016-09-13 00:19:29 +0000740 // We had an error, validate that one was emitted and recover by skipping to
741 // the next line.
742 assert(HadError && "Parse statement returned an error, but none emitted!");
743 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000744 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000745
Oliver Stannard21718282016-07-26 14:19:47 +0000746 getTargetParser().flushPendingInstructions(getStreamer());
747
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000748 if (TheCondState.TheCond != StartingCondState.TheCond ||
749 TheCondState.Ignore != StartingCondState.Ignore)
Eric Christopher04c7db32016-09-13 00:19:29 +0000750 return TokError("unmatched .ifs or .elses");
751
Kevin Enderbye5930f12010-07-28 20:55:35 +0000752 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000753 const auto &LineTables = getContext().getMCDwarfLineTables();
754 if (!LineTables.empty()) {
755 unsigned Index = 0;
756 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
757 if (File.Name.empty() && Index != 0)
Eric Christopher04c7db32016-09-13 00:19:29 +0000758 TokError("unassigned file number: " + Twine(Index) +
759 " for .file directives");
David Blaikie8bf66c42014-04-01 07:35:52 +0000760 ++Index;
761 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000762 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000763
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000764 // Check to see that all assembler local symbols were actually defined.
765 // Targets that don't do subsections via symbols may not want this, though,
766 // so conservatively exclude them. Only do this if we're finalizing, though,
767 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000768 if (!NoFinalize) {
769 if (MAI.hasSubsectionsViaSymbols()) {
770 for (const auto &TableEntry : getContext().getSymbols()) {
771 MCSymbol *Sym = TableEntry.getValue();
772 // Variable symbols may not be marked as defined, so check those
773 // explicitly. If we know it's a variable, we have a definition for
774 // the purposes of this check.
775 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
776 // FIXME: We would really like to refer back to where the symbol was
777 // first referenced for a source location. We need to add something
778 // to track that. Currently, we just point to the end of the file.
Eric Christopher04c7db32016-09-13 00:19:29 +0000779 HadError |=
780 Error(getTok().getLoc(), "assembler local symbol '" +
781 Sym->getName() + "' not defined");
Tim Northover6b3169b2016-04-11 19:50:46 +0000782 }
783 }
784
785 // Temporary symbols like the ones for directional jumps don't go in the
786 // symbol table. They also need to be diagnosed in all (final) cases.
Tim Northoverc0bef992016-04-13 19:46:54 +0000787 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
788 if (std::get<2>(LocSym)->isUndefined()) {
789 // Reset the state of any "# line file" directives we've seen to the
790 // context as it was at the diagnostic site.
791 CppHashInfo = std::get<1>(LocSym);
Eric Christopher04c7db32016-09-13 00:19:29 +0000792 HadError |= Error(std::get<0>(LocSym), "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +0000793 }
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000794 }
795 }
796
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000797 // Finalize the output stream if there are no errors and if the client wants
798 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000799 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000800 Out.Finish();
801
Oliver Stannard07b43d32015-11-17 09:58:07 +0000802 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000803}
804
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000805void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000806 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Eric Christopher04c7db32016-09-13 00:19:29 +0000807 TokError("expected section directive before assembly directive");
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000808 Out.InitSections(false);
Daniel Dunbare5444a82010-09-09 22:42:59 +0000809 }
810}
811
Jim Grosbach4b905842013-09-20 23:08:21 +0000812/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000813void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000814 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Dave1180e6892016-06-02 17:15:05 +0000815 Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000816
Chris Lattnere5074c42009-06-22 01:29:09 +0000817 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000818 if (Lexer.is(AsmToken::EndOfStatement))
Nirav Dave1180e6892016-06-02 17:15:05 +0000819 Lexer.Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000820}
821
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000822StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000823 const char *Start = getTok().getLoc().getPointer();
824
Jim Grosbach4b905842013-09-20 23:08:21 +0000825 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000826 Lexer.Lex();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000827
828 const char *End = getTok().getLoc().getPointer();
829 return StringRef(Start, End - Start);
830}
Chris Lattner78db3622009-06-22 05:51:26 +0000831
Jim Grosbach4b905842013-09-20 23:08:21 +0000832StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000833 const char *Start = getTok().getLoc().getPointer();
834
835 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000836 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000837 Lexer.Lex();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000838
839 const char *End = getTok().getLoc().getPointer();
840 return StringRef(Start, End - Start);
841}
842
Jim Grosbach4b905842013-09-20 23:08:21 +0000843/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000844/// NOTE: This assumes the leading '(' has already been consumed.
845///
846/// parenexpr ::= expr)
847///
Jim Grosbach4b905842013-09-20 23:08:21 +0000848bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
849 if (parseExpression(Res))
850 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000851 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000852 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000853 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000854 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000855 return false;
856}
Chris Lattner78db3622009-06-22 05:51:26 +0000857
Jim Grosbach4b905842013-09-20 23:08:21 +0000858/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000859/// NOTE: This assumes the leading '[' has already been consumed.
860///
861/// bracketexpr ::= expr]
862///
Jim Grosbach4b905842013-09-20 23:08:21 +0000863bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
864 if (parseExpression(Res))
865 return true;
Nirav Davea645433c2016-07-18 15:24:03 +0000866 EndLoc = getTok().getEndLoc();
867 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
868 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000869 return false;
870}
871
Jim Grosbach4b905842013-09-20 23:08:21 +0000872/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000873/// primaryexpr ::= (parenexpr
874/// primaryexpr ::= symbol
875/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000876/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000877/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000878bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000879 SMLoc FirstTokenLoc = getLexer().getLoc();
880 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
881 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000882 default:
883 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000884 // If we have an error assume that we've already handled it.
885 case AsmToken::Error:
886 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000887 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000888 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000889 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000890 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000891 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000892 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000893 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000894 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000895 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000896 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000897 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000898 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000899 if (FirstTokenKind == AsmToken::Dollar) {
900 if (Lexer.getMAI().getDollarIsPC()) {
901 // This is a '$' reference, which references the current PC. Emit a
902 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000903 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000904 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000905 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000906 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000907 EndLoc = FirstTokenLoc;
908 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000909 }
910 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000911 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000912 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000913 // Parse symbol variant
914 std::pair<StringRef, StringRef> Split;
915 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000916 if (FirstTokenKind == AsmToken::String) {
917 if (Lexer.is(AsmToken::At)) {
Nirav Davefd910412016-06-17 16:06:17 +0000918 Lex(); // eat @
David Majnemer6a5b8122014-06-19 01:25:43 +0000919 SMLoc AtLoc = getLexer().getLoc();
920 StringRef VName;
921 if (parseIdentifier(VName))
922 return Error(AtLoc, "expected symbol variant after '@'");
923
924 Split = std::make_pair(Identifier, VName);
925 }
926 } else {
927 Split = Identifier.split('@');
928 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000929 } else if (Lexer.is(AsmToken::LParen)) {
Nirav Davefd910412016-06-17 16:06:17 +0000930 Lex(); // eat '('.
David Peixotto8ad70b32013-12-04 22:43:20 +0000931 StringRef VName;
932 parseIdentifier(VName);
Nirav Davea645433c2016-07-18 15:24:03 +0000933 // eat ')'.
934 if (parseToken(AsmToken::RParen,
935 "unexpected token in variant, expected ')'"))
936 return true;
David Peixotto8ad70b32013-12-04 22:43:20 +0000937 Split = std::make_pair(Identifier, VName);
938 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000939
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000940 EndLoc = SMLoc::getFromPointer(Identifier.end());
941
Daniel Dunbard20cda02009-10-16 01:34:54 +0000942 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000943 StringRef SymbolName = Identifier;
944 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000945
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000946 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000947 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000948 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000949 if (Variant != MCSymbolRefExpr::VK_Invalid) {
950 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000951 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000952 Variant = MCSymbolRefExpr::VK_None;
953 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000954 return Error(SMLoc::getFromPointer(Split.second.begin()),
955 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000956 }
957 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000958
Jim Grosbach6f482002015-05-18 18:43:14 +0000959 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000960
Daniel Dunbard20cda02009-10-16 01:34:54 +0000961 // If this is an absolute variable reference, substitute it now to preserve
962 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000963 if (Sym->isVariable() &&
964 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000965 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000966 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000967
Vedant Kumar86dbd922015-08-31 17:44:53 +0000968 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000969 return false;
970 }
971
972 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000973 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000974 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000975 }
David Woodhousef42a6662014-02-01 16:20:54 +0000976 case AsmToken::BigNum:
977 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000978 case AsmToken::Integer: {
979 SMLoc Loc = getTok().getLoc();
980 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000981 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000982 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000983 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000984 // Look for 'b' or 'f' following an Integer as a directional label
985 if (Lexer.getKind() == AsmToken::Identifier) {
986 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000987 // Lookup the symbol variant if used.
988 std::pair<StringRef, StringRef> Split = IDVal.split('@');
989 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
990 if (Split.first.size() != IDVal.size()) {
991 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000992 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000993 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000994 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000995 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000996 if (IDVal == "f" || IDVal == "b") {
997 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +0000998 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +0000999 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00001000 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +00001001 return Error(Loc, "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +00001002 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001003 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +00001004 Lex(); // Eat identifier.
1005 }
1006 }
Chris Lattner78db3622009-06-22 05:51:26 +00001007 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +00001008 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001009 case AsmToken::Real: {
1010 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +00001011 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001012 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001013 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001014 Lex(); // Eat token.
1015 return false;
1016 }
Chris Lattner6b55cb92010-04-14 04:40:28 +00001017 case AsmToken::Dot: {
1018 // This is a '.' reference, which references the current PC. Emit a
1019 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +00001020 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001021 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001022 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001023 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001024 Lex(); // Eat identifier.
1025 return false;
1026 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001027 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001028 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +00001029 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +00001030 case AsmToken::LBrac:
1031 if (!PlatformParser->HasBracketExpressions())
1032 return TokError("brackets expression not supported on this target");
1033 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +00001034 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001035 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001036 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001038 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001039 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001040 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001041 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001042 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001044 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001045 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001046 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001047 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001048 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001049 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001050 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001051 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001052 return false;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +00001053 // MIPS unary expression operators. The lexer won't generate these tokens if
1054 // MCAsmInfo::HasMipsExpressions is false for the target.
1055 case AsmToken::PercentCall16:
1056 case AsmToken::PercentCall_Hi:
1057 case AsmToken::PercentCall_Lo:
1058 case AsmToken::PercentDtprel_Hi:
1059 case AsmToken::PercentDtprel_Lo:
1060 case AsmToken::PercentGot:
1061 case AsmToken::PercentGot_Disp:
1062 case AsmToken::PercentGot_Hi:
1063 case AsmToken::PercentGot_Lo:
1064 case AsmToken::PercentGot_Ofst:
1065 case AsmToken::PercentGot_Page:
1066 case AsmToken::PercentGottprel:
1067 case AsmToken::PercentGp_Rel:
1068 case AsmToken::PercentHi:
1069 case AsmToken::PercentHigher:
1070 case AsmToken::PercentHighest:
1071 case AsmToken::PercentLo:
1072 case AsmToken::PercentNeg:
1073 case AsmToken::PercentPcrel_Hi:
1074 case AsmToken::PercentPcrel_Lo:
1075 case AsmToken::PercentTlsgd:
1076 case AsmToken::PercentTlsldm:
1077 case AsmToken::PercentTprel_Hi:
1078 case AsmToken::PercentTprel_Lo:
1079 Lex(); // Eat the operator.
1080 if (Lexer.isNot(AsmToken::LParen))
1081 return TokError("expected '(' after operator");
1082 Lex(); // Eat the operator.
1083 if (parseExpression(Res, EndLoc))
1084 return true;
1085 if (Lexer.isNot(AsmToken::RParen))
1086 return TokError("expected ')'");
1087 Lex(); // Eat the operator.
1088 Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
1089 return !Res;
Chris Lattner78db3622009-06-22 05:51:26 +00001090 }
1091}
Chris Lattner7fdbce72009-06-22 06:32:03 +00001092
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001093bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001094 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001095 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +00001096}
1097
Daniel Dunbar55f16672010-09-17 02:47:07 +00001098const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +00001099AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +00001100 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +00001101 // Ask the target implementation about this expression first.
1102 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1103 if (NewE)
1104 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001105 // Recurse over the given expression, rebuilding it to apply the given variant
1106 // if there is exactly one symbol.
1107 switch (E->getKind()) {
1108 case MCExpr::Target:
1109 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001110 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001111
1112 case MCExpr::SymbolRef: {
1113 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1114
1115 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001116 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1117 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001118 return E;
1119 }
1120
Jim Grosbach13760bd2015-05-30 01:25:56 +00001121 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001122 }
1123
1124 case MCExpr::Unary: {
1125 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001126 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001127 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001128 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001129 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001130 }
1131
1132 case MCExpr::Binary: {
1133 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1135 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001136
1137 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001138 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001139
Jim Grosbach4b905842013-09-20 23:08:21 +00001140 if (!LHS)
1141 LHS = BE->getLHS();
1142 if (!RHS)
1143 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001144
Jim Grosbach13760bd2015-05-30 01:25:56 +00001145 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001146 }
1147 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001148
Craig Toppera2886c22012-02-07 05:05:23 +00001149 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001150}
1151
Jim Grosbach4b905842013-09-20 23:08:21 +00001152/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153///
Jim Grosbachbd164242011-08-20 16:24:13 +00001154/// expr ::= expr &&,|| expr -> lowest.
1155/// expr ::= expr |,^,&,! expr
1156/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1157/// expr ::= expr <<,>> expr
1158/// expr ::= expr +,- expr
1159/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001160/// expr ::= primaryexpr
1161///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001162bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001163 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001164 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001165 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001166 return true;
1167
Daniel Dunbar55f16672010-09-17 02:47:07 +00001168 // As a special case, we support 'a op b @ modifier' by rewriting the
1169 // expression to include the modifier. This is inefficient, but in general we
1170 // expect users to use 'a@modifier op b'.
1171 if (Lexer.getKind() == AsmToken::At) {
1172 Lex();
1173
1174 if (Lexer.isNot(AsmToken::Identifier))
1175 return TokError("unexpected symbol modifier following '@'");
1176
1177 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001178 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001179 if (Variant == MCSymbolRefExpr::VK_Invalid)
1180 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1181
Jim Grosbach4b905842013-09-20 23:08:21 +00001182 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001183 if (!ModifiedRes) {
1184 return TokError("invalid modifier '" + getTok().getIdentifier() +
1185 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001186 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001187
Daniel Dunbar55f16672010-09-17 02:47:07 +00001188 Res = ModifiedRes;
1189 Lex();
1190 }
1191
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001192 // Try to constant fold it up front, if possible.
1193 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001194 if (Res->evaluateAsAbsolute(Value))
1195 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001196
1197 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001198}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001199
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001200bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001201 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001202 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001203}
1204
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001205bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1206 SMLoc &EndLoc) {
1207 if (parseParenExpr(Res, EndLoc))
1208 return true;
1209
1210 for (; ParenDepth > 0; --ParenDepth) {
1211 if (parseBinOpRHS(1, Res, EndLoc))
1212 return true;
1213
1214 // We don't Lex() the last RParen.
1215 // This is the same behavior as parseParenExpression().
1216 if (ParenDepth - 1 > 0) {
Nirav Davea645433c2016-07-18 15:24:03 +00001217 EndLoc = getTok().getEndLoc();
1218 if (parseToken(AsmToken::RParen,
1219 "expected ')' in parentheses expression"))
1220 return true;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001221 }
1222 }
1223 return false;
1224}
1225
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001226bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001227 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001228
Daniel Dunbar75630b32009-06-30 02:10:03 +00001229 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001230 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001231 return true;
1232
Jim Grosbach13760bd2015-05-30 01:25:56 +00001233 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001234 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001235
1236 return false;
1237}
1238
David Majnemer0993e0b2015-10-26 03:15:34 +00001239static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1240 MCBinaryExpr::Opcode &Kind,
1241 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001242 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001243 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001244 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001245
Jim Grosbach4b905842013-09-20 23:08:21 +00001246 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001247 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001248 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001249 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001250 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001251 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001252 return 1;
1253
Jim Grosbach4b905842013-09-20 23:08:21 +00001254 // Low Precedence: |, &, ^
1255 //
1256 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001257 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001258 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001259 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001260 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001261 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001262 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001263 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001264 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001265 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001266
Jim Grosbach4b905842013-09-20 23:08:21 +00001267 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001268 case AsmToken::EqualEqual:
1269 Kind = MCBinaryExpr::EQ;
1270 return 3;
1271 case AsmToken::ExclaimEqual:
1272 case AsmToken::LessGreater:
1273 Kind = MCBinaryExpr::NE;
1274 return 3;
1275 case AsmToken::Less:
1276 Kind = MCBinaryExpr::LT;
1277 return 3;
1278 case AsmToken::LessEqual:
1279 Kind = MCBinaryExpr::LTE;
1280 return 3;
1281 case AsmToken::Greater:
1282 Kind = MCBinaryExpr::GT;
1283 return 3;
1284 case AsmToken::GreaterEqual:
1285 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001286 return 3;
1287
Jim Grosbach4b905842013-09-20 23:08:21 +00001288 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001289 case AsmToken::LessLess:
1290 Kind = MCBinaryExpr::Shl;
1291 return 4;
1292 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001293 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001294 return 4;
1295
Jim Grosbach4b905842013-09-20 23:08:21 +00001296 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001297 case AsmToken::Plus:
1298 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001299 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001300 case AsmToken::Minus:
1301 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001302 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001303
Jim Grosbach4b905842013-09-20 23:08:21 +00001304 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001305 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001306 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001307 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001308 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001309 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001310 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001311 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001312 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001313 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001314 }
1315}
1316
David Majnemer0993e0b2015-10-26 03:15:34 +00001317static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1318 MCBinaryExpr::Opcode &Kind,
1319 bool ShouldUseLogicalShr) {
1320 switch (K) {
1321 default:
1322 return 0; // not a binop.
1323
1324 // Lowest Precedence: &&, ||
1325 case AsmToken::AmpAmp:
1326 Kind = MCBinaryExpr::LAnd;
1327 return 2;
1328 case AsmToken::PipePipe:
1329 Kind = MCBinaryExpr::LOr;
1330 return 1;
1331
1332 // Low Precedence: ==, !=, <>, <, <=, >, >=
1333 case AsmToken::EqualEqual:
1334 Kind = MCBinaryExpr::EQ;
1335 return 3;
1336 case AsmToken::ExclaimEqual:
1337 case AsmToken::LessGreater:
1338 Kind = MCBinaryExpr::NE;
1339 return 3;
1340 case AsmToken::Less:
1341 Kind = MCBinaryExpr::LT;
1342 return 3;
1343 case AsmToken::LessEqual:
1344 Kind = MCBinaryExpr::LTE;
1345 return 3;
1346 case AsmToken::Greater:
1347 Kind = MCBinaryExpr::GT;
1348 return 3;
1349 case AsmToken::GreaterEqual:
1350 Kind = MCBinaryExpr::GTE;
1351 return 3;
1352
1353 // Low Intermediate Precedence: +, -
1354 case AsmToken::Plus:
1355 Kind = MCBinaryExpr::Add;
1356 return 4;
1357 case AsmToken::Minus:
1358 Kind = MCBinaryExpr::Sub;
1359 return 4;
1360
1361 // High Intermediate Precedence: |, &, ^
1362 //
1363 // FIXME: gas seems to support '!' as an infix operator?
1364 case AsmToken::Pipe:
1365 Kind = MCBinaryExpr::Or;
1366 return 5;
1367 case AsmToken::Caret:
1368 Kind = MCBinaryExpr::Xor;
1369 return 5;
1370 case AsmToken::Amp:
1371 Kind = MCBinaryExpr::And;
1372 return 5;
1373
1374 // Highest Precedence: *, /, %, <<, >>
1375 case AsmToken::Star:
1376 Kind = MCBinaryExpr::Mul;
1377 return 6;
1378 case AsmToken::Slash:
1379 Kind = MCBinaryExpr::Div;
1380 return 6;
1381 case AsmToken::Percent:
1382 Kind = MCBinaryExpr::Mod;
1383 return 6;
1384 case AsmToken::LessLess:
1385 Kind = MCBinaryExpr::Shl;
1386 return 6;
1387 case AsmToken::GreaterGreater:
1388 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1389 return 6;
1390 }
1391}
1392
1393unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1394 MCBinaryExpr::Opcode &Kind) {
1395 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1396 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1397 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1398}
1399
Jim Grosbach4b905842013-09-20 23:08:21 +00001400/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001401/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001402bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001403 SMLoc &EndLoc) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001404 while (true) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001405 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001406 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001407
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001408 // If the next token is lower precedence than we are allowed to eat, return
1409 // successfully with what we ate already.
1410 if (TokPrec < Precedence)
1411 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001412
Sean Callanan686ed8d2010-01-19 20:22:31 +00001413 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001414
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001415 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001416 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001417 if (parsePrimaryExpr(RHS, EndLoc))
1418 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001419
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001420 // If BinOp binds less tightly with RHS than the operator after RHS, let
1421 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001422 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001423 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001424 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1425 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001426
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001427 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001428 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001429 }
1430}
1431
Chris Lattner36e02122009-06-21 20:54:55 +00001432/// ParseStatement:
1433/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001434/// ::= Label* Directive ...Operands... EndOfStatement
1435/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001436bool AsmParser::parseStatement(ParseStatementInfo &Info,
1437 MCAsmParserSemaCallback *SI) {
Nirav Davefd910412016-06-17 16:06:17 +00001438 // Eat initial spaces and comments
1439 while (Lexer.is(AsmToken::Space))
1440 Lex();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001441 if (Lexer.is(AsmToken::EndOfStatement)) {
Nirav Davefd910412016-06-17 16:06:17 +00001442 // if this is a line comment we can drop it safely
1443 if (getTok().getString().front() == '\r' ||
1444 getTok().getString().front() == '\n')
1445 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001446 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001447 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001448 }
Nirav Dave9263ae32016-08-02 19:17:54 +00001449 if (Lexer.is(AsmToken::Hash)) {
1450 // Seeing a hash here means that it was an end-of-line comment in
1451 // an asm syntax where hash's are not comment and the previous
1452 // statement parser did not check the end of statement. Relex as
1453 // EndOfStatement.
1454 StringRef CommentStr = parseStringToEndOfStatement();
1455 Lexer.Lex();
1456 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1457 return false;
1458 }
Nirav Davefd910412016-06-17 16:06:17 +00001459 // Statements always start with an identifier.
Sean Callanan936b0d32010-01-19 21:44:56 +00001460 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001461 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001462 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001463 int64_t LocalLabelVal = -1;
Nirav Davefd910412016-06-17 16:06:17 +00001464 if (Lexer.is(AsmToken::HashDirective))
Jim Grosbach4b905842013-09-20 23:08:21 +00001465 return parseCppHashLineFilenameComment(IDLoc);
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001466 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001467 if (Lexer.is(AsmToken::Integer)) {
1468 LocalLabelVal = getTok().getIntVal();
1469 if (LocalLabelVal < 0) {
Eric Christopher04c7db32016-09-13 00:19:29 +00001470 if (!TheCondState.Ignore)
1471 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001472 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001473 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001474 IDVal = getTok().getString();
1475 Lex(); // Consume the integer token to be used as an identifier token.
1476 if (Lexer.getKind() != AsmToken::Colon) {
Eric Christopher04c7db32016-09-13 00:19:29 +00001477 if (!TheCondState.Ignore)
1478 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001479 }
1480 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001481 } else if (Lexer.is(AsmToken::Dot)) {
1482 // Treat '.' as a valid identifier in this context.
1483 Lex();
1484 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001485 } else if (Lexer.is(AsmToken::LCurly)) {
1486 // Treat '{' as a valid identifier in this context.
1487 Lex();
1488 IDVal = "{";
1489
1490 } else if (Lexer.is(AsmToken::RCurly)) {
1491 // Treat '}' as a valid identifier in this context.
1492 Lex();
1493 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001494 } else if (parseIdentifier(IDVal)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00001495 if (!TheCondState.Ignore)
1496 return TokError("unexpected token at start of statement");
Chris Lattner926885c2010-04-17 18:14:27 +00001497 IDVal = "";
1498 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001499
Chris Lattner926885c2010-04-17 18:14:27 +00001500 // Handle conditional assembly here before checking for skipping. We
1501 // have to do this so that .endif isn't skipped in a ".if 0" block for
1502 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001503 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001504 DirectiveKindMap.find(IDVal);
1505 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1506 ? DK_NO_DIRECTIVE
1507 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001508 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001509 default:
1510 break;
1511 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001512 case DK_IFEQ:
1513 case DK_IFGE:
1514 case DK_IFGT:
1515 case DK_IFLE:
1516 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001517 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001518 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001519 case DK_IFB:
1520 return parseDirectiveIfb(IDLoc, true);
1521 case DK_IFNB:
1522 return parseDirectiveIfb(IDLoc, false);
1523 case DK_IFC:
1524 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001525 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001526 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001527 case DK_IFNC:
1528 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001529 case DK_IFNES:
1530 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001531 case DK_IFDEF:
1532 return parseDirectiveIfdef(IDLoc, true);
1533 case DK_IFNDEF:
1534 case DK_IFNOTDEF:
1535 return parseDirectiveIfdef(IDLoc, false);
1536 case DK_ELSEIF:
1537 return parseDirectiveElseIf(IDLoc);
1538 case DK_ELSE:
1539 return parseDirectiveElse(IDLoc);
1540 case DK_ENDIF:
1541 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001542 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001543
Eli Bendersky88024712013-01-16 19:32:36 +00001544 // Ignore the statement if in the middle of inactive conditional
1545 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001546 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001547 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001548 return false;
1549 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001550
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001551 // FIXME: Recurse on local labels?
1552
1553 // See what kind of statement we have.
1554 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001555 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001556 if (!getTargetParser().isLabel(ID))
1557 break;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001558 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001559
Chris Lattner36e02122009-06-21 20:54:55 +00001560 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001561 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001562
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001563 // Diagnose attempt to use '.' as a label.
1564 if (IDVal == ".")
1565 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1566
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001567 // Diagnose attempt to use a variable as a label.
1568 //
1569 // FIXME: Diagnostics. Note the location of the definition as a label.
1570 // FIXME: This doesn't diagnose assignment to a symbol which has been
1571 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001572 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001573 if (LocalLabelVal == -1) {
1574 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001575 StringRef RewrittenLabel =
1576 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1577 assert(RewrittenLabel.size() &&
1578 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001579 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1580 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001581 IDVal = RewrittenLabel;
1582 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001583 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001584 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001585 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001586
1587 Sym->redefineIfPossible();
1588
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001589 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001590 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001591
Nirav Dave9263ae32016-08-02 19:17:54 +00001592 // End of Labels should be treated as end of line for lexing
1593 // purposes but that information is not available to the Lexer who
1594 // does not understand Labels. This may cause us to see a Hash
1595 // here instead of a preprocessor line comment.
1596 if (getTok().is(AsmToken::Hash)) {
1597 StringRef CommentStr = parseStringToEndOfStatement();
1598 Lexer.Lex();
1599 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1600 }
1601
Nirav Dave8ea792d2016-07-13 14:03:12 +00001602 // Consume any end of statement token, if present, to avoid spurious
1603 // AddBlankLine calls().
1604 if (getTok().is(AsmToken::EndOfStatement)) {
1605 Lex();
1606 }
1607
Daniel Dunbare73b2672009-08-26 22:13:22 +00001608 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001609 if (!ParsingInlineAsm)
1610 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001611
Kevin Enderbye7739d42011-12-09 18:09:40 +00001612 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001613 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001614 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001615 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1616 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001617
Tim Northover1744d0a2013-10-25 12:49:50 +00001618 getTargetParser().onLabelParsed(Sym);
1619
Eli Friedman0f4871d2012-10-22 23:58:19 +00001620 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001621 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001622
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001623 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001624 if (!getTargetParser().equalIsAsmAssignment())
1625 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001626 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001627 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001628
Jim Grosbach4b905842013-09-20 23:08:21 +00001629 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001630
1631 default: // Normal instruction or directive.
1632 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001633 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001634
1635 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001636 if (areMacrosEnabled())
1637 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1638 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001639 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001640
Michael J. Spencer530ce852010-10-09 11:00:50 +00001641 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001642
Eli Bendersky17233942013-01-15 22:59:42 +00001643 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001644 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001645 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001646 //
Eli Bendersky17233942013-01-15 22:59:42 +00001647 // 1. The target-specific assembly parser. Some directives are target
1648 // specific or may potentially behave differently on certain targets.
1649 // 2. Asm parser extensions. For example, platform-specific parsers
1650 // (like the ELF parser) register themselves as extensions.
1651 // 3. The generic directive parser implemented by this class. These are
1652 // all the directives that behave in a target and platform independent
1653 // manner, or at least have a default behavior that's shared between
1654 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001655
Oliver Stannard21718282016-07-26 14:19:47 +00001656 getTargetParser().flushPendingInstructions(getStreamer());
1657
Eric Christopher04c7db32016-09-13 00:19:29 +00001658 // First query the target-specific parser. It will return 'true' if it
1659 // isn't interested in this directive.
1660 if (!getTargetParser().ParseDirective(ID))
Akira Hatanakad3590752012-07-05 19:09:33 +00001661 return false;
1662
Alp Tokercb402912014-01-24 17:20:08 +00001663 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001664 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001665 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1666 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001667 if (Handler.first)
1668 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1669
1670 // Finally, if no one else is interested in this directive, it must be
1671 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001672 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001673 default:
1674 break;
1675 case DK_SET:
1676 case DK_EQU:
1677 return parseDirectiveSet(IDVal, true);
1678 case DK_EQUIV:
1679 return parseDirectiveSet(IDVal, false);
1680 case DK_ASCII:
1681 return parseDirectiveAscii(IDVal, false);
1682 case DK_ASCIZ:
1683 case DK_STRING:
1684 return parseDirectiveAscii(IDVal, true);
1685 case DK_BYTE:
1686 return parseDirectiveValue(1);
1687 case DK_SHORT:
1688 case DK_VALUE:
1689 case DK_2BYTE:
1690 return parseDirectiveValue(2);
1691 case DK_LONG:
1692 case DK_INT:
1693 case DK_4BYTE:
1694 return parseDirectiveValue(4);
1695 case DK_QUAD:
1696 case DK_8BYTE:
1697 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001698 case DK_OCTA:
1699 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001700 case DK_SINGLE:
1701 case DK_FLOAT:
1702 return parseDirectiveRealValue(APFloat::IEEEsingle);
1703 case DK_DOUBLE:
1704 return parseDirectiveRealValue(APFloat::IEEEdouble);
1705 case DK_ALIGN: {
1706 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1707 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1708 }
1709 case DK_ALIGN32: {
1710 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1711 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1712 }
1713 case DK_BALIGN:
1714 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1715 case DK_BALIGNW:
1716 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1717 case DK_BALIGNL:
1718 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1719 case DK_P2ALIGN:
1720 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1721 case DK_P2ALIGNW:
1722 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1723 case DK_P2ALIGNL:
1724 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1725 case DK_ORG:
1726 return parseDirectiveOrg();
1727 case DK_FILL:
1728 return parseDirectiveFill();
1729 case DK_ZERO:
1730 return parseDirectiveZero();
1731 case DK_EXTERN:
1732 eatToEndOfStatement(); // .extern is the default, ignore it.
1733 return false;
1734 case DK_GLOBL:
1735 case DK_GLOBAL:
1736 return parseDirectiveSymbolAttribute(MCSA_Global);
1737 case DK_LAZY_REFERENCE:
1738 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1739 case DK_NO_DEAD_STRIP:
1740 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1741 case DK_SYMBOL_RESOLVER:
1742 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1743 case DK_PRIVATE_EXTERN:
1744 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1745 case DK_REFERENCE:
1746 return parseDirectiveSymbolAttribute(MCSA_Reference);
1747 case DK_WEAK_DEFINITION:
1748 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1749 case DK_WEAK_REFERENCE:
1750 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1751 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1752 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1753 case DK_COMM:
1754 case DK_COMMON:
1755 return parseDirectiveComm(/*IsLocal=*/false);
1756 case DK_LCOMM:
1757 return parseDirectiveComm(/*IsLocal=*/true);
1758 case DK_ABORT:
1759 return parseDirectiveAbort();
1760 case DK_INCLUDE:
1761 return parseDirectiveInclude();
1762 case DK_INCBIN:
1763 return parseDirectiveIncbin();
1764 case DK_CODE16:
1765 case DK_CODE16GCC:
Nirav Davefd910412016-06-17 16:06:17 +00001766 return TokError(Twine(IDVal) +
1767 " not currently supported for this target");
Jim Grosbach4b905842013-09-20 23:08:21 +00001768 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001769 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001770 case DK_IRP:
1771 return parseDirectiveIrp(IDLoc);
1772 case DK_IRPC:
1773 return parseDirectiveIrpc(IDLoc);
1774 case DK_ENDR:
1775 return parseDirectiveEndr(IDLoc);
1776 case DK_BUNDLE_ALIGN_MODE:
1777 return parseDirectiveBundleAlignMode();
1778 case DK_BUNDLE_LOCK:
1779 return parseDirectiveBundleLock();
1780 case DK_BUNDLE_UNLOCK:
1781 return parseDirectiveBundleUnlock();
1782 case DK_SLEB128:
1783 return parseDirectiveLEB128(true);
1784 case DK_ULEB128:
1785 return parseDirectiveLEB128(false);
1786 case DK_SPACE:
1787 case DK_SKIP:
1788 return parseDirectiveSpace(IDVal);
1789 case DK_FILE:
1790 return parseDirectiveFile(IDLoc);
1791 case DK_LINE:
1792 return parseDirectiveLine();
1793 case DK_LOC:
1794 return parseDirectiveLoc();
1795 case DK_STABS:
1796 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001797 case DK_CV_FILE:
1798 return parseDirectiveCVFile();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00001799 case DK_CV_FUNC_ID:
1800 return parseDirectiveCVFuncId();
1801 case DK_CV_INLINE_SITE_ID:
1802 return parseDirectiveCVInlineSiteId();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001803 case DK_CV_LOC:
1804 return parseDirectiveCVLoc();
1805 case DK_CV_LINETABLE:
1806 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001807 case DK_CV_INLINE_LINETABLE:
1808 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001809 case DK_CV_DEF_RANGE:
1810 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001811 case DK_CV_STRINGTABLE:
1812 return parseDirectiveCVStringTable();
1813 case DK_CV_FILECHECKSUMS:
1814 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001815 case DK_CFI_SECTIONS:
1816 return parseDirectiveCFISections();
1817 case DK_CFI_STARTPROC:
1818 return parseDirectiveCFIStartProc();
1819 case DK_CFI_ENDPROC:
1820 return parseDirectiveCFIEndProc();
1821 case DK_CFI_DEF_CFA:
1822 return parseDirectiveCFIDefCfa(IDLoc);
1823 case DK_CFI_DEF_CFA_OFFSET:
1824 return parseDirectiveCFIDefCfaOffset();
1825 case DK_CFI_ADJUST_CFA_OFFSET:
1826 return parseDirectiveCFIAdjustCfaOffset();
1827 case DK_CFI_DEF_CFA_REGISTER:
1828 return parseDirectiveCFIDefCfaRegister(IDLoc);
1829 case DK_CFI_OFFSET:
1830 return parseDirectiveCFIOffset(IDLoc);
1831 case DK_CFI_REL_OFFSET:
1832 return parseDirectiveCFIRelOffset(IDLoc);
1833 case DK_CFI_PERSONALITY:
1834 return parseDirectiveCFIPersonalityOrLsda(true);
1835 case DK_CFI_LSDA:
1836 return parseDirectiveCFIPersonalityOrLsda(false);
1837 case DK_CFI_REMEMBER_STATE:
1838 return parseDirectiveCFIRememberState();
1839 case DK_CFI_RESTORE_STATE:
1840 return parseDirectiveCFIRestoreState();
1841 case DK_CFI_SAME_VALUE:
1842 return parseDirectiveCFISameValue(IDLoc);
1843 case DK_CFI_RESTORE:
1844 return parseDirectiveCFIRestore(IDLoc);
1845 case DK_CFI_ESCAPE:
1846 return parseDirectiveCFIEscape();
1847 case DK_CFI_SIGNAL_FRAME:
1848 return parseDirectiveCFISignalFrame();
1849 case DK_CFI_UNDEFINED:
1850 return parseDirectiveCFIUndefined(IDLoc);
1851 case DK_CFI_REGISTER:
1852 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001853 case DK_CFI_WINDOW_SAVE:
1854 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001855 case DK_MACROS_ON:
1856 case DK_MACROS_OFF:
1857 return parseDirectiveMacrosOnOff(IDVal);
1858 case DK_MACRO:
1859 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001860 case DK_EXITM:
1861 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001862 case DK_ENDM:
1863 case DK_ENDMACRO:
1864 return parseDirectiveEndMacro(IDVal);
1865 case DK_PURGEM:
1866 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001867 case DK_END:
1868 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001869 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001870 return parseDirectiveError(IDLoc, false);
1871 case DK_ERROR:
1872 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001873 case DK_WARNING:
1874 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001875 case DK_RELOC:
1876 return parseDirectiveReloc(IDLoc);
Petr Hosek731bb9c2016-08-23 21:34:53 +00001877 case DK_DC:
1878 return parseDirectiveValue(2);
1879 case DK_DC_A:
1880 return parseDirectiveValue(getContext().getAsmInfo()->getPointerSize());
1881 case DK_DC_B:
1882 return parseDirectiveValue(1);
1883 case DK_DC_D:
1884 return parseDirectiveRealValue(APFloat::IEEEdouble);
1885 case DK_DC_L:
1886 return parseDirectiveValue(4);
1887 case DK_DC_S:
1888 return parseDirectiveRealValue(APFloat::IEEEsingle);
1889 case DK_DC_W:
1890 return parseDirectiveValue(2);
1891 case DK_DC_X:
1892 return TokError(Twine(IDVal) +
1893 " not currently supported for this target");
Eli Friedman20b02642010-07-19 04:17:25 +00001894 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001895
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001896 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001897 }
Chris Lattner36e02122009-06-21 20:54:55 +00001898
Chad Rosierc7f552c2013-02-12 21:33:51 +00001899 // __asm _emit or __asm __emit
1900 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1901 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001902 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001903
1904 // __asm align
1905 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001906 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001907
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001908 if (ParsingInlineAsm && (IDVal == "even"))
1909 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001910 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001911
Chris Lattner7cbfa442010-05-19 23:34:33 +00001912 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001913 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001914 ParseInstructionInfo IInfo(Info.AsmRewrites);
Eric Christopher04c7db32016-09-13 00:19:29 +00001915 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
1916 Info.ParsedOperands);
1917 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001918
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001919 // Dump the parsed representation, if requested.
1920 if (getShowParsedOperands()) {
1921 SmallString<256> Str;
1922 raw_svector_ostream OS(Str);
1923 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001924 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001925 if (i != 0)
1926 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001927 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001928 }
1929 OS << "]";
1930
Jim Grosbach4b905842013-09-20 23:08:21 +00001931 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001932 }
1933
Oliver Stannard8b273082014-06-19 15:52:37 +00001934 // If we are generating dwarf for the current section then generate a .loc
1935 // directive for the instruction.
Eric Christopher04c7db32016-09-13 00:19:29 +00001936 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001937 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00001938 getStreamer().getCurrentSection().first)) {
1939 unsigned Line;
1940 if (ActiveMacros.empty())
1941 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1942 else
Frederic Riss16238d92015-06-25 21:57:33 +00001943 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
1944 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001945
Eli Bendersky88024712013-01-16 19:32:36 +00001946 // If we previously parsed a cpp hash file line comment then make sure the
1947 // current Dwarf File is for the CppHashFilename if not then emit the
1948 // Dwarf File table for it and adjust the line number for the .loc.
Tim Northoverc0bef992016-04-13 19:46:54 +00001949 if (CppHashInfo.Filename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00001950 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
Tim Northoverc0bef992016-04-13 19:46:54 +00001951 0, StringRef(), CppHashInfo.Filename);
David Blaikiec714ef42014-03-17 01:52:11 +00001952 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001953
Jim Grosbach4b905842013-09-20 23:08:21 +00001954 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1955 // cache with the different Loc from the call above we save the last
1956 // info we queried here with SrcMgr.FindLineNumber().
1957 unsigned CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001958 if (LastQueryIDLoc == CppHashInfo.Loc &&
1959 LastQueryBuffer == CppHashInfo.Buf)
Jim Grosbach4b905842013-09-20 23:08:21 +00001960 CppHashLocLineNo = LastQueryLine;
1961 else {
Tim Northoverc0bef992016-04-13 19:46:54 +00001962 CppHashLocLineNo =
1963 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001964 LastQueryLine = CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00001965 LastQueryIDLoc = CppHashInfo.Loc;
1966 LastQueryBuffer = CppHashInfo.Buf;
Jim Grosbach4b905842013-09-20 23:08:21 +00001967 }
Tim Northoverc0bef992016-04-13 19:46:54 +00001968 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001969 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001970
Jim Grosbach4b905842013-09-20 23:08:21 +00001971 getStreamer().EmitDwarfLocDirective(
1972 getContext().getGenDwarfFileNumber(), Line, 0,
1973 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1974 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001975 }
1976
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001977 // If parsing succeeded, match the instruction.
Eric Christopher04c7db32016-09-13 00:19:29 +00001978 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001979 uint64_t ErrorInfo;
Eric Christopher04c7db32016-09-13 00:19:29 +00001980 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1981 Info.ParsedOperands, Out,
1982 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001983 }
Eric Christopher04c7db32016-09-13 00:19:29 +00001984
1985 // Don't skip the rest of the line, the instruction parser is responsible for
1986 // that.
Chris Lattnera2a9d162010-09-11 16:18:25 +00001987 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001988}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001989
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00001990// Parse and erase curly braces marking block start/end
1991bool
1992AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
1993 // Identify curly brace marking block start/end
1994 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
1995 return false;
1996
1997 SMLoc StartLoc = Lexer.getLoc();
1998 Lex(); // Eat the brace
1999 if (Lexer.is(AsmToken::EndOfStatement))
2000 Lex(); // Eat EndOfStatement following the brace
2001
2002 // Erase the block start/end brace from the output asm string
2003 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
2004 StartLoc.getPointer());
2005 return true;
2006}
2007
Jim Grosbach4b905842013-09-20 23:08:21 +00002008/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00002009/// ::= # number "filename"
Craig Topper3c76c522015-09-20 23:35:59 +00002010bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00002011 Lex(); // Eat the hash token.
Nirav Davefd910412016-06-17 16:06:17 +00002012 // Lexer only ever emits HashDirective if it fully formed if it's
2013 // done the checking already so this is an internal error.
2014 assert(getTok().is(AsmToken::Integer) &&
2015 "Lexing Cpp line comment: Expected Integer");
Kevin Enderby72553612011-09-13 23:45:18 +00002016 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00002017 Lex();
Nirav Davefd910412016-06-17 16:06:17 +00002018 assert(getTok().is(AsmToken::String) &&
2019 "Lexing Cpp line comment: Expected String");
Kevin Enderby72553612011-09-13 23:45:18 +00002020 StringRef Filename = getTok().getString();
Nirav Davefd910412016-06-17 16:06:17 +00002021 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00002022 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00002023 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00002024
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002025 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
Tim Northoverc0bef992016-04-13 19:46:54 +00002026 CppHashInfo.Loc = L;
2027 CppHashInfo.Filename = Filename;
2028 CppHashInfo.LineNumber = LineNumber;
2029 CppHashInfo.Buf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00002030 return false;
2031}
2032
Jim Grosbach4b905842013-09-20 23:08:21 +00002033/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002034/// for the Filename and LineNo if any in the diagnostic.
2035void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002036 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002037 raw_ostream &OS = errs();
2038
2039 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00002040 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00002041 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2042 unsigned CppHashBuf =
Tim Northoverc0bef992016-04-13 19:46:54 +00002043 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002044
Jim Grosbach4b905842013-09-20 23:08:21 +00002045 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002046 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00002047 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2048 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
2049 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002050 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
2051 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002052 }
2053
Eric Christophera7c32732012-12-18 00:30:54 +00002054 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002055 // manager changed or buffer changed (like in a nested include) then just
2056 // print the normal diagnostic using its Filename and LineNo.
Tim Northoverc0bef992016-04-13 19:46:54 +00002057 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002058 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00002059 if (Parser->SavedDiagHandler)
2060 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2061 else
Craig Topper353eda42014-04-24 06:44:33 +00002062 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002063 return;
2064 }
2065
Eric Christophera7c32732012-12-18 00:30:54 +00002066 // Use the CppHashFilename and calculate a line number based on the
Tim Northoverc0bef992016-04-13 19:46:54 +00002067 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
2068 // for the diagnostic.
2069 const std::string &Filename = Parser->CppHashInfo.Filename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002070
2071 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
2072 int CppHashLocLineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00002073 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00002074 int LineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00002075 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002076
Jim Grosbach4b905842013-09-20 23:08:21 +00002077 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2078 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00002079 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002080
Benjamin Kramer47f5e302011-10-16 10:48:29 +00002081 if (Parser->SavedDiagHandler)
2082 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
2083 else
Craig Topper353eda42014-04-24 06:44:33 +00002084 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002085}
2086
Rafael Espindola2c064482012-08-21 18:29:30 +00002087// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
2088// difference being that that function accepts '@' as part of identifiers and
2089// we can't do that. AsmLexer.cpp should probably be changed to handle
2090// '@' as a special case when needed.
2091static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00002092 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
2093 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00002094}
2095
Rafael Espindola34b9c512012-06-03 23:57:14 +00002096bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00002097 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00002098 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00002099 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002100 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002101 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00002102 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00002103 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002104
Preston Gurd05500642012-09-19 20:36:12 +00002105 // A macro without parameters is handled differently on Darwin:
2106 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002107 while (!Body.empty()) {
2108 // Scan for the next substitution.
2109 std::size_t End = Body.size(), Pos = 0;
2110 for (; Pos != End; ++Pos) {
2111 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00002112 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113 // This macro has no parameters, look for $0, $1, etc.
2114 if (Body[Pos] != '$' || Pos + 1 == End)
2115 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002116
Rafael Espindola1134ab232011-06-05 02:43:45 +00002117 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00002118 if (Next == '$' || Next == 'n' ||
2119 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002120 break;
2121 } else {
2122 // This macro has parameters, look for \foo, \bar, etc.
2123 if (Body[Pos] == '\\' && Pos + 1 != End)
2124 break;
2125 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002126 }
2127
2128 // Add the prefix.
2129 OS << Body.slice(0, Pos);
2130
2131 // Check if we reached the end.
2132 if (Pos == End)
2133 break;
2134
Benjamin Kramer513e7442014-02-20 13:36:32 +00002135 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002136 switch (Body[Pos + 1]) {
2137 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002138 case '$':
2139 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002140 break;
2141
Jim Grosbach4b905842013-09-20 23:08:21 +00002142 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002143 case 'n':
2144 OS << A.size();
2145 break;
2146
Jim Grosbach4b905842013-09-20 23:08:21 +00002147 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002148 default: {
2149 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002150 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002151 if (Index >= A.size())
2152 break;
2153
2154 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002155 for (const AsmToken &Token : A[Index])
2156 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002157 break;
2158 }
2159 }
2160 Pos += 2;
2161 } else {
2162 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002163
2164 // Check for the \@ pseudo-variable.
2165 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002166 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002167 else
2168 while (isIdentifierChar(Body[I]) && I + 1 != End)
2169 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002170
Jim Grosbach4b905842013-09-20 23:08:21 +00002171 const char *Begin = Body.data() + Pos + 1;
2172 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002173 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002174
Toma Tabacu217116e2015-04-27 10:50:29 +00002175 if (Argument == "@") {
2176 OS << NumOfMacroInstantiations;
2177 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002178 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002179 for (; Index < NParameters; ++Index)
2180 if (Parameters[Index].Name == Argument)
2181 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002182
Toma Tabacu217116e2015-04-27 10:50:29 +00002183 if (Index == NParameters) {
2184 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2185 Pos += 3;
2186 else {
2187 OS << '\\' << Argument;
2188 Pos = I;
2189 }
2190 } else {
2191 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002192 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002193 // We expect no quotes around the string's contents when
2194 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002195 if (Token.getKind() != AsmToken::String || VarargParameter)
2196 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002197 else
Craig Topper84008482015-10-10 05:38:14 +00002198 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002199
2200 Pos += 1 + Argument.size();
2201 }
Preston Gurd05500642012-09-19 20:36:12 +00002202 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002203 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002204 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002205 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002206 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002207
Rafael Espindola1134ab232011-06-05 02:43:45 +00002208 return false;
2209}
Daniel Dunbar43235712010-07-18 18:54:11 +00002210
Nico Weber2a8f9222014-07-24 16:29:04 +00002211MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002212 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002213 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002214 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002215
Jim Grosbach4b905842013-09-20 23:08:21 +00002216static bool isOperator(AsmToken::TokenKind kind) {
2217 switch (kind) {
2218 default:
2219 return false;
2220 case AsmToken::Plus:
2221 case AsmToken::Minus:
2222 case AsmToken::Tilde:
2223 case AsmToken::Slash:
2224 case AsmToken::Star:
2225 case AsmToken::Dot:
2226 case AsmToken::Equal:
2227 case AsmToken::EqualEqual:
2228 case AsmToken::Pipe:
2229 case AsmToken::PipePipe:
2230 case AsmToken::Caret:
2231 case AsmToken::Amp:
2232 case AsmToken::AmpAmp:
2233 case AsmToken::Exclaim:
2234 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002235 case AsmToken::Less:
2236 case AsmToken::LessEqual:
2237 case AsmToken::LessLess:
2238 case AsmToken::LessGreater:
2239 case AsmToken::Greater:
2240 case AsmToken::GreaterEqual:
2241 case AsmToken::GreaterGreater:
2242 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002243 }
2244}
2245
David Majnemer16252452014-01-29 00:07:39 +00002246namespace {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002247
David Majnemer16252452014-01-29 00:07:39 +00002248class AsmLexerSkipSpaceRAII {
2249public:
2250 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2251 Lexer.setSkipSpace(SkipSpace);
2252 }
2253
2254 ~AsmLexerSkipSpaceRAII() {
2255 Lexer.setSkipSpace(true);
2256 }
2257
2258private:
2259 AsmLexer &Lexer;
2260};
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002261
2262} // end anonymous namespace
David Majnemer16252452014-01-29 00:07:39 +00002263
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002264bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2265
2266 if (Vararg) {
2267 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2268 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002269 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002270 }
2271 return false;
2272 }
2273
Rafael Espindola768b41c2012-06-15 14:02:34 +00002274 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002275
David Majnemer16252452014-01-29 00:07:39 +00002276 // Darwin doesn't use spaces to delmit arguments.
2277 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002278
Scott Egertona1fa68a2016-02-11 13:48:49 +00002279 bool SpaceEaten;
2280
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002281 while (true) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002282 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002283 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002284 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002285
Scott Egertona1fa68a2016-02-11 13:48:49 +00002286 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002287
Scott Egertona1fa68a2016-02-11 13:48:49 +00002288 if (Lexer.is(AsmToken::Comma))
2289 break;
2290
2291 if (Lexer.is(AsmToken::Space)) {
2292 SpaceEaten = true;
Nirav Dave1180e6892016-06-02 17:15:05 +00002293 Lexer.Lex(); // Eat spaces
Scott Egertona1fa68a2016-02-11 13:48:49 +00002294 }
Preston Gurd05500642012-09-19 20:36:12 +00002295
2296 // Spaces can delimit parameters, but could also be part an expression.
2297 // If the token after a space is an operator, add the token and the next
2298 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002299 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002300 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002301 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002302 Lexer.Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002303
Scott Egertona1fa68a2016-02-11 13:48:49 +00002304 // Whitespace after an operator can be ignored.
2305 if (Lexer.is(AsmToken::Space))
Nirav Dave1180e6892016-06-02 17:15:05 +00002306 Lexer.Lex();
Scott Egertona1fa68a2016-02-11 13:48:49 +00002307
2308 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002309 }
2310 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002311 if (SpaceEaten)
2312 break;
Preston Gurd05500642012-09-19 20:36:12 +00002313 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002314
Jim Grosbach4b905842013-09-20 23:08:21 +00002315 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002316 // to be able to fill in the remaining default parameter values
2317 if (Lexer.is(AsmToken::EndOfStatement))
2318 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002319
2320 // Adjust the current parentheses level.
2321 if (Lexer.is(AsmToken::LParen))
2322 ++ParenLevel;
2323 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2324 --ParenLevel;
2325
2326 // Append the token to the current argument list.
2327 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002328 Lexer.Lex();
Rafael Espindola768b41c2012-06-15 14:02:34 +00002329 }
Preston Gurd05500642012-09-19 20:36:12 +00002330
Rafael Espindola768b41c2012-06-15 14:02:34 +00002331 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002332 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002333 return false;
2334}
2335
2336// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002337bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002338 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002339 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002340 bool NamedParametersFound = false;
2341 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002342
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002343 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002344 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002345
Rafael Espindola768b41c2012-06-15 14:02:34 +00002346 // Parse two kinds of macro invocations:
2347 // - macros defined without any parameters accept an arbitrary number of them
2348 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002349 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002350 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2351 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002352 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002353 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002354
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002355 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00002356 if (parseIdentifier(FA.Name)) {
2357 Error(IDLoc, "invalid argument identifier for formal argument");
2358 eatToEndOfStatement();
2359 return true;
2360 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002361
Eric Christopher04c7db32016-09-13 00:19:29 +00002362 if (Lexer.isNot(AsmToken::Equal)) {
2363 TokError("expected '=' after formal parameter identifier");
2364 eatToEndOfStatement();
2365 return true;
2366 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002367 Lex();
2368
2369 NamedParametersFound = true;
2370 }
2371
Eric Christopher04c7db32016-09-13 00:19:29 +00002372 if (NamedParametersFound && FA.Name.empty()) {
2373 Error(IDLoc, "cannot mix positional and keyword arguments");
2374 eatToEndOfStatement();
2375 return true;
2376 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002377
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002378 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2379 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002380 return true;
2381
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002382 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002383 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002384 unsigned FAI = 0;
2385 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002386 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002387 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002388
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002389 if (FAI >= NParameters) {
Nirav Davefd910412016-06-17 16:06:17 +00002390 assert(M && "expected macro to be defined");
Eric Christopher04c7db32016-09-13 00:19:29 +00002391 Error(IDLoc,
2392 "parameter named '" + FA.Name + "' does not exist for macro '" +
2393 M->Name + "'");
2394 return true;
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002395 }
2396 PI = FAI;
2397 }
2398
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002399 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002400 if (A.size() <= PI)
2401 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002402 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002403
2404 if (FALocs.size() <= PI)
2405 FALocs.resize(PI + 1);
2406
2407 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002408 }
Jim Grosbach206661622012-07-30 22:44:17 +00002409
Preston Gurd242ed3152012-09-19 20:29:04 +00002410 // At the end of the statement, fill in remaining arguments that have
2411 // default values. If there aren't any, then the next argument is
2412 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002413 if (Lexer.is(AsmToken::EndOfStatement)) {
2414 bool Failure = false;
2415 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2416 if (A[FAI].empty()) {
2417 if (M->Parameters[FAI].Required) {
2418 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2419 "missing value for required parameter "
2420 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2421 Failure = true;
2422 }
2423
2424 if (!M->Parameters[FAI].Value.empty())
2425 A[FAI] = M->Parameters[FAI].Value;
2426 }
2427 }
2428 return Failure;
2429 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002430
2431 if (Lexer.is(AsmToken::Comma))
2432 Lex();
2433 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002434
2435 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002436}
2437
Jim Grosbach4b905842013-09-20 23:08:21 +00002438const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002439 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2440 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002441}
2442
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002443void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2444 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002445}
2446
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002447void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002448
Jim Grosbach4b905842013-09-20 23:08:21 +00002449bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Davide Italiano7c9fc732016-07-27 05:51:56 +00002450 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2451 // eliminate this, although we should protect against infinite loops.
2452 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2453 if (ActiveMacros.size() == MaxNestingDepth) {
2454 std::ostringstream MaxNestingDepthError;
2455 MaxNestingDepthError << "macros cannot be nested more than "
2456 << MaxNestingDepth << " levels deep."
2457 << " Use -asm-macro-max-nesting-depth to increase "
2458 "this limit.";
2459 return TokError(MaxNestingDepthError.str());
2460 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002461
Eli Bendersky38274122013-01-14 23:22:36 +00002462 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002463 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002464 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002465
Rafael Espindola1134ab232011-06-05 02:43:45 +00002466 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2467 // to hold the macro body with substitutions.
2468 SmallString<256> Buf;
2469 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002470 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002471
Toma Tabacu217116e2015-04-27 10:50:29 +00002472 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002473 return true;
2474
Eli Bendersky38274122013-01-14 23:22:36 +00002475 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002476 // instantiation.
2477 OS << ".endmacro\n";
2478
Rafael Espindola3560ff22014-08-27 20:03:13 +00002479 std::unique_ptr<MemoryBuffer> Instantiation =
2480 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002481
Daniel Dunbar43235712010-07-18 18:54:11 +00002482 // Create the macro instantiation object and add to the current macro
2483 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002484 MacroInstantiation *MI = new MacroInstantiation(
2485 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002486 ActiveMacros.push_back(MI);
2487
Toma Tabacu217116e2015-04-27 10:50:29 +00002488 ++NumOfMacroInstantiations;
2489
Daniel Dunbar43235712010-07-18 18:54:11 +00002490 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002491 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002492 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002493 Lex();
2494
2495 return false;
2496}
2497
Jim Grosbach4b905842013-09-20 23:08:21 +00002498void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002499 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002500 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002501 Lex();
2502
2503 // Pop the instantiation entry.
2504 delete ActiveMacros.back();
2505 ActiveMacros.pop_back();
2506}
2507
Jim Grosbach4b905842013-09-20 23:08:21 +00002508bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002509 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002510 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002511 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002512 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2513 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002514 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002515
Pete Cooper80d21cb2015-06-22 19:35:57 +00002516 if (!Sym) {
2517 // In the case where we parse an expression starting with a '.', we will
2518 // not generate an error, nor will we create a symbol. In this case we
2519 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002520 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002521 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002522
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002523 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002524 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002525 if (NoDeadStrip)
2526 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2527
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002528 return false;
2529}
2530
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002531/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002532/// ::= identifier
2533/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002534bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002535 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002536 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2537 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002538 // handle this as a context dependent token, instead we detect adjacent tokens
2539 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002540 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2541 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002542
Hans Wennborgce69d772013-10-18 20:46:28 +00002543 // Consume the prefix character, and check for a following identifier.
Nirav Dave1180e6892016-06-02 17:15:05 +00002544 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002545 if (Lexer.isNot(AsmToken::Identifier))
2546 return true;
2547
Hans Wennborgce69d772013-10-18 20:46:28 +00002548 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2549 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002550 return true;
2551
2552 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002553 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002554 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Nirav Davefd910412016-06-17 16:06:17 +00002555 Lex(); // Parser Lex to maintain invariants.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002556 return false;
2557 }
2558
Jim Grosbach4b905842013-09-20 23:08:21 +00002559 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002560 return true;
2561
Sean Callanan936b0d32010-01-19 21:44:56 +00002562 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002563
Sean Callanan686ed8d2010-01-19 20:22:31 +00002564 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002565
2566 return false;
2567}
2568
Jim Grosbach4b905842013-09-20 23:08:21 +00002569/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002570/// ::= .equ identifier ',' expression
2571/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002572/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002573bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002574 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002575
Nirav Davea645433c2016-07-18 15:24:03 +00002576 if (check(parseIdentifier(Name),
2577 "expected identifier after '" + Twine(IDVal) + "'") ||
2578 parseToken(AsmToken::Comma, "unexpected token in '" + Twine(IDVal) + "'"))
2579 return true;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002580
Jim Grosbach4b905842013-09-20 23:08:21 +00002581 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002582}
2583
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002584bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002585 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002586
2587 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002588 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002589 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2590 if (Str[i] != '\\') {
2591 Data += Str[i];
2592 continue;
2593 }
2594
2595 // Recognize escaped characters. Note that this escape semantics currently
2596 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2597 ++i;
2598 if (i == e)
2599 return TokError("unexpected backslash at end of string");
2600
2601 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002602 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002603 // Consume up to three octal characters.
2604 unsigned Value = Str[i] - '0';
2605
Jim Grosbach4b905842013-09-20 23:08:21 +00002606 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002607 ++i;
2608 Value = Value * 8 + (Str[i] - '0');
2609
Jim Grosbach4b905842013-09-20 23:08:21 +00002610 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002611 ++i;
2612 Value = Value * 8 + (Str[i] - '0');
2613 }
2614 }
2615
2616 if (Value > 255)
2617 return TokError("invalid octal escape sequence (out of range)");
2618
Jim Grosbach4b905842013-09-20 23:08:21 +00002619 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002620 continue;
2621 }
2622
2623 // Otherwise recognize individual escapes.
2624 switch (Str[i]) {
2625 default:
2626 // Just reject invalid escape sequences for now.
2627 return TokError("invalid escape sequence (unrecognized character)");
2628
2629 case 'b': Data += '\b'; break;
2630 case 'f': Data += '\f'; break;
2631 case 'n': Data += '\n'; break;
2632 case 'r': Data += '\r'; break;
2633 case 't': Data += '\t'; break;
2634 case '"': Data += '"'; break;
2635 case '\\': Data += '\\'; break;
2636 }
2637 }
2638
Nirav Davea645433c2016-07-18 15:24:03 +00002639 Lex();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002640 return false;
2641}
2642
Jim Grosbach4b905842013-09-20 23:08:21 +00002643/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002644/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002645bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002646 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002647 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002648
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002649 while (true) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002650 std::string Data;
Nirav Davea645433c2016-07-18 15:24:03 +00002651 if (check(getTok().isNot(AsmToken::String),
2652 "expected string in '" + Twine(IDVal) + "' directive") ||
2653 parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002654 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002655
Rafael Espindola64e1af82013-07-02 15:49:13 +00002656 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002657 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002658 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002659
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002660 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002661 break;
2662
Nirav Davea645433c2016-07-18 15:24:03 +00002663 if (parseToken(AsmToken::Comma,
2664 "unexpected token in '" + Twine(IDVal) + "' directive"))
2665 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002666 }
2667 }
2668
Sean Callanan686ed8d2010-01-19 20:22:31 +00002669 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002670 return false;
2671}
2672
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002673/// parseDirectiveReloc
2674/// ::= .reloc expression , identifier [ , expression ]
2675bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2676 const MCExpr *Offset;
2677 const MCExpr *Expr = nullptr;
2678
2679 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2680 if (parseExpression(Offset))
2681 return true;
2682
2683 // We can only deal with constant expressions at the moment.
2684 int64_t OffsetValue;
Nirav Davea645433c2016-07-18 15:24:03 +00002685 if (check(!Offset->evaluateAsAbsolute(OffsetValue), OffsetLoc,
2686 "expression is not a constant value") ||
2687 check(OffsetValue < 0, OffsetLoc, "expression is negative") ||
2688 parseToken(AsmToken::Comma, "expected comma") ||
2689 check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
2690 return true;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002691
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002692 SMLoc NameLoc = Lexer.getTok().getLoc();
2693 StringRef Name = Lexer.getTok().getIdentifier();
Nirav Davefd910412016-06-17 16:06:17 +00002694 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002695
2696 if (Lexer.is(AsmToken::Comma)) {
Nirav Davefd910412016-06-17 16:06:17 +00002697 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002698 SMLoc ExprLoc = Lexer.getLoc();
2699 if (parseExpression(Expr))
2700 return true;
2701
2702 MCValue Value;
2703 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2704 return Error(ExprLoc, "expression must be relocatable");
2705 }
2706
Nirav Davea645433c2016-07-18 15:24:03 +00002707 if (parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00002708 "unexpected token in .reloc directive"))
2709 return true;
2710
2711 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2712 return Error(NameLoc, "unknown relocation name");
2713
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002714 return false;
2715}
2716
Jim Grosbach4b905842013-09-20 23:08:21 +00002717/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002718/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002719bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002720 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002721 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002722
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002723 while (true) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002724 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002725 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002726 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002727 return true;
2728
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002729 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002730 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2731 assert(Size <= 8 && "Invalid size");
2732 uint64_t IntValue = MCE->getValue();
2733 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2734 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002735 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002736 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002737 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002738
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002739 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002740 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002741
Daniel Dunbara10e5192009-06-24 23:30:00 +00002742 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002743 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2744 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002745 }
2746 }
2747
Sean Callanan686ed8d2010-01-19 20:22:31 +00002748 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002749 return false;
2750}
2751
David Woodhoused6de0d92014-02-01 16:20:59 +00002752/// ParseDirectiveOctaValue
2753/// ::= .octa [ hexconstant (, hexconstant)* ]
2754bool AsmParser::parseDirectiveOctaValue() {
2755 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2756 checkForValidSection();
2757
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002758 while (true) {
Nirav Davea645433c2016-07-18 15:24:03 +00002759 if (getTok().is(AsmToken::Error))
David Woodhoused6de0d92014-02-01 16:20:59 +00002760 return true;
Nirav Davea645433c2016-07-18 15:24:03 +00002761 if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::BigNum))
David Woodhoused6de0d92014-02-01 16:20:59 +00002762 return TokError("unknown token in expression");
2763
2764 SMLoc ExprLoc = getLexer().getLoc();
2765 APInt IntValue = getTok().getAPIntVal();
2766 Lex();
2767
2768 uint64_t hi, lo;
2769 if (IntValue.isIntN(64)) {
2770 hi = 0;
2771 lo = IntValue.getZExtValue();
2772 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002773 // It might actually have more than 128 bits, but the top ones are zero.
2774 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002775 lo = IntValue.getLoBits(64).getZExtValue();
2776 } else
2777 return Error(ExprLoc, "literal value out of range for directive");
2778
2779 if (MAI.isLittleEndian()) {
2780 getStreamer().EmitIntValue(lo, 8);
2781 getStreamer().EmitIntValue(hi, 8);
2782 } else {
2783 getStreamer().EmitIntValue(hi, 8);
2784 getStreamer().EmitIntValue(lo, 8);
2785 }
2786
2787 if (getLexer().is(AsmToken::EndOfStatement))
2788 break;
2789
2790 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002791 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2792 return true;
David Woodhoused6de0d92014-02-01 16:20:59 +00002793 }
2794 }
2795
2796 Lex();
2797 return false;
2798}
2799
Jim Grosbach4b905842013-09-20 23:08:21 +00002800/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002801/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002802bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002803 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002804 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002805
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002806 while (true) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002807 // We don't truly support arithmetic on floating point expressions, so we
2808 // have to manually parse unary prefixes.
2809 bool IsNeg = false;
2810 if (getLexer().is(AsmToken::Minus)) {
Nirav Dave1180e6892016-06-02 17:15:05 +00002811 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002812 IsNeg = true;
2813 } else if (getLexer().is(AsmToken::Plus))
Nirav Dave1180e6892016-06-02 17:15:05 +00002814 Lexer.Lex();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002815
Nirav Dave1180e6892016-06-02 17:15:05 +00002816 if (Lexer.is(AsmToken::Error))
2817 return TokError(Lexer.getErr());
2818 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
2819 Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002820 return TokError("unexpected token in directive");
2821
2822 // Convert to an APFloat.
2823 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002824 StringRef IDVal = getTok().getString();
2825 if (getLexer().is(AsmToken::Identifier)) {
2826 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2827 Value = APFloat::getInf(Semantics);
2828 else if (!IDVal.compare_lower("nan"))
2829 Value = APFloat::getNaN(Semantics, false, ~0);
2830 else
2831 return TokError("invalid floating point literal");
2832 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002833 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002834 return TokError("invalid floating point literal");
2835 if (IsNeg)
2836 Value.changeSign();
2837
2838 // Consume the numeric token.
2839 Lex();
2840
2841 // Emit the value as an integer.
2842 APInt AsInt = Value.bitcastToAPInt();
2843 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002844 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002845
Nirav Dave1180e6892016-06-02 17:15:05 +00002846 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002847 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002848
Nirav Davea645433c2016-07-18 15:24:03 +00002849 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2850 return true;
Daniel Dunbar2af16532010-09-24 01:59:56 +00002851 }
2852 }
2853
2854 Lex();
2855 return false;
2856}
2857
Jim Grosbach4b905842013-09-20 23:08:21 +00002858/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002859/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002860bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002861 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002862
Petr Hosek67a94a72016-05-28 05:57:48 +00002863 SMLoc NumBytesLoc = Lexer.getLoc();
2864 const MCExpr *NumBytes;
2865 if (parseExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002866 return true;
2867
Rafael Espindolab91bac62010-10-05 19:42:57 +00002868 int64_t Val = 0;
2869 if (getLexer().is(AsmToken::Comma)) {
2870 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002871 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002872 return true;
2873 }
2874
Nirav Davea645433c2016-07-18 15:24:03 +00002875 if (parseToken(AsmToken::EndOfStatement,
2876 "unexpected token in '.zero' directive"))
2877 return true;
Petr Hosek67a94a72016-05-28 05:57:48 +00002878 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002879
2880 return false;
2881}
2882
Jim Grosbach4b905842013-09-20 23:08:21 +00002883/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002884/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002885bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002886 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002887
Petr Hosek67a94a72016-05-28 05:57:48 +00002888 SMLoc NumValuesLoc = Lexer.getLoc();
2889 const MCExpr *NumValues;
2890 if (parseExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002891 return true;
2892
Roman Divackye33098f2013-09-24 17:44:41 +00002893 int64_t FillSize = 1;
2894 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002895
David Majnemer522d3db2014-02-01 07:19:38 +00002896 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002897 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara10e5192009-06-24 23:30:00 +00002898
Nirav Davea645433c2016-07-18 15:24:03 +00002899 if (parseToken(AsmToken::Comma, "unexpected token in '.fill' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00002900 parseTokenLoc(SizeLoc) || parseAbsoluteExpression(FillSize))
Roman Divackye33098f2013-09-24 17:44:41 +00002901 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002902
Roman Divackye33098f2013-09-24 17:44:41 +00002903 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002904 if (parseToken(AsmToken::Comma,
2905 "unexpected token in '.fill' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00002906 parseTokenLoc(ExprLoc) || parseAbsoluteExpression(FillExpr) ||
Nirav Davea645433c2016-07-18 15:24:03 +00002907 parseToken(AsmToken::EndOfStatement,
2908 "unexpected token in '.fill' directive"))
Roman Divackye33098f2013-09-24 17:44:41 +00002909 return true;
Roman Divackye33098f2013-09-24 17:44:41 +00002910 }
2911 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002912
David Majnemer522d3db2014-02-01 07:19:38 +00002913 if (FillSize < 0) {
2914 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
Petr Hosek6abd38b2016-05-28 08:20:08 +00002915 return false;
David Majnemer522d3db2014-02-01 07:19:38 +00002916 }
2917 if (FillSize > 8) {
2918 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2919 FillSize = 8;
2920 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002921
David Majnemer522d3db2014-02-01 07:19:38 +00002922 if (!isUInt<32>(FillExpr) && FillSize > 4)
2923 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2924
Petr Hosek67a94a72016-05-28 05:57:48 +00002925 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002926
2927 return false;
2928}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002929
Jim Grosbach4b905842013-09-20 23:08:21 +00002930/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002931/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002933 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002934
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002935 const MCExpr *Offset;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002936 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002937 return true;
2938
2939 // Parse optional fill expression.
2940 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002941 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002942 if (parseToken(AsmToken::Comma, "unexpected token in '.org' directive") ||
2943 parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002944 return true;
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002945 }
2946
Nirav Davea645433c2016-07-18 15:24:03 +00002947 if (parseToken(AsmToken::EndOfStatement,
2948 "unexpected token in '.org' directive"))
2949 return true;
2950
Rafael Espindola7ae65d82015-11-04 23:59:18 +00002951 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002952 return false;
2953}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002954
Jim Grosbach4b905842013-09-20 23:08:21 +00002955/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002956/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002957bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002958 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002959
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002960 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002961 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002962 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002963 return true;
2964
2965 SMLoc MaxBytesLoc;
2966 bool HasFillExpr = false;
2967 int64_t FillExpr = 0;
2968 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002969 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002970 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2971 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002972
2973 // The fill expression can be omitted while specifying a maximum number of
2974 // alignment bytes, e.g:
2975 // .align 3,,4
Nirav Davea645433c2016-07-18 15:24:03 +00002976 if (getTok().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002977 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002978 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002979 return true;
2980 }
2981
Nirav Davea645433c2016-07-18 15:24:03 +00002982 if (getTok().isNot(AsmToken::EndOfStatement)) {
2983 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00002984 parseTokenLoc(MaxBytesLoc) || parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002985 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002986 }
2987 }
2988
Nirav Davea645433c2016-07-18 15:24:03 +00002989 if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
2990 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002991
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002992 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002993 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002994
2995 // Compute alignment in bytes.
2996 if (IsPow2) {
2997 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002998 if (Alignment >= 32) {
Eric Christopher04c7db32016-09-13 00:19:29 +00002999 Error(AlignmentLoc, "invalid alignment value");
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00003000 Alignment = 31;
3001 }
3002
Benjamin Kramer63951ad2009-09-06 09:35:10 +00003003 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00003004 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00003005 // Reject alignments that aren't either a power of two or zero,
3006 // for gas compatibility. Alignment of zero is silently rounded
3007 // up to one.
3008 if (Alignment == 0)
3009 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00003010 if (!isPowerOf2_64(Alignment))
Eric Christopher04c7db32016-09-13 00:19:29 +00003011 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003012 }
3013
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00003014 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003015 if (MaxBytesLoc.isValid()) {
3016 if (MaxBytesToFill < 1) {
Eric Christopher04c7db32016-09-13 00:19:29 +00003017 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00003018 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00003019 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003020 }
3021
3022 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00003023 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00003024 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003025 MaxBytesToFill = 0;
3026 }
3027 }
3028
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003029 // Check whether we should use optimal code alignment for this .align
3030 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00003031 const MCSection *Section = getStreamer().getCurrentSection().first;
3032 assert(Section && "must have section to emit alignment");
3033 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003034 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
3035 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003036 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003037 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00003038 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00003039 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
3040 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003041 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003042
Eric Christopher04c7db32016-09-13 00:19:29 +00003043 return false;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003044}
3045
Jim Grosbach4b905842013-09-20 23:08:21 +00003046/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00003047/// ::= .file [number] filename
3048/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00003049bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003050 // FIXME: I'm not sure what this is.
3051 int64_t FileNumber = -1;
3052 SMLoc FileNumberLoc = getLexer().getLoc();
3053 if (getLexer().is(AsmToken::Integer)) {
3054 FileNumber = getTok().getIntVal();
3055 Lex();
3056
3057 if (FileNumber < 1)
3058 return TokError("file number less than one");
3059 }
3060
Nirav Davea645433c2016-07-18 15:24:03 +00003061 std::string Path = getTok().getString();
Eli Bendersky17233942013-01-15 22:59:42 +00003062
3063 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003064 // Allow the strings to have escaped octal character sequence.
Nirav Davea645433c2016-07-18 15:24:03 +00003065 if (check(getTok().isNot(AsmToken::String),
3066 "unexpected token in '.file' directive") ||
3067 parseEscapedString(Path))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003068 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003069
3070 StringRef Directory;
3071 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003072 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00003073 if (getLexer().is(AsmToken::String)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003074 if (check(FileNumber == -1,
3075 "explicit path specified, but no file number") ||
3076 parseEscapedString(FilenameData))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003077 return true;
3078 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00003079 Directory = Path;
Eli Bendersky17233942013-01-15 22:59:42 +00003080 } else {
3081 Filename = Path;
3082 }
3083
Nirav Davea645433c2016-07-18 15:24:03 +00003084 if (parseToken(AsmToken::EndOfStatement,
3085 "unexpected token in '.file' directive"))
3086 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003087
3088 if (FileNumber == -1)
3089 getStreamer().EmitFileDirective(Filename);
3090 else {
David Blaikie22748082016-05-26 00:22:26 +00003091 // If there is -g option as well as debug info from directive file,
3092 // we turn off -g option, directly use the existing debug info instead.
David Blaikiedc3f01e2015-03-09 01:57:13 +00003093 if (getContext().getGenDwarfForAssembly())
David Blaikie22748082016-05-26 00:22:26 +00003094 getContext().setGenDwarfForAssembly(false);
3095 else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
David Blaikiec714ef42014-03-17 01:52:11 +00003096 0)
Eric Christopher04c7db32016-09-13 00:19:29 +00003097 Error(FileNumberLoc, "file number already allocated");
Eli Bendersky17233942013-01-15 22:59:42 +00003098 }
3099
3100 return false;
3101}
3102
Jim Grosbach4b905842013-09-20 23:08:21 +00003103/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003104/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003105bool AsmParser::parseDirectiveLine() {
Nirav Davea645433c2016-07-18 15:24:03 +00003106 int64_t LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003107 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003108 if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
3109 return true;
Jim Grosbach4b905842013-09-20 23:08:21 +00003110 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003111 // FIXME: Do something with the .line.
3112 }
Nirav Davea645433c2016-07-18 15:24:03 +00003113 if (parseToken(AsmToken::EndOfStatement,
3114 "unexpected token in '.line' directive"))
3115 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003116
3117 return false;
3118}
3119
Jim Grosbach4b905842013-09-20 23:08:21 +00003120/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003121/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3122/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3123/// The first number is a file number, must have been previously assigned with
3124/// a .file directive, the second number is the line number and optionally the
3125/// third number is a column position (zero if not specified). The remaining
3126/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003127bool AsmParser::parseDirectiveLoc() {
Nirav Davea645433c2016-07-18 15:24:03 +00003128 int64_t FileNumber = 0, LineNumber = 0;
3129 SMLoc Loc = getTok().getLoc();
3130 if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
3131 check(FileNumber < 1, Loc,
3132 "file number less than one in '.loc' directive") ||
3133 check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
3134 "unassigned file number in '.loc' directive"))
3135 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003136
Nirav Davea645433c2016-07-18 15:24:03 +00003137 // optional
Eli Bendersky17233942013-01-15 22:59:42 +00003138 if (getLexer().is(AsmToken::Integer)) {
3139 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003140 if (LineNumber < 0)
3141 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003142 Lex();
3143 }
3144
3145 int64_t ColumnPos = 0;
3146 if (getLexer().is(AsmToken::Integer)) {
3147 ColumnPos = getTok().getIntVal();
3148 if (ColumnPos < 0)
3149 return TokError("column position less than zero in '.loc' directive");
3150 Lex();
3151 }
3152
3153 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3154 unsigned Isa = 0;
3155 int64_t Discriminator = 0;
3156 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003157 while (true) {
Eli Bendersky17233942013-01-15 22:59:42 +00003158 if (getLexer().is(AsmToken::EndOfStatement))
3159 break;
3160
3161 StringRef Name;
3162 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003163 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003164 return TokError("unexpected token in '.loc' directive");
3165
3166 if (Name == "basic_block")
3167 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3168 else if (Name == "prologue_end")
3169 Flags |= DWARF2_FLAG_PROLOGUE_END;
3170 else if (Name == "epilogue_begin")
3171 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3172 else if (Name == "is_stmt") {
3173 Loc = getTok().getLoc();
3174 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003175 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003176 return true;
3177 // The expression must be the constant 0 or 1.
3178 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3179 int Value = MCE->getValue();
3180 if (Value == 0)
3181 Flags &= ~DWARF2_FLAG_IS_STMT;
3182 else if (Value == 1)
3183 Flags |= DWARF2_FLAG_IS_STMT;
3184 else
3185 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003186 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003187 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3188 }
Craig Topperf15655b2013-04-22 04:22:40 +00003189 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003190 Loc = getTok().getLoc();
3191 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003192 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003193 return true;
3194 // The expression must be a constant greater or equal to 0.
3195 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3196 int Value = MCE->getValue();
3197 if (Value < 0)
3198 return Error(Loc, "isa number less than zero");
3199 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003200 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003201 return Error(Loc, "isa number not a constant value");
3202 }
Craig Topperf15655b2013-04-22 04:22:40 +00003203 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003204 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003205 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003206 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003207 return Error(Loc, "unknown sub-directive in '.loc' directive");
3208 }
3209
3210 if (getLexer().is(AsmToken::EndOfStatement))
3211 break;
3212 }
3213 }
Nirav Davea645433c2016-07-18 15:24:03 +00003214 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003215
3216 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3217 Isa, Discriminator, StringRef());
3218
3219 return false;
3220}
3221
Jim Grosbach4b905842013-09-20 23:08:21 +00003222/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003223/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003224bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003225 return TokError("unsupported directive '.stabs'");
3226}
3227
Reid Kleckner2214ed82016-01-29 00:49:42 +00003228/// parseDirectiveCVFile
3229/// ::= .cv_file number filename
3230bool AsmParser::parseDirectiveCVFile() {
Nirav Davea645433c2016-07-18 15:24:03 +00003231 SMLoc FileNumberLoc = getTok().getLoc();
3232 int64_t FileNumber;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003233 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00003234
3235 if (parseIntToken(FileNumber,
3236 "expected file number in '.cv_file' directive") ||
3237 check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
3238 check(getTok().isNot(AsmToken::String),
3239 "unexpected token in '.cv_file' directive") ||
3240 // Usually directory and filename are together, otherwise just
3241 // directory. Allow the strings to have escaped octal character sequence.
3242 parseEscapedString(Filename) ||
3243 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00003244 "unexpected token in '.cv_file' directive"))
Reid Klecknera5b1eef2016-08-26 17:58:37 +00003245 return true;
Nirav Dave1ab71992016-07-18 19:35:21 +00003246
Reid Klecknera5b1eef2016-08-26 17:58:37 +00003247 if (!getStreamer().EmitCVFileDirective(FileNumber, Filename))
Nirav Dave1ab71992016-07-18 19:35:21 +00003248 Error(FileNumberLoc, "file number already allocated");
Reid Kleckner2214ed82016-01-29 00:49:42 +00003249
3250 return false;
3251}
3252
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003253bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
3254 StringRef DirectiveName) {
3255 SMLoc Loc;
3256 return parseTokenLoc(Loc) ||
3257 parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
3258 "' directive") ||
3259 check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
3260 "expected function id within range [0, UINT_MAX)");
3261}
3262
3263bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
3264 SMLoc Loc;
3265 return parseTokenLoc(Loc) ||
3266 parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
3267 "' directive") ||
3268 check(FileNumber < 1, Loc, "file number less than one in '" +
3269 DirectiveName + "' directive") ||
3270 check(!getCVContext().isValidFileNumber(FileNumber), Loc,
3271 "unassigned file number in '" + DirectiveName + "' directive");
3272}
3273
3274/// parseDirectiveCVFuncId
3275/// ::= .cv_func_id FunctionId
3276///
3277/// Introduces a function ID that can be used with .cv_loc.
3278bool AsmParser::parseDirectiveCVFuncId() {
3279 SMLoc FunctionIdLoc = getTok().getLoc();
3280 int64_t FunctionId;
3281
3282 if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
3283 parseToken(AsmToken::EndOfStatement,
3284 "unexpected token in '.cv_func_id' directive"))
3285 return true;
3286
3287 if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
3288 Error(FunctionIdLoc, "function id already allocated");
3289
3290 return false;
3291}
3292
3293/// parseDirectiveCVInlineSiteId
3294/// ::= .cv_inline_site_id FunctionId
3295/// "within" IAFunc
3296/// "inlined_at" IAFile IALine [IACol]
3297///
3298/// Introduces a function ID that can be used with .cv_loc. Includes "inlined
3299/// at" source location information for use in the line table of the caller,
3300/// whether the caller is a real function or another inlined call site.
3301bool AsmParser::parseDirectiveCVInlineSiteId() {
3302 SMLoc FunctionIdLoc = getTok().getLoc();
3303 int64_t FunctionId;
3304 int64_t IAFunc;
3305 int64_t IAFile;
3306 int64_t IALine;
3307 int64_t IACol = 0;
3308
3309 // FunctionId
3310 if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
3311 return true;
3312
3313 // "within"
3314 if (check((getLexer().isNot(AsmToken::Identifier) ||
3315 getTok().getIdentifier() != "within"),
3316 "expected 'within' identifier in '.cv_inline_site_id' directive"))
3317 return true;
3318 Lex();
3319
3320 // IAFunc
3321 if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
3322 return true;
3323
3324 // "inlined_at"
3325 if (check((getLexer().isNot(AsmToken::Identifier) ||
3326 getTok().getIdentifier() != "inlined_at"),
3327 "expected 'inlined_at' identifier in '.cv_inline_site_id' "
3328 "directive") )
3329 return true;
3330 Lex();
3331
3332 // IAFile IALine
3333 if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
3334 parseIntToken(IALine, "expected line number after 'inlined_at'"))
3335 return true;
3336
3337 // [IACol]
3338 if (getLexer().is(AsmToken::Integer)) {
3339 IACol = getTok().getIntVal();
3340 Lex();
3341 }
3342
3343 if (parseToken(AsmToken::EndOfStatement,
3344 "unexpected token in '.cv_inline_site_id' directive"))
3345 return true;
3346
3347 if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
3348 IALine, IACol, FunctionIdLoc))
Eric Christopher04c7db32016-09-13 00:19:29 +00003349 Error(FunctionIdLoc, "function id already allocated");
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003350
3351 return false;
3352}
3353
Reid Kleckner2214ed82016-01-29 00:49:42 +00003354/// parseDirectiveCVLoc
3355/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3356/// [is_stmt VALUE]
3357/// The first number is a file number, must have been previously assigned with
3358/// a .file directive, the second number is the line number and optionally the
3359/// third number is a column position (zero if not specified). The remaining
3360/// optional items are .loc sub-directives.
3361bool AsmParser::parseDirectiveCVLoc() {
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003362 SMLoc DirectiveLoc = getTok().getLoc();
Nirav Davea645433c2016-07-18 15:24:03 +00003363 SMLoc Loc;
3364 int64_t FunctionId, FileNumber;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003365 if (parseCVFunctionId(FunctionId, ".cv_loc") ||
3366 parseCVFileId(FileNumber, ".cv_loc"))
Nirav Davea645433c2016-07-18 15:24:03 +00003367 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003368
3369 int64_t LineNumber = 0;
3370 if (getLexer().is(AsmToken::Integer)) {
3371 LineNumber = getTok().getIntVal();
3372 if (LineNumber < 0)
3373 return TokError("line number less than zero in '.cv_loc' directive");
3374 Lex();
3375 }
3376
3377 int64_t ColumnPos = 0;
3378 if (getLexer().is(AsmToken::Integer)) {
3379 ColumnPos = getTok().getIntVal();
3380 if (ColumnPos < 0)
3381 return TokError("column position less than zero in '.cv_loc' directive");
3382 Lex();
3383 }
3384
3385 bool PrologueEnd = false;
3386 uint64_t IsStmt = 0;
3387 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3388 StringRef Name;
3389 SMLoc Loc = getTok().getLoc();
3390 if (parseIdentifier(Name))
3391 return TokError("unexpected token in '.cv_loc' directive");
3392
3393 if (Name == "prologue_end")
3394 PrologueEnd = true;
3395 else if (Name == "is_stmt") {
3396 Loc = getTok().getLoc();
3397 const MCExpr *Value;
3398 if (parseExpression(Value))
3399 return true;
3400 // The expression must be the constant 0 or 1.
3401 IsStmt = ~0ULL;
3402 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3403 IsStmt = MCE->getValue();
3404
3405 if (IsStmt > 1)
3406 return Error(Loc, "is_stmt value not 0 or 1");
3407 } else {
3408 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3409 }
3410 }
Nirav Davea645433c2016-07-18 15:24:03 +00003411 Lex();
Reid Kleckner2214ed82016-01-29 00:49:42 +00003412
3413 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003414 ColumnPos, PrologueEnd, IsStmt, StringRef(),
3415 DirectiveLoc);
Reid Kleckner2214ed82016-01-29 00:49:42 +00003416 return false;
3417}
3418
3419/// parseDirectiveCVLinetable
3420/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3421bool AsmParser::parseDirectiveCVLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003422 int64_t FunctionId;
3423 StringRef FnStartName, FnEndName;
3424 SMLoc Loc = getTok().getLoc();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003425 if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
Nirav Davea645433c2016-07-18 15:24:03 +00003426 parseToken(AsmToken::Comma,
3427 "unexpected token in '.cv_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003428 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3429 "expected identifier in directive") ||
Nirav Davea645433c2016-07-18 15:24:03 +00003430 parseToken(AsmToken::Comma,
3431 "unexpected token in '.cv_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003432 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3433 "expected identifier in directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00003434 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003435
3436 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3437 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3438
3439 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3440 return false;
3441}
3442
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003443/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003444/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003445bool AsmParser::parseDirectiveCVInlineLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003446 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3447 StringRef FnStartName, FnEndName;
3448 SMLoc Loc = getTok().getLoc();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003449 if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003450 parseTokenLoc(Loc) ||
Nirav Davea645433c2016-07-18 15:24:03 +00003451 parseIntToken(
3452 SourceFileId,
3453 "expected SourceField in '.cv_inline_linetable' directive") ||
3454 check(SourceFileId <= 0, Loc,
3455 "File id less than zero in '.cv_inline_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003456 parseTokenLoc(Loc) ||
Nirav Davea645433c2016-07-18 15:24:03 +00003457 parseIntToken(
3458 SourceLineNum,
3459 "expected SourceLineNum in '.cv_inline_linetable' directive") ||
3460 check(SourceLineNum < 0, Loc,
3461 "Line number less than zero in '.cv_inline_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003462 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3463 "expected identifier in directive") ||
3464 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3465 "expected identifier in directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00003466 return true;
David Majnemerc9911f22016-02-02 19:22:34 +00003467
Nirav Davea645433c2016-07-18 15:24:03 +00003468 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
3469 return true;
3470
3471 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3472 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003473 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3474 SourceLineNum, FnStartSym,
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003475 FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003476 return false;
3477}
3478
David Majnemer408b5e62016-02-05 01:55:49 +00003479/// parseDirectiveCVDefRange
3480/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3481bool AsmParser::parseDirectiveCVDefRange() {
3482 SMLoc Loc;
3483 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3484 while (getLexer().is(AsmToken::Identifier)) {
3485 Loc = getLexer().getLoc();
3486 StringRef GapStartName;
3487 if (parseIdentifier(GapStartName))
3488 return Error(Loc, "expected identifier in directive");
3489 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3490
3491 Loc = getLexer().getLoc();
3492 StringRef GapEndName;
3493 if (parseIdentifier(GapEndName))
3494 return Error(Loc, "expected identifier in directive");
3495 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3496
3497 Ranges.push_back({GapStartSym, GapEndSym});
3498 }
3499
David Majnemer408b5e62016-02-05 01:55:49 +00003500 std::string FixedSizePortion;
Nirav Davea645433c2016-07-18 15:24:03 +00003501 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
3502 parseEscapedString(FixedSizePortion))
David Majnemer408b5e62016-02-05 01:55:49 +00003503 return true;
David Majnemer408b5e62016-02-05 01:55:49 +00003504
3505 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3506 return false;
3507}
3508
Reid Kleckner2214ed82016-01-29 00:49:42 +00003509/// parseDirectiveCVStringTable
3510/// ::= .cv_stringtable
3511bool AsmParser::parseDirectiveCVStringTable() {
3512 getStreamer().EmitCVStringTableDirective();
3513 return false;
3514}
3515
3516/// parseDirectiveCVFileChecksums
3517/// ::= .cv_filechecksums
3518bool AsmParser::parseDirectiveCVFileChecksums() {
3519 getStreamer().EmitCVFileChecksumsDirective();
3520 return false;
3521}
3522
Jim Grosbach4b905842013-09-20 23:08:21 +00003523/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003524/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003525bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003526 StringRef Name;
3527 bool EH = false;
3528 bool Debug = false;
3529
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003530 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003531 return TokError("Expected an identifier");
3532
3533 if (Name == ".eh_frame")
3534 EH = true;
3535 else if (Name == ".debug_frame")
3536 Debug = true;
3537
3538 if (getLexer().is(AsmToken::Comma)) {
3539 Lex();
3540
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003541 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003542 return TokError("Expected an identifier");
3543
3544 if (Name == ".eh_frame")
3545 EH = true;
3546 else if (Name == ".debug_frame")
3547 Debug = true;
3548 }
3549
3550 getStreamer().EmitCFISections(EH, Debug);
3551 return false;
3552}
3553
Jim Grosbach4b905842013-09-20 23:08:21 +00003554/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003555/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003556bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003557 StringRef Simple;
3558 if (getLexer().isNot(AsmToken::EndOfStatement))
3559 if (parseIdentifier(Simple) || Simple != "simple")
3560 return TokError("unexpected token in .cfi_startproc directive");
3561
Nirav Davea645433c2016-07-18 15:24:03 +00003562 if (parseToken(AsmToken::EndOfStatement, "Expected end of statement"))
3563 return true;
3564
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003565 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003566 return false;
3567}
3568
Jim Grosbach4b905842013-09-20 23:08:21 +00003569/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003570/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003571bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003572 getStreamer().EmitCFIEndProc();
3573 return false;
3574}
3575
Jim Grosbach4b905842013-09-20 23:08:21 +00003576/// \brief parse register name or number.
3577bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003578 SMLoc DirectiveLoc) {
3579 unsigned RegNo;
3580
3581 if (getLexer().isNot(AsmToken::Integer)) {
3582 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3583 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003584 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003585 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003586 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003587
3588 return false;
3589}
3590
Jim Grosbach4b905842013-09-20 23:08:21 +00003591/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003592/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003593bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003594 int64_t Register = 0, Offset = 0;
3595 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3596 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3597 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003598 return true;
3599
3600 getStreamer().EmitCFIDefCfa(Register, Offset);
3601 return false;
3602}
3603
Jim Grosbach4b905842013-09-20 23:08:21 +00003604/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003605/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003606bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003607 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003608 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003609 return true;
3610
3611 getStreamer().EmitCFIDefCfaOffset(Offset);
3612 return false;
3613}
3614
Jim Grosbach4b905842013-09-20 23:08:21 +00003615/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003616/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003617bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003618 int64_t Register1 = 0, Register2 = 0;
3619 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
3620 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3621 parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003622 return true;
3623
3624 getStreamer().EmitCFIRegister(Register1, Register2);
3625 return false;
3626}
3627
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003628/// parseDirectiveCFIWindowSave
3629/// ::= .cfi_window_save
3630bool AsmParser::parseDirectiveCFIWindowSave() {
3631 getStreamer().EmitCFIWindowSave();
3632 return false;
3633}
3634
Jim Grosbach4b905842013-09-20 23:08:21 +00003635/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003636/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003637bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003638 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003639 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003640 return true;
3641
3642 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3643 return false;
3644}
3645
Jim Grosbach4b905842013-09-20 23:08:21 +00003646/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003647/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003648bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003649 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003650 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003651 return true;
3652
3653 getStreamer().EmitCFIDefCfaRegister(Register);
3654 return false;
3655}
3656
Jim Grosbach4b905842013-09-20 23:08:21 +00003657/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003658/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003659bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003660 int64_t Register = 0;
3661 int64_t Offset = 0;
3662
Nirav Davea645433c2016-07-18 15:24:03 +00003663 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3664 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3665 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003666 return true;
3667
3668 getStreamer().EmitCFIOffset(Register, Offset);
3669 return false;
3670}
3671
Jim Grosbach4b905842013-09-20 23:08:21 +00003672/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003673/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003674bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003675 int64_t Register = 0, Offset = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003676
Nirav Davea645433c2016-07-18 15:24:03 +00003677 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3678 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3679 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003680 return true;
3681
3682 getStreamer().EmitCFIRelOffset(Register, Offset);
3683 return false;
3684}
3685
3686static bool isValidEncoding(int64_t Encoding) {
3687 if (Encoding & ~0xff)
3688 return false;
3689
3690 if (Encoding == dwarf::DW_EH_PE_omit)
3691 return true;
3692
3693 const unsigned Format = Encoding & 0xf;
3694 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3695 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3696 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3697 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3698 return false;
3699
3700 const unsigned Application = Encoding & 0x70;
3701 if (Application != dwarf::DW_EH_PE_absptr &&
3702 Application != dwarf::DW_EH_PE_pcrel)
3703 return false;
3704
3705 return true;
3706}
3707
Jim Grosbach4b905842013-09-20 23:08:21 +00003708/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003709/// IsPersonality true for cfi_personality, false for cfi_lsda
3710/// ::= .cfi_personality encoding, [symbol_name]
3711/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003712bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003713 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003714 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003715 return true;
3716 if (Encoding == dwarf::DW_EH_PE_omit)
3717 return false;
3718
Eli Bendersky17233942013-01-15 22:59:42 +00003719 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00003720 if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
3721 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3722 check(parseIdentifier(Name), "expected identifier in directive"))
3723 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003724
Jim Grosbach6f482002015-05-18 18:43:14 +00003725 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003726
3727 if (IsPersonality)
3728 getStreamer().EmitCFIPersonality(Sym, Encoding);
3729 else
3730 getStreamer().EmitCFILsda(Sym, Encoding);
3731 return false;
3732}
3733
Jim Grosbach4b905842013-09-20 23:08:21 +00003734/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003735/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003736bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003737 getStreamer().EmitCFIRememberState();
3738 return false;
3739}
3740
Jim Grosbach4b905842013-09-20 23:08:21 +00003741/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003742/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003743bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003744 getStreamer().EmitCFIRestoreState();
3745 return false;
3746}
3747
Jim Grosbach4b905842013-09-20 23:08:21 +00003748/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003749/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003750bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003751 int64_t Register = 0;
3752
Jim Grosbach4b905842013-09-20 23:08:21 +00003753 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003754 return true;
3755
3756 getStreamer().EmitCFISameValue(Register);
3757 return false;
3758}
3759
Jim Grosbach4b905842013-09-20 23:08:21 +00003760/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003761/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003762bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003763 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003764 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003765 return true;
3766
3767 getStreamer().EmitCFIRestore(Register);
3768 return false;
3769}
3770
Jim Grosbach4b905842013-09-20 23:08:21 +00003771/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003772/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003773bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003774 std::string Values;
3775 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003776 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003777 return true;
3778
3779 Values.push_back((uint8_t)CurrValue);
3780
3781 while (getLexer().is(AsmToken::Comma)) {
3782 Lex();
3783
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003784 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003785 return true;
3786
3787 Values.push_back((uint8_t)CurrValue);
3788 }
3789
3790 getStreamer().EmitCFIEscape(Values);
3791 return false;
3792}
3793
Jim Grosbach4b905842013-09-20 23:08:21 +00003794/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003795/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003796bool AsmParser::parseDirectiveCFISignalFrame() {
Nirav Davea645433c2016-07-18 15:24:03 +00003797 if (parseToken(AsmToken::EndOfStatement,
3798 "unexpected token in '.cfi_signal_frame'"))
3799 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003800
3801 getStreamer().EmitCFISignalFrame();
3802 return false;
3803}
3804
Jim Grosbach4b905842013-09-20 23:08:21 +00003805/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003806/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003807bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003808 int64_t Register = 0;
3809
Jim Grosbach4b905842013-09-20 23:08:21 +00003810 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003811 return true;
3812
3813 getStreamer().EmitCFIUndefined(Register);
3814 return false;
3815}
3816
Jim Grosbach4b905842013-09-20 23:08:21 +00003817/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003818/// ::= .macros_on
3819/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003820bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00003821 if (parseToken(AsmToken::EndOfStatement,
3822 "unexpected token in '" + Directive + "' directive"))
3823 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003824
Jim Grosbach4b905842013-09-20 23:08:21 +00003825 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003826 return false;
3827}
3828
Jim Grosbach4b905842013-09-20 23:08:21 +00003829/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003830/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003831bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003832 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003833 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003834 return TokError("expected identifier in '.macro' directive");
3835
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003836 if (getLexer().is(AsmToken::Comma))
3837 Lex();
3838
Eli Bendersky17233942013-01-15 22:59:42 +00003839 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003840 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003841
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003842 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003843 return Error(Lexer.getLoc(),
3844 "Vararg parameter '" + Parameters.back().Name +
3845 "' should be last one in the list of parameters.");
3846
David Majnemer91fc4c22014-01-29 18:57:46 +00003847 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003848 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003849 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003850
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003851 if (Lexer.is(AsmToken::Colon)) {
3852 Lex(); // consume ':'
3853
3854 SMLoc QualLoc;
3855 StringRef Qualifier;
3856
3857 QualLoc = Lexer.getLoc();
3858 if (parseIdentifier(Qualifier))
3859 return Error(QualLoc, "missing parameter qualifier for "
3860 "'" + Parameter.Name + "' in macro '" + Name + "'");
3861
3862 if (Qualifier == "req")
3863 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003864 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003865 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003866 else
3867 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3868 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3869 }
3870
David Majnemer91fc4c22014-01-29 18:57:46 +00003871 if (getLexer().is(AsmToken::Equal)) {
3872 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003873
3874 SMLoc ParamLoc;
3875
3876 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003877 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003878 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003879
3880 if (Parameter.Required)
3881 Warning(ParamLoc, "pointless default value for required parameter "
3882 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003883 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003884
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003885 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003886
3887 if (getLexer().is(AsmToken::Comma))
3888 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003889 }
3890
Nirav Dave1180e6892016-06-02 17:15:05 +00003891 // Eat just the end of statement.
3892 Lexer.Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003893
Nirav Dave1180e6892016-06-02 17:15:05 +00003894 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
Eli Bendersky17233942013-01-15 22:59:42 +00003895 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003896 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003897 // Lex the macro definition.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003898 while (true) {
Nirav Dave1180e6892016-06-02 17:15:05 +00003899 // Ignore Lexing errors in macros.
3900 while (Lexer.is(AsmToken::Error)) {
3901 Lexer.Lex();
3902 }
3903
Eli Bendersky17233942013-01-15 22:59:42 +00003904 // Check whether we have reached the end of the file.
3905 if (getLexer().is(AsmToken::Eof))
3906 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3907
3908 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003909 if (getLexer().is(AsmToken::Identifier)) {
3910 if (getTok().getIdentifier() == ".endm" ||
3911 getTok().getIdentifier() == ".endmacro") {
3912 if (MacroDepth == 0) { // Outermost macro.
3913 EndToken = getTok();
Nirav Dave1180e6892016-06-02 17:15:05 +00003914 Lexer.Lex();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003915 if (getLexer().isNot(AsmToken::EndOfStatement))
3916 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3917 "' directive");
3918 break;
3919 } else {
3920 // Otherwise we just found the end of an inner macro.
3921 --MacroDepth;
3922 }
3923 } else if (getTok().getIdentifier() == ".macro") {
3924 // We allow nested macros. Those aren't instantiated until the outermost
3925 // macro is expanded so just ignore them for now.
3926 ++MacroDepth;
3927 }
Eli Bendersky17233942013-01-15 22:59:42 +00003928 }
3929
3930 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003931 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003932 }
3933
Jim Grosbach4b905842013-09-20 23:08:21 +00003934 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003935 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3936 }
3937
3938 const char *BodyStart = StartToken.getLoc().getPointer();
3939 const char *BodyEnd = EndToken.getLoc().getPointer();
3940 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003941 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003942 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00003943 return false;
3944}
3945
Jim Grosbach4b905842013-09-20 23:08:21 +00003946/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003947///
3948/// With the support added for named parameters there may be code out there that
3949/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003950/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003951/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003952/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003953/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3954/// warning that the positional parameter found in body which have no effect.
3955/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003956/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003957/// intended or change the macro to use the named parameters. It is possible
3958/// this warning will trigger when the none of the named parameters are used
3959/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003960void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003961 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003962 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003963 // If this macro is not defined with named parameters the warning we are
3964 // checking for here doesn't apply.
3965 unsigned NParameters = Parameters.size();
3966 if (NParameters == 0)
3967 return;
3968
3969 bool NamedParametersFound = false;
3970 bool PositionalParametersFound = false;
3971
3972 // Look at the body of the macro for use of both the named parameters and what
3973 // are likely to be positional parameters. This is what expandMacro() is
3974 // doing when it finds the parameters in the body.
3975 while (!Body.empty()) {
3976 // Scan for the next possible parameter.
3977 std::size_t End = Body.size(), Pos = 0;
3978 for (; Pos != End; ++Pos) {
3979 // Check for a substitution or escape.
3980 // This macro is defined with parameters, look for \foo, \bar, etc.
3981 if (Body[Pos] == '\\' && Pos + 1 != End)
3982 break;
3983
3984 // This macro should have parameters, but look for $0, $1, ..., $n too.
3985 if (Body[Pos] != '$' || Pos + 1 == End)
3986 continue;
3987 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003988 if (Next == '$' || Next == 'n' ||
3989 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003990 break;
3991 }
3992
3993 // Check if we reached the end.
3994 if (Pos == End)
3995 break;
3996
3997 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003998 switch (Body[Pos + 1]) {
3999 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00004000 case '$':
4001 break;
4002
Jim Grosbach4b905842013-09-20 23:08:21 +00004003 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00004004 case 'n':
4005 PositionalParametersFound = true;
4006 break;
4007
Jim Grosbach4b905842013-09-20 23:08:21 +00004008 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00004009 default: {
4010 PositionalParametersFound = true;
4011 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00004012 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00004013 }
4014 Pos += 2;
4015 } else {
4016 unsigned I = Pos + 1;
4017 while (isIdentifierChar(Body[I]) && I + 1 != End)
4018 ++I;
4019
Jim Grosbach4b905842013-09-20 23:08:21 +00004020 const char *Begin = Body.data() + Pos + 1;
4021 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00004022 unsigned Index = 0;
4023 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004024 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00004025 break;
4026
4027 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004028 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
4029 Pos += 3;
4030 else {
4031 Pos = I;
4032 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00004033 } else {
4034 NamedParametersFound = true;
4035 Pos += 1 + Argument.size();
4036 }
4037 }
4038 // Update the scan point.
4039 Body = Body.substr(Pos);
4040 }
4041
4042 if (!NamedParametersFound && PositionalParametersFound)
4043 Warning(DirectiveLoc, "macro defined with named parameters which are not "
4044 "used in macro body, possible positional parameter "
4045 "found in body which will have no effect");
4046}
4047
Nico Weber155dccd12014-07-24 17:08:39 +00004048/// parseDirectiveExitMacro
4049/// ::= .exitm
4050bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00004051 if (parseToken(AsmToken::EndOfStatement,
4052 "unexpected token in '" + Directive + "' directive"))
4053 return true;
Nico Weber155dccd12014-07-24 17:08:39 +00004054
4055 if (!isInsideMacroInstantiation())
4056 return TokError("unexpected '" + Directive + "' in file, "
4057 "no current macro definition");
4058
4059 // Exit all conditionals that are active in the current macro.
4060 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
4061 TheCondState = TheCondStack.back();
4062 TheCondStack.pop_back();
4063 }
4064
4065 handleMacroExit();
4066 return false;
4067}
4068
Jim Grosbach4b905842013-09-20 23:08:21 +00004069/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00004070/// ::= .endm
4071/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00004072bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00004073 if (getLexer().isNot(AsmToken::EndOfStatement))
4074 return TokError("unexpected token in '" + Directive + "' directive");
4075
4076 // If we are inside a macro instantiation, terminate the current
4077 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00004078 if (isInsideMacroInstantiation()) {
4079 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00004080 return false;
4081 }
4082
4083 // Otherwise, this .endmacro is a stray entry in the file; well formed
4084 // .endmacro directives are handled during the macro definition parsing.
4085 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00004086 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00004087}
4088
Jim Grosbach4b905842013-09-20 23:08:21 +00004089/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00004090/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00004091bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00004092 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00004093 SMLoc Loc;
Nirav Daved8858ca2016-08-30 14:15:43 +00004094 if (parseTokenLoc(Loc) ||
4095 check(parseIdentifier(Name), Loc,
4096 "expected identifier in '.purgem' directive") ||
Nirav Davea645433c2016-07-18 15:24:03 +00004097 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00004098 "unexpected token in '.purgem' directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00004099 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004100
Nirav Dave1ab71992016-07-18 19:35:21 +00004101 if (!lookupMacro(Name))
4102 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
4103
Jim Grosbach4b905842013-09-20 23:08:21 +00004104 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00004105 return false;
4106}
Eli Benderskyf483ff92012-12-20 19:05:53 +00004107
Jim Grosbach4b905842013-09-20 23:08:21 +00004108/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00004109/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004110bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004111 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004112
4113 // Expect a single argument: an expression that evaluates to a constant
4114 // in the inclusive range 0-30.
4115 SMLoc ExprLoc = getLexer().getLoc();
4116 int64_t AlignSizePow2;
Nirav Davea645433c2016-07-18 15:24:03 +00004117 if (parseAbsoluteExpression(AlignSizePow2) ||
4118 parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
4119 "in '.bundle_align_mode' "
4120 "directive") ||
4121 check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
4122 "invalid bundle alignment size (expected between 0 and 30)"))
Eli Benderskyf483ff92012-12-20 19:05:53 +00004123 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004124
4125 // Because of AlignSizePow2's verified range we can safely truncate it to
4126 // unsigned.
4127 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4128 return false;
4129}
4130
Jim Grosbach4b905842013-09-20 23:08:21 +00004131/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00004132/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00004133bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004134 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00004135 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004136
Eli Bendersky802b6282013-01-07 21:51:08 +00004137 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4138 StringRef Option;
4139 SMLoc Loc = getTok().getLoc();
4140 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004141 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004142
Nirav Davea645433c2016-07-18 15:24:03 +00004143 if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
4144 check(Option != "align_to_end", Loc, kInvalidOptionError) ||
4145 check(getTok().isNot(AsmToken::EndOfStatement), Loc,
4146 "unexpected token after '.bundle_lock' directive option"))
4147 return true;
Eli Bendersky802b6282013-01-07 21:51:08 +00004148 AlignToEnd = true;
4149 }
4150
Eli Benderskyf483ff92012-12-20 19:05:53 +00004151 Lex();
4152
Eli Bendersky802b6282013-01-07 21:51:08 +00004153 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004154 return false;
4155}
4156
Jim Grosbach4b905842013-09-20 23:08:21 +00004157/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004158/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004159bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004160 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00004161
Nirav Davea645433c2016-07-18 15:24:03 +00004162 if (parseToken(AsmToken::EndOfStatement,
4163 "unexpected token in '.bundle_unlock' directive"))
4164 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004165
4166 getStreamer().EmitBundleUnlock();
4167 return false;
4168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004171/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004172bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004173 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004174
Petr Hosek67a94a72016-05-28 05:57:48 +00004175 SMLoc NumBytesLoc = Lexer.getLoc();
4176 const MCExpr *NumBytes;
4177 if (parseExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004178 return true;
4179
4180 int64_t FillExpr = 0;
4181 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eli Bendersky17233942013-01-15 22:59:42 +00004182
Nirav Davea645433c2016-07-18 15:24:03 +00004183 if (parseToken(AsmToken::Comma,
4184 "unexpected token in '" + Twine(IDVal) + "' directive") ||
4185 parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004186 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004187 }
4188
Nirav Davea645433c2016-07-18 15:24:03 +00004189 if (parseToken(AsmToken::EndOfStatement,
4190 "unexpected token in '" + Twine(IDVal) + "' directive"))
4191 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004192
Eli Bendersky17233942013-01-15 22:59:42 +00004193 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Petr Hosek67a94a72016-05-28 05:57:48 +00004194 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
Eli Bendersky17233942013-01-15 22:59:42 +00004195
4196 return false;
4197}
4198
Jim Grosbach4b905842013-09-20 23:08:21 +00004199/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004200/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004201bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004202 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00004203 const MCExpr *Value;
4204
Eugene Zelenko33d7b762016-08-23 17:14:32 +00004205 while (true) {
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004206 if (parseExpression(Value))
4207 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004208
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004209 if (Signed)
4210 getStreamer().EmitSLEB128Value(Value);
4211 else
4212 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004213
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004214 if (getLexer().is(AsmToken::EndOfStatement))
4215 break;
4216
Nirav Davea645433c2016-07-18 15:24:03 +00004217 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4218 return true;
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004219 }
Nirav Davea645433c2016-07-18 15:24:03 +00004220 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00004221
4222 return false;
4223}
4224
Jim Grosbach4b905842013-09-20 23:08:21 +00004225/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004226/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004227bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004228 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00004229 while (true) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004230 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004231 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004232
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004233 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004234 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004235
Jim Grosbach6f482002015-05-18 18:43:14 +00004236 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004237
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004238 // Assembler local symbols don't make any sense here. Complain loudly.
4239 if (Sym->isTemporary())
4240 return Error(Loc, "non-local symbol required in directive");
4241
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004242 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4243 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004244
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004245 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004246 break;
4247
Nirav Davea645433c2016-07-18 15:24:03 +00004248 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4249 return true;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004250 }
4251 }
4252
Sean Callanan686ed8d2010-01-19 20:22:31 +00004253 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004254 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004255}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004256
Jim Grosbach4b905842013-09-20 23:08:21 +00004257/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004258/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004259bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004260 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00004261
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004262 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004263 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004264 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004265 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004266
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004267 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004268 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004269
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004270 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004271 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004272 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004273
4274 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004275 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004276 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004277 return true;
4278
4279 int64_t Pow2Alignment = 0;
4280 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004281 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004282 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004283 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004284 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004285 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004286
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004287 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4288 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004289 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4290
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004291 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004292 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4293 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004294 if (!isPowerOf2_64(Pow2Alignment))
4295 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4296 Pow2Alignment = Log2_64(Pow2Alignment);
4297 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004298 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004299
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004300 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004301 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004302
Sean Callanan686ed8d2010-01-19 20:22:31 +00004303 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004304
Chris Lattner28ad7542009-07-09 17:25:12 +00004305 // NOTE: a size of zero for a .comm should create a undefined symbol
4306 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004307 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004308 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004309 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004310
Eric Christopherbc818852010-05-14 01:38:54 +00004311 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004312 // may internally end up wanting an alignment in bytes.
4313 // FIXME: Diagnose overflow.
4314 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004315 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004316 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004317
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004318 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004319 return Error(IDLoc, "invalid symbol redefinition");
4320
Chris Lattner28ad7542009-07-09 17:25:12 +00004321 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004322 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004323 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004324 return false;
4325 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004326
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004327 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004328 return false;
4329}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004330
Jim Grosbach4b905842013-09-20 23:08:21 +00004331/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004332/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004333bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004334 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004335 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004336
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004337 StringRef Str = parseStringToEndOfStatement();
Nirav Davea645433c2016-07-18 15:24:03 +00004338 if (parseToken(AsmToken::EndOfStatement,
4339 "unexpected token in '.abort' directive"))
4340 return true;
Kevin Enderby56523ce2009-07-13 23:15:14 +00004341
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004342 if (Str.empty())
Eric Christopher04c7db32016-09-13 00:19:29 +00004343 Error(Loc, ".abort detected. Assembly stopping.");
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004344 else
Eric Christopher04c7db32016-09-13 00:19:29 +00004345 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004346 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004347
4348 return false;
4349}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004350
Jim Grosbach4b905842013-09-20 23:08:21 +00004351/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004352/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004353bool AsmParser::parseDirectiveInclude() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004354 // Allow the strings to have escaped octal character sequence.
4355 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004356 SMLoc IncludeLoc = getTok().getLoc();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004357
Nirav Davea645433c2016-07-18 15:24:03 +00004358 if (check(getTok().isNot(AsmToken::String),
4359 "expected string in '.include' directive") ||
4360 parseEscapedString(Filename) ||
4361 check(getTok().isNot(AsmToken::EndOfStatement),
4362 "unexpected token in '.include' directive") ||
4363 // Attempt to switch the lexer to the included file before consuming the
4364 // end of statement to avoid losing it when we switch.
4365 check(enterIncludeFile(Filename), IncludeLoc,
4366 "Could not find include file '" + Filename + "'"))
Chris Lattner693fbb82009-07-16 06:14:39 +00004367 return true;
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004368
4369 return false;
4370}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004371
Jim Grosbach4b905842013-09-20 23:08:21 +00004372/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00004373/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004374bool AsmParser::parseDirectiveIncbin() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004375 // Allow the strings to have escaped octal character sequence.
4376 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004377 SMLoc IncbinLoc = getTok().getLoc();
4378 if (check(getTok().isNot(AsmToken::String),
4379 "expected string in '.incbin' directive") ||
4380 parseEscapedString(Filename) ||
4381 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00004382 "unexpected token in '.incbin' directive"))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004383 return true;
Nirav Dave1ab71992016-07-18 19:35:21 +00004384
4385 // Attempt to process the included file.
4386 if (processIncbinFile(Filename))
4387 return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
Kevin Enderby109f25c2011-12-14 21:47:48 +00004388 return false;
4389}
4390
Jim Grosbach4b905842013-09-20 23:08:21 +00004391/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004392/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4393bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004394 TheCondStack.push_back(TheCondState);
4395 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004396 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004397 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004398 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004399 int64_t ExprValue;
Nirav Davea645433c2016-07-18 15:24:03 +00004400 if (parseAbsoluteExpression(ExprValue) ||
4401 parseToken(AsmToken::EndOfStatement,
4402 "unexpected token in '.if' directive"))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004403 return true;
4404
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004405 switch (DirKind) {
4406 default:
4407 llvm_unreachable("unsupported directive");
4408 case DK_IF:
4409 case DK_IFNE:
4410 break;
4411 case DK_IFEQ:
4412 ExprValue = ExprValue == 0;
4413 break;
4414 case DK_IFGE:
4415 ExprValue = ExprValue >= 0;
4416 break;
4417 case DK_IFGT:
4418 ExprValue = ExprValue > 0;
4419 break;
4420 case DK_IFLE:
4421 ExprValue = ExprValue <= 0;
4422 break;
4423 case DK_IFLT:
4424 ExprValue = ExprValue < 0;
4425 break;
4426 }
4427
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004428 TheCondState.CondMet = ExprValue;
4429 TheCondState.Ignore = !TheCondState.CondMet;
4430 }
4431
4432 return false;
4433}
4434
Jim Grosbach4b905842013-09-20 23:08:21 +00004435/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004436/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004437bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004438 TheCondStack.push_back(TheCondState);
4439 TheCondState.TheCond = AsmCond::IfCond;
4440
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004441 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004442 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004443 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004444 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004445
Nirav Davea645433c2016-07-18 15:24:03 +00004446 if (parseToken(AsmToken::EndOfStatement,
4447 "unexpected token in '.ifb' directive"))
4448 return true;
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004449
4450 TheCondState.CondMet = ExpectBlank == Str.empty();
4451 TheCondState.Ignore = !TheCondState.CondMet;
4452 }
4453
4454 return false;
4455}
4456
Jim Grosbach4b905842013-09-20 23:08:21 +00004457/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004458/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004459/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004460bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004461 TheCondStack.push_back(TheCondState);
4462 TheCondState.TheCond = AsmCond::IfCond;
4463
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004464 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004465 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004466 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004467 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004468
Nirav Davea645433c2016-07-18 15:24:03 +00004469 if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
4470 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004471
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004472 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004473
Nirav Davea645433c2016-07-18 15:24:03 +00004474 if (parseToken(AsmToken::EndOfStatement,
4475 "unexpected token in '.ifc' directive"))
4476 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004477
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004478 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004479 TheCondState.Ignore = !TheCondState.CondMet;
4480 }
4481
4482 return false;
4483}
4484
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004485/// parseDirectiveIfeqs
4486/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004487bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004488 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004489 if (ExpectEqual)
Eric Christopher04c7db32016-09-13 00:19:29 +00004490 TokError("expected string parameter for '.ifeqs' directive");
4491 else
4492 TokError("expected string parameter for '.ifnes' directive");
4493 eatToEndOfStatement();
4494 return true;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004495 }
4496
4497 StringRef String1 = getTok().getStringContents();
4498 Lex();
4499
4500 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004501 if (ExpectEqual)
Eric Christopher04c7db32016-09-13 00:19:29 +00004502 TokError("expected comma after first string for '.ifeqs' directive");
4503 else
4504 TokError("expected comma after first string for '.ifnes' directive");
4505 eatToEndOfStatement();
4506 return true;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004507 }
4508
4509 Lex();
4510
4511 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004512 if (ExpectEqual)
Eric Christopher04c7db32016-09-13 00:19:29 +00004513 TokError("expected string parameter for '.ifeqs' directive");
4514 else
4515 TokError("expected string parameter for '.ifnes' directive");
4516 eatToEndOfStatement();
4517 return true;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004518 }
4519
4520 StringRef String2 = getTok().getStringContents();
4521 Lex();
4522
4523 TheCondStack.push_back(TheCondState);
4524 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004525 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004526 TheCondState.Ignore = !TheCondState.CondMet;
4527
4528 return false;
4529}
4530
Jim Grosbach4b905842013-09-20 23:08:21 +00004531/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004532/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004533bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004534 StringRef Name;
4535 TheCondStack.push_back(TheCondState);
4536 TheCondState.TheCond = AsmCond::IfCond;
4537
4538 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004539 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004540 } else {
Nirav Davea645433c2016-07-18 15:24:03 +00004541 if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
4542 parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
4543 return true;
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004544
Jim Grosbach6f482002015-05-18 18:43:14 +00004545 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004546
4547 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004548 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004549 else
Craig Topper353eda42014-04-24 06:44:33 +00004550 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004551 TheCondState.Ignore = !TheCondState.CondMet;
4552 }
4553
4554 return false;
4555}
4556
Jim Grosbach4b905842013-09-20 23:08:21 +00004557/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004558/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004559bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004560 if (TheCondState.TheCond != AsmCond::IfCond &&
4561 TheCondState.TheCond != AsmCond::ElseIfCond)
Eric Christopher04c7db32016-09-13 00:19:29 +00004562 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4563 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004564 TheCondState.TheCond = AsmCond::ElseIfCond;
4565
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004566 bool LastIgnoreState = false;
4567 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004568 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004569 if (LastIgnoreState || TheCondState.CondMet) {
4570 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004571 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004572 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004573 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004574 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004575 return true;
4576
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004577 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004578 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004579
Sean Callanan686ed8d2010-01-19 20:22:31 +00004580 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004581 TheCondState.CondMet = ExprValue;
4582 TheCondState.Ignore = !TheCondState.CondMet;
4583 }
4584
4585 return false;
4586}
4587
Jim Grosbach4b905842013-09-20 23:08:21 +00004588/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004589/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004590bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004591 if (parseToken(AsmToken::EndOfStatement,
4592 "unexpected token in '.else' directive"))
4593 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004594
4595 if (TheCondState.TheCond != AsmCond::IfCond &&
4596 TheCondState.TheCond != AsmCond::ElseIfCond)
Eric Christopher04c7db32016-09-13 00:19:29 +00004597 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4598 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004599 TheCondState.TheCond = AsmCond::ElseCond;
4600 bool LastIgnoreState = false;
4601 if (!TheCondStack.empty())
4602 LastIgnoreState = TheCondStack.back().Ignore;
4603 if (LastIgnoreState || TheCondState.CondMet)
4604 TheCondState.Ignore = true;
4605 else
4606 TheCondState.Ignore = false;
4607
4608 return false;
4609}
4610
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004611/// parseDirectiveEnd
4612/// ::= .end
4613bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004614 if (parseToken(AsmToken::EndOfStatement,
4615 "unexpected token in '.end' directive"))
4616 return true;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004617
4618 while (Lexer.isNot(AsmToken::Eof))
4619 Lex();
4620
4621 return false;
4622}
4623
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004624/// parseDirectiveError
4625/// ::= .err
4626/// ::= .error [string]
4627bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4628 if (!TheCondStack.empty()) {
4629 if (TheCondStack.back().Ignore) {
4630 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004631 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004632 }
4633 }
4634
4635 if (!WithMessage)
4636 return Error(L, ".err encountered");
4637
4638 StringRef Message = ".error directive invoked in source file";
4639 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00004640 if (Lexer.isNot(AsmToken::String)) {
4641 TokError(".error argument must be a string");
4642 eatToEndOfStatement();
4643 return true;
4644 }
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004645
4646 Message = getTok().getStringContents();
4647 Lex();
4648 }
4649
Eric Christopher04c7db32016-09-13 00:19:29 +00004650 Error(L, Message);
4651 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004652}
4653
Nico Weber404012b2014-07-24 16:26:06 +00004654/// parseDirectiveWarning
4655/// ::= .warning [string]
4656bool AsmParser::parseDirectiveWarning(SMLoc L) {
4657 if (!TheCondStack.empty()) {
4658 if (TheCondStack.back().Ignore) {
4659 eatToEndOfStatement();
4660 return false;
4661 }
4662 }
4663
4664 StringRef Message = ".warning directive invoked in source file";
4665 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00004666 if (Lexer.isNot(AsmToken::String)) {
4667 TokError(".warning argument must be a string");
4668 eatToEndOfStatement();
4669 return true;
4670 }
Nico Weber404012b2014-07-24 16:26:06 +00004671
4672 Message = getTok().getStringContents();
4673 Lex();
4674 }
4675
Eric Christopher04c7db32016-09-13 00:19:29 +00004676 Warning(L, Message);
4677 return false;
Nico Weber404012b2014-07-24 16:26:06 +00004678}
4679
Jim Grosbach4b905842013-09-20 23:08:21 +00004680/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004681/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004682bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004683 if (parseToken(AsmToken::EndOfStatement,
4684 "unexpected token in '.endif' directive"))
4685 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004686
Jim Grosbach4b905842013-09-20 23:08:21 +00004687 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Eric Christopher04c7db32016-09-13 00:19:29 +00004688 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4689 ".else");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004690 if (!TheCondStack.empty()) {
4691 TheCondState = TheCondStack.back();
4692 TheCondStack.pop_back();
4693 }
4694
4695 return false;
4696}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004697
Eli Bendersky17233942013-01-15 22:59:42 +00004698void AsmParser::initializeDirectiveKindMap() {
4699 DirectiveKindMap[".set"] = DK_SET;
4700 DirectiveKindMap[".equ"] = DK_EQU;
4701 DirectiveKindMap[".equiv"] = DK_EQUIV;
4702 DirectiveKindMap[".ascii"] = DK_ASCII;
4703 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4704 DirectiveKindMap[".string"] = DK_STRING;
4705 DirectiveKindMap[".byte"] = DK_BYTE;
4706 DirectiveKindMap[".short"] = DK_SHORT;
4707 DirectiveKindMap[".value"] = DK_VALUE;
4708 DirectiveKindMap[".2byte"] = DK_2BYTE;
4709 DirectiveKindMap[".long"] = DK_LONG;
4710 DirectiveKindMap[".int"] = DK_INT;
4711 DirectiveKindMap[".4byte"] = DK_4BYTE;
4712 DirectiveKindMap[".quad"] = DK_QUAD;
4713 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004714 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004715 DirectiveKindMap[".single"] = DK_SINGLE;
4716 DirectiveKindMap[".float"] = DK_FLOAT;
4717 DirectiveKindMap[".double"] = DK_DOUBLE;
4718 DirectiveKindMap[".align"] = DK_ALIGN;
4719 DirectiveKindMap[".align32"] = DK_ALIGN32;
4720 DirectiveKindMap[".balign"] = DK_BALIGN;
4721 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4722 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4723 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4724 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4725 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4726 DirectiveKindMap[".org"] = DK_ORG;
4727 DirectiveKindMap[".fill"] = DK_FILL;
4728 DirectiveKindMap[".zero"] = DK_ZERO;
4729 DirectiveKindMap[".extern"] = DK_EXTERN;
4730 DirectiveKindMap[".globl"] = DK_GLOBL;
4731 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004732 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4733 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4734 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4735 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4736 DirectiveKindMap[".reference"] = DK_REFERENCE;
4737 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4738 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4739 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4740 DirectiveKindMap[".comm"] = DK_COMM;
4741 DirectiveKindMap[".common"] = DK_COMMON;
4742 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4743 DirectiveKindMap[".abort"] = DK_ABORT;
4744 DirectiveKindMap[".include"] = DK_INCLUDE;
4745 DirectiveKindMap[".incbin"] = DK_INCBIN;
4746 DirectiveKindMap[".code16"] = DK_CODE16;
4747 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4748 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004749 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004750 DirectiveKindMap[".irp"] = DK_IRP;
4751 DirectiveKindMap[".irpc"] = DK_IRPC;
4752 DirectiveKindMap[".endr"] = DK_ENDR;
4753 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4754 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4755 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4756 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004757 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4758 DirectiveKindMap[".ifge"] = DK_IFGE;
4759 DirectiveKindMap[".ifgt"] = DK_IFGT;
4760 DirectiveKindMap[".ifle"] = DK_IFLE;
4761 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004762 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004763 DirectiveKindMap[".ifb"] = DK_IFB;
4764 DirectiveKindMap[".ifnb"] = DK_IFNB;
4765 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004766 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004767 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004768 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004769 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4770 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4771 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4772 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4773 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004774 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004775 DirectiveKindMap[".endif"] = DK_ENDIF;
4776 DirectiveKindMap[".skip"] = DK_SKIP;
4777 DirectiveKindMap[".space"] = DK_SPACE;
4778 DirectiveKindMap[".file"] = DK_FILE;
4779 DirectiveKindMap[".line"] = DK_LINE;
4780 DirectiveKindMap[".loc"] = DK_LOC;
4781 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004782 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00004783 DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004784 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4785 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004786 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00004787 DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
David Majnemer408b5e62016-02-05 01:55:49 +00004788 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004789 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4790 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004791 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4792 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4793 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4794 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4795 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4796 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4797 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4798 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4799 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4800 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4801 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4802 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4803 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4804 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4805 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4806 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4807 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4808 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4809 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4810 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4811 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004812 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004813 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4814 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4815 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004816 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004817 DirectiveKindMap[".endm"] = DK_ENDM;
4818 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4819 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004820 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004821 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004822 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00004823 DirectiveKindMap[".reloc"] = DK_RELOC;
Petr Hosek731bb9c2016-08-23 21:34:53 +00004824 DirectiveKindMap[".dc"] = DK_DC;
4825 DirectiveKindMap[".dc.a"] = DK_DC_A;
4826 DirectiveKindMap[".dc.b"] = DK_DC_B;
4827 DirectiveKindMap[".dc.d"] = DK_DC_D;
4828 DirectiveKindMap[".dc.l"] = DK_DC_L;
4829 DirectiveKindMap[".dc.s"] = DK_DC_S;
4830 DirectiveKindMap[".dc.w"] = DK_DC_W;
4831 DirectiveKindMap[".dc.x"] = DK_DC_X;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004832}
4833
Jim Grosbach4b905842013-09-20 23:08:21 +00004834MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004835 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004836
Rafael Espindola34b9c512012-06-03 23:57:14 +00004837 unsigned NestLevel = 0;
Eugene Zelenko33d7b762016-08-23 17:14:32 +00004838 while (true) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004839 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004840 if (getLexer().is(AsmToken::Eof)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00004841 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004842 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004843 }
4844
Rafael Espindola34b9c512012-06-03 23:57:14 +00004845 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00004846 (getTok().getIdentifier() == ".rept" ||
4847 getTok().getIdentifier() == ".irp" ||
4848 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004849 ++NestLevel;
4850 }
4851
4852 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004853 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004854 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004855 EndToken = getTok();
4856 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004857 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00004858 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004859 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004860 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004861 break;
4862 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004863 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004864 }
4865
Rafael Espindola34b9c512012-06-03 23:57:14 +00004866 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004867 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004868 }
4869
4870 const char *BodyStart = StartToken.getLoc().getPointer();
4871 const char *BodyEnd = EndToken.getLoc().getPointer();
4872 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4873
Rafael Espindola34b9c512012-06-03 23:57:14 +00004874 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00004875 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004876 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004877}
4878
Jim Grosbach4b905842013-09-20 23:08:21 +00004879void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004880 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004881 OS << ".endr\n";
4882
Rafael Espindola3560ff22014-08-27 20:03:13 +00004883 std::unique_ptr<MemoryBuffer> Instantiation =
4884 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004885
Rafael Espindola34b9c512012-06-03 23:57:14 +00004886 // Create the macro instantiation object and add to the current macro
4887 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004888 MacroInstantiation *MI = new MacroInstantiation(
4889 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004890 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004891
Rafael Espindola34b9c512012-06-03 23:57:14 +00004892 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004893 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004894 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004895 Lex();
4896}
4897
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004898/// parseDirectiveRept
4899/// ::= .rep | .rept count
4900bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004901 const MCExpr *CountExpr;
4902 SMLoc CountLoc = getTok().getLoc();
4903 if (parseExpression(CountExpr))
4904 return true;
4905
Rafael Espindola34b9c512012-06-03 23:57:14 +00004906 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00004907 if (!CountExpr->evaluateAsAbsolute(Count)) {
Eric Christopher04c7db32016-09-13 00:19:29 +00004908 eatToEndOfStatement();
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004909 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4910 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004911
Nirav Davea645433c2016-07-18 15:24:03 +00004912 if (check(Count < 0, CountLoc, "Count is negative") ||
4913 parseToken(AsmToken::EndOfStatement,
4914 "unexpected token in '" + Dir + "' directive"))
4915 return true;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004916
4917 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004918 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004919 if (!M)
4920 return true;
4921
4922 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4923 // to hold the macro body with substitutions.
4924 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004925 raw_svector_ostream OS(Buf);
4926 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004927 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
4928 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004929 return true;
4930 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004931 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004932
4933 return false;
4934}
4935
Jim Grosbach4b905842013-09-20 23:08:21 +00004936/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004937/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004938bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004939 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00004940 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00004941 if (check(parseIdentifier(Parameter.Name),
4942 "expected identifier in '.irp' directive") ||
4943 parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
4944 parseMacroArguments(nullptr, A) ||
4945 parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004946 return true;
4947
Rafael Espindola768b41c2012-06-15 14:02:34 +00004948 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004949 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004950 if (!M)
4951 return true;
4952
4953 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4954 // to hold the macro body with substitutions.
4955 SmallString<256> Buf;
4956 raw_svector_ostream OS(Buf);
4957
Craig Topper84008482015-10-10 05:38:14 +00004958 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00004959 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
4960 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00004961 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004962 return true;
4963 }
4964
Jim Grosbach4b905842013-09-20 23:08:21 +00004965 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004966
4967 return false;
4968}
4969
Jim Grosbach4b905842013-09-20 23:08:21 +00004970/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004971/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004972bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004973 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00004974 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00004975
4976 if (check(parseIdentifier(Parameter.Name),
4977 "expected identifier in '.irpc' directive") ||
4978 parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
4979 parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004980 return true;
4981
4982 if (A.size() != 1 || A.front().size() != 1)
4983 return TokError("unexpected token in '.irpc' directive");
4984
4985 // Eat the end of statement.
Nirav Davea645433c2016-07-18 15:24:03 +00004986 if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
4987 return true;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004988
4989 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004990 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004991 if (!M)
4992 return true;
4993
4994 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4995 // to hold the macro body with substitutions.
4996 SmallString<256> Buf;
4997 raw_svector_ostream OS(Buf);
4998
4999 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00005000 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00005001 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00005002 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005003
Toma Tabacu217116e2015-04-27 10:50:29 +00005004 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
5005 // This is undocumented, but GAS seems to support it.
5006 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005007 return true;
5008 }
5009
Jim Grosbach4b905842013-09-20 23:08:21 +00005010 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005011
5012 return false;
5013}
5014
Jim Grosbach4b905842013-09-20 23:08:21 +00005015bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00005016 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00005017 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005018
5019 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00005020 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005021 assert(getLexer().is(AsmToken::EndOfStatement));
5022
Jim Grosbach4b905842013-09-20 23:08:21 +00005023 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005024 return false;
5025}
Rafael Espindola12d73d12010-09-11 16:45:15 +00005026
Jim Grosbach4b905842013-09-20 23:08:21 +00005027bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00005028 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00005029 const MCExpr *Value;
5030 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00005031 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00005032 return true;
5033 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5034 if (!MCE)
5035 return Error(ExprLoc, "unexpected expression in _emit");
5036 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00005037 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00005038 return Error(ExprLoc, "literal value out of range for directive");
5039
Craig Topper7d5b2312015-10-10 05:25:02 +00005040 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00005041 return false;
5042}
5043
Jim Grosbach4b905842013-09-20 23:08:21 +00005044bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00005045 const MCExpr *Value;
5046 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00005047 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00005048 return true;
5049 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5050 if (!MCE)
5051 return Error(ExprLoc, "unexpected expression in align");
5052 uint64_t IntValue = MCE->getValue();
5053 if (!isPowerOf2_64(IntValue))
5054 return Error(ExprLoc, "literal value not a power of two greater then zero");
5055
Craig Topper7d5b2312015-10-10 05:25:02 +00005056 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00005057 return false;
5058}
5059
Chad Rosierf43fcf52013-02-13 21:27:17 +00005060// We are comparing pointers, but the pointers are relative to a single string.
5061// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00005062static int rewritesSort(const AsmRewrite *AsmRewriteA,
5063 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00005064 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
5065 return -1;
5066 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
5067 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005068
Chad Rosierfce4fab2013-04-08 17:43:47 +00005069 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
5070 // rewrite to the same location. Make sure the SizeDirective rewrite is
5071 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
5072 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00005073 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
5074 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005075 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00005076
Jim Grosbach4b905842013-09-20 23:08:21 +00005077 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5078 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005079 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00005080 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00005081}
5082
Jim Grosbach4b905842013-09-20 23:08:21 +00005083bool AsmParser::parseMSInlineAsm(
5084 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
5085 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
5086 SmallVectorImpl<std::string> &Constraints,
5087 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5088 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00005089 SmallVector<void *, 4> InputDecls;
5090 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00005091 SmallVector<bool, 4> InputDeclsAddressOf;
5092 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00005093 SmallVector<std::string, 4> InputConstraints;
5094 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005095 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00005096
Benjamin Kramer1a136112013-02-15 20:37:21 +00005097 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005098
5099 // Prime the lexer.
5100 Lex();
5101
5102 // While we have input, parse each statement.
5103 unsigned InputIdx = 0;
5104 unsigned OutputIdx = 0;
5105 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005106 // Parse curly braces marking block start/end
5107 if (parseCurlyBlockScope(AsmStrRewrites))
5108 continue;
5109
Eli Friedman0f4871d2012-10-22 23:58:19 +00005110 ParseStatementInfo Info(&AsmStrRewrites);
Eric Christopher04c7db32016-09-13 00:19:29 +00005111 if (parseStatement(Info, &SI))
Chad Rosier149e8e02012-12-12 22:45:52 +00005112 return true;
Nirav Davec0c0f7a12016-09-12 20:03:02 +00005113
Eric Christopher04c7db32016-09-13 00:19:29 +00005114 if (Info.ParseError)
5115 return true;
Chad Rosier149e8e02012-12-12 22:45:52 +00005116
Benjamin Kramer1a136112013-02-15 20:37:21 +00005117 if (Info.Opcode == ~0U)
5118 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005119
Benjamin Kramer1a136112013-02-15 20:37:21 +00005120 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005121
Benjamin Kramer1a136112013-02-15 20:37:21 +00005122 // Build the list of clobbers, outputs and inputs.
5123 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005124 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005125
Benjamin Kramer1a136112013-02-15 20:37:21 +00005126 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005127 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005128 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005129
Benjamin Kramer1a136112013-02-15 20:37:21 +00005130 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005131 if (Operand.isReg() && !Operand.needAddressOf() &&
5132 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005133 unsigned NumDefs = Desc.getNumDefs();
5134 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005135 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5136 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005137 continue;
5138 }
5139
5140 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005141 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005142 if (SymName.empty())
5143 continue;
5144
David Blaikie960ea3f2014-06-08 16:18:35 +00005145 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005146 if (!OpDecl)
5147 continue;
5148
5149 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005150 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005151 if (isOutput) {
5152 ++InputIdx;
5153 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005154 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005155 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005156 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005157 } else {
5158 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005159 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5160 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005161 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005162 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005163 }
Reid Kleckneree088972013-12-10 18:27:32 +00005164
5165 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005166 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5167 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005168 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005169 }
5170
5171 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005172 NumOutputs = OutputDecls.size();
5173 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005174
5175 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005176 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5177 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5178 ClobberRegs.end());
5179 Clobbers.assign(ClobberRegs.size(), std::string());
5180 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5181 raw_string_ostream OS(Clobbers[I]);
5182 IP->printRegName(OS, ClobberRegs[I]);
5183 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005184
5185 // Merge the various outputs and inputs. Output are expected first.
5186 if (NumOutputs || NumInputs) {
5187 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005188 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005189 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005190 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005191 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005192 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005193 }
5194 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005195 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005196 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005197 }
5198 }
5199
5200 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005201 std::string AsmStringIR;
5202 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005203 StringRef ASMString =
5204 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5205 const char *AsmStart = ASMString.begin();
5206 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005207 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005208 for (const AsmRewrite &AR : AsmStrRewrites) {
5209 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005210 if (Kind == AOK_Delete)
5211 continue;
5212
David Majnemer8114c1a2014-06-23 02:17:16 +00005213 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005214 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005215
Chad Rosier120eefd2013-03-19 17:32:17 +00005216 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005217 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005218 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005219
Chad Rosier37e755c2012-10-23 17:43:43 +00005220 // Skip the original expression.
5221 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005222 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005223 continue;
5224 }
5225
Chad Rosierff10ed12013-04-12 16:26:42 +00005226 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005227 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005228 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005229 default:
5230 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005231 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005232 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005233 break;
5234 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005235 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005236 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005237 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005238 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005239 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005240 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005241 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005242 break;
5243 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005244 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005245 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005246 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005247 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005248 default: break;
5249 case 8: OS << "byte ptr "; break;
5250 case 16: OS << "word ptr "; break;
5251 case 32: OS << "dword ptr "; break;
5252 case 64: OS << "qword ptr "; break;
5253 case 80: OS << "xword ptr "; break;
5254 case 128: OS << "xmmword ptr "; break;
5255 case 256: OS << "ymmword ptr "; break;
5256 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005257 break;
5258 case AOK_Emit:
5259 OS << ".byte";
5260 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005261 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005262 // MS alignment directives are measured in bytes. If the native assembler
5263 // measures alignment in bytes, we can pass it straight through.
5264 OS << ".align";
5265 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5266 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005267
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005268 // Alignment is in log2 form, so print that instead and skip the original
5269 // immediate.
5270 unsigned Val = AR.Val;
5271 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005272 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005273 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5274 break;
5275 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005276 case AOK_EVEN:
5277 OS << ".even";
5278 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005279 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005280 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005281 OS.flush();
5282 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005283 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005284 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005285 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005286 case AOK_EndOfStatement:
5287 OS << "\n\t";
5288 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005289 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005290
Chad Rosier8bce6642012-10-18 15:49:34 +00005291 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005292 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005293 }
5294
5295 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005296 if (AsmStart != AsmEnd)
5297 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005298
5299 AsmString = OS.str();
5300 return false;
5301}
5302
Pete Cooper80d21cb2015-06-22 19:35:57 +00005303namespace llvm {
5304namespace MCParserUtils {
5305
5306/// Returns whether the given symbol is used anywhere in the given expression,
5307/// or subexpressions.
5308static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5309 switch (Value->getKind()) {
5310 case MCExpr::Binary: {
5311 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5312 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5313 isSymbolUsedInExpression(Sym, BE->getRHS());
5314 }
5315 case MCExpr::Target:
5316 case MCExpr::Constant:
5317 return false;
5318 case MCExpr::SymbolRef: {
5319 const MCSymbol &S =
5320 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5321 if (S.isVariable())
5322 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5323 return &S == Sym;
5324 }
5325 case MCExpr::Unary:
5326 return isSymbolUsedInExpression(
5327 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5328 }
5329
5330 llvm_unreachable("Unknown expr kind!");
5331}
5332
5333bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5334 MCAsmParser &Parser, MCSymbol *&Sym,
5335 const MCExpr *&Value) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00005336
5337 // FIXME: Use better location, we should use proper tokens.
Nirav Davefd910412016-06-17 16:06:17 +00005338 SMLoc EqualLoc = Parser.getTok().getLoc();
Pete Cooper80d21cb2015-06-22 19:35:57 +00005339
5340 if (Parser.parseExpression(Value)) {
5341 Parser.TokError("missing expression");
Eric Christopher04c7db32016-09-13 00:19:29 +00005342 Parser.eatToEndOfStatement();
Pete Cooper80d21cb2015-06-22 19:35:57 +00005343 return true;
5344 }
5345
5346 // Note: we don't count b as used in "a = b". This is to allow
5347 // a = b
5348 // b = c
5349
Nirav Davefd910412016-06-17 16:06:17 +00005350 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
Pete Cooper80d21cb2015-06-22 19:35:57 +00005351 return Parser.TokError("unexpected token in assignment");
5352
5353 // Eat the end of statement marker.
5354 Parser.Lex();
5355
5356 // Validate that the LHS is allowed to be a variable (either it has not been
5357 // used as a symbol, or it is an absolute symbol).
5358 Sym = Parser.getContext().lookupSymbol(Name);
5359 if (Sym) {
5360 // Diagnose assignment to a label.
5361 //
5362 // FIXME: Diagnostics. Note the location of the definition as a label.
5363 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5364 if (isSymbolUsedInExpression(Sym, Value))
5365 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005366 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5367 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005368 ; // Allow redefinitions of undefined symbols only used in directives.
5369 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5370 ; // Allow redefinitions of variables that haven't yet been used.
5371 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5372 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5373 else if (!Sym->isVariable())
5374 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5375 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5376 return Parser.Error(EqualLoc,
5377 "invalid reassignment of non-absolute variable '" +
5378 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005379 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005380 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005381 return false;
5382 } else
5383 Sym = Parser.getContext().getOrCreateSymbol(Name);
5384
5385 Sym->setRedefinable(allow_redef);
5386
5387 return false;
5388}
5389
Eugene Zelenko33d7b762016-08-23 17:14:32 +00005390} // end namespace MCParserUtils
5391} // end namespace llvm
Pete Cooper80d21cb2015-06-22 19:35:57 +00005392
Daniel Dunbar01e36072010-07-17 02:26:10 +00005393/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005394MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5395 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005396 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005397}