blob: 5e44266e26ebc15242d7a217abc4bf1919ffcd19 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000048static cl::opt<bool>
49FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
51
Eric Christophera7c32732012-12-18 00:30:54 +000052MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000053
Daniel Dunbar86033402010-07-12 17:54:38 +000054namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000058
59struct MCAsmMacroParameter {
60 StringRef Name;
61 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000062 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000063 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000064
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000065 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000066};
67
Eli Benderskya313ae62013-01-16 18:56:50 +000068typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
69
70struct MCAsmMacro {
71 StringRef Name;
72 StringRef Body;
73 MCAsmMacroParameters Parameters;
74
75public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000076 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000077 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000078};
79
Daniel Dunbar43235712010-07-18 18:54:11 +000080/// \brief Helper class for storing information about an active macro
81/// instantiation.
82struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000083 /// The location of the instantiation.
84 SMLoc InstantiationLoc;
85
Daniel Dunbar40f1d852012-12-01 01:38:48 +000086 /// The buffer where parsing should resume upon instantiation completion.
87 int ExitBuffer;
88
Daniel Dunbar43235712010-07-18 18:54:11 +000089 /// The location where parsing should resume upon instantiation completion.
90 SMLoc ExitLoc;
91
Nico Weber155dccd12014-07-24 17:08:39 +000092 /// The depth of TheCondStack at the start of the instantiation.
93 size_t CondStackDepth;
94
Daniel Dunbar43235712010-07-18 18:54:11 +000095public:
Rafael Espindolaf43a94e2014-08-17 22:48:55 +000096 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, StringRef I,
Nico Weber155dccd12014-07-24 17:08:39 +000097 size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000098};
99
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000102 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 unsigned Opcode;
106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000108 bool ParseError;
109
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 SmallVectorImpl<AsmRewrite> *AsmRewrites;
111
Craig Topper353eda42014-04-24 06:44:33 +0000112 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000114 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000115};
116
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000117/// \brief The concrete assembly parser instance.
118class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000119 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
120 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121private:
122 AsmLexer Lexer;
123 MCContext &Ctx;
124 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000125 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000127 SourceMgr::DiagHandlerTy SavedDiagHandler;
128 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000130
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131 /// This is the current buffer index we're lexing from as managed by the
132 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000133 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134
135 AsmCond TheCondState;
136 std::vector<AsmCond> TheCondStack;
137
Jim Grosbach4b905842013-09-20 23:08:21 +0000138 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000139 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000140 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000141 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000142
Jim Grosbach4b905842013-09-20 23:08:21 +0000143 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000144 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000147 std::vector<MacroInstantiation*> ActiveMacros;
148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000150 std::deque<MCAsmMacro> MacroLikeBodies;
151
Daniel Dunbar828984f2010-07-18 18:38:02 +0000152 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000153 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000154
Daniel Dunbar43325c42010-09-09 22:42:56 +0000155 /// Flag tracking whether any errors have been encountered.
156 unsigned HadError : 1;
157
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000158 /// The values from the last parsed cpp hash file line comment if any.
159 StringRef CppHashFilename;
160 int64_t CppHashLineNumber;
161 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000162 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000163 /// When generating dwarf for assembly source files we need to calculate the
164 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000165 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
167 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000168 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000170
Devang Patela173ee52012-01-31 18:14:05 +0000171 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
172 unsigned AssemblerDialect;
173
Jim Grosbach4b905842013-09-20 23:08:21 +0000174 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000175 bool IsDarwin;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000178 bool ParsingInlineAsm;
179
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000181 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000183 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184
Craig Topper59be68f2014-03-08 07:14:16 +0000185 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186
Craig Topper59be68f2014-03-08 07:14:16 +0000187 void addDirectiveHandler(StringRef Directive,
188 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000189 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 }
191
192public:
193 /// @name MCAsmParser Interface
194 /// {
195
Craig Topper59be68f2014-03-08 07:14:16 +0000196 SourceMgr &getSourceManager() override { return SrcMgr; }
197 MCAsmLexer &getLexer() override { return Lexer; }
198 MCContext &getContext() override { return Ctx; }
199 MCStreamer &getStreamer() override { return Out; }
200 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000201 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000202 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000203 else
204 return AssemblerDialect;
205 }
Craig Topper59be68f2014-03-08 07:14:16 +0000206 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000207 AssemblerDialect = i;
208 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000209
Craig Topper59be68f2014-03-08 07:14:16 +0000210 void Note(SMLoc L, const Twine &Msg,
211 ArrayRef<SMRange> Ranges = None) override;
212 bool Warning(SMLoc L, const Twine &Msg,
213 ArrayRef<SMRange> Ranges = None) override;
214 bool Error(SMLoc L, const Twine &Msg,
215 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000216
Craig Topper59be68f2014-03-08 07:14:16 +0000217 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper59be68f2014-03-08 07:14:16 +0000219 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
220 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000221
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000222 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000223 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000224 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000227 const MCInstrInfo *MII, const MCInstPrinter *IP,
228 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000229
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000230 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000231 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
232 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
233 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
234 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000235
Jim Grosbach4b905842013-09-20 23:08:21 +0000236 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000237 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000238 bool parseIdentifier(StringRef &Res) override;
239 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240
Craig Topper59be68f2014-03-08 07:14:16 +0000241 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000242 /// }
243
244private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 bool parseStatement(ParseStatementInfo &Info);
247 void eatToEndOfLine();
248 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000249
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000251 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000252 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000253 ArrayRef<MCAsmMacroParameter> Parameters,
254 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000255 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000256
Eli Benderskya313ae62013-01-16 18:56:50 +0000257 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000259
260 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Lookup a previously defined macro.
264 /// \param Name Macro name.
265 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000277 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000278 ///
279 /// \param M The macro.
280 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000281 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000282
283 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
David Majnemer91fc4c22014-01-29 18:57:46 +0000286 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000287 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000290 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void printMacroInstantiations();
293 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000294 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000295 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000296 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000297 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000298
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 /// \brief Enter the specified file. This returns true on failure.
300 bool enterIncludeFile(const std::string &Filename);
301
302 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000303 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000304 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000305
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000306 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000307 /// current token is not set; clients should ensure Lex() is called
308 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000309 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000310 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000311 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000312 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314 /// \brief Parse up to the end of statement and a return the contents from the
315 /// current token until the end of the statement; the current token on exit
316 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000317 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000318
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000319 /// \brief Parse until the end of a statement or a comma is encountered,
320 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000321 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322
Jim Grosbach4b905842013-09-20 23:08:21 +0000323 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000324 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
327 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
328 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000329
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000331
Eli Bendersky17233942013-01-15 22:59:42 +0000332 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000333 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000334 DK_NO_DIRECTIVE, // Placeholder
335 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000336 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
337 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000338 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000339 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000340 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000341 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
342 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
343 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
344 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000345 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
346 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
347 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000348 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
349 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
350 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
351 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
352 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
353 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000354 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000355 DK_MACROS_ON, DK_MACROS_OFF,
356 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000357 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000358 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000360 };
361
Jim Grosbach4b905842013-09-20 23:08:21 +0000362 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000363 /// directives parsed by this class.
364 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000365
366 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000367 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
368 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000369 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
371 bool parseDirectiveFill(); // ".fill"
372 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000373 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
375 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000376 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Eli Bendersky17233942013-01-15 22:59:42 +0000379 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveFile(SMLoc DirectiveLoc);
381 bool parseDirectiveLine();
382 bool parseDirectiveLoc();
383 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000384
385 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000387 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFISections();
389 bool parseDirectiveCFIStartProc();
390 bool parseDirectiveCFIEndProc();
391 bool parseDirectiveCFIDefCfaOffset();
392 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIAdjustCfaOffset();
394 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
398 bool parseDirectiveCFIRememberState();
399 bool parseDirectiveCFIRestoreState();
400 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIEscape();
403 bool parseDirectiveCFISignalFrame();
404 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000405
406 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000407 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000408 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectiveEndMacro(StringRef Directive);
410 bool parseDirectiveMacro(SMLoc DirectiveLoc);
411 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000412
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419
Eli Bendersky17233942013-01-15 22:59:42 +0000420 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000422
423 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000429
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveAbort(); // ".abort"
433 bool parseDirectiveInclude(); // ".include"
434 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000436 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
437 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000438 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000440 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000442 // ".ifeqs"
443 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000444 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
446 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
447 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
448 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000449 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000450
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000452 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000453
Rafael Espindola34b9c512012-06-03 23:57:14 +0000454 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
456 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000457 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000458 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000459 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
460 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
461 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000462
Chad Rosierc7f552c2013-02-12 21:33:51 +0000463 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000465 size_t Len);
466
467 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000468 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000469
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000470 // "end"
471 bool parseDirectiveEnd(SMLoc DirectiveLoc);
472
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000473 // ".err" or ".error"
474 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000475
Nico Weber404012b2014-07-24 16:26:06 +0000476 // ".warning"
477 bool parseDirectiveWarning(SMLoc DirectiveLoc);
478
Eli Bendersky17233942013-01-15 22:59:42 +0000479 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000480};
Daniel Dunbar86033402010-07-12 17:54:38 +0000481}
482
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000483namespace llvm {
484
485extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000486extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000487extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000488
489}
490
Chris Lattnerc35681b2010-01-19 19:46:13 +0000491enum { DEFAULT_ADDRSPACE = 0 };
492
Jim Grosbach4b905842013-09-20 23:08:21 +0000493AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
494 const MCAsmInfo &_MAI)
495 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000496 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
497 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
498 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000499 // Save the old handler.
500 SavedDiagHandler = SrcMgr.getDiagHandler();
501 SavedDiagContext = SrcMgr.getDiagContext();
502 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000503 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000504 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000505
Daniel Dunbarc5011082010-07-12 18:12:02 +0000506 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000507 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
508 case MCObjectFileInfo::IsCOFF:
509 PlatformParser = createCOFFAsmParser();
510 PlatformParser->Initialize(*this);
511 break;
512 case MCObjectFileInfo::IsMachO:
513 PlatformParser = createDarwinAsmParser();
514 PlatformParser->Initialize(*this);
515 IsDarwin = true;
516 break;
517 case MCObjectFileInfo::IsELF:
518 PlatformParser = createELFAsmParser();
519 PlatformParser->Initialize(*this);
520 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000521 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000522
Eli Bendersky17233942013-01-15 22:59:42 +0000523 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000524}
525
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000526AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000527 assert((HadError || ActiveMacros.empty()) &&
528 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000529
530 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000531 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
532 ie = MacroMap.end();
533 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000534 delete it->getValue();
535
Daniel Dunbarc5011082010-07-12 18:12:02 +0000536 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000537}
538
Jim Grosbach4b905842013-09-20 23:08:21 +0000539void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000540 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000541 for (std::vector<MacroInstantiation *>::const_reverse_iterator
542 it = ActiveMacros.rbegin(),
543 ie = ActiveMacros.rend();
544 it != ie; ++it)
545 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000546 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000547}
548
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000549void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
550 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
551 printMacroInstantiations();
552}
553
Chris Lattnera3a06812011-10-16 04:47:35 +0000554bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000555 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000556 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000557 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
558 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000559 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000560}
561
Chris Lattnera3a06812011-10-16 04:47:35 +0000562bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000563 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000564 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
565 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000566 return true;
567}
568
Jim Grosbach4b905842013-09-20 23:08:21 +0000569bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000570 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000571 unsigned NewBuf =
572 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
573 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000575
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000577 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000578 return false;
579}
Daniel Dunbar43235712010-07-18 18:54:11 +0000580
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000581/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000582/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000584bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000586 unsigned NewBuf =
587 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
588 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000589 return true;
590
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000591 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000592 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000593 return false;
594}
595
Alp Tokera55b95b2014-07-06 10:33:31 +0000596void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
597 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000598 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
599 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000600}
601
Sean Callanan7a77eae2010-01-21 00:19:58 +0000602const AsmToken &AsmParser::Lex() {
603 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605 if (tok->is(AsmToken::Eof)) {
606 // If this is the end of an included file, pop the parent file off the
607 // include stack.
608 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
609 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000610 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 tok = &Lexer.Lex();
612 }
613 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000614
Sean Callanan7a77eae2010-01-21 00:19:58 +0000615 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000616 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000617
Sean Callanan7a77eae2010-01-21 00:19:58 +0000618 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000619}
620
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000621bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000622 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000623 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000624 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000625
Chris Lattner36e02122009-06-21 20:54:55 +0000626 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000627 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000628
629 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000630 AsmCond StartingCondState = TheCondState;
631
Kevin Enderby6469fc22011-11-01 22:27:22 +0000632 // If we are generating dwarf for assembly source files save the initial text
633 // section and generate a .file directive.
634 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000635 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
636 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000637 auto InsertResult = getContext().addGenDwarfSection(
638 getStreamer().getCurrentSection().first);
639 assert(InsertResult.second && ".text section should not have debug info yet");
640 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000641 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
642 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000643 }
644
Chris Lattner73f36112009-07-02 21:53:43 +0000645 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000646 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000647 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000648 if (!parseStatement(Info))
649 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000650
Daniel Dunbar43325c42010-09-09 22:42:56 +0000651 // We had an error, validate that one was emitted and recover by skipping to
652 // the next line.
653 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000654 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000655 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000656
657 if (TheCondState.TheCond != StartingCondState.TheCond ||
658 TheCondState.Ignore != StartingCondState.Ignore)
659 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000660
661 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000662 const auto &LineTables = getContext().getMCDwarfLineTables();
663 if (!LineTables.empty()) {
664 unsigned Index = 0;
665 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
666 if (File.Name.empty() && Index != 0)
667 TokError("unassigned file number: " + Twine(Index) +
668 " for .file directives");
669 ++Index;
670 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000671 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000672
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000673 // Check to see that all assembler local symbols were actually defined.
674 // Targets that don't do subsections via symbols may not want this, though,
675 // so conservatively exclude them. Only do this if we're finalizing, though,
676 // as otherwise we won't necessarilly have seen everything yet.
677 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
678 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
679 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000680 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000681 i != e; ++i) {
682 MCSymbol *Sym = i->getValue();
683 // Variable symbols may not be marked as defined, so check those
684 // explicitly. If we know it's a variable, we have a definition for
685 // the purposes of this check.
686 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
687 // FIXME: We would really like to refer back to where the symbol was
688 // first referenced for a source location. We need to add something
689 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000690 printMessage(
691 getLexer().getLoc(), SourceMgr::DK_Error,
692 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000693 }
694 }
695
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000696 // Finalize the output stream if there are no errors and if the client wants
697 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000698 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000699 Out.Finish();
700
Chris Lattner73f36112009-07-02 21:53:43 +0000701 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000702}
703
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000704void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000705 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000706 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000707 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000708 }
709}
710
Jim Grosbach4b905842013-09-20 23:08:21 +0000711/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000712void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000713 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000714 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000715
Chris Lattnere5074c42009-06-22 01:29:09 +0000716 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000717 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000718 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000719}
720
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000721StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000722 const char *Start = getTok().getLoc().getPointer();
723
Jim Grosbach4b905842013-09-20 23:08:21 +0000724 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000725 Lex();
726
727 const char *End = getTok().getLoc().getPointer();
728 return StringRef(Start, End - Start);
729}
Chris Lattner78db3622009-06-22 05:51:26 +0000730
Jim Grosbach4b905842013-09-20 23:08:21 +0000731StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000732 const char *Start = getTok().getLoc().getPointer();
733
734 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000735 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000736 Lex();
737
738 const char *End = getTok().getLoc().getPointer();
739 return StringRef(Start, End - Start);
740}
741
Jim Grosbach4b905842013-09-20 23:08:21 +0000742/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000743/// NOTE: This assumes the leading '(' has already been consumed.
744///
745/// parenexpr ::= expr)
746///
Jim Grosbach4b905842013-09-20 23:08:21 +0000747bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
748 if (parseExpression(Res))
749 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000750 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000751 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000752 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000753 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000754 return false;
755}
Chris Lattner78db3622009-06-22 05:51:26 +0000756
Jim Grosbach4b905842013-09-20 23:08:21 +0000757/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000758/// NOTE: This assumes the leading '[' has already been consumed.
759///
760/// bracketexpr ::= expr]
761///
Jim Grosbach4b905842013-09-20 23:08:21 +0000762bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
763 if (parseExpression(Res))
764 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000765 if (Lexer.isNot(AsmToken::RBrac))
766 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000767 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000768 Lex();
769 return false;
770}
771
Jim Grosbach4b905842013-09-20 23:08:21 +0000772/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000773/// primaryexpr ::= (parenexpr
774/// primaryexpr ::= symbol
775/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000776/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000777/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000778bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000779 SMLoc FirstTokenLoc = getLexer().getLoc();
780 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
781 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000782 default:
783 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000784 // If we have an error assume that we've already handled it.
785 case AsmToken::Error:
786 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000787 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000788 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000789 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000790 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000791 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000792 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000793 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000794 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000795 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000796 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000797 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000798 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000799 if (FirstTokenKind == AsmToken::Dollar) {
800 if (Lexer.getMAI().getDollarIsPC()) {
801 // This is a '$' reference, which references the current PC. Emit a
802 // temporary label to the streamer and refer to it.
803 MCSymbol *Sym = Ctx.CreateTempSymbol();
804 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000805 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
806 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000807 EndLoc = FirstTokenLoc;
808 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000809 }
810 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000811 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000812 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000813 // Parse symbol variant
814 std::pair<StringRef, StringRef> Split;
815 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000816 if (FirstTokenKind == AsmToken::String) {
817 if (Lexer.is(AsmToken::At)) {
818 Lexer.Lex(); // eat @
819 SMLoc AtLoc = getLexer().getLoc();
820 StringRef VName;
821 if (parseIdentifier(VName))
822 return Error(AtLoc, "expected symbol variant after '@'");
823
824 Split = std::make_pair(Identifier, VName);
825 }
826 } else {
827 Split = Identifier.split('@');
828 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000829 } else if (Lexer.is(AsmToken::LParen)) {
830 Lexer.Lex(); // eat (
831 StringRef VName;
832 parseIdentifier(VName);
833 if (Lexer.isNot(AsmToken::RParen)) {
834 return Error(Lexer.getTok().getLoc(),
835 "unexpected token in variant, expected ')'");
836 }
837 Lexer.Lex(); // eat )
838 Split = std::make_pair(Identifier, VName);
839 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000840
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000841 EndLoc = SMLoc::getFromPointer(Identifier.end());
842
Daniel Dunbard20cda02009-10-16 01:34:54 +0000843 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000844 StringRef SymbolName = Identifier;
845 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000846
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000847 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000848 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000849 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000850 if (Variant != MCSymbolRefExpr::VK_Invalid) {
851 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000852 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000853 Variant = MCSymbolRefExpr::VK_None;
854 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000855 return Error(SMLoc::getFromPointer(Split.second.begin()),
856 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000857 }
858 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000859
Hans Wennborgce69d772013-10-18 20:46:28 +0000860 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
861
Daniel Dunbard20cda02009-10-16 01:34:54 +0000862 // If this is an absolute variable reference, substitute it now to preserve
863 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000864 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000865 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000866 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000867
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000868 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000869 return false;
870 }
871
872 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000873 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000874 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000875 }
David Woodhousef42a6662014-02-01 16:20:54 +0000876 case AsmToken::BigNum:
877 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000878 case AsmToken::Integer: {
879 SMLoc Loc = getTok().getLoc();
880 int64_t IntVal = getTok().getIntVal();
881 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000882 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000883 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 // Look for 'b' or 'f' following an Integer as a directional label
885 if (Lexer.getKind() == AsmToken::Identifier) {
886 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000887 // Lookup the symbol variant if used.
888 std::pair<StringRef, StringRef> Split = IDVal.split('@');
889 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
890 if (Split.first.size() != IDVal.size()) {
891 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000892 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000893 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000894 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000895 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000896 if (IDVal == "f" || IDVal == "b") {
897 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000898 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000899 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000900 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000901 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000902 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000903 Lex(); // Eat identifier.
904 }
905 }
Chris Lattner78db3622009-06-22 05:51:26 +0000906 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000907 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000908 case AsmToken::Real: {
909 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000910 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000911 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000912 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000913 Lex(); // Eat token.
914 return false;
915 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000916 case AsmToken::Dot: {
917 // This is a '.' reference, which references the current PC. Emit a
918 // temporary label to the streamer and refer to it.
919 MCSymbol *Sym = Ctx.CreateTempSymbol();
920 Out.EmitLabel(Sym);
921 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000922 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000923 Lex(); // Eat identifier.
924 return false;
925 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000926 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000927 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000929 case AsmToken::LBrac:
930 if (!PlatformParser->HasBracketExpressions())
931 return TokError("brackets expression not supported on this target");
932 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000933 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000934 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000935 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000936 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000938 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000940 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000941 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000942 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000943 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000944 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000945 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000946 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000947 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000948 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000949 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000950 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000951 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000952 }
953}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000954
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000955bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000956 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000957 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000958}
959
Daniel Dunbar55f16672010-09-17 02:47:07 +0000960const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000961AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000962 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000963 // Ask the target implementation about this expression first.
964 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
965 if (NewE)
966 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000967 // Recurse over the given expression, rebuilding it to apply the given variant
968 // if there is exactly one symbol.
969 switch (E->getKind()) {
970 case MCExpr::Target:
971 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000972 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000973
974 case MCExpr::SymbolRef: {
975 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
976
977 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000978 TokError("invalid variant on expression '" + getTok().getIdentifier() +
979 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000980 return E;
981 }
982
983 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
984 }
985
986 case MCExpr::Unary: {
987 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000990 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000991 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
992 }
993
994 case MCExpr::Binary: {
995 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000996 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
997 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998
999 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001000 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001
Jim Grosbach4b905842013-09-20 23:08:21 +00001002 if (!LHS)
1003 LHS = BE->getLHS();
1004 if (!RHS)
1005 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001006
1007 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1008 }
1009 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001010
Craig Toppera2886c22012-02-07 05:05:23 +00001011 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001012}
1013
Jim Grosbach4b905842013-09-20 23:08:21 +00001014/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001015///
Jim Grosbachbd164242011-08-20 16:24:13 +00001016/// expr ::= expr &&,|| expr -> lowest.
1017/// expr ::= expr |,^,&,! expr
1018/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1019/// expr ::= expr <<,>> expr
1020/// expr ::= expr +,- expr
1021/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001022/// expr ::= primaryexpr
1023///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001024bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001025 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001026 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001027 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001028 return true;
1029
Daniel Dunbar55f16672010-09-17 02:47:07 +00001030 // As a special case, we support 'a op b @ modifier' by rewriting the
1031 // expression to include the modifier. This is inefficient, but in general we
1032 // expect users to use 'a@modifier op b'.
1033 if (Lexer.getKind() == AsmToken::At) {
1034 Lex();
1035
1036 if (Lexer.isNot(AsmToken::Identifier))
1037 return TokError("unexpected symbol modifier following '@'");
1038
1039 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001040 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 if (Variant == MCSymbolRefExpr::VK_Invalid)
1042 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1043
Jim Grosbach4b905842013-09-20 23:08:21 +00001044 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001045 if (!ModifiedRes) {
1046 return TokError("invalid modifier '" + getTok().getIdentifier() +
1047 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001049
Daniel Dunbar55f16672010-09-17 02:47:07 +00001050 Res = ModifiedRes;
1051 Lex();
1052 }
1053
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001054 // Try to constant fold it up front, if possible.
1055 int64_t Value;
1056 if (Res->EvaluateAsAbsolute(Value))
1057 Res = MCConstantExpr::Create(Value, getContext());
1058
1059 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001060}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001061
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001062bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001063 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001064 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001065}
1066
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001067bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001068 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001069
Daniel Dunbar75630b32009-06-30 02:10:03 +00001070 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001071 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001072 return true;
1073
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001074 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001075 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001076
1077 return false;
1078}
1079
Michael J. Spencer530ce852010-10-09 11:00:50 +00001080static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001082 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001083 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001085
Jim Grosbach4b905842013-09-20 23:08:21 +00001086 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001089 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001092 return 1;
1093
Jim Grosbach4b905842013-09-20 23:08:21 +00001094 // Low Precedence: |, &, ^
1095 //
1096 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001097 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001098 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001099 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001100 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001101 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001102 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001103 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001104 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001105 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001106
Jim Grosbach4b905842013-09-20 23:08:21 +00001107 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001108 case AsmToken::EqualEqual:
1109 Kind = MCBinaryExpr::EQ;
1110 return 3;
1111 case AsmToken::ExclaimEqual:
1112 case AsmToken::LessGreater:
1113 Kind = MCBinaryExpr::NE;
1114 return 3;
1115 case AsmToken::Less:
1116 Kind = MCBinaryExpr::LT;
1117 return 3;
1118 case AsmToken::LessEqual:
1119 Kind = MCBinaryExpr::LTE;
1120 return 3;
1121 case AsmToken::Greater:
1122 Kind = MCBinaryExpr::GT;
1123 return 3;
1124 case AsmToken::GreaterEqual:
1125 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001126 return 3;
1127
Jim Grosbach4b905842013-09-20 23:08:21 +00001128 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 case AsmToken::LessLess:
1130 Kind = MCBinaryExpr::Shl;
1131 return 4;
1132 case AsmToken::GreaterGreater:
1133 Kind = MCBinaryExpr::Shr;
1134 return 4;
1135
Jim Grosbach4b905842013-09-20 23:08:21 +00001136 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001137 case AsmToken::Plus:
1138 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001140 case AsmToken::Minus:
1141 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001143
Jim Grosbach4b905842013-09-20 23:08:21 +00001144 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001145 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001146 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001147 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001148 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001149 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001150 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001151 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001152 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001153 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154 }
1155}
1156
Jim Grosbach4b905842013-09-20 23:08:21 +00001157/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001159bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001160 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001162 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001163 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001164
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001165 // If the next token is lower precedence than we are allowed to eat, return
1166 // successfully with what we ate already.
1167 if (TokPrec < Precedence)
1168 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001169
Sean Callanan686ed8d2010-01-19 20:22:31 +00001170 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001171
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001173 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001174 if (parsePrimaryExpr(RHS, EndLoc))
1175 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001176
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001177 // If BinOp binds less tightly with RHS than the operator after RHS, let
1178 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001179 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001180 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001181 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1182 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001183
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001184 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001185 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001186 }
1187}
1188
Chris Lattner36e02122009-06-21 20:54:55 +00001189/// ParseStatement:
1190/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001191/// ::= Label* Directive ...Operands... EndOfStatement
1192/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001193bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001195 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001196 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001197 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001198 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001199
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001201 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001202 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001203 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001205 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001206 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001207 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001208
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001209 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001210 if (Lexer.is(AsmToken::Integer)) {
1211 LocalLabelVal = getTok().getIntVal();
1212 if (LocalLabelVal < 0) {
1213 if (!TheCondState.Ignore)
1214 return TokError("unexpected token at start of statement");
1215 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001216 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001217 IDVal = getTok().getString();
1218 Lex(); // Consume the integer token to be used as an identifier token.
1219 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001220 if (!TheCondState.Ignore)
1221 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001222 }
1223 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001224 } else if (Lexer.is(AsmToken::Dot)) {
1225 // Treat '.' as a valid identifier in this context.
1226 Lex();
1227 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001228 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001229 if (!TheCondState.Ignore)
1230 return TokError("unexpected token at start of statement");
1231 IDVal = "";
1232 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001233
Chris Lattner926885c2010-04-17 18:14:27 +00001234 // Handle conditional assembly here before checking for skipping. We
1235 // have to do this so that .endif isn't skipped in a ".if 0" block for
1236 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001237 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001238 DirectiveKindMap.find(IDVal);
1239 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1240 ? DK_NO_DIRECTIVE
1241 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001242 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001243 default:
1244 break;
1245 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001246 case DK_IFEQ:
1247 case DK_IFGE:
1248 case DK_IFGT:
1249 case DK_IFLE:
1250 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001251 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001252 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001253 case DK_IFB:
1254 return parseDirectiveIfb(IDLoc, true);
1255 case DK_IFNB:
1256 return parseDirectiveIfb(IDLoc, false);
1257 case DK_IFC:
1258 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001259 case DK_IFEQS:
1260 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001261 case DK_IFNC:
1262 return parseDirectiveIfc(IDLoc, false);
1263 case DK_IFDEF:
1264 return parseDirectiveIfdef(IDLoc, true);
1265 case DK_IFNDEF:
1266 case DK_IFNOTDEF:
1267 return parseDirectiveIfdef(IDLoc, false);
1268 case DK_ELSEIF:
1269 return parseDirectiveElseIf(IDLoc);
1270 case DK_ELSE:
1271 return parseDirectiveElse(IDLoc);
1272 case DK_ENDIF:
1273 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001274 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001275
Eli Bendersky88024712013-01-16 19:32:36 +00001276 // Ignore the statement if in the middle of inactive conditional
1277 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001278 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001279 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001280 return false;
1281 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001283 // FIXME: Recurse on local labels?
1284
1285 // See what kind of statement we have.
1286 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001287 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001288 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001289
Chris Lattner36e02122009-06-21 20:54:55 +00001290 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001291 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001292
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001293 // Diagnose attempt to use '.' as a label.
1294 if (IDVal == ".")
1295 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1296
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001297 // Diagnose attempt to use a variable as a label.
1298 //
1299 // FIXME: Diagnostics. Note the location of the definition as a label.
1300 // FIXME: This doesn't diagnose assignment to a symbol which has been
1301 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001302 MCSymbol *Sym;
1303 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001304 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001305 else
1306 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001307 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001308 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001309
Daniel Dunbare73b2672009-08-26 22:13:22 +00001310 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001311 if (!ParsingInlineAsm)
1312 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001313
Kevin Enderbye7739d42011-12-09 18:09:40 +00001314 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001315 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001316 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001317 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1318 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001319
Tim Northover1744d0a2013-10-25 12:49:50 +00001320 getTargetParser().onLabelParsed(Sym);
1321
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001322 // Consume any end of statement token, if present, to avoid spurious
1323 // AddBlankLine calls().
1324 if (Lexer.is(AsmToken::EndOfStatement)) {
1325 Lex();
1326 if (Lexer.is(AsmToken::Eof))
1327 return false;
1328 }
1329
Eli Friedman0f4871d2012-10-22 23:58:19 +00001330 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001331 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001332
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001333 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001334 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001335 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001336
Jim Grosbach4b905842013-09-20 23:08:21 +00001337 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001338
1339 default: // Normal instruction or directive.
1340 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001341 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001342
1343 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001344 if (areMacrosEnabled())
1345 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1346 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001347 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001348
Michael J. Spencer530ce852010-10-09 11:00:50 +00001349 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001350
Eli Bendersky17233942013-01-15 22:59:42 +00001351 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001352 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001353 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001354 //
Eli Bendersky17233942013-01-15 22:59:42 +00001355 // 1. The target-specific assembly parser. Some directives are target
1356 // specific or may potentially behave differently on certain targets.
1357 // 2. Asm parser extensions. For example, platform-specific parsers
1358 // (like the ELF parser) register themselves as extensions.
1359 // 3. The generic directive parser implemented by this class. These are
1360 // all the directives that behave in a target and platform independent
1361 // manner, or at least have a default behavior that's shared between
1362 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001363
Eli Bendersky17233942013-01-15 22:59:42 +00001364 // First query the target-specific parser. It will return 'true' if it
1365 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001366 if (!getTargetParser().ParseDirective(ID))
1367 return false;
1368
Alp Tokercb402912014-01-24 17:20:08 +00001369 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001370 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001371 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1372 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001373 if (Handler.first)
1374 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1375
1376 // Finally, if no one else is interested in this directive, it must be
1377 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001378 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001379 default:
1380 break;
1381 case DK_SET:
1382 case DK_EQU:
1383 return parseDirectiveSet(IDVal, true);
1384 case DK_EQUIV:
1385 return parseDirectiveSet(IDVal, false);
1386 case DK_ASCII:
1387 return parseDirectiveAscii(IDVal, false);
1388 case DK_ASCIZ:
1389 case DK_STRING:
1390 return parseDirectiveAscii(IDVal, true);
1391 case DK_BYTE:
1392 return parseDirectiveValue(1);
1393 case DK_SHORT:
1394 case DK_VALUE:
1395 case DK_2BYTE:
1396 return parseDirectiveValue(2);
1397 case DK_LONG:
1398 case DK_INT:
1399 case DK_4BYTE:
1400 return parseDirectiveValue(4);
1401 case DK_QUAD:
1402 case DK_8BYTE:
1403 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001404 case DK_OCTA:
1405 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001406 case DK_SINGLE:
1407 case DK_FLOAT:
1408 return parseDirectiveRealValue(APFloat::IEEEsingle);
1409 case DK_DOUBLE:
1410 return parseDirectiveRealValue(APFloat::IEEEdouble);
1411 case DK_ALIGN: {
1412 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1413 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1414 }
1415 case DK_ALIGN32: {
1416 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1417 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1418 }
1419 case DK_BALIGN:
1420 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1421 case DK_BALIGNW:
1422 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1423 case DK_BALIGNL:
1424 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1425 case DK_P2ALIGN:
1426 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1427 case DK_P2ALIGNW:
1428 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1429 case DK_P2ALIGNL:
1430 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1431 case DK_ORG:
1432 return parseDirectiveOrg();
1433 case DK_FILL:
1434 return parseDirectiveFill();
1435 case DK_ZERO:
1436 return parseDirectiveZero();
1437 case DK_EXTERN:
1438 eatToEndOfStatement(); // .extern is the default, ignore it.
1439 return false;
1440 case DK_GLOBL:
1441 case DK_GLOBAL:
1442 return parseDirectiveSymbolAttribute(MCSA_Global);
1443 case DK_LAZY_REFERENCE:
1444 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1445 case DK_NO_DEAD_STRIP:
1446 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1447 case DK_SYMBOL_RESOLVER:
1448 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1449 case DK_PRIVATE_EXTERN:
1450 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1451 case DK_REFERENCE:
1452 return parseDirectiveSymbolAttribute(MCSA_Reference);
1453 case DK_WEAK_DEFINITION:
1454 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1455 case DK_WEAK_REFERENCE:
1456 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1457 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1458 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1459 case DK_COMM:
1460 case DK_COMMON:
1461 return parseDirectiveComm(/*IsLocal=*/false);
1462 case DK_LCOMM:
1463 return parseDirectiveComm(/*IsLocal=*/true);
1464 case DK_ABORT:
1465 return parseDirectiveAbort();
1466 case DK_INCLUDE:
1467 return parseDirectiveInclude();
1468 case DK_INCBIN:
1469 return parseDirectiveIncbin();
1470 case DK_CODE16:
1471 case DK_CODE16GCC:
1472 return TokError(Twine(IDVal) + " not supported yet");
1473 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001474 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001475 case DK_IRP:
1476 return parseDirectiveIrp(IDLoc);
1477 case DK_IRPC:
1478 return parseDirectiveIrpc(IDLoc);
1479 case DK_ENDR:
1480 return parseDirectiveEndr(IDLoc);
1481 case DK_BUNDLE_ALIGN_MODE:
1482 return parseDirectiveBundleAlignMode();
1483 case DK_BUNDLE_LOCK:
1484 return parseDirectiveBundleLock();
1485 case DK_BUNDLE_UNLOCK:
1486 return parseDirectiveBundleUnlock();
1487 case DK_SLEB128:
1488 return parseDirectiveLEB128(true);
1489 case DK_ULEB128:
1490 return parseDirectiveLEB128(false);
1491 case DK_SPACE:
1492 case DK_SKIP:
1493 return parseDirectiveSpace(IDVal);
1494 case DK_FILE:
1495 return parseDirectiveFile(IDLoc);
1496 case DK_LINE:
1497 return parseDirectiveLine();
1498 case DK_LOC:
1499 return parseDirectiveLoc();
1500 case DK_STABS:
1501 return parseDirectiveStabs();
1502 case DK_CFI_SECTIONS:
1503 return parseDirectiveCFISections();
1504 case DK_CFI_STARTPROC:
1505 return parseDirectiveCFIStartProc();
1506 case DK_CFI_ENDPROC:
1507 return parseDirectiveCFIEndProc();
1508 case DK_CFI_DEF_CFA:
1509 return parseDirectiveCFIDefCfa(IDLoc);
1510 case DK_CFI_DEF_CFA_OFFSET:
1511 return parseDirectiveCFIDefCfaOffset();
1512 case DK_CFI_ADJUST_CFA_OFFSET:
1513 return parseDirectiveCFIAdjustCfaOffset();
1514 case DK_CFI_DEF_CFA_REGISTER:
1515 return parseDirectiveCFIDefCfaRegister(IDLoc);
1516 case DK_CFI_OFFSET:
1517 return parseDirectiveCFIOffset(IDLoc);
1518 case DK_CFI_REL_OFFSET:
1519 return parseDirectiveCFIRelOffset(IDLoc);
1520 case DK_CFI_PERSONALITY:
1521 return parseDirectiveCFIPersonalityOrLsda(true);
1522 case DK_CFI_LSDA:
1523 return parseDirectiveCFIPersonalityOrLsda(false);
1524 case DK_CFI_REMEMBER_STATE:
1525 return parseDirectiveCFIRememberState();
1526 case DK_CFI_RESTORE_STATE:
1527 return parseDirectiveCFIRestoreState();
1528 case DK_CFI_SAME_VALUE:
1529 return parseDirectiveCFISameValue(IDLoc);
1530 case DK_CFI_RESTORE:
1531 return parseDirectiveCFIRestore(IDLoc);
1532 case DK_CFI_ESCAPE:
1533 return parseDirectiveCFIEscape();
1534 case DK_CFI_SIGNAL_FRAME:
1535 return parseDirectiveCFISignalFrame();
1536 case DK_CFI_UNDEFINED:
1537 return parseDirectiveCFIUndefined(IDLoc);
1538 case DK_CFI_REGISTER:
1539 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001540 case DK_CFI_WINDOW_SAVE:
1541 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001542 case DK_MACROS_ON:
1543 case DK_MACROS_OFF:
1544 return parseDirectiveMacrosOnOff(IDVal);
1545 case DK_MACRO:
1546 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001547 case DK_EXITM:
1548 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001549 case DK_ENDM:
1550 case DK_ENDMACRO:
1551 return parseDirectiveEndMacro(IDVal);
1552 case DK_PURGEM:
1553 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001554 case DK_END:
1555 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001556 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001557 return parseDirectiveError(IDLoc, false);
1558 case DK_ERROR:
1559 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001560 case DK_WARNING:
1561 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001562 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001563
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001564 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001565 }
Chris Lattner36e02122009-06-21 20:54:55 +00001566
Chad Rosierc7f552c2013-02-12 21:33:51 +00001567 // __asm _emit or __asm __emit
1568 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1569 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001571
1572 // __asm align
1573 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001575
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001576 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001577
Chris Lattner7cbfa442010-05-19 23:34:33 +00001578 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001579 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001580 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001582 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001583 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001584
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001585 // Dump the parsed representation, if requested.
1586 if (getShowParsedOperands()) {
1587 SmallString<256> Str;
1588 raw_svector_ostream OS(Str);
1589 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001590 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001591 if (i != 0)
1592 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001593 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001594 }
1595 OS << "]";
1596
Jim Grosbach4b905842013-09-20 23:08:21 +00001597 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001598 }
1599
Oliver Stannard8b273082014-06-19 15:52:37 +00001600 // If we are generating dwarf for the current section then generate a .loc
1601 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001602 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001603 getContext().getGenDwarfSectionSyms().count(
1604 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001605
Eli Bendersky88024712013-01-16 19:32:36 +00001606 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001607
Eli Bendersky88024712013-01-16 19:32:36 +00001608 // If we previously parsed a cpp hash file line comment then make sure the
1609 // current Dwarf File is for the CppHashFilename if not then emit the
1610 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001611 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001612 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1613 0, StringRef(), CppHashFilename);
1614 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001615
Jim Grosbach4b905842013-09-20 23:08:21 +00001616 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1617 // cache with the different Loc from the call above we save the last
1618 // info we queried here with SrcMgr.FindLineNumber().
1619 unsigned CppHashLocLineNo;
1620 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1621 CppHashLocLineNo = LastQueryLine;
1622 else {
1623 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1624 LastQueryLine = CppHashLocLineNo;
1625 LastQueryIDLoc = CppHashLoc;
1626 LastQueryBuffer = CppHashBuf;
1627 }
1628 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001629 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001630
Jim Grosbach4b905842013-09-20 23:08:21 +00001631 getStreamer().EmitDwarfLocDirective(
1632 getContext().getGenDwarfFileNumber(), Line, 0,
1633 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1634 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001635 }
1636
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001637 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001638 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001639 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001640 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1641 Info.ParsedOperands, Out,
1642 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001643 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001644
Chris Lattnera2a9d162010-09-11 16:18:25 +00001645 // Don't skip the rest of the line, the instruction parser is responsible for
1646 // that.
1647 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001648}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001649
Jim Grosbach4b905842013-09-20 23:08:21 +00001650/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001651/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001652void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001653 if (!Lexer.is(AsmToken::EndOfStatement))
1654 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001655 // Eat EOL.
1656 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001657}
1658
Jim Grosbach4b905842013-09-20 23:08:21 +00001659/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001660/// ::= # number "filename"
1661/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001662bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001663 Lex(); // Eat the hash token.
1664
1665 if (getLexer().isNot(AsmToken::Integer)) {
1666 // Consume the line since in cases it is not a well-formed line directive,
1667 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001668 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001669 return false;
1670 }
1671
1672 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001673 Lex();
1674
1675 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001677 return false;
1678 }
1679
1680 StringRef Filename = getTok().getString();
1681 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001682 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001683
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001684 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1685 CppHashLoc = L;
1686 CppHashFilename = Filename;
1687 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001688 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001689
1690 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001691 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001692 return false;
1693}
1694
Jim Grosbach4b905842013-09-20 23:08:21 +00001695/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696/// for the Filename and LineNo if any in the diagnostic.
1697void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001698 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699 raw_ostream &OS = errs();
1700
1701 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1702 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001703 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1704 unsigned CppHashBuf =
1705 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001709 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1710 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1711 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001712 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1713 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714 }
1715
Eric Christophera7c32732012-12-18 00:30:54 +00001716 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001717 // manager changed or buffer changed (like in a nested include) then just
1718 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001719 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001721 if (Parser->SavedDiagHandler)
1722 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1723 else
Craig Topper353eda42014-04-24 06:44:33 +00001724 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001725 return;
1726 }
1727
Eric Christophera7c32732012-12-18 00:30:54 +00001728 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001729 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1730 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001731 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732
1733 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1734 int CppHashLocLineNo =
1735 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001736 int LineNo =
1737 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001738
Jim Grosbach4b905842013-09-20 23:08:21 +00001739 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1740 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001741 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001742
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001743 if (Parser->SavedDiagHandler)
1744 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1745 else
Craig Topper353eda42014-04-24 06:44:33 +00001746 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001747}
1748
Rafael Espindola2c064482012-08-21 18:29:30 +00001749// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1750// difference being that that function accepts '@' as part of identifiers and
1751// we can't do that. AsmLexer.cpp should probably be changed to handle
1752// '@' as a special case when needed.
1753static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001754 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1755 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001756}
1757
Rafael Espindola34b9c512012-06-03 23:57:14 +00001758bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001759 ArrayRef<MCAsmMacroParameter> Parameters,
1760 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001761 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001762 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001763 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001765
Preston Gurd05500642012-09-19 20:36:12 +00001766 // A macro without parameters is handled differently on Darwin:
1767 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768 while (!Body.empty()) {
1769 // Scan for the next substitution.
1770 std::size_t End = Body.size(), Pos = 0;
1771 for (; Pos != End; ++Pos) {
1772 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001773 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 // This macro has no parameters, look for $0, $1, etc.
1775 if (Body[Pos] != '$' || Pos + 1 == End)
1776 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001777
Rafael Espindola1134ab232011-06-05 02:43:45 +00001778 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001779 if (Next == '$' || Next == 'n' ||
1780 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 break;
1782 } else {
1783 // This macro has parameters, look for \foo, \bar, etc.
1784 if (Body[Pos] == '\\' && Pos + 1 != End)
1785 break;
1786 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001787 }
1788
1789 // Add the prefix.
1790 OS << Body.slice(0, Pos);
1791
1792 // Check if we reached the end.
1793 if (Pos == End)
1794 break;
1795
Benjamin Kramer513e7442014-02-20 13:36:32 +00001796 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001797 switch (Body[Pos + 1]) {
1798 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001799 case '$':
1800 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001801 break;
1802
Jim Grosbach4b905842013-09-20 23:08:21 +00001803 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001804 case 'n':
1805 OS << A.size();
1806 break;
1807
Jim Grosbach4b905842013-09-20 23:08:21 +00001808 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 default: {
1810 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 if (Index >= A.size())
1813 break;
1814
1815 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001816 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001817 ie = A[Index].end();
1818 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001819 OS << it->getString();
1820 break;
1821 }
1822 }
1823 Pos += 2;
1824 } else {
1825 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001826 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001827 ++I;
1828
Jim Grosbach4b905842013-09-20 23:08:21 +00001829 const char *Begin = Body.data() + Pos + 1;
1830 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 unsigned Index = 0;
1832 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001833 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 break;
1835
Preston Gurd05500642012-09-19 20:36:12 +00001836 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001837 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1838 Pos += 3;
1839 else {
1840 OS << '\\' << Argument;
1841 Pos = I;
1842 }
Preston Gurd05500642012-09-19 20:36:12 +00001843 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001844 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001845 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001846 ie = A[Index].end();
1847 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001848 // We expect no quotes around the string's contents when
1849 // parsing for varargs.
1850 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001851 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001852 else
1853 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001854
Preston Gurd05500642012-09-19 20:36:12 +00001855 Pos += 1 + Argument.size();
1856 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001858 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001859 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001860 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001861
Rafael Espindola1134ab232011-06-05 02:43:45 +00001862 return false;
1863}
Daniel Dunbar43235712010-07-18 18:54:11 +00001864
Nico Weber2a8f9222014-07-24 16:29:04 +00001865MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001866 StringRef I, size_t CondStackDepth)
1867 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001868 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001869
Jim Grosbach4b905842013-09-20 23:08:21 +00001870static bool isOperator(AsmToken::TokenKind kind) {
1871 switch (kind) {
1872 default:
1873 return false;
1874 case AsmToken::Plus:
1875 case AsmToken::Minus:
1876 case AsmToken::Tilde:
1877 case AsmToken::Slash:
1878 case AsmToken::Star:
1879 case AsmToken::Dot:
1880 case AsmToken::Equal:
1881 case AsmToken::EqualEqual:
1882 case AsmToken::Pipe:
1883 case AsmToken::PipePipe:
1884 case AsmToken::Caret:
1885 case AsmToken::Amp:
1886 case AsmToken::AmpAmp:
1887 case AsmToken::Exclaim:
1888 case AsmToken::ExclaimEqual:
1889 case AsmToken::Percent:
1890 case AsmToken::Less:
1891 case AsmToken::LessEqual:
1892 case AsmToken::LessLess:
1893 case AsmToken::LessGreater:
1894 case AsmToken::Greater:
1895 case AsmToken::GreaterEqual:
1896 case AsmToken::GreaterGreater:
1897 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001898 }
1899}
1900
David Majnemer16252452014-01-29 00:07:39 +00001901namespace {
1902class AsmLexerSkipSpaceRAII {
1903public:
1904 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1905 Lexer.setSkipSpace(SkipSpace);
1906 }
1907
1908 ~AsmLexerSkipSpaceRAII() {
1909 Lexer.setSkipSpace(true);
1910 }
1911
1912private:
1913 AsmLexer &Lexer;
1914};
1915}
1916
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001917bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1918
1919 if (Vararg) {
1920 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1921 StringRef Str = parseStringToEndOfStatement();
1922 MA.push_back(AsmToken(AsmToken::String, Str));
1923 }
1924 return false;
1925 }
1926
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001928 unsigned AddTokens = 0;
1929
David Majnemer16252452014-01-29 00:07:39 +00001930 // Darwin doesn't use spaces to delmit arguments.
1931 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932
1933 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001934 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001935 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001936
David Majnemer91fc4c22014-01-29 18:57:46 +00001937 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001938 break;
Preston Gurd05500642012-09-19 20:36:12 +00001939
1940 if (Lexer.is(AsmToken::Space)) {
1941 Lex(); // Eat spaces
1942
1943 // Spaces can delimit parameters, but could also be part an expression.
1944 // If the token after a space is an operator, add the token and the next
1945 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001946 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001947 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001948 // Check to see whether the token is used as an operator,
1949 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001950 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001951 if (*NextChar == ' ')
1952 AddTokens = 2;
1953 }
1954
1955 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001956 break;
1957 }
1958 }
1959 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960
Jim Grosbach4b905842013-09-20 23:08:21 +00001961 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001962 // to be able to fill in the remaining default parameter values
1963 if (Lexer.is(AsmToken::EndOfStatement))
1964 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965
1966 // Adjust the current parentheses level.
1967 if (Lexer.is(AsmToken::LParen))
1968 ++ParenLevel;
1969 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1970 --ParenLevel;
1971
1972 // Append the token to the current argument list.
1973 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001974 if (AddTokens)
1975 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001976 Lex();
1977 }
Preston Gurd05500642012-09-19 20:36:12 +00001978
Rafael Espindola768b41c2012-06-15 14:02:34 +00001979 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001980 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001981 return false;
1982}
1983
1984// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001985bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001986 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001987 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001988 bool NamedParametersFound = false;
1989 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001991 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001992 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001993
Rafael Espindola768b41c2012-06-15 14:02:34 +00001994 // Parse two kinds of macro invocations:
1995 // - macros defined without any parameters accept an arbitrary number of them
1996 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001997 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001998 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1999 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002000 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002001 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002002
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002003 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002004 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002005 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002006 eatToEndOfStatement();
2007 return true;
2008 }
2009
2010 if (!Lexer.is(AsmToken::Equal)) {
2011 TokError("expected '=' after formal parameter identifier");
2012 eatToEndOfStatement();
2013 return true;
2014 }
2015 Lex();
2016
2017 NamedParametersFound = true;
2018 }
2019
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002020 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002021 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002022 eatToEndOfStatement();
2023 return true;
2024 }
2025
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002026 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2027 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002028 return true;
2029
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002031 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002032 unsigned FAI = 0;
2033 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002034 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002035 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002036
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002037 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002038 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002039 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002040 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002041 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002042 return true;
2043 }
2044 PI = FAI;
2045 }
2046
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002047 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002048 if (A.size() <= PI)
2049 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002050 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002051
2052 if (FALocs.size() <= PI)
2053 FALocs.resize(PI + 1);
2054
2055 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002056 }
Jim Grosbach206661622012-07-30 22:44:17 +00002057
Preston Gurd242ed3152012-09-19 20:29:04 +00002058 // At the end of the statement, fill in remaining arguments that have
2059 // default values. If there aren't any, then the next argument is
2060 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002061 if (Lexer.is(AsmToken::EndOfStatement)) {
2062 bool Failure = false;
2063 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2064 if (A[FAI].empty()) {
2065 if (M->Parameters[FAI].Required) {
2066 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2067 "missing value for required parameter "
2068 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2069 Failure = true;
2070 }
2071
2072 if (!M->Parameters[FAI].Value.empty())
2073 A[FAI] = M->Parameters[FAI].Value;
2074 }
2075 }
2076 return Failure;
2077 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002078
2079 if (Lexer.is(AsmToken::Comma))
2080 Lex();
2081 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002082
2083 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002084}
2085
Jim Grosbach4b905842013-09-20 23:08:21 +00002086const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2087 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002088 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002089}
2090
Jim Grosbach4b905842013-09-20 23:08:21 +00002091void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002092 MacroMap[Name] = new MCAsmMacro(Macro);
2093}
2094
Jim Grosbach4b905842013-09-20 23:08:21 +00002095void AsmParser::undefineMacro(StringRef Name) {
2096 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002097 if (I != MacroMap.end()) {
2098 delete I->getValue();
2099 MacroMap.erase(I);
2100 }
2101}
2102
Jim Grosbach4b905842013-09-20 23:08:21 +00002103bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002104 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2105 // this, although we should protect against infinite loops.
2106 if (ActiveMacros.size() == 20)
2107 return TokError("macros cannot be nested more than 20 levels deep");
2108
Eli Bendersky38274122013-01-14 23:22:36 +00002109 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002110 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002111 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002112
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2114 // to hold the macro body with substitutions.
2115 SmallString<256> Buf;
2116 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002117 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002118
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002119 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002120 return true;
2121
Eli Bendersky38274122013-01-14 23:22:36 +00002122 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002123 // instantiation.
2124 OS << ".endmacro\n";
2125
David Blaikie1961f142014-08-21 20:44:56 +00002126 std::unique_ptr<MemoryBuffer> Instantiation(
2127 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002128
Daniel Dunbar43235712010-07-18 18:54:11 +00002129 // Create the macro instantiation object and add to the current macro
2130 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00002131 MacroInstantiation *MI =
2132 new MacroInstantiation(NameLoc, CurBuffer, getTok().getLoc(),
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002133 Instantiation->getBuffer(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002134 ActiveMacros.push_back(MI);
2135
2136 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002137 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002138 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 Lex();
2140
2141 return false;
2142}
2143
Jim Grosbach4b905842013-09-20 23:08:21 +00002144void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002145 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002146 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002147 Lex();
2148
2149 // Pop the instantiation entry.
2150 delete ActiveMacros.back();
2151 ActiveMacros.pop_back();
2152}
2153
Jim Grosbach4b905842013-09-20 23:08:21 +00002154static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002156 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002157 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2158 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002159 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002160 case MCExpr::Target:
2161 case MCExpr::Constant:
2162 return false;
2163 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002164 const MCSymbol &S =
2165 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002166 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002168 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002169 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002170 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002171 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002172 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002173
2174 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002175}
2176
Jim Grosbach4b905842013-09-20 23:08:21 +00002177bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002178 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002179 // FIXME: Use better location, we should use proper tokens.
2180 SMLoc EqualLoc = Lexer.getLoc();
2181
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002182 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002183 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002184 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002185
Rafael Espindola72f5f172012-01-28 05:57:00 +00002186 // Note: we don't count b as used in "a = b". This is to allow
2187 // a = b
2188 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002189
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002190 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002191 return TokError("unexpected token in assignment");
2192
2193 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002194 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002195
Daniel Dunbar5f339242009-10-16 01:57:39 +00002196 // Validate that the LHS is allowed to be a variable (either it has not been
2197 // used as a symbol, or it is an absolute symbol).
2198 MCSymbol *Sym = getContext().LookupSymbol(Name);
2199 if (Sym) {
2200 // Diagnose assignment to a label.
2201 //
2202 // FIXME: Diagnostics. Note the location of the definition as a label.
2203 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002204 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002205 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2206 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002207 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002208 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2209 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002210 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002211 return Error(EqualLoc, "redefinition of '" + Name + "'");
2212 else if (!Sym->isVariable())
2213 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002214 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002215 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002216 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002217
2218 // Don't count these checks as uses.
2219 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002220 } else if (Name == ".") {
2221 if (Out.EmitValueToOffset(Value, 0)) {
2222 Error(EqualLoc, "expected absolute expression");
2223 eatToEndOfStatement();
2224 }
2225 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002226 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002227 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002228
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002229 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002230 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002231 if (NoDeadStrip)
2232 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2233
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002234 return false;
2235}
2236
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002237/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002238/// ::= identifier
2239/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002240bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002241 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002242 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2243 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 // handle this as a context dependent token, instead we detect adjacent tokens
2245 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002246 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2247 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002248
Hans Wennborgce69d772013-10-18 20:46:28 +00002249 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250 Lex();
2251 if (Lexer.isNot(AsmToken::Identifier))
2252 return true;
2253
Hans Wennborgce69d772013-10-18 20:46:28 +00002254 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2255 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002256 return true;
2257
2258 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002260 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002261 Lex();
2262 return false;
2263 }
2264
Jim Grosbach4b905842013-09-20 23:08:21 +00002265 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002266 return true;
2267
Sean Callanan936b0d32010-01-19 21:44:56 +00002268 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002269
Sean Callanan686ed8d2010-01-19 20:22:31 +00002270 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002271
2272 return false;
2273}
2274
Jim Grosbach4b905842013-09-20 23:08:21 +00002275/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002276/// ::= .equ identifier ',' expression
2277/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002278/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002279bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002280 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002281
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002282 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002283 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002284
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002285 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002286 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002287 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002288
Jim Grosbach4b905842013-09-20 23:08:21 +00002289 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002290}
2291
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002292bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002293 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002294
2295 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002296 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002297 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2298 if (Str[i] != '\\') {
2299 Data += Str[i];
2300 continue;
2301 }
2302
2303 // Recognize escaped characters. Note that this escape semantics currently
2304 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2305 ++i;
2306 if (i == e)
2307 return TokError("unexpected backslash at end of string");
2308
2309 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002311 // Consume up to three octal characters.
2312 unsigned Value = Str[i] - '0';
2313
Jim Grosbach4b905842013-09-20 23:08:21 +00002314 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002315 ++i;
2316 Value = Value * 8 + (Str[i] - '0');
2317
Jim Grosbach4b905842013-09-20 23:08:21 +00002318 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002319 ++i;
2320 Value = Value * 8 + (Str[i] - '0');
2321 }
2322 }
2323
2324 if (Value > 255)
2325 return TokError("invalid octal escape sequence (out of range)");
2326
Jim Grosbach4b905842013-09-20 23:08:21 +00002327 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002328 continue;
2329 }
2330
2331 // Otherwise recognize individual escapes.
2332 switch (Str[i]) {
2333 default:
2334 // Just reject invalid escape sequences for now.
2335 return TokError("invalid escape sequence (unrecognized character)");
2336
2337 case 'b': Data += '\b'; break;
2338 case 'f': Data += '\f'; break;
2339 case 'n': Data += '\n'; break;
2340 case 'r': Data += '\r'; break;
2341 case 't': Data += '\t'; break;
2342 case '"': Data += '"'; break;
2343 case '\\': Data += '\\'; break;
2344 }
2345 }
2346
2347 return false;
2348}
2349
Jim Grosbach4b905842013-09-20 23:08:21 +00002350/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002351/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002352bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002354 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002355
Daniel Dunbara10e5192009-06-24 23:30:00 +00002356 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002358 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359
Daniel Dunbaref668c12009-08-14 18:19:52 +00002360 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002361 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002362 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
Rafael Espindola64e1af82013-07-02 15:49:13 +00002364 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002366 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367
Sean Callanan686ed8d2010-01-19 20:22:31 +00002368 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002369
2370 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002371 break;
2372
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002373 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002374 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002376 }
2377 }
2378
Sean Callanan686ed8d2010-01-19 20:22:31 +00002379 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002380 return false;
2381}
2382
Jim Grosbach4b905842013-09-20 23:08:21 +00002383/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002384/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002385bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002386 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002387 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002388
Daniel Dunbara10e5192009-06-24 23:30:00 +00002389 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002390 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002391 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002392 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002393 return true;
2394
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002395 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002396 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2397 assert(Size <= 8 && "Invalid size");
2398 uint64_t IntValue = MCE->getValue();
2399 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2400 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002401 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002402 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002403 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002405 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002407
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002409 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002411 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002412 }
2413 }
2414
Sean Callanan686ed8d2010-01-19 20:22:31 +00002415 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002416 return false;
2417}
2418
David Woodhoused6de0d92014-02-01 16:20:59 +00002419/// ParseDirectiveOctaValue
2420/// ::= .octa [ hexconstant (, hexconstant)* ]
2421bool AsmParser::parseDirectiveOctaValue() {
2422 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2423 checkForValidSection();
2424
2425 for (;;) {
2426 if (Lexer.getKind() == AsmToken::Error)
2427 return true;
2428 if (Lexer.getKind() != AsmToken::Integer &&
2429 Lexer.getKind() != AsmToken::BigNum)
2430 return TokError("unknown token in expression");
2431
2432 SMLoc ExprLoc = getLexer().getLoc();
2433 APInt IntValue = getTok().getAPIntVal();
2434 Lex();
2435
2436 uint64_t hi, lo;
2437 if (IntValue.isIntN(64)) {
2438 hi = 0;
2439 lo = IntValue.getZExtValue();
2440 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002441 // It might actually have more than 128 bits, but the top ones are zero.
2442 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002443 lo = IntValue.getLoBits(64).getZExtValue();
2444 } else
2445 return Error(ExprLoc, "literal value out of range for directive");
2446
2447 if (MAI.isLittleEndian()) {
2448 getStreamer().EmitIntValue(lo, 8);
2449 getStreamer().EmitIntValue(hi, 8);
2450 } else {
2451 getStreamer().EmitIntValue(hi, 8);
2452 getStreamer().EmitIntValue(lo, 8);
2453 }
2454
2455 if (getLexer().is(AsmToken::EndOfStatement))
2456 break;
2457
2458 // FIXME: Improve diagnostic.
2459 if (getLexer().isNot(AsmToken::Comma))
2460 return TokError("unexpected token in directive");
2461 Lex();
2462 }
2463 }
2464
2465 Lex();
2466 return false;
2467}
2468
Jim Grosbach4b905842013-09-20 23:08:21 +00002469/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002471bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002472 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002473 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002474
2475 for (;;) {
2476 // We don't truly support arithmetic on floating point expressions, so we
2477 // have to manually parse unary prefixes.
2478 bool IsNeg = false;
2479 if (getLexer().is(AsmToken::Minus)) {
2480 Lex();
2481 IsNeg = true;
2482 } else if (getLexer().is(AsmToken::Plus))
2483 Lex();
2484
Michael J. Spencer530ce852010-10-09 11:00:50 +00002485 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002486 getLexer().isNot(AsmToken::Real) &&
2487 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002488 return TokError("unexpected token in directive");
2489
2490 // Convert to an APFloat.
2491 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002492 StringRef IDVal = getTok().getString();
2493 if (getLexer().is(AsmToken::Identifier)) {
2494 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2495 Value = APFloat::getInf(Semantics);
2496 else if (!IDVal.compare_lower("nan"))
2497 Value = APFloat::getNaN(Semantics, false, ~0);
2498 else
2499 return TokError("invalid floating point literal");
2500 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002501 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002502 return TokError("invalid floating point literal");
2503 if (IsNeg)
2504 Value.changeSign();
2505
2506 // Consume the numeric token.
2507 Lex();
2508
2509 // Emit the value as an integer.
2510 APInt AsInt = Value.bitcastToAPInt();
2511 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002512 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002513
2514 if (getLexer().is(AsmToken::EndOfStatement))
2515 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002516
Daniel Dunbar2af16532010-09-24 01:59:56 +00002517 if (getLexer().isNot(AsmToken::Comma))
2518 return TokError("unexpected token in directive");
2519 Lex();
2520 }
2521 }
2522
2523 Lex();
2524 return false;
2525}
2526
Jim Grosbach4b905842013-09-20 23:08:21 +00002527/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002528/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002529bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002530 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002531
2532 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002534 return true;
2535
Rafael Espindolab91bac62010-10-05 19:42:57 +00002536 int64_t Val = 0;
2537 if (getLexer().is(AsmToken::Comma)) {
2538 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002539 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002540 return true;
2541 }
2542
Rafael Espindola922e3f42010-09-16 15:03:59 +00002543 if (getLexer().isNot(AsmToken::EndOfStatement))
2544 return TokError("unexpected token in '.zero' directive");
2545
2546 Lex();
2547
Rafael Espindola64e1af82013-07-02 15:49:13 +00002548 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002549
2550 return false;
2551}
2552
Jim Grosbach4b905842013-09-20 23:08:21 +00002553/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002554/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002555bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002556 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002557
David Majnemer522d3db2014-02-01 07:19:38 +00002558 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002559 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002560 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002561 return true;
2562
David Majnemer522d3db2014-02-01 07:19:38 +00002563 if (NumValues < 0) {
2564 Warning(RepeatLoc,
2565 "'.fill' directive with negative repeat count has no effect");
2566 NumValues = 0;
2567 }
2568
Roman Divackye33098f2013-09-24 17:44:41 +00002569 int64_t FillSize = 1;
2570 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002571
David Majnemer522d3db2014-02-01 07:19:38 +00002572 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2574 if (getLexer().isNot(AsmToken::Comma))
2575 return TokError("unexpected token in '.fill' directive");
2576 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002577
David Majnemer522d3db2014-02-01 07:19:38 +00002578 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002579 if (parseAbsoluteExpression(FillSize))
2580 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002581
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2583 if (getLexer().isNot(AsmToken::Comma))
2584 return TokError("unexpected token in '.fill' directive");
2585 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002586
David Majnemer522d3db2014-02-01 07:19:38 +00002587 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002588 if (parseAbsoluteExpression(FillExpr))
2589 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002590
Roman Divackye33098f2013-09-24 17:44:41 +00002591 if (getLexer().isNot(AsmToken::EndOfStatement))
2592 return TokError("unexpected token in '.fill' directive");
2593
2594 Lex();
2595 }
2596 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002597
David Majnemer522d3db2014-02-01 07:19:38 +00002598 if (FillSize < 0) {
2599 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2600 NumValues = 0;
2601 }
2602 if (FillSize > 8) {
2603 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2604 FillSize = 8;
2605 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002606
David Majnemer522d3db2014-02-01 07:19:38 +00002607 if (!isUInt<32>(FillExpr) && FillSize > 4)
2608 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2609
2610 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2611 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2612
2613 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2614 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
Alexey Samsonove5864c62014-08-20 22:46:38 +00002615 if (NonZeroFillSize < FillSize)
2616 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
David Majnemer522d3db2014-02-01 07:19:38 +00002617 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002618
2619 return false;
2620}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002621
Jim Grosbach4b905842013-09-20 23:08:21 +00002622/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002623/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002624bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002625 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002626
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002627 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002628 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002629 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002630 return true;
2631
2632 // Parse optional fill expression.
2633 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002634 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2635 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002636 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002637 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002638
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002639 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002640 return true;
2641
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002642 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002643 return TokError("unexpected token in '.org' directive");
2644 }
2645
Sean Callanan686ed8d2010-01-19 20:22:31 +00002646 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002647
Jim Grosbachb5912772012-01-27 00:37:08 +00002648 // Only limited forms of relocatable expressions are accepted here, it
2649 // has to be relative to the current section. The streamer will return
2650 // 'true' if the expression wasn't evaluatable.
2651 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2652 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002653
2654 return false;
2655}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002656
Jim Grosbach4b905842013-09-20 23:08:21 +00002657/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002659bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002660 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002661
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002662 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002663 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002664 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002665 return true;
2666
2667 SMLoc MaxBytesLoc;
2668 bool HasFillExpr = false;
2669 int64_t FillExpr = 0;
2670 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002671 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2672 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002673 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002674 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675
2676 // The fill expression can be omitted while specifying a maximum number of
2677 // alignment bytes, e.g:
2678 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002679 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002680 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002681 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 return true;
2683 }
2684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002685 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2686 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002687 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002688 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002690 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002691 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002692 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002693
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002694 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002695 return TokError("unexpected token in directive");
2696 }
2697 }
2698
Sean Callanan686ed8d2010-01-19 20:22:31 +00002699 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002700
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002701 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002702 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002703
2704 // Compute alignment in bytes.
2705 if (IsPow2) {
2706 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002707 if (Alignment >= 32) {
2708 Error(AlignmentLoc, "invalid alignment value");
2709 Alignment = 31;
2710 }
2711
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002712 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002713 } else {
2714 // Reject alignments that aren't a power of two, for gas compatibility.
2715 if (!isPowerOf2_64(Alignment))
2716 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002717 }
2718
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002719 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002720 if (MaxBytesLoc.isValid()) {
2721 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002722 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002723 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002724 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002725 }
2726
2727 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002728 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002729 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002730 MaxBytesToFill = 0;
2731 }
2732 }
2733
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002734 // Check whether we should use optimal code alignment for this .align
2735 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002736 const MCSection *Section = getStreamer().getCurrentSection().first;
2737 assert(Section && "must have section to emit alignment");
2738 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002739 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2740 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002741 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002742 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002743 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002744 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2745 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002746 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002747
2748 return false;
2749}
2750
Jim Grosbach4b905842013-09-20 23:08:21 +00002751/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002752/// ::= .file [number] filename
2753/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002754bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002755 // FIXME: I'm not sure what this is.
2756 int64_t FileNumber = -1;
2757 SMLoc FileNumberLoc = getLexer().getLoc();
2758 if (getLexer().is(AsmToken::Integer)) {
2759 FileNumber = getTok().getIntVal();
2760 Lex();
2761
2762 if (FileNumber < 1)
2763 return TokError("file number less than one");
2764 }
2765
2766 if (getLexer().isNot(AsmToken::String))
2767 return TokError("unexpected token in '.file' directive");
2768
2769 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002770 // Allow the strings to have escaped octal character sequence.
2771 std::string Path = getTok().getString();
2772 if (parseEscapedString(Path))
2773 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002774 Lex();
2775
2776 StringRef Directory;
2777 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002778 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002779 if (getLexer().is(AsmToken::String)) {
2780 if (FileNumber == -1)
2781 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002782 if (parseEscapedString(FilenameData))
2783 return true;
2784 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002785 Directory = Path;
2786 Lex();
2787 } else {
2788 Filename = Path;
2789 }
2790
2791 if (getLexer().isNot(AsmToken::EndOfStatement))
2792 return TokError("unexpected token in '.file' directive");
2793
2794 if (FileNumber == -1)
2795 getStreamer().EmitFileDirective(Filename);
2796 else {
2797 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002798 Error(DirectiveLoc,
2799 "input can't have .file dwarf directives when -g is "
2800 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002801
David Blaikiec714ef42014-03-17 01:52:11 +00002802 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2803 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002804 Error(FileNumberLoc, "file number already allocated");
2805 }
2806
2807 return false;
2808}
2809
Jim Grosbach4b905842013-09-20 23:08:21 +00002810/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002811/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002812bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002813 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2814 if (getLexer().isNot(AsmToken::Integer))
2815 return TokError("unexpected token in '.line' directive");
2816
2817 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002818 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002819 Lex();
2820
2821 // FIXME: Do something with the .line.
2822 }
2823
2824 if (getLexer().isNot(AsmToken::EndOfStatement))
2825 return TokError("unexpected token in '.line' directive");
2826
2827 return false;
2828}
2829
Jim Grosbach4b905842013-09-20 23:08:21 +00002830/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002831/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2832/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2833/// The first number is a file number, must have been previously assigned with
2834/// a .file directive, the second number is the line number and optionally the
2835/// third number is a column position (zero if not specified). The remaining
2836/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002837bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002838 if (getLexer().isNot(AsmToken::Integer))
2839 return TokError("unexpected token in '.loc' directive");
2840 int64_t FileNumber = getTok().getIntVal();
2841 if (FileNumber < 1)
2842 return TokError("file number less than one in '.loc' directive");
2843 if (!getContext().isValidDwarfFileNumber(FileNumber))
2844 return TokError("unassigned file number in '.loc' directive");
2845 Lex();
2846
2847 int64_t LineNumber = 0;
2848 if (getLexer().is(AsmToken::Integer)) {
2849 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002850 if (LineNumber < 0)
2851 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002852 Lex();
2853 }
2854
2855 int64_t ColumnPos = 0;
2856 if (getLexer().is(AsmToken::Integer)) {
2857 ColumnPos = getTok().getIntVal();
2858 if (ColumnPos < 0)
2859 return TokError("column position less than zero in '.loc' directive");
2860 Lex();
2861 }
2862
2863 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2864 unsigned Isa = 0;
2865 int64_t Discriminator = 0;
2866 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2867 for (;;) {
2868 if (getLexer().is(AsmToken::EndOfStatement))
2869 break;
2870
2871 StringRef Name;
2872 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002873 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002874 return TokError("unexpected token in '.loc' directive");
2875
2876 if (Name == "basic_block")
2877 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2878 else if (Name == "prologue_end")
2879 Flags |= DWARF2_FLAG_PROLOGUE_END;
2880 else if (Name == "epilogue_begin")
2881 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2882 else if (Name == "is_stmt") {
2883 Loc = getTok().getLoc();
2884 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002885 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002886 return true;
2887 // The expression must be the constant 0 or 1.
2888 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2889 int Value = MCE->getValue();
2890 if (Value == 0)
2891 Flags &= ~DWARF2_FLAG_IS_STMT;
2892 else if (Value == 1)
2893 Flags |= DWARF2_FLAG_IS_STMT;
2894 else
2895 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002896 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002897 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2898 }
Craig Topperf15655b2013-04-22 04:22:40 +00002899 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002900 Loc = getTok().getLoc();
2901 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002902 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002903 return true;
2904 // The expression must be a constant greater or equal to 0.
2905 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2906 int Value = MCE->getValue();
2907 if (Value < 0)
2908 return Error(Loc, "isa number less than zero");
2909 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002910 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return Error(Loc, "isa number not a constant value");
2912 }
Craig Topperf15655b2013-04-22 04:22:40 +00002913 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002914 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002915 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002916 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002917 return Error(Loc, "unknown sub-directive in '.loc' directive");
2918 }
2919
2920 if (getLexer().is(AsmToken::EndOfStatement))
2921 break;
2922 }
2923 }
2924
2925 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2926 Isa, Discriminator, StringRef());
2927
2928 return false;
2929}
2930
Jim Grosbach4b905842013-09-20 23:08:21 +00002931/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002932/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002933bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002934 return TokError("unsupported directive '.stabs'");
2935}
2936
Jim Grosbach4b905842013-09-20 23:08:21 +00002937/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002938/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002939bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002940 StringRef Name;
2941 bool EH = false;
2942 bool Debug = false;
2943
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002944 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002945 return TokError("Expected an identifier");
2946
2947 if (Name == ".eh_frame")
2948 EH = true;
2949 else if (Name == ".debug_frame")
2950 Debug = true;
2951
2952 if (getLexer().is(AsmToken::Comma)) {
2953 Lex();
2954
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002955 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002956 return TokError("Expected an identifier");
2957
2958 if (Name == ".eh_frame")
2959 EH = true;
2960 else if (Name == ".debug_frame")
2961 Debug = true;
2962 }
2963
2964 getStreamer().EmitCFISections(EH, Debug);
2965 return false;
2966}
2967
Jim Grosbach4b905842013-09-20 23:08:21 +00002968/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002969/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002970bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002971 StringRef Simple;
2972 if (getLexer().isNot(AsmToken::EndOfStatement))
2973 if (parseIdentifier(Simple) || Simple != "simple")
2974 return TokError("unexpected token in .cfi_startproc directive");
2975
2976 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002977 return false;
2978}
2979
Jim Grosbach4b905842013-09-20 23:08:21 +00002980/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002981/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002982bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002983 getStreamer().EmitCFIEndProc();
2984 return false;
2985}
2986
Jim Grosbach4b905842013-09-20 23:08:21 +00002987/// \brief parse register name or number.
2988bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002989 SMLoc DirectiveLoc) {
2990 unsigned RegNo;
2991
2992 if (getLexer().isNot(AsmToken::Integer)) {
2993 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2994 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002995 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002996 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002997 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002998
2999 return false;
3000}
3001
Jim Grosbach4b905842013-09-20 23:08:21 +00003002/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003003/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003004bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003005 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003006 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003007 return true;
3008
3009 if (getLexer().isNot(AsmToken::Comma))
3010 return TokError("unexpected token in directive");
3011 Lex();
3012
3013 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003014 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003015 return true;
3016
3017 getStreamer().EmitCFIDefCfa(Register, Offset);
3018 return false;
3019}
3020
Jim Grosbach4b905842013-09-20 23:08:21 +00003021/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003022/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003023bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003024 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003025 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003026 return true;
3027
3028 getStreamer().EmitCFIDefCfaOffset(Offset);
3029 return false;
3030}
3031
Jim Grosbach4b905842013-09-20 23:08:21 +00003032/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003033/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003034bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003035 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003036 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003037 return true;
3038
3039 if (getLexer().isNot(AsmToken::Comma))
3040 return TokError("unexpected token in directive");
3041 Lex();
3042
3043 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003044 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003045 return true;
3046
3047 getStreamer().EmitCFIRegister(Register1, Register2);
3048 return false;
3049}
3050
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003051/// parseDirectiveCFIWindowSave
3052/// ::= .cfi_window_save
3053bool AsmParser::parseDirectiveCFIWindowSave() {
3054 getStreamer().EmitCFIWindowSave();
3055 return false;
3056}
3057
Jim Grosbach4b905842013-09-20 23:08:21 +00003058/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003059/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003060bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003061 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003062 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003063 return true;
3064
3065 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3066 return false;
3067}
3068
Jim Grosbach4b905842013-09-20 23:08:21 +00003069/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003070/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003071bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003072 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003073 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003074 return true;
3075
3076 getStreamer().EmitCFIDefCfaRegister(Register);
3077 return false;
3078}
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003081/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003082bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003083 int64_t Register = 0;
3084 int64_t Offset = 0;
3085
Jim Grosbach4b905842013-09-20 23:08:21 +00003086 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003087 return true;
3088
3089 if (getLexer().isNot(AsmToken::Comma))
3090 return TokError("unexpected token in directive");
3091 Lex();
3092
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003093 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003094 return true;
3095
3096 getStreamer().EmitCFIOffset(Register, Offset);
3097 return false;
3098}
3099
Jim Grosbach4b905842013-09-20 23:08:21 +00003100/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003101/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003102bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003103 int64_t Register = 0;
3104
Jim Grosbach4b905842013-09-20 23:08:21 +00003105 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003106 return true;
3107
3108 if (getLexer().isNot(AsmToken::Comma))
3109 return TokError("unexpected token in directive");
3110 Lex();
3111
3112 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003113 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003114 return true;
3115
3116 getStreamer().EmitCFIRelOffset(Register, Offset);
3117 return false;
3118}
3119
3120static bool isValidEncoding(int64_t Encoding) {
3121 if (Encoding & ~0xff)
3122 return false;
3123
3124 if (Encoding == dwarf::DW_EH_PE_omit)
3125 return true;
3126
3127 const unsigned Format = Encoding & 0xf;
3128 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3129 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3130 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3131 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3132 return false;
3133
3134 const unsigned Application = Encoding & 0x70;
3135 if (Application != dwarf::DW_EH_PE_absptr &&
3136 Application != dwarf::DW_EH_PE_pcrel)
3137 return false;
3138
3139 return true;
3140}
3141
Jim Grosbach4b905842013-09-20 23:08:21 +00003142/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003143/// IsPersonality true for cfi_personality, false for cfi_lsda
3144/// ::= .cfi_personality encoding, [symbol_name]
3145/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003146bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003147 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003148 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003149 return true;
3150 if (Encoding == dwarf::DW_EH_PE_omit)
3151 return false;
3152
3153 if (!isValidEncoding(Encoding))
3154 return TokError("unsupported encoding.");
3155
3156 if (getLexer().isNot(AsmToken::Comma))
3157 return TokError("unexpected token in directive");
3158 Lex();
3159
3160 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003161 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003162 return TokError("expected identifier in directive");
3163
3164 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3165
3166 if (IsPersonality)
3167 getStreamer().EmitCFIPersonality(Sym, Encoding);
3168 else
3169 getStreamer().EmitCFILsda(Sym, Encoding);
3170 return false;
3171}
3172
Jim Grosbach4b905842013-09-20 23:08:21 +00003173/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003174/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003175bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003176 getStreamer().EmitCFIRememberState();
3177 return false;
3178}
3179
Jim Grosbach4b905842013-09-20 23:08:21 +00003180/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003181/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003182bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003183 getStreamer().EmitCFIRestoreState();
3184 return false;
3185}
3186
Jim Grosbach4b905842013-09-20 23:08:21 +00003187/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003188/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003189bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003190 int64_t Register = 0;
3191
Jim Grosbach4b905842013-09-20 23:08:21 +00003192 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003193 return true;
3194
3195 getStreamer().EmitCFISameValue(Register);
3196 return false;
3197}
3198
Jim Grosbach4b905842013-09-20 23:08:21 +00003199/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003200/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003201bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003202 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003203 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003204 return true;
3205
3206 getStreamer().EmitCFIRestore(Register);
3207 return false;
3208}
3209
Jim Grosbach4b905842013-09-20 23:08:21 +00003210/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003211/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003212bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003213 std::string Values;
3214 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003215 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003216 return true;
3217
3218 Values.push_back((uint8_t)CurrValue);
3219
3220 while (getLexer().is(AsmToken::Comma)) {
3221 Lex();
3222
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003223 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003224 return true;
3225
3226 Values.push_back((uint8_t)CurrValue);
3227 }
3228
3229 getStreamer().EmitCFIEscape(Values);
3230 return false;
3231}
3232
Jim Grosbach4b905842013-09-20 23:08:21 +00003233/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003234/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003235bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003236 if (getLexer().isNot(AsmToken::EndOfStatement))
3237 return Error(getLexer().getLoc(),
3238 "unexpected token in '.cfi_signal_frame'");
3239
3240 getStreamer().EmitCFISignalFrame();
3241 return false;
3242}
3243
Jim Grosbach4b905842013-09-20 23:08:21 +00003244/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003245/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003246bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003247 int64_t Register = 0;
3248
Jim Grosbach4b905842013-09-20 23:08:21 +00003249 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003250 return true;
3251
3252 getStreamer().EmitCFIUndefined(Register);
3253 return false;
3254}
3255
Jim Grosbach4b905842013-09-20 23:08:21 +00003256/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003257/// ::= .macros_on
3258/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003259bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003260 if (getLexer().isNot(AsmToken::EndOfStatement))
3261 return Error(getLexer().getLoc(),
3262 "unexpected token in '" + Directive + "' directive");
3263
Jim Grosbach4b905842013-09-20 23:08:21 +00003264 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003265 return false;
3266}
3267
Jim Grosbach4b905842013-09-20 23:08:21 +00003268/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003269/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003270bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003271 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003272 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003273 return TokError("expected identifier in '.macro' directive");
3274
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003275 if (getLexer().is(AsmToken::Comma))
3276 Lex();
3277
Eli Bendersky17233942013-01-15 22:59:42 +00003278 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003279 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003280
3281 if (Parameters.size() && Parameters.back().Vararg)
3282 return Error(Lexer.getLoc(),
3283 "Vararg parameter '" + Parameters.back().Name +
3284 "' should be last one in the list of parameters.");
3285
David Majnemer91fc4c22014-01-29 18:57:46 +00003286 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003287 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003288 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003289
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003290 if (Lexer.is(AsmToken::Colon)) {
3291 Lex(); // consume ':'
3292
3293 SMLoc QualLoc;
3294 StringRef Qualifier;
3295
3296 QualLoc = Lexer.getLoc();
3297 if (parseIdentifier(Qualifier))
3298 return Error(QualLoc, "missing parameter qualifier for "
3299 "'" + Parameter.Name + "' in macro '" + Name + "'");
3300
3301 if (Qualifier == "req")
3302 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003303 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003304 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003305 else
3306 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3307 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3308 }
3309
David Majnemer91fc4c22014-01-29 18:57:46 +00003310 if (getLexer().is(AsmToken::Equal)) {
3311 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003312
3313 SMLoc ParamLoc;
3314
3315 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003316 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003317 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003318
3319 if (Parameter.Required)
3320 Warning(ParamLoc, "pointless default value for required parameter "
3321 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003322 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003323
3324 Parameters.push_back(Parameter);
3325
3326 if (getLexer().is(AsmToken::Comma))
3327 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003328 }
3329
3330 // Eat the end of statement.
3331 Lex();
3332
3333 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003334 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003335
3336 // Lex the macro definition.
3337 for (;;) {
3338 // Check whether we have reached the end of the file.
3339 if (getLexer().is(AsmToken::Eof))
3340 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3341
3342 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003343 if (getLexer().is(AsmToken::Identifier)) {
3344 if (getTok().getIdentifier() == ".endm" ||
3345 getTok().getIdentifier() == ".endmacro") {
3346 if (MacroDepth == 0) { // Outermost macro.
3347 EndToken = getTok();
3348 Lex();
3349 if (getLexer().isNot(AsmToken::EndOfStatement))
3350 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3351 "' directive");
3352 break;
3353 } else {
3354 // Otherwise we just found the end of an inner macro.
3355 --MacroDepth;
3356 }
3357 } else if (getTok().getIdentifier() == ".macro") {
3358 // We allow nested macros. Those aren't instantiated until the outermost
3359 // macro is expanded so just ignore them for now.
3360 ++MacroDepth;
3361 }
Eli Bendersky17233942013-01-15 22:59:42 +00003362 }
3363
3364 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003365 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003366 }
3367
Jim Grosbach4b905842013-09-20 23:08:21 +00003368 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003369 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3370 }
3371
3372 const char *BodyStart = StartToken.getLoc().getPointer();
3373 const char *BodyEnd = EndToken.getLoc().getPointer();
3374 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003375 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3376 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003377 return false;
3378}
3379
Jim Grosbach4b905842013-09-20 23:08:21 +00003380/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003381///
3382/// With the support added for named parameters there may be code out there that
3383/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003384/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003385/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003386/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003387/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3388/// warning that the positional parameter found in body which have no effect.
3389/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003390/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003391/// intended or change the macro to use the named parameters. It is possible
3392/// this warning will trigger when the none of the named parameters are used
3393/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003394void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003395 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003396 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003397 // If this macro is not defined with named parameters the warning we are
3398 // checking for here doesn't apply.
3399 unsigned NParameters = Parameters.size();
3400 if (NParameters == 0)
3401 return;
3402
3403 bool NamedParametersFound = false;
3404 bool PositionalParametersFound = false;
3405
3406 // Look at the body of the macro for use of both the named parameters and what
3407 // are likely to be positional parameters. This is what expandMacro() is
3408 // doing when it finds the parameters in the body.
3409 while (!Body.empty()) {
3410 // Scan for the next possible parameter.
3411 std::size_t End = Body.size(), Pos = 0;
3412 for (; Pos != End; ++Pos) {
3413 // Check for a substitution or escape.
3414 // This macro is defined with parameters, look for \foo, \bar, etc.
3415 if (Body[Pos] == '\\' && Pos + 1 != End)
3416 break;
3417
3418 // This macro should have parameters, but look for $0, $1, ..., $n too.
3419 if (Body[Pos] != '$' || Pos + 1 == End)
3420 continue;
3421 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003422 if (Next == '$' || Next == 'n' ||
3423 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003424 break;
3425 }
3426
3427 // Check if we reached the end.
3428 if (Pos == End)
3429 break;
3430
3431 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003432 switch (Body[Pos + 1]) {
3433 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003434 case '$':
3435 break;
3436
Jim Grosbach4b905842013-09-20 23:08:21 +00003437 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003438 case 'n':
3439 PositionalParametersFound = true;
3440 break;
3441
Jim Grosbach4b905842013-09-20 23:08:21 +00003442 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003443 default: {
3444 PositionalParametersFound = true;
3445 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003446 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003447 }
3448 Pos += 2;
3449 } else {
3450 unsigned I = Pos + 1;
3451 while (isIdentifierChar(Body[I]) && I + 1 != End)
3452 ++I;
3453
Jim Grosbach4b905842013-09-20 23:08:21 +00003454 const char *Begin = Body.data() + Pos + 1;
3455 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003456 unsigned Index = 0;
3457 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003458 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003459 break;
3460
3461 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003462 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3463 Pos += 3;
3464 else {
3465 Pos = I;
3466 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003467 } else {
3468 NamedParametersFound = true;
3469 Pos += 1 + Argument.size();
3470 }
3471 }
3472 // Update the scan point.
3473 Body = Body.substr(Pos);
3474 }
3475
3476 if (!NamedParametersFound && PositionalParametersFound)
3477 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3478 "used in macro body, possible positional parameter "
3479 "found in body which will have no effect");
3480}
3481
Nico Weber155dccd12014-07-24 17:08:39 +00003482/// parseDirectiveExitMacro
3483/// ::= .exitm
3484bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3485 if (getLexer().isNot(AsmToken::EndOfStatement))
3486 return TokError("unexpected token in '" + Directive + "' directive");
3487
3488 if (!isInsideMacroInstantiation())
3489 return TokError("unexpected '" + Directive + "' in file, "
3490 "no current macro definition");
3491
3492 // Exit all conditionals that are active in the current macro.
3493 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3494 TheCondState = TheCondStack.back();
3495 TheCondStack.pop_back();
3496 }
3497
3498 handleMacroExit();
3499 return false;
3500}
3501
Jim Grosbach4b905842013-09-20 23:08:21 +00003502/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003503/// ::= .endm
3504/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003505bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003506 if (getLexer().isNot(AsmToken::EndOfStatement))
3507 return TokError("unexpected token in '" + Directive + "' directive");
3508
3509 // If we are inside a macro instantiation, terminate the current
3510 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003511 if (isInsideMacroInstantiation()) {
3512 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003513 return false;
3514 }
3515
3516 // Otherwise, this .endmacro is a stray entry in the file; well formed
3517 // .endmacro directives are handled during the macro definition parsing.
3518 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003519 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003520}
3521
Jim Grosbach4b905842013-09-20 23:08:21 +00003522/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003523/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003524bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003525 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003526 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003527 return TokError("expected identifier in '.purgem' directive");
3528
3529 if (getLexer().isNot(AsmToken::EndOfStatement))
3530 return TokError("unexpected token in '.purgem' directive");
3531
Jim Grosbach4b905842013-09-20 23:08:21 +00003532 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003533 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3534
Jim Grosbach4b905842013-09-20 23:08:21 +00003535 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003536 return false;
3537}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003538
Jim Grosbach4b905842013-09-20 23:08:21 +00003539/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003540/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003541bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003542 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003543
3544 // Expect a single argument: an expression that evaluates to a constant
3545 // in the inclusive range 0-30.
3546 SMLoc ExprLoc = getLexer().getLoc();
3547 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003548 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003549 return true;
3550 else if (getLexer().isNot(AsmToken::EndOfStatement))
3551 return TokError("unexpected token after expression in"
3552 " '.bundle_align_mode' directive");
3553 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3554 return Error(ExprLoc,
3555 "invalid bundle alignment size (expected between 0 and 30)");
3556
3557 Lex();
3558
3559 // Because of AlignSizePow2's verified range we can safely truncate it to
3560 // unsigned.
3561 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3562 return false;
3563}
3564
Jim Grosbach4b905842013-09-20 23:08:21 +00003565/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003566/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003567bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003568 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003569 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003570
Eli Bendersky802b6282013-01-07 21:51:08 +00003571 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3572 StringRef Option;
3573 SMLoc Loc = getTok().getLoc();
3574 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003575 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003576
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003577 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003578 return Error(Loc, kInvalidOptionError);
3579
3580 if (Option != "align_to_end")
3581 return Error(Loc, kInvalidOptionError);
3582 else if (getLexer().isNot(AsmToken::EndOfStatement))
3583 return Error(Loc,
3584 "unexpected token after '.bundle_lock' directive option");
3585 AlignToEnd = true;
3586 }
3587
Eli Benderskyf483ff92012-12-20 19:05:53 +00003588 Lex();
3589
Eli Bendersky802b6282013-01-07 21:51:08 +00003590 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003591 return false;
3592}
3593
Jim Grosbach4b905842013-09-20 23:08:21 +00003594/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003595/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003596bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003597 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003598
3599 if (getLexer().isNot(AsmToken::EndOfStatement))
3600 return TokError("unexpected token in '.bundle_unlock' directive");
3601 Lex();
3602
3603 getStreamer().EmitBundleUnlock();
3604 return false;
3605}
3606
Jim Grosbach4b905842013-09-20 23:08:21 +00003607/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003608/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003609bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003610 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003611
3612 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003613 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003614 return true;
3615
3616 int64_t FillExpr = 0;
3617 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3618 if (getLexer().isNot(AsmToken::Comma))
3619 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3620 Lex();
3621
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003622 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003623 return true;
3624
3625 if (getLexer().isNot(AsmToken::EndOfStatement))
3626 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3627 }
3628
3629 Lex();
3630
3631 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003632 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3633 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003634
3635 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003636 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003637
3638 return false;
3639}
3640
Jim Grosbach4b905842013-09-20 23:08:21 +00003641/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003642/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003643bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003644 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003645 const MCExpr *Value;
3646
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003647 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003648 return true;
3649
3650 if (getLexer().isNot(AsmToken::EndOfStatement))
3651 return TokError("unexpected token in directive");
3652
3653 if (Signed)
3654 getStreamer().EmitSLEB128Value(Value);
3655 else
3656 getStreamer().EmitULEB128Value(Value);
3657
3658 return false;
3659}
3660
Jim Grosbach4b905842013-09-20 23:08:21 +00003661/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003662/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003663bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003664 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003665 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003666 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003667 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003668
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003669 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003670 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003671
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003672 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003673
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003674 // Assembler local symbols don't make any sense here. Complain loudly.
3675 if (Sym->isTemporary())
3676 return Error(Loc, "non-local symbol required in directive");
3677
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003678 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3679 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003680
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003681 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003682 break;
3683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003684 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003685 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003686 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003687 }
3688 }
3689
Sean Callanan686ed8d2010-01-19 20:22:31 +00003690 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003691 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003692}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003693
Jim Grosbach4b905842013-09-20 23:08:21 +00003694/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003695/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003696bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003697 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003698
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003699 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003700 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003701 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003702 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003703
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003704 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003705 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003706
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003707 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003709 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003710
3711 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003712 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003713 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003714 return true;
3715
3716 int64_t Pow2Alignment = 0;
3717 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003718 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003719 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003720 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003721 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003722 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003723
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003724 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3725 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003726 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3727
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003728 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003729 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3730 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003731 if (!isPowerOf2_64(Pow2Alignment))
3732 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3733 Pow2Alignment = Log2_64(Pow2Alignment);
3734 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003735 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003736
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003737 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003738 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003739
Sean Callanan686ed8d2010-01-19 20:22:31 +00003740 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003741
Chris Lattner28ad7542009-07-09 17:25:12 +00003742 // NOTE: a size of zero for a .comm should create a undefined symbol
3743 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003744 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003745 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003746 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003747
Eric Christopherbc818852010-05-14 01:38:54 +00003748 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003749 // may internally end up wanting an alignment in bytes.
3750 // FIXME: Diagnose overflow.
3751 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003752 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003753 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003754
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003755 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003756 return Error(IDLoc, "invalid symbol redefinition");
3757
Chris Lattner28ad7542009-07-09 17:25:12 +00003758 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003759 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003760 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003761 return false;
3762 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003763
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003764 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003765 return false;
3766}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003767
Jim Grosbach4b905842013-09-20 23:08:21 +00003768/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003769/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003770bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003771 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003772 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003773
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003774 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003775 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003776 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003777
Sean Callanan686ed8d2010-01-19 20:22:31 +00003778 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003779
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003780 if (Str.empty())
3781 Error(Loc, ".abort detected. Assembly stopping.");
3782 else
3783 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003784 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003785
3786 return false;
3787}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003788
Jim Grosbach4b905842013-09-20 23:08:21 +00003789/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003790/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003791bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003792 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003793 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003794
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003795 // Allow the strings to have escaped octal character sequence.
3796 std::string Filename;
3797 if (parseEscapedString(Filename))
3798 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003799 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003800 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003801
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003802 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003803 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003804
Chris Lattner693fbb82009-07-16 06:14:39 +00003805 // Attempt to switch the lexer to the included file before consuming the end
3806 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003807 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003808 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003809 return true;
3810 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003811
3812 return false;
3813}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003814
Jim Grosbach4b905842013-09-20 23:08:21 +00003815/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003816/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003817bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003818 if (getLexer().isNot(AsmToken::String))
3819 return TokError("expected string in '.incbin' directive");
3820
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003821 // Allow the strings to have escaped octal character sequence.
3822 std::string Filename;
3823 if (parseEscapedString(Filename))
3824 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003825 SMLoc IncbinLoc = getLexer().getLoc();
3826 Lex();
3827
3828 if (getLexer().isNot(AsmToken::EndOfStatement))
3829 return TokError("unexpected token in '.incbin' directive");
3830
Kevin Enderby109f25c2011-12-14 21:47:48 +00003831 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003832 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003833 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3834 return true;
3835 }
3836
3837 return false;
3838}
3839
Jim Grosbach4b905842013-09-20 23:08:21 +00003840/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003841/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3842bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003843 TheCondStack.push_back(TheCondState);
3844 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003845 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003846 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003847 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003848 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003849 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003850 return true;
3851
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003852 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003853 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003854
Sean Callanan686ed8d2010-01-19 20:22:31 +00003855 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003856
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003857 switch (DirKind) {
3858 default:
3859 llvm_unreachable("unsupported directive");
3860 case DK_IF:
3861 case DK_IFNE:
3862 break;
3863 case DK_IFEQ:
3864 ExprValue = ExprValue == 0;
3865 break;
3866 case DK_IFGE:
3867 ExprValue = ExprValue >= 0;
3868 break;
3869 case DK_IFGT:
3870 ExprValue = ExprValue > 0;
3871 break;
3872 case DK_IFLE:
3873 ExprValue = ExprValue <= 0;
3874 break;
3875 case DK_IFLT:
3876 ExprValue = ExprValue < 0;
3877 break;
3878 }
3879
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003880 TheCondState.CondMet = ExprValue;
3881 TheCondState.Ignore = !TheCondState.CondMet;
3882 }
3883
3884 return false;
3885}
3886
Jim Grosbach4b905842013-09-20 23:08:21 +00003887/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003888/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003889bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003890 TheCondStack.push_back(TheCondState);
3891 TheCondState.TheCond = AsmCond::IfCond;
3892
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003893 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003894 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003895 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003896 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003897
3898 if (getLexer().isNot(AsmToken::EndOfStatement))
3899 return TokError("unexpected token in '.ifb' directive");
3900
3901 Lex();
3902
3903 TheCondState.CondMet = ExpectBlank == Str.empty();
3904 TheCondState.Ignore = !TheCondState.CondMet;
3905 }
3906
3907 return false;
3908}
3909
Jim Grosbach4b905842013-09-20 23:08:21 +00003910/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003911/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003912/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003913bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003914 TheCondStack.push_back(TheCondState);
3915 TheCondState.TheCond = AsmCond::IfCond;
3916
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003917 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003918 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003919 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003920 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003921
3922 if (getLexer().isNot(AsmToken::Comma))
3923 return TokError("unexpected token in '.ifc' directive");
3924
3925 Lex();
3926
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003927 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003928
3929 if (getLexer().isNot(AsmToken::EndOfStatement))
3930 return TokError("unexpected token in '.ifc' directive");
3931
3932 Lex();
3933
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003934 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003935 TheCondState.Ignore = !TheCondState.CondMet;
3936 }
3937
3938 return false;
3939}
3940
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003941/// parseDirectiveIfeqs
3942/// ::= .ifeqs string1, string2
3943bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3944 if (Lexer.isNot(AsmToken::String)) {
3945 TokError("expected string parameter for '.ifeqs' directive");
3946 eatToEndOfStatement();
3947 return true;
3948 }
3949
3950 StringRef String1 = getTok().getStringContents();
3951 Lex();
3952
3953 if (Lexer.isNot(AsmToken::Comma)) {
3954 TokError("expected comma after first string for '.ifeqs' directive");
3955 eatToEndOfStatement();
3956 return true;
3957 }
3958
3959 Lex();
3960
3961 if (Lexer.isNot(AsmToken::String)) {
3962 TokError("expected string parameter for '.ifeqs' directive");
3963 eatToEndOfStatement();
3964 return true;
3965 }
3966
3967 StringRef String2 = getTok().getStringContents();
3968 Lex();
3969
3970 TheCondStack.push_back(TheCondState);
3971 TheCondState.TheCond = AsmCond::IfCond;
3972 TheCondState.CondMet = String1 == String2;
3973 TheCondState.Ignore = !TheCondState.CondMet;
3974
3975 return false;
3976}
3977
Jim Grosbach4b905842013-09-20 23:08:21 +00003978/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003979/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003980bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003981 StringRef Name;
3982 TheCondStack.push_back(TheCondState);
3983 TheCondState.TheCond = AsmCond::IfCond;
3984
3985 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003986 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003987 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003988 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003989 return TokError("expected identifier after '.ifdef'");
3990
3991 Lex();
3992
3993 MCSymbol *Sym = getContext().LookupSymbol(Name);
3994
3995 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003996 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003997 else
Craig Topper353eda42014-04-24 06:44:33 +00003998 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003999 TheCondState.Ignore = !TheCondState.CondMet;
4000 }
4001
4002 return false;
4003}
4004
Jim Grosbach4b905842013-09-20 23:08:21 +00004005/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004006/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004007bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008 if (TheCondState.TheCond != AsmCond::IfCond &&
4009 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004010 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4011 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004012 TheCondState.TheCond = AsmCond::ElseIfCond;
4013
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014 bool LastIgnoreState = false;
4015 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004016 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 if (LastIgnoreState || TheCondState.CondMet) {
4018 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004019 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004020 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004021 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004022 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004023 return true;
4024
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004025 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004026 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004027
Sean Callanan686ed8d2010-01-19 20:22:31 +00004028 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004029 TheCondState.CondMet = ExprValue;
4030 TheCondState.Ignore = !TheCondState.CondMet;
4031 }
4032
4033 return false;
4034}
4035
Jim Grosbach4b905842013-09-20 23:08:21 +00004036/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004037/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004038bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004039 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004040 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004041
Sean Callanan686ed8d2010-01-19 20:22:31 +00004042 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004043
4044 if (TheCondState.TheCond != AsmCond::IfCond &&
4045 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004046 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4047 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004048 TheCondState.TheCond = AsmCond::ElseCond;
4049 bool LastIgnoreState = false;
4050 if (!TheCondStack.empty())
4051 LastIgnoreState = TheCondStack.back().Ignore;
4052 if (LastIgnoreState || TheCondState.CondMet)
4053 TheCondState.Ignore = true;
4054 else
4055 TheCondState.Ignore = false;
4056
4057 return false;
4058}
4059
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004060/// parseDirectiveEnd
4061/// ::= .end
4062bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4063 if (getLexer().isNot(AsmToken::EndOfStatement))
4064 return TokError("unexpected token in '.end' directive");
4065
4066 Lex();
4067
4068 while (Lexer.isNot(AsmToken::Eof))
4069 Lex();
4070
4071 return false;
4072}
4073
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004074/// parseDirectiveError
4075/// ::= .err
4076/// ::= .error [string]
4077bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4078 if (!TheCondStack.empty()) {
4079 if (TheCondStack.back().Ignore) {
4080 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004081 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004082 }
4083 }
4084
4085 if (!WithMessage)
4086 return Error(L, ".err encountered");
4087
4088 StringRef Message = ".error directive invoked in source file";
4089 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4090 if (Lexer.isNot(AsmToken::String)) {
4091 TokError(".error argument must be a string");
4092 eatToEndOfStatement();
4093 return true;
4094 }
4095
4096 Message = getTok().getStringContents();
4097 Lex();
4098 }
4099
4100 Error(L, Message);
4101 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004102}
4103
Nico Weber404012b2014-07-24 16:26:06 +00004104/// parseDirectiveWarning
4105/// ::= .warning [string]
4106bool AsmParser::parseDirectiveWarning(SMLoc L) {
4107 if (!TheCondStack.empty()) {
4108 if (TheCondStack.back().Ignore) {
4109 eatToEndOfStatement();
4110 return false;
4111 }
4112 }
4113
4114 StringRef Message = ".warning directive invoked in source file";
4115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4116 if (Lexer.isNot(AsmToken::String)) {
4117 TokError(".warning argument must be a string");
4118 eatToEndOfStatement();
4119 return true;
4120 }
4121
4122 Message = getTok().getStringContents();
4123 Lex();
4124 }
4125
4126 Warning(L, Message);
4127 return false;
4128}
4129
Jim Grosbach4b905842013-09-20 23:08:21 +00004130/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004131/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004132bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004133 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004134 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004135
Sean Callanan686ed8d2010-01-19 20:22:31 +00004136 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004137
Jim Grosbach4b905842013-09-20 23:08:21 +00004138 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004139 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4140 ".else");
4141 if (!TheCondStack.empty()) {
4142 TheCondState = TheCondStack.back();
4143 TheCondStack.pop_back();
4144 }
4145
4146 return false;
4147}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004148
Eli Bendersky17233942013-01-15 22:59:42 +00004149void AsmParser::initializeDirectiveKindMap() {
4150 DirectiveKindMap[".set"] = DK_SET;
4151 DirectiveKindMap[".equ"] = DK_EQU;
4152 DirectiveKindMap[".equiv"] = DK_EQUIV;
4153 DirectiveKindMap[".ascii"] = DK_ASCII;
4154 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4155 DirectiveKindMap[".string"] = DK_STRING;
4156 DirectiveKindMap[".byte"] = DK_BYTE;
4157 DirectiveKindMap[".short"] = DK_SHORT;
4158 DirectiveKindMap[".value"] = DK_VALUE;
4159 DirectiveKindMap[".2byte"] = DK_2BYTE;
4160 DirectiveKindMap[".long"] = DK_LONG;
4161 DirectiveKindMap[".int"] = DK_INT;
4162 DirectiveKindMap[".4byte"] = DK_4BYTE;
4163 DirectiveKindMap[".quad"] = DK_QUAD;
4164 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004165 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004166 DirectiveKindMap[".single"] = DK_SINGLE;
4167 DirectiveKindMap[".float"] = DK_FLOAT;
4168 DirectiveKindMap[".double"] = DK_DOUBLE;
4169 DirectiveKindMap[".align"] = DK_ALIGN;
4170 DirectiveKindMap[".align32"] = DK_ALIGN32;
4171 DirectiveKindMap[".balign"] = DK_BALIGN;
4172 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4173 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4174 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4175 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4176 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4177 DirectiveKindMap[".org"] = DK_ORG;
4178 DirectiveKindMap[".fill"] = DK_FILL;
4179 DirectiveKindMap[".zero"] = DK_ZERO;
4180 DirectiveKindMap[".extern"] = DK_EXTERN;
4181 DirectiveKindMap[".globl"] = DK_GLOBL;
4182 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004183 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4184 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4185 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4186 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4187 DirectiveKindMap[".reference"] = DK_REFERENCE;
4188 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4189 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4190 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4191 DirectiveKindMap[".comm"] = DK_COMM;
4192 DirectiveKindMap[".common"] = DK_COMMON;
4193 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4194 DirectiveKindMap[".abort"] = DK_ABORT;
4195 DirectiveKindMap[".include"] = DK_INCLUDE;
4196 DirectiveKindMap[".incbin"] = DK_INCBIN;
4197 DirectiveKindMap[".code16"] = DK_CODE16;
4198 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4199 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004200 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004201 DirectiveKindMap[".irp"] = DK_IRP;
4202 DirectiveKindMap[".irpc"] = DK_IRPC;
4203 DirectiveKindMap[".endr"] = DK_ENDR;
4204 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4205 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4206 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4207 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004208 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4209 DirectiveKindMap[".ifge"] = DK_IFGE;
4210 DirectiveKindMap[".ifgt"] = DK_IFGT;
4211 DirectiveKindMap[".ifle"] = DK_IFLE;
4212 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004213 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004214 DirectiveKindMap[".ifb"] = DK_IFB;
4215 DirectiveKindMap[".ifnb"] = DK_IFNB;
4216 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004217 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004218 DirectiveKindMap[".ifnc"] = DK_IFNC;
4219 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4220 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4221 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4222 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4223 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004224 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004225 DirectiveKindMap[".endif"] = DK_ENDIF;
4226 DirectiveKindMap[".skip"] = DK_SKIP;
4227 DirectiveKindMap[".space"] = DK_SPACE;
4228 DirectiveKindMap[".file"] = DK_FILE;
4229 DirectiveKindMap[".line"] = DK_LINE;
4230 DirectiveKindMap[".loc"] = DK_LOC;
4231 DirectiveKindMap[".stabs"] = DK_STABS;
4232 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4233 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4234 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4235 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4236 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4237 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4238 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4239 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4240 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4241 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4242 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4243 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4244 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4245 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4246 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4247 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4248 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4249 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4250 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4251 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4252 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004253 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004254 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4255 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4256 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004257 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004258 DirectiveKindMap[".endm"] = DK_ENDM;
4259 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4260 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004261 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004262 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004263 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004264}
4265
Jim Grosbach4b905842013-09-20 23:08:21 +00004266MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004267 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004268
Rafael Espindola34b9c512012-06-03 23:57:14 +00004269 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004270 for (;;) {
4271 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004272 if (getLexer().is(AsmToken::Eof)) {
4273 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004274 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004275 }
4276
Rafael Espindola34b9c512012-06-03 23:57:14 +00004277 if (Lexer.is(AsmToken::Identifier) &&
4278 (getTok().getIdentifier() == ".rept")) {
4279 ++NestLevel;
4280 }
4281
4282 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004283 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004284 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004285 EndToken = getTok();
4286 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004287 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4288 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004289 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004290 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004291 break;
4292 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004293 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004294 }
4295
Rafael Espindola34b9c512012-06-03 23:57:14 +00004296 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004297 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004298 }
4299
4300 const char *BodyStart = StartToken.getLoc().getPointer();
4301 const char *BodyEnd = EndToken.getLoc().getPointer();
4302 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4303
Rafael Espindola34b9c512012-06-03 23:57:14 +00004304 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004305 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004306 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004307}
4308
Jim Grosbach4b905842013-09-20 23:08:21 +00004309void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004310 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004311 OS << ".endr\n";
4312
David Blaikie1961f142014-08-21 20:44:56 +00004313 std::unique_ptr<MemoryBuffer> Instantiation(
4314 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004315
Rafael Espindola34b9c512012-06-03 23:57:14 +00004316 // Create the macro instantiation object and add to the current macro
4317 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00004318 MacroInstantiation *MI =
4319 new MacroInstantiation(DirectiveLoc, CurBuffer, getTok().getLoc(),
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00004320 Instantiation->getBuffer(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004321 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004322
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004324 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004325 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004326 Lex();
4327}
4328
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004329/// parseDirectiveRept
4330/// ::= .rep | .rept count
4331bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004332 const MCExpr *CountExpr;
4333 SMLoc CountLoc = getTok().getLoc();
4334 if (parseExpression(CountExpr))
4335 return true;
4336
Rafael Espindola34b9c512012-06-03 23:57:14 +00004337 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004338 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4339 eatToEndOfStatement();
4340 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4341 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004342
4343 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004344 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004345
4346 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004347 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004348
4349 // Eat the end of statement.
4350 Lex();
4351
4352 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004353 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004354 if (!M)
4355 return true;
4356
4357 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4358 // to hold the macro body with substitutions.
4359 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004360 raw_svector_ostream OS(Buf);
4361 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004362 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004363 return true;
4364 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004365 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004366
4367 return false;
4368}
4369
Jim Grosbach4b905842013-09-20 23:08:21 +00004370/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004371/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004372bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004373 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004374
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004375 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004376 return TokError("expected identifier in '.irp' directive");
4377
Rafael Espindola768b41c2012-06-15 14:02:34 +00004378 if (Lexer.isNot(AsmToken::Comma))
4379 return TokError("expected comma in '.irp' directive");
4380
4381 Lex();
4382
Eli Bendersky38274122013-01-14 23:22:36 +00004383 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004384 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004385 return true;
4386
4387 // Eat the end of statement.
4388 Lex();
4389
4390 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004391 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004392 if (!M)
4393 return true;
4394
4395 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4396 // to hold the macro body with substitutions.
4397 SmallString<256> Buf;
4398 raw_svector_ostream OS(Buf);
4399
Eli Bendersky38274122013-01-14 23:22:36 +00004400 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004401 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004402 return true;
4403 }
4404
Jim Grosbach4b905842013-09-20 23:08:21 +00004405 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004406
4407 return false;
4408}
4409
Jim Grosbach4b905842013-09-20 23:08:21 +00004410/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004411/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004412bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004413 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004414
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004415 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004416 return TokError("expected identifier in '.irpc' directive");
4417
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004418 if (Lexer.isNot(AsmToken::Comma))
4419 return TokError("expected comma in '.irpc' directive");
4420
4421 Lex();
4422
Eli Bendersky38274122013-01-14 23:22:36 +00004423 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004424 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004425 return true;
4426
4427 if (A.size() != 1 || A.front().size() != 1)
4428 return TokError("unexpected token in '.irpc' directive");
4429
4430 // Eat the end of statement.
4431 Lex();
4432
4433 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004434 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004435 if (!M)
4436 return true;
4437
4438 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4439 // to hold the macro body with substitutions.
4440 SmallString<256> Buf;
4441 raw_svector_ostream OS(Buf);
4442
4443 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004444 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004445 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004446 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004447
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004448 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004449 return true;
4450 }
4451
Jim Grosbach4b905842013-09-20 23:08:21 +00004452 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004453
4454 return false;
4455}
4456
Jim Grosbach4b905842013-09-20 23:08:21 +00004457bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004458 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004459 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004460
4461 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004462 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004463 assert(getLexer().is(AsmToken::EndOfStatement));
4464
Jim Grosbach4b905842013-09-20 23:08:21 +00004465 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004466 return false;
4467}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004468
Jim Grosbach4b905842013-09-20 23:08:21 +00004469bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004470 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004471 const MCExpr *Value;
4472 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004473 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004474 return true;
4475 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4476 if (!MCE)
4477 return Error(ExprLoc, "unexpected expression in _emit");
4478 uint64_t IntValue = MCE->getValue();
4479 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4480 return Error(ExprLoc, "literal value out of range for directive");
4481
Chad Rosierc7f552c2013-02-12 21:33:51 +00004482 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4483 return false;
4484}
4485
Jim Grosbach4b905842013-09-20 23:08:21 +00004486bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004487 const MCExpr *Value;
4488 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004489 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004490 return true;
4491 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4492 if (!MCE)
4493 return Error(ExprLoc, "unexpected expression in align");
4494 uint64_t IntValue = MCE->getValue();
4495 if (!isPowerOf2_64(IntValue))
4496 return Error(ExprLoc, "literal value not a power of two greater then zero");
4497
Jim Grosbach4b905842013-09-20 23:08:21 +00004498 Info.AsmRewrites->push_back(
4499 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004500 return false;
4501}
4502
Chad Rosierf43fcf52013-02-13 21:27:17 +00004503// We are comparing pointers, but the pointers are relative to a single string.
4504// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004505static int rewritesSort(const AsmRewrite *AsmRewriteA,
4506 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004507 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4508 return -1;
4509 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4510 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004511
Chad Rosierfce4fab2013-04-08 17:43:47 +00004512 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4513 // rewrite to the same location. Make sure the SizeDirective rewrite is
4514 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4515 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004516 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4517 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004518 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004519
Jim Grosbach4b905842013-09-20 23:08:21 +00004520 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4521 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004522 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004523 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004524}
4525
Jim Grosbach4b905842013-09-20 23:08:21 +00004526bool AsmParser::parseMSInlineAsm(
4527 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4528 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4529 SmallVectorImpl<std::string> &Constraints,
4530 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4531 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004532 SmallVector<void *, 4> InputDecls;
4533 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004534 SmallVector<bool, 4> InputDeclsAddressOf;
4535 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004536 SmallVector<std::string, 4> InputConstraints;
4537 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004538 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004539
Benjamin Kramer1a136112013-02-15 20:37:21 +00004540 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004541
4542 // Prime the lexer.
4543 Lex();
4544
4545 // While we have input, parse each statement.
4546 unsigned InputIdx = 0;
4547 unsigned OutputIdx = 0;
4548 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004549 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004550 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004551 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004552
Chad Rosier149e8e02012-12-12 22:45:52 +00004553 if (Info.ParseError)
4554 return true;
4555
Benjamin Kramer1a136112013-02-15 20:37:21 +00004556 if (Info.Opcode == ~0U)
4557 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004558
Benjamin Kramer1a136112013-02-15 20:37:21 +00004559 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004560
Benjamin Kramer1a136112013-02-15 20:37:21 +00004561 // Build the list of clobbers, outputs and inputs.
4562 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004563 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004564
Benjamin Kramer1a136112013-02-15 20:37:21 +00004565 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004566 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004567 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004568
Benjamin Kramer1a136112013-02-15 20:37:21 +00004569 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004570 if (Operand.isReg() && !Operand.needAddressOf() &&
4571 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004572 unsigned NumDefs = Desc.getNumDefs();
4573 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004574 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4575 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004576 continue;
4577 }
4578
4579 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004580 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004581 if (SymName.empty())
4582 continue;
4583
David Blaikie960ea3f2014-06-08 16:18:35 +00004584 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004585 if (!OpDecl)
4586 continue;
4587
4588 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004589 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004590 if (isOutput) {
4591 ++InputIdx;
4592 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004593 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4594 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004595 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004596 } else {
4597 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004598 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4599 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004600 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004601 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004602 }
Reid Kleckneree088972013-12-10 18:27:32 +00004603
4604 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004605 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4606 Desc.getNumImplicitDefs());
4607 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004608 }
4609
4610 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004611 NumOutputs = OutputDecls.size();
4612 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004613
4614 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004615 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4616 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4617 ClobberRegs.end());
4618 Clobbers.assign(ClobberRegs.size(), std::string());
4619 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4620 raw_string_ostream OS(Clobbers[I]);
4621 IP->printRegName(OS, ClobberRegs[I]);
4622 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004623
4624 // Merge the various outputs and inputs. Output are expected first.
4625 if (NumOutputs || NumInputs) {
4626 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004627 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004628 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004629 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004630 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004631 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004632 }
4633 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004634 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004635 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004636 }
4637 }
4638
4639 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004640 std::string AsmStringIR;
4641 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004642 StringRef ASMString =
4643 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4644 const char *AsmStart = ASMString.begin();
4645 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004646 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004647 for (const AsmRewrite &AR : AsmStrRewrites) {
4648 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004649 if (Kind == AOK_Delete)
4650 continue;
4651
David Majnemer8114c1a2014-06-23 02:17:16 +00004652 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004653 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004654
Chad Rosier120eefd2013-03-19 17:32:17 +00004655 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004656 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004657 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004658
Chad Rosier37e755c2012-10-23 17:43:43 +00004659 // Skip the original expression.
4660 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004661 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004662 continue;
4663 }
4664
Chad Rosierff10ed12013-04-12 16:26:42 +00004665 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004666 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004667 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004668 default:
4669 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004670 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004671 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004672 break;
4673 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004674 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004675 break;
4676 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004677 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004678 break;
4679 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004680 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004681 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004682 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004683 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004684 default: break;
4685 case 8: OS << "byte ptr "; break;
4686 case 16: OS << "word ptr "; break;
4687 case 32: OS << "dword ptr "; break;
4688 case 64: OS << "qword ptr "; break;
4689 case 80: OS << "xword ptr "; break;
4690 case 128: OS << "xmmword ptr "; break;
4691 case 256: OS << "ymmword ptr "; break;
4692 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004693 break;
4694 case AOK_Emit:
4695 OS << ".byte";
4696 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004697 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004698 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004699 OS << ".align " << Val;
4700
4701 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004702 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004703 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4704 break;
4705 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004706 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004707 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004708 OS.flush();
4709 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004710 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004711 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004712 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004714
Chad Rosier8bce6642012-10-18 15:49:34 +00004715 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004716 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004717 }
4718
4719 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004720 if (AsmStart != AsmEnd)
4721 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004722
4723 AsmString = OS.str();
4724 return false;
4725}
4726
Daniel Dunbar01e36072010-07-17 02:26:10 +00004727/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004728MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4729 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004730 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004731}