blob: e817a8fa57df9610ed2f14ee98ebd34aae0555dd [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
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000181 /// The values from the last parsed cpp hash file line comment if any.
Tim Northoverc0bef992016-04-13 19:46:54 +0000182 struct CppHashInfoTy {
183 StringRef Filename;
Andrew Kaylorca196472016-04-21 20:09:35 +0000184 int64_t LineNumber = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000185 SMLoc Loc;
Andrew Kaylorca196472016-04-21 20:09:35 +0000186 unsigned Buf = 0;
Tim Northoverc0bef992016-04-13 19:46:54 +0000187 };
188 CppHashInfoTy CppHashInfo;
189
190 /// \brief List of forward directional labels for diagnosis at the end.
191 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
192
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000193 /// When generating dwarf for assembly source files we need to calculate the
194 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000195 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000196 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
197 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000198 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000199 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000200
Devang Patela173ee52012-01-31 18:14:05 +0000201 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
202 unsigned AssemblerDialect;
203
Jim Grosbach4b905842013-09-20 23:08:21 +0000204 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000205 bool IsDarwin;
206
Jim Grosbach4b905842013-09-20 23:08:21 +0000207 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000208 bool ParsingInlineAsm;
209
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000210public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000211 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212 const MCAsmInfo &MAI);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000213 ~AsmParser() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000214
Craig Topper59be68f2014-03-08 07:14:16 +0000215 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000216
Craig Topper59be68f2014-03-08 07:14:16 +0000217 void addDirectiveHandler(StringRef Directive,
218 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000219 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220 }
221
Toma Tabacu11e14a92015-04-21 11:50:52 +0000222 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
223 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
224 }
225
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000226public:
227 /// @name MCAsmParser Interface
228 /// {
229
Craig Topper59be68f2014-03-08 07:14:16 +0000230 SourceMgr &getSourceManager() override { return SrcMgr; }
231 MCAsmLexer &getLexer() override { return Lexer; }
232 MCContext &getContext() override { return Ctx; }
233 MCStreamer &getStreamer() override { return Out; }
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000234
Reid Klecknera5b1eef2016-08-26 17:58:37 +0000235 CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
236
Craig Topper59be68f2014-03-08 07:14:16 +0000237 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000238 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000239 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000240 else
241 return AssemblerDialect;
242 }
Craig Topper59be68f2014-03-08 07:14:16 +0000243 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000244 AssemblerDialect = i;
245 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000246
Nirav Dave2364748a2016-09-16 18:30:20 +0000247 void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
248 bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
249 bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000250
Craig Topper59be68f2014-03-08 07:14:16 +0000251 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Yunzhong Gao27ea29b2016-09-02 23:15:29 +0000253 void setParsingInlineAsm(bool V) override {
254 ParsingInlineAsm = V;
255 Lexer.setParsingMSInlineAsm(V);
256 }
Craig Topper59be68f2014-03-08 07:14:16 +0000257 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000258
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000259 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000260 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000261 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000262 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000263 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000264 const MCInstrInfo *MII, const MCInstPrinter *IP,
265 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000266
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000267 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000268 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
269 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
270 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +0000271 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
272 SMLoc &EndLoc) override;
Craig Topper59be68f2014-03-08 07:14:16 +0000273 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000274
Petr Hosek4cb08ce2016-09-23 19:25:15 +0000275 /// \brief Parse a floating point expression using the float \p Semantics
276 /// and set \p Res to the value.
277 bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
278
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000280 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000281 bool parseIdentifier(StringRef &Res) override;
282 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000283
Nirav Davef43cc9f2016-10-10 15:24:54 +0000284 bool checkForValidSection() override;
Nirav Davea645433c2016-07-18 15:24:03 +0000285
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000286 /// }
287
288private:
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000289 bool parseStatement(ParseStatementInfo &Info,
290 MCAsmParserSemaCallback *SI);
Marina Yatsina5f5de9f2016-03-07 18:11:16 +0000291 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
Craig Topper3c76c522015-09-20 23:35:59 +0000292 bool parseCppHashLineFilenameComment(SMLoc L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000293
Jim Grosbach4b905842013-09-20 23:08:21 +0000294 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000295 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000296 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000297 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +0000298 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
Craig Topper3c76c522015-09-20 23:35:59 +0000299 SMLoc L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000300
Eli Benderskya313ae62013-01-16 18:56:50 +0000301 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000303
304 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000306
307 /// \brief Lookup a previously defined macro.
308 /// \param Name Macro name.
309 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000310 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000311
312 /// \brief Define a new macro with the given name and information.
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000313 void defineMacro(StringRef Name, MCAsmMacro Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000314
315 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000316 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000317
318 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000320
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000321 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000322 ///
323 /// \param M The macro.
324 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000325 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000326
327 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000328 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000329
David Majnemer91fc4c22014-01-29 18:57:46 +0000330 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000331 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000332
333 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 void printMacroInstantiations();
337 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Nirav Dave2364748a2016-09-16 18:30:20 +0000338 SMRange Range = None) const {
339 ArrayRef<SMRange> Ranges(Range);
Chris Lattner72845262011-10-16 05:47:55 +0000340 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000341 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000342 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000343
Jim Grosbach4b905842013-09-20 23:08:21 +0000344 /// \brief Enter the specified file. This returns true on failure.
345 bool enterIncludeFile(const std::string &Filename);
346
347 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000348 /// This returns true on failure.
Petr Hosek2f4ac442016-09-23 00:41:06 +0000349 bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
350 const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
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,
Petr Hosek4cb08ce2016-09-23 19:25:15 +0000391 DK_DCB, DK_DCB_B, DK_DCB_D, DK_DCB_L, DK_DCB_S, DK_DCB_W, DK_DCB_X,
Petr Hosek85b2f672016-09-23 21:53:36 +0000392 DK_DS, DK_DS_B, DK_DS_D, DK_DS_L, DK_DS_P, DK_DS_S, DK_DS_W, DK_DS_X,
David Woodhoused6de0d92014-02-01 16:20:59 +0000393 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000394 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000395 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000396 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Lang Hamesf9033bb2016-04-11 18:33:45 +0000397 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER,
Lang Hames1b640e02016-03-15 01:43:05 +0000398 DK_PRIVATE_EXTERN, DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000399 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
400 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000401 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
Sid Manning51c35602015-03-18 14:20:54 +0000402 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
403 DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000404 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000405 DK_CV_FILE, DK_CV_FUNC_ID, DK_CV_INLINE_SITE_ID, DK_CV_LOC, DK_CV_LINETABLE,
406 DK_CV_INLINE_LINETABLE, DK_CV_DEF_RANGE, DK_CV_STRINGTABLE,
407 DK_CV_FILECHECKSUMS,
Eli Bendersky17233942013-01-15 22:59:42 +0000408 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
409 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
410 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
411 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
412 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000413 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000414 DK_MACROS_ON, DK_MACROS_OFF,
415 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000416 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000417 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000418 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000419 };
420
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000422 /// directives parsed by this class.
423 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000424
425 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000427 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000429 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
431 bool parseDirectiveFill(); // ".fill"
432 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000433 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
435 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Eli Bendersky17233942013-01-15 22:59:42 +0000439 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveFile(SMLoc DirectiveLoc);
441 bool parseDirectiveLine();
442 bool parseDirectiveLoc();
443 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000444
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000445 // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
446 // ".cv_inline_linetable", ".cv_def_range"
Reid Kleckner2214ed82016-01-29 00:49:42 +0000447 bool parseDirectiveCVFile();
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000448 bool parseDirectiveCVFuncId();
449 bool parseDirectiveCVInlineSiteId();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000450 bool parseDirectiveCVLoc();
451 bool parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000452 bool parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +0000453 bool parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +0000454 bool parseDirectiveCVStringTable();
455 bool parseDirectiveCVFileChecksums();
456
Eli Bendersky17233942013-01-15 22:59:42 +0000457 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000459 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveCFISections();
461 bool parseDirectiveCFIStartProc();
462 bool parseDirectiveCFIEndProc();
463 bool parseDirectiveCFIDefCfaOffset();
464 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
465 bool parseDirectiveCFIAdjustCfaOffset();
466 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
467 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
468 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
469 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
470 bool parseDirectiveCFIRememberState();
471 bool parseDirectiveCFIRestoreState();
472 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
473 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
474 bool parseDirectiveCFIEscape();
475 bool parseDirectiveCFISignalFrame();
476 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000477
478 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000479 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000480 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000481 bool parseDirectiveEndMacro(StringRef Directive);
482 bool parseDirectiveMacro(SMLoc DirectiveLoc);
483 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000484
Eli Benderskyf483ff92012-12-20 19:05:53 +0000485 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000486 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000487 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000488 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000489 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000490 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000491
Eli Bendersky17233942013-01-15 22:59:42 +0000492 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000493 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000494
Petr Hosek4cb08ce2016-09-23 19:25:15 +0000495 // ".dcb"
496 bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
497 bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
Petr Hosek85b2f672016-09-23 21:53:36 +0000498 // ".ds"
499 bool parseDirectiveDS(StringRef IDVal, unsigned Size);
Petr Hosek4cb08ce2016-09-23 19:25:15 +0000500
Eli Bendersky17233942013-01-15 22:59:42 +0000501 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000502 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000503
Jim Grosbach4b905842013-09-20 23:08:21 +0000504 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000505 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000506 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000507
Jim Grosbach4b905842013-09-20 23:08:21 +0000508 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000509
Jim Grosbach4b905842013-09-20 23:08:21 +0000510 bool parseDirectiveAbort(); // ".abort"
511 bool parseDirectiveInclude(); // ".include"
512 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000513
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000514 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
515 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000516 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000517 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000518 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000519 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Sid Manning51c35602015-03-18 14:20:54 +0000520 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
521 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000522 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000523 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
524 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
525 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
526 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000527 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000528
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000530 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000531
Rafael Espindola34b9c512012-06-03 23:57:14 +0000532 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000533 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
534 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000535 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000536 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000537 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
538 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
539 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000540
Chad Rosierc7f552c2013-02-12 21:33:51 +0000541 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000542 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000543 size_t Len);
544
545 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000546 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000547
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000548 // "end"
549 bool parseDirectiveEnd(SMLoc DirectiveLoc);
550
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000551 // ".err" or ".error"
552 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000553
Nico Weber404012b2014-07-24 16:26:06 +0000554 // ".warning"
555 bool parseDirectiveWarning(SMLoc DirectiveLoc);
556
Eli Bendersky17233942013-01-15 22:59:42 +0000557 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000558};
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000559
560} // end anonymous namespace
Daniel Dunbar86033402010-07-12 17:54:38 +0000561
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000562namespace llvm {
563
564extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000565extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000566extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000567
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000568} // end namespace llvm
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000569
Chris Lattnerc35681b2010-01-19 19:46:13 +0000570enum { DEFAULT_ADDRSPACE = 0 };
571
David Blaikie9f380a32015-03-16 18:06:57 +0000572AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
573 const MCAsmInfo &MAI)
574 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
575 PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
Nirav Dave2364748a2016-09-16 18:30:20 +0000576 MacrosEnabledFlag(true), CppHashInfo(), AssemblerDialect(~0U),
577 IsDarwin(false), ParsingInlineAsm(false) {
578 HadError = false;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000579 // Save the old handler.
580 SavedDiagHandler = SrcMgr.getDiagHandler();
581 SavedDiagContext = SrcMgr.getDiagContext();
582 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000583 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000584 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000585
Daniel Dunbarc5011082010-07-12 18:12:02 +0000586 // Initialize the platform / file format parser.
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000587 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
588 case MCObjectFileInfo::IsCOFF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000589 PlatformParser.reset(createCOFFAsmParser());
590 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000591 case MCObjectFileInfo::IsMachO:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000592 PlatformParser.reset(createDarwinAsmParser());
593 IsDarwin = true;
594 break;
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000595 case MCObjectFileInfo::IsELF:
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000596 PlatformParser.reset(createELFAsmParser());
597 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000598 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000599
Benjamin Kramercb3e06b2014-10-03 18:32:55 +0000600 PlatformParser->Initialize(*this);
Eli Bendersky17233942013-01-15 22:59:42 +0000601 initializeDirectiveKindMap();
Toma Tabacu217116e2015-04-27 10:50:29 +0000602
603 NumOfMacroInstantiations = 0;
Chris Lattner351a7ef2009-09-27 21:16:52 +0000604}
605
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000606AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000607 assert((HadError || ActiveMacros.empty()) &&
608 "Unexpected active macro instantiation!");
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000609}
610
Jim Grosbach4b905842013-09-20 23:08:21 +0000611void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000612 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000613 for (std::vector<MacroInstantiation *>::const_reverse_iterator
614 it = ActiveMacros.rbegin(),
615 ie = ActiveMacros.rend();
616 it != ie; ++it)
617 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000618 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000619}
620
Nirav Dave2364748a2016-09-16 18:30:20 +0000621void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
622 printPendingErrors();
623 printMessage(L, SourceMgr::DK_Note, Msg, Range);
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000624 printMacroInstantiations();
625}
626
Nirav Dave2364748a2016-09-16 18:30:20 +0000627bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
Colin LeMahieufe36f832015-07-27 22:39:14 +0000628 if(getTargetParser().getTargetOptions().MCNoWarn)
629 return false;
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000630 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Nirav Dave2364748a2016-09-16 18:30:20 +0000631 return Error(L, Msg, Range);
632 printMessage(L, SourceMgr::DK_Warning, Msg, Range);
Jim Grosbach4b905842013-09-20 23:08:21 +0000633 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000634 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000635}
636
Nirav Dave2364748a2016-09-16 18:30:20 +0000637bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000638 HadError = true;
Nirav Dave2364748a2016-09-16 18:30:20 +0000639 printMessage(L, SourceMgr::DK_Error, Msg, Range);
Jim Grosbach4b905842013-09-20 23:08:21 +0000640 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000641 return true;
642}
643
Jim Grosbach4b905842013-09-20 23:08:21 +0000644bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000645 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000646 unsigned NewBuf =
647 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
648 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000649 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000650
Sean Callanan7a77eae2010-01-21 00:19:58 +0000651 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000652 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000653 return false;
654}
Daniel Dunbar43235712010-07-18 18:54:11 +0000655
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000656/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000657/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000658/// returns true on failure.
Petr Hosek2f4ac442016-09-23 00:41:06 +0000659bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
660 const MCExpr *Count, SMLoc Loc) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000661 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000662 unsigned NewBuf =
663 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
664 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000665 return true;
666
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000667 // Pick up the bytes from the file and emit them.
Petr Hosek2f4ac442016-09-23 00:41:06 +0000668 StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
669 Bytes = Bytes.drop_front(Skip);
670 if (Count) {
671 int64_t Res;
672 if (!Count->evaluateAsAbsolute(Res))
673 return Error(Loc, "expected absolute expression");
674 if (Res < 0)
675 return Warning(Loc, "negative count has no effect");
676 Bytes = Bytes.take_front(Res);
677 }
678 getStreamer().EmitBytes(Bytes);
Kevin Enderby109f25c2011-12-14 21:47:48 +0000679 return false;
680}
681
Alp Tokera55b95b2014-07-06 10:33:31 +0000682void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
683 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000684 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
685 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000686}
687
Sean Callanan7a77eae2010-01-21 00:19:58 +0000688const AsmToken &AsmParser::Lex() {
Nirav Dave1180e6892016-06-02 17:15:05 +0000689 if (Lexer.getTok().is(AsmToken::Error))
690 Error(Lexer.getErrLoc(), Lexer.getErr());
691
Nirav Dave53a72f42016-07-11 12:42:14 +0000692 // if it's a end of statement with a comment in it
693 if (getTok().is(AsmToken::EndOfStatement)) {
694 // if this is a line comment output it.
695 if (getTok().getString().front() != '\n' &&
696 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
697 Out.addExplicitComment(Twine(getTok().getString()));
698 }
699
Sean Callanan7a77eae2010-01-21 00:19:58 +0000700 const AsmToken *tok = &Lexer.Lex();
Nirav Dave53a72f42016-07-11 12:42:14 +0000701
702 // Parse comments here to be deferred until end of next statement.
Nirav Davefd910412016-06-17 16:06:17 +0000703 while (tok->is(AsmToken::Comment)) {
Nirav Dave53a72f42016-07-11 12:42:14 +0000704 if (MAI.preserveAsmComments())
705 Out.addExplicitComment(Twine(tok->getString()));
Nirav Davefd910412016-06-17 16:06:17 +0000706 tok = &Lexer.Lex();
707 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000708
Sean Callanan7a77eae2010-01-21 00:19:58 +0000709 if (tok->is(AsmToken::Eof)) {
710 // If this is the end of an included file, pop the parent file off the
711 // include stack.
712 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
713 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000714 jumpToLoc(ParentIncludeLoc);
Nirav Davefd910412016-06-17 16:06:17 +0000715 return Lex();
Sean Callanan7a77eae2010-01-21 00:19:58 +0000716 }
717 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000718
Sean Callanan7a77eae2010-01-21 00:19:58 +0000719 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000720}
721
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000722bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000723 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000724 if (!NoInitialTextSection)
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000725 Out.InitSections(false);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000726
Chris Lattner36e02122009-06-21 20:54:55 +0000727 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000728 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000729
730 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000731 AsmCond StartingCondState = TheCondState;
732
Kevin Enderby6469fc22011-11-01 22:27:22 +0000733 // If we are generating dwarf for assembly source files save the initial text
734 // section and generate a .file directive.
735 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000736 MCSection *Sec = getStreamer().getCurrentSection().first;
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000737 if (!Sec->getBeginSymbol()) {
738 MCSymbol *SectionStartSym = getContext().createTempSymbol();
739 getStreamer().EmitLabel(SectionStartSym);
740 Sec->setBeginSymbol(SectionStartSym);
741 }
Rafael Espindolae0746792015-05-21 16:52:32 +0000742 bool InsertResult = getContext().addGenDwarfSection(Sec);
743 assert(InsertResult && ".text section should not have debug info yet");
Rafael Espindolafa160c72015-05-21 17:09:22 +0000744 (void)InsertResult;
David Blaikiec714ef42014-03-17 01:52:11 +0000745 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
746 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000747 }
748
Chris Lattner73f36112009-07-02 21:53:43 +0000749 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000750 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000751 ParseStatementInfo Info;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +0000752 if (!parseStatement(Info, nullptr))
Jim Grosbach4b905842013-09-20 23:08:21 +0000753 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000754
Nirav Dave2364748a2016-09-16 18:30:20 +0000755 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
756 // for printing ErrMsg via Lex() only if no (presumably better) parser error
757 // exists.
758 if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
Nirav Dave1180e6892016-06-02 17:15:05 +0000759 Lex();
760 }
761
Nirav Dave2364748a2016-09-16 18:30:20 +0000762 // parseStatement returned true so may need to emit an error.
763 printPendingErrors();
764
765 // Skipping to the next line if needed.
766 if (!getLexer().isAtStartOfStatement())
767 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000768 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000769
Nirav Dave2364748a2016-09-16 18:30:20 +0000770 // All errors should have been emitted.
771 assert(!hasPendingError() && "unexpected error from parseStatement");
772
Oliver Stannard21718282016-07-26 14:19:47 +0000773 getTargetParser().flushPendingInstructions(getStreamer());
774
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000775 if (TheCondState.TheCond != StartingCondState.TheCond ||
776 TheCondState.Ignore != StartingCondState.Ignore)
Nirav Dave2364748a2016-09-16 18:30:20 +0000777 printError(getTok().getLoc(), "unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000778 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000779 const auto &LineTables = getContext().getMCDwarfLineTables();
780 if (!LineTables.empty()) {
781 unsigned Index = 0;
782 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
783 if (File.Name.empty() && Index != 0)
Nirav Dave2364748a2016-09-16 18:30:20 +0000784 printError(getTok().getLoc(), "unassigned file number: " +
785 Twine(Index) +
786 " for .file directives");
David Blaikie8bf66c42014-04-01 07:35:52 +0000787 ++Index;
788 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000789 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000790
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000791 // Check to see that all assembler local symbols were actually defined.
792 // Targets that don't do subsections via symbols may not want this, though,
793 // so conservatively exclude them. Only do this if we're finalizing, though,
794 // as otherwise we won't necessarilly have seen everything yet.
Tim Northover6b3169b2016-04-11 19:50:46 +0000795 if (!NoFinalize) {
796 if (MAI.hasSubsectionsViaSymbols()) {
797 for (const auto &TableEntry : getContext().getSymbols()) {
798 MCSymbol *Sym = TableEntry.getValue();
799 // Variable symbols may not be marked as defined, so check those
800 // explicitly. If we know it's a variable, we have a definition for
801 // the purposes of this check.
802 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
803 // FIXME: We would really like to refer back to where the symbol was
804 // first referenced for a source location. We need to add something
805 // to track that. Currently, we just point to the end of the file.
Nirav Dave2364748a2016-09-16 18:30:20 +0000806 printError(getTok().getLoc(), "assembler local symbol '" +
807 Sym->getName() + "' not defined");
Tim Northover6b3169b2016-04-11 19:50:46 +0000808 }
809 }
810
811 // Temporary symbols like the ones for directional jumps don't go in the
812 // symbol table. They also need to be diagnosed in all (final) cases.
Tim Northoverc0bef992016-04-13 19:46:54 +0000813 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
814 if (std::get<2>(LocSym)->isUndefined()) {
815 // Reset the state of any "# line file" directives we've seen to the
816 // context as it was at the diagnostic site.
817 CppHashInfo = std::get<1>(LocSym);
Nirav Dave2364748a2016-09-16 18:30:20 +0000818 printError(std::get<0>(LocSym), "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +0000819 }
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000820 }
821 }
822
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000823 // Finalize the output stream if there are no errors and if the client wants
824 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000825 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000826 Out.Finish();
827
Oliver Stannard07b43d32015-11-17 09:58:07 +0000828 return HadError || getContext().hadError();
Chris Lattner36e02122009-06-21 20:54:55 +0000829}
830
Nirav Davef43cc9f2016-10-10 15:24:54 +0000831bool AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000832 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000833 Out.InitSections(false);
Nirav Davef43cc9f2016-10-10 15:24:54 +0000834 return Error(getTok().getLoc(),
835 "expected section directive before assembly directive");
Daniel Dunbare5444a82010-09-09 22:42:59 +0000836 }
Nirav Davef43cc9f2016-10-10 15:24:54 +0000837 return false;
Daniel Dunbare5444a82010-09-09 22:42:59 +0000838}
839
Jim Grosbach4b905842013-09-20 23:08:21 +0000840/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000841void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000842 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Dave1180e6892016-06-02 17:15:05 +0000843 Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000844
Chris Lattnere5074c42009-06-22 01:29:09 +0000845 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000846 if (Lexer.is(AsmToken::EndOfStatement))
Nirav Dave1180e6892016-06-02 17:15:05 +0000847 Lexer.Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000848}
849
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000850StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000851 const char *Start = getTok().getLoc().getPointer();
852
Jim Grosbach4b905842013-09-20 23:08:21 +0000853 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000854 Lexer.Lex();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000855
856 const char *End = getTok().getLoc().getPointer();
857 return StringRef(Start, End - Start);
858}
Chris Lattner78db3622009-06-22 05:51:26 +0000859
Jim Grosbach4b905842013-09-20 23:08:21 +0000860StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000861 const char *Start = getTok().getLoc().getPointer();
862
863 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000864 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Nirav Davefd910412016-06-17 16:06:17 +0000865 Lexer.Lex();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000866
867 const char *End = getTok().getLoc().getPointer();
868 return StringRef(Start, End - Start);
869}
870
Jim Grosbach4b905842013-09-20 23:08:21 +0000871/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000872/// NOTE: This assumes the leading '(' has already been consumed.
873///
874/// parenexpr ::= expr)
875///
Jim Grosbach4b905842013-09-20 23:08:21 +0000876bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
877 if (parseExpression(Res))
878 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000879 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000880 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000881 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000882 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000883 return false;
884}
Chris Lattner78db3622009-06-22 05:51:26 +0000885
Jim Grosbach4b905842013-09-20 23:08:21 +0000886/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000887/// NOTE: This assumes the leading '[' has already been consumed.
888///
889/// bracketexpr ::= expr]
890///
Jim Grosbach4b905842013-09-20 23:08:21 +0000891bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
892 if (parseExpression(Res))
893 return true;
Nirav Davea645433c2016-07-18 15:24:03 +0000894 EndLoc = getTok().getEndLoc();
895 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
896 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000897 return false;
898}
899
Jim Grosbach4b905842013-09-20 23:08:21 +0000900/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000901/// primaryexpr ::= (parenexpr
902/// primaryexpr ::= symbol
903/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000904/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000905/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000906bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000907 SMLoc FirstTokenLoc = getLexer().getLoc();
908 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
909 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000910 default:
911 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000912 // If we have an error assume that we've already handled it.
913 case AsmToken::Error:
914 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000915 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000916 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000918 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000919 Res = MCUnaryExpr::createLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000921 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000922 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000923 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000924 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000925 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000926 if (parseIdentifier(Identifier)) {
Nirav Daved0463322016-10-12 13:58:07 +0000927 // We may have failed but $ may be a valid token.
928 if (getTok().is(AsmToken::Dollar)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000929 if (Lexer.getMAI().getDollarIsPC()) {
Nirav Daved0463322016-10-12 13:58:07 +0000930 Lex();
David Majnemer0c58bc62013-09-25 10:47:21 +0000931 // This is a '$' reference, which references the current PC. Emit a
932 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +0000933 MCSymbol *Sym = Ctx.createTempSymbol();
David Majnemer0c58bc62013-09-25 10:47:21 +0000934 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000935 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Jack Carter721726a2013-10-04 21:26:15 +0000936 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000937 EndLoc = FirstTokenLoc;
938 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000939 }
940 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000941 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000942 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000943 // Parse symbol variant
944 std::pair<StringRef, StringRef> Split;
945 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000946 if (FirstTokenKind == AsmToken::String) {
947 if (Lexer.is(AsmToken::At)) {
Nirav Davefd910412016-06-17 16:06:17 +0000948 Lex(); // eat @
David Majnemer6a5b8122014-06-19 01:25:43 +0000949 SMLoc AtLoc = getLexer().getLoc();
950 StringRef VName;
951 if (parseIdentifier(VName))
952 return Error(AtLoc, "expected symbol variant after '@'");
953
954 Split = std::make_pair(Identifier, VName);
955 }
956 } else {
957 Split = Identifier.split('@');
958 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000959 } else if (Lexer.is(AsmToken::LParen)) {
Nirav Davefd910412016-06-17 16:06:17 +0000960 Lex(); // eat '('.
David Peixotto8ad70b32013-12-04 22:43:20 +0000961 StringRef VName;
962 parseIdentifier(VName);
Nirav Davea645433c2016-07-18 15:24:03 +0000963 // eat ')'.
964 if (parseToken(AsmToken::RParen,
965 "unexpected token in variant, expected ')'"))
966 return true;
David Peixotto8ad70b32013-12-04 22:43:20 +0000967 Split = std::make_pair(Identifier, VName);
968 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000969
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000970 EndLoc = SMLoc::getFromPointer(Identifier.end());
971
Daniel Dunbard20cda02009-10-16 01:34:54 +0000972 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000973 StringRef SymbolName = Identifier;
974 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000975
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000976 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000977 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000978 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000979 if (Variant != MCSymbolRefExpr::VK_Invalid) {
980 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000981 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000982 Variant = MCSymbolRefExpr::VK_None;
983 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000984 return Error(SMLoc::getFromPointer(Split.second.begin()),
985 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 }
987 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000988
Jim Grosbach6f482002015-05-18 18:43:14 +0000989 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
Hans Wennborgce69d772013-10-18 20:46:28 +0000990
Daniel Dunbard20cda02009-10-16 01:34:54 +0000991 // If this is an absolute variable reference, substitute it now to preserve
992 // semantics in the face of reassignment.
Vedant Kumar86dbd922015-08-31 17:44:53 +0000993 if (Sym->isVariable() &&
994 isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000995 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000996 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000997
Vedant Kumar86dbd922015-08-31 17:44:53 +0000998 Res = Sym->getVariableValue(/*SetUsed*/ false);
Daniel Dunbard20cda02009-10-16 01:34:54 +0000999 return false;
1000 }
1001
1002 // Otherwise create a symbol ref.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001003 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +00001004 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +00001005 }
David Woodhousef42a6662014-02-01 16:20:54 +00001006 case AsmToken::BigNum:
1007 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +00001008 case AsmToken::Integer: {
1009 SMLoc Loc = getTok().getLoc();
1010 int64_t IntVal = getTok().getIntVal();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001011 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001012 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001013 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +00001014 // Look for 'b' or 'f' following an Integer as a directional label
1015 if (Lexer.getKind() == AsmToken::Identifier) {
1016 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +00001017 // Lookup the symbol variant if used.
1018 std::pair<StringRef, StringRef> Split = IDVal.split('@');
1019 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1020 if (Split.first.size() != IDVal.size()) {
1021 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001022 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +00001023 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001024 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +00001025 }
Jim Grosbach4b905842013-09-20 23:08:21 +00001026 if (IDVal == "f" || IDVal == "b") {
1027 MCSymbol *Sym =
Jim Grosbach6f482002015-05-18 18:43:14 +00001028 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
Jim Grosbach13760bd2015-05-30 01:25:56 +00001029 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00001030 if (IDVal == "b" && Sym->isUndefined())
Tim Northover6b3169b2016-04-11 19:50:46 +00001031 return Error(Loc, "directional label undefined");
Tim Northoverc0bef992016-04-13 19:46:54 +00001032 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001033 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +00001034 Lex(); // Eat identifier.
1035 }
1036 }
Chris Lattner78db3622009-06-22 05:51:26 +00001037 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +00001038 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001039 case AsmToken::Real: {
1040 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +00001041 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001042 Res = MCConstantExpr::create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001043 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +00001044 Lex(); // Eat token.
1045 return false;
1046 }
Chris Lattner6b55cb92010-04-14 04:40:28 +00001047 case AsmToken::Dot: {
1048 // This is a '.' reference, which references the current PC. Emit a
1049 // temporary label to the streamer and refer to it.
Jim Grosbach6f482002015-05-18 18:43:14 +00001050 MCSymbol *Sym = Ctx.createTempSymbol();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001051 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001052 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001053 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +00001054 Lex(); // Eat identifier.
1055 return false;
1056 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001057 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001058 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +00001059 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +00001060 case AsmToken::LBrac:
1061 if (!PlatformParser->HasBracketExpressions())
1062 return TokError("brackets expression not supported on this target");
1063 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +00001064 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001065 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001066 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001069 Res = MCUnaryExpr::createMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001070 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001071 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001072 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001073 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001074 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001075 Res = MCUnaryExpr::createPlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001076 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +00001078 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +00001079 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001080 return true;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001081 Res = MCUnaryExpr::createNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001082 return false;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +00001083 // MIPS unary expression operators. The lexer won't generate these tokens if
1084 // MCAsmInfo::HasMipsExpressions is false for the target.
1085 case AsmToken::PercentCall16:
1086 case AsmToken::PercentCall_Hi:
1087 case AsmToken::PercentCall_Lo:
1088 case AsmToken::PercentDtprel_Hi:
1089 case AsmToken::PercentDtprel_Lo:
1090 case AsmToken::PercentGot:
1091 case AsmToken::PercentGot_Disp:
1092 case AsmToken::PercentGot_Hi:
1093 case AsmToken::PercentGot_Lo:
1094 case AsmToken::PercentGot_Ofst:
1095 case AsmToken::PercentGot_Page:
1096 case AsmToken::PercentGottprel:
1097 case AsmToken::PercentGp_Rel:
1098 case AsmToken::PercentHi:
1099 case AsmToken::PercentHigher:
1100 case AsmToken::PercentHighest:
1101 case AsmToken::PercentLo:
1102 case AsmToken::PercentNeg:
1103 case AsmToken::PercentPcrel_Hi:
1104 case AsmToken::PercentPcrel_Lo:
1105 case AsmToken::PercentTlsgd:
1106 case AsmToken::PercentTlsldm:
1107 case AsmToken::PercentTprel_Hi:
1108 case AsmToken::PercentTprel_Lo:
1109 Lex(); // Eat the operator.
1110 if (Lexer.isNot(AsmToken::LParen))
1111 return TokError("expected '(' after operator");
1112 Lex(); // Eat the operator.
1113 if (parseExpression(Res, EndLoc))
1114 return true;
1115 if (Lexer.isNot(AsmToken::RParen))
1116 return TokError("expected ')'");
1117 Lex(); // Eat the operator.
1118 Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
1119 return !Res;
Chris Lattner78db3622009-06-22 05:51:26 +00001120 }
1121}
Chris Lattner7fdbce72009-06-22 06:32:03 +00001122
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001123bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +00001124 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001125 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +00001126}
1127
Daniel Dunbar55f16672010-09-17 02:47:07 +00001128const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +00001129AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +00001130 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +00001131 // Ask the target implementation about this expression first.
1132 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1133 if (NewE)
1134 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001135 // Recurse over the given expression, rebuilding it to apply the given variant
1136 // if there is exactly one symbol.
1137 switch (E->getKind()) {
1138 case MCExpr::Target:
1139 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +00001140 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001141
1142 case MCExpr::SymbolRef: {
1143 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1144
1145 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001146 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1147 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001148 return E;
1149 }
1150
Jim Grosbach13760bd2015-05-30 01:25:56 +00001151 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001152 }
1153
1154 case MCExpr::Unary: {
1155 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001156 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001157 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +00001158 return nullptr;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001159 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001160 }
1161
1162 case MCExpr::Binary: {
1163 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1165 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001166
1167 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001168 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001169
Jim Grosbach4b905842013-09-20 23:08:21 +00001170 if (!LHS)
1171 LHS = BE->getLHS();
1172 if (!RHS)
1173 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001174
Jim Grosbach13760bd2015-05-30 01:25:56 +00001175 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001176 }
1177 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001178
Craig Toppera2886c22012-02-07 05:05:23 +00001179 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001180}
1181
Jim Grosbach4b905842013-09-20 23:08:21 +00001182/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001183///
Jim Grosbachbd164242011-08-20 16:24:13 +00001184/// expr ::= expr &&,|| expr -> lowest.
1185/// expr ::= expr |,^,&,! expr
1186/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1187/// expr ::= expr <<,>> expr
1188/// expr ::= expr +,- expr
1189/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001190/// expr ::= primaryexpr
1191///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001192bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001193 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001194 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001195 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001196 return true;
1197
Daniel Dunbar55f16672010-09-17 02:47:07 +00001198 // As a special case, we support 'a op b @ modifier' by rewriting the
1199 // expression to include the modifier. This is inefficient, but in general we
1200 // expect users to use 'a@modifier op b'.
1201 if (Lexer.getKind() == AsmToken::At) {
1202 Lex();
1203
1204 if (Lexer.isNot(AsmToken::Identifier))
1205 return TokError("unexpected symbol modifier following '@'");
1206
1207 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001208 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001209 if (Variant == MCSymbolRefExpr::VK_Invalid)
1210 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1211
Jim Grosbach4b905842013-09-20 23:08:21 +00001212 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001213 if (!ModifiedRes) {
1214 return TokError("invalid modifier '" + getTok().getIdentifier() +
1215 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001216 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001217
Daniel Dunbar55f16672010-09-17 02:47:07 +00001218 Res = ModifiedRes;
1219 Lex();
1220 }
1221
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001222 // Try to constant fold it up front, if possible.
1223 int64_t Value;
Jim Grosbach13760bd2015-05-30 01:25:56 +00001224 if (Res->evaluateAsAbsolute(Value))
1225 Res = MCConstantExpr::create(Value, getContext());
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001226
1227 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001228}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001229
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001230bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001231 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001232 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001233}
1234
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001235bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1236 SMLoc &EndLoc) {
1237 if (parseParenExpr(Res, EndLoc))
1238 return true;
1239
1240 for (; ParenDepth > 0; --ParenDepth) {
1241 if (parseBinOpRHS(1, Res, EndLoc))
1242 return true;
1243
1244 // We don't Lex() the last RParen.
1245 // This is the same behavior as parseParenExpression().
1246 if (ParenDepth - 1 > 0) {
Nirav Davea645433c2016-07-18 15:24:03 +00001247 EndLoc = getTok().getEndLoc();
1248 if (parseToken(AsmToken::RParen,
1249 "expected ')' in parentheses expression"))
1250 return true;
Toma Tabacu7bc44dc2015-06-25 09:52:02 +00001251 }
1252 }
1253 return false;
1254}
1255
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001256bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001257 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001258
Daniel Dunbar75630b32009-06-30 02:10:03 +00001259 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001260 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001261 return true;
1262
Jim Grosbach13760bd2015-05-30 01:25:56 +00001263 if (!Expr->evaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001264 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001265
1266 return false;
1267}
1268
David Majnemer0993e0b2015-10-26 03:15:34 +00001269static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1270 MCBinaryExpr::Opcode &Kind,
1271 bool ShouldUseLogicalShr) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001272 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001273 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001274 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001275
Jim Grosbach4b905842013-09-20 23:08:21 +00001276 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001277 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001278 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001279 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001280 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001281 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001282 return 1;
1283
Jim Grosbach4b905842013-09-20 23:08:21 +00001284 // Low Precedence: |, &, ^
1285 //
1286 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001287 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001288 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001289 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001290 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001291 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001292 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001293 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001294 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001295 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001296
Jim Grosbach4b905842013-09-20 23:08:21 +00001297 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001298 case AsmToken::EqualEqual:
1299 Kind = MCBinaryExpr::EQ;
1300 return 3;
1301 case AsmToken::ExclaimEqual:
1302 case AsmToken::LessGreater:
1303 Kind = MCBinaryExpr::NE;
1304 return 3;
1305 case AsmToken::Less:
1306 Kind = MCBinaryExpr::LT;
1307 return 3;
1308 case AsmToken::LessEqual:
1309 Kind = MCBinaryExpr::LTE;
1310 return 3;
1311 case AsmToken::Greater:
1312 Kind = MCBinaryExpr::GT;
1313 return 3;
1314 case AsmToken::GreaterEqual:
1315 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001316 return 3;
1317
Jim Grosbach4b905842013-09-20 23:08:21 +00001318 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001319 case AsmToken::LessLess:
1320 Kind = MCBinaryExpr::Shl;
1321 return 4;
1322 case AsmToken::GreaterGreater:
David Majnemer0993e0b2015-10-26 03:15:34 +00001323 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
Jim Grosbachbd164242011-08-20 16:24:13 +00001324 return 4;
1325
Jim Grosbach4b905842013-09-20 23:08:21 +00001326 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001327 case AsmToken::Plus:
1328 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001329 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001330 case AsmToken::Minus:
1331 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001332 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001333
Jim Grosbach4b905842013-09-20 23:08:21 +00001334 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001335 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001336 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001337 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001338 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001339 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001340 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001341 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001342 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001343 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001344 }
1345}
1346
David Majnemer0993e0b2015-10-26 03:15:34 +00001347static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1348 MCBinaryExpr::Opcode &Kind,
1349 bool ShouldUseLogicalShr) {
1350 switch (K) {
1351 default:
1352 return 0; // not a binop.
1353
1354 // Lowest Precedence: &&, ||
1355 case AsmToken::AmpAmp:
1356 Kind = MCBinaryExpr::LAnd;
1357 return 2;
1358 case AsmToken::PipePipe:
1359 Kind = MCBinaryExpr::LOr;
1360 return 1;
1361
1362 // Low Precedence: ==, !=, <>, <, <=, >, >=
1363 case AsmToken::EqualEqual:
1364 Kind = MCBinaryExpr::EQ;
1365 return 3;
1366 case AsmToken::ExclaimEqual:
1367 case AsmToken::LessGreater:
1368 Kind = MCBinaryExpr::NE;
1369 return 3;
1370 case AsmToken::Less:
1371 Kind = MCBinaryExpr::LT;
1372 return 3;
1373 case AsmToken::LessEqual:
1374 Kind = MCBinaryExpr::LTE;
1375 return 3;
1376 case AsmToken::Greater:
1377 Kind = MCBinaryExpr::GT;
1378 return 3;
1379 case AsmToken::GreaterEqual:
1380 Kind = MCBinaryExpr::GTE;
1381 return 3;
1382
1383 // Low Intermediate Precedence: +, -
1384 case AsmToken::Plus:
1385 Kind = MCBinaryExpr::Add;
1386 return 4;
1387 case AsmToken::Minus:
1388 Kind = MCBinaryExpr::Sub;
1389 return 4;
1390
1391 // High Intermediate Precedence: |, &, ^
1392 //
1393 // FIXME: gas seems to support '!' as an infix operator?
1394 case AsmToken::Pipe:
1395 Kind = MCBinaryExpr::Or;
1396 return 5;
1397 case AsmToken::Caret:
1398 Kind = MCBinaryExpr::Xor;
1399 return 5;
1400 case AsmToken::Amp:
1401 Kind = MCBinaryExpr::And;
1402 return 5;
1403
1404 // Highest Precedence: *, /, %, <<, >>
1405 case AsmToken::Star:
1406 Kind = MCBinaryExpr::Mul;
1407 return 6;
1408 case AsmToken::Slash:
1409 Kind = MCBinaryExpr::Div;
1410 return 6;
1411 case AsmToken::Percent:
1412 Kind = MCBinaryExpr::Mod;
1413 return 6;
1414 case AsmToken::LessLess:
1415 Kind = MCBinaryExpr::Shl;
1416 return 6;
1417 case AsmToken::GreaterGreater:
1418 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1419 return 6;
1420 }
1421}
1422
1423unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1424 MCBinaryExpr::Opcode &Kind) {
1425 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1426 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1427 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1428}
1429
Jim Grosbach4b905842013-09-20 23:08:21 +00001430/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001431/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001432bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001433 SMLoc &EndLoc) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001434 while (true) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001435 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001436 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001437
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001438 // If the next token is lower precedence than we are allowed to eat, return
1439 // successfully with what we ate already.
1440 if (TokPrec < Precedence)
1441 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001442
Sean Callanan686ed8d2010-01-19 20:22:31 +00001443 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001444
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001445 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001446 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001447 if (parsePrimaryExpr(RHS, EndLoc))
1448 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001449
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001450 // If BinOp binds less tightly with RHS than the operator after RHS, let
1451 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001452 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001453 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001454 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1455 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001456
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001457 // Merge LHS and RHS according to operator.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001458 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001459 }
1460}
1461
Chris Lattner36e02122009-06-21 20:54:55 +00001462/// ParseStatement:
1463/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001464/// ::= Label* Directive ...Operands... EndOfStatement
1465/// ::= Label* Identifier OperandList* EndOfStatement
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001466bool AsmParser::parseStatement(ParseStatementInfo &Info,
1467 MCAsmParserSemaCallback *SI) {
Nirav Dave2364748a2016-09-16 18:30:20 +00001468 assert(!hasPendingError() && "parseStatement started with pending error");
Nirav Davefd910412016-06-17 16:06:17 +00001469 // Eat initial spaces and comments
1470 while (Lexer.is(AsmToken::Space))
1471 Lex();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001472 if (Lexer.is(AsmToken::EndOfStatement)) {
Nirav Davefd910412016-06-17 16:06:17 +00001473 // if this is a line comment we can drop it safely
1474 if (getTok().getString().front() == '\r' ||
1475 getTok().getString().front() == '\n')
1476 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001477 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001478 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001479 }
Nirav Dave9263ae32016-08-02 19:17:54 +00001480 if (Lexer.is(AsmToken::Hash)) {
1481 // Seeing a hash here means that it was an end-of-line comment in
1482 // an asm syntax where hash's are not comment and the previous
1483 // statement parser did not check the end of statement. Relex as
1484 // EndOfStatement.
1485 StringRef CommentStr = parseStringToEndOfStatement();
1486 Lexer.Lex();
1487 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1488 return false;
1489 }
Nirav Davefd910412016-06-17 16:06:17 +00001490 // Statements always start with an identifier.
Sean Callanan936b0d32010-01-19 21:44:56 +00001491 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001492 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001493 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001494 int64_t LocalLabelVal = -1;
Nirav Davefd910412016-06-17 16:06:17 +00001495 if (Lexer.is(AsmToken::HashDirective))
Jim Grosbach4b905842013-09-20 23:08:21 +00001496 return parseCppHashLineFilenameComment(IDLoc);
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001497 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001498 if (Lexer.is(AsmToken::Integer)) {
1499 LocalLabelVal = getTok().getIntVal();
1500 if (LocalLabelVal < 0) {
Nirav Dave2364748a2016-09-16 18:30:20 +00001501 if (!TheCondState.Ignore) {
1502 Lex(); // always eat a token
1503 return Error(IDLoc, "unexpected token at start of statement");
1504 }
Kevin Enderby0510b482010-05-17 23:08:19 +00001505 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001506 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001507 IDVal = getTok().getString();
1508 Lex(); // Consume the integer token to be used as an identifier token.
1509 if (Lexer.getKind() != AsmToken::Colon) {
Nirav Dave2364748a2016-09-16 18:30:20 +00001510 if (!TheCondState.Ignore) {
1511 Lex(); // always eat a token
1512 return Error(IDLoc, "unexpected token at start of statement");
1513 }
Kevin Enderby0510b482010-05-17 23:08:19 +00001514 }
1515 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001516 } else if (Lexer.is(AsmToken::Dot)) {
1517 // Treat '.' as a valid identifier in this context.
1518 Lex();
1519 IDVal = ".";
Colin LeMahieu8a0453e2015-11-09 00:31:07 +00001520 } else if (Lexer.is(AsmToken::LCurly)) {
1521 // Treat '{' as a valid identifier in this context.
1522 Lex();
1523 IDVal = "{";
1524
1525 } else if (Lexer.is(AsmToken::RCurly)) {
1526 // Treat '}' as a valid identifier in this context.
1527 Lex();
1528 IDVal = "}";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001529 } else if (parseIdentifier(IDVal)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00001530 if (!TheCondState.Ignore) {
1531 Lex(); // always eat a token
1532 return Error(IDLoc, "unexpected token at start of statement");
1533 }
Chris Lattner926885c2010-04-17 18:14:27 +00001534 IDVal = "";
1535 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001536
Chris Lattner926885c2010-04-17 18:14:27 +00001537 // Handle conditional assembly here before checking for skipping. We
1538 // have to do this so that .endif isn't skipped in a ".if 0" block for
1539 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001540 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001541 DirectiveKindMap.find(IDVal);
1542 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1543 ? DK_NO_DIRECTIVE
1544 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001545 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 default:
1547 break;
1548 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001549 case DK_IFEQ:
1550 case DK_IFGE:
1551 case DK_IFGT:
1552 case DK_IFLE:
1553 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001554 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001555 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001556 case DK_IFB:
1557 return parseDirectiveIfb(IDLoc, true);
1558 case DK_IFNB:
1559 return parseDirectiveIfb(IDLoc, false);
1560 case DK_IFC:
1561 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001562 case DK_IFEQS:
Sid Manning51c35602015-03-18 14:20:54 +00001563 return parseDirectiveIfeqs(IDLoc, true);
Jim Grosbach4b905842013-09-20 23:08:21 +00001564 case DK_IFNC:
1565 return parseDirectiveIfc(IDLoc, false);
Sid Manning51c35602015-03-18 14:20:54 +00001566 case DK_IFNES:
1567 return parseDirectiveIfeqs(IDLoc, false);
Jim Grosbach4b905842013-09-20 23:08:21 +00001568 case DK_IFDEF:
1569 return parseDirectiveIfdef(IDLoc, true);
1570 case DK_IFNDEF:
1571 case DK_IFNOTDEF:
1572 return parseDirectiveIfdef(IDLoc, false);
1573 case DK_ELSEIF:
1574 return parseDirectiveElseIf(IDLoc);
1575 case DK_ELSE:
1576 return parseDirectiveElse(IDLoc);
1577 case DK_ENDIF:
1578 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001579 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001580
Eli Bendersky88024712013-01-16 19:32:36 +00001581 // Ignore the statement if in the middle of inactive conditional
1582 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001583 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001584 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001585 return false;
1586 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001587
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001588 // FIXME: Recurse on local labels?
1589
1590 // See what kind of statement we have.
1591 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001592 case AsmToken::Colon: {
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001593 if (!getTargetParser().isLabel(ID))
1594 break;
Nirav Davef43cc9f2016-10-10 15:24:54 +00001595 if (checkForValidSection())
1596 return true;
Daniel Dunbare5444a82010-09-09 22:42:59 +00001597
Chris Lattner36e02122009-06-21 20:54:55 +00001598 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001599 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001600
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001601 // Diagnose attempt to use '.' as a label.
1602 if (IDVal == ".")
1603 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1604
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001605 // Diagnose attempt to use a variable as a label.
1606 //
1607 // FIXME: Diagnostics. Note the location of the definition as a label.
1608 // FIXME: This doesn't diagnose assignment to a symbol which has been
1609 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001610 MCSymbol *Sym;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001611 if (LocalLabelVal == -1) {
1612 if (ParsingInlineAsm && SI) {
Nico Weber67e715f2015-06-19 23:43:47 +00001613 StringRef RewrittenLabel =
1614 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1615 assert(RewrittenLabel.size() &&
1616 "We should have an internal name here.");
Craig Topper7d5b2312015-10-10 05:25:02 +00001617 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1618 RewrittenLabel);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001619 IDVal = RewrittenLabel;
1620 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001621 Sym = getContext().getOrCreateSymbol(IDVal);
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00001622 } else
Jim Grosbach6f482002015-05-18 18:43:14 +00001623 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
David Majnemer58cb80c2014-12-24 10:27:50 +00001624
1625 Sym->redefineIfPossible();
1626
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001627 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001628 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001629
Nirav Dave9263ae32016-08-02 19:17:54 +00001630 // End of Labels should be treated as end of line for lexing
1631 // purposes but that information is not available to the Lexer who
1632 // does not understand Labels. This may cause us to see a Hash
1633 // here instead of a preprocessor line comment.
1634 if (getTok().is(AsmToken::Hash)) {
1635 StringRef CommentStr = parseStringToEndOfStatement();
1636 Lexer.Lex();
1637 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1638 }
1639
Nirav Dave8ea792d2016-07-13 14:03:12 +00001640 // Consume any end of statement token, if present, to avoid spurious
1641 // AddBlankLine calls().
1642 if (getTok().is(AsmToken::EndOfStatement)) {
1643 Lex();
1644 }
1645
Daniel Dunbare73b2672009-08-26 22:13:22 +00001646 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001647 if (!ParsingInlineAsm)
1648 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001649
Kevin Enderbye7739d42011-12-09 18:09:40 +00001650 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001651 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001652 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001653 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1654 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001655
Tim Northover1744d0a2013-10-25 12:49:50 +00001656 getTargetParser().onLabelParsed(Sym);
1657
Eli Friedman0f4871d2012-10-22 23:58:19 +00001658 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001659 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001660
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001661 case AsmToken::Equal:
Colin LeMahieu7820dff2015-11-09 00:15:45 +00001662 if (!getTargetParser().equalIsAsmAssignment())
1663 break;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001664 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001665 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001666
Jim Grosbach4b905842013-09-20 23:08:21 +00001667 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001668
1669 default: // Normal instruction or directive.
1670 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001671 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001672
1673 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001674 if (areMacrosEnabled())
1675 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1676 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001677 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001678
Michael J. Spencer530ce852010-10-09 11:00:50 +00001679 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001680
Eli Bendersky17233942013-01-15 22:59:42 +00001681 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001682 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001683 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001684 //
Eli Bendersky17233942013-01-15 22:59:42 +00001685 // 1. The target-specific assembly parser. Some directives are target
1686 // specific or may potentially behave differently on certain targets.
1687 // 2. Asm parser extensions. For example, platform-specific parsers
1688 // (like the ELF parser) register themselves as extensions.
1689 // 3. The generic directive parser implemented by this class. These are
1690 // all the directives that behave in a target and platform independent
1691 // manner, or at least have a default behavior that's shared between
1692 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001693
Oliver Stannard21718282016-07-26 14:19:47 +00001694 getTargetParser().flushPendingInstructions(getStreamer());
1695
Nirav Dave2364748a2016-09-16 18:30:20 +00001696 SMLoc StartTokLoc = getTok().getLoc();
1697 bool TPDirectiveReturn = getTargetParser().ParseDirective(ID);
1698
1699 if (hasPendingError())
1700 return true;
1701 // Currently the return value should be true if we are
1702 // uninterested but as this is at odds with the standard parsing
1703 // convention (return true = error) we have instances of a parsed
1704 // directive that fails returning true as an error. Catch these
1705 // cases as best as possible errors here.
1706 if (TPDirectiveReturn && StartTokLoc != getTok().getLoc())
1707 return true;
1708 // Return if we did some parsing or believe we succeeded.
1709 if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc())
Akira Hatanakad3590752012-07-05 19:09:33 +00001710 return false;
1711
Alp Tokercb402912014-01-24 17:20:08 +00001712 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001713 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1715 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001716 if (Handler.first)
1717 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1718
1719 // Finally, if no one else is interested in this directive, it must be
1720 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001721 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 default:
1723 break;
1724 case DK_SET:
1725 case DK_EQU:
1726 return parseDirectiveSet(IDVal, true);
1727 case DK_EQUIV:
1728 return parseDirectiveSet(IDVal, false);
1729 case DK_ASCII:
1730 return parseDirectiveAscii(IDVal, false);
1731 case DK_ASCIZ:
1732 case DK_STRING:
1733 return parseDirectiveAscii(IDVal, true);
1734 case DK_BYTE:
1735 return parseDirectiveValue(1);
1736 case DK_SHORT:
1737 case DK_VALUE:
1738 case DK_2BYTE:
1739 return parseDirectiveValue(2);
1740 case DK_LONG:
1741 case DK_INT:
1742 case DK_4BYTE:
1743 return parseDirectiveValue(4);
1744 case DK_QUAD:
1745 case DK_8BYTE:
1746 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001747 case DK_OCTA:
1748 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001749 case DK_SINGLE:
1750 case DK_FLOAT:
1751 return parseDirectiveRealValue(APFloat::IEEEsingle);
1752 case DK_DOUBLE:
1753 return parseDirectiveRealValue(APFloat::IEEEdouble);
1754 case DK_ALIGN: {
1755 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1756 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1757 }
1758 case DK_ALIGN32: {
1759 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1760 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1761 }
1762 case DK_BALIGN:
1763 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1764 case DK_BALIGNW:
1765 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1766 case DK_BALIGNL:
1767 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1768 case DK_P2ALIGN:
1769 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1770 case DK_P2ALIGNW:
1771 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1772 case DK_P2ALIGNL:
1773 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1774 case DK_ORG:
1775 return parseDirectiveOrg();
1776 case DK_FILL:
1777 return parseDirectiveFill();
1778 case DK_ZERO:
1779 return parseDirectiveZero();
1780 case DK_EXTERN:
1781 eatToEndOfStatement(); // .extern is the default, ignore it.
1782 return false;
1783 case DK_GLOBL:
1784 case DK_GLOBAL:
1785 return parseDirectiveSymbolAttribute(MCSA_Global);
1786 case DK_LAZY_REFERENCE:
1787 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1788 case DK_NO_DEAD_STRIP:
1789 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1790 case DK_SYMBOL_RESOLVER:
1791 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1792 case DK_PRIVATE_EXTERN:
1793 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1794 case DK_REFERENCE:
1795 return parseDirectiveSymbolAttribute(MCSA_Reference);
1796 case DK_WEAK_DEFINITION:
1797 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1798 case DK_WEAK_REFERENCE:
1799 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1800 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1801 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1802 case DK_COMM:
1803 case DK_COMMON:
1804 return parseDirectiveComm(/*IsLocal=*/false);
1805 case DK_LCOMM:
1806 return parseDirectiveComm(/*IsLocal=*/true);
1807 case DK_ABORT:
1808 return parseDirectiveAbort();
1809 case DK_INCLUDE:
1810 return parseDirectiveInclude();
1811 case DK_INCBIN:
1812 return parseDirectiveIncbin();
1813 case DK_CODE16:
1814 case DK_CODE16GCC:
Nirav Davefd910412016-06-17 16:06:17 +00001815 return TokError(Twine(IDVal) +
1816 " not currently supported for this target");
Jim Grosbach4b905842013-09-20 23:08:21 +00001817 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001818 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001819 case DK_IRP:
1820 return parseDirectiveIrp(IDLoc);
1821 case DK_IRPC:
1822 return parseDirectiveIrpc(IDLoc);
1823 case DK_ENDR:
1824 return parseDirectiveEndr(IDLoc);
1825 case DK_BUNDLE_ALIGN_MODE:
1826 return parseDirectiveBundleAlignMode();
1827 case DK_BUNDLE_LOCK:
1828 return parseDirectiveBundleLock();
1829 case DK_BUNDLE_UNLOCK:
1830 return parseDirectiveBundleUnlock();
1831 case DK_SLEB128:
1832 return parseDirectiveLEB128(true);
1833 case DK_ULEB128:
1834 return parseDirectiveLEB128(false);
1835 case DK_SPACE:
1836 case DK_SKIP:
1837 return parseDirectiveSpace(IDVal);
1838 case DK_FILE:
1839 return parseDirectiveFile(IDLoc);
1840 case DK_LINE:
1841 return parseDirectiveLine();
1842 case DK_LOC:
1843 return parseDirectiveLoc();
1844 case DK_STABS:
1845 return parseDirectiveStabs();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001846 case DK_CV_FILE:
1847 return parseDirectiveCVFile();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00001848 case DK_CV_FUNC_ID:
1849 return parseDirectiveCVFuncId();
1850 case DK_CV_INLINE_SITE_ID:
1851 return parseDirectiveCVInlineSiteId();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001852 case DK_CV_LOC:
1853 return parseDirectiveCVLoc();
1854 case DK_CV_LINETABLE:
1855 return parseDirectiveCVLinetable();
David Majnemer6fcbd7e2016-01-29 19:24:12 +00001856 case DK_CV_INLINE_LINETABLE:
1857 return parseDirectiveCVInlineLinetable();
David Majnemer408b5e62016-02-05 01:55:49 +00001858 case DK_CV_DEF_RANGE:
1859 return parseDirectiveCVDefRange();
Reid Kleckner2214ed82016-01-29 00:49:42 +00001860 case DK_CV_STRINGTABLE:
1861 return parseDirectiveCVStringTable();
1862 case DK_CV_FILECHECKSUMS:
1863 return parseDirectiveCVFileChecksums();
Jim Grosbach4b905842013-09-20 23:08:21 +00001864 case DK_CFI_SECTIONS:
1865 return parseDirectiveCFISections();
1866 case DK_CFI_STARTPROC:
1867 return parseDirectiveCFIStartProc();
1868 case DK_CFI_ENDPROC:
1869 return parseDirectiveCFIEndProc();
1870 case DK_CFI_DEF_CFA:
1871 return parseDirectiveCFIDefCfa(IDLoc);
1872 case DK_CFI_DEF_CFA_OFFSET:
1873 return parseDirectiveCFIDefCfaOffset();
1874 case DK_CFI_ADJUST_CFA_OFFSET:
1875 return parseDirectiveCFIAdjustCfaOffset();
1876 case DK_CFI_DEF_CFA_REGISTER:
1877 return parseDirectiveCFIDefCfaRegister(IDLoc);
1878 case DK_CFI_OFFSET:
1879 return parseDirectiveCFIOffset(IDLoc);
1880 case DK_CFI_REL_OFFSET:
1881 return parseDirectiveCFIRelOffset(IDLoc);
1882 case DK_CFI_PERSONALITY:
1883 return parseDirectiveCFIPersonalityOrLsda(true);
1884 case DK_CFI_LSDA:
1885 return parseDirectiveCFIPersonalityOrLsda(false);
1886 case DK_CFI_REMEMBER_STATE:
1887 return parseDirectiveCFIRememberState();
1888 case DK_CFI_RESTORE_STATE:
1889 return parseDirectiveCFIRestoreState();
1890 case DK_CFI_SAME_VALUE:
1891 return parseDirectiveCFISameValue(IDLoc);
1892 case DK_CFI_RESTORE:
1893 return parseDirectiveCFIRestore(IDLoc);
1894 case DK_CFI_ESCAPE:
1895 return parseDirectiveCFIEscape();
1896 case DK_CFI_SIGNAL_FRAME:
1897 return parseDirectiveCFISignalFrame();
1898 case DK_CFI_UNDEFINED:
1899 return parseDirectiveCFIUndefined(IDLoc);
1900 case DK_CFI_REGISTER:
1901 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001902 case DK_CFI_WINDOW_SAVE:
1903 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001904 case DK_MACROS_ON:
1905 case DK_MACROS_OFF:
1906 return parseDirectiveMacrosOnOff(IDVal);
1907 case DK_MACRO:
1908 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001909 case DK_EXITM:
1910 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001911 case DK_ENDM:
1912 case DK_ENDMACRO:
1913 return parseDirectiveEndMacro(IDVal);
1914 case DK_PURGEM:
1915 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001916 case DK_END:
1917 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001918 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001919 return parseDirectiveError(IDLoc, false);
1920 case DK_ERROR:
1921 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001922 case DK_WARNING:
1923 return parseDirectiveWarning(IDLoc);
Daniel Sanders9f6ad492015-11-12 13:33:00 +00001924 case DK_RELOC:
1925 return parseDirectiveReloc(IDLoc);
Petr Hosek731bb9c2016-08-23 21:34:53 +00001926 case DK_DC:
1927 return parseDirectiveValue(2);
1928 case DK_DC_A:
1929 return parseDirectiveValue(getContext().getAsmInfo()->getPointerSize());
1930 case DK_DC_B:
1931 return parseDirectiveValue(1);
1932 case DK_DC_D:
1933 return parseDirectiveRealValue(APFloat::IEEEdouble);
1934 case DK_DC_L:
1935 return parseDirectiveValue(4);
1936 case DK_DC_S:
1937 return parseDirectiveRealValue(APFloat::IEEEsingle);
1938 case DK_DC_W:
1939 return parseDirectiveValue(2);
Petr Hosek4cb08ce2016-09-23 19:25:15 +00001940 case DK_DCB:
1941 case DK_DCB_W:
1942 return parseDirectiveDCB(IDVal, 2);
1943 case DK_DCB_B:
1944 return parseDirectiveDCB(IDVal, 1);
1945 case DK_DCB_D:
1946 return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble);
1947 case DK_DCB_L:
1948 return parseDirectiveDCB(IDVal, 4);
1949 case DK_DCB_S:
1950 return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle);
Petr Hosek731bb9c2016-08-23 21:34:53 +00001951 case DK_DC_X:
Petr Hosek4cb08ce2016-09-23 19:25:15 +00001952 case DK_DCB_X:
Petr Hosek731bb9c2016-08-23 21:34:53 +00001953 return TokError(Twine(IDVal) +
1954 " not currently supported for this target");
Petr Hosek85b2f672016-09-23 21:53:36 +00001955 case DK_DS:
1956 case DK_DS_W:
1957 return parseDirectiveDS(IDVal, 2);
1958 case DK_DS_B:
1959 return parseDirectiveDS(IDVal, 1);
1960 case DK_DS_D:
1961 return parseDirectiveDS(IDVal, 8);
1962 case DK_DS_L:
1963 case DK_DS_S:
1964 return parseDirectiveDS(IDVal, 4);
1965 case DK_DS_P:
1966 case DK_DS_X:
1967 return parseDirectiveDS(IDVal, 12);
Eli Friedman20b02642010-07-19 04:17:25 +00001968 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001969
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001970 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001971 }
Chris Lattner36e02122009-06-21 20:54:55 +00001972
Chad Rosierc7f552c2013-02-12 21:33:51 +00001973 // __asm _emit or __asm __emit
1974 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1975 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001976 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001977
1978 // __asm align
1979 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001980 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001981
Michael Zuckerman02ecd432015-12-13 17:07:23 +00001982 if (ParsingInlineAsm && (IDVal == "even"))
1983 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
Nirav Davef43cc9f2016-10-10 15:24:54 +00001984 if (checkForValidSection())
1985 return true;
Daniel Dunbare5444a82010-09-09 22:42:59 +00001986
Chris Lattner7cbfa442010-05-19 23:34:33 +00001987 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001988 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001989 ParseInstructionInfo IInfo(Info.AsmRewrites);
Nirav Dave2364748a2016-09-16 18:30:20 +00001990 bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
1991 Info.ParsedOperands);
1992 Info.ParseError = ParseHadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001993
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001994 // Dump the parsed representation, if requested.
1995 if (getShowParsedOperands()) {
1996 SmallString<256> Str;
1997 raw_svector_ostream OS(Str);
1998 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001999 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00002000 if (i != 0)
2001 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00002002 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00002003 }
2004 OS << "]";
2005
Jim Grosbach4b905842013-09-20 23:08:21 +00002006 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00002007 }
2008
Nirav Dave2364748a2016-09-16 18:30:20 +00002009 // Fail even if ParseInstruction erroneously returns false.
2010 if (hasPendingError() || ParseHadError)
2011 return true;
2012
Oliver Stannard8b273082014-06-19 15:52:37 +00002013 // If we are generating dwarf for the current section then generate a .loc
2014 // directive for the instruction.
Nirav Dave2364748a2016-09-16 18:30:20 +00002015 if (!ParseHadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00002016 getContext().getGenDwarfSectionSyms().count(
Saleem Abdulrasool4d6ed7c2014-12-24 06:32:43 +00002017 getStreamer().getCurrentSection().first)) {
2018 unsigned Line;
2019 if (ActiveMacros.empty())
2020 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
2021 else
Frederic Riss16238d92015-06-25 21:57:33 +00002022 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
2023 ActiveMacros.front()->ExitBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00002024
Eli Bendersky88024712013-01-16 19:32:36 +00002025 // If we previously parsed a cpp hash file line comment then make sure the
2026 // current Dwarf File is for the CppHashFilename if not then emit the
2027 // Dwarf File table for it and adjust the line number for the .loc.
Tim Northoverc0bef992016-04-13 19:46:54 +00002028 if (CppHashInfo.Filename.size()) {
David Blaikiec714ef42014-03-17 01:52:11 +00002029 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
Tim Northoverc0bef992016-04-13 19:46:54 +00002030 0, StringRef(), CppHashInfo.Filename);
David Blaikiec714ef42014-03-17 01:52:11 +00002031 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00002032
Jim Grosbach4b905842013-09-20 23:08:21 +00002033 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
2034 // cache with the different Loc from the call above we save the last
2035 // info we queried here with SrcMgr.FindLineNumber().
2036 unsigned CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00002037 if (LastQueryIDLoc == CppHashInfo.Loc &&
2038 LastQueryBuffer == CppHashInfo.Buf)
Jim Grosbach4b905842013-09-20 23:08:21 +00002039 CppHashLocLineNo = LastQueryLine;
2040 else {
Tim Northoverc0bef992016-04-13 19:46:54 +00002041 CppHashLocLineNo =
2042 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
Jim Grosbach4b905842013-09-20 23:08:21 +00002043 LastQueryLine = CppHashLocLineNo;
Tim Northoverc0bef992016-04-13 19:46:54 +00002044 LastQueryIDLoc = CppHashInfo.Loc;
2045 LastQueryBuffer = CppHashInfo.Buf;
Jim Grosbach4b905842013-09-20 23:08:21 +00002046 }
Tim Northoverc0bef992016-04-13 19:46:54 +00002047 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00002048 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00002049
Jim Grosbach4b905842013-09-20 23:08:21 +00002050 getStreamer().EmitDwarfLocDirective(
2051 getContext().getGenDwarfFileNumber(), Line, 0,
2052 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
2053 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00002054 }
2055
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00002056 // If parsing succeeded, match the instruction.
Nirav Dave2364748a2016-09-16 18:30:20 +00002057 if (!ParseHadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00002058 uint64_t ErrorInfo;
Nirav Dave2364748a2016-09-16 18:30:20 +00002059 if (getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
2060 Info.ParsedOperands, Out,
2061 ErrorInfo, ParsingInlineAsm))
2062 return true;
Chad Rosier49963552012-10-13 00:26:04 +00002063 }
Chris Lattnera2a9d162010-09-11 16:18:25 +00002064 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00002065}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00002066
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00002067// Parse and erase curly braces marking block start/end
2068bool
2069AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
2070 // Identify curly brace marking block start/end
2071 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
2072 return false;
2073
2074 SMLoc StartLoc = Lexer.getLoc();
2075 Lex(); // Eat the brace
2076 if (Lexer.is(AsmToken::EndOfStatement))
2077 Lex(); // Eat EndOfStatement following the brace
2078
2079 // Erase the block start/end brace from the output asm string
2080 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
2081 StartLoc.getPointer());
2082 return true;
2083}
2084
Jim Grosbach4b905842013-09-20 23:08:21 +00002085/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00002086/// ::= # number "filename"
Craig Topper3c76c522015-09-20 23:35:59 +00002087bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Kevin Enderby72553612011-09-13 23:45:18 +00002088 Lex(); // Eat the hash token.
Nirav Davefd910412016-06-17 16:06:17 +00002089 // Lexer only ever emits HashDirective if it fully formed if it's
2090 // done the checking already so this is an internal error.
2091 assert(getTok().is(AsmToken::Integer) &&
2092 "Lexing Cpp line comment: Expected Integer");
Kevin Enderby72553612011-09-13 23:45:18 +00002093 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00002094 Lex();
Nirav Davefd910412016-06-17 16:06:17 +00002095 assert(getTok().is(AsmToken::String) &&
2096 "Lexing Cpp line comment: Expected String");
Kevin Enderby72553612011-09-13 23:45:18 +00002097 StringRef Filename = getTok().getString();
Nirav Davefd910412016-06-17 16:06:17 +00002098 Lex();
Nirav Dave2364748a2016-09-16 18:30:20 +00002099
Kevin Enderby72553612011-09-13 23:45:18 +00002100 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00002101 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00002102
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002103 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
Tim Northoverc0bef992016-04-13 19:46:54 +00002104 CppHashInfo.Loc = L;
2105 CppHashInfo.Filename = Filename;
2106 CppHashInfo.LineNumber = LineNumber;
2107 CppHashInfo.Buf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00002108 return false;
2109}
2110
Jim Grosbach4b905842013-09-20 23:08:21 +00002111/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002112/// for the Filename and LineNo if any in the diagnostic.
2113void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002114 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002115 raw_ostream &OS = errs();
2116
2117 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
Craig Topper3c76c522015-09-20 23:35:59 +00002118 SMLoc DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00002119 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2120 unsigned CppHashBuf =
Tim Northoverc0bef992016-04-13 19:46:54 +00002121 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002122
Jim Grosbach4b905842013-09-20 23:08:21 +00002123 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002124 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00002125 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2126 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
2127 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002128 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
2129 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002130 }
2131
Eric Christophera7c32732012-12-18 00:30:54 +00002132 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002133 // manager changed or buffer changed (like in a nested include) then just
2134 // print the normal diagnostic using its Filename and LineNo.
Tim Northoverc0bef992016-04-13 19:46:54 +00002135 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002136 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00002137 if (Parser->SavedDiagHandler)
2138 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2139 else
Craig Topper353eda42014-04-24 06:44:33 +00002140 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002141 return;
2142 }
2143
Eric Christophera7c32732012-12-18 00:30:54 +00002144 // Use the CppHashFilename and calculate a line number based on the
Tim Northoverc0bef992016-04-13 19:46:54 +00002145 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
2146 // for the diagnostic.
2147 const std::string &Filename = Parser->CppHashInfo.Filename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002148
2149 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
2150 int CppHashLocLineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00002151 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00002152 int LineNo =
Tim Northoverc0bef992016-04-13 19:46:54 +00002153 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002154
Jim Grosbach4b905842013-09-20 23:08:21 +00002155 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2156 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00002157 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002158
Benjamin Kramer47f5e302011-10-16 10:48:29 +00002159 if (Parser->SavedDiagHandler)
2160 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
2161 else
Craig Topper353eda42014-04-24 06:44:33 +00002162 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00002163}
2164
Rafael Espindola2c064482012-08-21 18:29:30 +00002165// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
2166// difference being that that function accepts '@' as part of identifiers and
2167// we can't do that. AsmLexer.cpp should probably be changed to handle
2168// '@' as a special case when needed.
2169static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00002170 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
2171 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00002172}
2173
Rafael Espindola34b9c512012-06-03 23:57:14 +00002174bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00002175 ArrayRef<MCAsmMacroParameter> Parameters,
Toma Tabacu217116e2015-04-27 10:50:29 +00002176 ArrayRef<MCAsmMacroArgument> A,
Craig Topper3c76c522015-09-20 23:35:59 +00002177 bool EnableAtPseudoVariable, SMLoc L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002178 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002179 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00002180 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00002181 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002182
Preston Gurd05500642012-09-19 20:36:12 +00002183 // A macro without parameters is handled differently on Darwin:
2184 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002185 while (!Body.empty()) {
2186 // Scan for the next substitution.
2187 std::size_t End = Body.size(), Pos = 0;
2188 for (; Pos != End; ++Pos) {
2189 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00002190 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00002191 // This macro has no parameters, look for $0, $1, etc.
2192 if (Body[Pos] != '$' || Pos + 1 == End)
2193 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002194
Rafael Espindola1134ab232011-06-05 02:43:45 +00002195 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00002196 if (Next == '$' || Next == 'n' ||
2197 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002198 break;
2199 } else {
2200 // This macro has parameters, look for \foo, \bar, etc.
2201 if (Body[Pos] == '\\' && Pos + 1 != End)
2202 break;
2203 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002204 }
2205
2206 // Add the prefix.
2207 OS << Body.slice(0, Pos);
2208
2209 // Check if we reached the end.
2210 if (Pos == End)
2211 break;
2212
Benjamin Kramer513e7442014-02-20 13:36:32 +00002213 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002214 switch (Body[Pos + 1]) {
2215 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00002216 case '$':
2217 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002218 break;
2219
Jim Grosbach4b905842013-09-20 23:08:21 +00002220 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00002221 case 'n':
2222 OS << A.size();
2223 break;
2224
Jim Grosbach4b905842013-09-20 23:08:21 +00002225 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00002226 default: {
2227 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00002228 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00002229 if (Index >= A.size())
2230 break;
2231
2232 // Otherwise substitute with the token values, with spaces eliminated.
Craig Topper84008482015-10-10 05:38:14 +00002233 for (const AsmToken &Token : A[Index])
2234 OS << Token.getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00002235 break;
2236 }
2237 }
2238 Pos += 2;
2239 } else {
2240 unsigned I = Pos + 1;
Toma Tabacu217116e2015-04-27 10:50:29 +00002241
2242 // Check for the \@ pseudo-variable.
2243 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00002244 ++I;
Toma Tabacu217116e2015-04-27 10:50:29 +00002245 else
2246 while (isIdentifierChar(Body[I]) && I + 1 != End)
2247 ++I;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002248
Jim Grosbach4b905842013-09-20 23:08:21 +00002249 const char *Begin = Body.data() + Pos + 1;
2250 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002251 unsigned Index = 0;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002252
Toma Tabacu217116e2015-04-27 10:50:29 +00002253 if (Argument == "@") {
2254 OS << NumOfMacroInstantiations;
2255 Pos += 2;
Preston Gurd05500642012-09-19 20:36:12 +00002256 } else {
Toma Tabacu217116e2015-04-27 10:50:29 +00002257 for (; Index < NParameters; ++Index)
2258 if (Parameters[Index].Name == Argument)
2259 break;
Rafael Espindola1134ab232011-06-05 02:43:45 +00002260
Toma Tabacu217116e2015-04-27 10:50:29 +00002261 if (Index == NParameters) {
2262 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2263 Pos += 3;
2264 else {
2265 OS << '\\' << Argument;
2266 Pos = I;
2267 }
2268 } else {
2269 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Craig Topper84008482015-10-10 05:38:14 +00002270 for (const AsmToken &Token : A[Index])
Toma Tabacu217116e2015-04-27 10:50:29 +00002271 // We expect no quotes around the string's contents when
2272 // parsing for varargs.
Craig Topper84008482015-10-10 05:38:14 +00002273 if (Token.getKind() != AsmToken::String || VarargParameter)
2274 OS << Token.getString();
Toma Tabacu217116e2015-04-27 10:50:29 +00002275 else
Craig Topper84008482015-10-10 05:38:14 +00002276 OS << Token.getStringContents();
Toma Tabacu217116e2015-04-27 10:50:29 +00002277
2278 Pos += 1 + Argument.size();
2279 }
Preston Gurd05500642012-09-19 20:36:12 +00002280 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00002281 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002282 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00002283 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00002284 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002285
Rafael Espindola1134ab232011-06-05 02:43:45 +00002286 return false;
2287}
Daniel Dunbar43235712010-07-18 18:54:11 +00002288
Nico Weber2a8f9222014-07-24 16:29:04 +00002289MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002290 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002291 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00002292 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00002293
Jim Grosbach4b905842013-09-20 23:08:21 +00002294static bool isOperator(AsmToken::TokenKind kind) {
2295 switch (kind) {
2296 default:
2297 return false;
2298 case AsmToken::Plus:
2299 case AsmToken::Minus:
2300 case AsmToken::Tilde:
2301 case AsmToken::Slash:
2302 case AsmToken::Star:
2303 case AsmToken::Dot:
2304 case AsmToken::Equal:
2305 case AsmToken::EqualEqual:
2306 case AsmToken::Pipe:
2307 case AsmToken::PipePipe:
2308 case AsmToken::Caret:
2309 case AsmToken::Amp:
2310 case AsmToken::AmpAmp:
2311 case AsmToken::Exclaim:
2312 case AsmToken::ExclaimEqual:
Jim Grosbach4b905842013-09-20 23:08:21 +00002313 case AsmToken::Less:
2314 case AsmToken::LessEqual:
2315 case AsmToken::LessLess:
2316 case AsmToken::LessGreater:
2317 case AsmToken::Greater:
2318 case AsmToken::GreaterEqual:
2319 case AsmToken::GreaterGreater:
2320 return true;
Preston Gurd05500642012-09-19 20:36:12 +00002321 }
2322}
2323
David Majnemer16252452014-01-29 00:07:39 +00002324namespace {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002325
David Majnemer16252452014-01-29 00:07:39 +00002326class AsmLexerSkipSpaceRAII {
2327public:
2328 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2329 Lexer.setSkipSpace(SkipSpace);
2330 }
2331
2332 ~AsmLexerSkipSpaceRAII() {
2333 Lexer.setSkipSpace(true);
2334 }
2335
2336private:
2337 AsmLexer &Lexer;
2338};
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002339
2340} // end anonymous namespace
David Majnemer16252452014-01-29 00:07:39 +00002341
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002342bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2343
2344 if (Vararg) {
2345 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2346 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002347 MA.emplace_back(AsmToken::String, Str);
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002348 }
2349 return false;
2350 }
2351
Rafael Espindola768b41c2012-06-15 14:02:34 +00002352 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00002353
David Majnemer16252452014-01-29 00:07:39 +00002354 // Darwin doesn't use spaces to delmit arguments.
2355 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00002356
Scott Egertona1fa68a2016-02-11 13:48:49 +00002357 bool SpaceEaten;
2358
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002359 while (true) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002360 SpaceEaten = false;
David Majnemer16252452014-01-29 00:07:39 +00002361 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002362 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00002363
Scott Egertona1fa68a2016-02-11 13:48:49 +00002364 if (ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00002365
Scott Egertona1fa68a2016-02-11 13:48:49 +00002366 if (Lexer.is(AsmToken::Comma))
2367 break;
2368
2369 if (Lexer.is(AsmToken::Space)) {
2370 SpaceEaten = true;
Nirav Dave1180e6892016-06-02 17:15:05 +00002371 Lexer.Lex(); // Eat spaces
Scott Egertona1fa68a2016-02-11 13:48:49 +00002372 }
Preston Gurd05500642012-09-19 20:36:12 +00002373
2374 // Spaces can delimit parameters, but could also be part an expression.
2375 // If the token after a space is an operator, add the token and the next
2376 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00002377 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00002378 if (isOperator(Lexer.getKind())) {
Scott Egertona1fa68a2016-02-11 13:48:49 +00002379 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002380 Lexer.Lex();
Preston Gurd05500642012-09-19 20:36:12 +00002381
Scott Egertona1fa68a2016-02-11 13:48:49 +00002382 // Whitespace after an operator can be ignored.
2383 if (Lexer.is(AsmToken::Space))
Nirav Dave1180e6892016-06-02 17:15:05 +00002384 Lexer.Lex();
Scott Egertona1fa68a2016-02-11 13:48:49 +00002385
2386 continue;
Preston Gurd05500642012-09-19 20:36:12 +00002387 }
2388 }
Scott Egertona1fa68a2016-02-11 13:48:49 +00002389 if (SpaceEaten)
2390 break;
Preston Gurd05500642012-09-19 20:36:12 +00002391 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002392
Jim Grosbach4b905842013-09-20 23:08:21 +00002393 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00002394 // to be able to fill in the remaining default parameter values
2395 if (Lexer.is(AsmToken::EndOfStatement))
2396 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002397
2398 // Adjust the current parentheses level.
2399 if (Lexer.is(AsmToken::LParen))
2400 ++ParenLevel;
2401 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2402 --ParenLevel;
2403
2404 // Append the token to the current argument list.
2405 MA.push_back(getTok());
Nirav Dave1180e6892016-06-02 17:15:05 +00002406 Lexer.Lex();
Rafael Espindola768b41c2012-06-15 14:02:34 +00002407 }
Preston Gurd05500642012-09-19 20:36:12 +00002408
Rafael Espindola768b41c2012-06-15 14:02:34 +00002409 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00002410 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002411 return false;
2412}
2413
2414// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00002415bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00002416 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00002417 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002418 bool NamedParametersFound = false;
2419 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002420
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002421 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002422 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002423
Rafael Espindola768b41c2012-06-15 14:02:34 +00002424 // Parse two kinds of macro invocations:
2425 // - macros defined without any parameters accept an arbitrary number of them
2426 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002427 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002428 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2429 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002430 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002431 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002432
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002433 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00002434 if (parseIdentifier(FA.Name))
2435 return Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002436
Nirav Dave2364748a2016-09-16 18:30:20 +00002437 if (Lexer.isNot(AsmToken::Equal))
2438 return TokError("expected '=' after formal parameter identifier");
2439
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002440 Lex();
2441
2442 NamedParametersFound = true;
2443 }
2444
Nirav Dave2364748a2016-09-16 18:30:20 +00002445 if (NamedParametersFound && FA.Name.empty())
2446 return Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002447
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002448 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2449 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002450 return true;
2451
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002452 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002453 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002454 unsigned FAI = 0;
2455 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002456 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002457 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002458
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002459 if (FAI >= NParameters) {
Nirav Davefd910412016-06-17 16:06:17 +00002460 assert(M && "expected macro to be defined");
Nirav Dave2364748a2016-09-16 18:30:20 +00002461 return Error(IDLoc, "parameter named '" + FA.Name +
2462 "' does not exist for macro '" + M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002463 }
2464 PI = FAI;
2465 }
2466
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002467 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002468 if (A.size() <= PI)
2469 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002470 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002471
2472 if (FALocs.size() <= PI)
2473 FALocs.resize(PI + 1);
2474
2475 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002476 }
Jim Grosbach206661622012-07-30 22:44:17 +00002477
Preston Gurd242ed3152012-09-19 20:29:04 +00002478 // At the end of the statement, fill in remaining arguments that have
2479 // default values. If there aren't any, then the next argument is
2480 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002481 if (Lexer.is(AsmToken::EndOfStatement)) {
2482 bool Failure = false;
2483 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2484 if (A[FAI].empty()) {
2485 if (M->Parameters[FAI].Required) {
2486 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2487 "missing value for required parameter "
2488 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2489 Failure = true;
2490 }
2491
2492 if (!M->Parameters[FAI].Value.empty())
2493 A[FAI] = M->Parameters[FAI].Value;
2494 }
2495 }
2496 return Failure;
2497 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002498
2499 if (Lexer.is(AsmToken::Comma))
2500 Lex();
2501 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002502
2503 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002504}
2505
Jim Grosbach4b905842013-09-20 23:08:21 +00002506const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002507 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
2508 return (I == MacroMap.end()) ? nullptr : &I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002509}
2510
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002511void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
2512 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
Eli Bendersky38274122013-01-14 23:22:36 +00002513}
2514
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00002515void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
Eli Bendersky38274122013-01-14 23:22:36 +00002516
Jim Grosbach4b905842013-09-20 23:08:21 +00002517bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Davide Italiano7c9fc732016-07-27 05:51:56 +00002518 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2519 // eliminate this, although we should protect against infinite loops.
2520 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2521 if (ActiveMacros.size() == MaxNestingDepth) {
2522 std::ostringstream MaxNestingDepthError;
2523 MaxNestingDepthError << "macros cannot be nested more than "
2524 << MaxNestingDepth << " levels deep."
2525 << " Use -asm-macro-max-nesting-depth to increase "
2526 "this limit.";
2527 return TokError(MaxNestingDepthError.str());
2528 }
Daniel Dunbar43235712010-07-18 18:54:11 +00002529
Eli Bendersky38274122013-01-14 23:22:36 +00002530 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002531 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002532 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002533
Rafael Espindola1134ab232011-06-05 02:43:45 +00002534 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2535 // to hold the macro body with substitutions.
2536 SmallString<256> Buf;
2537 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002538 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002539
Toma Tabacu217116e2015-04-27 10:50:29 +00002540 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002541 return true;
2542
Eli Bendersky38274122013-01-14 23:22:36 +00002543 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002544 // instantiation.
2545 OS << ".endmacro\n";
2546
Rafael Espindola3560ff22014-08-27 20:03:13 +00002547 std::unique_ptr<MemoryBuffer> Instantiation =
2548 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002549
Daniel Dunbar43235712010-07-18 18:54:11 +00002550 // Create the macro instantiation object and add to the current macro
2551 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002552 MacroInstantiation *MI = new MacroInstantiation(
2553 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002554 ActiveMacros.push_back(MI);
2555
Toma Tabacu217116e2015-04-27 10:50:29 +00002556 ++NumOfMacroInstantiations;
2557
Daniel Dunbar43235712010-07-18 18:54:11 +00002558 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002559 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002560 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002561 Lex();
2562
2563 return false;
2564}
2565
Jim Grosbach4b905842013-09-20 23:08:21 +00002566void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002567 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002568 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002569 Lex();
2570
2571 // Pop the instantiation entry.
2572 delete ActiveMacros.back();
2573 ActiveMacros.pop_back();
2574}
2575
Jim Grosbach4b905842013-09-20 23:08:21 +00002576bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002577 bool NoDeadStrip) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00002578 MCSymbol *Sym;
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002579 const MCExpr *Value;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002580 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2581 Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002582 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002583
Pete Cooper80d21cb2015-06-22 19:35:57 +00002584 if (!Sym) {
2585 // In the case where we parse an expression starting with a '.', we will
2586 // not generate an error, nor will we create a symbol. In this case we
2587 // should just return out.
Anders Waldenborg84809572014-02-17 20:48:32 +00002588 return false;
Pete Cooper80d21cb2015-06-22 19:35:57 +00002589 }
David Majnemer58cb80c2014-12-24 10:27:50 +00002590
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002591 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002592 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002593 if (NoDeadStrip)
2594 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2595
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002596 return false;
2597}
2598
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002599/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002600/// ::= identifier
2601/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002602bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002603 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002604 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2605 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002606 // handle this as a context dependent token, instead we detect adjacent tokens
2607 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002608 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2609 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002610
Hans Wennborgce69d772013-10-18 20:46:28 +00002611 // Consume the prefix character, and check for a following identifier.
Nirav Daved0463322016-10-12 13:58:07 +00002612
2613 AsmToken Buf[1];
2614 Lexer.peekTokens(Buf, false);
2615
2616 if (Buf[0].isNot(AsmToken::Identifier))
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002617 return true;
2618
Hans Wennborgce69d772013-10-18 20:46:28 +00002619 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
Nirav Daved0463322016-10-12 13:58:07 +00002620 if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002621 return true;
2622
Nirav Daved0463322016-10-12 13:58:07 +00002623 // eat $ or @
2624 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002625 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002626 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002627 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Nirav Davefd910412016-06-17 16:06:17 +00002628 Lex(); // Parser Lex to maintain invariants.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002629 return false;
2630 }
2631
Jim Grosbach4b905842013-09-20 23:08:21 +00002632 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002633 return true;
2634
Sean Callanan936b0d32010-01-19 21:44:56 +00002635 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002636
Sean Callanan686ed8d2010-01-19 20:22:31 +00002637 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002638
2639 return false;
2640}
2641
Jim Grosbach4b905842013-09-20 23:08:21 +00002642/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002643/// ::= .equ identifier ',' expression
2644/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002645/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002646bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002647 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002648
Nirav Davea645433c2016-07-18 15:24:03 +00002649 if (check(parseIdentifier(Name),
2650 "expected identifier after '" + Twine(IDVal) + "'") ||
2651 parseToken(AsmToken::Comma, "unexpected token in '" + Twine(IDVal) + "'"))
2652 return true;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002653
Jim Grosbach4b905842013-09-20 23:08:21 +00002654 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002655}
2656
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002657bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002658 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002659
2660 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002661 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002662 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2663 if (Str[i] != '\\') {
2664 Data += Str[i];
2665 continue;
2666 }
2667
2668 // Recognize escaped characters. Note that this escape semantics currently
2669 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2670 ++i;
2671 if (i == e)
2672 return TokError("unexpected backslash at end of string");
2673
2674 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002675 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002676 // Consume up to three octal characters.
2677 unsigned Value = Str[i] - '0';
2678
Jim Grosbach4b905842013-09-20 23:08:21 +00002679 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002680 ++i;
2681 Value = Value * 8 + (Str[i] - '0');
2682
Jim Grosbach4b905842013-09-20 23:08:21 +00002683 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002684 ++i;
2685 Value = Value * 8 + (Str[i] - '0');
2686 }
2687 }
2688
2689 if (Value > 255)
2690 return TokError("invalid octal escape sequence (out of range)");
2691
Jim Grosbach4b905842013-09-20 23:08:21 +00002692 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002693 continue;
2694 }
2695
2696 // Otherwise recognize individual escapes.
2697 switch (Str[i]) {
2698 default:
2699 // Just reject invalid escape sequences for now.
2700 return TokError("invalid escape sequence (unrecognized character)");
2701
2702 case 'b': Data += '\b'; break;
2703 case 'f': Data += '\f'; break;
2704 case 'n': Data += '\n'; break;
2705 case 'r': Data += '\r'; break;
2706 case 't': Data += '\t'; break;
2707 case '"': Data += '"'; break;
2708 case '\\': Data += '\\'; break;
2709 }
2710 }
2711
Nirav Davea645433c2016-07-18 15:24:03 +00002712 Lex();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002713 return false;
2714}
2715
Jim Grosbach4b905842013-09-20 23:08:21 +00002716/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002717/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002718bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002719 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00002720 if (checkForValidSection())
2721 return true;
Daniel Dunbare5444a82010-09-09 22:42:59 +00002722
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002723 while (true) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002724 std::string Data;
Nirav Davea645433c2016-07-18 15:24:03 +00002725 if (check(getTok().isNot(AsmToken::String),
2726 "expected string in '" + Twine(IDVal) + "' directive") ||
2727 parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002728 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002729
Rafael Espindola64e1af82013-07-02 15:49:13 +00002730 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002731 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002732 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002733
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002734 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002735 break;
2736
Nirav Davea645433c2016-07-18 15:24:03 +00002737 if (parseToken(AsmToken::Comma,
2738 "unexpected token in '" + Twine(IDVal) + "' directive"))
2739 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002740 }
2741 }
2742
Sean Callanan686ed8d2010-01-19 20:22:31 +00002743 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002744 return false;
2745}
2746
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002747/// parseDirectiveReloc
2748/// ::= .reloc expression , identifier [ , expression ]
2749bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
2750 const MCExpr *Offset;
2751 const MCExpr *Expr = nullptr;
2752
2753 SMLoc OffsetLoc = Lexer.getTok().getLoc();
2754 if (parseExpression(Offset))
2755 return true;
2756
2757 // We can only deal with constant expressions at the moment.
2758 int64_t OffsetValue;
Nirav Davea645433c2016-07-18 15:24:03 +00002759 if (check(!Offset->evaluateAsAbsolute(OffsetValue), OffsetLoc,
2760 "expression is not a constant value") ||
2761 check(OffsetValue < 0, OffsetLoc, "expression is negative") ||
2762 parseToken(AsmToken::Comma, "expected comma") ||
2763 check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
2764 return true;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002765
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002766 SMLoc NameLoc = Lexer.getTok().getLoc();
2767 StringRef Name = Lexer.getTok().getIdentifier();
Nirav Davefd910412016-06-17 16:06:17 +00002768 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002769
2770 if (Lexer.is(AsmToken::Comma)) {
Nirav Davefd910412016-06-17 16:06:17 +00002771 Lex();
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002772 SMLoc ExprLoc = Lexer.getLoc();
2773 if (parseExpression(Expr))
2774 return true;
2775
2776 MCValue Value;
2777 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
2778 return Error(ExprLoc, "expression must be relocatable");
2779 }
2780
Nirav Davea645433c2016-07-18 15:24:03 +00002781 if (parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00002782 "unexpected token in .reloc directive"))
2783 return true;
2784
2785 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc))
2786 return Error(NameLoc, "unknown relocation name");
2787
Daniel Sanders9f6ad492015-11-12 13:33:00 +00002788 return false;
2789}
2790
Jim Grosbach4b905842013-09-20 23:08:21 +00002791/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002792/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002793bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002794 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00002795 if (checkForValidSection())
2796 return true;
Daniel Dunbare5444a82010-09-09 22:42:59 +00002797
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002798 while (true) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002799 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002800 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002801 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002802 return true;
2803
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002804 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002805 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2806 assert(Size <= 8 && "Invalid size");
2807 uint64_t IntValue = MCE->getValue();
2808 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2809 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002810 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002811 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002812 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002813
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002814 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002815 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002816
Daniel Dunbara10e5192009-06-24 23:30:00 +00002817 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002818 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2819 return true;
Daniel Dunbara10e5192009-06-24 23:30:00 +00002820 }
2821 }
2822
Sean Callanan686ed8d2010-01-19 20:22:31 +00002823 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002824 return false;
2825}
2826
David Woodhoused6de0d92014-02-01 16:20:59 +00002827/// ParseDirectiveOctaValue
2828/// ::= .octa [ hexconstant (, hexconstant)* ]
2829bool AsmParser::parseDirectiveOctaValue() {
2830 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00002831 if (checkForValidSection())
2832 return true;
David Woodhoused6de0d92014-02-01 16:20:59 +00002833
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002834 while (true) {
Nirav Davea645433c2016-07-18 15:24:03 +00002835 if (getTok().is(AsmToken::Error))
David Woodhoused6de0d92014-02-01 16:20:59 +00002836 return true;
Nirav Davea645433c2016-07-18 15:24:03 +00002837 if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::BigNum))
David Woodhoused6de0d92014-02-01 16:20:59 +00002838 return TokError("unknown token in expression");
2839
2840 SMLoc ExprLoc = getLexer().getLoc();
2841 APInt IntValue = getTok().getAPIntVal();
2842 Lex();
2843
2844 uint64_t hi, lo;
2845 if (IntValue.isIntN(64)) {
2846 hi = 0;
2847 lo = IntValue.getZExtValue();
2848 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002849 // It might actually have more than 128 bits, but the top ones are zero.
2850 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002851 lo = IntValue.getLoBits(64).getZExtValue();
2852 } else
2853 return Error(ExprLoc, "literal value out of range for directive");
2854
2855 if (MAI.isLittleEndian()) {
2856 getStreamer().EmitIntValue(lo, 8);
2857 getStreamer().EmitIntValue(hi, 8);
2858 } else {
2859 getStreamer().EmitIntValue(hi, 8);
2860 getStreamer().EmitIntValue(lo, 8);
2861 }
2862
2863 if (getLexer().is(AsmToken::EndOfStatement))
2864 break;
2865
2866 // FIXME: Improve diagnostic.
Nirav Davea645433c2016-07-18 15:24:03 +00002867 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2868 return true;
David Woodhoused6de0d92014-02-01 16:20:59 +00002869 }
2870 }
2871
2872 Lex();
2873 return false;
2874}
2875
Petr Hosek4cb08ce2016-09-23 19:25:15 +00002876bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
2877 // We don't truly support arithmetic on floating point expressions, so we
2878 // have to manually parse unary prefixes.
2879 bool IsNeg = false;
2880 if (getLexer().is(AsmToken::Minus)) {
2881 Lexer.Lex();
2882 IsNeg = true;
2883 } else if (getLexer().is(AsmToken::Plus))
2884 Lexer.Lex();
2885
2886 if (Lexer.is(AsmToken::Error))
2887 return TokError(Lexer.getErr());
2888 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
2889 Lexer.isNot(AsmToken::Identifier))
2890 return TokError("unexpected token in directive");
2891
2892 // Convert to an APFloat.
2893 APFloat Value(Semantics);
2894 StringRef IDVal = getTok().getString();
2895 if (getLexer().is(AsmToken::Identifier)) {
2896 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2897 Value = APFloat::getInf(Semantics);
2898 else if (!IDVal.compare_lower("nan"))
2899 Value = APFloat::getNaN(Semantics, false, ~0);
2900 else
2901 return TokError("invalid floating point literal");
2902 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
2903 APFloat::opInvalidOp)
2904 return TokError("invalid floating point literal");
2905 if (IsNeg)
2906 Value.changeSign();
2907
2908 // Consume the numeric token.
2909 Lex();
2910
2911 Res = Value.bitcastToAPInt();
2912
2913 return false;
2914}
2915
Jim Grosbach4b905842013-09-20 23:08:21 +00002916/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002917/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002918bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002919 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00002920 if (checkForValidSection())
2921 return true;
Daniel Dunbar2af16532010-09-24 01:59:56 +00002922
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002923 while (true) {
Petr Hosek4cb08ce2016-09-23 19:25:15 +00002924 APInt AsInt;
2925 if (parseRealValue(Semantics, AsInt))
2926 return true;
Daniel Dunbar2af16532010-09-24 01:59:56 +00002927
Daniel Dunbar2af16532010-09-24 01:59:56 +00002928 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002929 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002930
Nirav Dave1180e6892016-06-02 17:15:05 +00002931 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002932 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002933
Nirav Davea645433c2016-07-18 15:24:03 +00002934 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
2935 return true;
Daniel Dunbar2af16532010-09-24 01:59:56 +00002936 }
2937 }
2938
2939 Lex();
2940 return false;
2941}
2942
Jim Grosbach4b905842013-09-20 23:08:21 +00002943/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002944/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002945bool AsmParser::parseDirectiveZero() {
Petr Hosek67a94a72016-05-28 05:57:48 +00002946 SMLoc NumBytesLoc = Lexer.getLoc();
2947 const MCExpr *NumBytes;
Nirav Davef43cc9f2016-10-10 15:24:54 +00002948 if (checkForValidSection() || parseExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002949 return true;
2950
Rafael Espindolab91bac62010-10-05 19:42:57 +00002951 int64_t Val = 0;
2952 if (getLexer().is(AsmToken::Comma)) {
2953 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002954 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002955 return true;
2956 }
2957
Nirav Davea645433c2016-07-18 15:24:03 +00002958 if (parseToken(AsmToken::EndOfStatement,
2959 "unexpected token in '.zero' directive"))
2960 return true;
Petr Hosek67a94a72016-05-28 05:57:48 +00002961 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002962
2963 return false;
2964}
2965
Jim Grosbach4b905842013-09-20 23:08:21 +00002966/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002967/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002968bool AsmParser::parseDirectiveFill() {
Petr Hosek67a94a72016-05-28 05:57:48 +00002969 SMLoc NumValuesLoc = Lexer.getLoc();
2970 const MCExpr *NumValues;
Nirav Davef43cc9f2016-10-10 15:24:54 +00002971 if (checkForValidSection() || parseExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002972 return true;
2973
Roman Divackye33098f2013-09-24 17:44:41 +00002974 int64_t FillSize = 1;
2975 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002976
David Majnemer522d3db2014-02-01 07:19:38 +00002977 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002978 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara10e5192009-06-24 23:30:00 +00002979
Nirav Davea645433c2016-07-18 15:24:03 +00002980 if (parseToken(AsmToken::Comma, "unexpected token in '.fill' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00002981 parseTokenLoc(SizeLoc) || parseAbsoluteExpression(FillSize))
Roman Divackye33098f2013-09-24 17:44:41 +00002982 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002983
Roman Divackye33098f2013-09-24 17:44:41 +00002984 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00002985 if (parseToken(AsmToken::Comma,
2986 "unexpected token in '.fill' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00002987 parseTokenLoc(ExprLoc) || parseAbsoluteExpression(FillExpr) ||
Nirav Davea645433c2016-07-18 15:24:03 +00002988 parseToken(AsmToken::EndOfStatement,
2989 "unexpected token in '.fill' directive"))
Roman Divackye33098f2013-09-24 17:44:41 +00002990 return true;
Roman Divackye33098f2013-09-24 17:44:41 +00002991 }
2992 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002993
David Majnemer522d3db2014-02-01 07:19:38 +00002994 if (FillSize < 0) {
2995 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
Petr Hosek6abd38b2016-05-28 08:20:08 +00002996 return false;
David Majnemer522d3db2014-02-01 07:19:38 +00002997 }
2998 if (FillSize > 8) {
2999 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
3000 FillSize = 8;
3001 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00003002
David Majnemer522d3db2014-02-01 07:19:38 +00003003 if (!isUInt<32>(FillExpr) && FillSize > 4)
3004 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
3005
Petr Hosek67a94a72016-05-28 05:57:48 +00003006 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00003007
3008 return false;
3009}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003010
Jim Grosbach4b905842013-09-20 23:08:21 +00003011/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003012/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003013bool AsmParser::parseDirectiveOrg() {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00003014 const MCExpr *Offset;
Nirav Davef43cc9f2016-10-10 15:24:54 +00003015 if (checkForValidSection() || parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003016 return true;
3017
3018 // Parse optional fill expression.
3019 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003020 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003021 if (parseToken(AsmToken::Comma, "unexpected token in '.org' directive") ||
3022 parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003023 return true;
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003024 }
3025
Nirav Davea645433c2016-07-18 15:24:03 +00003026 if (parseToken(AsmToken::EndOfStatement,
3027 "unexpected token in '.org' directive"))
3028 return true;
3029
Rafael Espindola7ae65d82015-11-04 23:59:18 +00003030 getStreamer().emitValueToOffset(Offset, FillExpr);
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00003031 return false;
3032}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003033
Jim Grosbach4b905842013-09-20 23:08:21 +00003034/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003035/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003037 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003038 int64_t Alignment;
Nirav Davef43cc9f2016-10-10 15:24:54 +00003039 if (checkForValidSection() || parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003040 return true;
3041
3042 SMLoc MaxBytesLoc;
3043 bool HasFillExpr = false;
3044 int64_t FillExpr = 0;
3045 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003046 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003047 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
3048 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003049
3050 // The fill expression can be omitted while specifying a maximum number of
3051 // alignment bytes, e.g:
3052 // .align 3,,4
Nirav Davea645433c2016-07-18 15:24:03 +00003053 if (getTok().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003054 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003055 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003056 return true;
3057 }
3058
Nirav Davea645433c2016-07-18 15:24:03 +00003059 if (getTok().isNot(AsmToken::EndOfStatement)) {
3060 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003061 parseTokenLoc(MaxBytesLoc) || parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003062 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003063 }
3064 }
3065
Nirav Davea645433c2016-07-18 15:24:03 +00003066 if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
3067 return true;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003068
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003069 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003070 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003071
Nirav Dave2364748a2016-09-16 18:30:20 +00003072 // Always emit an alignment here even if we thrown an error.
3073 bool ReturnVal = false;
3074
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003075 // Compute alignment in bytes.
3076 if (IsPow2) {
3077 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00003078 if (Alignment >= 32) {
Nirav Dave2364748a2016-09-16 18:30:20 +00003079 ReturnVal |= Error(AlignmentLoc, "invalid alignment value");
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00003080 Alignment = 31;
3081 }
3082
Benjamin Kramer63951ad2009-09-06 09:35:10 +00003083 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00003084 } else {
Davide Italianocb2da712015-09-08 18:59:47 +00003085 // Reject alignments that aren't either a power of two or zero,
3086 // for gas compatibility. Alignment of zero is silently rounded
3087 // up to one.
3088 if (Alignment == 0)
3089 Alignment = 1;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00003090 if (!isPowerOf2_64(Alignment))
Nirav Dave2364748a2016-09-16 18:30:20 +00003091 ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003092 }
3093
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00003094 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003095 if (MaxBytesLoc.isValid()) {
3096 if (MaxBytesToFill < 1) {
Nirav Dave2364748a2016-09-16 18:30:20 +00003097 ReturnVal |= Error(MaxBytesLoc,
3098 "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00003099 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00003100 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003101 }
3102
3103 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00003104 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00003105 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003106 MaxBytesToFill = 0;
3107 }
3108 }
3109
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003110 // Check whether we should use optimal code alignment for this .align
3111 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00003112 const MCSection *Section = getStreamer().getCurrentSection().first;
3113 assert(Section && "must have section to emit alignment");
3114 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003115 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
3116 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003117 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003118 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00003119 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00003120 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
3121 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00003122 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003123
Nirav Dave2364748a2016-09-16 18:30:20 +00003124 return ReturnVal;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00003125}
3126
Jim Grosbach4b905842013-09-20 23:08:21 +00003127/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00003128/// ::= .file [number] filename
3129/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00003130bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003131 // FIXME: I'm not sure what this is.
3132 int64_t FileNumber = -1;
3133 SMLoc FileNumberLoc = getLexer().getLoc();
3134 if (getLexer().is(AsmToken::Integer)) {
3135 FileNumber = getTok().getIntVal();
3136 Lex();
3137
3138 if (FileNumber < 1)
3139 return TokError("file number less than one");
3140 }
3141
Nirav Davea645433c2016-07-18 15:24:03 +00003142 std::string Path = getTok().getString();
Eli Bendersky17233942013-01-15 22:59:42 +00003143
3144 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003145 // Allow the strings to have escaped octal character sequence.
Nirav Davea645433c2016-07-18 15:24:03 +00003146 if (check(getTok().isNot(AsmToken::String),
3147 "unexpected token in '.file' directive") ||
3148 parseEscapedString(Path))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003149 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003150
3151 StringRef Directory;
3152 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003153 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00003154 if (getLexer().is(AsmToken::String)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003155 if (check(FileNumber == -1,
3156 "explicit path specified, but no file number") ||
3157 parseEscapedString(FilenameData))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003158 return true;
3159 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00003160 Directory = Path;
Eli Bendersky17233942013-01-15 22:59:42 +00003161 } else {
3162 Filename = Path;
3163 }
3164
Nirav Davea645433c2016-07-18 15:24:03 +00003165 if (parseToken(AsmToken::EndOfStatement,
3166 "unexpected token in '.file' directive"))
3167 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003168
3169 if (FileNumber == -1)
3170 getStreamer().EmitFileDirective(Filename);
3171 else {
David Blaikie22748082016-05-26 00:22:26 +00003172 // If there is -g option as well as debug info from directive file,
3173 // we turn off -g option, directly use the existing debug info instead.
David Blaikiedc3f01e2015-03-09 01:57:13 +00003174 if (getContext().getGenDwarfForAssembly())
David Blaikie22748082016-05-26 00:22:26 +00003175 getContext().setGenDwarfForAssembly(false);
3176 else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
David Blaikiec714ef42014-03-17 01:52:11 +00003177 0)
Nirav Dave2364748a2016-09-16 18:30:20 +00003178 return Error(FileNumberLoc, "file number already allocated");
Eli Bendersky17233942013-01-15 22:59:42 +00003179 }
3180
3181 return false;
3182}
3183
Jim Grosbach4b905842013-09-20 23:08:21 +00003184/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00003185/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00003186bool AsmParser::parseDirectiveLine() {
Nirav Davea645433c2016-07-18 15:24:03 +00003187 int64_t LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003188 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Nirav Davea645433c2016-07-18 15:24:03 +00003189 if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
3190 return true;
Jim Grosbach4b905842013-09-20 23:08:21 +00003191 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00003192 // FIXME: Do something with the .line.
3193 }
Nirav Davea645433c2016-07-18 15:24:03 +00003194 if (parseToken(AsmToken::EndOfStatement,
3195 "unexpected token in '.line' directive"))
3196 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003197
3198 return false;
3199}
3200
Jim Grosbach4b905842013-09-20 23:08:21 +00003201/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00003202/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3203/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3204/// The first number is a file number, must have been previously assigned with
3205/// a .file directive, the second number is the line number and optionally the
3206/// third number is a column position (zero if not specified). The remaining
3207/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00003208bool AsmParser::parseDirectiveLoc() {
Nirav Davea645433c2016-07-18 15:24:03 +00003209 int64_t FileNumber = 0, LineNumber = 0;
3210 SMLoc Loc = getTok().getLoc();
3211 if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
3212 check(FileNumber < 1, Loc,
3213 "file number less than one in '.loc' directive") ||
3214 check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
3215 "unassigned file number in '.loc' directive"))
3216 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003217
Nirav Davea645433c2016-07-18 15:24:03 +00003218 // optional
Eli Bendersky17233942013-01-15 22:59:42 +00003219 if (getLexer().is(AsmToken::Integer)) {
3220 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00003221 if (LineNumber < 0)
3222 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003223 Lex();
3224 }
3225
3226 int64_t ColumnPos = 0;
3227 if (getLexer().is(AsmToken::Integer)) {
3228 ColumnPos = getTok().getIntVal();
3229 if (ColumnPos < 0)
3230 return TokError("column position less than zero in '.loc' directive");
3231 Lex();
3232 }
3233
3234 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3235 unsigned Isa = 0;
3236 int64_t Discriminator = 0;
3237 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003238 while (true) {
Eli Bendersky17233942013-01-15 22:59:42 +00003239 if (getLexer().is(AsmToken::EndOfStatement))
3240 break;
3241
3242 StringRef Name;
3243 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003244 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003245 return TokError("unexpected token in '.loc' directive");
3246
3247 if (Name == "basic_block")
3248 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3249 else if (Name == "prologue_end")
3250 Flags |= DWARF2_FLAG_PROLOGUE_END;
3251 else if (Name == "epilogue_begin")
3252 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3253 else if (Name == "is_stmt") {
3254 Loc = getTok().getLoc();
3255 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003256 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003257 return true;
3258 // The expression must be the constant 0 or 1.
3259 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3260 int Value = MCE->getValue();
3261 if (Value == 0)
3262 Flags &= ~DWARF2_FLAG_IS_STMT;
3263 else if (Value == 1)
3264 Flags |= DWARF2_FLAG_IS_STMT;
3265 else
3266 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00003267 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003268 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3269 }
Craig Topperf15655b2013-04-22 04:22:40 +00003270 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00003271 Loc = getTok().getLoc();
3272 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003273 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003274 return true;
3275 // The expression must be a constant greater or equal to 0.
3276 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3277 int Value = MCE->getValue();
3278 if (Value < 0)
3279 return Error(Loc, "isa number less than zero");
3280 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00003281 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003282 return Error(Loc, "isa number not a constant value");
3283 }
Craig Topperf15655b2013-04-22 04:22:40 +00003284 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003285 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00003286 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00003287 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00003288 return Error(Loc, "unknown sub-directive in '.loc' directive");
3289 }
3290
3291 if (getLexer().is(AsmToken::EndOfStatement))
3292 break;
3293 }
3294 }
Nirav Davea645433c2016-07-18 15:24:03 +00003295 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003296
3297 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3298 Isa, Discriminator, StringRef());
3299
3300 return false;
3301}
3302
Jim Grosbach4b905842013-09-20 23:08:21 +00003303/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00003304/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00003305bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00003306 return TokError("unsupported directive '.stabs'");
3307}
3308
Reid Kleckner2214ed82016-01-29 00:49:42 +00003309/// parseDirectiveCVFile
3310/// ::= .cv_file number filename
3311bool AsmParser::parseDirectiveCVFile() {
Nirav Davea645433c2016-07-18 15:24:03 +00003312 SMLoc FileNumberLoc = getTok().getLoc();
3313 int64_t FileNumber;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003314 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00003315
3316 if (parseIntToken(FileNumber,
3317 "expected file number in '.cv_file' directive") ||
3318 check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
3319 check(getTok().isNot(AsmToken::String),
3320 "unexpected token in '.cv_file' directive") ||
3321 // Usually directory and filename are together, otherwise just
3322 // directory. Allow the strings to have escaped octal character sequence.
3323 parseEscapedString(Filename) ||
3324 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00003325 "unexpected token in '.cv_file' directive"))
Reid Klecknera5b1eef2016-08-26 17:58:37 +00003326 return true;
Nirav Dave1ab71992016-07-18 19:35:21 +00003327
Reid Klecknera5b1eef2016-08-26 17:58:37 +00003328 if (!getStreamer().EmitCVFileDirective(FileNumber, Filename))
Nirav Dave1ab71992016-07-18 19:35:21 +00003329 Error(FileNumberLoc, "file number already allocated");
Reid Kleckner2214ed82016-01-29 00:49:42 +00003330
3331 return false;
3332}
3333
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003334bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
3335 StringRef DirectiveName) {
3336 SMLoc Loc;
3337 return parseTokenLoc(Loc) ||
3338 parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
3339 "' directive") ||
3340 check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
3341 "expected function id within range [0, UINT_MAX)");
3342}
3343
3344bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
3345 SMLoc Loc;
3346 return parseTokenLoc(Loc) ||
3347 parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
3348 "' directive") ||
3349 check(FileNumber < 1, Loc, "file number less than one in '" +
3350 DirectiveName + "' directive") ||
3351 check(!getCVContext().isValidFileNumber(FileNumber), Loc,
3352 "unassigned file number in '" + DirectiveName + "' directive");
3353}
3354
3355/// parseDirectiveCVFuncId
3356/// ::= .cv_func_id FunctionId
3357///
3358/// Introduces a function ID that can be used with .cv_loc.
3359bool AsmParser::parseDirectiveCVFuncId() {
3360 SMLoc FunctionIdLoc = getTok().getLoc();
3361 int64_t FunctionId;
3362
3363 if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
3364 parseToken(AsmToken::EndOfStatement,
3365 "unexpected token in '.cv_func_id' directive"))
3366 return true;
3367
3368 if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
3369 Error(FunctionIdLoc, "function id already allocated");
3370
3371 return false;
3372}
3373
3374/// parseDirectiveCVInlineSiteId
3375/// ::= .cv_inline_site_id FunctionId
3376/// "within" IAFunc
3377/// "inlined_at" IAFile IALine [IACol]
3378///
3379/// Introduces a function ID that can be used with .cv_loc. Includes "inlined
3380/// at" source location information for use in the line table of the caller,
3381/// whether the caller is a real function or another inlined call site.
3382bool AsmParser::parseDirectiveCVInlineSiteId() {
3383 SMLoc FunctionIdLoc = getTok().getLoc();
3384 int64_t FunctionId;
3385 int64_t IAFunc;
3386 int64_t IAFile;
3387 int64_t IALine;
3388 int64_t IACol = 0;
3389
3390 // FunctionId
3391 if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
3392 return true;
3393
3394 // "within"
3395 if (check((getLexer().isNot(AsmToken::Identifier) ||
3396 getTok().getIdentifier() != "within"),
3397 "expected 'within' identifier in '.cv_inline_site_id' directive"))
3398 return true;
3399 Lex();
3400
3401 // IAFunc
3402 if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
3403 return true;
3404
3405 // "inlined_at"
3406 if (check((getLexer().isNot(AsmToken::Identifier) ||
3407 getTok().getIdentifier() != "inlined_at"),
3408 "expected 'inlined_at' identifier in '.cv_inline_site_id' "
3409 "directive") )
3410 return true;
3411 Lex();
3412
3413 // IAFile IALine
3414 if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
3415 parseIntToken(IALine, "expected line number after 'inlined_at'"))
3416 return true;
3417
3418 // [IACol]
3419 if (getLexer().is(AsmToken::Integer)) {
3420 IACol = getTok().getIntVal();
3421 Lex();
3422 }
3423
3424 if (parseToken(AsmToken::EndOfStatement,
3425 "unexpected token in '.cv_inline_site_id' directive"))
3426 return true;
3427
3428 if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
3429 IALine, IACol, FunctionIdLoc))
Nirav Dave2364748a2016-09-16 18:30:20 +00003430 return Error(FunctionIdLoc, "function id already allocated");
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003431
3432 return false;
3433}
3434
Reid Kleckner2214ed82016-01-29 00:49:42 +00003435/// parseDirectiveCVLoc
3436/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3437/// [is_stmt VALUE]
3438/// The first number is a file number, must have been previously assigned with
3439/// a .file directive, the second number is the line number and optionally the
3440/// third number is a column position (zero if not specified). The remaining
3441/// optional items are .loc sub-directives.
3442bool AsmParser::parseDirectiveCVLoc() {
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003443 SMLoc DirectiveLoc = getTok().getLoc();
Nirav Davea645433c2016-07-18 15:24:03 +00003444 SMLoc Loc;
3445 int64_t FunctionId, FileNumber;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003446 if (parseCVFunctionId(FunctionId, ".cv_loc") ||
3447 parseCVFileId(FileNumber, ".cv_loc"))
Nirav Davea645433c2016-07-18 15:24:03 +00003448 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003449
3450 int64_t LineNumber = 0;
3451 if (getLexer().is(AsmToken::Integer)) {
3452 LineNumber = getTok().getIntVal();
3453 if (LineNumber < 0)
3454 return TokError("line number less than zero in '.cv_loc' directive");
3455 Lex();
3456 }
3457
3458 int64_t ColumnPos = 0;
3459 if (getLexer().is(AsmToken::Integer)) {
3460 ColumnPos = getTok().getIntVal();
3461 if (ColumnPos < 0)
3462 return TokError("column position less than zero in '.cv_loc' directive");
3463 Lex();
3464 }
3465
3466 bool PrologueEnd = false;
3467 uint64_t IsStmt = 0;
3468 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3469 StringRef Name;
3470 SMLoc Loc = getTok().getLoc();
3471 if (parseIdentifier(Name))
3472 return TokError("unexpected token in '.cv_loc' directive");
3473
3474 if (Name == "prologue_end")
3475 PrologueEnd = true;
3476 else if (Name == "is_stmt") {
3477 Loc = getTok().getLoc();
3478 const MCExpr *Value;
3479 if (parseExpression(Value))
3480 return true;
3481 // The expression must be the constant 0 or 1.
3482 IsStmt = ~0ULL;
3483 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3484 IsStmt = MCE->getValue();
3485
3486 if (IsStmt > 1)
3487 return Error(Loc, "is_stmt value not 0 or 1");
3488 } else {
3489 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3490 }
3491 }
Nirav Davea645433c2016-07-18 15:24:03 +00003492 Lex();
Reid Kleckner2214ed82016-01-29 00:49:42 +00003493
3494 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber,
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003495 ColumnPos, PrologueEnd, IsStmt, StringRef(),
3496 DirectiveLoc);
Reid Kleckner2214ed82016-01-29 00:49:42 +00003497 return false;
3498}
3499
3500/// parseDirectiveCVLinetable
3501/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3502bool AsmParser::parseDirectiveCVLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003503 int64_t FunctionId;
3504 StringRef FnStartName, FnEndName;
3505 SMLoc Loc = getTok().getLoc();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003506 if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
Nirav Davea645433c2016-07-18 15:24:03 +00003507 parseToken(AsmToken::Comma,
3508 "unexpected token in '.cv_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003509 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3510 "expected identifier in directive") ||
Nirav Davea645433c2016-07-18 15:24:03 +00003511 parseToken(AsmToken::Comma,
3512 "unexpected token in '.cv_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003513 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3514 "expected identifier in directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00003515 return true;
Reid Kleckner2214ed82016-01-29 00:49:42 +00003516
3517 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3518 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3519
3520 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3521 return false;
3522}
3523
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003524/// parseDirectiveCVInlineLinetable
David Majnemerc9911f22016-02-02 19:22:34 +00003525/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003526bool AsmParser::parseDirectiveCVInlineLinetable() {
Nirav Davea645433c2016-07-18 15:24:03 +00003527 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3528 StringRef FnStartName, FnEndName;
3529 SMLoc Loc = getTok().getLoc();
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003530 if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003531 parseTokenLoc(Loc) ||
Nirav Davea645433c2016-07-18 15:24:03 +00003532 parseIntToken(
3533 SourceFileId,
3534 "expected SourceField in '.cv_inline_linetable' directive") ||
3535 check(SourceFileId <= 0, Loc,
3536 "File id less than zero in '.cv_inline_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003537 parseTokenLoc(Loc) ||
Nirav Davea645433c2016-07-18 15:24:03 +00003538 parseIntToken(
3539 SourceLineNum,
3540 "expected SourceLineNum in '.cv_inline_linetable' directive") ||
3541 check(SourceLineNum < 0, Loc,
3542 "Line number less than zero in '.cv_inline_linetable' directive") ||
Nirav Daved8858ca2016-08-30 14:15:43 +00003543 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3544 "expected identifier in directive") ||
3545 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3546 "expected identifier in directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00003547 return true;
David Majnemerc9911f22016-02-02 19:22:34 +00003548
Nirav Davea645433c2016-07-18 15:24:03 +00003549 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
3550 return true;
3551
3552 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3553 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
Reid Kleckner1fcd6102016-02-02 17:41:18 +00003554 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3555 SourceLineNum, FnStartSym,
Reid Klecknera9f4cc92016-09-07 16:15:31 +00003556 FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +00003557 return false;
3558}
3559
David Majnemer408b5e62016-02-05 01:55:49 +00003560/// parseDirectiveCVDefRange
3561/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3562bool AsmParser::parseDirectiveCVDefRange() {
3563 SMLoc Loc;
3564 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3565 while (getLexer().is(AsmToken::Identifier)) {
3566 Loc = getLexer().getLoc();
3567 StringRef GapStartName;
3568 if (parseIdentifier(GapStartName))
3569 return Error(Loc, "expected identifier in directive");
3570 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3571
3572 Loc = getLexer().getLoc();
3573 StringRef GapEndName;
3574 if (parseIdentifier(GapEndName))
3575 return Error(Loc, "expected identifier in directive");
3576 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3577
3578 Ranges.push_back({GapStartSym, GapEndSym});
3579 }
3580
David Majnemer408b5e62016-02-05 01:55:49 +00003581 std::string FixedSizePortion;
Nirav Davea645433c2016-07-18 15:24:03 +00003582 if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
3583 parseEscapedString(FixedSizePortion))
David Majnemer408b5e62016-02-05 01:55:49 +00003584 return true;
David Majnemer408b5e62016-02-05 01:55:49 +00003585
3586 getStreamer().EmitCVDefRangeDirective(Ranges, FixedSizePortion);
3587 return false;
3588}
3589
Reid Kleckner2214ed82016-01-29 00:49:42 +00003590/// parseDirectiveCVStringTable
3591/// ::= .cv_stringtable
3592bool AsmParser::parseDirectiveCVStringTable() {
3593 getStreamer().EmitCVStringTableDirective();
3594 return false;
3595}
3596
3597/// parseDirectiveCVFileChecksums
3598/// ::= .cv_filechecksums
3599bool AsmParser::parseDirectiveCVFileChecksums() {
3600 getStreamer().EmitCVFileChecksumsDirective();
3601 return false;
3602}
3603
Jim Grosbach4b905842013-09-20 23:08:21 +00003604/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00003605/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00003606bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00003607 StringRef Name;
3608 bool EH = false;
3609 bool Debug = false;
3610
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003611 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003612 return TokError("Expected an identifier");
3613
3614 if (Name == ".eh_frame")
3615 EH = true;
3616 else if (Name == ".debug_frame")
3617 Debug = true;
3618
3619 if (getLexer().is(AsmToken::Comma)) {
3620 Lex();
3621
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003622 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003623 return TokError("Expected an identifier");
3624
3625 if (Name == ".eh_frame")
3626 EH = true;
3627 else if (Name == ".debug_frame")
3628 Debug = true;
3629 }
3630
3631 getStreamer().EmitCFISections(EH, Debug);
3632 return false;
3633}
3634
Jim Grosbach4b905842013-09-20 23:08:21 +00003635/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00003636/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00003637bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00003638 StringRef Simple;
3639 if (getLexer().isNot(AsmToken::EndOfStatement))
3640 if (parseIdentifier(Simple) || Simple != "simple")
3641 return TokError("unexpected token in .cfi_startproc directive");
3642
Nirav Davea645433c2016-07-18 15:24:03 +00003643 if (parseToken(AsmToken::EndOfStatement, "Expected end of statement"))
3644 return true;
3645
Oliver Stannardcf6bfb12014-11-03 12:19:03 +00003646 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00003647 return false;
3648}
3649
Jim Grosbach4b905842013-09-20 23:08:21 +00003650/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00003651/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00003652bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00003653 getStreamer().EmitCFIEndProc();
3654 return false;
3655}
3656
Jim Grosbach4b905842013-09-20 23:08:21 +00003657/// \brief parse register name or number.
3658bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00003659 SMLoc DirectiveLoc) {
3660 unsigned RegNo;
3661
3662 if (getLexer().isNot(AsmToken::Integer)) {
3663 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
3664 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00003665 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00003666 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003667 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003668
3669 return false;
3670}
3671
Jim Grosbach4b905842013-09-20 23:08:21 +00003672/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003673/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003674bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003675 int64_t Register = 0, Offset = 0;
3676 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3677 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3678 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003679 return true;
3680
3681 getStreamer().EmitCFIDefCfa(Register, Offset);
3682 return false;
3683}
3684
Jim Grosbach4b905842013-09-20 23:08:21 +00003685/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003686/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003687bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003688 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003689 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003690 return true;
3691
3692 getStreamer().EmitCFIDefCfaOffset(Offset);
3693 return false;
3694}
3695
Jim Grosbach4b905842013-09-20 23:08:21 +00003696/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003697/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003698bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003699 int64_t Register1 = 0, Register2 = 0;
3700 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
3701 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3702 parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003703 return true;
3704
3705 getStreamer().EmitCFIRegister(Register1, Register2);
3706 return false;
3707}
3708
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003709/// parseDirectiveCFIWindowSave
3710/// ::= .cfi_window_save
3711bool AsmParser::parseDirectiveCFIWindowSave() {
3712 getStreamer().EmitCFIWindowSave();
3713 return false;
3714}
3715
Jim Grosbach4b905842013-09-20 23:08:21 +00003716/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003717/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003718bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003719 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003720 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003721 return true;
3722
3723 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3724 return false;
3725}
3726
Jim Grosbach4b905842013-09-20 23:08:21 +00003727/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003728/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003729bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003730 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003731 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003732 return true;
3733
3734 getStreamer().EmitCFIDefCfaRegister(Register);
3735 return false;
3736}
3737
Jim Grosbach4b905842013-09-20 23:08:21 +00003738/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003739/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003740bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003741 int64_t Register = 0;
3742 int64_t Offset = 0;
3743
Nirav Davea645433c2016-07-18 15:24:03 +00003744 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3745 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3746 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003747 return true;
3748
3749 getStreamer().EmitCFIOffset(Register, Offset);
3750 return false;
3751}
3752
Jim Grosbach4b905842013-09-20 23:08:21 +00003753/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003754/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003755bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00003756 int64_t Register = 0, Offset = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003757
Nirav Davea645433c2016-07-18 15:24:03 +00003758 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
3759 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3760 parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003761 return true;
3762
3763 getStreamer().EmitCFIRelOffset(Register, Offset);
3764 return false;
3765}
3766
3767static bool isValidEncoding(int64_t Encoding) {
3768 if (Encoding & ~0xff)
3769 return false;
3770
3771 if (Encoding == dwarf::DW_EH_PE_omit)
3772 return true;
3773
3774 const unsigned Format = Encoding & 0xf;
3775 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3776 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3777 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3778 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3779 return false;
3780
3781 const unsigned Application = Encoding & 0x70;
3782 if (Application != dwarf::DW_EH_PE_absptr &&
3783 Application != dwarf::DW_EH_PE_pcrel)
3784 return false;
3785
3786 return true;
3787}
3788
Jim Grosbach4b905842013-09-20 23:08:21 +00003789/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003790/// IsPersonality true for cfi_personality, false for cfi_lsda
3791/// ::= .cfi_personality encoding, [symbol_name]
3792/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003793bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003794 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003795 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003796 return true;
3797 if (Encoding == dwarf::DW_EH_PE_omit)
3798 return false;
3799
Eli Bendersky17233942013-01-15 22:59:42 +00003800 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00003801 if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
3802 parseToken(AsmToken::Comma, "unexpected token in directive") ||
3803 check(parseIdentifier(Name), "expected identifier in directive"))
3804 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003805
Jim Grosbach6f482002015-05-18 18:43:14 +00003806 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003807
3808 if (IsPersonality)
3809 getStreamer().EmitCFIPersonality(Sym, Encoding);
3810 else
3811 getStreamer().EmitCFILsda(Sym, Encoding);
3812 return false;
3813}
3814
Jim Grosbach4b905842013-09-20 23:08:21 +00003815/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003816/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003817bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003818 getStreamer().EmitCFIRememberState();
3819 return false;
3820}
3821
Jim Grosbach4b905842013-09-20 23:08:21 +00003822/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003823/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003824bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003825 getStreamer().EmitCFIRestoreState();
3826 return false;
3827}
3828
Jim Grosbach4b905842013-09-20 23:08:21 +00003829/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003830/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003831bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003832 int64_t Register = 0;
3833
Jim Grosbach4b905842013-09-20 23:08:21 +00003834 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003835 return true;
3836
3837 getStreamer().EmitCFISameValue(Register);
3838 return false;
3839}
3840
Jim Grosbach4b905842013-09-20 23:08:21 +00003841/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003842/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003843bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003844 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003845 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003846 return true;
3847
3848 getStreamer().EmitCFIRestore(Register);
3849 return false;
3850}
3851
Jim Grosbach4b905842013-09-20 23:08:21 +00003852/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003853/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003854bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003855 std::string Values;
3856 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003857 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003858 return true;
3859
3860 Values.push_back((uint8_t)CurrValue);
3861
3862 while (getLexer().is(AsmToken::Comma)) {
3863 Lex();
3864
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003865 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003866 return true;
3867
3868 Values.push_back((uint8_t)CurrValue);
3869 }
3870
3871 getStreamer().EmitCFIEscape(Values);
3872 return false;
3873}
3874
Jim Grosbach4b905842013-09-20 23:08:21 +00003875/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003876/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003877bool AsmParser::parseDirectiveCFISignalFrame() {
Nirav Davea645433c2016-07-18 15:24:03 +00003878 if (parseToken(AsmToken::EndOfStatement,
3879 "unexpected token in '.cfi_signal_frame'"))
3880 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003881
3882 getStreamer().EmitCFISignalFrame();
3883 return false;
3884}
3885
Jim Grosbach4b905842013-09-20 23:08:21 +00003886/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003887/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003888bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003889 int64_t Register = 0;
3890
Jim Grosbach4b905842013-09-20 23:08:21 +00003891 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003892 return true;
3893
3894 getStreamer().EmitCFIUndefined(Register);
3895 return false;
3896}
3897
Jim Grosbach4b905842013-09-20 23:08:21 +00003898/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003899/// ::= .macros_on
3900/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003901bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00003902 if (parseToken(AsmToken::EndOfStatement,
3903 "unexpected token in '" + Directive + "' directive"))
3904 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003905
Jim Grosbach4b905842013-09-20 23:08:21 +00003906 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003907 return false;
3908}
3909
Jim Grosbach4b905842013-09-20 23:08:21 +00003910/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003911/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003912bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003913 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003914 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003915 return TokError("expected identifier in '.macro' directive");
3916
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003917 if (getLexer().is(AsmToken::Comma))
3918 Lex();
3919
Eli Bendersky17233942013-01-15 22:59:42 +00003920 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003921 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003922
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00003923 if (!Parameters.empty() && Parameters.back().Vararg)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003924 return Error(Lexer.getLoc(),
3925 "Vararg parameter '" + Parameters.back().Name +
3926 "' should be last one in the list of parameters.");
3927
David Majnemer91fc4c22014-01-29 18:57:46 +00003928 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003929 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003930 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003931
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003932 if (Lexer.is(AsmToken::Colon)) {
3933 Lex(); // consume ':'
3934
3935 SMLoc QualLoc;
3936 StringRef Qualifier;
3937
3938 QualLoc = Lexer.getLoc();
3939 if (parseIdentifier(Qualifier))
3940 return Error(QualLoc, "missing parameter qualifier for "
3941 "'" + Parameter.Name + "' in macro '" + Name + "'");
3942
3943 if (Qualifier == "req")
3944 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003945 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003946 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003947 else
3948 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3949 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3950 }
3951
David Majnemer91fc4c22014-01-29 18:57:46 +00003952 if (getLexer().is(AsmToken::Equal)) {
3953 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003954
3955 SMLoc ParamLoc;
3956
3957 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003958 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003959 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003960
3961 if (Parameter.Required)
3962 Warning(ParamLoc, "pointless default value for required parameter "
3963 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003964 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003965
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00003966 Parameters.push_back(std::move(Parameter));
David Majnemer91fc4c22014-01-29 18:57:46 +00003967
3968 if (getLexer().is(AsmToken::Comma))
3969 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003970 }
3971
Nirav Dave1180e6892016-06-02 17:15:05 +00003972 // Eat just the end of statement.
3973 Lexer.Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003974
Nirav Dave1180e6892016-06-02 17:15:05 +00003975 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
Eli Bendersky17233942013-01-15 22:59:42 +00003976 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003977 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003978 // Lex the macro definition.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00003979 while (true) {
Nirav Dave1180e6892016-06-02 17:15:05 +00003980 // Ignore Lexing errors in macros.
3981 while (Lexer.is(AsmToken::Error)) {
3982 Lexer.Lex();
3983 }
3984
Eli Bendersky17233942013-01-15 22:59:42 +00003985 // Check whether we have reached the end of the file.
3986 if (getLexer().is(AsmToken::Eof))
3987 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3988
3989 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003990 if (getLexer().is(AsmToken::Identifier)) {
3991 if (getTok().getIdentifier() == ".endm" ||
3992 getTok().getIdentifier() == ".endmacro") {
3993 if (MacroDepth == 0) { // Outermost macro.
3994 EndToken = getTok();
Nirav Dave1180e6892016-06-02 17:15:05 +00003995 Lexer.Lex();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003996 if (getLexer().isNot(AsmToken::EndOfStatement))
3997 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3998 "' directive");
3999 break;
4000 } else {
4001 // Otherwise we just found the end of an inner macro.
4002 --MacroDepth;
4003 }
4004 } else if (getTok().getIdentifier() == ".macro") {
4005 // We allow nested macros. Those aren't instantiated until the outermost
4006 // macro is expanded so just ignore them for now.
4007 ++MacroDepth;
4008 }
Eli Bendersky17233942013-01-15 22:59:42 +00004009 }
4010
4011 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004012 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00004013 }
4014
Jim Grosbach4b905842013-09-20 23:08:21 +00004015 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00004016 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
4017 }
4018
4019 const char *BodyStart = StartToken.getLoc().getPointer();
4020 const char *BodyEnd = EndToken.getLoc().getPointer();
4021 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00004022 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
Benjamin Kramercb3e06b2014-10-03 18:32:55 +00004023 defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
Eli Bendersky17233942013-01-15 22:59:42 +00004024 return false;
4025}
4026
Jim Grosbach4b905842013-09-20 23:08:21 +00004027/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00004028///
4029/// With the support added for named parameters there may be code out there that
4030/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00004031/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00004032/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00004033/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00004034/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
4035/// warning that the positional parameter found in body which have no effect.
4036/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00004037/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00004038/// intended or change the macro to use the named parameters. It is possible
4039/// this warning will trigger when the none of the named parameters are used
4040/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00004041void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00004042 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004043 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00004044 // If this macro is not defined with named parameters the warning we are
4045 // checking for here doesn't apply.
4046 unsigned NParameters = Parameters.size();
4047 if (NParameters == 0)
4048 return;
4049
4050 bool NamedParametersFound = false;
4051 bool PositionalParametersFound = false;
4052
4053 // Look at the body of the macro for use of both the named parameters and what
4054 // are likely to be positional parameters. This is what expandMacro() is
4055 // doing when it finds the parameters in the body.
4056 while (!Body.empty()) {
4057 // Scan for the next possible parameter.
4058 std::size_t End = Body.size(), Pos = 0;
4059 for (; Pos != End; ++Pos) {
4060 // Check for a substitution or escape.
4061 // This macro is defined with parameters, look for \foo, \bar, etc.
4062 if (Body[Pos] == '\\' && Pos + 1 != End)
4063 break;
4064
4065 // This macro should have parameters, but look for $0, $1, ..., $n too.
4066 if (Body[Pos] != '$' || Pos + 1 == End)
4067 continue;
4068 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00004069 if (Next == '$' || Next == 'n' ||
4070 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00004071 break;
4072 }
4073
4074 // Check if we reached the end.
4075 if (Pos == End)
4076 break;
4077
4078 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00004079 switch (Body[Pos + 1]) {
4080 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00004081 case '$':
4082 break;
4083
Jim Grosbach4b905842013-09-20 23:08:21 +00004084 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00004085 case 'n':
4086 PositionalParametersFound = true;
4087 break;
4088
Jim Grosbach4b905842013-09-20 23:08:21 +00004089 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00004090 default: {
4091 PositionalParametersFound = true;
4092 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00004093 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00004094 }
4095 Pos += 2;
4096 } else {
4097 unsigned I = Pos + 1;
4098 while (isIdentifierChar(Body[I]) && I + 1 != End)
4099 ++I;
4100
Jim Grosbach4b905842013-09-20 23:08:21 +00004101 const char *Begin = Body.data() + Pos + 1;
4102 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00004103 unsigned Index = 0;
4104 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004105 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00004106 break;
4107
4108 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004109 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
4110 Pos += 3;
4111 else {
4112 Pos = I;
4113 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00004114 } else {
4115 NamedParametersFound = true;
4116 Pos += 1 + Argument.size();
4117 }
4118 }
4119 // Update the scan point.
4120 Body = Body.substr(Pos);
4121 }
4122
4123 if (!NamedParametersFound && PositionalParametersFound)
4124 Warning(DirectiveLoc, "macro defined with named parameters which are not "
4125 "used in macro body, possible positional parameter "
4126 "found in body which will have no effect");
4127}
4128
Nico Weber155dccd12014-07-24 17:08:39 +00004129/// parseDirectiveExitMacro
4130/// ::= .exitm
4131bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
Nirav Davea645433c2016-07-18 15:24:03 +00004132 if (parseToken(AsmToken::EndOfStatement,
4133 "unexpected token in '" + Directive + "' directive"))
4134 return true;
Nico Weber155dccd12014-07-24 17:08:39 +00004135
4136 if (!isInsideMacroInstantiation())
4137 return TokError("unexpected '" + Directive + "' in file, "
4138 "no current macro definition");
4139
4140 // Exit all conditionals that are active in the current macro.
4141 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
4142 TheCondState = TheCondStack.back();
4143 TheCondStack.pop_back();
4144 }
4145
4146 handleMacroExit();
4147 return false;
4148}
4149
Jim Grosbach4b905842013-09-20 23:08:21 +00004150/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00004151/// ::= .endm
4152/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00004153bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00004154 if (getLexer().isNot(AsmToken::EndOfStatement))
4155 return TokError("unexpected token in '" + Directive + "' directive");
4156
4157 // If we are inside a macro instantiation, terminate the current
4158 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00004159 if (isInsideMacroInstantiation()) {
4160 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00004161 return false;
4162 }
4163
4164 // Otherwise, this .endmacro is a stray entry in the file; well formed
4165 // .endmacro directives are handled during the macro definition parsing.
4166 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00004167 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00004168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00004171/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00004172bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00004173 StringRef Name;
Nirav Davea645433c2016-07-18 15:24:03 +00004174 SMLoc Loc;
Nirav Daved8858ca2016-08-30 14:15:43 +00004175 if (parseTokenLoc(Loc) ||
4176 check(parseIdentifier(Name), Loc,
4177 "expected identifier in '.purgem' directive") ||
Nirav Davea645433c2016-07-18 15:24:03 +00004178 parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00004179 "unexpected token in '.purgem' directive"))
Nirav Davea645433c2016-07-18 15:24:03 +00004180 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004181
Nirav Dave1ab71992016-07-18 19:35:21 +00004182 if (!lookupMacro(Name))
4183 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
4184
Jim Grosbach4b905842013-09-20 23:08:21 +00004185 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00004186 return false;
4187}
Eli Benderskyf483ff92012-12-20 19:05:53 +00004188
Jim Grosbach4b905842013-09-20 23:08:21 +00004189/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00004190/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004191bool AsmParser::parseDirectiveBundleAlignMode() {
Eli Benderskyf483ff92012-12-20 19:05:53 +00004192 // Expect a single argument: an expression that evaluates to a constant
4193 // in the inclusive range 0-30.
4194 SMLoc ExprLoc = getLexer().getLoc();
4195 int64_t AlignSizePow2;
Nirav Davef43cc9f2016-10-10 15:24:54 +00004196 if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) ||
Nirav Davea645433c2016-07-18 15:24:03 +00004197 parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
4198 "in '.bundle_align_mode' "
4199 "directive") ||
4200 check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
4201 "invalid bundle alignment size (expected between 0 and 30)"))
Eli Benderskyf483ff92012-12-20 19:05:53 +00004202 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004203
4204 // Because of AlignSizePow2's verified range we can safely truncate it to
4205 // unsigned.
4206 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4207 return false;
4208}
4209
Jim Grosbach4b905842013-09-20 23:08:21 +00004210/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00004211/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00004212bool AsmParser::parseDirectiveBundleLock() {
Nirav Davef43cc9f2016-10-10 15:24:54 +00004213 if (checkForValidSection())
4214 return true;
Eli Bendersky802b6282013-01-07 21:51:08 +00004215 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004216
Eli Bendersky802b6282013-01-07 21:51:08 +00004217 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4218 StringRef Option;
4219 SMLoc Loc = getTok().getLoc();
4220 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00004221 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00004222
Nirav Davea645433c2016-07-18 15:24:03 +00004223 if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
4224 check(Option != "align_to_end", Loc, kInvalidOptionError) ||
4225 check(getTok().isNot(AsmToken::EndOfStatement), Loc,
4226 "unexpected token after '.bundle_lock' directive option"))
4227 return true;
Eli Bendersky802b6282013-01-07 21:51:08 +00004228 AlignToEnd = true;
4229 }
4230
Eli Benderskyf483ff92012-12-20 19:05:53 +00004231 Lex();
4232
Eli Bendersky802b6282013-01-07 21:51:08 +00004233 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00004234 return false;
4235}
4236
Jim Grosbach4b905842013-09-20 23:08:21 +00004237/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00004238/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00004239bool AsmParser::parseDirectiveBundleUnlock() {
Nirav Davef43cc9f2016-10-10 15:24:54 +00004240 if (checkForValidSection() ||
4241 parseToken(AsmToken::EndOfStatement,
Nirav Davea645433c2016-07-18 15:24:03 +00004242 "unexpected token in '.bundle_unlock' directive"))
4243 return true;
Eli Benderskyf483ff92012-12-20 19:05:53 +00004244
4245 getStreamer().EmitBundleUnlock();
4246 return false;
4247}
4248
Jim Grosbach4b905842013-09-20 23:08:21 +00004249/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00004250/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004251bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Eli Bendersky17233942013-01-15 22:59:42 +00004252
Petr Hosek67a94a72016-05-28 05:57:48 +00004253 SMLoc NumBytesLoc = Lexer.getLoc();
4254 const MCExpr *NumBytes;
Nirav Davef43cc9f2016-10-10 15:24:54 +00004255 if (checkForValidSection() || parseExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00004256 return true;
4257
4258 int64_t FillExpr = 0;
4259 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eli Bendersky17233942013-01-15 22:59:42 +00004260
Nirav Davea645433c2016-07-18 15:24:03 +00004261 if (parseToken(AsmToken::Comma,
4262 "unexpected token in '" + Twine(IDVal) + "' directive") ||
4263 parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00004264 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004265 }
4266
Nirav Davea645433c2016-07-18 15:24:03 +00004267 if (parseToken(AsmToken::EndOfStatement,
4268 "unexpected token in '" + Twine(IDVal) + "' directive"))
4269 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004270
Eli Bendersky17233942013-01-15 22:59:42 +00004271 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Petr Hosek67a94a72016-05-28 05:57:48 +00004272 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
Eli Bendersky17233942013-01-15 22:59:42 +00004273
4274 return false;
4275}
4276
Petr Hosek4cb08ce2016-09-23 19:25:15 +00004277/// parseDirectiveDCB
4278/// ::= .dcb.{b, l, w} expression, expression
4279bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
Petr Hosek4cb08ce2016-09-23 19:25:15 +00004280 SMLoc NumValuesLoc = Lexer.getLoc();
4281 int64_t NumValues;
Nirav Davef43cc9f2016-10-10 15:24:54 +00004282 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
Petr Hosek4cb08ce2016-09-23 19:25:15 +00004283 return true;
4284
4285 if (NumValues < 0) {
4286 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4287 return false;
4288 }
4289
4290 if (parseToken(AsmToken::Comma,
4291 "unexpected token in '" + Twine(IDVal) + "' directive"))
4292 return true;
4293
4294 const MCExpr *Value;
4295 SMLoc ExprLoc = getLexer().getLoc();
4296 if (parseExpression(Value))
4297 return true;
4298
4299 // Special case constant expressions to match code generator.
4300 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
4301 assert(Size <= 8 && "Invalid size");
4302 uint64_t IntValue = MCE->getValue();
4303 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
4304 return Error(ExprLoc, "literal value out of range for directive");
4305 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4306 getStreamer().EmitIntValue(IntValue, Size);
4307 } else {
4308 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4309 getStreamer().EmitValue(Value, Size, ExprLoc);
4310 }
4311
4312 if (parseToken(AsmToken::EndOfStatement,
4313 "unexpected token in '" + Twine(IDVal) + "' directive"))
4314 return true;
4315
4316 return false;
4317}
4318
4319/// parseDirectiveRealDCB
4320/// ::= .dcb.{d, s} expression, expression
4321bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
Petr Hosek4cb08ce2016-09-23 19:25:15 +00004322 SMLoc NumValuesLoc = Lexer.getLoc();
4323 int64_t NumValues;
Nirav Davef43cc9f2016-10-10 15:24:54 +00004324 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
Petr Hosek4cb08ce2016-09-23 19:25:15 +00004325 return true;
4326
4327 if (NumValues < 0) {
4328 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4329 return false;
4330 }
4331
4332 if (parseToken(AsmToken::Comma,
4333 "unexpected token in '" + Twine(IDVal) + "' directive"))
4334 return true;
4335
4336 APInt AsInt;
4337 if (parseRealValue(Semantics, AsInt))
4338 return true;
4339
4340 if (parseToken(AsmToken::EndOfStatement,
4341 "unexpected token in '" + Twine(IDVal) + "' directive"))
4342 return true;
4343
4344 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4345 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
4346 AsInt.getBitWidth() / 8);
4347
4348 return false;
4349}
4350
Petr Hosek85b2f672016-09-23 21:53:36 +00004351/// parseDirectiveDS
4352/// ::= .ds.{b, d, l, p, s, w, x} expression
4353bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
Petr Hosek85b2f672016-09-23 21:53:36 +00004354
4355 SMLoc NumValuesLoc = Lexer.getLoc();
4356 int64_t NumValues;
Nirav Davef43cc9f2016-10-10 15:24:54 +00004357 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
Petr Hosek85b2f672016-09-23 21:53:36 +00004358 return true;
4359
4360 if (NumValues < 0) {
4361 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4362 return false;
4363 }
4364
4365 if (parseToken(AsmToken::EndOfStatement,
4366 "unexpected token in '" + Twine(IDVal) + "' directive"))
4367 return true;
4368
4369 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4370 getStreamer().emitFill(Size, 0);
4371
4372 return false;
4373}
4374
Jim Grosbach4b905842013-09-20 23:08:21 +00004375/// parseDirectiveLEB128
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004376/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004377bool AsmParser::parseDirectiveLEB128(bool Signed) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00004378 if (checkForValidSection())
4379 return true;
4380
Eli Bendersky17233942013-01-15 22:59:42 +00004381 const MCExpr *Value;
4382
Eugene Zelenko33d7b762016-08-23 17:14:32 +00004383 while (true) {
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004384 if (parseExpression(Value))
4385 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00004386
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004387 if (Signed)
4388 getStreamer().EmitSLEB128Value(Value);
4389 else
4390 getStreamer().EmitULEB128Value(Value);
Eli Bendersky17233942013-01-15 22:59:42 +00004391
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004392 if (getLexer().is(AsmToken::EndOfStatement))
4393 break;
4394
Nirav Davea645433c2016-07-18 15:24:03 +00004395 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4396 return true;
Benjamin Kramer68ca67b2015-02-19 20:24:04 +00004397 }
Nirav Davea645433c2016-07-18 15:24:03 +00004398 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00004399
4400 return false;
4401}
4402
Jim Grosbach4b905842013-09-20 23:08:21 +00004403/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00004404/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004405bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00004407 while (true) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004408 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004409 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004410
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004411 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004412 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004413
Jim Grosbach6f482002015-05-18 18:43:14 +00004414 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00004415
Jim Grosbachebdf32f2011-09-15 17:56:49 +00004416 // Assembler local symbols don't make any sense here. Complain loudly.
4417 if (Sym->isTemporary())
4418 return Error(Loc, "non-local symbol required in directive");
4419
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00004420 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
4421 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00004422
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004423 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00004424 break;
4425
Nirav Davea645433c2016-07-18 15:24:03 +00004426 if (parseToken(AsmToken::Comma, "unexpected token in directive"))
4427 return true;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004428 }
4429 }
4430
Sean Callanan686ed8d2010-01-19 20:22:31 +00004431 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00004432 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00004433}
Chris Lattnera1e11f52009-07-07 20:30:46 +00004434
Jim Grosbach4b905842013-09-20 23:08:21 +00004435/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00004436/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004437bool AsmParser::parseDirectiveComm(bool IsLocal) {
Nirav Davef43cc9f2016-10-10 15:24:54 +00004438 if (checkForValidSection())
4439 return true;
Daniel Dunbare5444a82010-09-09 22:42:59 +00004440
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004441 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00004442 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004443 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004444 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004445
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00004446 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +00004447 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004448
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004449 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004450 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00004451 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004452
4453 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004454 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004455 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004456 return true;
4457
4458 int64_t Pow2Alignment = 0;
4459 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004460 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00004461 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004462 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004463 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00004464 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00004465
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004466 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4467 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004468 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4469
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004470 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00004471 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4472 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00004473 if (!isPowerOf2_64(Pow2Alignment))
4474 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4475 Pow2Alignment = Log2_64(Pow2Alignment);
4476 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004477 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00004478
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004479 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00004480 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004481
Sean Callanan686ed8d2010-01-19 20:22:31 +00004482 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00004483
Chris Lattner28ad7542009-07-09 17:25:12 +00004484 // NOTE: a size of zero for a .comm should create a undefined symbol
4485 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00004486 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004487 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00004488 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004489
Eric Christopherbc818852010-05-14 01:38:54 +00004490 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00004491 // may internally end up wanting an alignment in bytes.
4492 // FIXME: Diagnose overflow.
4493 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00004494 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00004495 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00004496
Daniel Dunbar6860ac72009-08-22 07:22:36 +00004497 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00004498 return Error(IDLoc, "invalid symbol redefinition");
4499
Chris Lattner28ad7542009-07-09 17:25:12 +00004500 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004501 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00004502 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00004503 return false;
4504 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00004505
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004506 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00004507 return false;
4508}
Chris Lattner07cadaf2009-07-10 22:20:30 +00004509
Jim Grosbach4b905842013-09-20 23:08:21 +00004510/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004511/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00004512bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004513 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004514 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004515
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004516 StringRef Str = parseStringToEndOfStatement();
Nirav Davea645433c2016-07-18 15:24:03 +00004517 if (parseToken(AsmToken::EndOfStatement,
4518 "unexpected token in '.abort' directive"))
4519 return true;
Kevin Enderby56523ce2009-07-13 23:15:14 +00004520
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004521 if (Str.empty())
Nirav Dave2364748a2016-09-16 18:30:20 +00004522 return Error(Loc, ".abort detected. Assembly stopping.");
Daniel Dunbareb6bb322009-07-27 23:20:52 +00004523 else
Nirav Dave2364748a2016-09-16 18:30:20 +00004524 return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00004525 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00004526
4527 return false;
4528}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00004529
Jim Grosbach4b905842013-09-20 23:08:21 +00004530/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004531/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00004532bool AsmParser::parseDirectiveInclude() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004533 // Allow the strings to have escaped octal character sequence.
4534 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004535 SMLoc IncludeLoc = getTok().getLoc();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004536
Nirav Davea645433c2016-07-18 15:24:03 +00004537 if (check(getTok().isNot(AsmToken::String),
4538 "expected string in '.include' directive") ||
4539 parseEscapedString(Filename) ||
4540 check(getTok().isNot(AsmToken::EndOfStatement),
4541 "unexpected token in '.include' directive") ||
4542 // Attempt to switch the lexer to the included file before consuming the
4543 // end of statement to avoid losing it when we switch.
4544 check(enterIncludeFile(Filename), IncludeLoc,
4545 "Could not find include file '" + Filename + "'"))
Chris Lattner693fbb82009-07-16 06:14:39 +00004546 return true;
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00004547
4548 return false;
4549}
Kevin Enderby09ea5702009-07-15 15:30:11 +00004550
Jim Grosbach4b905842013-09-20 23:08:21 +00004551/// parseDirectiveIncbin
Petr Hosek2f4ac442016-09-23 00:41:06 +00004552/// ::= .incbin "filename" [ , skip [ , count ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00004553bool AsmParser::parseDirectiveIncbin() {
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004554 // Allow the strings to have escaped octal character sequence.
4555 std::string Filename;
Nirav Davea645433c2016-07-18 15:24:03 +00004556 SMLoc IncbinLoc = getTok().getLoc();
4557 if (check(getTok().isNot(AsmToken::String),
4558 "expected string in '.incbin' directive") ||
Petr Hosek2f4ac442016-09-23 00:41:06 +00004559 parseEscapedString(Filename))
4560 return true;
4561
4562 int64_t Skip = 0;
4563 const MCExpr *Count = nullptr;
4564 SMLoc SkipLoc, CountLoc;
4565 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4566 if (parseToken(AsmToken::Comma, "unexpected token in '.incbin' directive"))
4567 return true;
4568
4569 // The skip expression can be omitted while specifying the count, e.g:
4570 // .incbin "filename",,4
4571 if (getTok().isNot(AsmToken::Comma)) {
4572 if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip))
4573 return true;
4574 }
4575
4576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4577 if (parseToken(AsmToken::Comma, "unexpected token in '.incbin' directive") ||
4578 parseTokenLoc(CountLoc) || parseExpression(Count))
4579 return true;
4580 }
4581 }
4582
4583 if (parseToken(AsmToken::EndOfStatement,
Nirav Dave1ab71992016-07-18 19:35:21 +00004584 "unexpected token in '.incbin' directive"))
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00004585 return true;
Nirav Dave1ab71992016-07-18 19:35:21 +00004586
Petr Hosek2f4ac442016-09-23 00:41:06 +00004587 if (check(Skip < 0, SkipLoc, "skip is negative"))
4588 return true;
4589
Nirav Dave1ab71992016-07-18 19:35:21 +00004590 // Attempt to process the included file.
Petr Hosek2f4ac442016-09-23 00:41:06 +00004591 if (processIncbinFile(Filename, Skip, Count, CountLoc))
Nirav Dave1ab71992016-07-18 19:35:21 +00004592 return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
Kevin Enderby109f25c2011-12-14 21:47:48 +00004593 return false;
4594}
4595
Jim Grosbach4b905842013-09-20 23:08:21 +00004596/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004597/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4598bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004599 TheCondStack.push_back(TheCondState);
4600 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004601 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004602 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004603 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004604 int64_t ExprValue;
Nirav Davea645433c2016-07-18 15:24:03 +00004605 if (parseAbsoluteExpression(ExprValue) ||
4606 parseToken(AsmToken::EndOfStatement,
4607 "unexpected token in '.if' directive"))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004608 return true;
4609
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004610 switch (DirKind) {
4611 default:
4612 llvm_unreachable("unsupported directive");
4613 case DK_IF:
4614 case DK_IFNE:
4615 break;
4616 case DK_IFEQ:
4617 ExprValue = ExprValue == 0;
4618 break;
4619 case DK_IFGE:
4620 ExprValue = ExprValue >= 0;
4621 break;
4622 case DK_IFGT:
4623 ExprValue = ExprValue > 0;
4624 break;
4625 case DK_IFLE:
4626 ExprValue = ExprValue <= 0;
4627 break;
4628 case DK_IFLT:
4629 ExprValue = ExprValue < 0;
4630 break;
4631 }
4632
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004633 TheCondState.CondMet = ExprValue;
4634 TheCondState.Ignore = !TheCondState.CondMet;
4635 }
4636
4637 return false;
4638}
4639
Jim Grosbach4b905842013-09-20 23:08:21 +00004640/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004641/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00004642bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004643 TheCondStack.push_back(TheCondState);
4644 TheCondState.TheCond = AsmCond::IfCond;
4645
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004646 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004647 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004648 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004649 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004650
Nirav Davea645433c2016-07-18 15:24:03 +00004651 if (parseToken(AsmToken::EndOfStatement,
4652 "unexpected token in '.ifb' directive"))
4653 return true;
Benjamin Kramer62c18b02012-05-12 11:18:42 +00004654
4655 TheCondState.CondMet = ExpectBlank == Str.empty();
4656 TheCondState.Ignore = !TheCondState.CondMet;
4657 }
4658
4659 return false;
4660}
4661
Jim Grosbach4b905842013-09-20 23:08:21 +00004662/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004663/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004664/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00004665bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004666 TheCondStack.push_back(TheCondState);
4667 TheCondState.TheCond = AsmCond::IfCond;
4668
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00004669 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004670 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004671 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00004672 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004673
Nirav Davea645433c2016-07-18 15:24:03 +00004674 if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
4675 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004676
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004677 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004678
Nirav Davea645433c2016-07-18 15:24:03 +00004679 if (parseToken(AsmToken::EndOfStatement,
4680 "unexpected token in '.ifc' directive"))
4681 return true;
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004682
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00004683 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004684 TheCondState.Ignore = !TheCondState.CondMet;
4685 }
4686
4687 return false;
4688}
4689
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004690/// parseDirectiveIfeqs
4691/// ::= .ifeqs string1, string2
Sid Manning51c35602015-03-18 14:20:54 +00004692bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004693 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004694 if (ExpectEqual)
Nirav Dave2364748a2016-09-16 18:30:20 +00004695 return TokError("expected string parameter for '.ifeqs' directive");
4696 return TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004697 }
4698
4699 StringRef String1 = getTok().getStringContents();
4700 Lex();
4701
4702 if (Lexer.isNot(AsmToken::Comma)) {
Sid Manning51c35602015-03-18 14:20:54 +00004703 if (ExpectEqual)
Nirav Dave2364748a2016-09-16 18:30:20 +00004704 return TokError(
4705 "expected comma after first string for '.ifeqs' directive");
4706 return TokError("expected comma after first string for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004707 }
4708
4709 Lex();
4710
4711 if (Lexer.isNot(AsmToken::String)) {
Sid Manning51c35602015-03-18 14:20:54 +00004712 if (ExpectEqual)
Nirav Dave2364748a2016-09-16 18:30:20 +00004713 return TokError("expected string parameter for '.ifeqs' directive");
4714 return TokError("expected string parameter for '.ifnes' directive");
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004715 }
4716
4717 StringRef String2 = getTok().getStringContents();
4718 Lex();
4719
4720 TheCondStack.push_back(TheCondState);
4721 TheCondState.TheCond = AsmCond::IfCond;
Sid Manning51c35602015-03-18 14:20:54 +00004722 TheCondState.CondMet = ExpectEqual == (String1 == String2);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004723 TheCondState.Ignore = !TheCondState.CondMet;
4724
4725 return false;
4726}
4727
Jim Grosbach4b905842013-09-20 23:08:21 +00004728/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00004729/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00004730bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004731 StringRef Name;
4732 TheCondStack.push_back(TheCondState);
4733 TheCondState.TheCond = AsmCond::IfCond;
4734
4735 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004736 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004737 } else {
Nirav Davea645433c2016-07-18 15:24:03 +00004738 if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
4739 parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
4740 return true;
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004741
Jim Grosbach6f482002015-05-18 18:43:14 +00004742 MCSymbol *Sym = getContext().lookupSymbol(Name);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004743
4744 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00004745 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004746 else
Craig Topper353eda42014-04-24 06:44:33 +00004747 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004748 TheCondState.Ignore = !TheCondState.CondMet;
4749 }
4750
4751 return false;
4752}
4753
Jim Grosbach4b905842013-09-20 23:08:21 +00004754/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004755/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004756bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004757 if (TheCondState.TheCond != AsmCond::IfCond &&
4758 TheCondState.TheCond != AsmCond::ElseIfCond)
Nirav Dave2364748a2016-09-16 18:30:20 +00004759 return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
4760 " .if or an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004761 TheCondState.TheCond = AsmCond::ElseIfCond;
4762
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004763 bool LastIgnoreState = false;
4764 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004765 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004766 if (LastIgnoreState || TheCondState.CondMet) {
4767 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004768 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004769 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004770 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004771 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004772 return true;
4773
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004774 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004775 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004776
Sean Callanan686ed8d2010-01-19 20:22:31 +00004777 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004778 TheCondState.CondMet = ExprValue;
4779 TheCondState.Ignore = !TheCondState.CondMet;
4780 }
4781
4782 return false;
4783}
4784
Jim Grosbach4b905842013-09-20 23:08:21 +00004785/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004786/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004787bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004788 if (parseToken(AsmToken::EndOfStatement,
4789 "unexpected token in '.else' directive"))
4790 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004791
4792 if (TheCondState.TheCond != AsmCond::IfCond &&
4793 TheCondState.TheCond != AsmCond::ElseIfCond)
Nirav Dave2364748a2016-09-16 18:30:20 +00004794 return Error(DirectiveLoc, "Encountered a .else that doesn't follow "
4795 " an .if or an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004796 TheCondState.TheCond = AsmCond::ElseCond;
4797 bool LastIgnoreState = false;
4798 if (!TheCondStack.empty())
4799 LastIgnoreState = TheCondStack.back().Ignore;
4800 if (LastIgnoreState || TheCondState.CondMet)
4801 TheCondState.Ignore = true;
4802 else
4803 TheCondState.Ignore = false;
4804
4805 return false;
4806}
4807
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004808/// parseDirectiveEnd
4809/// ::= .end
4810bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004811 if (parseToken(AsmToken::EndOfStatement,
4812 "unexpected token in '.end' directive"))
4813 return true;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004814
4815 while (Lexer.isNot(AsmToken::Eof))
4816 Lex();
4817
4818 return false;
4819}
4820
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004821/// parseDirectiveError
4822/// ::= .err
4823/// ::= .error [string]
4824bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4825 if (!TheCondStack.empty()) {
4826 if (TheCondStack.back().Ignore) {
4827 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004828 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004829 }
4830 }
4831
4832 if (!WithMessage)
4833 return Error(L, ".err encountered");
4834
4835 StringRef Message = ".error directive invoked in source file";
4836 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00004837 if (Lexer.isNot(AsmToken::String))
4838 return TokError(".error argument must be a string");
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004839
4840 Message = getTok().getStringContents();
4841 Lex();
4842 }
4843
Nirav Dave2364748a2016-09-16 18:30:20 +00004844 return Error(L, Message);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004845}
4846
Nico Weber404012b2014-07-24 16:26:06 +00004847/// parseDirectiveWarning
4848/// ::= .warning [string]
4849bool AsmParser::parseDirectiveWarning(SMLoc L) {
4850 if (!TheCondStack.empty()) {
4851 if (TheCondStack.back().Ignore) {
4852 eatToEndOfStatement();
4853 return false;
4854 }
4855 }
4856
4857 StringRef Message = ".warning directive invoked in source file";
4858 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00004859 if (Lexer.isNot(AsmToken::String))
4860 return TokError(".warning argument must be a string");
Nico Weber404012b2014-07-24 16:26:06 +00004861
4862 Message = getTok().getStringContents();
4863 Lex();
4864 }
4865
Nirav Dave2364748a2016-09-16 18:30:20 +00004866 return Warning(L, Message);
Nico Weber404012b2014-07-24 16:26:06 +00004867}
4868
Jim Grosbach4b905842013-09-20 23:08:21 +00004869/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004870/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004871bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Nirav Davea645433c2016-07-18 15:24:03 +00004872 if (parseToken(AsmToken::EndOfStatement,
4873 "unexpected token in '.endif' directive"))
4874 return true;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004875
Jim Grosbach4b905842013-09-20 23:08:21 +00004876 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Nirav Dave2364748a2016-09-16 18:30:20 +00004877 return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
4878 "an .if or .else");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004879 if (!TheCondStack.empty()) {
4880 TheCondState = TheCondStack.back();
4881 TheCondStack.pop_back();
4882 }
4883
4884 return false;
4885}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004886
Eli Bendersky17233942013-01-15 22:59:42 +00004887void AsmParser::initializeDirectiveKindMap() {
4888 DirectiveKindMap[".set"] = DK_SET;
4889 DirectiveKindMap[".equ"] = DK_EQU;
4890 DirectiveKindMap[".equiv"] = DK_EQUIV;
4891 DirectiveKindMap[".ascii"] = DK_ASCII;
4892 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4893 DirectiveKindMap[".string"] = DK_STRING;
4894 DirectiveKindMap[".byte"] = DK_BYTE;
4895 DirectiveKindMap[".short"] = DK_SHORT;
4896 DirectiveKindMap[".value"] = DK_VALUE;
4897 DirectiveKindMap[".2byte"] = DK_2BYTE;
4898 DirectiveKindMap[".long"] = DK_LONG;
4899 DirectiveKindMap[".int"] = DK_INT;
4900 DirectiveKindMap[".4byte"] = DK_4BYTE;
4901 DirectiveKindMap[".quad"] = DK_QUAD;
4902 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004903 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004904 DirectiveKindMap[".single"] = DK_SINGLE;
4905 DirectiveKindMap[".float"] = DK_FLOAT;
4906 DirectiveKindMap[".double"] = DK_DOUBLE;
4907 DirectiveKindMap[".align"] = DK_ALIGN;
4908 DirectiveKindMap[".align32"] = DK_ALIGN32;
4909 DirectiveKindMap[".balign"] = DK_BALIGN;
4910 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4911 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4912 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4913 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4914 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4915 DirectiveKindMap[".org"] = DK_ORG;
4916 DirectiveKindMap[".fill"] = DK_FILL;
4917 DirectiveKindMap[".zero"] = DK_ZERO;
4918 DirectiveKindMap[".extern"] = DK_EXTERN;
4919 DirectiveKindMap[".globl"] = DK_GLOBL;
4920 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004921 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4922 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4923 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4924 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4925 DirectiveKindMap[".reference"] = DK_REFERENCE;
4926 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4927 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4928 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4929 DirectiveKindMap[".comm"] = DK_COMM;
4930 DirectiveKindMap[".common"] = DK_COMMON;
4931 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4932 DirectiveKindMap[".abort"] = DK_ABORT;
4933 DirectiveKindMap[".include"] = DK_INCLUDE;
4934 DirectiveKindMap[".incbin"] = DK_INCBIN;
4935 DirectiveKindMap[".code16"] = DK_CODE16;
4936 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4937 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004938 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004939 DirectiveKindMap[".irp"] = DK_IRP;
4940 DirectiveKindMap[".irpc"] = DK_IRPC;
4941 DirectiveKindMap[".endr"] = DK_ENDR;
4942 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4943 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4944 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4945 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004946 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4947 DirectiveKindMap[".ifge"] = DK_IFGE;
4948 DirectiveKindMap[".ifgt"] = DK_IFGT;
4949 DirectiveKindMap[".ifle"] = DK_IFLE;
4950 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004951 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004952 DirectiveKindMap[".ifb"] = DK_IFB;
4953 DirectiveKindMap[".ifnb"] = DK_IFNB;
4954 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004955 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004956 DirectiveKindMap[".ifnc"] = DK_IFNC;
Sid Manning51c35602015-03-18 14:20:54 +00004957 DirectiveKindMap[".ifnes"] = DK_IFNES;
Eli Bendersky17233942013-01-15 22:59:42 +00004958 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4959 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4960 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4961 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4962 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004963 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004964 DirectiveKindMap[".endif"] = DK_ENDIF;
4965 DirectiveKindMap[".skip"] = DK_SKIP;
4966 DirectiveKindMap[".space"] = DK_SPACE;
4967 DirectiveKindMap[".file"] = DK_FILE;
4968 DirectiveKindMap[".line"] = DK_LINE;
4969 DirectiveKindMap[".loc"] = DK_LOC;
4970 DirectiveKindMap[".stabs"] = DK_STABS;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004971 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00004972 DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004973 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
4974 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
David Majnemer6fcbd7e2016-01-29 19:24:12 +00004975 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
Reid Klecknera9f4cc92016-09-07 16:15:31 +00004976 DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
David Majnemer408b5e62016-02-05 01:55:49 +00004977 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
Reid Kleckner2214ed82016-01-29 00:49:42 +00004978 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
4979 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
Eli Bendersky17233942013-01-15 22:59:42 +00004980 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4981 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4982 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4983 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4984 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4985 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4986 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4987 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4988 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4989 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4990 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4991 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4992 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4993 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4994 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4995 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4996 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4997 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4998 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4999 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
5000 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00005001 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00005002 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
5003 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
5004 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00005005 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00005006 DirectiveKindMap[".endm"] = DK_ENDM;
5007 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
5008 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00005009 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00005010 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00005011 DirectiveKindMap[".warning"] = DK_WARNING;
Daniel Sanders9f6ad492015-11-12 13:33:00 +00005012 DirectiveKindMap[".reloc"] = DK_RELOC;
Petr Hosek731bb9c2016-08-23 21:34:53 +00005013 DirectiveKindMap[".dc"] = DK_DC;
5014 DirectiveKindMap[".dc.a"] = DK_DC_A;
5015 DirectiveKindMap[".dc.b"] = DK_DC_B;
5016 DirectiveKindMap[".dc.d"] = DK_DC_D;
5017 DirectiveKindMap[".dc.l"] = DK_DC_L;
5018 DirectiveKindMap[".dc.s"] = DK_DC_S;
5019 DirectiveKindMap[".dc.w"] = DK_DC_W;
5020 DirectiveKindMap[".dc.x"] = DK_DC_X;
Petr Hosek4cb08ce2016-09-23 19:25:15 +00005021 DirectiveKindMap[".dcb"] = DK_DCB;
5022 DirectiveKindMap[".dcb.b"] = DK_DCB_B;
5023 DirectiveKindMap[".dcb.d"] = DK_DCB_D;
5024 DirectiveKindMap[".dcb.l"] = DK_DCB_L;
5025 DirectiveKindMap[".dcb.s"] = DK_DCB_S;
5026 DirectiveKindMap[".dcb.w"] = DK_DCB_W;
5027 DirectiveKindMap[".dcb.x"] = DK_DCB_X;
Petr Hosek85b2f672016-09-23 21:53:36 +00005028 DirectiveKindMap[".ds"] = DK_DS;
5029 DirectiveKindMap[".ds.b"] = DK_DS_B;
5030 DirectiveKindMap[".ds.d"] = DK_DS_D;
5031 DirectiveKindMap[".ds.l"] = DK_DS_L;
5032 DirectiveKindMap[".ds.p"] = DK_DS_P;
5033 DirectiveKindMap[".ds.s"] = DK_DS_S;
5034 DirectiveKindMap[".ds.w"] = DK_DS_W;
5035 DirectiveKindMap[".ds.x"] = DK_DS_X;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00005036}
5037
Jim Grosbach4b905842013-09-20 23:08:21 +00005038MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005039 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005040
Rafael Espindola34b9c512012-06-03 23:57:14 +00005041 unsigned NestLevel = 0;
Eugene Zelenko33d7b762016-08-23 17:14:32 +00005042 while (true) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005043 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00005044 if (getLexer().is(AsmToken::Eof)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00005045 printError(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00005046 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005047 }
5048
Rafael Espindola34b9c512012-06-03 23:57:14 +00005049 if (Lexer.is(AsmToken::Identifier) &&
Nikolay Haustov95b4fcd2016-03-01 08:18:28 +00005050 (getTok().getIdentifier() == ".rept" ||
5051 getTok().getIdentifier() == ".irp" ||
5052 getTok().getIdentifier() == ".irpc")) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00005053 ++NestLevel;
5054 }
5055
5056 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00005057 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00005058 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005059 EndToken = getTok();
5060 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00005061 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Nirav Dave2364748a2016-09-16 18:30:20 +00005062 printError(getTok().getLoc(),
5063 "unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00005064 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00005065 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005066 break;
5067 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00005068 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005069 }
5070
Rafael Espindola34b9c512012-06-03 23:57:14 +00005071 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00005072 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005073 }
5074
5075 const char *BodyStart = StartToken.getLoc().getPointer();
5076 const char *BodyEnd = EndToken.getLoc().getPointer();
5077 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
5078
Rafael Espindola34b9c512012-06-03 23:57:14 +00005079 // We Are Anonymous.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00005080 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00005081 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00005082}
5083
Jim Grosbach4b905842013-09-20 23:08:21 +00005084void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00005085 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005086 OS << ".endr\n";
5087
Rafael Espindola3560ff22014-08-27 20:03:13 +00005088 std::unique_ptr<MemoryBuffer> Instantiation =
5089 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005090
Rafael Espindola34b9c512012-06-03 23:57:14 +00005091 // Create the macro instantiation object and add to the current macro
5092 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00005093 MacroInstantiation *MI = new MacroInstantiation(
5094 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00005095 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005096
Rafael Espindola34b9c512012-06-03 23:57:14 +00005097 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00005098 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00005099 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00005100 Lex();
5101}
5102
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00005103/// parseDirectiveRept
5104/// ::= .rep | .rept count
5105bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00005106 const MCExpr *CountExpr;
5107 SMLoc CountLoc = getTok().getLoc();
5108 if (parseExpression(CountExpr))
5109 return true;
5110
Rafael Espindola34b9c512012-06-03 23:57:14 +00005111 int64_t Count;
Jim Grosbach13760bd2015-05-30 01:25:56 +00005112 if (!CountExpr->evaluateAsAbsolute(Count)) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00005113 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
5114 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00005115
Nirav Davea645433c2016-07-18 15:24:03 +00005116 if (check(Count < 0, CountLoc, "Count is negative") ||
5117 parseToken(AsmToken::EndOfStatement,
5118 "unexpected token in '" + Dir + "' directive"))
5119 return true;
Rafael Espindola34b9c512012-06-03 23:57:14 +00005120
5121 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00005122 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00005123 if (!M)
5124 return true;
5125
5126 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5127 // to hold the macro body with substitutions.
5128 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00005129 raw_svector_ostream OS(Buf);
5130 while (Count--) {
Toma Tabacu217116e2015-04-27 10:50:29 +00005131 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
5132 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00005133 return true;
5134 }
Jim Grosbach4b905842013-09-20 23:08:21 +00005135 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005136
5137 return false;
5138}
5139
Jim Grosbach4b905842013-09-20 23:08:21 +00005140/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00005141/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00005142bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00005143 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00005144 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00005145 if (check(parseIdentifier(Parameter.Name),
5146 "expected identifier in '.irp' directive") ||
5147 parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
5148 parseMacroArguments(nullptr, A) ||
5149 parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
Rafael Espindola768b41c2012-06-15 14:02:34 +00005150 return true;
5151
Rafael Espindola768b41c2012-06-15 14:02:34 +00005152 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00005153 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00005154 if (!M)
5155 return true;
5156
5157 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5158 // to hold the macro body with substitutions.
5159 SmallString<256> Buf;
5160 raw_svector_ostream OS(Buf);
5161
Craig Topper84008482015-10-10 05:38:14 +00005162 for (const MCAsmMacroArgument &Arg : A) {
Toma Tabacu217116e2015-04-27 10:50:29 +00005163 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
5164 // This is undocumented, but GAS seems to support it.
Craig Topper84008482015-10-10 05:38:14 +00005165 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00005166 return true;
5167 }
5168
Jim Grosbach4b905842013-09-20 23:08:21 +00005169 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00005170
5171 return false;
5172}
5173
Jim Grosbach4b905842013-09-20 23:08:21 +00005174/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005175/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00005176bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00005177 MCAsmMacroParameter Parameter;
Eli Bendersky38274122013-01-14 23:22:36 +00005178 MCAsmMacroArguments A;
Nirav Davea645433c2016-07-18 15:24:03 +00005179
5180 if (check(parseIdentifier(Parameter.Name),
5181 "expected identifier in '.irpc' directive") ||
5182 parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
5183 parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005184 return true;
5185
5186 if (A.size() != 1 || A.front().size() != 1)
5187 return TokError("unexpected token in '.irpc' directive");
5188
5189 // Eat the end of statement.
Nirav Davea645433c2016-07-18 15:24:03 +00005190 if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
5191 return true;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005192
5193 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00005194 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005195 if (!M)
5196 return true;
5197
5198 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5199 // to hold the macro body with substitutions.
5200 SmallString<256> Buf;
5201 raw_svector_ostream OS(Buf);
5202
5203 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00005204 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00005205 MCAsmMacroArgument Arg;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00005206 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005207
Toma Tabacu217116e2015-04-27 10:50:29 +00005208 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
5209 // This is undocumented, but GAS seems to support it.
5210 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005211 return true;
5212 }
5213
Jim Grosbach4b905842013-09-20 23:08:21 +00005214 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00005215
5216 return false;
5217}
5218
Jim Grosbach4b905842013-09-20 23:08:21 +00005219bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00005220 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00005221 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005222
5223 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00005224 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005225 assert(getLexer().is(AsmToken::EndOfStatement));
5226
Jim Grosbach4b905842013-09-20 23:08:21 +00005227 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00005228 return false;
5229}
Rafael Espindola12d73d12010-09-11 16:45:15 +00005230
Jim Grosbach4b905842013-09-20 23:08:21 +00005231bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00005232 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00005233 const MCExpr *Value;
5234 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00005235 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00005236 return true;
5237 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5238 if (!MCE)
5239 return Error(ExprLoc, "unexpected expression in _emit");
5240 uint64_t IntValue = MCE->getValue();
Craig Topper55b1f292015-10-10 20:17:07 +00005241 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
Eli Friedman0f4871d2012-10-22 23:58:19 +00005242 return Error(ExprLoc, "literal value out of range for directive");
5243
Craig Topper7d5b2312015-10-10 05:25:02 +00005244 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
Chad Rosierc7f552c2013-02-12 21:33:51 +00005245 return false;
5246}
5247
Jim Grosbach4b905842013-09-20 23:08:21 +00005248bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00005249 const MCExpr *Value;
5250 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00005251 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00005252 return true;
5253 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5254 if (!MCE)
5255 return Error(ExprLoc, "unexpected expression in align");
5256 uint64_t IntValue = MCE->getValue();
5257 if (!isPowerOf2_64(IntValue))
5258 return Error(ExprLoc, "literal value not a power of two greater then zero");
5259
Craig Topper7d5b2312015-10-10 05:25:02 +00005260 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
Eli Friedman0f4871d2012-10-22 23:58:19 +00005261 return false;
5262}
5263
Chad Rosierf43fcf52013-02-13 21:27:17 +00005264// We are comparing pointers, but the pointers are relative to a single string.
5265// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00005266static int rewritesSort(const AsmRewrite *AsmRewriteA,
5267 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00005268 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
5269 return -1;
5270 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
5271 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005272
Chad Rosierfce4fab2013-04-08 17:43:47 +00005273 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
5274 // rewrite to the same location. Make sure the SizeDirective rewrite is
5275 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
5276 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00005277 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
5278 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005279 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00005280
Jim Grosbach4b905842013-09-20 23:08:21 +00005281 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5282 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00005283 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00005284 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00005285}
5286
Jim Grosbach4b905842013-09-20 23:08:21 +00005287bool AsmParser::parseMSInlineAsm(
5288 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
5289 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
5290 SmallVectorImpl<std::string> &Constraints,
5291 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5292 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00005293 SmallVector<void *, 4> InputDecls;
5294 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00005295 SmallVector<bool, 4> InputDeclsAddressOf;
5296 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00005297 SmallVector<std::string, 4> InputConstraints;
5298 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005299 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00005300
Benjamin Kramer1a136112013-02-15 20:37:21 +00005301 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00005302
5303 // Prime the lexer.
5304 Lex();
5305
5306 // While we have input, parse each statement.
5307 unsigned InputIdx = 0;
5308 unsigned OutputIdx = 0;
5309 while (getLexer().isNot(AsmToken::Eof)) {
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005310 // Parse curly braces marking block start/end
5311 if (parseCurlyBlockScope(AsmStrRewrites))
5312 continue;
5313
Eli Friedman0f4871d2012-10-22 23:58:19 +00005314 ParseStatementInfo Info(&AsmStrRewrites);
Nirav Dave2364748a2016-09-16 18:30:20 +00005315 bool StatementErr = parseStatement(Info, &SI);
Nirav Dave9fa8af22016-09-13 13:55:06 +00005316
Nirav Dave2364748a2016-09-16 18:30:20 +00005317 if (StatementErr || Info.ParseError) {
5318 // Emit pending errors if any exist.
5319 printPendingErrors();
Nico Webere204c482016-09-13 18:17:00 +00005320 return true;
Nirav Dave2364748a2016-09-16 18:30:20 +00005321 }
5322
5323 // No pending error should exist here.
5324 assert(!hasPendingError() && "unexpected error from parseStatement");
Chad Rosier149e8e02012-12-12 22:45:52 +00005325
Benjamin Kramer1a136112013-02-15 20:37:21 +00005326 if (Info.Opcode == ~0U)
5327 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005328
Benjamin Kramer1a136112013-02-15 20:37:21 +00005329 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00005330
Benjamin Kramer1a136112013-02-15 20:37:21 +00005331 // Build the list of clobbers, outputs and inputs.
5332 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00005333 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005334
Benjamin Kramer1a136112013-02-15 20:37:21 +00005335 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00005336 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00005337 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00005338
Benjamin Kramer1a136112013-02-15 20:37:21 +00005339 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00005340 if (Operand.isReg() && !Operand.needAddressOf() &&
5341 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00005342 unsigned NumDefs = Desc.getNumDefs();
5343 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00005344 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5345 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005346 continue;
5347 }
5348
5349 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00005350 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00005351 if (SymName.empty())
5352 continue;
5353
David Blaikie960ea3f2014-06-08 16:18:35 +00005354 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00005355 if (!OpDecl)
5356 continue;
5357
5358 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00005359 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005360 if (isOutput) {
5361 ++InputIdx;
5362 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005363 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
Yaron Keren075759a2015-03-30 15:42:36 +00005364 OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005365 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
Benjamin Kramer1a136112013-02-15 20:37:21 +00005366 } else {
5367 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00005368 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5369 InputConstraints.push_back(Operand.getConstraint().str());
Craig Topper7d5b2312015-10-10 05:25:02 +00005370 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
Chad Rosier8bce6642012-10-18 15:49:34 +00005371 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005372 }
Reid Kleckneree088972013-12-10 18:27:32 +00005373
5374 // Consider implicit defs to be clobbers. Think of cpuid and push.
Craig Toppere5e035a32015-12-05 07:13:35 +00005375 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5376 Desc.getNumImplicitDefs());
David Majnemer8114c1a2014-06-23 02:17:16 +00005377 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00005378 }
5379
5380 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00005381 NumOutputs = OutputDecls.size();
5382 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00005383
5384 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00005385 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5386 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5387 ClobberRegs.end());
5388 Clobbers.assign(ClobberRegs.size(), std::string());
5389 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5390 raw_string_ostream OS(Clobbers[I]);
5391 IP->printRegName(OS, ClobberRegs[I]);
5392 }
Chad Rosier8bce6642012-10-18 15:49:34 +00005393
5394 // Merge the various outputs and inputs. Output are expected first.
5395 if (NumOutputs || NumInputs) {
5396 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00005397 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005398 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00005399 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005400 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005401 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005402 }
5403 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00005404 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00005405 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00005406 }
5407 }
5408
5409 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00005410 std::string AsmStringIR;
5411 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00005412 StringRef ASMString =
5413 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5414 const char *AsmStart = ASMString.begin();
5415 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00005416 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00005417 for (const AsmRewrite &AR : AsmStrRewrites) {
5418 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00005419 if (Kind == AOK_Delete)
5420 continue;
5421
David Majnemer8114c1a2014-06-23 02:17:16 +00005422 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00005423 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00005424
Chad Rosier120eefd2013-03-19 17:32:17 +00005425 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005426 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00005427 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00005428
Chad Rosier37e755c2012-10-23 17:43:43 +00005429 // Skip the original expression.
5430 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00005431 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00005432 continue;
5433 }
5434
Chad Rosierff10ed12013-04-12 16:26:42 +00005435 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00005436 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00005437 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00005438 default:
5439 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005440 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00005441 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00005442 break;
5443 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005444 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00005445 break;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005446 case AOK_Label:
Matt Arsenault4e273432014-12-04 00:06:57 +00005447 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
Ehsan Akhgaridb0e7062014-09-22 02:21:35 +00005448 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005449 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005450 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005451 break;
5452 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00005453 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00005454 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00005455 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00005456 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00005457 default: break;
5458 case 8: OS << "byte ptr "; break;
5459 case 16: OS << "word ptr "; break;
5460 case 32: OS << "dword ptr "; break;
5461 case 64: OS << "qword ptr "; break;
5462 case 80: OS << "xword ptr "; break;
5463 case 128: OS << "xmmword ptr "; break;
5464 case 256: OS << "ymmword ptr "; break;
5465 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00005466 break;
5467 case AOK_Emit:
5468 OS << ".byte";
5469 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005470 case AOK_Align: {
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005471 // MS alignment directives are measured in bytes. If the native assembler
5472 // measures alignment in bytes, we can pass it straight through.
5473 OS << ".align";
5474 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
5475 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00005476
Reid Klecknerfb1c1c72015-10-27 17:32:48 +00005477 // Alignment is in log2 form, so print that instead and skip the original
5478 // immediate.
5479 unsigned Val = AR.Val;
5480 OS << ' ' << Val;
Benjamin Kramer1a136112013-02-15 20:37:21 +00005481 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00005482 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
5483 break;
5484 }
Michael Zuckerman02ecd432015-12-13 17:07:23 +00005485 case AOK_EVEN:
5486 OS << ".even";
5487 break;
Chad Rosierf0e87202012-10-25 20:41:34 +00005488 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005489 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00005490 OS.flush();
5491 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00005492 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00005493 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00005494 break;
Marina Yatsina5f5de9f2016-03-07 18:11:16 +00005495 case AOK_EndOfStatement:
5496 OS << "\n\t";
5497 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00005498 }
Chad Rosier0f48c552012-10-19 20:57:14 +00005499
Chad Rosier8bce6642012-10-18 15:49:34 +00005500 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00005501 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00005502 }
5503
5504 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00005505 if (AsmStart != AsmEnd)
5506 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00005507
5508 AsmString = OS.str();
5509 return false;
5510}
5511
Pete Cooper80d21cb2015-06-22 19:35:57 +00005512namespace llvm {
5513namespace MCParserUtils {
5514
5515/// Returns whether the given symbol is used anywhere in the given expression,
5516/// or subexpressions.
5517static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
5518 switch (Value->getKind()) {
5519 case MCExpr::Binary: {
5520 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
5521 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
5522 isSymbolUsedInExpression(Sym, BE->getRHS());
5523 }
5524 case MCExpr::Target:
5525 case MCExpr::Constant:
5526 return false;
5527 case MCExpr::SymbolRef: {
5528 const MCSymbol &S =
5529 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
5530 if (S.isVariable())
5531 return isSymbolUsedInExpression(Sym, S.getVariableValue());
5532 return &S == Sym;
5533 }
5534 case MCExpr::Unary:
5535 return isSymbolUsedInExpression(
5536 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
5537 }
5538
5539 llvm_unreachable("Unknown expr kind!");
5540}
5541
5542bool parseAssignmentExpression(StringRef Name, bool allow_redef,
5543 MCAsmParser &Parser, MCSymbol *&Sym,
5544 const MCExpr *&Value) {
Pete Cooper80d21cb2015-06-22 19:35:57 +00005545
5546 // FIXME: Use better location, we should use proper tokens.
Nirav Davefd910412016-06-17 16:06:17 +00005547 SMLoc EqualLoc = Parser.getTok().getLoc();
Pete Cooper80d21cb2015-06-22 19:35:57 +00005548
5549 if (Parser.parseExpression(Value)) {
5550 Parser.TokError("missing expression");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005551 return true;
5552 }
5553
5554 // Note: we don't count b as used in "a = b". This is to allow
5555 // a = b
5556 // b = c
5557
Nirav Davefd910412016-06-17 16:06:17 +00005558 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
Pete Cooper80d21cb2015-06-22 19:35:57 +00005559 return Parser.TokError("unexpected token in assignment");
5560
5561 // Eat the end of statement marker.
5562 Parser.Lex();
5563
5564 // Validate that the LHS is allowed to be a variable (either it has not been
5565 // used as a symbol, or it is an absolute symbol).
5566 Sym = Parser.getContext().lookupSymbol(Name);
5567 if (Sym) {
5568 // Diagnose assignment to a label.
5569 //
5570 // FIXME: Diagnostics. Note the location of the definition as a label.
5571 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
5572 if (isSymbolUsedInExpression(Sym, Value))
5573 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
Vedant Kumar86dbd922015-08-31 17:44:53 +00005574 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
5575 !Sym->isVariable())
Pete Cooper80d21cb2015-06-22 19:35:57 +00005576 ; // Allow redefinitions of undefined symbols only used in directives.
5577 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
5578 ; // Allow redefinitions of variables that haven't yet been used.
5579 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
5580 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
5581 else if (!Sym->isVariable())
5582 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
5583 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
5584 return Parser.Error(EqualLoc,
5585 "invalid reassignment of non-absolute variable '" +
5586 Name + "'");
Pete Cooper80d21cb2015-06-22 19:35:57 +00005587 } else if (Name == ".") {
Rafael Espindola7ae65d82015-11-04 23:59:18 +00005588 Parser.getStreamer().emitValueToOffset(Value, 0);
Pete Cooper80d21cb2015-06-22 19:35:57 +00005589 return false;
5590 } else
5591 Sym = Parser.getContext().getOrCreateSymbol(Name);
5592
5593 Sym->setRedefinable(allow_redef);
5594
5595 return false;
5596}
5597
Eugene Zelenko33d7b762016-08-23 17:14:32 +00005598} // end namespace MCParserUtils
5599} // end namespace llvm
Pete Cooper80d21cb2015-06-22 19:35:57 +00005600
Daniel Dunbar01e36072010-07-17 02:26:10 +00005601/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00005602MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
5603 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00005604 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00005605}